/** * The wire protocol of a WebSocket carried over a `MessagePort`. * * A `MessagePort` is a natural carrier for a WebSocket: messages arrive in * order, and strings and transferable `ArrayBuffer`s cross it as they are. The * protocol on top is these frames, the same in both directions: * * - `m` — one message. An `ArrayBuffer` is transferred, never copied. * - `c` — close. The side that closes first sends it; the other answers with * its own `c` (the echo), and both are then closed cleanly. * - `p` — ping, client to server, carrying the client's heartbeat period. * - `q` — pong, server to client. * - `a` — ack: the receiver consumed `n` more bytes of messages. This is the * flow control: what a side has sent and not yet seen acknowledged is its * `bufferedAmount`, and the server end can wait for it to fall * (`whenDrained`). Added in version 2; version 1 had no backpressure. * * HEARTBEAT. A `MessagePort` never reports that the other side died, so the * client pings every `hb` ms and closes with 1006 when pongs stop for `2·hb`; * the server closes with 1006 when pings stop for `3·hb`. That is how a socket * notices a closed tab or a terminated worker. * * Unknown frames are ignored, so a later version can add frames a peer of * this version skips. */ /** The version of this frame protocol. Sent in the upgrade hand-shake. */ export declare const WS_PORT_PROTOCOL_VERSION = 2; /** Default heartbeat period of the client, in ms. */ export declare const DEFAULT_HEARTBEAT_MS = 10000; /** How long a side waits for the echo of its close frame, in ms. */ export declare const DEFAULT_CLOSE_TIMEOUT_MS = 5000; export type WsFrame = { t: "m"; d: string | ArrayBuffer; } | { t: "c"; code: number; reason: string; } | { t: "p"; hb: number; } | { t: "q"; } | { t: "a"; n: number; }; /** * The timer functions a socket uses. On a page's main thread, background * throttling delays timers by up to a minute, which would close healthy * sockets; pass unthrottled ones (worker-timers, say) there. */ export interface WsTimers { setTimeout(fn: () => void, ms: number): unknown; clearTimeout(id: unknown): void; setInterval(fn: () => void, ms: number): unknown; clearInterval(id: unknown): void; } export declare const defaultTimers: WsTimers; /** Recovers a frame from a message's data, or `undefined` for anything else. */ export declare function readFrame(data: unknown): WsFrame | undefined; /** Posts `frame`, transferring a message's `ArrayBuffer`. */ export declare function postFrame(port: MessagePort, frame: WsFrame): void; /** The size a message counts for in `bufferedAmount`: UTF-8 bytes, or the buffer's length. */ export declare function messageSize(data: string | ArrayBuffer): number; /** The UTF-8 length of `text`, without encoding it. */ export declare function utf8Length(text: string): number; //# sourceMappingURL=frames.d.ts.map