@alecsibilia/ui-kit 0.0.0-stage → 0.1.0-alpha.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +7 -0
- package/LICENSE +201 -0
- package/README.md +233 -2
- package/bin/ui-kit.js +12 -0
- package/dist/alchemy/app.d.ts +34 -0
- package/dist/alchemy/app.js +88 -0
- package/dist/alchemy/index.d.ts +11 -0
- package/dist/alchemy/index.js +11 -0
- package/dist/alchemy/live-rooms.d.ts +21 -0
- package/dist/alchemy/live-rooms.js +31 -0
- package/dist/alchemy/migrations.d.ts +28 -0
- package/dist/alchemy/migrations.js +56 -0
- package/dist/alchemy/stage.d.ts +20 -0
- package/dist/alchemy/stage.js +20 -0
- package/dist/alchemy/wrangler-config.d.ts +76 -0
- package/dist/alchemy/wrangler-config.js +82 -0
- package/dist/build/app-modules.d.ts +11 -0
- package/dist/build/app-modules.js +40 -0
- package/dist/build/check.d.ts +4 -0
- package/dist/build/check.js +170 -0
- package/dist/build/cli.d.ts +1 -0
- package/dist/build/cli.js +79 -0
- package/dist/build/feature.d.ts +8 -0
- package/dist/build/feature.js +24 -0
- package/dist/build/index.d.ts +13 -0
- package/dist/build/index.js +13 -0
- package/dist/build/measure-cpu.d.ts +2 -0
- package/dist/build/measure-cpu.js +196 -0
- package/dist/build/prerender.d.ts +5 -0
- package/dist/build/prerender.js +109 -0
- package/dist/build/preview.d.ts +7 -0
- package/dist/build/preview.js +41 -0
- package/dist/build/seo-files.d.ts +27 -0
- package/dist/build/seo-files.js +24 -0
- package/dist/client/head-swap.d.ts +45 -0
- package/dist/client/head-swap.js +183 -0
- package/dist/client/hydration-pass.d.ts +56 -0
- package/dist/client/hydration-pass.js +186 -0
- package/dist/client/hydration-reset.d.ts +82 -0
- package/dist/client/hydration-reset.js +154 -0
- package/dist/client/index.d.ts +37 -0
- package/dist/client/index.js +65 -0
- package/dist/client/nav-client.d.ts +35 -0
- package/dist/client/nav-client.js +371 -0
- package/dist/live/collab/doc-provider.d.ts +74 -0
- package/dist/live/collab/doc-provider.js +412 -0
- package/dist/live/collab/index.d.ts +51 -0
- package/dist/live/collab/index.jsx +142 -0
- package/dist/live/collab-status.d.ts +25 -0
- package/dist/live/collab-status.js +17 -0
- package/dist/live/cursors/cursor-engine.d.ts +57 -0
- package/dist/live/cursors/cursor-engine.js +243 -0
- package/dist/live/cursors/index.d.ts +46 -0
- package/dist/live/cursors/index.jsx +110 -0
- package/dist/live/declaration.d.ts +113 -0
- package/dist/live/declaration.js +94 -0
- package/dist/live/index.d.ts +8 -0
- package/dist/live/index.js +8 -0
- package/dist/live/protocol.d.ts +224 -0
- package/dist/live/protocol.js +272 -0
- package/dist/live/server/doc-store.d.ts +68 -0
- package/dist/live/server/doc-store.js +187 -0
- package/dist/live/server/index.d.ts +60 -0
- package/dist/live/server/index.js +77 -0
- package/dist/live/server/room-presence.d.ts +24 -0
- package/dist/live/server/room-presence.js +123 -0
- package/dist/live/server/room-sockets.d.ts +65 -0
- package/dist/live/server/room-sockets.js +97 -0
- package/dist/live/server/room.d.ts +20 -0
- package/dist/live/server/room.js +348 -0
- package/dist/live/server/route.d.ts +56 -0
- package/dist/live/server/route.js +159 -0
- package/dist/live/server/types.d.ts +163 -0
- package/dist/live/server/types.js +2 -0
- package/dist/logic/agent-actions.d.ts +133 -0
- package/dist/logic/agent-actions.js +150 -0
- package/dist/logic/command-types.d.ts +53 -0
- package/dist/logic/command-types.js +0 -0
- package/dist/logic/dev.d.ts +9 -0
- package/dist/logic/dev.js +10 -0
- package/dist/logic/draft-rebase.d.ts +59 -0
- package/dist/logic/draft-rebase.js +71 -0
- package/dist/logic/form-state.d.ts +105 -0
- package/dist/logic/form-state.js +93 -0
- package/dist/logic/head.d.ts +145 -0
- package/dist/logic/head.js +140 -0
- package/dist/logic/index.d.ts +27 -0
- package/dist/logic/index.js +25 -0
- package/dist/logic/island-def.d.ts +89 -0
- package/dist/logic/island-def.js +59 -0
- package/dist/logic/kit-commands.d.ts +104 -0
- package/dist/logic/kit-commands.js +72 -0
- package/dist/logic/kit-errors.d.ts +25 -0
- package/dist/logic/kit-errors.js +6 -0
- package/dist/logic/listen.d.ts +99 -0
- package/dist/logic/listen.js +92 -0
- package/dist/logic/mutation-def.d.ts +65 -0
- package/dist/logic/mutation-def.js +43 -0
- package/dist/logic/navigation-data.d.ts +50 -0
- package/dist/logic/navigation-data.js +55 -0
- package/dist/logic/navigation-policy.d.ts +74 -0
- package/dist/logic/navigation-policy.js +127 -0
- package/dist/logic/page-model-def.d.ts +108 -0
- package/dist/logic/page-model-def.js +31 -0
- package/dist/logic/path.d.ts +31 -0
- package/dist/logic/path.js +48 -0
- package/dist/logic/query-def.d.ts +56 -0
- package/dist/logic/query-def.js +37 -0
- package/dist/logic/query-key.d.ts +24 -0
- package/dist/logic/query-key.js +35 -0
- package/dist/logic/query-types.d.ts +28 -0
- package/dist/logic/query-types.js +2 -0
- package/dist/logic/resources.d.ts +163 -0
- package/dist/logic/resources.js +62 -0
- package/dist/logic/result.d.ts +30 -0
- package/dist/logic/result.js +23 -0
- package/dist/logic/route-model.d.ts +139 -0
- package/dist/logic/route-model.js +207 -0
- package/dist/logic/route-params.d.ts +80 -0
- package/dist/logic/route-params.js +114 -0
- package/dist/logic/route-table.d.ts +308 -0
- package/dist/logic/route-table.js +206 -0
- package/dist/logic/seo.d.ts +61 -0
- package/dist/logic/seo.js +101 -0
- package/dist/logic/site.d.ts +12 -0
- package/dist/logic/site.js +0 -0
- package/dist/logic/subscriptions.d.ts +177 -0
- package/dist/logic/subscriptions.js +152 -0
- package/dist/render/index.d.ts +32 -0
- package/dist/render/index.js +52 -0
- package/dist/runtime/island-wake.d.ts +38 -0
- package/dist/runtime/island-wake.js +147 -0
- package/dist/runtime/query-scope.d.ts +224 -0
- package/dist/runtime/query-scope.js +222 -0
- package/dist/runtime/room-hub.d.ts +107 -0
- package/dist/runtime/room-hub.js +307 -0
- package/dist/runtime/subscription-runner.d.ts +71 -0
- package/dist/runtime/subscription-runner.js +240 -0
- package/dist/server/bindings.d.ts +41 -0
- package/dist/server/bindings.js +74 -0
- package/dist/server/browser.d.ts +9 -0
- package/dist/server/browser.js +9 -0
- package/dist/server/edge-cache.d.ts +28 -0
- package/dist/server/edge-cache.js +35 -0
- package/dist/server/errors.d.ts +45 -0
- package/dist/server/errors.js +36 -0
- package/dist/server/form-actions-setup.d.ts +1 -0
- package/dist/server/form-actions-setup.js +15 -0
- package/dist/server/form-actions.d.ts +121 -0
- package/dist/server/form-actions.js +211 -0
- package/dist/server/index.d.ts +36 -0
- package/dist/server/index.js +35 -0
- package/dist/server/inputs.d.ts +45 -0
- package/dist/server/inputs.js +77 -0
- package/dist/server/rate-limit.d.ts +29 -0
- package/dist/server/rate-limit.js +32 -0
- package/dist/server/read-cache.d.ts +74 -0
- package/dist/server/read-cache.js +58 -0
- package/dist/server/request-info.d.ts +17 -0
- package/dist/server/request-info.js +11 -0
- package/dist/server/request.d.ts +52 -0
- package/dist/server/request.js +73 -0
- package/dist/server/response-cookies.d.ts +23 -0
- package/dist/server/response-cookies.js +24 -0
- package/dist/server/run-effect.d.ts +42 -0
- package/dist/server/run-effect.js +92 -0
- package/dist/server/run.d.ts +63 -0
- package/dist/server/run.js +66 -0
- package/dist/server/session.d.ts +125 -0
- package/dist/server/session.js +247 -0
- package/dist/test/fake-bindings.d.ts +32 -0
- package/dist/test/fake-bindings.js +108 -0
- package/dist/test/fake-room.d.ts +66 -0
- package/dist/test/fake-room.js +216 -0
- package/dist/test/index.d.ts +19 -0
- package/dist/test/index.js +44 -0
- package/dist/test/probe.d.ts +11 -0
- package/dist/test/probe.js +11 -0
- package/dist/test/solid-jsx.d.ts +23 -0
- package/dist/test/solid-jsx.js +28 -0
- package/dist/test/solid-preload.d.ts +1 -0
- package/dist/test/solid-preload.js +29 -0
- package/dist/ui/code-editor/index.d.ts +21 -0
- package/dist/ui/code-editor/index.jsx +286 -0
- package/dist/ui/collab-text/index.d.ts +29 -0
- package/dist/ui/collab-text/index.jsx +96 -0
- package/dist/ui/components/controls.d.ts +69 -0
- package/dist/ui/components/controls.jsx +81 -0
- package/dist/ui/components/feedback.d.ts +26 -0
- package/dist/ui/components/feedback.jsx +80 -0
- package/dist/ui/components/layout.d.ts +74 -0
- package/dist/ui/components/layout.jsx +146 -0
- package/dist/ui/components/lists.d.ts +69 -0
- package/dist/ui/components/lists.jsx +133 -0
- package/dist/ui/components/navigation.d.ts +73 -0
- package/dist/ui/components/navigation.jsx +112 -0
- package/dist/ui/components/text.d.ts +34 -0
- package/dist/ui/components/text.jsx +163 -0
- package/dist/ui/index.d.ts +7 -0
- package/dist/ui/index.js +7 -0
- package/dist/ui/jsx-runtime.d.ts +19 -0
- package/dist/ui/jsx-runtime.js +0 -0
- package/dist/ui/list-keys.d.ts +31 -0
- package/dist/ui/list-keys.js +45 -0
- package/dist/ui/styles/base.css +38 -0
- package/dist/ui/styles/components.css +608 -0
- package/dist/ui/styles/tokens.css +27 -0
- package/dist/ui/styles/transitions.css +23 -0
- package/dist/ui/styles/workbench.css +428 -0
- package/dist/ui/styles.css +9 -0
- package/dist/view/agent-actions-view.d.ts +30 -0
- package/dist/view/agent-actions-view.js +27 -0
- package/dist/view/agent-surface.d.ts +42 -0
- package/dist/view/agent-surface.js +123 -0
- package/dist/view/app-scope.d.ts +51 -0
- package/dist/view/app-scope.js +75 -0
- package/dist/view/boundary.d.ts +18 -0
- package/dist/view/boundary.jsx +26 -0
- package/dist/view/command-runner.d.ts +16 -0
- package/dist/view/command-runner.js +245 -0
- package/dist/view/form.d.ts +82 -0
- package/dist/view/form.jsx +109 -0
- package/dist/view/head-view.d.ts +31 -0
- package/dist/view/head-view.js +46 -0
- package/dist/view/index.d.ts +41 -0
- package/dist/view/index.js +40 -0
- package/dist/view/island-root.d.ts +30 -0
- package/dist/view/island-root.js +36 -0
- package/dist/view/island.d.ts +102 -0
- package/dist/view/island.jsx +154 -0
- package/dist/view/match.d.ts +41 -0
- package/dist/view/match.js +111 -0
- package/dist/view/model.d.ts +92 -0
- package/dist/view/model.js +162 -0
- package/dist/view/navigation-head.d.ts +29 -0
- package/dist/view/navigation-head.jsx +38 -0
- package/dist/view/page-model.d.ts +46 -0
- package/dist/view/page-model.js +146 -0
- package/dist/view/query-effects.d.ts +87 -0
- package/dist/view/query-effects.js +147 -0
- package/dist/view/query-ok.d.ts +45 -0
- package/dist/view/query-ok.js +96 -0
- package/dist/view/query.d.ts +115 -0
- package/dist/view/query.jsx +179 -0
- package/dist/view/result-view.d.ts +14 -0
- package/dist/view/result-view.jsx +15 -0
- package/dist/view/route-pages.d.ts +43 -0
- package/dist/view/route-pages.js +59 -0
- package/dist/view/router.d.ts +44 -0
- package/dist/view/router.js +123 -0
- package/dist/vite/dev-middleware.d.ts +12 -0
- package/dist/vite/dev-middleware.js +39 -0
- package/dist/vite/dev-objects.d.ts +25 -0
- package/dist/vite/dev-objects.js +127 -0
- package/dist/vite/dev-platform.d.ts +19 -0
- package/dist/vite/dev-platform.js +77 -0
- package/dist/vite/dev-seo-files.d.ts +11 -0
- package/dist/vite/dev-seo-files.js +40 -0
- package/dist/vite/index.d.ts +53 -0
- package/dist/vite/index.js +148 -0
- package/dist/vite/local-config.d.ts +34 -0
- package/dist/vite/local-config.js +72 -0
- package/dist/vite/worker-build.d.ts +24 -0
- package/dist/vite/worker-build.js +84 -0
- package/dist/worker/create-worker.d.ts +52 -0
- package/dist/worker/create-worker.js +90 -0
- package/dist/worker/index.d.ts +12 -0
- package/dist/worker/index.js +12 -0
- package/dist/worker/page-routes.d.ts +88 -0
- package/dist/worker/page-routes.js +100 -0
- package/dist/worker/page-status.d.ts +41 -0
- package/dist/worker/page-status.js +23 -0
- package/dist/worker/request-prelude.d.ts +69 -0
- package/dist/worker/request-prelude.js +107 -0
- package/dist/worker/security.d.ts +76 -0
- package/dist/worker/security.js +137 -0
- package/dist/worker/server-app.d.ts +54 -0
- package/dist/worker/server-app.js +76 -0
- package/guards/browser-only.js +5 -0
- package/guards/node-only.js +6 -0
- package/guards/server-only.js +5 -0
- package/migrations/0001-sessions.sql +22 -0
- package/package.json +233 -5
|
@@ -0,0 +1,224 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Live rooms: the wire protocol and the client contract. Shared by the
|
|
3
|
+
* Worker route, the room Durable Object (`@alecsibilia/ui-kit/live/server`) and the
|
|
4
|
+
* kit's browser parts. No Effect, no Solid, no DOM, no Yjs: safe in any
|
|
5
|
+
* bundle. Full guide: `docs/guides/ui-kit-live-rooms.md`.
|
|
6
|
+
*
|
|
7
|
+
* ## Sockets
|
|
8
|
+
*
|
|
9
|
+
* A room is one Durable Object (`idFromName(roomId)`, `roomId` = `type:key`).
|
|
10
|
+
* A page opens at most:
|
|
11
|
+
* - one **room socket** per room: `GET /live/room/<type>/<key>` (WebSocket).
|
|
12
|
+
* Presence and cursors share it. Text frames, below.
|
|
13
|
+
* - one **doc socket** per collaborative doc it shows:
|
|
14
|
+
* `GET /live/room/<type>/<key>/doc/<doc>`. Binary frames in the
|
|
15
|
+
* y-websocket message format (sync 0, awareness 1, auth 2, query
|
|
16
|
+
* awareness 3), so a stock `y-websocket` `WebsocketProvider` with
|
|
17
|
+
* `serverUrl = <origin>/live/room/<type>/<key>/doc` and `roomname = <doc>`
|
|
18
|
+
* speaks it. The text frame `ping` is answered `pong` (both sockets).
|
|
19
|
+
*
|
|
20
|
+
* Upgrade refusals are plain HTTP: 403 (foreign Origin, or `authorize`
|
|
21
|
+
* said no to `join`), 404 (undeclared type / doc, bad key), 426 (not an
|
|
22
|
+
* upgrade), 429 (connect rate limit), 503 (room full).
|
|
23
|
+
*
|
|
24
|
+
* ## Room socket frames
|
|
25
|
+
*
|
|
26
|
+
* Server → client (JSON text):
|
|
27
|
+
* - `{"t":"welcome","self","room","cursors","access","docs"}` once, first;
|
|
28
|
+
* - `{"t":"presence","peers":[Peer],"tabs":n}` on every join / leave (the
|
|
29
|
+
* whole list; `peers` dedupes a signed-in user's tabs, every anonymous
|
|
30
|
+
* tab is its own `Guest` peer; `tabs` counts sockets);
|
|
31
|
+
* - `{"t":"cursors","c":[[peerId,x,y] | [peerId]]}` at most every
|
|
32
|
+
* {@link CURSOR_FLUSH_MS}, only cursors that moved, never your own;
|
|
33
|
+
* `[peerId]` = that cursor left the surface;
|
|
34
|
+
* - `{"t":"saved","doc","at"}` after a doc's `persist` hook ran (a client
|
|
35
|
+
* may refetch the query that reads the persisted copy).
|
|
36
|
+
*
|
|
37
|
+
* Client → server (text): `c <x> <y>` (x in 1/10000 of the surface width,
|
|
38
|
+
* y in CSS px from its top, integers; see {@link encodeCursor}), `l`
|
|
39
|
+
* (cursor left the surface / tab hidden), `ping`.
|
|
40
|
+
*
|
|
41
|
+
* Close codes the server uses: {@link LIVE_CLOSE}.
|
|
42
|
+
*/
|
|
43
|
+
import { type AnyLiveRoomsDecl, type CursorsOf, type DocNameOf, type DocRoomTypeOf, type RoomTypeOf } from './declaration.ts';
|
|
44
|
+
export * from './declaration.ts';
|
|
45
|
+
/** Where every room socket lives. */
|
|
46
|
+
export declare const LIVE_ROOM_PATH = "/live/room/";
|
|
47
|
+
/** Room keys: URL-safe, short (a plan id, a route name). Checked again on the server. */
|
|
48
|
+
export declare const ROOM_KEY_PATTERN: RegExp;
|
|
49
|
+
/** Peer kinds: a signed-in user (named) or an anonymous visitor (one per tab). */
|
|
50
|
+
export type PeerKind = 'Member' | 'Guest';
|
|
51
|
+
/** One peer in a presence list. */
|
|
52
|
+
export interface Peer {
|
|
53
|
+
/** Stable for the socket's life; the key of its cursor. Not a user id. */
|
|
54
|
+
readonly id: string;
|
|
55
|
+
/** Display name; `"Guest"` for anonymous peers. */
|
|
56
|
+
readonly name: string;
|
|
57
|
+
readonly kind: PeerKind;
|
|
58
|
+
/** A CSS hue 0..359 (stable per user, or per tab for guests). */
|
|
59
|
+
readonly hue: number;
|
|
60
|
+
}
|
|
61
|
+
/** What the viewer may do with the room's docs (from `authorize(user, room, "edit")`). */
|
|
62
|
+
export type RoomAccess = 'Editor' | 'Reader';
|
|
63
|
+
/** A cursor update: `[peerId, x, y]` moved, `[peerId]` left. */
|
|
64
|
+
export type CursorEntry = readonly [string, number, number] | readonly [string];
|
|
65
|
+
export type RoomServerFrame = {
|
|
66
|
+
readonly t: 'welcome';
|
|
67
|
+
/** Your own peer id (skip it in presence lists if you like). */
|
|
68
|
+
readonly self: string;
|
|
69
|
+
readonly room: string;
|
|
70
|
+
/** Whether this room type takes cursors (send none otherwise). */
|
|
71
|
+
readonly cursors: boolean;
|
|
72
|
+
readonly access: RoomAccess;
|
|
73
|
+
/** The room type's doc names. */
|
|
74
|
+
readonly docs: readonly string[];
|
|
75
|
+
} | {
|
|
76
|
+
readonly t: 'presence';
|
|
77
|
+
readonly peers: readonly Peer[];
|
|
78
|
+
readonly tabs: number;
|
|
79
|
+
} | {
|
|
80
|
+
readonly t: 'cursors';
|
|
81
|
+
readonly c: readonly CursorEntry[];
|
|
82
|
+
} | {
|
|
83
|
+
readonly t: 'saved';
|
|
84
|
+
readonly doc: string;
|
|
85
|
+
readonly at: number;
|
|
86
|
+
};
|
|
87
|
+
/** Liveness: answered by the runtime without waking the room. */
|
|
88
|
+
export declare const LIVE_PING: {
|
|
89
|
+
readonly request: 'ping';
|
|
90
|
+
readonly response: 'pong';
|
|
91
|
+
};
|
|
92
|
+
/** Client frame: the cursor left the surface (or the tab was hidden). */
|
|
93
|
+
export declare const CURSOR_LEAVE_FRAME = "l";
|
|
94
|
+
/** Close codes the room sends. */
|
|
95
|
+
export declare const LIVE_CLOSE: {
|
|
96
|
+
/**
|
|
97
|
+
* The room re-checked `authorize(user, room, "edit")` (or the session)
|
|
98
|
+
* and it no longer holds: the session's sockets in that room close.
|
|
99
|
+
* Reconnecting re-identifies (as a guest after sign-out).
|
|
100
|
+
*/
|
|
101
|
+
readonly ACCESS_CHANGED: 4403;
|
|
102
|
+
};
|
|
103
|
+
/**
|
|
104
|
+
* Timings both sides agree on. Server values are enforced; client values
|
|
105
|
+
* are the contract the kit's browser parts follow (`docs/guides/ui-kit-live-rooms.md`).
|
|
106
|
+
*/
|
|
107
|
+
export declare const LIVE_TIMING: {
|
|
108
|
+
/** Client: at most one cursor frame per this many ms (≤ 20/s). */
|
|
109
|
+
readonly CURSOR_SEND_MS: 50;
|
|
110
|
+
/** Server: cursor fan-out batch window. */
|
|
111
|
+
readonly CURSOR_FLUSH_MS: 50;
|
|
112
|
+
/** Client: close a doc socket this long after the tab is hidden (awareness cleared at once). */
|
|
113
|
+
readonly DOC_HIDDEN_CLOSE_MS: 30000;
|
|
114
|
+
/** Client: stop awareness renewals after this long without a local edit or selection change. */
|
|
115
|
+
readonly AWARENESS_IDLE_MS: 30000;
|
|
116
|
+
/** Client: optional `ping` interval for dead-socket detection (auto-answered, no wake). */
|
|
117
|
+
readonly PING_MS: 30000;
|
|
118
|
+
/** Client: reconnect backoff bounds (jittered). */
|
|
119
|
+
readonly RECONNECT_MIN_MS: 1000;
|
|
120
|
+
readonly RECONNECT_MAX_MS: 30000;
|
|
121
|
+
};
|
|
122
|
+
/** Server fan-out batch window for cursors (alias of `LIVE_TIMING.CURSOR_FLUSH_MS`). */
|
|
123
|
+
export declare const CURSOR_FLUSH_MS: 50;
|
|
124
|
+
/** A room id: `type:key`. */
|
|
125
|
+
export declare function roomIdOf(type: string, key: string): string;
|
|
126
|
+
/**
|
|
127
|
+
* A typed reference to one room: what client parts and server hooks pass
|
|
128
|
+
* around. Plain JSON (it goes through island props).
|
|
129
|
+
*
|
|
130
|
+
* `cursors` is the room type's declared capability, as a literal type:
|
|
131
|
+
* `<LiveCursors room>` takes only a {@link CursorRoomRef}, so pointing it at
|
|
132
|
+
* a room type without `cursors: true` is a type error, and the room's
|
|
133
|
+
* `welcome` frame says the same at runtime.
|
|
134
|
+
*/
|
|
135
|
+
export interface RoomRef<T extends string = string, C extends boolean = boolean> {
|
|
136
|
+
readonly type: T;
|
|
137
|
+
readonly key: string;
|
|
138
|
+
/** `type:key` (the Durable Object name). */
|
|
139
|
+
readonly id: string;
|
|
140
|
+
/** The room socket path. */
|
|
141
|
+
readonly path: string;
|
|
142
|
+
/** Whether this room type takes live cursors (from the declaration). */
|
|
143
|
+
readonly cursors: C;
|
|
144
|
+
}
|
|
145
|
+
/** A room whose type declares `cursors: true` (what `<LiveCursors room>` takes). */
|
|
146
|
+
export type CursorRoomRef = RoomRef<string, true>;
|
|
147
|
+
/**
|
|
148
|
+
* A reference to a declared room. The type is checked against the
|
|
149
|
+
* declaration at compile time; the key's shape is checked by the server
|
|
150
|
+
* (a bad key gets a 404 upgrade, never a thrown render).
|
|
151
|
+
*
|
|
152
|
+
* @example
|
|
153
|
+
* ```ts
|
|
154
|
+
* const room = roomRef(LIVE_ROOMS, "plan", planId); // roomRef(LIVE_ROOMS, "nope", x) is a type error
|
|
155
|
+
* room.path; // "/live/room/plan/voltar-main"
|
|
156
|
+
* room.cursors; // true (type: true), from `plan: { cursors: true }`
|
|
157
|
+
* ```
|
|
158
|
+
*/
|
|
159
|
+
export declare function roomRef<D extends AnyLiveRoomsDecl, T extends RoomTypeOf<D>>(decl: D, type: T, key: string): RoomRef<T, CursorsOf<D, T>>;
|
|
160
|
+
/**
|
|
161
|
+
* A reference to one collaborative doc of a room: what the kit's collab
|
|
162
|
+
* part (`<CollabText doc>`) takes. Plain JSON (island props).
|
|
163
|
+
*/
|
|
164
|
+
export interface DocRef {
|
|
165
|
+
readonly room: RoomRef;
|
|
166
|
+
readonly doc: string;
|
|
167
|
+
/** The doc socket path (`<room path>/doc/<doc>`). */
|
|
168
|
+
readonly path: string;
|
|
169
|
+
}
|
|
170
|
+
/**
|
|
171
|
+
* A declared doc of a declared room type. `docRef(LIVE_ROOMS, pageRoom, "notes")`
|
|
172
|
+
* (a type with no docs) and an undeclared doc name are type errors.
|
|
173
|
+
*
|
|
174
|
+
* @example
|
|
175
|
+
* ```ts
|
|
176
|
+
* const notes = docRef(LIVE_ROOMS, planRoom("voltar-main"), "notes");
|
|
177
|
+
* notes.path; // "/live/room/plan/voltar-main/doc/notes"
|
|
178
|
+
* ```
|
|
179
|
+
*/
|
|
180
|
+
export declare function docRef<D extends AnyLiveRoomsDecl, T extends DocRoomTypeOf<D>>(_decl: D, room: RoomRef<T>, doc: DocNameOf<D, T>): DocRef;
|
|
181
|
+
/**
|
|
182
|
+
* Where a doc's socket lives, split the way `y-websocket` wants it
|
|
183
|
+
* (`new WebsocketProvider(origin + serverPath, roomname, doc)`), plus the
|
|
184
|
+
* whole path. Only declared docs of declared types typecheck.
|
|
185
|
+
*/
|
|
186
|
+
export declare function docSocket<D extends AnyLiveRoomsDecl, T extends DocRoomTypeOf<D>>(_decl: D, room: RoomRef<T>, doc: DocNameOf<D, T>): {
|
|
187
|
+
readonly serverPath: string;
|
|
188
|
+
readonly roomname: string;
|
|
189
|
+
readonly path: string;
|
|
190
|
+
};
|
|
191
|
+
/** A parsed `/live/room/...` path. */
|
|
192
|
+
export type LivePath = {
|
|
193
|
+
readonly _tag: 'Room';
|
|
194
|
+
readonly type: string;
|
|
195
|
+
readonly key: string;
|
|
196
|
+
} | {
|
|
197
|
+
readonly _tag: 'Doc';
|
|
198
|
+
readonly type: string;
|
|
199
|
+
readonly key: string;
|
|
200
|
+
readonly doc: string;
|
|
201
|
+
};
|
|
202
|
+
/**
|
|
203
|
+
* Parses a live room path: `undefined` when it isn't under
|
|
204
|
+
* {@link LIVE_ROOM_PATH} at all, `"Bad"` when it is but doesn't name a
|
|
205
|
+
* well-formed room / doc. Doesn't check the declaration.
|
|
206
|
+
*/
|
|
207
|
+
export declare function parseLivePath(pathname: string): LivePath | 'Bad' | undefined;
|
|
208
|
+
/** Whether a parsed path names a declared type (and, for a doc socket, a declared doc). */
|
|
209
|
+
export declare function isDeclaredPath(decl: AnyLiveRoomsDecl, path: LivePath): boolean;
|
|
210
|
+
/** A stable hue for a string (FNV-1a). */
|
|
211
|
+
export declare function hueOf(id: string): number;
|
|
212
|
+
/** Longest client cursor frame the server reads. */
|
|
213
|
+
export declare const MAX_CURSOR_FRAME = 24;
|
|
214
|
+
/**
|
|
215
|
+
* A client cursor frame, or `undefined` when the point is out of range.
|
|
216
|
+
*
|
|
217
|
+
* @param xFraction - 0..1 across the surface's width
|
|
218
|
+
* @param yPx - CSS px down from the surface's top
|
|
219
|
+
*/
|
|
220
|
+
export declare function encodeCursor(xFraction: number, yPx: number): string | undefined;
|
|
221
|
+
/** Server side: a client cursor frame → `[x, y]`, `null` (left), or `undefined` (junk). */
|
|
222
|
+
export declare function decodeCursor(text: string): readonly [number, number] | null | undefined;
|
|
223
|
+
/** Parses a room-socket text frame; anything else (a `pong`, junk) is `undefined`. */
|
|
224
|
+
export declare function parseRoomFrame(text: unknown): RoomServerFrame | undefined;
|
|
@@ -0,0 +1,272 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Live rooms: the wire protocol and the client contract. Shared by the
|
|
3
|
+
* Worker route, the room Durable Object (`@alecsibilia/ui-kit/live/server`) and the
|
|
4
|
+
* kit's browser parts. No Effect, no Solid, no DOM, no Yjs: safe in any
|
|
5
|
+
* bundle. Full guide: `docs/guides/ui-kit-live-rooms.md`.
|
|
6
|
+
*
|
|
7
|
+
* ## Sockets
|
|
8
|
+
*
|
|
9
|
+
* A room is one Durable Object (`idFromName(roomId)`, `roomId` = `type:key`).
|
|
10
|
+
* A page opens at most:
|
|
11
|
+
* - one **room socket** per room: `GET /live/room/<type>/<key>` (WebSocket).
|
|
12
|
+
* Presence and cursors share it. Text frames, below.
|
|
13
|
+
* - one **doc socket** per collaborative doc it shows:
|
|
14
|
+
* `GET /live/room/<type>/<key>/doc/<doc>`. Binary frames in the
|
|
15
|
+
* y-websocket message format (sync 0, awareness 1, auth 2, query
|
|
16
|
+
* awareness 3), so a stock `y-websocket` `WebsocketProvider` with
|
|
17
|
+
* `serverUrl = <origin>/live/room/<type>/<key>/doc` and `roomname = <doc>`
|
|
18
|
+
* speaks it. The text frame `ping` is answered `pong` (both sockets).
|
|
19
|
+
*
|
|
20
|
+
* Upgrade refusals are plain HTTP: 403 (foreign Origin, or `authorize`
|
|
21
|
+
* said no to `join`), 404 (undeclared type / doc, bad key), 426 (not an
|
|
22
|
+
* upgrade), 429 (connect rate limit), 503 (room full).
|
|
23
|
+
*
|
|
24
|
+
* ## Room socket frames
|
|
25
|
+
*
|
|
26
|
+
* Server → client (JSON text):
|
|
27
|
+
* - `{"t":"welcome","self","room","cursors","access","docs"}` once, first;
|
|
28
|
+
* - `{"t":"presence","peers":[Peer],"tabs":n}` on every join / leave (the
|
|
29
|
+
* whole list; `peers` dedupes a signed-in user's tabs, every anonymous
|
|
30
|
+
* tab is its own `Guest` peer; `tabs` counts sockets);
|
|
31
|
+
* - `{"t":"cursors","c":[[peerId,x,y] | [peerId]]}` at most every
|
|
32
|
+
* {@link CURSOR_FLUSH_MS}, only cursors that moved, never your own;
|
|
33
|
+
* `[peerId]` = that cursor left the surface;
|
|
34
|
+
* - `{"t":"saved","doc","at"}` after a doc's `persist` hook ran (a client
|
|
35
|
+
* may refetch the query that reads the persisted copy).
|
|
36
|
+
*
|
|
37
|
+
* Client → server (text): `c <x> <y>` (x in 1/10000 of the surface width,
|
|
38
|
+
* y in CSS px from its top, integers; see {@link encodeCursor}), `l`
|
|
39
|
+
* (cursor left the surface / tab hidden), `ping`.
|
|
40
|
+
*
|
|
41
|
+
* Close codes the server uses: {@link LIVE_CLOSE}.
|
|
42
|
+
*/
|
|
43
|
+
import { roomCapabilities, ROOM_TYPE_PATTERN, } from './declaration.js';
|
|
44
|
+
export * from './declaration.js';
|
|
45
|
+
/** Where every room socket lives. */
|
|
46
|
+
export const LIVE_ROOM_PATH = '/live/room/';
|
|
47
|
+
/** Room keys: URL-safe, short (a plan id, a route name). Checked again on the server. */
|
|
48
|
+
export const ROOM_KEY_PATTERN = /^[A-Za-z0-9_-]{1,64}$/;
|
|
49
|
+
/** Liveness: answered by the runtime without waking the room. */
|
|
50
|
+
export const LIVE_PING = { request: 'ping', response: 'pong' };
|
|
51
|
+
/** Client frame: the cursor left the surface (or the tab was hidden). */
|
|
52
|
+
export const CURSOR_LEAVE_FRAME = 'l';
|
|
53
|
+
/** Close codes the room sends. */
|
|
54
|
+
export const LIVE_CLOSE = {
|
|
55
|
+
/**
|
|
56
|
+
* The room re-checked `authorize(user, room, "edit")` (or the session)
|
|
57
|
+
* and it no longer holds: the session's sockets in that room close.
|
|
58
|
+
* Reconnecting re-identifies (as a guest after sign-out).
|
|
59
|
+
*/
|
|
60
|
+
ACCESS_CHANGED: 4403,
|
|
61
|
+
};
|
|
62
|
+
/**
|
|
63
|
+
* Timings both sides agree on. Server values are enforced; client values
|
|
64
|
+
* are the contract the kit's browser parts follow (`docs/guides/ui-kit-live-rooms.md`).
|
|
65
|
+
*/
|
|
66
|
+
export const LIVE_TIMING = {
|
|
67
|
+
/** Client: at most one cursor frame per this many ms (≤ 20/s). */
|
|
68
|
+
CURSOR_SEND_MS: 50,
|
|
69
|
+
/** Server: cursor fan-out batch window. */
|
|
70
|
+
CURSOR_FLUSH_MS: 50,
|
|
71
|
+
/** Client: close a doc socket this long after the tab is hidden (awareness cleared at once). */
|
|
72
|
+
DOC_HIDDEN_CLOSE_MS: 30_000,
|
|
73
|
+
/** Client: stop awareness renewals after this long without a local edit or selection change. */
|
|
74
|
+
AWARENESS_IDLE_MS: 30_000,
|
|
75
|
+
/** Client: optional `ping` interval for dead-socket detection (auto-answered, no wake). */
|
|
76
|
+
PING_MS: 30_000,
|
|
77
|
+
/** Client: reconnect backoff bounds (jittered). */
|
|
78
|
+
RECONNECT_MIN_MS: 1_000,
|
|
79
|
+
RECONNECT_MAX_MS: 30_000,
|
|
80
|
+
};
|
|
81
|
+
/** Server fan-out batch window for cursors (alias of `LIVE_TIMING.CURSOR_FLUSH_MS`). */
|
|
82
|
+
export const CURSOR_FLUSH_MS = LIVE_TIMING.CURSOR_FLUSH_MS;
|
|
83
|
+
/** A room id: `type:key`. */
|
|
84
|
+
export function roomIdOf(type, key) {
|
|
85
|
+
return `${type}:${key}`;
|
|
86
|
+
}
|
|
87
|
+
/**
|
|
88
|
+
* A reference to a declared room. The type is checked against the
|
|
89
|
+
* declaration at compile time; the key's shape is checked by the server
|
|
90
|
+
* (a bad key gets a 404 upgrade, never a thrown render).
|
|
91
|
+
*
|
|
92
|
+
* @example
|
|
93
|
+
* ```ts
|
|
94
|
+
* const room = roomRef(LIVE_ROOMS, "plan", planId); // roomRef(LIVE_ROOMS, "nope", x) is a type error
|
|
95
|
+
* room.path; // "/live/room/plan/voltar-main"
|
|
96
|
+
* room.cursors; // true (type: true), from `plan: { cursors: true }`
|
|
97
|
+
* ```
|
|
98
|
+
*/
|
|
99
|
+
export function roomRef(decl, type, key) {
|
|
100
|
+
return {
|
|
101
|
+
type,
|
|
102
|
+
key,
|
|
103
|
+
id: roomIdOf(type, key),
|
|
104
|
+
path: `${LIVE_ROOM_PATH}${type}/${encodeURIComponent(key)}`,
|
|
105
|
+
cursors: (roomCapabilities(decl, type)?.cursors ?? false),
|
|
106
|
+
};
|
|
107
|
+
}
|
|
108
|
+
/**
|
|
109
|
+
* A declared doc of a declared room type. `docRef(LIVE_ROOMS, pageRoom, "notes")`
|
|
110
|
+
* (a type with no docs) and an undeclared doc name are type errors.
|
|
111
|
+
*
|
|
112
|
+
* @example
|
|
113
|
+
* ```ts
|
|
114
|
+
* const notes = docRef(LIVE_ROOMS, planRoom("voltar-main"), "notes");
|
|
115
|
+
* notes.path; // "/live/room/plan/voltar-main/doc/notes"
|
|
116
|
+
* ```
|
|
117
|
+
*/
|
|
118
|
+
export function docRef(_decl, room, doc) {
|
|
119
|
+
return { room, doc, path: `${room.path}/doc/${doc}` };
|
|
120
|
+
}
|
|
121
|
+
/**
|
|
122
|
+
* Where a doc's socket lives, split the way `y-websocket` wants it
|
|
123
|
+
* (`new WebsocketProvider(origin + serverPath, roomname, doc)`), plus the
|
|
124
|
+
* whole path. Only declared docs of declared types typecheck.
|
|
125
|
+
*/
|
|
126
|
+
export function docSocket(_decl, room, doc) {
|
|
127
|
+
const serverPath = `${room.path}/doc`;
|
|
128
|
+
return { serverPath, roomname: doc, path: `${serverPath}/${doc}` };
|
|
129
|
+
}
|
|
130
|
+
/**
|
|
131
|
+
* Parses a live room path: `undefined` when it isn't under
|
|
132
|
+
* {@link LIVE_ROOM_PATH} at all, `"Bad"` when it is but doesn't name a
|
|
133
|
+
* well-formed room / doc. Doesn't check the declaration.
|
|
134
|
+
*/
|
|
135
|
+
export function parseLivePath(pathname) {
|
|
136
|
+
if (!pathname.startsWith(LIVE_ROOM_PATH))
|
|
137
|
+
return undefined;
|
|
138
|
+
const parts = pathname.slice(LIVE_ROOM_PATH.length).split('/');
|
|
139
|
+
const [type, rawKey, docWord, doc] = parts;
|
|
140
|
+
if (type === undefined ||
|
|
141
|
+
rawKey === undefined ||
|
|
142
|
+
!ROOM_TYPE_PATTERN.test(type))
|
|
143
|
+
return 'Bad';
|
|
144
|
+
let key;
|
|
145
|
+
try {
|
|
146
|
+
key = decodeURIComponent(rawKey);
|
|
147
|
+
}
|
|
148
|
+
catch {
|
|
149
|
+
return 'Bad';
|
|
150
|
+
}
|
|
151
|
+
if (!ROOM_KEY_PATTERN.test(key))
|
|
152
|
+
return 'Bad';
|
|
153
|
+
if (parts.length === 2)
|
|
154
|
+
return { _tag: 'Room', type, key };
|
|
155
|
+
if (parts.length === 4 &&
|
|
156
|
+
docWord === 'doc' &&
|
|
157
|
+
doc !== undefined &&
|
|
158
|
+
doc.length > 0) {
|
|
159
|
+
return { _tag: 'Doc', type, key, doc };
|
|
160
|
+
}
|
|
161
|
+
return 'Bad';
|
|
162
|
+
}
|
|
163
|
+
/** Whether a parsed path names a declared type (and, for a doc socket, a declared doc). */
|
|
164
|
+
export function isDeclaredPath(decl, path) {
|
|
165
|
+
const caps = roomCapabilities(decl, path.type);
|
|
166
|
+
if (!caps)
|
|
167
|
+
return false;
|
|
168
|
+
return path._tag === 'Room' || caps.docs.includes(path.doc);
|
|
169
|
+
}
|
|
170
|
+
/** A stable hue for a string (FNV-1a). */
|
|
171
|
+
export function hueOf(id) {
|
|
172
|
+
let hash = 0x811c9dc5;
|
|
173
|
+
for (let i = 0; i < id.length; i += 1) {
|
|
174
|
+
hash ^= id.charCodeAt(i);
|
|
175
|
+
hash = Math.imul(hash, 0x01000193);
|
|
176
|
+
}
|
|
177
|
+
return (hash >>> 0) % 360;
|
|
178
|
+
}
|
|
179
|
+
/** Largest cursor coordinate values (x is 1/10000 of the width). */
|
|
180
|
+
const CURSOR_X_MAX = 10_000;
|
|
181
|
+
const CURSOR_Y_MAX = 100_000;
|
|
182
|
+
/** Longest client cursor frame the server reads. */
|
|
183
|
+
export const MAX_CURSOR_FRAME = 24;
|
|
184
|
+
/**
|
|
185
|
+
* A client cursor frame, or `undefined` when the point is out of range.
|
|
186
|
+
*
|
|
187
|
+
* @param xFraction - 0..1 across the surface's width
|
|
188
|
+
* @param yPx - CSS px down from the surface's top
|
|
189
|
+
*/
|
|
190
|
+
export function encodeCursor(xFraction, yPx) {
|
|
191
|
+
if (!(xFraction >= 0 && xFraction <= 1) ||
|
|
192
|
+
!(yPx >= 0 && yPx < CURSOR_Y_MAX))
|
|
193
|
+
return undefined;
|
|
194
|
+
return `c ${Math.round(xFraction * CURSOR_X_MAX)} ${Math.round(yPx)}`;
|
|
195
|
+
}
|
|
196
|
+
/** Server side: a client cursor frame → `[x, y]`, `null` (left), or `undefined` (junk). */
|
|
197
|
+
export function decodeCursor(text) {
|
|
198
|
+
if (text === CURSOR_LEAVE_FRAME)
|
|
199
|
+
return null;
|
|
200
|
+
if (text.length > MAX_CURSOR_FRAME || !text.startsWith('c '))
|
|
201
|
+
return undefined;
|
|
202
|
+
const parts = text.split(' ');
|
|
203
|
+
if (parts.length !== 3)
|
|
204
|
+
return undefined;
|
|
205
|
+
const x = Number(parts[1]);
|
|
206
|
+
const y = Number(parts[2]);
|
|
207
|
+
if (!Number.isInteger(x) || !Number.isInteger(y))
|
|
208
|
+
return undefined;
|
|
209
|
+
if (x < 0 || x > CURSOR_X_MAX || y < 0 || y >= CURSOR_Y_MAX)
|
|
210
|
+
return undefined;
|
|
211
|
+
return [x, y];
|
|
212
|
+
}
|
|
213
|
+
function isRecord(value) {
|
|
214
|
+
return typeof value === 'object' && value !== null;
|
|
215
|
+
}
|
|
216
|
+
function isPeer(value) {
|
|
217
|
+
return (isRecord(value) &&
|
|
218
|
+
typeof value.id === 'string' &&
|
|
219
|
+
typeof value.name === 'string' &&
|
|
220
|
+
(value.kind === 'Member' || value.kind === 'Guest') &&
|
|
221
|
+
typeof value.hue === 'number');
|
|
222
|
+
}
|
|
223
|
+
function isCursorEntry(value) {
|
|
224
|
+
if (!Array.isArray(value) || typeof value[0] !== 'string')
|
|
225
|
+
return false;
|
|
226
|
+
if (value.length === 1)
|
|
227
|
+
return true;
|
|
228
|
+
return (value.length === 3 &&
|
|
229
|
+
typeof value[1] === 'number' &&
|
|
230
|
+
typeof value[2] === 'number');
|
|
231
|
+
}
|
|
232
|
+
/** Parses a room-socket text frame; anything else (a `pong`, junk) is `undefined`. */
|
|
233
|
+
export function parseRoomFrame(text) {
|
|
234
|
+
if (typeof text !== 'string' || text.charCodeAt(0) !== 123 /* { */)
|
|
235
|
+
return undefined;
|
|
236
|
+
let value;
|
|
237
|
+
try {
|
|
238
|
+
value = JSON.parse(text);
|
|
239
|
+
}
|
|
240
|
+
catch {
|
|
241
|
+
return undefined;
|
|
242
|
+
}
|
|
243
|
+
if (!isRecord(value))
|
|
244
|
+
return undefined;
|
|
245
|
+
switch (value.t) {
|
|
246
|
+
case 'welcome':
|
|
247
|
+
return typeof value.self === 'string' &&
|
|
248
|
+
typeof value.room === 'string' &&
|
|
249
|
+
typeof value.cursors === 'boolean' &&
|
|
250
|
+
(value.access === 'Editor' || value.access === 'Reader') &&
|
|
251
|
+
Array.isArray(value.docs) &&
|
|
252
|
+
value.docs.every((doc) => typeof doc === 'string')
|
|
253
|
+
? value
|
|
254
|
+
: undefined;
|
|
255
|
+
case 'presence':
|
|
256
|
+
return Array.isArray(value.peers) &&
|
|
257
|
+
value.peers.every(isPeer) &&
|
|
258
|
+
typeof value.tabs === 'number'
|
|
259
|
+
? value
|
|
260
|
+
: undefined;
|
|
261
|
+
case 'cursors':
|
|
262
|
+
return Array.isArray(value.c) && value.c.every(isCursorEntry)
|
|
263
|
+
? value
|
|
264
|
+
: undefined;
|
|
265
|
+
case 'saved':
|
|
266
|
+
return typeof value.doc === 'string' && typeof value.at === 'number'
|
|
267
|
+
? value
|
|
268
|
+
: undefined;
|
|
269
|
+
default:
|
|
270
|
+
return undefined;
|
|
271
|
+
}
|
|
272
|
+
}
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import * as Y from 'yjs';
|
|
2
|
+
import type { RoomState } from './types.ts';
|
|
3
|
+
/** y-websocket message types. */
|
|
4
|
+
export declare const MESSAGE: {
|
|
5
|
+
readonly SYNC: 0;
|
|
6
|
+
readonly AWARENESS: 1;
|
|
7
|
+
readonly AUTH: 2;
|
|
8
|
+
readonly QUERY_AWARENESS: 3;
|
|
9
|
+
};
|
|
10
|
+
/** Compact a doc's log past this many rows. */
|
|
11
|
+
export declare const COMPACT_AFTER_ROWS = 200;
|
|
12
|
+
/** Default cap on a doc's stored bytes. */
|
|
13
|
+
export declare const DEFAULT_DOC_MAX_BYTES: number;
|
|
14
|
+
/** A doc loaded into the room's memory (rebuilt from SQLite after each wake). */
|
|
15
|
+
export interface LoadedDoc {
|
|
16
|
+
readonly ydoc: Y.Doc;
|
|
17
|
+
/** Stored bytes (sum of the log rows; exact after compaction). */
|
|
18
|
+
bytes: number;
|
|
19
|
+
rows: number;
|
|
20
|
+
}
|
|
21
|
+
export declare function ensureTables(state: RoomState): void;
|
|
22
|
+
/** Reads a doc's log into a fresh Y.Doc (no seed). */
|
|
23
|
+
export declare function readDoc(state: RoomState, doc: string): LoadedDoc;
|
|
24
|
+
/** Appends one update row. */
|
|
25
|
+
export declare function appendUpdate(state: RoomState, loaded: LoadedDoc, doc: string, update: Uint8Array): void;
|
|
26
|
+
/** Replaces a doc's log with one snapshot row when it has grown past {@link COMPACT_AFTER_ROWS}. */
|
|
27
|
+
export declare function maybeCompact(state: RoomState, loaded: LoadedDoc, doc: string): void;
|
|
28
|
+
export declare function readMeta(state: RoomState, key: string): string | undefined;
|
|
29
|
+
export declare function writeMeta(state: RoomState, key: string, value: string): void;
|
|
30
|
+
export declare function deleteMeta(state: RoomState, key: string): void;
|
|
31
|
+
/** Meta rows whose key starts with `prefix`. */
|
|
32
|
+
export declare function metaWithPrefix(state: RoomState, prefix: string): {
|
|
33
|
+
key: string;
|
|
34
|
+
value: string;
|
|
35
|
+
}[];
|
|
36
|
+
/** The server's sync step 1 (the client answers with what the server lacks). */
|
|
37
|
+
export declare function syncStep1Frame(ydoc: Y.Doc): Uint8Array;
|
|
38
|
+
/** An update broadcast to the doc's other sockets. */
|
|
39
|
+
export declare function updateFrame(update: Uint8Array): Uint8Array;
|
|
40
|
+
/** y-protocols' "permission denied" auth message (y-websocket logs it). */
|
|
41
|
+
export declare function permissionDeniedFrame(reason: string): Uint8Array;
|
|
42
|
+
/** What a binary doc frame is. */
|
|
43
|
+
export type DocFrame = {
|
|
44
|
+
readonly _tag: 'SyncStep1';
|
|
45
|
+
} | {
|
|
46
|
+
readonly _tag: 'SyncWrite';
|
|
47
|
+
} | {
|
|
48
|
+
readonly _tag: 'Awareness';
|
|
49
|
+
readonly update: Uint8Array;
|
|
50
|
+
} | {
|
|
51
|
+
readonly _tag: 'QueryAwareness';
|
|
52
|
+
} | {
|
|
53
|
+
readonly _tag: 'Unknown';
|
|
54
|
+
};
|
|
55
|
+
/** Classifies a frame without applying it. */
|
|
56
|
+
export declare function classifyDocFrame(data: Uint8Array): DocFrame;
|
|
57
|
+
/**
|
|
58
|
+
* Applies a sync frame (step 1 answers with step 2; step 2 / update
|
|
59
|
+
* writes) with `origin` as the transaction origin. Returns the reply, if any.
|
|
60
|
+
*/
|
|
61
|
+
export declare function applySync(ydoc: Y.Doc, data: Uint8Array, origin: unknown): Uint8Array | undefined;
|
|
62
|
+
/** Most awareness client ids one socket may announce (the attachment is ≤ 2 KB). */
|
|
63
|
+
export declare const MAX_AWARENESS_IDS = 8;
|
|
64
|
+
/** The awareness clocks a socket announced, updated with one awareness update. */
|
|
65
|
+
export declare function trackAwareness(previous: Readonly<Record<string, number>> | undefined, update: Uint8Array): Record<string, number>;
|
|
66
|
+
/** An awareness update removing a closed socket's states (clock + 1, state null). */
|
|
67
|
+
export declare function awarenessRemovalFrame(clocks: Readonly<Record<string, number>>): Uint8Array | undefined;
|
|
68
|
+
export { Y };
|