UNPKG

@credo-ts/core

Version:

<p align="center"> <br /> <img alt="Credo Logo" src="https://github.com/openwallet-foundation/credo-ts/blob/c7886cb8377ceb8ee4efe8d264211e561a75072d/images/credo-logo.png" height="250px" /> </p> <h1 align="center"><b>Credo - Core</b></h1

45 lines (44 loc) • 1.42 kB
//#region src/utils/fetch.ts async function fetchWithTimeout(fetch, url, init) { const abortController = new AbortController(); const timeoutMs = init?.timeoutMs ?? 5e3; const maxResponseBytes = init?.maxResponseBytes; const timeout = setTimeout(() => abortController.abort(), timeoutMs); try { const response = await fetch(url, { ...init, signal: abortController.signal }); if (maxResponseBytes === void 0) return response; const contentLength = response.headers.get("content-length"); if (contentLength && parseInt(contentLength, 10) > maxResponseBytes) { abortController.abort(); throw new Error(`Response too large: ${contentLength} bytes exceeds limit of ${maxResponseBytes}`); } const reader = response.body?.getReader(); if (!reader) return response; let receivedBytes = 0; const chunks = []; while (true) { const { done, value } = await reader.read(); if (done) break; receivedBytes += value.length; if (receivedBytes > maxResponseBytes) { await reader.cancel(); throw new Error(`Response exceeded limit of ${maxResponseBytes} bytes`); } chunks.push(value); } const body = new Blob(chunks); return new Response(body, { status: response.status, statusText: response.statusText, headers: response.headers }); } finally { clearTimeout(timeout); } } //#endregion export { fetchWithTimeout }; //# sourceMappingURL=fetch.mjs.map