Files
hyperframes/packages/cli/src/capture/readBoundedResponse.ts
T
James Russo 1aa5b9e4fa fix(cli): validate captured image and font downloads (#3809)
* fix(cli): validate captured image and font downloads

* fix(cli): bound nested assets and preserve distinct fonts
2026-09-09 07:15:29 -04:00

44 lines
1.4 KiB
TypeScript

export interface DownloadByteBudget {
remainingBytes: number;
}
/** Shared by the font and image passes of one website capture. */
export function createCaptureDownloadBudget(): DownloadByteBudget {
return { remainingBytes: 100 * 1024 * 1024 };
}
/** Read a download without trusting Content-Length or retaining an oversized response. */
export async function readBoundedResponse(
response: Response,
maxBytes: number,
budget?: DownloadByteBudget,
): Promise<Buffer | null> {
if (!response.body) return null;
const reader = response.body.getReader();
let complete = false;
try {
const declared = Number(response.headers.get("content-length"));
if (declared > Math.min(maxBytes, budget?.remainingBytes ?? maxBytes)) return null;
const chunks: Uint8Array[] = [];
let total = 0;
while (true) {
const { done, value } = await reader.read();
if (done) {
complete = true;
return Buffer.concat(chunks, total);
}
if (budget) {
const available = budget.remainingBytes;
budget.remainingBytes = Math.max(0, available - value.byteLength);
if (value.byteLength > available) return null;
}
total += value.byteLength;
if (total > maxBytes) return null;
chunks.push(value);
}
} finally {
if (!complete) await reader.cancel().catch(() => {});
reader.releaseLock();
}
}