$ show node-fetch/node-fetch
node-fetch is a widely used ESM-only JavaScript library that brings the browser Fetch API to Node.js. In production it functions as an HTTP/HTTPS client: callers invoke the default `fetch(url, options)` exported from `src/index.js` and receive a `Response` backed by native Node.js Readable streams. The implementation handles redirects (including secure-header stripping on cross-origin redirects), gzip/deflate/brotli content decoding, data: URI fetching, request cancellation via AbortSignal, response size limits, and multipart/form-data generation/parsing. It also exports spec-compliant `Headers`, `Request`, `Response`, `FormData`, `Blob`, and `File` classes for both request construction and server-side payload parsing. Architecturally, the code is a modular library, not a service: `src/body.js` provides the shared Body mixin; `src/request.js` and `src/response.js` build on it; `src/headers.js` implements the Web IDL Headers interface; `src/index.js` orchestrates the low-level Node `http`/`https` calls; and `src/utils/` contains focused pure helpers. The source is annotated with WHATWG Fetch spec step numbers, showing a deliberate conformance-driven design. Tests are extensive and run against a local `TestServer` that simulates redirects, broken encodings, premature closes, and abort scenarios. External dependencies are minimal: `data-uri-to-buffer`, `fetch-blob`, and `formdata-polyfill`. The package declares Node `^12.20.0 || ^14.13.1 || >=16.0.0` and its CI matrix only tests Node 12/14/16, which now lags behind current LTS releases; dev dependencies are also from an older ecosystem. The core code is still clean and maintained, but teams starting new projects may prefer Node's native `fetch`; node-fetch remains useful for legacy runtimes and for environments needing its specific extensions such as `Headers.raw()` and custom agents.
The LLM identified 3 files worth highlighting for reuse:
Hand-coded streaming multipart/form-data parser using a byte-level state machine with Boyer-Moore-style boundary skipping, lookbehind buffers for false boundary matches, and callback-based header/value/part emission. It has no internal node-fetch coupling (only external Blob/FormData polyfill imports) and could be adapted into a standalone streaming multipart parser.
Complete, spec-referenced implementation of the W3C Referrer Policy algorithms: URL stripping, request referrer determination, trustworthiness checks, and header parsing. It imports only `node:net` and is entirely independent of the rest of node-fetch, making it directly extractable into a standalone utility package.
Spec-compliant Headers class built by extending URLSearchParams and returning a Proxy that validates HTTP header tokens, lowercases header names, and normalizes iteration. It also provides the non-spec `raw()` method and a custom Node.js inspection serializer for `http.request` compatibility. This is a well-isolated, elegant solution for a notoriously fiddly Web IDL interface.