import { type WsTimers } from "./frames.js"; /** What `connect` gives a socket once the server accepted it (the 101). */ export interface WebSocketConnection { port: MessagePort; /** The subprotocol the server picked, `""` for none. */ protocol: string; } /** * Opens the transport of one socket: resolves once the server accepted the * upgrade, rejects when it refused or could not be reached. Where the port * comes from — a ServiceWorker, a relay, a handler in the same page — is the * caller's business. */ export type WebSocketConnector = (url: string, protocols: string[]) => Promise; export interface WebSocketClassOptions { /** Ping period, in ms. Pongs missing for twice that close the socket with 1006. Default 10 s. */ heartbeatMs?: number; /** Timer functions; see `WsTimers`. */ timers?: WsTimers; /** How long `close()` waits for the server's echo, in ms. Default 5 s. */ closeTimeoutMs?: number; } type Listener = ((event: Event) => void) | null; /** The instance side of the class `createWebSocketClass` returns: the WHATWG `WebSocket` surface. */ export interface PortWebSocket extends EventTarget { readonly url: string; readonly protocol: string; readonly extensions: string; readonly readyState: number; readonly bufferedAmount: number; binaryType: BinaryType; onopen: Listener; onmessage: ((event: MessageEvent) => void) | null; onerror: Listener; onclose: ((event: CloseEvent) => void) | null; send(data: string | ArrayBufferLike | ArrayBufferView | Blob): void; close(code?: number, reason?: string): void; /** Not in the standard: see `PortSocketCore.whenDrained`. */ whenDrained(limit?: number): Promise; readonly CONNECTING: 0; readonly OPEN: 1; readonly CLOSING: 2; readonly CLOSED: 3; } export interface PortWebSocketClass { new (url: string | URL, protocols?: string | string[]): PortWebSocket; readonly CONNECTING: 0; readonly OPEN: 1; readonly CLOSING: 2; readonly CLOSED: 3; } /** * A WHATWG-compatible `WebSocket` class whose transport is a `MessagePort` * from `connect`. Code written against the browser's `WebSocket` runs on it * unchanged: * * ```ts * const WS = createWebSocketClass(adapter.connectWebSocket); * const ws = new WS(`${baseUrl}chat`, ["v1"]); * ws.onmessage = (event) => console.log(event.data); * ``` * * It validates as the standard does (url, scheme, fragment, subprotocol * tokens and duplicates, close codes and reasons), has `binaryType`, the `on*` * handlers next to `addEventListener`, and the close handshake. * `bufferedAmount` is real: the server acknowledges what it consumed. * * The real `globalThis.WebSocket` is left alone; install the class where you * want it. */ export declare function createWebSocketClass(connect: WebSocketConnector, options?: WebSocketClassOptions): PortWebSocketClass; export {}; //# sourceMappingURL=client.d.ts.map