UNPKG

@trpc/server

Version:

The tRPC server library

95 lines (94 loc) • 3.79 kB
import { An as BaseHandlerOptions, Ti as MaybePromise, rr as inferRouterContext, t as Encoder, ur as CreateContextCallback, zn as AnyRouter } from "./unstable-core-do-not-import-Db2TnFh8.cjs"; import { o as NodeHTTPCreateContextFnOptions } from "./index-Dn1ucZS5.cjs"; import { IncomingMessage } from "http"; import { WebSocket, WebSocketServer } from "ws"; //#region src/adapters/ws.d.ts /** * What `createContext` gets in the WebSocket adapter: the upgrade request * as `req`, the socket as `res`, and `info`. It's called once per * connection. * @see https://trpc.io/docs/server/websockets#creating-a-websocket-server * @public */ type CreateWSSContextFnOptions = NodeHTTPCreateContextFnOptions<IncomingMessage, WebSocket>; /** * The `createContext` function the WebSocket adapter takes. * @public */ type CreateWSSContextFn<TRouter extends AnyRouter> = (opts: CreateWSSContextFnOptions) => MaybePromise<inferRouterContext<TRouter>>; /** The options of `getWSConnectionHandler`. */ type WSConnectionHandlerOptions<TRouter extends AnyRouter> = BaseHandlerOptions<TRouter, IncomingMessage> & CreateContextCallback<inferRouterContext<TRouter>, CreateWSSContextFn<TRouter>>; /** * The options of `applyWSSHandler`. * @see https://trpc.io/docs/server/websockets#creating-a-websocket-server */ type WSSHandlerOptions<TRouter extends AnyRouter> = WSConnectionHandlerOptions<TRouter> & { /** The `ws` server to serve the router on. */ wss: WebSocketServer; /** Only connections whose URL starts with this are served. */ prefix?: string; /** * Sends pings to each client and closes connections that stop * answering. */ keepAlive?: { /** * Whether to send pings. * @default false */ enabled: boolean; /** * How long a connection can be quiet before a ping, in milliseconds. * @default 30_000 */ pingMs?: number; /** * How long to wait for a pong before closing the connection, in * milliseconds. * @default 5_000 */ pongWaitMs?: number; }; /** * Stops answering clients' pings. Meant for tests. * @default false */ dangerouslyDisablePong?: boolean; /** * Encodes and decodes messages, for formats other than JSON. * Experimental: it may change in any release. * @default jsonEncoder */ experimental_encoder?: Encoder; }; /** * Returns a `ws` connection listener that serves a router. `applyWSSHandler` * uses it; call it to attach tRPC to your own WebSocket server. */ declare function getWSConnectionHandler<TRouter extends AnyRouter>(opts: WSSHandlerOptions<TRouter>): (client: WebSocket, req: IncomingMessage) => void; /** * Pings a client after `pingMs` of quiet, and closes the connection if no * message comes within `pongWaitMs`. */ declare function handleKeepAlive(client: WebSocket, pingMs?: number, pongWaitMs?: number): void; /** * Serves a router on a `ws` WebSocket server. Returns * `broadcastReconnectNotification()`, which tells every client to * reconnect, for example before a deploy. * @example * ```ts * const wss = new WebSocketServer({ port: 3001 }); * const handler = applyWSSHandler({ wss, router: appRouter, createContext }); * process.on('SIGTERM', () => { * handler.broadcastReconnectNotification(); * wss.close(); * }); * ``` * @see https://trpc.io/docs/server/websockets#creating-a-websocket-server */ declare function applyWSSHandler<TRouter extends AnyRouter>(opts: WSSHandlerOptions<TRouter>): { broadcastReconnectNotification: () => void; }; //#endregion export { applyWSSHandler as a, WSSHandlerOptions as i, CreateWSSContextFnOptions as n, getWSConnectionHandler as o, WSConnectionHandlerOptions as r, handleKeepAlive as s, CreateWSSContextFn as t }; //# sourceMappingURL=ws-D-DF_k_T.d.cts.map