import type { ServerRequest, Params } from 'worktop/request'; declare global { const WebSocketPair: { new(): { /** the `client` socket */ 0: WebSocket, /** the `server` socket */ 1: WebSocket, }; }; interface ResponseInit { webSocket?: WebSocket; } } export interface WebSocket { accept(): void; send(message: number | string): void; close(code?: number, reason?: string): void; addEventListener(type: 'error', listener: (this: WebSocket, ev: Event) => any): void; addEventListener(type: 'close', listener: (this: WebSocket, ev: CloseEvent) => any): void; addEventListener(type: 'message', listener: (this: WebSocket, ev: MessageEvent) => any): void; } type Context = Record; export interface Socket { send: WebSocket['send']; close: WebSocket['close']; context: C; event: | { type: 'close' } & CloseEvent | { type: 'message' } & MessageEvent | { type: 'error' } & Event; } export type SocketHandler< P extends Params = Params, C extends Context = Context, > = (req: ServerRequest

, socket: Socket) => Promise|void; /** * Ensure the incoming `Request` can be upgraded to a Websocket connection. * @NOTE This is called automatically within the `listen()` method. */ export function connect

(req: ServerRequest

| Request): Response | void; /** * Establish a Websocket connection. * Attach the `handler` as the 'message' event listener. * @NOTE Invokes the `connect()` middleware automatically. */ export function listen< P extends Params = Params, C extends Context = Context, >(handler: SocketHandler): (req: ServerRequest

) => Response;