testing/transports/ws_transport.ts

Cross-process WebSocket transport.

Implements the shared WsClient interface (see testing/transports/ws_client.ts) over a real WebSocket upgrade against a spawned test binary. The cookie captured by the sibling FetchTransport on bootstrap is threaded onto the upgrade request so the WS session authenticates as the same account.

Uses the ws npm package — Node's native WebSocket (from undici) follows the WHATWG spec strictly and doesn't accept custom request headers on construction, so cookie-on-upgrade requires the ws package's headers option. fuz_app declares ws as an optional peerDependency — consumers wiring cross-process tests install it themselves (npm install --save-dev ws).

view source

Declarations
#

5 declarations

create_admitted_ws_transport
#

testing/transports/ws_transport.ts view source

(options: WsTransportOptions): Promise<WsTransportClient> import {create_admitted_ws_transport} from '@fuzdev/fuz_app/testing/transports/ws_transport.js';

Open a real-upgrade WS client and round-trip heartbeat on it, so the backend has admitted the connection by the time this resolves.

Both spines answer the upgrade handshake before the connection is registered and admitted (the credential is re-read in between), and deliver no broadcast or targeted notification to a connection still pending admission. A request is answered only once the connection is admitted, so the round trip is the one signal a client has. Suites that open a socket and then wait for a server-initiated message, or that need connections registered in a known order, open through this.

heartbeat is a protocol action, present on every WS endpoint. Its response frame (id WS_ADMISSION_PROBE_ID) stays in the client's messages.

options

returns

Promise<WsTransportClient>

throws

  • Error - if the upgrade fails, or the heartbeat is refused or times out — the socket is closed first

create_ws_transport
#

testing/transports/ws_transport.ts view source

(options: WsTransportOptions): Promise<WsTransportClient> import {create_ws_transport} from '@fuzdev/fuz_app/testing/transports/ws_transport.js';

Build a real-upgrade WS client pinned to options.base_url + ws_path.

Resolves once the upgrade succeeds and the socket is in OPEN state; rejects if the upgrade is refused (401, allowlist rejection, network failure). Incoming messages are JSON-parsed and pushed onto the messages array; wait_for checks already-received messages first before waiting for new arrivals.

An open socket is not yet an admitted connection: both spines answer the handshake first and admit the connection a credential re-check later, and deliver no server-initiated message to it until then. Requests sent meanwhile are queued and answered, so a suite that only round-trips can use this directly; one that waits for a push (a broadcast, a targeted notification) opens with create_admitted_ws_transport instead.

options

returns

Promise<WsTransportClient>

throws

  • Error - if the upgrade fails (status, network) — the rejection message carries the underlying error so the test surfaces the real cause rather than hanging.

WS_ADMISSION_PROBE_ID
#

WsTransportClient
#

testing/transports/ws_transport.ts view source

WsTransportClient import type {WsTransportClient} from '@fuzdev/fuz_app/testing/transports/ws_transport.js';

A cross-process WsClient with one addition: send_text writes a frame verbatim, for the cases a JSON-serializing send cannot express (a frame that is not JSON at all).

inheritance

extends: WsClient

send_text

Send text as one text frame, unserialized.

type (text: string): Promise<void>

text

type string
returns Promise<void>

throws

  • Error - if called after the socket closed

WsTransportOptions
#

testing/transports/ws_transport.ts view source

WsTransportOptions import type {WsTransportOptions} from '@fuzdev/fuz_app/testing/transports/ws_transport.js';

Construction options for create_ws_transport.

base_url

Base URL the binary is reachable at — e.g. http://localhost:8788. Converted to ws:// for the upgrade.

type string

readonly

ws_path

WebSocket endpoint path on the binary (e.g. /api/ws).

type string

readonly

cookies

Session cookie values (full Set-Cookie strings as FetchTransport.cookies() returns them) threaded onto the upgrade Cookie header. Without these the upgrade is anonymous and per-action auth fails on the first message.

type readonly string[]

readonly

origin?

Origin header for the upgrade. Backends running with ALLOWED_ORIGINS=http://localhost:* accept http://localhost:<port>. Defaults to base_url — acceptable because cross-process tests always run against localhost. null sends no Origin at all, which a daemon-token upgrade needs: both spines discard a daemon token (and a bearer token) presented in a browser context.

type string | null

readonly

default_timeout_ms?

Optional per-call default for wait_for timeouts. Falls back to WS_CLIENT_DEFAULT_TIMEOUT_MS if omitted.

type number

readonly

on_request?

Optional responder for server-initiated requests (the server→client direction ActionPeer adds). Attached before the upgrade so a connect-time or on-demand peer/ping is answered as soon as it arrives. Omit for the default observe-only client — a server-initiated request is then surfaced as a normal message (today's behavior).

type (request: { jsonrpc: "2.0"; id: string | number; method: string; params?: ({} & Record<string, unknown>) | undefined; } & Record<string, unknown>): WsResponderOutcome | Promise<...>

readonly

request

headers?

Extra headers for the upgrade request — e.g. an X-Forwarded-For that a backend trusting the loopback proxy resolves as the connection's client IP, which keys the per-IP action rate limiter for every message on the socket. cookies and origin set their own headers.

type Readonly<Record<string, string>>

readonly

Depends on
#

Imported by
#