import { readBoundedResponseJson } from './bounded-response.js'; const MAX_SESSION_RESPONSE_BYTES = 64 * 1024; export async function readTargetSessionJson(response: Response): Promise { return readBoundedResponseJson(response, MAX_SESSION_RESPONSE_BYTES, (kind) => new Error( kind === 'size' ? 'Target session response exceeds the size limit' : 'Target session response is malformed', ), ); } export async function readOAuthError(response: Response): Promise { try { const value = await readTargetSessionJson(response); if (value === null || typeof value !== 'object' || Array.isArray(value)) return null; const record = value as Record; return Object.keys(record).length === 1 && typeof record.error === 'string' ? record.error : null; } catch { return null; } }