@johnhenry/browsermesh-sync 0.3.0 → 0.4.1

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/README.md CHANGED
@@ -15,7 +15,7 @@ CRDT sync engine, delta sync, file transfer, and real-time collaboration for Bro
15
15
 
16
16
  ## Provenance
17
17
 
18
- Extracted from the private `clawser` monorepo (previously `packages/browsermesh-sync`), where it was manually published to npm, unscoped, as `browsermesh-sync@0.1.0` (2026-07-17) with no CI ever automating that publish. This is its first release as part of the `@johnhenry/browsermesh` monorepo; the version restarts at `0.0.0` per family convention.
18
+ Extracted from a private monorepo (previously `packages/browsermesh-sync`), where it was manually published to npm, unscoped, as `browsermesh-sync@0.1.0` (2026-07-17) with no CI ever automating that publish. This is its first release as part of the `@johnhenry/browsermesh` monorepo; the version restarts at `0.0.0` per family convention.
19
19
 
20
20
 
21
21
  ## Modules
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@johnhenry/browsermesh-sync",
3
- "version": "0.3.0",
3
+ "version": "0.4.1",
4
4
  "description": "CRDT sync engine, delta sync, file transfer, and collaboration for BrowserMesh",
5
5
  "type": "module",
6
6
  "main": "./src/index.mjs",
@@ -1,6 +1,6 @@
1
1
  /**
2
2
  // STATUS: EXPERIMENTAL — wires peer-collab CRDT into mesh transport layer
3
- * clawser-peer-collab-bridge.js — Connects the CollabSession (Yjs CRDT)
3
+ * collab-bridge.mjs — Connects the CollabSession (Yjs CRDT)
4
4
  * and MeshSyncEngine to the PeerSession transport and SyncCoordinator.
5
5
  *
6
6
  * CollabBridge: per-peer adapter that bridges CollabSession's simple
@@ -20,7 +20,7 @@
20
20
  * collabManager.attach(peerNode);
21
21
  *
22
22
  * Run tests:
23
- * node --import ./web/test/_setup-globals.mjs --test web/test/clawser-peer-collab-bridge.test.mjs
23
+ * cd packages/browsermesh-sync && node --import ./test/_setup-globals.mjs --test test/collab-bridge.test.mjs
24
24
  */
25
25
 
