import { TypedEvent } from 'rettime' import type { WebSocketData, WebSocketClientHandle, WebSocketClientConnection, WebSocketServerHandle, WebSocketServerConnection, } from '../interceptors/WebSocket' /** * The connection information. */ export interface WebSocketConnectionInfo { /** * The protocols supported by the WebSocket client. */ protocols: string | Array | undefined } /** * An intercepted WebSocket connection: the client and the server handles. * * @note Typed against the handles, not the connection classes, * so that a connection living anywhere (in this process, in another * runtime, or in a custom implementation) can be given to whoever * consumes intercepted connections (e.g. a handler). */ export interface WebSocketConnectionEventData { /** * The incoming WebSocket client connection. */ client: WebSocketClientHandle /** * The original WebSocket server connection. */ server: WebSocketServerHandle info: WebSocketConnectionInfo } /** * The connection intercepted by the `WebSocketInterceptor`: * both the client and the server live in this process. */ export interface WebSocketInterceptedConnection< Message = WebSocketData, > extends WebSocketConnectionEventData { client: WebSocketClientConnection server: WebSocketServerConnection } export class WebSocketConnectionEvent extends TypedEvent< WebSocketInterceptedConnection, void, 'connection' > implements WebSocketInterceptedConnection { public client: WebSocketClientConnection public server: WebSocketServerConnection public info: WebSocketConnectionInfo constructor(data: WebSocketInterceptedConnection) { super('connection', { data }) this.client = data.client this.server = data.server this.info = data.info } } export type WebSocketEventMap = { connection: WebSocketConnectionEvent & Extension }