import type { PresenceHub, PresenceMember } from './presence'; import type { SyncEngine } from './syncEngine'; import type { FrameSerializer } from '../serializer'; import { type SyncMutationRejection } from '../reconciliation'; /** * Wire protocol for the sync-engine WebSocket. One connection multiplexes many * collection subscriptions, each tagged with a client-chosen `id`. */ /** Client → server. */ export type ClientFrame = { type: 'authenticate'; ticket: string; } | { type: 'subscribe'; id: string; collection: string; params?: unknown; /** * Resume from a point already applied (catch-up instead of snapshot). * * Accepts either: * - `number` (pre-1.18 legacy) — the version of THIS engine instance. * - `string` (1.17.0+ cursor) — an opaque cross-instance resume * cursor returned by the server on prior snapshot/diff/frame. * The client round-trips it unmodified. */ since?: number | string; } | { type: 'unsubscribe'; id: string; } | { type: 'mutate'; mutationId: number; name: string; args?: unknown; /** Stable across reconnect/restart; enables atomic receipt replay. */ operationId?: string; } | { type: 'presence-join'; room: string; memberId: string; state: unknown; } | { type: 'presence-set'; room: string; state: unknown; } | { type: 'presence-leave'; room: string; }; /** One subscription's delta within a {@link ServerFrame} `frame`. */ export type FrameDiff = { id: string; added: T[]; removed: T[]; changed: T[]; }; /** * Server → client. `version` is THIS engine's local change-feed watermark. * `cursor` (1.17.0+) is an opaque cross-instance resume cursor — round-trip * it on `subscribe.since` to resume across cluster shards. */ export type ServerFrame = { type: 'snapshot'; id: string; rows: T[]; version?: number; cursor?: string; } | { type: 'diff'; id: string; added: T[]; removed: T[]; changed: T[]; version?: number; cursor?: string; } | { type: 'frame'; version?: number; cursor?: string; diffs: FrameDiff[]; } | { type: 'presence'; room: string; joined: PresenceMember[]; updated: PresenceMember[]; left: string[]; } | { type: 'error'; id?: string; message: string; } | { type: 'ack'; mutationId: number; operationId?: string; result?: unknown; } | { type: 'reject'; mutationId: number; operationId?: string; message: string; /** Typed in 2.22; absent frames are treated as permanent. */ rejection?: SyncMutationRejection; }; export type SyncConnectionOptions = { engine: SyncEngine; /** Resolved auth context for this connection; passed to every subscribe. */ ctx: unknown; /** * Send a frame to the client (the transport serializes it). May return * a number — by convention `-1` signals transport-layer backpressure (the * value Bun's `ws.send()` returns when the WS buffer is full). The * connection tracks consecutive `-1` returns and surfaces them via * `connection.stats().slowSendsRecent`. Legacy `void`-returning sends * keep working unchanged. * * NOTE: 1.16.0 — `send` receives the typed `ServerFrame`. The connection * does NOT pre-serialize; the WS adapter (`syncSocket`) wraps `send` to * call `serializer.encodeServer(frame)` before `ws.send(...)`. This keeps * the connection layer transport-agnostic. */ send: (frame: ServerFrame) => void | number; /** Optional presence hub; enables the `presence-*` frames (see createPresenceHub). */ presence?: PresenceHub; /** * Wire-format serializer (1.16.0). Defaults to `jsonSerializer` — * the historical JSON-over-WS behavior. Both ends of the connection * MUST use the same serializer; pair this option with the matching * client-side `serializer` to opt into binary frames. */ serializer?: FrameSerializer; }; /** * Connection-level operational counters surfaced via {@link SyncConnection.stats}. * Pair with the WS adapter's own backpressure signal for end-to-end slow-client * detection. */ export type SyncConnectionStats = { /** Active subscriptions on this connection. */ subscriptionCount: number; /** Active presence-room memberships on this connection. */ presenceRoomCount: number; /** Frames successfully sent (non-backpressure return) since the connection opened. */ framesSent: number; /** Consecutive `-1` (backpressure) returns from `send` since the last successful send. */ slowSendsRecent: number; }; export type SyncConnection = { /** Handle one client frame (a parsed object or a raw JSON string). */ handle: (raw: unknown) => Promise; /** Tear down every subscription on this connection (call on socket close). */ close: () => void; /** * Point-in-time connection counters — subscription count, frames sent, and * how many consecutive `send` calls came back with the transport's backpressure * signal. Cheap; safe to call from a metering loop. * * Added in 1.14.0. */ stats: () => SyncConnectionStats; }; /** * The per-connection protocol handler — transport-agnostic glue between a single * client socket and the {@link SyncEngine}. It owns that connection's * subscriptions: a `subscribe` frame authorizes + hydrates and replies with a * `snapshot`, then streams `diff` frames; `unsubscribe`/`close` release views. * * Pure (no WebSocket import) so it can be unit-tested with a fake `send`; the * Elysia `syncSocket` plugin is the thin adapter that feeds it socket events. */ export declare const createSyncConnection: ({ engine, ctx, send: rawSend, presence, serializer }: SyncConnectionOptions) => SyncConnection;