Wiki
the map of any codebase

Module · source

source Module

The source directory is the root of the Ky library implementation. It contains the runtime feature-detection layer (source/core/constants.ts) and the core request-execution engine (source/core/Ky.ts), along with the surrounding utilities, error types, and type definitions that the public ky entry point composes into the final HTTP client.

The two files surfaced here form the backbone of the runtime: constants.ts defines compile-once feature probes and configuration tables (supported HTTP methods, response shortcut types, retry defaults, option registries), while Ky.ts defines the Ky class that wires those constants together with hooks, timeouts, retries, and schema validation to produce a ResponsePromise.

Feature detection (source/core/constants.ts)

A set of module-level constants probe the host environment exactly once at import time. They are intended to be referenced later by Ky and the body/stream utilities to gate optional functionality.

  • supportsRequestStreams — Performs a non-trivial probe: it constructs a Request with a ReadableStream body and a duplex getter, then checks whether the duplex option was read and that no automatic Content-Type was attached. It also catches the QQBrowser/iOS "unsupported BodyInit type" error and returns false in that case.
  • supportsAbortController — Simple typeof globalThis.AbortController === 'function' check.
  • supportsAbortSignal — Requires both AbortSignal and the static AbortSignal.any method to exist, so it gates use of signal composition rather than just cancellation.
  • supportsResponseStreams — Checks for a global ReadableStream.
  • supportsFormData — Checks for a global FormData.

Request/response configuration tables

  • responseTypes — Maps Ky's response shortcut names to Accept header values: jsonapplication/json, texttext/*, formDatamultipart/form-data, and arrayBuffer/blob/bytes*/*. The comment on bytes notes it is feature-checked at runtime before being exposed (the actual probe lives in test code as typeof Response.prototype.bytes === 'function').
  • requestMethods, responseTypes, validate, retry, kyOptionKeys, requestOptionsRegistry — Static tables describing the supported HTTP verbs, validation defaults, retry defaults, and which option keys belong to Ky vs. the underlying Request.
  • maxSafeTimeout, usualFormBoundarySize, stop — Numeric/sentinel constants used by timeout handling, body size estimation, and the hook short-circuit protocol.

Retry primitives

  • type ForceRetryOptions and class RetryMarker — Declared together in constants.ts. RetryMarker holds an options field set in its constructor and is used as an in-band signal that a hook has requested a retry with overridden options. It pairs with the ForceRetryError imported in Ky.ts.

The Ky class (source/core/Ky.ts)

Ky is the per-request state machine. Each request instantiates one through the static create method (split across four ranges in the source — create (part 1..4) — covering option normalization, request construction, hook invocation, and response handling).

Key instance state:

  • request — The underlying Request object that will be passed to fetch.
  • #abortController — Private controller used to implement timeout and to abort on retry, in combination with the supportsAbortController/supportsAbortSignal probes.
  • #retryCount — Tracks retry attempts against the retry defaults from constants.ts.
  • #input — The original Input (URL/Request/string) the caller supplied.
  • #normalizeSearchParams — Private helper that reconciles searchParams options with any URL query string, using the deletedParametersSymbol imported from utils/merge.js.

Internal helpers in Ky.ts

A cluster of module-private helpers above the class supports request lifecycle concerns:

  • maxErrorResponseBodySize, timedOutResponseData, createTextDecoder — Bound the amount of response body retained on errors and decode timeout placeholder data.
  • prefixUrlRenamedErrorMessage, invalidSchemaMessage — Static error messages thrown for misuse.
  • cloneRetryOptions, cloneSearchParametersForInitHook, cloneInitHookOptions — Defensive copies passed to beforeRequest/beforeRetry hooks so user mutations don't leak into Ky's normalized state.
  • isRequestInstance, isResponseInstance, objectToStringObject.prototype.toString-based type guards that work across realms.
  • validateJsonWithSchema — Bridges Standard Schema (StandardSchemaV1, imported from ../types/standard-schema.js) into the .json() path, raising SchemaValidationError on failure.

Declarations

source/core/constants.ts
constsupportsRequestStreams:4
constsupportsAbortController:34
constsupportsAbortSignal:35
constsupportsResponseStreams:36
constsupportsFormData:37
constrequestMethods:39
constvalidate:41
constresponseTypes:46
constmaxSafeTimeout:58
constusualFormBoundarySize:61
conststop:66
typeForceRetryOptions:71
classRetryMarker:153
fieldoptions:154
methodconstructor:156
constretry:244
constkyOptionKeys:246
constrequestOptionsRegistry:269
source/core/Ky.ts
constmaxErrorResponseBodySize:44
constprefixUrlRenamedErrorMessage:45
consttimedOutResponseData:46
constcreateTextDecoder:48
constinvalidSchemaMessage:60
constcloneRetryOptions:62
constobjectToString:76
constisRequestInstance:78
constisResponseInstance:84
constcloneSearchParametersForInitHook:87
functioncloneInitHookOptions:96
constvalidateJsonWithSchema:112
classKy:142
methodcreate (part 1):143
methodcreate (part 2):205
methodcreate (part 3):267
methodcreate (part 4):329
method#normalizeSearchParams:340
fieldrequest:349
field#abortController:350
field#retryCount:351
field#input:353
field#options:354
field#originalRequest:355
field#userProvidedAbortSignal:356
field#beforeRetryHookErrors:357
field#cachedNormalizedOptions:358
field#startTime:359
field#returnedResponseFromBeforeRetryHook:360
field#responseRequests:361
methodconstructor (part 1):364
methodconstructor (part 2):418
methodconstructor (part 3):472
method#calculateDelay:487
method#calculateRetryDelay:504
method#decorateResponse:592
method#getResponseData:609
method#getErrorDataTimeout:635
method#isJsonContentType:649
method#readResponseText:655
method#parseJson:719
method#cancelBody:740

Cited sources· 5