CodeVetter · Repo Unpack · Data flow
sindresorhus/got
Inputs become normalized Options before native transport options are constructed. Request serializes payloads, prepares generated headers, and reconciles hook mutations before sending data. Incoming messages become decorated responses and either flow outward as stream chunks or accumulate for Promise parsing. Cache adapters, cookie jars, DNS stores, and HTTP/2 pools retain different kinds of state across calls.
- The public factory rejects url properties inside options objects; Options clones defaults, merges overrides, then assigns the URL so explicit searchParams replace URL query parameters.
- _finalizeBody() turns form into URLSearchParams text, JSON into stringifyJson output, and native FormData into a serialized body; generated content-type and known content-length headers accompany those transformations.
- _makeRequest() derives Basic authorization from username/password unless overridden or explicitly omitted, obtains cookie strings from the jar, then awaits beforeRequest hooks before reconciling changed headers and URL boundaries.
- createNativeRequestOptions() maps TLS settings, headers, agent, lookup, family, and createConnection into Node transport fields; Unix URLs become socketPath/path options only when enableUnixSockets permits them.
- _onResponseBase() attaches sanitized final URL, original request URL, redirects, request reference, cache origin, IP, and retry count; final responses are decompressed when enabled, while followed redirect bodies are drained without decompression.
- Promise mode gathers chunks with _setRawBody() into Uint8Array, optionally caches incremental UTF-8 decoding, then parseBody() produces text, JSON, or bytes; stream mode emits response and forwards readable data without that buffered completion gate.
- Cross-origin follow-up options are merged through mergeNextRequestOptions(), which handles explicit body replacement, cookie-jar removal, URL/query overrides, and stripping inherited credentials/body unless replacements were explicitly supplied.
- State retention is delegated: Set-Cookie values are written to the supplied jar in wire order, HTTP representations use the supplied cache adapter, and DnsCache stores resolved entries with clear-version checks that prevent stale asynchronous writes from surviving invalidation.