@trpc/server
Version:
95 lines (94 loc) • 3.7 kB
text/typescript
import "../index-DVXG_v0W.cjs";
import "../http-11_NyRWk.cjs";
import { An as BaseHandlerOptions, Ti as MaybePromise, n as jsonEncoder, rr as inferRouterContext, t as Encoder, ur as CreateContextCallback, zn as AnyRouter } from "../unstable-core-do-not-import-C8Gy-fzS.cjs";
import { NodeHTTPCreateContextFnOptions } from "./node-http/index.cjs";
import { IncomingMessage } from "http";
import { WebSocket, WebSocketServer } from "ws";
/**
* 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
*/
export type CreateWSSContextFnOptions = NodeHTTPCreateContextFnOptions<IncomingMessage, WebSocket>;
/**
* The `createContext` function the WebSocket adapter takes.
* @public
*/
export type CreateWSSContextFn<TRouter extends AnyRouter> = (opts: CreateWSSContextFnOptions) => MaybePromise<inferRouterContext<TRouter>>;
/** The options of `getWSConnectionHandler`. */
export 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
*/
export 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.
*/
export 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`.
*/
export 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
*/
export declare function applyWSSHandler<TRouter extends AnyRouter>(opts: WSSHandlerOptions<TRouter>): {
broadcastReconnectNotification: () => void;
};
export { type Encoder, jsonEncoder };
//# sourceMappingURL=ws.d.cts.map