26
26
  import {
@@ -52,16 +52,16 @@ export const CRDT_SYNC_SERVICE_TYPE = 'crdt-sync'
52
52
  * One CollabBridge per connected peer.
53
53
  */
54
54
  export class CollabBridge {
55
- /** @type {import('./clawser-peer-session.js').PeerSession} */
55
+ /** @type {PeerSession} */
56
56
  #peerSession
57
57
 
58
58
  /** @type {Map<string, CollabSession>} docId -> CollabSession */
59
59
  #collabSessions = new Map()
60
60
 
61
- /** @type {import('./clawser-mesh-sync.js').MeshSyncEngine|null} */
61
+ /** @type {import('./sync.mjs').MeshSyncEngine|null} */
62
62
  #syncEngine
63
63
 
64
- /** @type {import('./clawser-mesh-delta-sync.js').SyncCoordinator|null} */
64
+ /** @type {import('./delta-sync.mjs').SyncCoordinator|null} */
65
65
  #syncCoordinator
66
66
 
67
67
  /** @type {string} */
@@ -78,9 +78,9 @@ export class CollabBridge {
78
78
 
79
79
  /**
80
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]
81
+ * @param {PeerSession} opts.peerSession
82
+ * @param {import('./sync.mjs').MeshSyncEngine} [opts.syncEngine]
83
+ * @param {import('./delta-sync.mjs').SyncCoordinator} [opts.syncCoordinator]
84
84
  * @param {Function} [opts.onLog]
85
85
  */
86
86
  constructor({ peerSession, syncEngine, syncCoordinator, onLog }) {
@@ -340,13 +340,13 @@ export class CollabManager {
340
340
  /** @type {Map<string, CollabBridge>} remotePodId -> CollabBridge */
341
341
  #bridges = new Map()
342
342
 
343
- /** @type {import('./clawser-mesh-sync.js').MeshSyncEngine|null} */
343
+ /** @type {import('./sync.mjs').MeshSyncEngine|null} */
344
344
  #syncEngine
345
345
 
346
- /** @type {import('./clawser-mesh-delta-sync.js').SyncCoordinator|null} */
346
+ /** @type {import('./delta-sync.mjs').SyncCoordinator|null} */
347
347
  #syncCoordinator
348
348
 
349
- /** @type {import('./clawser-peer-session.js').SessionManager|null} */
349
+ /** @type {SessionManager|null} */
350
350
  #sessionManager
351
351
 
352
352
  /** @type {string} */
@@ -373,9 +373,9 @@ export class CollabManager {
373
373
  /**
374
374
  * @param {object} opts
375
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]
376
+ * @param {import('./sync.mjs').MeshSyncEngine} [opts.syncEngine]
377
+ * @param {import('./delta-sync.mjs').SyncCoordinator} [opts.syncCoordinator]
378
+ * @param {SessionManager} [opts.sessionManager]
379
379
  * @param {Function} [opts.onLog]
380
380
  */
381
381
  constructor({ localPodId, syncEngine, syncCoordinator, sessionManager, onLog }) {
@@ -400,7 +400,7 @@ export class CollabManager {
400
400
  /**
401
401
  * Attach to a PeerNode to auto-manage bridges on peer lifecycle events.
402
402
  *
403
- * @param {import('./clawser-peer-node.js').PeerNode} peerNode
403
+ * @param {PeerNode} peerNode
404
404
  */
405
405
  attach(peerNode) {
406
406
  if (this.#peerNode) this.detach()
@@ -496,7 +496,7 @@ export class CollabManager {
496
496
  * falls back to direct PeerNode session lookup.
497
497
  *
498
498
  * @param {string} peerId
499
- * @returns {import('./clawser-peer-session.js').PeerSession|null}
499
+ * @returns {PeerSession|null}
500
500
  */
501
501
  #findPeerSession(peerId) {
502
502
  // Try SessionManager first (provides full PeerSession with handler routing)
package/src/collab.mjs CHANGED
@@ -1,6 +1,6 @@
1
1
  /**
2
2
  // STATUS: EXPERIMENTAL — complete implementation, not yet integrated into main application
3
- * clawser-peer-collab.js -- Real-time collaborative editing via Yjs CRDT.
3
+ * collab.mjs -- Real-time collaborative editing via Yjs CRDT.
4
4
  *
5
5
  * YjsAdapter: wraps Yjs Doc with shared types and sync primitives.
6
6
  * AwarenessState: cursor/selection presence (custom, no Yjs awareness dep).
@@ -11,7 +11,7 @@
11
11
  * No browser-only imports at module level.
12
12
  *
13
13
  * Run tests:
14
- * node --import ./web/test/_setup-globals.mjs --test web/test/clawser-peer-collab.test.mjs
14
+ * cd packages/browsermesh-sync && node --import ./test/_setup-globals.mjs --test test/collab.test.mjs
15
15
  */
16
16
 
17
17
  // ---------------------------------------------------------------------------
@@ -1,6 +1,6 @@
1
1
  /**
2
- // STATUS: INTEGRATED — wired into ClawserPod lifecycle, proven via E2E testing
3
- * clawser-mesh-delta-sync.js -- Delta-Based Synchronization Protocol.
2
+ // STATUS: INTEGRATED — wired into Pod (@johnhenry/browsermesh-pod) lifecycle, proven via E2E testing
3
+ * delta-sync.mjs -- Delta-Based Synchronization Protocol.
4
4
  *
5
5
  * Efficient state synchronization using delta (diff) encoding:
6
6
  *
@@ -14,7 +14,7 @@
14
14
  * No browser-only imports at module level.
15
15
  *
16
16
  * Run tests:
17
- * node --import ./web/test/_setup-globals.mjs --test web/test/clawser-mesh-delta-sync.test.mjs
17
+ * cd packages/browsermesh-sync && node --import ./test/_setup-globals.mjs --test test/delta-sync.test.mjs
18
18
  */
19
19
 
20
20
  // ---------------------------------------------------------------------------
package/src/files.mjs CHANGED
@@ -1,11 +1,11 @@
1
1
  /**
2
- // STATUS: INTEGRATED — wired into ClawserPod lifecycle, proven via E2E testing
3
- * Clawser Mesh Files
2
+ // STATUS: INTEGRATED — wired into Pod (@johnhenry/browsermesh-pod) lifecycle, proven via E2E testing
3
+ * Mesh Files
4
4
  *
5
5
  * Content-addressed file transfer with SHA-256 chunking, progress tracking,
6
6
  * and resume support. Builds on the streams module for transport.
7
7
  *
8
- * @module clawser-mesh-files
8
+ * @module files
9
9
  */
10
10
 
11
11
  import { MESH_TYPE, MESH_ERROR } from '@johnhenry/browsermesh-primitives';
@@ -308,6 +308,12 @@ export class MeshFileTransfer {
308
308
  // Callbacks
309
309
  #onOffer = null;
310
310
  #onProgress = null;
311
+
312
+ /** @type {Set<Function>} multi-listener progress subscribers */
313
+ #progressListeners = new Set();
314
+
315
+ /** @type {Map<string, 'send'|'receive'>} transferId → local role */
316
+ #directions = new Map();
311
317
  #onComplete = null;
312
318
  #onSend = null;
313
319
 
@@ -323,6 +329,28 @@ export class MeshFileTransfer {
323
329
  onComplete(cb) { this.#onComplete = cb; return this; }
324
330
  onSend(cb) { this.#onSend = cb; return this; }
325
331
 
332
+ /**
333
+ * Subscribe to transfer progress. Unlike the single-slot `onProgress`, any
334
+ * number of listeners may subscribe and none displaces another.
335
+ *
336
+ * Listeners receive `{ transferId, direction, bytesDone, bytesTotal,
337
+ * percentComplete, transferRate, state }` where `direction` is `'send'`
338
+ * (this instance created the offer) or `'receive'` (it accepted one) and
339
+ * `state` is the transfer status. An event fires after every chunk
340
+ * (`'transferring'`) and once more on reaching `'completed'` or
341
+ * `'cancelled'`. Events are NOT throttled; throttle in the consumer.
342
+ * A throwing listener is isolated and does not affect other listeners or
343
+ * the transfer.
344
+ *
345
+ * @param {(event: object) => void} fn
346
+ * @returns {() => void} unsubscribe
347
+ */
348
+ subscribeProgress(fn) {
349
+ if (typeof fn !== 'function') throw new TypeError('subscribeProgress requires a function');
350
+ this.#progressListeners.add(fn);
351
+ return () => { this.#progressListeners.delete(fn); };
352
+ }
353
+
326
354
  /** Access the chunk store. */
327
355
  get store() { return this.#store; }
328
356
 
@@ -343,6 +371,7 @@ export class MeshFileTransfer {
343
371
  expires: opts.expires,
344
372
  });
345
373
  this.#offers.set(offer.transferId, offer);
374
+ this.#directions.set(offer.transferId, 'send');
346
375
  const fileSizes = offer.files.map(f => f.size);
347
376
  const state = new TransferState({
348
377
  transferId: offer.transferId,
@@ -373,6 +402,7 @@ export class MeshFileTransfer {
373
402
  throw new Error('Transfer offer has expired');
374
403
  }
375
404
  this.#offers.set(offer.transferId, offer);
405
+ this.#directions.set(offer.transferId, 'receive');
376
406
  const fileSizes = offer.files.map(f => f.size);
377
407
  const state = new TransferState({
378
408
  transferId: offer.transferId,
@@ -403,6 +433,7 @@ export class MeshFileTransfer {
403
433
  const state = this.#states.get(transferId);
404
434
  if (state) {
405
435
  state.status = 'cancelled';
436
+ this._notifyProgress(transferId, state);
406
437
  }
407
438
 
408
439
  this._emit({
@@ -442,7 +473,9 @@ export class MeshFileTransfer {
442
473
  }
443
474
 
444
475
  if (state.isComplete()) {
476
+ const wasDone = state.status === 'completed';
445
477
  state.status = 'completed';
478
+ if (!wasDone) this._notifyProgress(transferId, state);
446
479
  this._emit({
447
480
  t: MESH_TYPE.FILE_COMPLETE,
448
481
  p: { transferId },
@@ -477,7 +510,9 @@ export class MeshFileTransfer {
477
510
  this._emitProgress(transferId, state);
478
511
 
479
512
  if (state.isComplete()) {
513
+ const wasDone = state.status === 'completed';
480
514
  state.status = 'completed';
515
+ if (!wasDone) this._notifyProgress(transferId, state);
481
516
  this._emit({
482
517
  t: MESH_TYPE.FILE_COMPLETE,
483
518
  p: { transferId },
@@ -540,6 +575,7 @@ export class MeshFileTransfer {
540
575
  const state = this.#states.get(transferId);
541
576
  if (state && state.status !== 'completed' && state.status !== 'failed' && state.status !== 'cancelled') {
542
577
  state.status = 'cancelled';
578
+ this._notifyProgress(transferId, state);
543
579
  }
544
580
 
545
581
  this._emit({
@@ -578,19 +614,27 @@ export class MeshFileTransfer {
578
614
  }
579
615
  case MESH_TYPE.FILE_REJECT: {
580
616
  const state = this.#states.get(msg.p.transferId);
581
- if (state) state.status = 'cancelled';
617
+ if (state && state.status !== 'cancelled') {
618
+ state.status = 'cancelled';
619
+ this._notifyProgress(msg.p.transferId, state);
620
+ }
582
621
  this.#offers.delete(msg.p.transferId);
583
622
  break;
584
623
  }
585
624
  case MESH_TYPE.FILE_COMPLETE: {
586
625
  const state = this.#states.get(msg.p.transferId);
626
+ const wasDone = state?.status === 'completed';
587
627
  if (state) state.status = 'completed';
628
+ if (state && !wasDone) this._notifyProgress(msg.p.transferId, state);
588
629
  if (this.#onComplete) this.#onComplete(msg.p.transferId, state);
589
630
  break;
590
631
  }
591
632
  case MESH_TYPE.FILE_CANCEL: {
592
633
  const state = this.#states.get(msg.p.transferId);
593
- if (state && state.status !== 'completed') state.status = 'cancelled';
634
+ if (state && state.status !== 'completed' && state.status !== 'cancelled') {
635
+ state.status = 'cancelled';
636
+ this._notifyProgress(msg.p.transferId, state);
637
+ }
594
638
  break;
595
639
  }
596
640
  }
@@ -602,7 +646,24 @@ export class MeshFileTransfer {
602
646
  if (this.#onSend) this.#onSend(msg);
603
647
  }
604
648
 
649
+ _notifyProgress(transferId, state) {
650
+ if (this.#progressListeners.size === 0) return;
651
+ const event = {
652
+ transferId,
653
+ direction: this.#directions.get(transferId) ?? 'receive',
654
+ bytesDone: state.bytesTransferred,
655
+ bytesTotal: state.totalSize,
656
+ percentComplete: state.percentComplete,
657
+ transferRate: state.transferRate,
658
+ state: state.status,
659
+ };
660
+ for (const fn of [...this.#progressListeners]) {
661
+ try { fn({ ...event }); } catch { /* listener errors never affect the transfer */ }
662
+ }
663
+ }
664
+
605
665
  _emitProgress(transferId, state) {
666
+ this._notifyProgress(transferId, state);
606
667
  if (this.#onProgress) {
607
668
  this.#onProgress(transferId, {
608
669
  bytesTransferred: state.bytesTransferred,
@@ -1,6 +1,6 @@
1
1
  /**
2
- // STATUS: INTEGRATED — wired into ClawserPod lifecycle, proven via E2E testing
3
- * clawser-peer-memory-sync.js — CRDT-backed agent memory replication.
2
+ // STATUS: INTEGRATED — wired into Pod (@johnhenry/browsermesh-pod) lifecycle, proven via E2E testing
3
+ * memory-sync.mjs — CRDT-backed agent memory replication.
4
4
  *
5
5
  * Replicate agent memory across the mesh using CRDTs. An agent's memory
6
6
  * survives device failure — when the user reconnects, memory resyncs
@@ -18,7 +18,7 @@
18
18
  * - trust: use podId to look up trust scores
19
19
  *
20
20
  * Run tests:
21
- * node --import ./web/test/_setup-globals.mjs --test web/test/clawser-peer-memory-sync.test.mjs
21
+ * cd packages/browsermesh-sync && node --import ./test/_setup-globals.mjs --test test/memory-sync.test.mjs
22
22
  */
23
23
 
24
24
  // ---------------------------------------------------------------------------
package/src/migration.mjs CHANGED
@@ -1,13 +1,13 @@
1
1
  import { silentCatch } from './silent-catch.mjs'
2
2
  /**
3
- // STATUS: INTEGRATED — wired into ClawserPod lifecycle, proven via E2E testing
4
- * clawser-mesh-migration.js -- Pod State Migration Between Peers.
3
+ // STATUS: INTEGRATED — wired into Pod (@johnhenry/browsermesh-pod) lifecycle, proven via E2E testing
4
+ * migration.mjs -- Pod State Migration Between Peers.
5
5
  *
6
6
  * Manages checkpointing, transferring, verifying, and activating pod state
7
7
  * on a target peer. Supports zero-downtime handoff via a dual-active window.
8
8
  *
9
9
  * Run tests:
10
- * node --import ./web/test/_setup-globals.mjs --test web/test/clawser-mesh-migration.test.mjs
10
+ * cd packages/browsermesh-sync && node --import ./test/_setup-globals.mjs --test test/migration.test.mjs
11
11
  */
12
12
 
13
13
  // ---------------------------------------------------------------------------
@@ -913,7 +913,7 @@ export class MigrationEngine {
913
913
  if (valid) {
914
914
  this.#checkpoints.set(payload.migrationId || checkpoint.checkpointId, checkpoint);
915
915
  }
916
- } catch (e) { silentCatch('clawser-mesh-migration', 'ignore-malformed-checkpoints', e) }
916
+ } catch (e) { silentCatch('migration', 'ignore-malformed-checkpoints', e) }
917
917
  });
918
918
 
919
919
  // Inbound: state transfer payload
@@ -2,31 +2,43 @@
2
2
  * silent-catch.mjs — debug-gated structured logger for the
3
3
  * `try { … } catch { /* ignore *\/ }` pattern.
4
4
  *
5
- * Vendored from clawser-silent-catch.mjs (fully standalone, no other
6
- * clawser-app imports) so this package doesn't need to reach into web/.
5
+ * Standalone module (no other imports) so this package is self-contained.
7
6
  *
8
7
  * Replaces silent catches with an opt-in inspectable record. Default
9
8
  * behaviour is unchanged (silent), so this is a drop-in replacement.
10
9
  * Users surface the events by enabling debug mode:
11
10
  *
12
- * localStorage.setItem('clawser_debug', 'true') // persistent
13
- * // or, in DevTools: clawserDebug.enable()
11
+ * localStorage.setItem('browsermesh_debug', 'true') // persistent
12
+ * // or, in DevTools: browsermeshDebug.enable()
13
+ *
14
+ * The old `clawser_debug` localStorage key and `clawserDebug` global are
15
+ * deprecated aliases, kept for compatibility.
14
16
  */
15
17
 
18
+ /** localStorage key that enables debug mode. */
19
+ const DEBUG_KEY = 'browsermesh_debug';
20
+ /** Deprecated alias for DEBUG_KEY, kept for compatibility. */
21
+ const LEGACY_DEBUG_KEY = 'clawser_debug';
22
+
23
+ function readDebugFlag() {
24
+ return localStorage.getItem(DEBUG_KEY) === 'true'
25
+ || localStorage.getItem(LEGACY_DEBUG_KEY) === 'true';
26
+ }
27
+
16
28
  let enabled = false;
17
29
  try {
18
30
  if (typeof localStorage !== 'undefined') {
19
- enabled = localStorage.getItem('clawser_debug') === 'true';
31
+ enabled = readDebugFlag();
20
32
  }
21
33
  } catch {
22
34
  // localStorage may throw in privacy / sandbox contexts — treat as disabled
23
35
  }
24
36
 
25
- /** Re-check the localStorage flag; called by clawserDebug.enable/disable. */
37
+ /** Re-check the localStorage flag; called by browsermeshDebug.enable/disable. */
26
38
  export function refreshSilentCatchState() {
27
39
  try {
28
40
  if (typeof localStorage !== 'undefined') {
29
- enabled = localStorage.getItem('clawser_debug') === 'true';
41
+ enabled = readDebugFlag();
30
42
  }
31
43
  } catch {
32
44
  enabled = false;
@@ -36,7 +48,7 @@ export function refreshSilentCatchState() {
36
48
  /**
37
49
  * Log a silent-catch event. No-op unless debug mode is enabled.
38
50
  *
39
- * @param {string} module — file/component (e.g. 'clawser-pod')
51
+ * @param {string} module — file/component (e.g. 'pod')
40
52
  * @param {string} operation — what was attempted (e.g. 'relay-disconnect')
41
53
  * @param {*} error — the caught value
42
54
  * @param {object} [context] — extra structured fields
@@ -51,5 +63,5 @@ export function silentCatch(module, operation, error, context) {
51
63
  }
52
64
  const entry = { module, operation, error: error?.message || String(error) };
53
65
  if (context) Object.assign(entry, context);
54
- console.warn('[clawser:silent-catch]', entry);
66
+ console.warn('[browsermesh:silent-catch]', entry);
55
67
  }
package/src/sync.mjs CHANGED
@@ -1,5 +1,5 @@
1
1
  /**
2
- * clawser-mesh-sync.js — CRDT State Replication Engine.
2
+ * sync.mjs — CRDT State Replication Engine.
3
3
  * Manages CRDT documents with storage persistence and delta-based peer sync.
4
4
  *
5
5
  * Usage: