trellis 3.1.35 → 3.2.2
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 +23 -5
- package/README.md +63 -156
- package/bin/trellis.mjs +2 -75
- package/dist/{better-sqlite-backend-ahx5p0br.js → better-sqlite-backend-2P4F6AOJ.js} +91 -29
- package/dist/boot-middleware-MHLHFCUC.js +9 -0
- package/dist/browser/index.d.ts +18 -0
- package/dist/browser/index.d.ts.map +1 -0
- package/dist/browser/index.js +95 -0
- package/dist/{index-53f3b8p8.js → chunk-2W3MHTMG.js} +552 -213
- package/dist/chunk-44FFQKQX.js +142 -0
- package/dist/chunk-4HYPLFJD.js +150 -0
- package/dist/chunk-4XIVMVHC.js +361 -0
- package/dist/{index-yp88he8n.js → chunk-6CTA6MCE.js} +88 -64
- package/dist/chunk-6GEZ6DN5.js +394 -0
- package/dist/chunk-7ZFRVCUE.js +211 -0
- package/dist/chunk-CAG4FULI.js +121 -0
- package/dist/chunk-FF36CQHZ.js +61 -0
- package/dist/{index-h32txmxe.js → chunk-FKQO5A3H.js} +6 -13
- package/dist/{index-nq520y6k.js → chunk-FWRS4CSC.js} +46 -32
- package/dist/chunk-GFZCJ4EH.js +108 -0
- package/dist/chunk-GLM4HGN4.js +55 -0
- package/dist/{index-4cdr7x2x.js → chunk-GU374KWZ.js} +182 -111
- package/dist/chunk-H6JB3PZ3.js +45 -0
- package/dist/chunk-IZX2PLIB.js +1240 -0
- package/dist/chunk-J2TXUFCZ.js +289 -0
- package/dist/chunk-J3WYZO3Q.js +211 -0
- package/dist/{index-xzym9w0m.js → chunk-JA7AIHRK.js} +12 -7
- package/dist/chunk-JHHEXEDM.js +162 -0
- package/dist/chunk-JUEMDWLU.js +233 -0
- package/dist/chunk-KFQGP6VL.js +33 -0
- package/dist/chunk-KIJITUZB.js +419 -0
- package/dist/{index-4wxa8xz4.js → chunk-KQ4A2D2P.js} +162 -23
- package/dist/chunk-KSU2GW22.js +958 -0
- package/dist/{index-rv1p47gp.js → chunk-L5N5PR2S.js} +3015 -2480
- package/dist/{index-h9e2efx4.js → chunk-LNUIGRDZ.js} +71 -55
- package/dist/{index-skhn0agf.js → chunk-MLC5BUYO.js} +19 -20
- package/dist/chunk-MPXUVGT3.js +104 -0
- package/dist/chunk-N6MYZT4O.js +68 -0
- package/dist/chunk-O2AV5WMN.js +48 -0
- package/dist/chunk-PZ4XYZ4J.js +143 -0
- package/dist/chunk-QB5ISE47.js +419 -0
- package/dist/chunk-RSWUFTO2.js +857 -0
- package/dist/chunk-S3XLFZ27.js +1095 -0
- package/dist/{index-w5wccer3.js → chunk-S6GBJ2ZB.js} +28 -12
- package/dist/{index-7pjz3tsy.js → chunk-SGMDTWJJ.js} +57 -15
- package/dist/chunk-SPSPV5BC.js +390 -0
- package/dist/chunk-TCKXSOBP.js +123 -0
- package/dist/chunk-UBGUDMUV.js +59 -0
- package/dist/{index-wt8rz4gn.js → chunk-UQISRW6T.js} +97 -26
- package/dist/chunk-USEINYUL.js +12 -0
- package/dist/{index-bmyt7k8n.js → chunk-UZRUP7QW.js} +12 -4
- package/dist/{index-xhcp6xrn.js → chunk-VBYUTRXX.js} +695 -514
- package/dist/{index-vkpkfwhq.js → chunk-VDAKEOML.js} +234 -194
- package/dist/chunk-WXSWA3MV.js +0 -0
- package/dist/{index-n9f2qyh5.js → chunk-X5FLTRUB.js} +55 -33
- package/dist/{index-h7zxhhhh.js → chunk-XMQ7G77X.js} +46 -38
- package/dist/{index-w7ng765c.js → chunk-YCM7ZYEZ.js} +102 -49
- package/dist/cli/deploy-cli.d.ts +18 -0
- package/dist/cli/deploy-cli.d.ts.map +1 -0
- package/dist/cli/errors.d.ts +6 -0
- package/dist/cli/errors.d.ts.map +1 -0
- package/dist/cli/examples.d.ts +23 -0
- package/dist/cli/examples.d.ts.map +1 -0
- package/dist/cli/index.d.ts +1 -1
- package/dist/cli/index.d.ts.map +1 -1
- package/dist/cli/index.js +2426 -6225
- package/dist/client/index.browser.d.ts +29 -0
- package/dist/client/index.browser.d.ts.map +1 -0
- package/dist/client/index.browser.js +64 -0
- package/dist/client/index.d.ts +6 -1
- package/dist/client/index.d.ts.map +1 -1
- package/dist/client/index.js +41 -21
- package/dist/client/live.d.ts +67 -0
- package/dist/client/live.d.ts.map +1 -0
- package/dist/client/reactive.d.ts +31 -0
- package/dist/client/reactive.d.ts.map +1 -0
- package/dist/client/sdk.browser.d.ts +104 -0
- package/dist/client/sdk.browser.d.ts.map +1 -0
- package/dist/client/sdk.browser.js +9 -0
- package/dist/client/sdk.d.ts +26 -2
- package/dist/client/sdk.d.ts.map +1 -1
- package/dist/client/sdk.js +9 -0
- package/dist/client/vcs-client.d.ts +129 -0
- package/dist/client/vcs-client.d.ts.map +1 -0
- package/dist/cms/index.js +171 -163
- package/dist/{config-8hczw0rs.js → config-PFTP67TR.js} +8 -9
- package/dist/core/computation/index.d.ts +5 -0
- package/dist/core/computation/index.d.ts.map +1 -0
- package/dist/core/computation/rollup.d.ts +18 -0
- package/dist/core/computation/rollup.d.ts.map +1 -0
- package/dist/core/index.d.ts +5 -0
- package/dist/core/index.d.ts.map +1 -1
- package/dist/core/index.js +171 -104
- package/dist/core/kernel/boot-middleware.d.ts +10 -0
- package/dist/core/kernel/boot-middleware.d.ts.map +1 -0
- package/dist/core/kernel/logic-middleware.d.ts +7 -1
- package/dist/core/kernel/logic-middleware.d.ts.map +1 -1
- package/dist/core/kernel/trellis-kernel.d.ts.map +1 -1
- package/dist/core/ontology/types.d.ts +5 -0
- package/dist/core/ontology/types.d.ts.map +1 -1
- package/dist/core/persist/better-sqlite-backend.d.ts +9 -2
- package/dist/core/persist/better-sqlite-backend.d.ts.map +1 -1
- package/dist/core/persist/factory.d.ts +6 -7
- package/dist/core/persist/factory.d.ts.map +1 -1
- package/dist/core/persist/factory.js +2 -3
- package/dist/core/persist/sqljs-backend.js +2 -3
- package/dist/db/index.d.ts +4 -1
- package/dist/db/index.d.ts.map +1 -1
- package/dist/db/index.js +67 -54
- package/dist/db/inspector.js +11 -19
- package/dist/db/trellis.css +1 -1
- package/dist/decisions/index.js +9 -10
- package/dist/deploy-LQ7GUGJ5.js +9 -0
- package/dist/deploy-YNVG5E4E.js +9 -0
- package/dist/deploy-cli-24BCHAXY.js +67 -0
- package/dist/deploy-cli-C35HTITO.js +67 -0
- package/dist/embeddings/auto-embed.d.ts +2 -2
- package/dist/embeddings/auto-embed.d.ts.map +1 -1
- package/dist/embeddings/index.js +20 -22
- package/dist/embeddings/search.d.ts +2 -1
- package/dist/embeddings/search.d.ts.map +1 -1
- package/dist/embeddings/store.d.ts +25 -4
- package/dist/embeddings/store.d.ts.map +1 -1
- package/dist/engine.d.ts +59 -19
- package/dist/engine.d.ts.map +1 -1
- package/dist/import-UZEKXU3B.js +10 -0
- package/dist/index.js +102 -93
- package/dist/launch-explorer-HZ4ZJA4X.js +95 -0
- package/dist/links/index.js +24 -26
- package/dist/mcp/server.d.ts.map +1 -1
- package/dist/plugins/agent-memory/index.js +316 -0
- package/dist/plugins/idea-garden/index.js +165 -0
- package/dist/plugins/plan-approval/index.js +579 -0
- package/dist/plugins/proactive-watcher/index.js +194 -0
- package/dist/{index-2r4jxwnb.js → query-XYLTJYSA.js} +14 -15
- package/dist/react/index.js +39 -70
- package/dist/react/realtime.js +11 -0
- package/dist/react/schema-hooks.js +87 -0
- package/dist/realtime/broadcast-channel-transport.d.ts +43 -0
- package/dist/realtime/broadcast-channel-transport.d.ts.map +1 -0
- package/dist/realtime/durable-object-relay-transport.d.ts +96 -0
- package/dist/realtime/durable-object-relay-transport.d.ts.map +1 -0
- package/dist/realtime/index.d.ts +44 -0
- package/dist/realtime/index.d.ts.map +1 -0
- package/dist/realtime/index.js +40 -0
- package/dist/realtime/memory-hub.d.ts +32 -0
- package/dist/realtime/memory-hub.d.ts.map +1 -0
- package/dist/realtime/persistent-channel.d.ts +105 -0
- package/dist/realtime/persistent-channel.d.ts.map +1 -0
- package/dist/realtime/presence.d.ts +73 -0
- package/dist/realtime/presence.d.ts.map +1 -0
- package/dist/realtime/relay-persistence.d.ts +37 -0
- package/dist/realtime/relay-persistence.d.ts.map +1 -0
- package/dist/realtime/relay-persistence.js +107 -0
- package/dist/realtime/relay-server.d.ts +103 -0
- package/dist/realtime/relay-server.d.ts.map +1 -0
- package/dist/realtime/room.d.ts +98 -0
- package/dist/realtime/room.d.ts.map +1 -0
- package/dist/realtime/text.d.ts +73 -0
- package/dist/realtime/text.d.ts.map +1 -0
- package/dist/realtime/types.d.ts +89 -0
- package/dist/realtime/types.d.ts.map +1 -0
- package/dist/realtime/websocket-relay-transport.d.ts +28 -0
- package/dist/realtime/websocket-relay-transport.d.ts.map +1 -0
- package/dist/realtime.bundle.js +1100 -0
- package/dist/{remote-manager-xvbg4230.js → remote-manager-ODE3T2KR.js} +25 -15
- package/dist/scaffold/seed.d.ts.map +1 -1
- package/dist/scaffold/write.d.ts +1 -1
- package/dist/scaffold/write.d.ts.map +1 -1
- package/dist/schema/define.d.ts +137 -0
- package/dist/schema/define.d.ts.map +1 -0
- package/dist/schema/entity-projection.d.ts +34 -0
- package/dist/schema/entity-projection.d.ts.map +1 -0
- package/dist/schema/eql.d.ts +46 -0
- package/dist/schema/eql.d.ts.map +1 -0
- package/dist/schema/index.d.ts +21 -0
- package/dist/schema/index.d.ts.map +1 -0
- package/dist/schema/index.js +46 -0
- package/dist/schema/kernel-resolve.d.ts +21 -0
- package/dist/schema/kernel-resolve.d.ts.map +1 -0
- package/dist/schema/mutations.d.ts +20 -0
- package/dist/schema/mutations.d.ts.map +1 -0
- package/dist/schema/resolve.d.ts +21 -0
- package/dist/schema/resolve.d.ts.map +1 -0
- package/dist/server/deploy-meta.d.ts +25 -0
- package/dist/server/deploy-meta.d.ts.map +1 -0
- package/dist/server/deploy.d.ts +7 -2
- package/dist/server/deploy.d.ts.map +1 -1
- package/dist/server/index.d.ts +7 -2
- package/dist/server/index.d.ts.map +1 -1
- package/dist/server/index.js +100 -58
- package/dist/server/node-adapter.js +5 -104
- package/dist/server/realtime.d.ts +14 -3
- package/dist/server/realtime.d.ts.map +1 -1
- package/dist/server/server.d.ts +8 -16
- package/dist/server/server.d.ts.map +1 -1
- package/dist/server/sprites.d.ts +18 -1
- package/dist/server/sprites.d.ts.map +1 -1
- package/dist/server/tenancy.d.ts +4 -0
- package/dist/server/tenancy.d.ts.map +1 -1
- package/dist/server/usage-meter.d.ts +53 -0
- package/dist/server/usage-meter.d.ts.map +1 -0
- package/dist/server-MB3OPSPI.js +18 -0
- package/dist/server-ZVWGLLRF.js +18 -0
- package/dist/sprites-KQGTGM6W.js +27 -0
- package/dist/{sprites-vc4qbrp1.js → sprites-RU3E4XQN.js} +6 -7
- package/dist/svelte/index.d.ts +17 -0
- package/dist/svelte/index.d.ts.map +1 -0
- package/dist/svelte/index.js +51 -0
- package/dist/svelte/schema-hooks.d.ts +45 -0
- package/dist/svelte/schema-hooks.d.ts.map +1 -0
- package/dist/svelte/schema-hooks.js +71 -0
- package/dist/svelte/stores.d.ts +78 -0
- package/dist/svelte/stores.d.ts.map +1 -0
- package/dist/sync/http-transport.d.ts +3 -3
- package/dist/sync/http-transport.d.ts.map +1 -1
- package/dist/sync/index.d.ts +15 -1
- package/dist/sync/index.d.ts.map +1 -1
- package/dist/sync/index.js +617 -0
- package/dist/sync/memory-room.d.ts +37 -0
- package/dist/sync/memory-room.d.ts.map +1 -0
- package/dist/sync/memory-transport.d.ts +3 -3
- package/dist/sync/memory-transport.d.ts.map +1 -1
- package/dist/sync/partykit-room.d.ts +40 -0
- package/dist/sync/partykit-room.d.ts.map +1 -0
- package/dist/sync/partykit-transport.d.ts +84 -0
- package/dist/sync/partykit-transport.d.ts.map +1 -0
- package/dist/sync/room-core.d.ts +53 -0
- package/dist/sync/room-core.d.ts.map +1 -0
- package/dist/sync/sync-engine.d.ts +40 -2
- package/dist/sync/sync-engine.d.ts.map +1 -1
- package/dist/sync/sync-room-server.d.ts +39 -0
- package/dist/sync/sync-room-server.d.ts.map +1 -0
- package/dist/sync/types.d.ts +70 -3
- package/dist/sync/types.d.ts.map +1 -1
- package/dist/sync/vcs-sync-peer.d.ts +63 -0
- package/dist/sync/vcs-sync-peer.d.ts.map +1 -0
- package/dist/sync/ws-transport.d.ts +2 -2
- package/dist/sync/ws-transport.d.ts.map +1 -1
- package/dist/tenancy-NBLLGCNJ.js +14 -0
- package/dist/tenancy-POMWQP6M.js +16 -0
- package/dist/ui/client.html +355 -67
- package/dist/ui/launch-explorer.d.ts +22 -0
- package/dist/ui/launch-explorer.d.ts.map +1 -0
- package/dist/ui/server.d.ts +1 -1
- package/dist/ui/server.d.ts.map +1 -1
- package/dist/vcs/blob-store.d.ts.map +1 -1
- package/dist/vcs/idb-op-log.d.ts +67 -0
- package/dist/vcs/idb-op-log.d.ts.map +1 -0
- package/dist/vcs/index.d.ts +1 -0
- package/dist/vcs/index.d.ts.map +1 -1
- package/dist/vcs/index.js +81 -72
- package/dist/vcs/issue.d.ts +22 -5
- package/dist/vcs/issue.d.ts.map +1 -1
- package/dist/vcs/lane-materialize.d.ts +5 -0
- package/dist/vcs/lane-materialize.d.ts.map +1 -1
- package/dist/vcs/op-log.d.ts +28 -2
- package/dist/vcs/op-log.d.ts.map +1 -1
- package/dist/vcs/ops.d.ts +10 -0
- package/dist/vcs/ops.d.ts.map +1 -1
- package/dist/vcs/types.d.ts +2 -0
- package/dist/vcs/types.d.ts.map +1 -1
- package/dist/{vm-config-6xhsj6b3.js → vm-config-JI4OXKDQ.js} +9 -10
- package/dist/vue/hooks.d.ts +64 -0
- package/dist/vue/hooks.d.ts.map +1 -0
- package/dist/vue/index.d.ts +19 -0
- package/dist/vue/index.d.ts.map +1 -0
- package/dist/vue/index.js +59 -0
- package/dist/vue/schema-hooks.d.ts +41 -0
- package/dist/vue/schema-hooks.d.ts.map +1 -0
- package/dist/vue/schema-hooks.js +64 -0
- package/package.json +130 -17
- package/dist/deploy-999q207z.js +0 -10
- package/dist/engine-y0724kjq.js +0 -8
- package/dist/import-s2b8e0ft.js +0 -11
- package/dist/index-a76rekgs.js +0 -67
- package/dist/index-c9h37r6h.js +0 -1
- package/dist/index-hr9qvv77.js +0 -860
- package/dist/index-k5b0xskw.js +0 -1
- package/dist/index-nzb3am4f.js +0 -80
- package/dist/index-wncptktd.js +0 -292
- package/dist/index-y6a4kj0p.js +0 -43
- package/dist/index-yhwjgfvj.js +0 -342
- package/dist/sdk-bepky0xs.js +0 -16
- package/dist/server-szdjx0nt.js +0 -13
- package/dist/sqlite-backend-0vsmc6qj.js +0 -8
- package/dist/tenancy-pjm32b4v.js +0 -14
|
@@ -0,0 +1,958 @@
|
|
|
1
|
+
import {
|
|
2
|
+
Signal
|
|
3
|
+
} from "./chunk-UBGUDMUV.js";
|
|
4
|
+
|
|
5
|
+
// src/realtime/room.ts
|
|
6
|
+
var RealtimeRoom = class _RealtimeRoom {
|
|
7
|
+
id;
|
|
8
|
+
transport;
|
|
9
|
+
myState;
|
|
10
|
+
peers = /* @__PURE__ */ new Map();
|
|
11
|
+
/** Sender timestamps from presence messages (not local lastSeen). */
|
|
12
|
+
presenceTsByPeer = /* @__PURE__ */ new Map();
|
|
13
|
+
channelHandlers = /* @__PURE__ */ new Map();
|
|
14
|
+
_presence = new Signal([]);
|
|
15
|
+
now;
|
|
16
|
+
heartbeatMs;
|
|
17
|
+
timeoutMs;
|
|
18
|
+
heartbeatTimer;
|
|
19
|
+
unsubscribe;
|
|
20
|
+
closed = false;
|
|
21
|
+
pendingReplay = null;
|
|
22
|
+
/** Ids of broadcasts already delivered — keeps replay idempotent (G-Set). */
|
|
23
|
+
seenMsgIds = /* @__PURE__ */ new Set();
|
|
24
|
+
constructor(opts) {
|
|
25
|
+
this.transport = opts.transport;
|
|
26
|
+
this.id = opts.transport.id;
|
|
27
|
+
this.myState = opts.initialPresence ?? {};
|
|
28
|
+
this.now = opts.now ?? (() => Date.now());
|
|
29
|
+
this.heartbeatMs = opts.heartbeatMs ?? 2e3;
|
|
30
|
+
this.timeoutMs = opts.timeoutMs ?? 6e3;
|
|
31
|
+
this.unsubscribe = this.transport.onMessage((m) => this.handle(m));
|
|
32
|
+
this.recomputePresence();
|
|
33
|
+
}
|
|
34
|
+
/** Join a room and announce presence. */
|
|
35
|
+
static join(opts) {
|
|
36
|
+
const room = new _RealtimeRoom(opts);
|
|
37
|
+
room.announceHello();
|
|
38
|
+
room.announcePresence();
|
|
39
|
+
room.startHeartbeat();
|
|
40
|
+
return room;
|
|
41
|
+
}
|
|
42
|
+
// -------------------------------------------------------------------------
|
|
43
|
+
// Presence
|
|
44
|
+
// -------------------------------------------------------------------------
|
|
45
|
+
/** The local peer id. */
|
|
46
|
+
get selfId() {
|
|
47
|
+
return this.id;
|
|
48
|
+
}
|
|
49
|
+
/** Current local presence state. */
|
|
50
|
+
getSelfState() {
|
|
51
|
+
return this.myState;
|
|
52
|
+
}
|
|
53
|
+
/** Merge a partial update into local presence and broadcast it. */
|
|
54
|
+
setPresence(partial) {
|
|
55
|
+
this.myState = { ...this.myState, ...partial };
|
|
56
|
+
this.recomputePresence();
|
|
57
|
+
this.announcePresence();
|
|
58
|
+
}
|
|
59
|
+
/** Replace local presence wholesale and broadcast it. */
|
|
60
|
+
replacePresence(state) {
|
|
61
|
+
this.myState = state;
|
|
62
|
+
this.recomputePresence();
|
|
63
|
+
this.announcePresence();
|
|
64
|
+
}
|
|
65
|
+
/** All peers including self (self first). */
|
|
66
|
+
getPresence() {
|
|
67
|
+
return this._presence.value;
|
|
68
|
+
}
|
|
69
|
+
/** Peers excluding self. */
|
|
70
|
+
getOthers() {
|
|
71
|
+
return this._presence.value.filter((p) => !p.self);
|
|
72
|
+
}
|
|
73
|
+
/** Subscribe to presence changes. Called immediately with current peers. */
|
|
74
|
+
onPresence(cb) {
|
|
75
|
+
const unsub = this._presence.subscribe(cb);
|
|
76
|
+
this.flushPendingReplay();
|
|
77
|
+
return unsub;
|
|
78
|
+
}
|
|
79
|
+
/** Reactive presence signal (for framework adapters). */
|
|
80
|
+
get presenceSignal() {
|
|
81
|
+
return this._presence;
|
|
82
|
+
}
|
|
83
|
+
// -------------------------------------------------------------------------
|
|
84
|
+
// Broadcast pub/sub
|
|
85
|
+
// -------------------------------------------------------------------------
|
|
86
|
+
/**
|
|
87
|
+
* Fire-and-forget broadcast to all other peers on a channel. Returns the
|
|
88
|
+
* stable message id assigned to this broadcast — persist it alongside an
|
|
89
|
+
* optimistic local render so an echoed copy (relay replay, reconnect) is
|
|
90
|
+
* deduplicated by id rather than re-rendered.
|
|
91
|
+
*/
|
|
92
|
+
broadcast(channel, event, payload) {
|
|
93
|
+
const id = this.newMsgId();
|
|
94
|
+
if (this.closed) return id;
|
|
95
|
+
this.seenMsgIds.add(id);
|
|
96
|
+
this.transport.send({
|
|
97
|
+
v: 1,
|
|
98
|
+
t: "msg",
|
|
99
|
+
from: this.id,
|
|
100
|
+
channel,
|
|
101
|
+
event,
|
|
102
|
+
payload,
|
|
103
|
+
ts: this.now(),
|
|
104
|
+
id
|
|
105
|
+
});
|
|
106
|
+
return id;
|
|
107
|
+
}
|
|
108
|
+
newMsgId() {
|
|
109
|
+
const rand = typeof globalThis.crypto?.randomUUID === "function" ? globalThis.crypto.randomUUID() : Math.random().toString(36).slice(2);
|
|
110
|
+
return `${this.id}:${rand}`;
|
|
111
|
+
}
|
|
112
|
+
/** Subscribe to broadcasts on a channel. Returns an unsubscribe fn. */
|
|
113
|
+
on(channel, handler) {
|
|
114
|
+
let set = this.channelHandlers.get(channel);
|
|
115
|
+
if (!set) {
|
|
116
|
+
set = /* @__PURE__ */ new Set();
|
|
117
|
+
this.channelHandlers.set(channel, set);
|
|
118
|
+
}
|
|
119
|
+
set.add(handler);
|
|
120
|
+
this.flushPendingReplay();
|
|
121
|
+
return () => {
|
|
122
|
+
set.delete(handler);
|
|
123
|
+
};
|
|
124
|
+
}
|
|
125
|
+
// -------------------------------------------------------------------------
|
|
126
|
+
// Lifecycle
|
|
127
|
+
// -------------------------------------------------------------------------
|
|
128
|
+
/**
|
|
129
|
+
* Apply a relay replay batch (chat history, text snapshot, presence).
|
|
130
|
+
* Used when reconnecting to a hub with {@link RelayPersistence}.
|
|
131
|
+
*/
|
|
132
|
+
replay(messages) {
|
|
133
|
+
for (const message of messages) {
|
|
134
|
+
this.integrateRemote(message);
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
/** Announce departure and tear down. */
|
|
138
|
+
leave() {
|
|
139
|
+
if (this.closed) return;
|
|
140
|
+
this.closed = true;
|
|
141
|
+
try {
|
|
142
|
+
this.transport.send({ v: 1, t: "bye", from: this.id, ts: this.now() });
|
|
143
|
+
} catch {
|
|
144
|
+
}
|
|
145
|
+
if (this.heartbeatTimer !== void 0) {
|
|
146
|
+
clearInterval(this.heartbeatTimer);
|
|
147
|
+
this.heartbeatTimer = void 0;
|
|
148
|
+
}
|
|
149
|
+
this.unsubscribe();
|
|
150
|
+
this.transport.close();
|
|
151
|
+
}
|
|
152
|
+
// -------------------------------------------------------------------------
|
|
153
|
+
// Internal
|
|
154
|
+
// -------------------------------------------------------------------------
|
|
155
|
+
handle(message) {
|
|
156
|
+
if (this.closed) return;
|
|
157
|
+
if (message.t === "replay") {
|
|
158
|
+
this.pendingReplay = message.messages;
|
|
159
|
+
this.flushPendingReplay();
|
|
160
|
+
return;
|
|
161
|
+
}
|
|
162
|
+
if (message.from === this.id) return;
|
|
163
|
+
this.integrateRemote(message);
|
|
164
|
+
}
|
|
165
|
+
flushPendingReplay() {
|
|
166
|
+
if (!this.pendingReplay || this.pendingReplay.length === 0) return;
|
|
167
|
+
if (!this.hasActiveSubscribers()) return;
|
|
168
|
+
const batch = this.pendingReplay;
|
|
169
|
+
this.pendingReplay = null;
|
|
170
|
+
this.replay(batch);
|
|
171
|
+
}
|
|
172
|
+
hasActiveSubscribers() {
|
|
173
|
+
if (this._presence.hasSubscribers()) return true;
|
|
174
|
+
for (const handlers of this.channelHandlers.values()) {
|
|
175
|
+
if (handlers.size > 0) return true;
|
|
176
|
+
}
|
|
177
|
+
return false;
|
|
178
|
+
}
|
|
179
|
+
integrateRemote(message) {
|
|
180
|
+
switch (message.t) {
|
|
181
|
+
case "hello":
|
|
182
|
+
this.announcePresence();
|
|
183
|
+
break;
|
|
184
|
+
case "presence":
|
|
185
|
+
this.upsertPeer(message.from, message.state, message.ts);
|
|
186
|
+
break;
|
|
187
|
+
case "bye":
|
|
188
|
+
if (this.peers.delete(message.from)) {
|
|
189
|
+
this.presenceTsByPeer.delete(message.from);
|
|
190
|
+
this.recomputePresence();
|
|
191
|
+
}
|
|
192
|
+
break;
|
|
193
|
+
case "msg": {
|
|
194
|
+
if (message.id !== void 0 && this.seenMsgIds.has(message.id)) return;
|
|
195
|
+
const handlers = this.channelHandlers.get(message.channel);
|
|
196
|
+
if (!handlers || handlers.size === 0) return;
|
|
197
|
+
if (message.id !== void 0) this.seenMsgIds.add(message.id);
|
|
198
|
+
const event = {
|
|
199
|
+
from: message.from,
|
|
200
|
+
channel: message.channel,
|
|
201
|
+
event: message.event,
|
|
202
|
+
payload: message.payload,
|
|
203
|
+
ts: message.ts,
|
|
204
|
+
id: message.id
|
|
205
|
+
};
|
|
206
|
+
for (const handler of handlers) {
|
|
207
|
+
try {
|
|
208
|
+
handler(event);
|
|
209
|
+
} catch {
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
break;
|
|
213
|
+
}
|
|
214
|
+
case "replay":
|
|
215
|
+
break;
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
upsertPeer(id, state, ts) {
|
|
219
|
+
const prevTs = this.presenceTsByPeer.get(id) ?? 0;
|
|
220
|
+
if (ts < prevTs) {
|
|
221
|
+
const existing = this.peers.get(id);
|
|
222
|
+
if (existing) existing.lastSeen = this.now();
|
|
223
|
+
return;
|
|
224
|
+
}
|
|
225
|
+
this.presenceTsByPeer.set(id, ts);
|
|
226
|
+
this.peers.set(id, { id, state, lastSeen: this.now(), self: false });
|
|
227
|
+
this.recomputePresence();
|
|
228
|
+
}
|
|
229
|
+
announceHello() {
|
|
230
|
+
this.transport.send({ v: 1, t: "hello", from: this.id });
|
|
231
|
+
}
|
|
232
|
+
announcePresence() {
|
|
233
|
+
this.transport.send({
|
|
234
|
+
v: 1,
|
|
235
|
+
t: "presence",
|
|
236
|
+
from: this.id,
|
|
237
|
+
state: this.myState,
|
|
238
|
+
ts: this.now()
|
|
239
|
+
});
|
|
240
|
+
}
|
|
241
|
+
startHeartbeat() {
|
|
242
|
+
if (this.heartbeatMs <= 0 || typeof setInterval !== "function") {
|
|
243
|
+
return;
|
|
244
|
+
}
|
|
245
|
+
this.heartbeatTimer = setInterval(() => {
|
|
246
|
+
this.announcePresence();
|
|
247
|
+
this.pruneExpired();
|
|
248
|
+
}, this.heartbeatMs);
|
|
249
|
+
}
|
|
250
|
+
pruneExpired() {
|
|
251
|
+
const cutoff = this.now() - this.timeoutMs;
|
|
252
|
+
let changed = false;
|
|
253
|
+
for (const [id, peer] of this.peers) {
|
|
254
|
+
if (peer.lastSeen < cutoff) {
|
|
255
|
+
this.peers.delete(id);
|
|
256
|
+
this.presenceTsByPeer.delete(id);
|
|
257
|
+
changed = true;
|
|
258
|
+
}
|
|
259
|
+
}
|
|
260
|
+
if (changed) this.recomputePresence();
|
|
261
|
+
}
|
|
262
|
+
recomputePresence() {
|
|
263
|
+
const self = {
|
|
264
|
+
id: this.id,
|
|
265
|
+
state: this.myState,
|
|
266
|
+
lastSeen: this.now(),
|
|
267
|
+
self: true
|
|
268
|
+
};
|
|
269
|
+
const others = [...this.peers.values()].sort(
|
|
270
|
+
(a, b) => a.id.localeCompare(b.id)
|
|
271
|
+
);
|
|
272
|
+
this._presence.value = [self, ...others];
|
|
273
|
+
}
|
|
274
|
+
};
|
|
275
|
+
|
|
276
|
+
// src/realtime/memory-hub.ts
|
|
277
|
+
var MemoryHub = class {
|
|
278
|
+
transports = /* @__PURE__ */ new Set();
|
|
279
|
+
/** Create a transport bound to this hub for the given peer id. */
|
|
280
|
+
connect(id) {
|
|
281
|
+
const transport = new MemoryRealtimeTransport(this, id);
|
|
282
|
+
this.transports.add(transport);
|
|
283
|
+
return transport;
|
|
284
|
+
}
|
|
285
|
+
/** Number of currently connected transports. */
|
|
286
|
+
size() {
|
|
287
|
+
return this.transports.size;
|
|
288
|
+
}
|
|
289
|
+
/** @internal */
|
|
290
|
+
_broadcast(from, message) {
|
|
291
|
+
for (const transport of this.transports) {
|
|
292
|
+
if (transport === from) continue;
|
|
293
|
+
transport._deliver(message);
|
|
294
|
+
}
|
|
295
|
+
}
|
|
296
|
+
/** @internal */
|
|
297
|
+
_remove(transport) {
|
|
298
|
+
this.transports.delete(transport);
|
|
299
|
+
}
|
|
300
|
+
};
|
|
301
|
+
var MemoryRealtimeTransport = class {
|
|
302
|
+
id;
|
|
303
|
+
hub;
|
|
304
|
+
handlers = /* @__PURE__ */ new Set();
|
|
305
|
+
closed = false;
|
|
306
|
+
constructor(hub, id) {
|
|
307
|
+
this.hub = hub;
|
|
308
|
+
this.id = id;
|
|
309
|
+
}
|
|
310
|
+
send(message) {
|
|
311
|
+
if (this.closed) return;
|
|
312
|
+
this.hub._broadcast(this, message);
|
|
313
|
+
}
|
|
314
|
+
onMessage(handler) {
|
|
315
|
+
this.handlers.add(handler);
|
|
316
|
+
return () => {
|
|
317
|
+
this.handlers.delete(handler);
|
|
318
|
+
};
|
|
319
|
+
}
|
|
320
|
+
close() {
|
|
321
|
+
if (this.closed) return;
|
|
322
|
+
this.closed = true;
|
|
323
|
+
this.hub._remove(this);
|
|
324
|
+
this.handlers.clear();
|
|
325
|
+
}
|
|
326
|
+
/** @internal */
|
|
327
|
+
_deliver(message) {
|
|
328
|
+
if (this.closed) return;
|
|
329
|
+
for (const handler of this.handlers) {
|
|
330
|
+
try {
|
|
331
|
+
handler(message);
|
|
332
|
+
} catch {
|
|
333
|
+
}
|
|
334
|
+
}
|
|
335
|
+
}
|
|
336
|
+
};
|
|
337
|
+
|
|
338
|
+
// src/realtime/broadcast-channel-transport.ts
|
|
339
|
+
var BroadcastChannelTransport = class {
|
|
340
|
+
id;
|
|
341
|
+
bc;
|
|
342
|
+
handlers = /* @__PURE__ */ new Set();
|
|
343
|
+
closed = false;
|
|
344
|
+
constructor(opts) {
|
|
345
|
+
this.id = opts.id;
|
|
346
|
+
const Impl = opts.BroadcastChannelImpl ?? globalThis.BroadcastChannel;
|
|
347
|
+
if (!Impl) {
|
|
348
|
+
throw new Error(
|
|
349
|
+
"BroadcastChannelTransport requires BroadcastChannel or opts.BroadcastChannelImpl."
|
|
350
|
+
);
|
|
351
|
+
}
|
|
352
|
+
this.bc = new Impl(opts.channel);
|
|
353
|
+
const onIncoming = (event) => {
|
|
354
|
+
const message = event.data;
|
|
355
|
+
if (!message || typeof message !== "object" || message.v !== 1) return;
|
|
356
|
+
for (const handler of this.handlers) {
|
|
357
|
+
try {
|
|
358
|
+
handler(message);
|
|
359
|
+
} catch {
|
|
360
|
+
}
|
|
361
|
+
}
|
|
362
|
+
};
|
|
363
|
+
if (this.bc.addEventListener) {
|
|
364
|
+
this.bc.addEventListener("message", onIncoming);
|
|
365
|
+
} else {
|
|
366
|
+
this.bc.onmessage = onIncoming;
|
|
367
|
+
}
|
|
368
|
+
}
|
|
369
|
+
send(message) {
|
|
370
|
+
if (this.closed) return;
|
|
371
|
+
this.bc.postMessage(message);
|
|
372
|
+
}
|
|
373
|
+
onMessage(handler) {
|
|
374
|
+
this.handlers.add(handler);
|
|
375
|
+
return () => {
|
|
376
|
+
this.handlers.delete(handler);
|
|
377
|
+
};
|
|
378
|
+
}
|
|
379
|
+
close() {
|
|
380
|
+
if (this.closed) return;
|
|
381
|
+
this.closed = true;
|
|
382
|
+
this.handlers.clear();
|
|
383
|
+
try {
|
|
384
|
+
this.bc.close();
|
|
385
|
+
} catch {
|
|
386
|
+
}
|
|
387
|
+
}
|
|
388
|
+
};
|
|
389
|
+
|
|
390
|
+
// src/realtime/websocket-relay-transport.ts
|
|
391
|
+
var WebSocketRelayTransport = class {
|
|
392
|
+
id;
|
|
393
|
+
ws;
|
|
394
|
+
handlers = /* @__PURE__ */ new Set();
|
|
395
|
+
closed = false;
|
|
396
|
+
pending = [];
|
|
397
|
+
constructor(opts) {
|
|
398
|
+
this.id = opts.id;
|
|
399
|
+
const WS = opts.WebSocketImpl ?? globalThis.WebSocket;
|
|
400
|
+
if (!WS) {
|
|
401
|
+
throw new Error(
|
|
402
|
+
"WebSocketRelayTransport requires WebSocket or opts.WebSocketImpl."
|
|
403
|
+
);
|
|
404
|
+
}
|
|
405
|
+
this.ws = new WS(opts.url);
|
|
406
|
+
this.ws.addEventListener("open", () => this.flushPending());
|
|
407
|
+
this.ws.addEventListener("message", (event) => {
|
|
408
|
+
let message;
|
|
409
|
+
try {
|
|
410
|
+
message = JSON.parse(String(event.data));
|
|
411
|
+
} catch {
|
|
412
|
+
return;
|
|
413
|
+
}
|
|
414
|
+
if (!message || typeof message !== "object" || message.v !== 1) return;
|
|
415
|
+
for (const handler of this.handlers) {
|
|
416
|
+
try {
|
|
417
|
+
handler(message);
|
|
418
|
+
} catch {
|
|
419
|
+
}
|
|
420
|
+
}
|
|
421
|
+
});
|
|
422
|
+
this.ws.addEventListener("close", () => {
|
|
423
|
+
if (!this.closed) this.pending = [];
|
|
424
|
+
});
|
|
425
|
+
}
|
|
426
|
+
send(message) {
|
|
427
|
+
if (this.closed) return;
|
|
428
|
+
if (this.ws.readyState === 1) {
|
|
429
|
+
this.ws.send(JSON.stringify(message));
|
|
430
|
+
return;
|
|
431
|
+
}
|
|
432
|
+
this.pending.push(message);
|
|
433
|
+
}
|
|
434
|
+
onMessage(handler) {
|
|
435
|
+
this.handlers.add(handler);
|
|
436
|
+
return () => {
|
|
437
|
+
this.handlers.delete(handler);
|
|
438
|
+
};
|
|
439
|
+
}
|
|
440
|
+
close() {
|
|
441
|
+
if (this.closed) return;
|
|
442
|
+
this.closed = true;
|
|
443
|
+
this.pending = [];
|
|
444
|
+
this.handlers.clear();
|
|
445
|
+
try {
|
|
446
|
+
this.ws.close();
|
|
447
|
+
} catch {
|
|
448
|
+
}
|
|
449
|
+
}
|
|
450
|
+
flushPending() {
|
|
451
|
+
if (this.ws.readyState !== 1) return;
|
|
452
|
+
const batch = this.pending.splice(0);
|
|
453
|
+
for (const message of batch) {
|
|
454
|
+
this.ws.send(JSON.stringify(message));
|
|
455
|
+
}
|
|
456
|
+
}
|
|
457
|
+
};
|
|
458
|
+
|
|
459
|
+
// src/realtime/durable-object-relay-transport.ts
|
|
460
|
+
var DEFAULT_RECONNECT = {
|
|
461
|
+
maxAttempts: 0,
|
|
462
|
+
baseDelayMs: 500,
|
|
463
|
+
maxDelayMs: 1e4
|
|
464
|
+
};
|
|
465
|
+
function defaultBuildUrl(opts) {
|
|
466
|
+
let url = opts.url;
|
|
467
|
+
if (opts.room) {
|
|
468
|
+
url = `${url.replace(/\/$/, "")}/${encodeURIComponent(opts.room)}`;
|
|
469
|
+
}
|
|
470
|
+
if (opts.auth) {
|
|
471
|
+
url += `${url.includes("?") ? "&" : "?"}token=${encodeURIComponent(opts.auth)}`;
|
|
472
|
+
}
|
|
473
|
+
return url;
|
|
474
|
+
}
|
|
475
|
+
var DurableObjectRelayTransport = class {
|
|
476
|
+
id;
|
|
477
|
+
url;
|
|
478
|
+
WS;
|
|
479
|
+
reconnect;
|
|
480
|
+
maxPending;
|
|
481
|
+
ws = null;
|
|
482
|
+
handlers = /* @__PURE__ */ new Set();
|
|
483
|
+
pending = [];
|
|
484
|
+
closed = false;
|
|
485
|
+
attempts = 0;
|
|
486
|
+
reconnectTimer = null;
|
|
487
|
+
constructor(opts) {
|
|
488
|
+
this.id = opts.id;
|
|
489
|
+
const WS = opts.WebSocketImpl ?? globalThis.WebSocket;
|
|
490
|
+
if (!WS) {
|
|
491
|
+
throw new Error(
|
|
492
|
+
"DurableObjectRelayTransport requires WebSocket or opts.WebSocketImpl."
|
|
493
|
+
);
|
|
494
|
+
}
|
|
495
|
+
this.WS = WS;
|
|
496
|
+
this.url = (opts.buildUrl ?? defaultBuildUrl)({
|
|
497
|
+
url: opts.url,
|
|
498
|
+
room: opts.room,
|
|
499
|
+
auth: opts.auth
|
|
500
|
+
});
|
|
501
|
+
this.reconnect = opts.reconnect === false ? null : { ...DEFAULT_RECONNECT, ...opts.reconnect ?? {} };
|
|
502
|
+
this.maxPending = opts.maxPending ?? 128;
|
|
503
|
+
this.open();
|
|
504
|
+
}
|
|
505
|
+
send(message) {
|
|
506
|
+
if (this.closed) return;
|
|
507
|
+
if (this.ws && this.ws.readyState === 1) {
|
|
508
|
+
this.ws.send(JSON.stringify(message));
|
|
509
|
+
return;
|
|
510
|
+
}
|
|
511
|
+
this.pending.push(message);
|
|
512
|
+
if (this.pending.length > this.maxPending) {
|
|
513
|
+
this.pending.splice(0, this.pending.length - this.maxPending);
|
|
514
|
+
}
|
|
515
|
+
}
|
|
516
|
+
onMessage(handler) {
|
|
517
|
+
this.handlers.add(handler);
|
|
518
|
+
return () => {
|
|
519
|
+
this.handlers.delete(handler);
|
|
520
|
+
};
|
|
521
|
+
}
|
|
522
|
+
close() {
|
|
523
|
+
if (this.closed) return;
|
|
524
|
+
this.closed = true;
|
|
525
|
+
if (this.reconnectTimer) clearTimeout(this.reconnectTimer);
|
|
526
|
+
this.reconnectTimer = null;
|
|
527
|
+
this.pending = [];
|
|
528
|
+
this.handlers.clear();
|
|
529
|
+
try {
|
|
530
|
+
this.ws?.close();
|
|
531
|
+
} catch {
|
|
532
|
+
}
|
|
533
|
+
this.ws = null;
|
|
534
|
+
}
|
|
535
|
+
open() {
|
|
536
|
+
if (this.closed) return;
|
|
537
|
+
const ws = new this.WS(this.url);
|
|
538
|
+
this.ws = ws;
|
|
539
|
+
ws.addEventListener("open", () => {
|
|
540
|
+
if (this.closed) return;
|
|
541
|
+
this.attempts = 0;
|
|
542
|
+
try {
|
|
543
|
+
ws.send(JSON.stringify({ v: 1, t: "hello", from: this.id }));
|
|
544
|
+
} catch {
|
|
545
|
+
}
|
|
546
|
+
this.flushPending();
|
|
547
|
+
});
|
|
548
|
+
ws.addEventListener("message", (event) => {
|
|
549
|
+
const data = event.data;
|
|
550
|
+
let message;
|
|
551
|
+
try {
|
|
552
|
+
message = JSON.parse(String(data));
|
|
553
|
+
} catch {
|
|
554
|
+
return;
|
|
555
|
+
}
|
|
556
|
+
if (!message || typeof message !== "object" || message.v !== 1) return;
|
|
557
|
+
for (const handler of this.handlers) {
|
|
558
|
+
try {
|
|
559
|
+
handler(message);
|
|
560
|
+
} catch {
|
|
561
|
+
}
|
|
562
|
+
}
|
|
563
|
+
});
|
|
564
|
+
ws.addEventListener("close", () => {
|
|
565
|
+
if (this.ws === ws) this.ws = null;
|
|
566
|
+
this.scheduleReconnect();
|
|
567
|
+
});
|
|
568
|
+
ws.addEventListener("error", () => {
|
|
569
|
+
});
|
|
570
|
+
}
|
|
571
|
+
scheduleReconnect() {
|
|
572
|
+
if (this.closed || !this.reconnect || this.reconnectTimer) return;
|
|
573
|
+
if (this.reconnect.maxAttempts > 0 && this.attempts >= this.reconnect.maxAttempts) {
|
|
574
|
+
return;
|
|
575
|
+
}
|
|
576
|
+
const delay = Math.min(
|
|
577
|
+
this.reconnect.baseDelayMs * 2 ** this.attempts,
|
|
578
|
+
this.reconnect.maxDelayMs
|
|
579
|
+
);
|
|
580
|
+
this.attempts += 1;
|
|
581
|
+
this.reconnectTimer = setTimeout(() => {
|
|
582
|
+
this.reconnectTimer = null;
|
|
583
|
+
this.open();
|
|
584
|
+
}, delay);
|
|
585
|
+
this.reconnectTimer?.unref?.();
|
|
586
|
+
}
|
|
587
|
+
flushPending() {
|
|
588
|
+
if (!this.ws || this.ws.readyState !== 1) return;
|
|
589
|
+
const batch = this.pending.splice(0);
|
|
590
|
+
for (const message of batch) {
|
|
591
|
+
this.ws.send(JSON.stringify(message));
|
|
592
|
+
}
|
|
593
|
+
}
|
|
594
|
+
};
|
|
595
|
+
|
|
596
|
+
// src/realtime/presence.ts
|
|
597
|
+
function createPresenceTransport(opts) {
|
|
598
|
+
if (opts.transport) return opts.transport;
|
|
599
|
+
if (opts.relayUrl) {
|
|
600
|
+
return new DurableObjectRelayTransport({
|
|
601
|
+
id: opts.peerId,
|
|
602
|
+
url: opts.relayUrl,
|
|
603
|
+
room: opts.room,
|
|
604
|
+
auth: opts.auth,
|
|
605
|
+
WebSocketImpl: opts.WebSocketImpl
|
|
606
|
+
});
|
|
607
|
+
}
|
|
608
|
+
return new BroadcastChannelTransport({
|
|
609
|
+
id: opts.peerId,
|
|
610
|
+
channel: `presence:${opts.room}`,
|
|
611
|
+
BroadcastChannelImpl: opts.BroadcastChannelImpl
|
|
612
|
+
});
|
|
613
|
+
}
|
|
614
|
+
function joinPresence(opts) {
|
|
615
|
+
return RealtimeRoom.join({
|
|
616
|
+
transport: createPresenceTransport(opts),
|
|
617
|
+
initialPresence: opts.initialPresence,
|
|
618
|
+
heartbeatMs: opts.heartbeatMs,
|
|
619
|
+
timeoutMs: opts.timeoutMs,
|
|
620
|
+
now: opts.now
|
|
621
|
+
});
|
|
622
|
+
}
|
|
623
|
+
|
|
624
|
+
// src/realtime/persistent-channel.ts
|
|
625
|
+
var DEFAULT_MAX_RECORDS = 200;
|
|
626
|
+
var PersistentChannel = class _PersistentChannel {
|
|
627
|
+
/** Reactive, deduped, `(ts, id)`-sorted message list. */
|
|
628
|
+
messages = new Signal([]);
|
|
629
|
+
room;
|
|
630
|
+
channel;
|
|
631
|
+
event;
|
|
632
|
+
store;
|
|
633
|
+
max;
|
|
634
|
+
resolveMeta;
|
|
635
|
+
now;
|
|
636
|
+
records = [];
|
|
637
|
+
byId = /* @__PURE__ */ new Map();
|
|
638
|
+
unsubscribe;
|
|
639
|
+
disposed = false;
|
|
640
|
+
constructor(room, channel, opts) {
|
|
641
|
+
this.room = room;
|
|
642
|
+
this.channel = channel;
|
|
643
|
+
this.event = opts.event ?? "message";
|
|
644
|
+
this.store = opts.store;
|
|
645
|
+
this.max = opts.max ?? DEFAULT_MAX_RECORDS;
|
|
646
|
+
this.resolveMeta = opts.resolveMeta;
|
|
647
|
+
this.now = opts.now ?? (() => Date.now());
|
|
648
|
+
this.unsubscribe = this.room.on(this.channel, this.handleEvent);
|
|
649
|
+
void this.hydrate();
|
|
650
|
+
}
|
|
651
|
+
/** Create a persistent view over `room`'s `channel`. */
|
|
652
|
+
static create(room, channel, opts = {}) {
|
|
653
|
+
return new _PersistentChannel(room, channel, opts);
|
|
654
|
+
}
|
|
655
|
+
/**
|
|
656
|
+
* Optimistically record locally, broadcast to peers, and persist. Returns the
|
|
657
|
+
* stable message id. The room dedups its own id, so the broadcast never
|
|
658
|
+
* echoes back into this channel.
|
|
659
|
+
*/
|
|
660
|
+
send(payload, meta) {
|
|
661
|
+
const id = this.room.broadcast(this.channel, this.event, payload);
|
|
662
|
+
this.commitOne({ id, from: this.room.selfId, ts: this.now(), payload, meta });
|
|
663
|
+
return id;
|
|
664
|
+
}
|
|
665
|
+
/** Current records (deduped + sorted). Read without subscribing. */
|
|
666
|
+
snapshot() {
|
|
667
|
+
return this.messages.peek();
|
|
668
|
+
}
|
|
669
|
+
/** Stop listening. Does not clear the durable store. */
|
|
670
|
+
dispose() {
|
|
671
|
+
if (this.disposed) return;
|
|
672
|
+
this.disposed = true;
|
|
673
|
+
this.unsubscribe();
|
|
674
|
+
}
|
|
675
|
+
// --- internal --------------------------------------------------------------
|
|
676
|
+
handleEvent = (event) => {
|
|
677
|
+
if (event.event !== this.event) return;
|
|
678
|
+
this.commitOne({
|
|
679
|
+
id: event.id ?? `${event.from}:${event.ts}`,
|
|
680
|
+
from: event.from,
|
|
681
|
+
ts: event.ts,
|
|
682
|
+
payload: event.payload,
|
|
683
|
+
meta: this.resolveMeta?.(event)
|
|
684
|
+
});
|
|
685
|
+
};
|
|
686
|
+
async hydrate() {
|
|
687
|
+
if (!this.store) return;
|
|
688
|
+
let loaded;
|
|
689
|
+
try {
|
|
690
|
+
loaded = await this.store.load();
|
|
691
|
+
} catch {
|
|
692
|
+
return;
|
|
693
|
+
}
|
|
694
|
+
if (this.disposed) return;
|
|
695
|
+
let changed = false;
|
|
696
|
+
for (const rec of loaded) {
|
|
697
|
+
if (this.add(rec)) changed = true;
|
|
698
|
+
}
|
|
699
|
+
if (changed) this.commit();
|
|
700
|
+
}
|
|
701
|
+
/** Insert one record and publish if it was new. */
|
|
702
|
+
commitOne(rec) {
|
|
703
|
+
if (this.add(rec)) this.commit();
|
|
704
|
+
}
|
|
705
|
+
/** Pure insert with dedup + cap. Returns true when a new record was added. */
|
|
706
|
+
add(rec) {
|
|
707
|
+
if (!rec || !rec.id || this.byId.has(rec.id)) return false;
|
|
708
|
+
this.byId.set(rec.id, rec);
|
|
709
|
+
this.records.push(rec);
|
|
710
|
+
this.records.sort((a, b) => a.ts - b.ts || a.id.localeCompare(b.id));
|
|
711
|
+
if (this.records.length > this.max) {
|
|
712
|
+
const evicted = this.records.splice(0, this.records.length - this.max);
|
|
713
|
+
for (const m of evicted) this.byId.delete(m.id);
|
|
714
|
+
}
|
|
715
|
+
return true;
|
|
716
|
+
}
|
|
717
|
+
commit() {
|
|
718
|
+
this.messages.value = [...this.records];
|
|
719
|
+
void this.store?.save(this.records);
|
|
720
|
+
}
|
|
721
|
+
};
|
|
722
|
+
function localStorageChannelStore(key, storage = globalThis.localStorage) {
|
|
723
|
+
return {
|
|
724
|
+
load() {
|
|
725
|
+
try {
|
|
726
|
+
const raw = storage?.getItem(key);
|
|
727
|
+
const parsed = raw ? JSON.parse(raw) : [];
|
|
728
|
+
return Array.isArray(parsed) ? parsed : [];
|
|
729
|
+
} catch {
|
|
730
|
+
return [];
|
|
731
|
+
}
|
|
732
|
+
},
|
|
733
|
+
save(records) {
|
|
734
|
+
try {
|
|
735
|
+
storage?.setItem(key, JSON.stringify(records));
|
|
736
|
+
} catch {
|
|
737
|
+
}
|
|
738
|
+
}
|
|
739
|
+
};
|
|
740
|
+
}
|
|
741
|
+
|
|
742
|
+
// src/realtime/text.ts
|
|
743
|
+
function parseId(id) {
|
|
744
|
+
const at = id.indexOf("@");
|
|
745
|
+
return {
|
|
746
|
+
counter: Number(id.slice(0, at)),
|
|
747
|
+
peer: id.slice(at + 1)
|
|
748
|
+
};
|
|
749
|
+
}
|
|
750
|
+
function compareSiblings(a, b) {
|
|
751
|
+
const pa = parseId(a);
|
|
752
|
+
const pb = parseId(b);
|
|
753
|
+
if (pa.counter !== pb.counter) return pb.counter - pa.counter;
|
|
754
|
+
return pb.peer.localeCompare(pa.peer);
|
|
755
|
+
}
|
|
756
|
+
var RealtimeText = class {
|
|
757
|
+
nodes = /* @__PURE__ */ new Map();
|
|
758
|
+
counter = 0;
|
|
759
|
+
peerId;
|
|
760
|
+
listeners = /* @__PURE__ */ new Set();
|
|
761
|
+
room;
|
|
762
|
+
channel;
|
|
763
|
+
unsubscribe;
|
|
764
|
+
constructor(opts) {
|
|
765
|
+
this.peerId = opts.peerId;
|
|
766
|
+
this.room = opts.room;
|
|
767
|
+
this.channel = opts.channel ?? "text";
|
|
768
|
+
if (this.room) {
|
|
769
|
+
this.unsubscribe = this.room.on(this.channel, (e) => {
|
|
770
|
+
if (e.from === this.peerId) return;
|
|
771
|
+
this.onRemote(e.event, e.payload);
|
|
772
|
+
});
|
|
773
|
+
this.room.broadcast(this.channel, "state-req", {});
|
|
774
|
+
}
|
|
775
|
+
}
|
|
776
|
+
/** Materialized visible text. */
|
|
777
|
+
toString() {
|
|
778
|
+
return this.visibleNodes().map((n) => n.ch).join("");
|
|
779
|
+
}
|
|
780
|
+
/** Current length of visible text. */
|
|
781
|
+
get length() {
|
|
782
|
+
return this.visibleNodes().length;
|
|
783
|
+
}
|
|
784
|
+
/** Insert a string at a visible index. Broadcasts ops when room-bound. */
|
|
785
|
+
insert(index, str) {
|
|
786
|
+
if (str.length === 0) return [];
|
|
787
|
+
const visible = this.visibleNodes();
|
|
788
|
+
let after = index <= 0 ? null : visible[index - 1]?.id ?? null;
|
|
789
|
+
const ops = [];
|
|
790
|
+
for (const ch of str) {
|
|
791
|
+
const id = this.nextId();
|
|
792
|
+
const node = { id, ch, after, deleted: false };
|
|
793
|
+
this.nodes.set(id, node);
|
|
794
|
+
ops.push({ op: "ins", id, ch, after });
|
|
795
|
+
after = id;
|
|
796
|
+
}
|
|
797
|
+
this.emit();
|
|
798
|
+
this.publish(ops);
|
|
799
|
+
return ops;
|
|
800
|
+
}
|
|
801
|
+
/** Delete `count` visible characters starting at `index`. */
|
|
802
|
+
delete(index, count = 1) {
|
|
803
|
+
if (count <= 0) return [];
|
|
804
|
+
const visible = this.visibleNodes();
|
|
805
|
+
const ops = [];
|
|
806
|
+
for (let k = 0; k < count; k++) {
|
|
807
|
+
const target = visible[index + k];
|
|
808
|
+
if (!target) break;
|
|
809
|
+
const node = this.nodes.get(target.id);
|
|
810
|
+
if (node && !node.deleted) {
|
|
811
|
+
node.deleted = true;
|
|
812
|
+
ops.push({ op: "del", id: node.id });
|
|
813
|
+
}
|
|
814
|
+
}
|
|
815
|
+
if (ops.length > 0) {
|
|
816
|
+
this.emit();
|
|
817
|
+
this.publish(ops);
|
|
818
|
+
}
|
|
819
|
+
return ops;
|
|
820
|
+
}
|
|
821
|
+
/** Apply a single remote op. Returns true if the document changed. */
|
|
822
|
+
applyOp(op) {
|
|
823
|
+
if (op.op === "ins") {
|
|
824
|
+
if (this.nodes.has(op.id)) return false;
|
|
825
|
+
this.bumpCounter(op.id);
|
|
826
|
+
this.nodes.set(op.id, {
|
|
827
|
+
id: op.id,
|
|
828
|
+
ch: op.ch,
|
|
829
|
+
after: op.after,
|
|
830
|
+
deleted: false
|
|
831
|
+
});
|
|
832
|
+
return true;
|
|
833
|
+
}
|
|
834
|
+
const node = this.nodes.get(op.id);
|
|
835
|
+
if (!node || node.deleted) return false;
|
|
836
|
+
node.deleted = true;
|
|
837
|
+
return true;
|
|
838
|
+
}
|
|
839
|
+
/** Apply a batch of remote ops, emitting once. */
|
|
840
|
+
applyOps(ops) {
|
|
841
|
+
let changed = false;
|
|
842
|
+
for (const op of ops) {
|
|
843
|
+
if (this.applyOp(op)) changed = true;
|
|
844
|
+
}
|
|
845
|
+
if (changed) this.emit();
|
|
846
|
+
}
|
|
847
|
+
/** Snapshot of all nodes (including tombstones) for state sync. */
|
|
848
|
+
getNodes() {
|
|
849
|
+
return [...this.nodes.values()];
|
|
850
|
+
}
|
|
851
|
+
/** Merge a remote snapshot of nodes. */
|
|
852
|
+
mergeNodes(nodes) {
|
|
853
|
+
let changed = false;
|
|
854
|
+
for (const incoming of nodes) {
|
|
855
|
+
this.bumpCounter(incoming.id);
|
|
856
|
+
const existing = this.nodes.get(incoming.id);
|
|
857
|
+
if (!existing) {
|
|
858
|
+
this.nodes.set(incoming.id, { ...incoming });
|
|
859
|
+
changed = true;
|
|
860
|
+
} else if (incoming.deleted && !existing.deleted) {
|
|
861
|
+
existing.deleted = true;
|
|
862
|
+
changed = true;
|
|
863
|
+
}
|
|
864
|
+
}
|
|
865
|
+
if (changed) this.emit();
|
|
866
|
+
}
|
|
867
|
+
/** Subscribe to text changes. Returns an unsubscribe fn. */
|
|
868
|
+
onChange(cb) {
|
|
869
|
+
this.listeners.add(cb);
|
|
870
|
+
return () => {
|
|
871
|
+
this.listeners.delete(cb);
|
|
872
|
+
};
|
|
873
|
+
}
|
|
874
|
+
/** Detach room bindings. */
|
|
875
|
+
dispose() {
|
|
876
|
+
this.unsubscribe?.();
|
|
877
|
+
this.listeners.clear();
|
|
878
|
+
}
|
|
879
|
+
// -------------------------------------------------------------------------
|
|
880
|
+
// Internal
|
|
881
|
+
// -------------------------------------------------------------------------
|
|
882
|
+
onRemote(event, payload) {
|
|
883
|
+
switch (event) {
|
|
884
|
+
case "op":
|
|
885
|
+
this.applyOps(payload);
|
|
886
|
+
break;
|
|
887
|
+
case "state-req":
|
|
888
|
+
this.room?.broadcast(this.channel, "state", this.getNodes());
|
|
889
|
+
break;
|
|
890
|
+
case "state":
|
|
891
|
+
this.mergeNodes(payload);
|
|
892
|
+
break;
|
|
893
|
+
}
|
|
894
|
+
}
|
|
895
|
+
publish(ops) {
|
|
896
|
+
this.room?.broadcast(this.channel, "op", ops);
|
|
897
|
+
}
|
|
898
|
+
visibleNodes() {
|
|
899
|
+
const children = /* @__PURE__ */ new Map();
|
|
900
|
+
for (const node of this.nodes.values()) {
|
|
901
|
+
const list = children.get(node.after);
|
|
902
|
+
if (list) list.push(node);
|
|
903
|
+
else children.set(node.after, [node]);
|
|
904
|
+
}
|
|
905
|
+
for (const list of children.values()) {
|
|
906
|
+
list.sort((a, b) => compareSiblings(a.id, b.id));
|
|
907
|
+
}
|
|
908
|
+
const result = [];
|
|
909
|
+
const stack = [...children.get(null) ?? []].reverse();
|
|
910
|
+
while (stack.length > 0) {
|
|
911
|
+
const node = stack.pop();
|
|
912
|
+
if (!node.deleted) result.push(node);
|
|
913
|
+
const kids = children.get(node.id);
|
|
914
|
+
if (kids) {
|
|
915
|
+
for (let i = kids.length - 1; i >= 0; i--) stack.push(kids[i]);
|
|
916
|
+
}
|
|
917
|
+
}
|
|
918
|
+
return result;
|
|
919
|
+
}
|
|
920
|
+
nextId() {
|
|
921
|
+
this.counter += 1;
|
|
922
|
+
return `${this.counter}@${this.peerId}`;
|
|
923
|
+
}
|
|
924
|
+
bumpCounter(id) {
|
|
925
|
+
const { counter } = parseId(id);
|
|
926
|
+
if (Number.isFinite(counter) && counter > this.counter) {
|
|
927
|
+
this.counter = counter;
|
|
928
|
+
}
|
|
929
|
+
}
|
|
930
|
+
emit() {
|
|
931
|
+
const text = this.toString();
|
|
932
|
+
for (const cb of this.listeners) {
|
|
933
|
+
try {
|
|
934
|
+
cb(text);
|
|
935
|
+
} catch {
|
|
936
|
+
}
|
|
937
|
+
}
|
|
938
|
+
}
|
|
939
|
+
};
|
|
940
|
+
|
|
941
|
+
// src/realtime/types.ts
|
|
942
|
+
var REALTIME_PROTOCOL = 1;
|
|
943
|
+
|
|
944
|
+
export {
|
|
945
|
+
RealtimeRoom,
|
|
946
|
+
MemoryHub,
|
|
947
|
+
MemoryRealtimeTransport,
|
|
948
|
+
BroadcastChannelTransport,
|
|
949
|
+
WebSocketRelayTransport,
|
|
950
|
+
DurableObjectRelayTransport,
|
|
951
|
+
createPresenceTransport,
|
|
952
|
+
joinPresence,
|
|
953
|
+
DEFAULT_MAX_RECORDS,
|
|
954
|
+
PersistentChannel,
|
|
955
|
+
localStorageChannelStore,
|
|
956
|
+
RealtimeText,
|
|
957
|
+
REALTIME_PROTOCOL
|
|
958
|
+
};
|