import type { Context, Params } from 'worktop'; import type { Dict, Strict, Promisable } from 'worktop/utils'; export type State = Dict; export interface Socket { send: WebSocket['send']; close: WebSocket['close']; state: S; // todo: not happy w/ name event: | { type: 'open' } & Event | { type: 'close' } & CloseEvent | { type: 'message' } & MessageEvent | { type: 'error' } & Event; } export type SocketHandler< C extends Context = Context, P extends Params = Params, S extends State = State, > = ( request: Request, context: C, // todo: omit socket: Socket ) => Promisable; /** * Establish a Websocket connection. * Attach the `handler` as the 'message' event listener. * @NOTE Invokes the `ws.connect` middleware automatically. */ export function listen< C extends Context = Context, P extends Params = Params, >(handler: SocketHandler): ( request: Request, context: Omit & { params: Strict

; } ) => Response;