@johnhenry/browsermesh-sync 0.0.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/LICENSE +21 -0
- package/README.md +36 -0
- package/package.json +31 -0
- package/src/collab-bridge.mjs +661 -0
- package/src/collab.mjs +363 -0
- package/src/delta-sync.mjs +1059 -0
- package/src/files.mjs +640 -0
- package/src/index.mjs +8 -0
- package/src/memory-sync.mjs +712 -0
- package/src/migration.mjs +986 -0
- package/src/silent-catch.mjs +55 -0
- package/src/sync.mjs +419 -0
|
@@ -0,0 +1,661 @@
|
|
|
1
|
+
/**
|
|
2
|
+
// STATUS: EXPERIMENTAL — wires peer-collab CRDT into mesh transport layer
|
|
3
|
+
* clawser-peer-collab-bridge.js — Connects the CollabSession (Yjs CRDT)
|
|
4
|
+
* and MeshSyncEngine to the PeerSession transport and SyncCoordinator.
|
|
5
|
+
*
|
|
6
|
+
* CollabBridge: per-peer adapter that bridges CollabSession's simple
|
|
7
|
+
* {send, onMessage} interface to PeerSession's handler-based routing.
|
|
8
|
+
*
|
|
9
|
+
* CollabManager: lifecycle manager that creates/destroys CollabBridges
|
|
10
|
+
* in response to peer:connect / peer:disconnect events and orchestrates
|
|
11
|
+
* document replication via MeshSyncEngine + SyncCoordinator.
|
|
12
|
+
*
|
|
13
|
+
* Usage (inside initMeshSubsystem):
|
|
14
|
+
* const collabManager = new CollabManager({
|
|
15
|
+
* syncEngine: state.syncEngine,
|
|
16
|
+
* syncCoordinator: state.syncCoordinator,
|
|
17
|
+
* sessionManager: state.sessionManager,
|
|
18
|
+
* localPodId: state.pod.podId,
|
|
19
|
+
* });
|
|
20
|
+
* collabManager.attach(peerNode);
|
|
21
|
+
*
|
|
22
|
+
* Run tests:
|
|
23
|
+
* node --import ./web/test/_setup-globals.mjs --test web/test/clawser-peer-collab-bridge.test.mjs
|
|
24
|
+
*/
|
|
25
|
+
|
|
26
|
+
import {
|
|
27
|
+
COLLAB_UPDATE,
|
|
28
|
+
COLLAB_AWARENESS,
|
|
29
|
+
COLLAB_SYNC,
|
|
30
|
+
CollabSession,
|
|
31
|
+
} from './collab.mjs'
|
|
32
|
+
|
|
33
|
+
// ---------------------------------------------------------------------------
|
|
34
|
+
// Wire constants — service type strings used with PeerSession.registerHandler
|
|
35
|
+
// ---------------------------------------------------------------------------
|
|
36
|
+
|
|
37
|
+
/** Service type for collab updates routed through PeerSession. */
|
|
38
|
+
export const COLLAB_SERVICE_TYPE = 'collab'
|
|
39
|
+
|
|
40
|
+
/** Service type for CRDT sync-engine payloads routed through PeerSession. */
|
|
41
|
+
export const CRDT_SYNC_SERVICE_TYPE = 'crdt-sync'
|
|
42
|
+
|
|
43
|
+
// ---------------------------------------------------------------------------
|
|
44
|
+
// CollabBridge
|
|
45
|
+
// ---------------------------------------------------------------------------
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* Adapts a PeerSession to the simple {send, onMessage} interface that
|
|
49
|
+
* CollabSession expects. Also bridges MeshSyncEngine document sync
|
|
50
|
+
* payloads over the same PeerSession.
|
|
51
|
+
*
|
|
52
|
+
* One CollabBridge per connected peer.
|
|
53
|
+
*/
|
|
54
|
+
export class CollabBridge {
|
|
55
|
+
/** @type {import('./clawser-peer-session.js').PeerSession} */
|
|
56
|
+
#peerSession
|
|
57
|
+
|
|
58
|
+
/** @type {Map<string, CollabSession>} docId -> CollabSession */
|
|
59
|
+
#collabSessions = new Map()
|
|
60
|
+
|
|
61
|
+
/** @type {import('./clawser-mesh-sync.js').MeshSyncEngine|null} */
|
|
62
|
+
#syncEngine
|
|
63
|
+
|
|
64
|
+
/** @type {import('./clawser-mesh-delta-sync.js').SyncCoordinator|null} */
|
|
65
|
+
#syncCoordinator
|
|
66
|
+
|
|
67
|
+
/** @type {string} */
|
|
68
|
+
#remotePodId
|
|
69
|
+
|
|
70
|
+
/** @type {Function[]} */
|
|
71
|
+
#messageCallbacks = []
|
|
72
|
+
|
|
73
|
+
/** @type {boolean} */
|
|
74
|
+
#destroyed = false
|
|
75
|
+
|
|
76
|
+
/** @type {Function} */
|
|
77
|
+
#onLog
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* @param {object} opts
|
|
81
|
+
* @param {import('./clawser-peer-session.js').PeerSession} opts.peerSession
|
|
82
|
+
* @param {import('./clawser-mesh-sync.js').MeshSyncEngine} [opts.syncEngine]
|
|
83
|
+
* @param {import('./clawser-mesh-delta-sync.js').SyncCoordinator} [opts.syncCoordinator]
|
|
84
|
+
* @param {Function} [opts.onLog]
|
|
85
|
+
*/
|
|
86
|
+
constructor({ peerSession, syncEngine, syncCoordinator, onLog }) {
|
|
87
|
+
if (!peerSession) throw new Error('peerSession is required')
|
|
88
|
+
this.#peerSession = peerSession
|
|
89
|
+
this.#remotePodId = peerSession.remotePodId
|
|
90
|
+
this.#syncEngine = syncEngine || null
|
|
91
|
+
this.#syncCoordinator = syncCoordinator || null
|
|
92
|
+
this.#onLog = onLog || (() => {})
|
|
93
|
+
|
|
94
|
+
// Register handler for incoming collab messages on the PeerSession
|
|
95
|
+
this.#peerSession.registerHandler(COLLAB_SERVICE_TYPE, (envelope) => {
|
|
96
|
+
const msg = envelope.payload || envelope
|
|
97
|
+
if (!msg) return
|
|
98
|
+
// Dispatch to all registered message callbacks (CollabSession listeners)
|
|
99
|
+
for (const cb of this.#messageCallbacks) {
|
|
100
|
+
try { cb(msg) } catch { /* listener errors don't propagate */ }
|
|
101
|
+
}
|
|
102
|
+
})
|
|
103
|
+
|
|
104
|
+
// Register handler for CRDT sync-engine payloads
|
|
105
|
+
this.#peerSession.registerHandler(CRDT_SYNC_SERVICE_TYPE, (envelope) => {
|
|
106
|
+
const payload = envelope.payload || envelope
|
|
107
|
+
if (!payload) return
|
|
108
|
+
this.#handleCrdtSync(payload)
|
|
109
|
+
})
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
/** Remote peer's pod ID. */
|
|
113
|
+
get remotePodId() { return this.#remotePodId }
|
|
114
|
+
|
|
115
|
+
/** Whether this bridge has been destroyed. */
|
|
116
|
+
get destroyed() { return this.#destroyed }
|
|
117
|
+
|
|
118
|
+
/** Number of active collab sessions on this bridge. */
|
|
119
|
+
get sessionCount() { return this.#collabSessions.size }
|
|
120
|
+
|
|
121
|
+
// -- PeerSession-to-CollabSession adapter ---------------------------------
|
|
122
|
+
|
|
123
|
+
/**
|
|
124
|
+
* Create the {send, onMessage} adapter object that CollabSession expects.
|
|
125
|
+
* @returns {{ send: Function, onMessage: Function }}
|
|
126
|
+
*/
|
|
127
|
+
#createSessionAdapter() {
|
|
128
|
+
return {
|
|
129
|
+
send: (msg) => {
|
|
130
|
+
if (this.#destroyed) return
|
|
131
|
+
try {
|
|
132
|
+
this.#peerSession.send(COLLAB_SERVICE_TYPE, msg)
|
|
133
|
+
} catch (err) {
|
|
134
|
+
this.#onLog(0, `CollabBridge send error: ${err.message}`)
|
|
135
|
+
}
|
|
136
|
+
},
|
|
137
|
+
onMessage: (cb) => {
|
|
138
|
+
this.#messageCallbacks.push(cb)
|
|
139
|
+
},
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
// -- Collab session management --------------------------------------------
|
|
144
|
+
|
|
145
|
+
/**
|
|
146
|
+
* Open a collaborative editing session for a document.
|
|
147
|
+
* Creates a CollabSession backed by the PeerSession transport.
|
|
148
|
+
*
|
|
149
|
+
* @param {string} docId - Document identifier
|
|
150
|
+
* @param {object} [opts]
|
|
151
|
+
* @param {object} [opts.Y] - Yjs module (for real Y.js integration)
|
|
152
|
+
* @returns {CollabSession}
|
|
153
|
+
*/
|
|
154
|
+
openDocument(docId, opts = {}) {
|
|
155
|
+
if (this.#destroyed) throw new Error('Bridge is destroyed')
|
|
156
|
+
if (this.#collabSessions.has(docId)) {
|
|
157
|
+
return this.#collabSessions.get(docId)
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
const adapter = this.#createSessionAdapter()
|
|
161
|
+
const collab = new CollabSession({
|
|
162
|
+
session: adapter,
|
|
163
|
+
docId,
|
|
164
|
+
Y: opts.Y,
|
|
165
|
+
})
|
|
166
|
+
collab.start()
|
|
167
|
+
|
|
168
|
+
this.#collabSessions.set(docId, collab)
|
|
169
|
+
this.#onLog(2, `Opened collab session for doc "${docId}" with peer ${this.#remotePodId}`)
|
|
170
|
+
|
|
171
|
+
// Sync initial state
|
|
172
|
+
collab.syncWithPeer()
|
|
173
|
+
|
|
174
|
+
return collab
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
/**
|
|
178
|
+
* Close a collaborative editing session for a document.
|
|
179
|
+
* @param {string} docId
|
|
180
|
+
*/
|
|
181
|
+
closeDocument(docId) {
|
|
182
|
+
const collab = this.#collabSessions.get(docId)
|
|
183
|
+
if (!collab) return
|
|
184
|
+
collab.close()
|
|
185
|
+
this.#collabSessions.delete(docId)
|
|
186
|
+
this.#onLog(2, `Closed collab session for doc "${docId}" with peer ${this.#remotePodId}`)
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
/**
|
|
190
|
+
* Get an open collab session by docId.
|
|
191
|
+
* @param {string} docId
|
|
192
|
+
* @returns {CollabSession|null}
|
|
193
|
+
*/
|
|
194
|
+
getDocument(docId) {
|
|
195
|
+
return this.#collabSessions.get(docId) || null
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
/**
|
|
199
|
+
* List all open document IDs on this bridge.
|
|
200
|
+
* @returns {string[]}
|
|
201
|
+
*/
|
|
202
|
+
listDocuments() {
|
|
203
|
+
return [...this.#collabSessions.keys()]
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
// -- CRDT Sync Engine bridge ----------------------------------------------
|
|
207
|
+
|
|
208
|
+
/**
|
|
209
|
+
* Push a MeshSyncEngine document's state to the remote peer.
|
|
210
|
+
* @param {string} docId - Document ID in the MeshSyncEngine
|
|
211
|
+
*/
|
|
212
|
+
pushSyncDocument(docId) {
|
|
213
|
+
if (this.#destroyed || !this.#syncEngine) return
|
|
214
|
+
|
|
215
|
+
const payload = this.#syncEngine.prepareSyncPayload(docId)
|
|
216
|
+
if (!payload) {
|
|
217
|
+
this.#onLog(1, `No sync payload for doc "${docId}"`)
|
|
218
|
+
return
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
try {
|
|
222
|
+
this.#peerSession.send(CRDT_SYNC_SERVICE_TYPE, {
|
|
223
|
+
action: 'sync',
|
|
224
|
+
docId,
|
|
225
|
+
...payload,
|
|
226
|
+
})
|
|
227
|
+
} catch (err) {
|
|
228
|
+
this.#onLog(0, `CRDT sync push error: ${err.message}`)
|
|
229
|
+
}
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
/**
|
|
233
|
+
* Request a delta sync from the SyncCoordinator for this peer.
|
|
234
|
+
*/
|
|
235
|
+
requestDeltaSync() {
|
|
236
|
+
if (this.#destroyed || !this.#syncCoordinator) return
|
|
237
|
+
|
|
238
|
+
try {
|
|
239
|
+
this.#syncCoordinator.requestSync(this.#remotePodId)
|
|
240
|
+
} catch (err) {
|
|
241
|
+
this.#onLog(0, `Delta sync request error: ${err.message}`)
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
/**
|
|
246
|
+
* Push all MeshSyncEngine documents to the remote peer.
|
|
247
|
+
*/
|
|
248
|
+
pushAllSyncDocuments() {
|
|
249
|
+
if (this.#destroyed || !this.#syncEngine) return
|
|
250
|
+
|
|
251
|
+
for (const doc of this.#syncEngine.listDocuments()) {
|
|
252
|
+
this.pushSyncDocument(doc.id)
|
|
253
|
+
}
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
/**
|
|
257
|
+
* Handle an incoming CRDT sync payload from the remote peer.
|
|
258
|
+
* @param {object} payload
|
|
259
|
+
*/
|
|
260
|
+
#handleCrdtSync(payload) {
|
|
261
|
+
if (!payload || !payload.docId) return
|
|
262
|
+
|
|
263
|
+
if (payload.action === 'sync' && this.#syncEngine) {
|
|
264
|
+
// Merge remote CRDT state into local MeshSyncEngine document
|
|
265
|
+
const localDoc = this.#syncEngine.get(payload.docId)
|
|
266
|
+
if (localDoc) {
|
|
267
|
+
try {
|
|
268
|
+
this.#syncEngine.merge(payload.docId, {
|
|
269
|
+
crdt: payload.crdt,
|
|
270
|
+
version: payload.version,
|
|
271
|
+
})
|
|
272
|
+
this.#onLog(2, `Merged CRDT doc "${payload.docId}" from peer ${this.#remotePodId}`)
|
|
273
|
+
} catch (err) {
|
|
274
|
+
this.#onLog(0, `CRDT merge error for "${payload.docId}": ${err.message}`)
|
|
275
|
+
}
|
|
276
|
+
} else {
|
|
277
|
+
this.#onLog(1, `Received sync for unknown doc "${payload.docId}" — ignoring`)
|
|
278
|
+
}
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
if (payload.action === 'delta' && this.#syncCoordinator) {
|
|
282
|
+
// Handle delta sync messages through the SyncCoordinator wire protocol
|
|
283
|
+
try {
|
|
284
|
+
this.#syncCoordinator.handleMessage(this.#remotePodId, payload.message)
|
|
285
|
+
this.#onLog(2, `Applied delta sync from peer ${this.#remotePodId}`)
|
|
286
|
+
} catch (err) {
|
|
287
|
+
this.#onLog(0, `Delta sync error from ${this.#remotePodId}: ${err.message}`)
|
|
288
|
+
}
|
|
289
|
+
}
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
// -- Lifecycle ------------------------------------------------------------
|
|
293
|
+
|
|
294
|
+
/**
|
|
295
|
+
* Destroy the bridge — close all collab sessions, remove handlers.
|
|
296
|
+
*/
|
|
297
|
+
destroy() {
|
|
298
|
+
if (this.#destroyed) return
|
|
299
|
+
this.#destroyed = true
|
|
300
|
+
|
|
301
|
+
// Close all collab sessions
|
|
302
|
+
for (const [docId, collab] of this.#collabSessions) {
|
|
303
|
+
try { collab.close() } catch { /* best effort */ }
|
|
304
|
+
}
|
|
305
|
+
this.#collabSessions.clear()
|
|
306
|
+
|
|
307
|
+
// Remove PeerSession handlers
|
|
308
|
+
try { this.#peerSession.removeHandler(COLLAB_SERVICE_TYPE) } catch { /* */ }
|
|
309
|
+
try { this.#peerSession.removeHandler(CRDT_SYNC_SERVICE_TYPE) } catch { /* */ }
|
|
310
|
+
|
|
311
|
+
this.#messageCallbacks = []
|
|
312
|
+
this.#onLog(2, `CollabBridge destroyed for peer ${this.#remotePodId}`)
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
/**
|
|
316
|
+
* Serialize bridge state.
|
|
317
|
+
* @returns {object}
|
|
318
|
+
*/
|
|
319
|
+
toJSON() {
|
|
320
|
+
return {
|
|
321
|
+
remotePodId: this.#remotePodId,
|
|
322
|
+
destroyed: this.#destroyed,
|
|
323
|
+
documents: [...this.#collabSessions.keys()],
|
|
324
|
+
}
|
|
325
|
+
}
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
// ---------------------------------------------------------------------------
|
|
329
|
+
// CollabManager
|
|
330
|
+
// ---------------------------------------------------------------------------
|
|
331
|
+
|
|
332
|
+
/**
|
|
333
|
+
* Lifecycle manager for CollabBridges. Hooks into PeerNode events to
|
|
334
|
+
* automatically create/destroy bridges when peers connect/disconnect.
|
|
335
|
+
*
|
|
336
|
+
* Also provides an API for opening/closing collaborative documents that
|
|
337
|
+
* automatically replicates across all connected peers.
|
|
338
|
+
*/
|
|
339
|
+
export class CollabManager {
|
|
340
|
+
/** @type {Map<string, CollabBridge>} remotePodId -> CollabBridge */
|
|
341
|
+
#bridges = new Map()
|
|
342
|
+
|
|
343
|
+
/** @type {import('./clawser-mesh-sync.js').MeshSyncEngine|null} */
|
|
344
|
+
#syncEngine
|
|
345
|
+
|
|
346
|
+
/** @type {import('./clawser-mesh-delta-sync.js').SyncCoordinator|null} */
|
|
347
|
+
#syncCoordinator
|
|
348
|
+
|
|
349
|
+
/** @type {import('./clawser-peer-session.js').SessionManager|null} */
|
|
350
|
+
#sessionManager
|
|
351
|
+
|
|
352
|
+
/** @type {string} */
|
|
353
|
+
#localPodId
|
|
354
|
+
|
|
355
|
+
/** @type {Set<string>} docIds that should be auto-opened on new peers */
|
|
356
|
+
#sharedDocuments = new Set()
|
|
357
|
+
|
|
358
|
+
/** @type {Map<string, Function>} docId -> unsubscribe fn from MeshSyncEngine */
|
|
359
|
+
#syncSubscriptions = new Map()
|
|
360
|
+
|
|
361
|
+
/** @type {Function} */
|
|
362
|
+
#onLog
|
|
363
|
+
|
|
364
|
+
/** @type {object|null} reference to the attached PeerNode */
|
|
365
|
+
#peerNode = null
|
|
366
|
+
|
|
367
|
+
/** @type {Function|null} bound connect handler for cleanup */
|
|
368
|
+
#boundConnect = null
|
|
369
|
+
|
|
370
|
+
/** @type {Function|null} bound disconnect handler for cleanup */
|
|
371
|
+
#boundDisconnect = null
|
|
372
|
+
|
|
373
|
+
/**
|
|
374
|
+
* @param {object} opts
|
|
375
|
+
* @param {string} opts.localPodId
|
|
376
|
+
* @param {import('./clawser-mesh-sync.js').MeshSyncEngine} [opts.syncEngine]
|
|
377
|
+
* @param {import('./clawser-mesh-delta-sync.js').SyncCoordinator} [opts.syncCoordinator]
|
|
378
|
+
* @param {import('./clawser-peer-session.js').SessionManager} [opts.sessionManager]
|
|
379
|
+
* @param {Function} [opts.onLog]
|
|
380
|
+
*/
|
|
381
|
+
constructor({ localPodId, syncEngine, syncCoordinator, sessionManager, onLog }) {
|
|
382
|
+
if (!localPodId || typeof localPodId !== 'string') {
|
|
383
|
+
throw new Error('localPodId is required')
|
|
384
|
+
}
|
|
385
|
+
this.#localPodId = localPodId
|
|
386
|
+
this.#syncEngine = syncEngine || null
|
|
387
|
+
this.#syncCoordinator = syncCoordinator || null
|
|
388
|
+
this.#sessionManager = sessionManager || null
|
|
389
|
+
this.#onLog = onLog || (() => {})
|
|
390
|
+
}
|
|
391
|
+
|
|
392
|
+
/** Number of active bridges. */
|
|
393
|
+
get bridgeCount() { return this.#bridges.size }
|
|
394
|
+
|
|
395
|
+
/** Local pod ID. */
|
|
396
|
+
get localPodId() { return this.#localPodId }
|
|
397
|
+
|
|
398
|
+
// -- PeerNode attachment ---------------------------------------------------
|
|
399
|
+
|
|
400
|
+
/**
|
|
401
|
+
* Attach to a PeerNode to auto-manage bridges on peer lifecycle events.
|
|
402
|
+
*
|
|
403
|
+
* @param {import('./clawser-peer-node.js').PeerNode} peerNode
|
|
404
|
+
*/
|
|
405
|
+
attach(peerNode) {
|
|
406
|
+
if (this.#peerNode) this.detach()
|
|
407
|
+
|
|
408
|
+
this.#peerNode = peerNode
|
|
409
|
+
|
|
410
|
+
this.#boundConnect = (peer) => {
|
|
411
|
+
const peerId = peer?.fingerprint || peer?.podId || peer?.pubKey
|
|
412
|
+
if (!peerId) return
|
|
413
|
+
this.#onPeerConnect(peerId)
|
|
414
|
+
}
|
|
415
|
+
|
|
416
|
+
this.#boundDisconnect = (peer) => {
|
|
417
|
+
const peerId = peer?.fingerprint || peer?.podId || peer?.pubKey
|
|
418
|
+
if (!peerId) return
|
|
419
|
+
this.#onPeerDisconnect(peerId)
|
|
420
|
+
}
|
|
421
|
+
|
|
422
|
+
peerNode.on('peer:connect', this.#boundConnect)
|
|
423
|
+
peerNode.on('peer:disconnect', this.#boundDisconnect)
|
|
424
|
+
|
|
425
|
+
this.#onLog(2, `CollabManager attached to PeerNode`)
|
|
426
|
+
}
|
|
427
|
+
|
|
428
|
+
/**
|
|
429
|
+
* Detach from the PeerNode. Stops listening for lifecycle events.
|
|
430
|
+
*/
|
|
431
|
+
detach() {
|
|
432
|
+
if (this.#peerNode && this.#boundConnect) {
|
|
433
|
+
this.#peerNode.off('peer:connect', this.#boundConnect)
|
|
434
|
+
}
|
|
435
|
+
if (this.#peerNode && this.#boundDisconnect) {
|
|
436
|
+
this.#peerNode.off('peer:disconnect', this.#boundDisconnect)
|
|
437
|
+
}
|
|
438
|
+
this.#peerNode = null
|
|
439
|
+
this.#boundConnect = null
|
|
440
|
+
this.#boundDisconnect = null
|
|
441
|
+
}
|
|
442
|
+
|
|
443
|
+
// -- Peer lifecycle handlers -----------------------------------------------
|
|
444
|
+
|
|
445
|
+
/**
|
|
446
|
+
* Handle a new peer connection. Looks up the PeerSession from the
|
|
447
|
+
* SessionManager, creates a CollabBridge, and opens any shared documents.
|
|
448
|
+
*
|
|
449
|
+
* @param {string} peerId
|
|
450
|
+
*/
|
|
451
|
+
#onPeerConnect(peerId) {
|
|
452
|
+
if (this.#bridges.has(peerId)) return
|
|
453
|
+
|
|
454
|
+
// Find a PeerSession for this peer
|
|
455
|
+
const peerSession = this.#findPeerSession(peerId)
|
|
456
|
+
if (!peerSession) {
|
|
457
|
+
this.#onLog(1, `No PeerSession found for peer ${peerId} — skipping collab bridge`)
|
|
458
|
+
return
|
|
459
|
+
}
|
|
460
|
+
|
|
461
|
+
const bridge = new CollabBridge({
|
|
462
|
+
peerSession,
|
|
463
|
+
syncEngine: this.#syncEngine,
|
|
464
|
+
syncCoordinator: this.#syncCoordinator,
|
|
465
|
+
onLog: this.#onLog,
|
|
466
|
+
})
|
|
467
|
+
|
|
468
|
+
this.#bridges.set(peerId, bridge)
|
|
469
|
+
this.#onLog(2, `CollabBridge created for peer ${peerId}`)
|
|
470
|
+
|
|
471
|
+
// Auto-open any shared documents
|
|
472
|
+
for (const docId of this.#sharedDocuments) {
|
|
473
|
+
bridge.openDocument(docId)
|
|
474
|
+
}
|
|
475
|
+
|
|
476
|
+
// Push all sync engine documents to new peer
|
|
477
|
+
bridge.pushAllSyncDocuments()
|
|
478
|
+
}
|
|
479
|
+
|
|
480
|
+
/**
|
|
481
|
+
* Handle a peer disconnection. Destroys the CollabBridge.
|
|
482
|
+
*
|
|
483
|
+
* @param {string} peerId
|
|
484
|
+
*/
|
|
485
|
+
#onPeerDisconnect(peerId) {
|
|
486
|
+
const bridge = this.#bridges.get(peerId)
|
|
487
|
+
if (!bridge) return
|
|
488
|
+
|
|
489
|
+
bridge.destroy()
|
|
490
|
+
this.#bridges.delete(peerId)
|
|
491
|
+
this.#onLog(2, `CollabBridge destroyed for peer ${peerId}`)
|
|
492
|
+
}
|
|
493
|
+
|
|
494
|
+
/**
|
|
495
|
+
* Find a PeerSession for a given peer ID. Checks the SessionManager first,
|
|
496
|
+
* falls back to direct PeerNode session lookup.
|
|
497
|
+
*
|
|
498
|
+
* @param {string} peerId
|
|
499
|
+
* @returns {import('./clawser-peer-session.js').PeerSession|null}
|
|
500
|
+
*/
|
|
501
|
+
#findPeerSession(peerId) {
|
|
502
|
+
// Try SessionManager first (provides full PeerSession with handler routing)
|
|
503
|
+
if (this.#sessionManager) {
|
|
504
|
+
const sessions = this.#sessionManager.getSessionsForPeer(peerId)
|
|
505
|
+
if (sessions.length > 0) {
|
|
506
|
+
return sessions[0]
|
|
507
|
+
}
|
|
508
|
+
}
|
|
509
|
+
return null
|
|
510
|
+
}
|
|
511
|
+
|
|
512
|
+
// -- Document management ---------------------------------------------------
|
|
513
|
+
|
|
514
|
+
/**
|
|
515
|
+
* Share a document across all connected peers. Opens a CollabSession
|
|
516
|
+
* on every existing bridge and marks the document for auto-open on
|
|
517
|
+
* future peer connections.
|
|
518
|
+
*
|
|
519
|
+
* @param {string} docId
|
|
520
|
+
* @param {object} [opts]
|
|
521
|
+
* @param {object} [opts.Y] - Yjs module
|
|
522
|
+
* @returns {Map<string, CollabSession>} peerId -> CollabSession
|
|
523
|
+
*/
|
|
524
|
+
shareDocument(docId, opts = {}) {
|
|
525
|
+
this.#sharedDocuments.add(docId)
|
|
526
|
+
const sessions = new Map()
|
|
527
|
+
|
|
528
|
+
for (const [peerId, bridge] of this.#bridges) {
|
|
529
|
+
const collab = bridge.openDocument(docId, opts)
|
|
530
|
+
sessions.set(peerId, collab)
|
|
531
|
+
}
|
|
532
|
+
|
|
533
|
+
// If the document exists in the sync engine, subscribe to updates
|
|
534
|
+
// and push changes to all peers automatically
|
|
535
|
+
if (this.#syncEngine && this.#syncEngine.get(docId)) {
|
|
536
|
+
this.#subscribeSyncDocument(docId)
|
|
537
|
+
}
|
|
538
|
+
|
|
539
|
+
this.#onLog(2, `Document "${docId}" shared across ${sessions.size} peer(s)`)
|
|
540
|
+
return sessions
|
|
541
|
+
}
|
|
542
|
+
|
|
543
|
+
/**
|
|
544
|
+
* Unshare a document. Closes all CollabSessions for it.
|
|
545
|
+
*
|
|
546
|
+
* @param {string} docId
|
|
547
|
+
*/
|
|
548
|
+
unshareDocument(docId) {
|
|
549
|
+
this.#sharedDocuments.delete(docId)
|
|
550
|
+
|
|
551
|
+
for (const [, bridge] of this.#bridges) {
|
|
552
|
+
bridge.closeDocument(docId)
|
|
553
|
+
}
|
|
554
|
+
|
|
555
|
+
// Unsubscribe from sync engine updates
|
|
556
|
+
const unsub = this.#syncSubscriptions.get(docId)
|
|
557
|
+
if (unsub) {
|
|
558
|
+
unsub()
|
|
559
|
+
this.#syncSubscriptions.delete(docId)
|
|
560
|
+
}
|
|
561
|
+
|
|
562
|
+
this.#onLog(2, `Document "${docId}" unshared`)
|
|
563
|
+
}
|
|
564
|
+
|
|
565
|
+
/**
|
|
566
|
+
* Subscribe to MeshSyncEngine document changes and push them to all peers.
|
|
567
|
+
* @param {string} docId
|
|
568
|
+
*/
|
|
569
|
+
#subscribeSyncDocument(docId) {
|
|
570
|
+
if (this.#syncSubscriptions.has(docId)) return
|
|
571
|
+
if (!this.#syncEngine) return
|
|
572
|
+
|
|
573
|
+
const unsub = this.#syncEngine.subscribe(docId, () => {
|
|
574
|
+
// On any local change, push updated state to all peers
|
|
575
|
+
for (const [, bridge] of this.#bridges) {
|
|
576
|
+
bridge.pushSyncDocument(docId)
|
|
577
|
+
}
|
|
578
|
+
})
|
|
579
|
+
|
|
580
|
+
this.#syncSubscriptions.set(docId, unsub)
|
|
581
|
+
}
|
|
582
|
+
|
|
583
|
+
/**
|
|
584
|
+
* Get the CollabBridge for a specific peer.
|
|
585
|
+
* @param {string} peerId
|
|
586
|
+
* @returns {CollabBridge|null}
|
|
587
|
+
*/
|
|
588
|
+
getBridge(peerId) {
|
|
589
|
+
return this.#bridges.get(peerId) || null
|
|
590
|
+
}
|
|
591
|
+
|
|
592
|
+
/**
|
|
593
|
+
* List all peer IDs with active bridges.
|
|
594
|
+
* @returns {string[]}
|
|
595
|
+
*/
|
|
596
|
+
listPeers() {
|
|
597
|
+
return [...this.#bridges.keys()]
|
|
598
|
+
}
|
|
599
|
+
|
|
600
|
+
/**
|
|
601
|
+
* List all shared document IDs.
|
|
602
|
+
* @returns {string[]}
|
|
603
|
+
*/
|
|
604
|
+
listSharedDocuments() {
|
|
605
|
+
return [...this.#sharedDocuments]
|
|
606
|
+
}
|
|
607
|
+
|
|
608
|
+
/**
|
|
609
|
+
* Push a specific sync-engine document to all peers.
|
|
610
|
+
* @param {string} docId
|
|
611
|
+
*/
|
|
612
|
+
broadcastSyncDocument(docId) {
|
|
613
|
+
for (const [, bridge] of this.#bridges) {
|
|
614
|
+
bridge.pushSyncDocument(docId)
|
|
615
|
+
}
|
|
616
|
+
}
|
|
617
|
+
|
|
618
|
+
/**
|
|
619
|
+
* Request a delta sync from the SyncCoordinator for all peers.
|
|
620
|
+
*/
|
|
621
|
+
requestDeltaSyncAll() {
|
|
622
|
+
for (const [, bridge] of this.#bridges) {
|
|
623
|
+
bridge.requestDeltaSync()
|
|
624
|
+
}
|
|
625
|
+
}
|
|
626
|
+
|
|
627
|
+
// -- Lifecycle ------------------------------------------------------------
|
|
628
|
+
|
|
629
|
+
/**
|
|
630
|
+
* Destroy the manager — detach from PeerNode, destroy all bridges,
|
|
631
|
+
* unsubscribe from sync engine.
|
|
632
|
+
*/
|
|
633
|
+
destroy() {
|
|
634
|
+
this.detach()
|
|
635
|
+
|
|
636
|
+
for (const [, bridge] of this.#bridges) {
|
|
637
|
+
bridge.destroy()
|
|
638
|
+
}
|
|
639
|
+
this.#bridges.clear()
|
|
640
|
+
|
|
641
|
+
for (const [, unsub] of this.#syncSubscriptions) {
|
|
642
|
+
try { unsub() } catch { /* best effort */ }
|
|
643
|
+
}
|
|
644
|
+
this.#syncSubscriptions.clear()
|
|
645
|
+
this.#sharedDocuments.clear()
|
|
646
|
+
|
|
647
|
+
this.#onLog(2, `CollabManager destroyed`)
|
|
648
|
+
}
|
|
649
|
+
|
|
650
|
+
/**
|
|
651
|
+
* Serialize manager state.
|
|
652
|
+
* @returns {object}
|
|
653
|
+
*/
|
|
654
|
+
toJSON() {
|
|
655
|
+
return {
|
|
656
|
+
localPodId: this.#localPodId,
|
|
657
|
+
bridges: [...this.#bridges.entries()].map(([k, v]) => ({ peerId: k, ...v.toJSON() })),
|
|
658
|
+
sharedDocuments: [...this.#sharedDocuments],
|
|
659
|
+
}
|
|
660
|
+
}
|
|
661
|
+
}
|