UNPKG

@epic-web/test-server

Version:

Utility for creating HTTP and WebSocket servers for testing

56 lines (53 loc) 4.4 kB
import * as stream from 'stream'; import * as ws from 'ws'; import { IncomingMessage } from 'node:http'; import { TestHttpServer } from './http.js'; import 'hono'; import '@hono/node-server'; interface TestWebSocketServerOptions { server: TestHttpServer; pathname?: string; } /** * Attach a disposable WebSocket middleware to the given HTTP test server. * * @example * await using server = await createTestHttpServer() * await using wss = createWebSocketMiddleware({ server }) */ declare function createWebSocketMiddleware(options: TestWebSocketServerOptions): { [Symbol.asyncDispose](): Promise<void>; raw: ws.Server<typeof ws.default, typeof IncomingMessage>; on: { (event: "connection", cb: (this: ws.Server<typeof ws.default, typeof IncomingMessage>, websocket: ws.default, request: IncomingMessage) => void): ws.Server<typeof ws.default, typeof IncomingMessage>; (event: "error", cb: (this: ws.Server<typeof ws.default, typeof IncomingMessage>, error: Error) => void): ws.Server<typeof ws.default, typeof IncomingMessage>; (event: "headers", cb: (this: ws.Server<typeof ws.default, typeof IncomingMessage>, headers: string[], request: IncomingMessage) => void): ws.Server<typeof ws.default, typeof IncomingMessage>; (event: "close" | "listening", cb: (this: ws.Server<typeof ws.default, typeof IncomingMessage>) => void): ws.Server<typeof ws.default, typeof IncomingMessage>; (event: "wsClientError", cb: (this: ws.Server<typeof ws.default, typeof IncomingMessage>, error: Error, socket: stream.Duplex, request: IncomingMessage) => void): ws.Server<typeof ws.default, typeof IncomingMessage>; (event: string | symbol, listener: (this: ws.Server<typeof ws.default, typeof IncomingMessage>, ...args: any[]) => void): ws.Server<typeof ws.default, typeof IncomingMessage>; }; once: { (event: "connection", cb: (this: ws.Server<typeof ws.default, typeof IncomingMessage>, websocket: ws.default, request: IncomingMessage) => void): ws.Server<typeof ws.default, typeof IncomingMessage>; (event: "error", cb: (this: ws.Server<typeof ws.default, typeof IncomingMessage>, error: Error) => void): ws.Server<typeof ws.default, typeof IncomingMessage>; (event: "headers", cb: (this: ws.Server<typeof ws.default, typeof IncomingMessage>, headers: string[], request: IncomingMessage) => void): ws.Server<typeof ws.default, typeof IncomingMessage>; (event: "close" | "listening", cb: (this: ws.Server<typeof ws.default, typeof IncomingMessage>) => void): ws.Server<typeof ws.default, typeof IncomingMessage>; (event: "wsClientError", cb: (this: ws.Server<typeof ws.default, typeof IncomingMessage>, error: Error, socket: stream.Duplex, request: IncomingMessage) => void): ws.Server<typeof ws.default, typeof IncomingMessage>; (event: string | symbol, listener: (this: ws.Server<typeof ws.default, typeof IncomingMessage>, ...args: any[]) => void): ws.Server<typeof ws.default, typeof IncomingMessage>; }; off: { (event: "connection", cb: (this: ws.Server<typeof ws.default, typeof IncomingMessage>, websocket: ws.default, request: IncomingMessage) => void): ws.Server<typeof ws.default, typeof IncomingMessage>; (event: "error", cb: (this: ws.Server<typeof ws.default, typeof IncomingMessage>, error: Error) => void): ws.Server<typeof ws.default, typeof IncomingMessage>; (event: "headers", cb: (this: ws.Server<typeof ws.default, typeof IncomingMessage>, headers: string[], request: IncomingMessage) => void): ws.Server<typeof ws.default, typeof IncomingMessage>; (event: "close" | "listening", cb: (this: ws.Server<typeof ws.default, typeof IncomingMessage>) => void): ws.Server<typeof ws.default, typeof IncomingMessage>; (event: "wsClientError", cb: (this: ws.Server<typeof ws.default, typeof IncomingMessage>, error: Error, socket: stream.Duplex, request: IncomingMessage) => void): ws.Server<typeof ws.default, typeof IncomingMessage>; (event: string | symbol, listener: (this: ws.Server<typeof ws.default, typeof IncomingMessage>, ...args: any[]) => void): ws.Server<typeof ws.default, typeof IncomingMessage>; }; readonly ws: { url(): URL; }; readonly wss: { url(): URL; }; close(): Promise<void>; }; export { type TestWebSocketServerOptions, createWebSocketMiddleware };