UNPKG

@firtoz/socka

Version:

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

47 lines (43 loc) • 2.51 kB
import { Context } from 'hono'; import { b as SockaStrictWebSocketInit, S as SockaWebSocketSession, c as SockaWebSocketInit, a as SockaWebSocketSessionConfigUnion } from '../SockaWebSocketSession-CmEtwVE-.js'; import { WSEvents } from 'hono/ws'; import { WebSocket as WebSocket$1 } from 'ws'; import { S as SockaContractBound } from '../socka-report-error-BALZPjd1.js'; import '@standard-schema/spec'; /** * Build {@link SockaStrictWebSocketInit} from a Hono {@link Context} by synthesizing a * **`Request`** from **`c.req.url`** (method GET; URL matches the incoming upgrade). * * Used when **`sockaHonoNodeWs` / `sockaHonoCloudflare`** omit a custom **`sockaInit`** and * strict upgrade is the default: **`createData`** then always receives * **`init.request`** without you writing **`sockaInit: (ctx) => ({ request: new Request(ctx.req.url) })`** by hand. * * For full fidelity to the original upgrade (headers, method), pass your own **`sockaInit`** * that forwards the real **`Request`** from your runtime instead of this helper. */ declare function sockaHonoStrictInitFromContext(c: Context): SockaStrictWebSocketInit; type SockaHonoNodeWsOptions<TContract extends SockaContractBound, TData> = { /** Shared map; default is a new `Map`. */ sessions?: Map<WebSocket, SockaWebSocketSession<TContract, TData>>; /** * Per-upgrade init for `createData`. When omitted, defaults to * {@link sockaHonoStrictInitFromContext} so `Request` is always available. */ sockaInit?: (c: Context) => SockaWebSocketInit | undefined; /** * Resolve the session map and config from this upgrade’s Hono context (e.g. multi-room * from `c.req.param("roomId")`). When set, overrides the outer `config` / static `sessions` * for each connection. */ resolveScope?: (c: Context) => { sessions: Map<WebSocket, SockaWebSocketSession<TContract, TData>>; config: SockaWebSocketSessionConfigUnion<TContract, TData>; }; }; /** * Returns the callback passed to `upgradeWebSocket` from * {@link https://github.com/honojs/middleware/tree/main/packages/node-ws @hono/node-ws} * `createNodeWebSocket({ app }).upgradeWebSocket`. */ declare function sockaHonoNodeWs<TContract extends SockaContractBound, TData>(config: SockaWebSocketSessionConfigUnion<TContract, TData>, options?: SockaHonoNodeWsOptions<TContract, TData>): (c: Context) => WSEvents<WebSocket$1>; export { type SockaHonoNodeWsOptions, sockaHonoNodeWs, sockaHonoStrictInitFromContext };