net.ts

The net host module: the network as an import rather than a global.

net.fetch is an own frozen closure, so const {fetch} = net works and it can be handed to anything that takes an injected fetch. There's no bare fetch export: the only way to the network is through a handle, and net.narrow({hosts}) makes a narrower one to hand down.

A host is hostname, any port, or hostname:port, normalized through URL, one trailing dot dropped, so a grant is compared as a request's URL will be sent. Redirects are the escape a host check has to close, so a handle follows them itself: a Location is checked before anything is sent to it, and a refused host rejects with not_granted and no request sent. A caller's redirect: 'manual' or 'error' passes through unchanged.

Handles and their prototypes are frozen, null-prototype, and constructing one takes a key only this module holds. A handle's hosts live in private fields, so narrow called on any other object throws TypeError and fetch, a closure, ignores its receiver.

This is the Node implementation, which runs on the Node Gro. Its gaps against the runtime's builtin, which it documents rather than the API weakening to match:

  • The hostname is checked, not the address it connects to, so a granted name resolving to a private range, or rebinding between requests, isn't refused.
  • The globals stay: any module can call the global fetch or import node:http, so a handle bounds only code that uses it, a review signal, not a boundary.
  • The root handle allows every host, where the runtime's holds only the accepted ones.
  • Following relies on Node exposing a manual response's status and Location, where the Fetch standard filters them behind an opaqueredirect.
  • The dispatcher every send names delegates to undici's versioned global dispatcher, read each send, so a proxy from setGlobalDispatcher keeps working, and so can a dispatcher a module installs there to reroute every send. A missing one rejects rather than send past it.
  • Beside native follow, following differs in five ways: a response after a cross-origin hop has type basic, not cors, nor is a no-cors one opaque, as each hop is its own request; a Request with a stream body is buffered and replayed, as its body's source isn't observable, buffering whether or not a redirect comes, so a large upload streams through init, where a stream is refused as native follow refuses one; a request with integrity that's redirected rejects unless every hop's body matches, as the integrity check runs on each hop rather than only the last; a non-HTTP(S) Location is not_granted rather than a TypeError; and a redirect's own Referrer-Policy isn't applied to the next hop.
  • Intrinsics stay pollutable. fetch, Request, Response, Headers, URL, and Object's functions are copied at load, so a fetch replaced before this module loads is the one called, but code that patches their prototypes after this module loads runs inside these ops, and a Response is a real one, so awaiting it adopts a polluted then.
  • Object.prototype stays reachable. undici's init dictionary reads a key the init doesn't own through it, and undici's Client and net.connect read their connection options through it, so a polluted Object.prototype can change the connection, including its destination, through options like socketPath and lookup. That is part of this module being advisory, and no list of polluted keys could be complete. Every send names redirect and the dispatcher, so an ordinary option or a Request can't reroute or follow.
view source

Declarations
#

3 declarations

net
#

net.ts view source

Net import {net} from '@fuzdev/gro/net.js';

The net host module's root handle. Import it where authority enters (a task, a script, an entry module) and hand a narrower net.narrow({hosts}) down.

Net
#

net.ts view source

Net import type {Net} from '@fuzdev/gro/net.js';

The network, reached through a handle bound to its hosts.

fetch

fetch, bound to this handle's hosts: an own frozen closure, so it destructures and hands down as a bare function, and it's assignable to typeof globalThis.fetch. Redirects are followed by this handle, each Location checked before anything is sent to it. A caller's redirect: 'manual' or 'error' passes through unchanged, so a manual caller sees the response and decides.

type (input: string | Request | URL, init?: RequestInit | undefined): Promise<Response>

readonly

input

type string | URL | Request

init?

type RequestInit
optional
returns Promise<Response>

throws

  • NetError - `not_granted`, a host outside this handle's set, or a scheme other than `http:` and `https:`, on the first request or a redirect
  • TypeError - a network failure, an invalid `Location`, more than 20 redirects, or a redirect that can't replay a stream body

narrow

A handle limited to hosts, as env.narrow limits names. It can only shrink: a narrower one holds hostname:port beneath a hostname it's allowed, never the reverse.

type (options: NetNarrowOptions): Net

options

returns Net

throws

  • TypeError - for a host that isn't exact: `*`, a scheme, a path, or a value `URL` refuses
  • NetError - `not_granted`, a host this handle doesn't allow

NetNarrowOptions
#

net.ts view source

NetNarrowOptions import type {NetNarrowOptions} from '@fuzdev/gro/net.js';

The options of narrow.

hosts

The hosts the new handle reaches, each hostname (any port) or hostname:port. An empty list reaches nothing.

type readonly string[]

Depends on
#