Skip to main content

Module fetch

Module fetch 

Source
Expand description

A WHATWG fetch + Headers + Request + Response, so code that expects fetch works. It is a thin surface over the ferrijs-fetch engine, reached through a FetchBackend the host chooses — one HTTP stack, one place the net policy applies.

Web-standard names: Headers, Request, Response are the WHATWG classes. Headers is spec (lowercase + RFC7230 validate, value normalize, , combine, separate set-cookie + getSetCookie, sorted real iterators, forEach). Response / Request are constructible with the spec accessors (status/ok/redirected/ type/bodyUsed/headers/…), single-use bodies (text/json/arrayBuffer), clone(), and static Response.json/error/redirect. fetch(url, { signal }) is wired to AbortController/AbortSignal (see abort): an already-aborted signal rejects before I/O and an in-flight abort drops the request future. Response.body is a WHATWG ReadableStream that pulls chunks live off the socket (the body is NOT buffered) — the same stream object on every access, drained by text()/json()/ arrayBuffer(), and pipeable through a TransformStream into a WritableStream. clone() tees it, so both responses read the full payload off one socket even when nothing has been buffered yet. See streams.

Bodies: fetch(url, { body }), new Request(input, { body }) and new Response(body) all take the same BodyInit union, so all three go through the one “extract a body” step in body_init — never their own subset of the union. Headers is likewise a view over the engine’s ferrijs_fetch::Headers list, so the JS class and the Rust client share one set of header semantics.

Net policy: fetch hands the realm’s container to the engine as the request’s NetGuard, which checks the initial URL and every redirect hop against it. The container only ever narrows, so a check made later in the request is never looser than one made at the call. A refusal is thrown as the PermissionDeniedError it is.

Re-exports§

pub use backend::Client;
pub use backend::FetchBackend;
pub use backend::FetchFuture;
pub use backend::FetchRequest;
pub use ferrijs_fetch as engine;

Modules§

abort
Runtime-side glue over the vendored WHATWG AbortController / AbortSignal (ferrijs_std::abort).
backend
What the fetch global sends through.
body_init
The WHATWG “extract a body” step (Fetch §7.4), in one place.
multipart
The bridge between the JS FormData class and the core multipart wire types.
streams
Native underlying sources for the vendored WHATWG ReadableStream (ferrijs_std::stream_web).

Structs§

FetchExtension
The realm’s fetch as an crate::Extension, over backend.
FetchRequestJs
WHATWG Request (spec subset). Constructible (new Request(input, init?) where input is a URL string or another Request), with url/method/headers/redirect/credentials/bodyUsed accessors and text/json/arrayBuffer/clone. A signal passed in init is carried (as the native abort channel) and forwarded by fetch(request); fetch reads url/method/headers/body/ redirect/credentials/signal off a Request argument.
FetchResponseJs
WHATWG Response (spec subset). Constructible (new Response(body?, init?)), with status/ok/statusText/url/redirected/type/ bodyUsed/headers accessors, text/json/arrayBuffer body readers (single-use: a second read throws, per spec), clone() (throws once the body is used), and static Response.json, Response.error, Response.redirect. This is the global Response (the Playwright page-network Response is no longer a global — it is only ever a return value, matching Playwright itself).
HeadersJs
WHATWG Headers, a view over the core ferrijs_fetch::Headers list: names are lowercased and RFC7230-validated, values are HTTP-whitespace normalized and validated, append combines same-name values with , while set-cookie is kept as separate entries, getSetCookie() returns them all, and iteration is sorted by name. The list semantics live in core (fetch::headers) so this class and the Rust HTTP client cannot drift; only the JS surface is here. keys/values/entries/[Symbol.iterator] return real iterator objects.

Functions§

define_classes
Define the Headers / Request / Response classes on globalThis. Idempotent per realm; install calls it.
function
A fetch function over backend, not installed anywhere: for a host handing a caller an attenuated capability rather than the global.
install
Install globalThis.fetch over backend, with the classes it speaks. A realm’s net grant applies to every request it sends.