/** Bounded quota reads; never include response bodies or credentials in errors. */ export async function fetchJson( url: string, init: RequestInit, ): Promise { const response = await fetch(url, { ...init, redirect: "error", signal: AbortSignal.timeout(15000), }); if (!response.ok) { await response.body?.cancel(); throw new Error(`Quota HTTP ${response.status}`); } if (!response.body) throw new Error("Empty quota response"); const reader = response.body.getReader(); const chunks: Uint8Array[] = []; let size = 0; try { while (true) { const chunk = await reader.read(); if (chunk.done) break; size += chunk.value.byteLength; if (size > 1024 * 1024) { await reader.cancel(); throw new Error("Quota response exceeds 1 MiB"); } chunks.push(chunk.value); } } finally { reader.releaseLock(); } try { return JSON.parse(Buffer.concat(chunks).toString("utf8")) as unknown; } catch { throw new Error("Malformed quota response"); } }