histoire
Version:
A fast story book powered by Vite
27 lines (26 loc) • 945 B
JavaScript
import { Buffer } from 'node:buffer';
/** Download bounded bytes without allowing a response body to outlive timeout/retirement. */
export async function downloadAgentArchive(url, signal) {
const response = await fetch(url, { signal: AbortSignal.any([signal, AbortSignal.timeout(60_000)]) });
if (!response.ok || !response.body)
throw new Error('Cannot download agent archive');
const reader = response.body.getReader();
const chunks = [];
let bytes = 0;
try {
while (true) {
const next = await reader.read();
if (next.done)
break;
bytes += next.value.byteLength;
if (bytes > 512 * 1024 * 1024)
throw new Error('Agent archive too large');
chunks.push(next.value);
}
}
finally {
await reader.cancel().catch(() => { });
}
signal.throwIfAborted();
return Buffer.concat(chunks);
}