@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,348 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The room Durable Object's logic, as plain functions over structural
|
|
3
|
+
* slices of the runtime (`RoomState`, `RoomRuntime`), so `bun test` runs it
|
|
4
|
+
* with fakes. `createLiveRooms` wraps it in the class workerd constructs.
|
|
5
|
+
*
|
|
6
|
+
* Hibernation: every socket is accepted with the hibernation API, and all
|
|
7
|
+
* that must survive an eviction lives in the socket's attachment
|
|
8
|
+
* (identity, access, last auth check, awareness clocks) or SQLite (docs,
|
|
9
|
+
* pending persists). Memory (loaded Y.Docs, pending cursors, rate buckets)
|
|
10
|
+
* is a cache rebuilt lazily after a wake. Nothing here sets a timer while
|
|
11
|
+
* the room is idle: the only timer is the 50 ms cursor flush, armed by a
|
|
12
|
+
* cursor frame; the only alarm is a pending doc `persist`.
|
|
13
|
+
*
|
|
14
|
+
* Socket tags: `r` (room socket), `d:<doc>` (doc socket), `s:<session>`
|
|
15
|
+
* (every socket of a signed-in session, to close them together).
|
|
16
|
+
*/
|
|
17
|
+
import { appendUpdate, applySync, awarenessRemovalFrame, classifyDocFrame, DEFAULT_DOC_MAX_BYTES, deleteMeta, ensureTables, maybeCompact, metaWithPrefix, permissionDeniedFrame, readDoc, readMeta, syncStep1Frame, trackAwareness, updateFrame, writeMeta, Y, } from './doc-store.js';
|
|
18
|
+
import { createRoomPresence } from './room-presence.js';
|
|
19
|
+
import { DEFAULT_PERSIST_DEBOUNCE_MS, DEFAULT_RECHECK_EDIT_MS, MAX_AWARENESS_FRAME, MAX_DOC_FRAME, PERSIST_PREFIX, RATE, ROOM_META, ROOM_TAG, attachmentOf, closeSafe, docTag, parseIdentity, presenceIdOf, sendSafe, sendableCode, sessionTag, take, } from './room-sockets.js';
|
|
20
|
+
import {} from './types.js';
|
|
21
|
+
import { matchTag } from '../../logic/result.js';
|
|
22
|
+
import { roomCapabilities } from '../declaration.js';
|
|
23
|
+
import { LIVE_CLOSE, roomIdOf, roomRef, } from '../protocol.js';
|
|
24
|
+
export { DEFAULT_PERSIST_DEBOUNCE_MS, DEFAULT_RECHECK_EDIT_MS, MAX_AWARENESS_FRAME, MAX_DOC_FRAME, RATE, } from './room-sockets.js';
|
|
25
|
+
/**
|
|
26
|
+
* One room object's logic.
|
|
27
|
+
*
|
|
28
|
+
* @param decl - the app's declaration (capabilities per type)
|
|
29
|
+
* @param hooks - the app's hooks (authorize, doc seed / persist)
|
|
30
|
+
* @param state - the object's `DurableObjectState` (or a fake)
|
|
31
|
+
* @param env - the object's bindings, handed to hooks
|
|
32
|
+
* @param runtime - workerd's socket pair, `101`, clock and timers (or fakes)
|
|
33
|
+
*/
|
|
34
|
+
export function createRoomCore(decl, hooks, state, env, runtime) {
|
|
35
|
+
const typeHooks = hooks.types;
|
|
36
|
+
const recheckMs = hooks.recheckEditMs ?? DEFAULT_RECHECK_EDIT_MS;
|
|
37
|
+
// Memory: a cache, rebuilt after each wake.
|
|
38
|
+
const docs = new Map();
|
|
39
|
+
const { movedPids, broadcastRoom, broadcastPresence, queueCursor, roomMessage, bucketOf, } = createRoomPresence({ state, runtime });
|
|
40
|
+
const chains = new WeakMap();
|
|
41
|
+
const deniedOnce = new WeakSet();
|
|
42
|
+
let tablesReady = false;
|
|
43
|
+
const docHooksOf = (type, doc) => typeHooks[type]?.docs?.[doc];
|
|
44
|
+
const tables = () => {
|
|
45
|
+
if (tablesReady)
|
|
46
|
+
return;
|
|
47
|
+
ensureTables(state);
|
|
48
|
+
tablesReady = true;
|
|
49
|
+
};
|
|
50
|
+
// --- docs ----------------------------------------------------------------
|
|
51
|
+
function refOf(type, key) {
|
|
52
|
+
return roomRef(decl, type, key);
|
|
53
|
+
}
|
|
54
|
+
function loadDoc(type, key, doc) {
|
|
55
|
+
const existing = docs.get(doc);
|
|
56
|
+
if (existing)
|
|
57
|
+
return existing;
|
|
58
|
+
const loading = (async () => {
|
|
59
|
+
tables();
|
|
60
|
+
const loaded = readDoc(state, doc);
|
|
61
|
+
if (loaded.rows === 0) {
|
|
62
|
+
const seed = docHooksOf(type, doc)?.seed;
|
|
63
|
+
const text = seed
|
|
64
|
+
? await seed(refOf(type, key), { env, now: runtime.now() })
|
|
65
|
+
: undefined;
|
|
66
|
+
if (text) {
|
|
67
|
+
loaded.ydoc.getText(doc).insert(0, text);
|
|
68
|
+
appendUpdate(state, loaded, doc, Y.encodeStateAsUpdate(loaded.ydoc));
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
loaded.ydoc.on('update', (update, origin) => {
|
|
72
|
+
onDocUpdate(type, key, doc, loaded, update, origin);
|
|
73
|
+
});
|
|
74
|
+
return loaded;
|
|
75
|
+
})();
|
|
76
|
+
docs.set(doc, loading);
|
|
77
|
+
loading.catch(() => docs.delete(doc));
|
|
78
|
+
return loading;
|
|
79
|
+
}
|
|
80
|
+
function onDocUpdate(type, key, doc, loaded, update, origin) {
|
|
81
|
+
appendUpdate(state, loaded, doc, update);
|
|
82
|
+
const frame = updateFrame(update);
|
|
83
|
+
for (const socket of state.getWebSockets(docTag(doc))) {
|
|
84
|
+
if (socket !== origin)
|
|
85
|
+
sendSafe(socket, frame);
|
|
86
|
+
}
|
|
87
|
+
maybeCompact(state, loaded, doc);
|
|
88
|
+
const persist = docHooksOf(type, doc)?.persist;
|
|
89
|
+
if (persist)
|
|
90
|
+
schedulePersist(doc, persist.debounceMs ?? DEFAULT_PERSIST_DEBOUNCE_MS);
|
|
91
|
+
}
|
|
92
|
+
/** Records a due time for the doc's persist (first unsaved edit wins) and arms the alarm. */
|
|
93
|
+
function schedulePersist(doc, debounceMs) {
|
|
94
|
+
const metaKey = `${PERSIST_PREFIX}${doc}`;
|
|
95
|
+
if (readMeta(state, metaKey) !== undefined)
|
|
96
|
+
return;
|
|
97
|
+
const due = runtime.now() + debounceMs;
|
|
98
|
+
writeMeta(state, metaKey, String(due));
|
|
99
|
+
void state.storage.getAlarm().then((at) => {
|
|
100
|
+
if (at === null || at > due)
|
|
101
|
+
return state.storage.setAlarm(due);
|
|
102
|
+
});
|
|
103
|
+
}
|
|
104
|
+
/** Re-runs `authorize(…, "edit")` when the last check is older than `recheckMs`. False closes the session's sockets. */
|
|
105
|
+
async function stillEditor(socket, att) {
|
|
106
|
+
if (att.access !== 'Editor')
|
|
107
|
+
return false;
|
|
108
|
+
const now = runtime.now();
|
|
109
|
+
if (now - att.checkedAt < recheckMs)
|
|
110
|
+
return true;
|
|
111
|
+
const type = typeHooks[att.type];
|
|
112
|
+
const ok = type
|
|
113
|
+
? await type.authorize(att.user, refOf(att.type, att.key), 'edit', {
|
|
114
|
+
env,
|
|
115
|
+
now,
|
|
116
|
+
})
|
|
117
|
+
: false;
|
|
118
|
+
if (ok) {
|
|
119
|
+
att.checkedAt = now;
|
|
120
|
+
socket.serializeAttachment(att);
|
|
121
|
+
return true;
|
|
122
|
+
}
|
|
123
|
+
revoke(socket, att);
|
|
124
|
+
return false;
|
|
125
|
+
}
|
|
126
|
+
/** Closes every socket of the session in this room (or just this one for a guest). */
|
|
127
|
+
function revoke(socket, att) {
|
|
128
|
+
const targets = att.user._tag === 'Member'
|
|
129
|
+
? state.getWebSockets(sessionTag(att.user.session))
|
|
130
|
+
: [socket];
|
|
131
|
+
for (const target of targets)
|
|
132
|
+
closeSafe(target, LIVE_CLOSE.ACCESS_CHANGED, 'access changed');
|
|
133
|
+
}
|
|
134
|
+
function deny(socket, reason) {
|
|
135
|
+
if (deniedOnce.has(socket))
|
|
136
|
+
return;
|
|
137
|
+
deniedOnce.add(socket);
|
|
138
|
+
sendSafe(socket, permissionDeniedFrame(reason));
|
|
139
|
+
}
|
|
140
|
+
async function docMessage(socket, att, data) {
|
|
141
|
+
const doc = att.doc;
|
|
142
|
+
if (doc === undefined)
|
|
143
|
+
return;
|
|
144
|
+
const frame = classifyDocFrame(data);
|
|
145
|
+
return matchTag(frame, {
|
|
146
|
+
SyncStep1: async () => {
|
|
147
|
+
const loaded = await loadDoc(att.type, att.key, doc);
|
|
148
|
+
const reply = applySync(loaded.ydoc, data, socket);
|
|
149
|
+
if (reply)
|
|
150
|
+
sendSafe(socket, reply);
|
|
151
|
+
},
|
|
152
|
+
SyncWrite: async () => {
|
|
153
|
+
if (!(await stillEditor(socket, att))) {
|
|
154
|
+
deny(socket, 'read-only');
|
|
155
|
+
return;
|
|
156
|
+
}
|
|
157
|
+
const loaded = await loadDoc(att.type, att.key, doc);
|
|
158
|
+
const max = docHooksOf(att.type, doc)?.maxBytes ?? DEFAULT_DOC_MAX_BYTES;
|
|
159
|
+
if (loaded.bytes + data.byteLength > max) {
|
|
160
|
+
deny(socket, 'doc-full');
|
|
161
|
+
return;
|
|
162
|
+
}
|
|
163
|
+
const reply = applySync(loaded.ydoc, data, socket);
|
|
164
|
+
if (reply)
|
|
165
|
+
sendSafe(socket, reply);
|
|
166
|
+
},
|
|
167
|
+
Awareness: async (awareness) => {
|
|
168
|
+
if (data.byteLength > MAX_AWARENESS_FRAME)
|
|
169
|
+
return;
|
|
170
|
+
att.aw = trackAwareness(att.aw, awareness.update);
|
|
171
|
+
socket.serializeAttachment(att);
|
|
172
|
+
relay(socket, doc, data);
|
|
173
|
+
},
|
|
174
|
+
// Stateless server: ask the other clients to re-announce themselves.
|
|
175
|
+
QueryAwareness: async () => relay(socket, doc, data),
|
|
176
|
+
Unknown: async () => { },
|
|
177
|
+
});
|
|
178
|
+
}
|
|
179
|
+
function relay(socket, doc, data) {
|
|
180
|
+
for (const other of state.getWebSockets(docTag(doc))) {
|
|
181
|
+
if (other !== socket)
|
|
182
|
+
sendSafe(other, data);
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
// --- handlers ------------------------------------------------------------
|
|
186
|
+
async function fetch(request) {
|
|
187
|
+
const identity = parseIdentity(request);
|
|
188
|
+
const caps = identity
|
|
189
|
+
? roomCapabilities(decl, identity.room.type)
|
|
190
|
+
: undefined;
|
|
191
|
+
if (!identity || !caps)
|
|
192
|
+
return new Response('Bad room request', { status: 400 });
|
|
193
|
+
const socketDoc = identity.socket._tag === 'Doc' ? identity.socket.doc : undefined;
|
|
194
|
+
if (socketDoc !== undefined && !caps.docs.includes(socketDoc)) {
|
|
195
|
+
return new Response('No such doc', { status: 404 });
|
|
196
|
+
}
|
|
197
|
+
if (state.getWebSockets().length >= caps.maxSockets) {
|
|
198
|
+
return new Response('Room is full', { status: 503 });
|
|
199
|
+
}
|
|
200
|
+
const { client, server } = runtime.newPair();
|
|
201
|
+
const tags = [socketDoc === undefined ? ROOM_TAG : docTag(socketDoc)];
|
|
202
|
+
if (identity.user._tag === 'Member')
|
|
203
|
+
tags.push(sessionTag(identity.user.session));
|
|
204
|
+
state.acceptWebSocket(server, tags);
|
|
205
|
+
const att = {
|
|
206
|
+
peer: identity.peer,
|
|
207
|
+
pid: presenceIdOf(identity.user, identity.peer),
|
|
208
|
+
user: identity.user,
|
|
209
|
+
access: identity.access,
|
|
210
|
+
kind: socketDoc === undefined ? 'r' : 'd',
|
|
211
|
+
...(socketDoc === undefined ? {} : { doc: socketDoc }),
|
|
212
|
+
type: identity.room.type,
|
|
213
|
+
key: identity.room.key,
|
|
214
|
+
checkedAt: runtime.now(),
|
|
215
|
+
};
|
|
216
|
+
server.serializeAttachment(att);
|
|
217
|
+
if (socketDoc === undefined) {
|
|
218
|
+
sendSafe(server, JSON.stringify({
|
|
219
|
+
t: 'welcome',
|
|
220
|
+
self: att.pid,
|
|
221
|
+
room: roomIdOf(att.type, att.key),
|
|
222
|
+
cursors: caps.cursors,
|
|
223
|
+
access: att.access,
|
|
224
|
+
docs: caps.docs,
|
|
225
|
+
}));
|
|
226
|
+
broadcastPresence(caps);
|
|
227
|
+
}
|
|
228
|
+
else {
|
|
229
|
+
tables();
|
|
230
|
+
if (readMeta(state, ROOM_META) === undefined) {
|
|
231
|
+
writeMeta(state, ROOM_META, JSON.stringify({ type: att.type, key: att.key }));
|
|
232
|
+
}
|
|
233
|
+
const loaded = await loadDoc(att.type, att.key, socketDoc);
|
|
234
|
+
sendSafe(server, syncStep1Frame(loaded.ydoc));
|
|
235
|
+
// Ask the others to announce their awareness, so the newcomer sees carets now, not at the next renewal.
|
|
236
|
+
const others = state
|
|
237
|
+
.getWebSockets(docTag(socketDoc))
|
|
238
|
+
.filter((s) => s !== server);
|
|
239
|
+
if (others.length > 0) {
|
|
240
|
+
const query = new Uint8Array([3]);
|
|
241
|
+
for (const other of others)
|
|
242
|
+
sendSafe(other, query);
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
return runtime.switchingProtocols(client);
|
|
246
|
+
}
|
|
247
|
+
async function message(socket, data) {
|
|
248
|
+
const att = attachmentOf(socket);
|
|
249
|
+
if (!att)
|
|
250
|
+
return;
|
|
251
|
+
const caps = roomCapabilities(decl, att.type);
|
|
252
|
+
if (!caps)
|
|
253
|
+
return;
|
|
254
|
+
if (att.kind === 'r') {
|
|
255
|
+
roomMessage(socket, att, caps, data);
|
|
256
|
+
return;
|
|
257
|
+
}
|
|
258
|
+
if (typeof data === 'string' || data.byteLength > MAX_DOC_FRAME)
|
|
259
|
+
return;
|
|
260
|
+
if (!take(bucketOf(socket, RATE.DOC.burst), RATE.DOC, runtime.now()))
|
|
261
|
+
return;
|
|
262
|
+
// One frame at a time per socket: an awaited auth re-check must not reorder its updates.
|
|
263
|
+
const previous = chains.get(socket) ?? Promise.resolve();
|
|
264
|
+
const next = previous.then(() => docMessage(socket, att, new Uint8Array(data)));
|
|
265
|
+
chains.set(socket, next.catch(() => { }));
|
|
266
|
+
return next;
|
|
267
|
+
}
|
|
268
|
+
async function close(socket, code) {
|
|
269
|
+
closeSafe(socket, sendableCode(code), '');
|
|
270
|
+
const att = attachmentOf(socket);
|
|
271
|
+
if (!att)
|
|
272
|
+
return;
|
|
273
|
+
const caps = roomCapabilities(decl, att.type);
|
|
274
|
+
if (att.kind === 'r') {
|
|
275
|
+
if (caps?.cursors &&
|
|
276
|
+
movedPids.has(att.pid) &&
|
|
277
|
+
!hasSameUserElsewhere(socket, att.pid)) {
|
|
278
|
+
movedPids.delete(att.pid);
|
|
279
|
+
queueCursorIfAnyone([att.pid]);
|
|
280
|
+
}
|
|
281
|
+
if (caps)
|
|
282
|
+
broadcastPresence(caps, socket);
|
|
283
|
+
return;
|
|
284
|
+
}
|
|
285
|
+
const removal = att.aw ? awarenessRemovalFrame(att.aw) : undefined;
|
|
286
|
+
if (removal && att.doc !== undefined) {
|
|
287
|
+
for (const other of state.getWebSockets(docTag(att.doc))) {
|
|
288
|
+
if (other !== socket)
|
|
289
|
+
sendSafe(other, removal);
|
|
290
|
+
}
|
|
291
|
+
}
|
|
292
|
+
}
|
|
293
|
+
function hasSameUserElsewhere(socket, pid) {
|
|
294
|
+
return state
|
|
295
|
+
.getWebSockets(ROOM_TAG)
|
|
296
|
+
.some((s) => s !== socket && attachmentOf(s)?.pid === pid);
|
|
297
|
+
}
|
|
298
|
+
function queueCursorIfAnyone(entry) {
|
|
299
|
+
if (state.getWebSockets(ROOM_TAG).length > 1)
|
|
300
|
+
queueCursor(entry);
|
|
301
|
+
}
|
|
302
|
+
/** Runs every due `persist`, then re-arms for the next one. A throw makes workerd retry the alarm. */
|
|
303
|
+
async function alarm() {
|
|
304
|
+
tables();
|
|
305
|
+
const roomJson = readMeta(state, ROOM_META);
|
|
306
|
+
if (roomJson === undefined)
|
|
307
|
+
return;
|
|
308
|
+
const room = JSON.parse(roomJson);
|
|
309
|
+
const now = runtime.now();
|
|
310
|
+
let nextDue;
|
|
311
|
+
for (const { key, value } of metaWithPrefix(state, PERSIST_PREFIX)) {
|
|
312
|
+
const due = Number(value);
|
|
313
|
+
const doc = key.slice(PERSIST_PREFIX.length);
|
|
314
|
+
if (due > now) {
|
|
315
|
+
nextDue = nextDue === undefined ? due : Math.min(nextDue, due);
|
|
316
|
+
continue;
|
|
317
|
+
}
|
|
318
|
+
const persist = docHooksOf(room.type, doc)?.persist;
|
|
319
|
+
if (persist) {
|
|
320
|
+
const loaded = await loadDoc(room.type, room.key, doc);
|
|
321
|
+
// Cleared before the hook runs: edits during the await schedule the next run.
|
|
322
|
+
deleteMeta(state, key);
|
|
323
|
+
try {
|
|
324
|
+
await persist.run({
|
|
325
|
+
room: refOf(room.type, room.key),
|
|
326
|
+
doc,
|
|
327
|
+
text: loaded.ydoc.getText(doc).toString(),
|
|
328
|
+
state: Y.encodeStateAsUpdate(loaded.ydoc),
|
|
329
|
+
}, { env, now });
|
|
330
|
+
}
|
|
331
|
+
catch (error) {
|
|
332
|
+
if (readMeta(state, key) === undefined)
|
|
333
|
+
writeMeta(state, key, String(now));
|
|
334
|
+
throw error;
|
|
335
|
+
}
|
|
336
|
+
broadcastRoom({ t: 'saved', doc, at: runtime.now() });
|
|
337
|
+
}
|
|
338
|
+
else {
|
|
339
|
+
deleteMeta(state, key);
|
|
340
|
+
}
|
|
341
|
+
}
|
|
342
|
+
const remaining = metaWithPrefix(state, PERSIST_PREFIX).map((row) => Number(row.value));
|
|
343
|
+
const soonest = Math.min(...remaining, nextDue ?? Number.POSITIVE_INFINITY);
|
|
344
|
+
if (Number.isFinite(soonest))
|
|
345
|
+
await state.storage.setAlarm(Math.max(soonest, runtime.now() + 1));
|
|
346
|
+
}
|
|
347
|
+
return { fetch, message, close, alarm };
|
|
348
|
+
}
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import { type ConnectLimiter, type LiveRoomsHooks, type RoomNamespace } from './types.ts';
|
|
2
|
+
/**
|
|
3
|
+
* The Worker side of live rooms: everything that happens before an
|
|
4
|
+
* upgrade reaches a room's Durable Object. In order:
|
|
5
|
+
*
|
|
6
|
+
* 1. the path is under `/live/room/` (else `undefined`: not ours);
|
|
7
|
+
* 2. it names a declared room type (and doc) with a well-formed key, and
|
|
8
|
+
* the type's `key` hook accepts it (else 404);
|
|
9
|
+
* 3. it is a WebSocket upgrade (else 426);
|
|
10
|
+
* 4. its `Origin` is ours (else 403: cross-site WebSocket hijacking);
|
|
11
|
+
* 5. the connect rate limit, per client IP, when declared (else 429);
|
|
12
|
+
* 6. the app resolves the user (`resolveUser`, its session) and
|
|
13
|
+
* `authorize(user, room, "join")` passes (else 403); for a type with
|
|
14
|
+
* docs, `authorize(user, room, "edit")` sets Editor / Reader;
|
|
15
|
+
* 7. the request is rebuilt with only the WebSocket handshake headers plus
|
|
16
|
+
* the identity header (`x-live-identity`): the cookie and anything
|
|
17
|
+
* client-sent named `x-live-*` never reach the room.
|
|
18
|
+
*/
|
|
19
|
+
import { type AnyLiveRoomsDecl } from '../declaration.ts';
|
|
20
|
+
/**
|
|
21
|
+
* Whether an upgrade may open a socket: its `Origin` is the request's own
|
|
22
|
+
* origin or one of `extraOrigins` (the dev server's, whose proxy forwards
|
|
23
|
+
* to another port). Browsers always send `Origin` on a WebSocket
|
|
24
|
+
* handshake, so a cross-site page can't open a socket with a visitor's
|
|
25
|
+
* cookie; a request without `Origin` is a non-browser client, which gets
|
|
26
|
+
* nothing its own credentials don't give it.
|
|
27
|
+
*/
|
|
28
|
+
export declare function isAllowedOrigin(request: Request, extraOrigins: readonly string[]): boolean;
|
|
29
|
+
/** The bindings live rooms read from `env`, checked. */
|
|
30
|
+
export interface LiveRoomsEnv {
|
|
31
|
+
readonly rooms: RoomNamespace;
|
|
32
|
+
readonly limiter: ConnectLimiter | undefined;
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* The declaration's bindings from a Worker `env`, or a clear error naming
|
|
36
|
+
* the missing one and where to enable it. This is the server's one way to
|
|
37
|
+
* reach them (the app's `Bindings` service doesn't carry them: only the
|
|
38
|
+
* Worker entry and the room use them, outside Effect).
|
|
39
|
+
*
|
|
40
|
+
* @throws Error when the Durable Object (or a declared rate limit) binding is missing.
|
|
41
|
+
*/
|
|
42
|
+
export declare function liveRoomsEnv(decl: AnyLiveRoomsDecl, env: unknown): LiveRoomsEnv;
|
|
43
|
+
/** Options of one route call. */
|
|
44
|
+
export interface RouteOptions {
|
|
45
|
+
/** Origins besides the request's own that may connect (the dev server's). */
|
|
46
|
+
readonly extraOrigins?: readonly string[];
|
|
47
|
+
/** Epoch ms (tests). */
|
|
48
|
+
readonly now?: number;
|
|
49
|
+
}
|
|
50
|
+
/** A 128-bit random peer id (base64url). */
|
|
51
|
+
export declare function randomPeerId(): string;
|
|
52
|
+
/**
|
|
53
|
+
* Builds the Worker route for a declaration and the app's hooks.
|
|
54
|
+
* `undefined` means "not a live room path"; the Worker goes on routing.
|
|
55
|
+
*/
|
|
56
|
+
export declare function createLiveRoomsRoute<D extends AnyLiveRoomsDecl, Env>(decl: D, hooks: LiveRoomsHooks<D, Env>): (request: Request, env: Env, options?: RouteOptions) => Promise<Response> | undefined;
|
|
@@ -0,0 +1,159 @@
|
|
|
1
|
+
import { IDENTITY_HEADER, } from './types.js';
|
|
2
|
+
/**
|
|
3
|
+
* The Worker side of live rooms: everything that happens before an
|
|
4
|
+
* upgrade reaches a room's Durable Object. In order:
|
|
5
|
+
*
|
|
6
|
+
* 1. the path is under `/live/room/` (else `undefined`: not ours);
|
|
7
|
+
* 2. it names a declared room type (and doc) with a well-formed key, and
|
|
8
|
+
* the type's `key` hook accepts it (else 404);
|
|
9
|
+
* 3. it is a WebSocket upgrade (else 426);
|
|
10
|
+
* 4. its `Origin` is ours (else 403: cross-site WebSocket hijacking);
|
|
11
|
+
* 5. the connect rate limit, per client IP, when declared (else 429);
|
|
12
|
+
* 6. the app resolves the user (`resolveUser`, its session) and
|
|
13
|
+
* `authorize(user, room, "join")` passes (else 403); for a type with
|
|
14
|
+
* docs, `authorize(user, room, "edit")` sets Editor / Reader;
|
|
15
|
+
* 7. the request is rebuilt with only the WebSocket handshake headers plus
|
|
16
|
+
* the identity header (`x-live-identity`): the cookie and anything
|
|
17
|
+
* client-sent named `x-live-*` never reach the room.
|
|
18
|
+
*/
|
|
19
|
+
import { roomCapabilities } from '../declaration.js';
|
|
20
|
+
import { isDeclaredPath, parseLivePath, roomRef, } from '../protocol.js';
|
|
21
|
+
function text(status, body) {
|
|
22
|
+
return new Response(body, {
|
|
23
|
+
status,
|
|
24
|
+
headers: {
|
|
25
|
+
'Content-Type': 'text/plain; charset=utf-8',
|
|
26
|
+
'Cache-Control': 'no-store',
|
|
27
|
+
},
|
|
28
|
+
});
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* Whether an upgrade may open a socket: its `Origin` is the request's own
|
|
32
|
+
* origin or one of `extraOrigins` (the dev server's, whose proxy forwards
|
|
33
|
+
* to another port). Browsers always send `Origin` on a WebSocket
|
|
34
|
+
* handshake, so a cross-site page can't open a socket with a visitor's
|
|
35
|
+
* cookie; a request without `Origin` is a non-browser client, which gets
|
|
36
|
+
* nothing its own credentials don't give it.
|
|
37
|
+
*/
|
|
38
|
+
export function isAllowedOrigin(request, extraOrigins) {
|
|
39
|
+
const origin = request.headers.get('origin');
|
|
40
|
+
if (origin === null)
|
|
41
|
+
return true;
|
|
42
|
+
if (origin === new URL(request.url).origin)
|
|
43
|
+
return true;
|
|
44
|
+
return extraOrigins.includes(origin);
|
|
45
|
+
}
|
|
46
|
+
function isUpgrade(request) {
|
|
47
|
+
return request.headers.get('upgrade')?.toLowerCase() === 'websocket';
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* The declaration's bindings from a Worker `env`, or a clear error naming
|
|
51
|
+
* the missing one and where to enable it. This is the server's one way to
|
|
52
|
+
* reach them (the app's `Bindings` service doesn't carry them: only the
|
|
53
|
+
* Worker entry and the room use them, outside Effect).
|
|
54
|
+
*
|
|
55
|
+
* @throws Error when the Durable Object (or a declared rate limit) binding is missing.
|
|
56
|
+
*/
|
|
57
|
+
export function liveRoomsEnv(decl, env) {
|
|
58
|
+
const record = (typeof env === 'object' && env !== null ? env : {});
|
|
59
|
+
const { binding, className } = decl.durableObject;
|
|
60
|
+
const ns = record[binding];
|
|
61
|
+
if (typeof ns?.idFromName !== 'function' || typeof ns.get !== 'function') {
|
|
62
|
+
throw new Error(`live rooms: the Worker env has no Durable Object binding \`${binding}\` (class \`${className}\`). ` +
|
|
63
|
+
'Enable live rooms in alchemy.run.ts (`liveRoomsBindings` from @alecsibilia/ui-kit/alchemy) (local workerd derives it from the resource declaration), ' +
|
|
64
|
+
`and export \`${className}\` from the Worker entry.`);
|
|
65
|
+
}
|
|
66
|
+
let limiter;
|
|
67
|
+
if (decl.connectLimit) {
|
|
68
|
+
const value = record[decl.connectLimit.binding];
|
|
69
|
+
if (typeof value?.limit !== 'function') {
|
|
70
|
+
throw new Error(`live rooms: the Worker env has no rate limit binding \`${decl.connectLimit.binding}\` ` +
|
|
71
|
+
'(declared as `connectLimit`). Add it through `liveRoomsBindings` (local workerd derives it from the resource declaration).');
|
|
72
|
+
}
|
|
73
|
+
limiter = value;
|
|
74
|
+
}
|
|
75
|
+
return { rooms: ns, limiter };
|
|
76
|
+
}
|
|
77
|
+
/** Handshake headers the room needs; nothing else is forwarded. */
|
|
78
|
+
const FORWARDED_HEADERS = [
|
|
79
|
+
'upgrade',
|
|
80
|
+
'connection',
|
|
81
|
+
'sec-websocket-key',
|
|
82
|
+
'sec-websocket-version',
|
|
83
|
+
'sec-websocket-protocol',
|
|
84
|
+
'sec-websocket-extensions',
|
|
85
|
+
];
|
|
86
|
+
/** A 128-bit random peer id (base64url). */
|
|
87
|
+
export function randomPeerId() {
|
|
88
|
+
const bytes = crypto.getRandomValues(new Uint8Array(12));
|
|
89
|
+
let binary = '';
|
|
90
|
+
for (const byte of bytes)
|
|
91
|
+
binary += String.fromCharCode(byte);
|
|
92
|
+
return btoa(binary).replace(/\+/g, '-').replace(/\//g, '_');
|
|
93
|
+
}
|
|
94
|
+
/**
|
|
95
|
+
* Builds the Worker route for a declaration and the app's hooks.
|
|
96
|
+
* `undefined` means "not a live room path"; the Worker goes on routing.
|
|
97
|
+
*/
|
|
98
|
+
export function createLiveRoomsRoute(decl, hooks) {
|
|
99
|
+
const typeHooks = hooks.types;
|
|
100
|
+
return (request, env, options = {}) => {
|
|
101
|
+
const path = parseLivePath(new URL(request.url).pathname);
|
|
102
|
+
if (path === undefined)
|
|
103
|
+
return undefined;
|
|
104
|
+
return connect(request, env, options, path);
|
|
105
|
+
};
|
|
106
|
+
async function connect(request, env, options, path) {
|
|
107
|
+
if (path === undefined ||
|
|
108
|
+
path === 'Bad' ||
|
|
109
|
+
!isDeclaredPath(decl, path)) {
|
|
110
|
+
return text(404, 'No such room');
|
|
111
|
+
}
|
|
112
|
+
const type = typeHooks[path.type];
|
|
113
|
+
if (!type || (type.key && !type.key(path.key)))
|
|
114
|
+
return text(404, 'No such room');
|
|
115
|
+
if (!isUpgrade(request))
|
|
116
|
+
return text(426, 'Expected a WebSocket upgrade');
|
|
117
|
+
if (!isAllowedOrigin(request, options.extraOrigins ?? [])) {
|
|
118
|
+
return text(403, 'Cross-origin WebSocket upgrade refused');
|
|
119
|
+
}
|
|
120
|
+
const bindings = liveRoomsEnv(decl, env);
|
|
121
|
+
if (bindings.limiter) {
|
|
122
|
+
const ip = request.headers.get('cf-connecting-ip') ?? 'local';
|
|
123
|
+
const { success } = await bindings.limiter.limit({ key: ip });
|
|
124
|
+
if (!success)
|
|
125
|
+
return text(429, 'Too many connections');
|
|
126
|
+
}
|
|
127
|
+
const room = roomRef(decl, path.type, path.key);
|
|
128
|
+
const ctx = { env, now: options.now ?? Date.now() };
|
|
129
|
+
const user = await hooks.resolveUser(request, env);
|
|
130
|
+
if (!(await type.authorize(user, room, 'join', ctx)))
|
|
131
|
+
return text(403, 'Not allowed in this room');
|
|
132
|
+
const caps = roomCapabilities(decl, path.type);
|
|
133
|
+
const access = caps &&
|
|
134
|
+
caps.docs.length > 0 &&
|
|
135
|
+
(await type.authorize(user, room, 'edit', ctx))
|
|
136
|
+
? 'Editor'
|
|
137
|
+
: 'Reader';
|
|
138
|
+
const identity = {
|
|
139
|
+
peer: randomPeerId(),
|
|
140
|
+
user,
|
|
141
|
+
access,
|
|
142
|
+
socket: path._tag === 'Room'
|
|
143
|
+
? { _tag: 'Room' }
|
|
144
|
+
: { _tag: 'Doc', doc: path.doc },
|
|
145
|
+
room: { type: path.type, key: path.key },
|
|
146
|
+
};
|
|
147
|
+
const headers = new Headers();
|
|
148
|
+
for (const name of FORWARDED_HEADERS) {
|
|
149
|
+
const value = request.headers.get(name);
|
|
150
|
+
if (value !== null)
|
|
151
|
+
headers.set(name, value);
|
|
152
|
+
}
|
|
153
|
+
headers.set(IDENTITY_HEADER, JSON.stringify(identity));
|
|
154
|
+
const ns = bindings.rooms;
|
|
155
|
+
return ns
|
|
156
|
+
.get(ns.idFromName(room.id))
|
|
157
|
+
.fetch(new Request(request.url, { method: 'GET', headers }));
|
|
158
|
+
}
|
|
159
|
+
}
|