import { O as ResolveEditor, n as WorkerRoute } from "../../index-FUN1hqS9.js"; import { i as EditorSession } from "../../types-B8yzm4Q0.js"; import { at as PageId } from "../../index-BtXZuYn4.js"; import { D as EditorRouteEnv, n as DraftSoftLocks } from "../../versions-DF-daHUH.js"; //#region src/core/realtime/index.d.ts /** WS envelope version; bump if the message shape changes (clients check `v`). */ declare const REALTIME_PROTOCOL_VERSION = 1; /** Who is in a session—broadcast for presence. Resolved from the editor session * by the route (see {@link realtimeRoute}), never trusted from the client. Only * the display slice ({@link RealtimePeer}) is fanned out—email/role stay in the * socket's attachment and never leave the DO. */ interface RealtimePeer { id: string; name: string; } /** Held soft-locks: field name → the editor id currently holding it. */ type RealtimeLocks = Record; /** Server → client messages (ADR §4). */ type RealtimeServerMessage = { v: number; t: "welcome"; you: RealtimePeer; peers: RealtimePeer[]; snapshot: Record; locks: RealtimeLocks; } | { v: number; t: "presence"; peers: RealtimePeer[]; } | { v: number; t: "change"; field: string; value: unknown; rev: number; from: string; } | { v: number; t: "ack"; rev: number; } | { v: number; t: "locks"; locks: RealtimeLocks; } | { v: number; t: "pong"; }; /** Client → server messages (ADR §4). `v` is optional/advisory on the way in. */ type RealtimeClientMessage = { v?: number; t: "hello"; } | { v?: number; t: "ping"; } | { v?: number; t: "change"; field: string; value: unknown; rev?: number; } | { v?: number; t: "claim"; field: string; } | { v?: number; t: "release"; field: string; } | { v?: number; t: "bye"; }; /** Which page a session persists to—parsed from the upgrade path, stashed so the * alarm flush (no request in scope) knows where to write. */ interface EditSessionTarget { slug: string; id: PageId; } /** The site-injected coalesced flush. Kept out of this framework-agnostic module * so persistence (D1 / the versioned draft path) stays in the site's DO subclass. */ type EditSessionPersist = (snapshot: Record, editor: EditorSession, target: EditSessionTarget) => void | Promise; interface EditSessionConfig { /** Allowlist—a `change` for any other field is dropped (mirrors the collection * config; the persist path re-validates, so this is a cheap first gate). */ fields: readonly string[]; /** Fields under the rich-text soft-lock (for example, `["body"]`): only the lock holder * may `change` them and their values are NOT fanned out to peers (peers render * them read-only and reload on release), so raw rich-text never crosses sockets. */ lockFields?: readonly string[]; /** Coalesced flush to D1. Omit for a presence-only session (no persistence). */ persist?: EditSessionPersist; /** Alarm cadence, ms. Default {@link DEFAULT_FLUSH_MS} (10 seconds). */ flushMs?: number; /** Injectable clock for tests. Default `Date.now`. */ now?: () => number; } /** The presence message for a set of connected sockets. Exported for tests. */ declare function presenceMessage(sockets: readonly WebSocket[]): RealtimeServerMessage; /** Parse a raw WS frame into a known client message, or `null`. Exported for tests. */ declare function parseClientMessage(raw: string | ArrayBuffer): RealtimeClientMessage | null; /** The hibernatable-WebSocket handlers a site's DO subclass delegates to. */ interface EditSession { fetch(request: Request): Promise; webSocketMessage(ws: WebSocket, message: string | ArrayBuffer): Promise; webSocketClose(ws: WebSocket, code: number, reason: string, wasClean: boolean): Promise; webSocketError(ws: WebSocket, error: unknown): Promise; /** Fires on the coalescing alarm—flushes dirty fields through `config.persist`. */ alarm(): Promise; } /** * Build the per-page edit-session logic over a Durable Object's `ctx` * (`DurableObjectState`): * - **connect** → accept a *hibernatable* socket (`ctx.acceptWebSocket`), attach * the editor identity from the forwarded params, stash the page target, * broadcast presence; * - **`hello`** → reply `welcome` (you + peers + the current field snapshot + * held locks); **`ping`** → `pong`; * - **`change`** → validate the field, record it in `ctx.storage`, `ack` the rev, * broadcast to peers (except lock-guarded fields), and arm the coalescing alarm; * - **`claim`/`release`** → acquire/release a rich-text soft-lock, broadcast locks; * - **`alarm`** → flush the coalesced dirty snapshot through `config.persist` * (the existing `applySaveDraft` path—one write path), re-arm if still dirty; * - **disconnect** → release that editor's locks, re-broadcast presence + locks. * * All authoritative state (fields, rev, locks, target, last writer) lives in * `ctx.storage`, so it survives hibernation—presence itself is rebuilt from * `ctx.getWebSockets()` + each socket's `serializeAttachment`. */ declare function createEditSession(ctx: DurableObjectState, config: EditSessionConfig): EditSession; interface RealtimeRouteConfig { /** Resolve the editor session (site wraps its own auth). */ resolveEditor: ResolveEditor; /** * The DO namespace binding—typically `(env) => env.EDIT_SESSION`. Return * `undefined` (binding not provisioned) and the route answers 503, so realtime * is cleanly absent rather than erroring. */ namespace: (env: Env) => DurableObjectNamespace | undefined; /** Mount base. Default `/api/louise/realtime`. */ path?: string; } /** * Build the realtime upgrade route: `GET /api/louise/realtime/:slug/:id` (a * WebSocket handshake). It guards the upgrade as a same-origin, session-gated * mutation (a browser sends `Origin` on a WS handshake), then forwards it to the * per-page Durable Object (`idFromName(":")`), stamping the * server-resolved editor identity so the DO never trusts the client for presence. * Returns `undefined` for a path it doesn't own so `composeWorker` falls through. */ declare function realtimeRoute(cfg: RealtimeRouteConfig): WorkerRoute; interface RealtimeSoftLocksConfig { /** The same Durable Object namespace {@link realtimeRoute} forwards to. When * it returns `undefined`, there's no session, so no field is locked. */ namespace: (env: Env) => DurableObjectNamespace | undefined; /** The session's `lockFields`, for example, `["body"]`. */ fields: readonly string[]; } /** * The soft-locks a page's realtime session holds, for `versionsRoute`'s * `softLocks` (#572). The session enforces its locks only on its own socket; a * surface whose socket dropped saves through the draft route instead, and * without this, that save could replace a body another editor holds. * * versionsRoute({ * ...pagesDraftDeps, * resolveEditor, * softLocks: realtimeSoftLocks({ namespace: (env) => env.EDIT_SESSION, fields: ["body"] }), * }); * * A save that changes a lockable field makes one request to that page's * Durable Object. Leave `softLocks` out of the deps the session's own * `persist` uses: the session already checks its locks. */ declare function realtimeSoftLocks(cfg: RealtimeSoftLocksConfig): DraftSoftLocks; //#endregion export { EditSession, EditSessionConfig, EditSessionPersist, EditSessionTarget, REALTIME_PROTOCOL_VERSION, RealtimeClientMessage, RealtimeLocks, RealtimePeer, RealtimeRouteConfig, RealtimeServerMessage, RealtimeSoftLocksConfig, createEditSession, parseClientMessage, presenceMessage, realtimeRoute, realtimeSoftLocks };