UNPKG

@firtoz/socka

Version:

Standard Schema–first WebSocket RPC for TypeScript — Bun, Hono, Node ws, Cloudflare Workers, Durable Objects

64 lines (61 loc) • 3.67 kB
import { ServerWebSocket } from 'bun'; import { S as SockaContractBound, a as SockaWireFormat } from '../socka-report-error-BALZPjd1.js'; import { S as SockaWebSocketSession, a as SockaWebSocketSessionConfigUnion, b as SockaStrictWebSocketInit } from '../SockaWebSocketSession-CmEtwVE-.js'; import '@standard-schema/spec'; /** * Reads the upgrade {@link Request} from Bun **`ServerWebSocket.data`** when your * **`fetch`** handler stored it there (e.g. **`server.upgrade(req, { data: { roomId, request: req } })`**). * * Pair with the default strict upgrade on {@link SockaWebSocketSessionConfig} so * **`createData`** is typed with {@link SockaStrictWebSocketInit} and **`init.request`** * is always defined. If **`request`** is missing from **`data`**, this returns **`undefined`** * and the session constructor will throw — that usually means you forgot * to pass **`request`** on upgrade (or set **`strictUpgradeRequest: false`**). */ declare function sockaBunInitFromWsData(ws: ServerWebSocket<unknown>): SockaStrictWebSocketInit | undefined; /** `ServerWebSocket.data` shape after {@link sockaBunUpgrade}. */ type SockaBunUpgradeData<TExtra> = TExtra & { request: Request; }; /** * Calls {@link Bun.Server.upgrade} with **`request: req`** merged into **`data`**, so * {@link sockaBunInitFromWsData} and the default strict upgrade always see the HTTP * upgrade request (query params, cookies path). */ declare function sockaBunUpgrade<TExtra extends Record<string, unknown>>(server: { upgrade: (req: Request, opts: { data: SockaBunUpgradeData<TExtra>; }) => boolean; }, req: Request, data?: TExtra): boolean; type SockaBunResolveScope<TContract extends SockaContractBound, TData, TWsData = undefined> = (ws: ServerWebSocket<TWsData>) => { sessionMap: Map<WebSocket, SockaWebSocketSession<TContract, TData>>; config: SockaWebSocketSessionConfigUnion<TContract, TData>; }; type SockaBunWebSocketHandlers<TContract extends SockaContractBound, TData, TWsData = undefined> = { sessionMap: Map<WebSocket, SockaWebSocketSession<TContract, TData>>; /** Pass into `Bun.serve({ ..., websocket })`. */ websocket: { open: (ws: ServerWebSocket<TWsData>) => void; message: (ws: ServerWebSocket<TWsData>, message: unknown) => void | Promise<void>; close: (ws: ServerWebSocket<TWsData>) => void | Promise<void>; }; wireFormat: SockaWireFormat; }; /** * WebSocket handlers for `Bun.serve` when using `ServerWebSocket` (no * `addEventListener`). Inbound frames are dispatched with the same logic as * `attachSockaWebSocket`. * * **Single-room:** pass a {@link SockaWebSocketSessionConfig} and optional shared `sessionMap`. * * **Multi-room:** pass `{ resolveScope }` where `resolveScope(ws)` returns the * `sessionMap` and `config` for that socket’s scope (e.g. from `ws.data.roomId`). * The returned `sessionMap` is an empty placeholder; real maps come from `resolveScope`. */ declare function createSockaBunWebSocketHandlers<TContract extends SockaContractBound, TData>(config: SockaWebSocketSessionConfigUnion<TContract, TData>, options?: { sessionMap?: Map<WebSocket, SockaWebSocketSession<TContract, TData>>; }): SockaBunWebSocketHandlers<TContract, TData, undefined>; declare function createSockaBunWebSocketHandlers<TContract extends SockaContractBound, TData, TWsData>(options: { resolveScope: SockaBunResolveScope<TContract, TData, TWsData>; }): SockaBunWebSocketHandlers<TContract, TData, TWsData>; export { type SockaBunResolveScope, type SockaBunUpgradeData, type SockaBunWebSocketHandlers, createSockaBunWebSocketHandlers, sockaBunInitFromWsData, sockaBunUpgrade };