### Run bomb demo example Source: https://github.com/agentscope-ai-java/capped-fetch/blob/master/README.md Runs the bomb demo example script. ```sh node examples/bomb-demo.ts ``` -------------------------------- ### Install capped-fetch with npm Source: https://github.com/agentscope-ai-java/capped-fetch/blob/master/README.md Install the package via npm. Requires Node.js >= 20.6. No runtime dependencies. ```sh npm install capped-fetch ``` -------------------------------- ### Example: Node decodes gzip despite Accept-Encoding: identity Source: https://github.com/agentscope-ai-java/capped-fetch/blob/master/README.md Shows that Node's fetch decodes Content-Encoding: gzip even when the request asks for identity, returning 50 MB from a 50 KB response. This demonstrates why a decompressed-size cap cannot be applied externally. ```text request: accept-encoding: identity response: content-encoding: gzip (50 KB) fetch gives you: 52428800 bytes ``` -------------------------------- ### Build and test distribution Source: https://github.com/agentscope-ai-java/capped-fetch/blob/master/README.md Builds the project and runs the distribution tests. This is what CI runs against Node 20 and 22. ```sh npm run build && npm run test:dist # what CI runs against node 20 and 22 ``` -------------------------------- ### Basic usage of cappedFetch Source: https://github.com/agentscope-ai-java/capped-fetch/blob/master/README.md Import cappedFetch and call it with a URL and options object. The maxBytes option caps the decoded body size, and timeout limits the whole operation. Returns a standard Response. ```ts import { cappedFetch } from 'capped-fetch'; const res = await cappedFetch('https://example.com/data.json', { maxBytes: 10 * 1024 * 1024, // decoded body cap timeout: 30_000, // whole operation, redirects and body included }); const data = await res.json(); ``` -------------------------------- ### Run full test suite with Node test runner Source: https://github.com/agentscope-ai-java/capped-fetch/blob/master/README.md Runs the full test suite using Node's built-in test runner. Requires Node 24+ for type stripping. ```sh node --test "test/*.test.ts" # full suite, needs node 24+ for type stripping ``` -------------------------------- ### Benchmark output: fetch vs cappedFetch on bomb response Source: https://github.com/agentscope-ai-java/capped-fetch/blob/master/README.md Output from running `node examples/bomb-demo.ts`. Shows fetch reading 200 MB and using 642 MB RSS, while cappedFetch refuses with 'Response exceeded maxBytes (10485760 bytes decoded)' and uses only 19 MB. ```text one response: 199 KB on the wire, 200 MB decoded (1029x) fetch 176ms rss + 642 MB read 200 MB cappedFetch 19ms rss + 19 MB refused — Response exceeded maxBytes (10485760 bytes decoded) ``` -------------------------------- ### Handle capped-fetch errors Source: https://github.com/agentscope-ai-java/capped-fetch/blob/master/README.md Catch CompressionBombError and ResponseTooLargeError. CompressionBombError exposes compressedBytes and decompressedBytes; ResponseTooLargeError exposes limit. Both extend CappedFetchError. ```ts import { cappedFetch, CompressionBombError, ResponseTooLargeError } from 'capped-fetch'; try { await (await cappedFetch(url)).text(); } catch (err) { if (err instanceof CompressionBombError) { console.warn(`${err.compressedBytes} bytes inflated to ${err.decompressedBytes}`); } else if (err instanceof ResponseTooLargeError) { console.warn(`over the ${err.limit} byte limit`); } } ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.