@johnhenry/browsermesh-apps 0.2.0 → 0.4.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.
Files changed (50) hide show
  1. package/README.md +232 -1
  2. package/package.json +7 -3
  3. package/src/agent-runtime.mjs +342 -0
  4. package/src/chunk-replication.mjs +751 -0
  5. package/src/cloud-storage-backend.mjs +932 -0
  6. package/src/cloud-storage.mjs +859 -0
  7. package/src/compat.mjs +98 -0
  8. package/src/grant-log.mjs +840 -0
  9. package/src/index.mjs +34 -2
  10. package/src/key-distribution.mjs +525 -0
  11. package/src/manifest-sync.mjs +363 -0
  12. package/src/mesh-agent-swarm.mjs +376 -0
  13. package/src/mesh-bootstrap.mjs +1131 -12
  14. package/src/mesh-compute.mjs +397 -0
  15. package/src/mesh-dht.mjs +182 -0
  16. package/src/mesh-fetch.mjs +246 -0
  17. package/src/mesh-hardening.mjs +226 -0
  18. package/src/mesh-health.mjs +282 -0
  19. package/src/mesh-keepalive.mjs +353 -0
  20. package/src/mesh-kv.mjs +673 -0
  21. package/src/mesh-orchestrator-tools.mjs +251 -0
  22. package/src/mesh-orchestrator.mjs +343 -0
  23. package/src/mesh-rpc.mjs +300 -0
  24. package/src/mesh-service.mjs +403 -0
  25. package/src/mesh-swarm.mjs +677 -0
  26. package/src/mesh-timestamp.mjs +265 -0
  27. package/src/mesh-torrent.mjs +683 -0
  28. package/src/mesh-verification.mjs +367 -0
  29. package/src/mesh-websocket.mjs +722 -0
  30. package/src/observability-bridge.mjs +453 -0
  31. package/src/orchestrator.mjs +18 -5
  32. package/src/peer-agent-swarm.mjs +12 -1
  33. package/src/peer-chat.mjs +230 -61
  34. package/src/peer-compute.mjs +66 -3
  35. package/src/peer-escrow.mjs +350 -1
  36. package/src/peer-files.mjs +362 -168
  37. package/src/peer-ipfs.mjs +205 -10
  38. package/src/peer-node.mjs +33 -2
  39. package/src/peer-routing.mjs +369 -5
  40. package/src/peer-terminal.mjs +400 -130
  41. package/src/serverless-executor-andbox.mjs +136 -0
  42. package/src/serverless-fetch.mjs +70 -0
  43. package/src/serverless-functions.mjs +67 -0
  44. package/src/serverless-peer-select.mjs +69 -0
  45. package/src/serverless-proxy.mjs +127 -0
  46. package/src/serverless-router.mjs +115 -0
  47. package/src/serverless-sites.mjs +116 -0
  48. package/src/serverless-static.mjs +202 -0
  49. package/src/serverless-wire.mjs +87 -0
  50. package/src/peer-session.mjs +0 -937
package/src/peer-ipfs.mjs CHANGED
@@ -1,12 +1,75 @@
1
1
  /**
2
- // STATUS: INTEGRATED — wired into ClawserPod lifecycle, proven via E2E testing
3
- * clawser-peer-ipfs.js -- Optional Helia/IPFS integration for content-addressed storage.
2
+ * peer-ipfs.mjs -- Mesh-local content-addressed storage (issue #123, split
3
+ * from #84: originally, incorrectly, claimed to depend on `PeerSession` --
4
+ * confirmed self-contained, no `PeerSession` import at all).
4
5
  *
5
- * Feature-flagged -- disabled by default. CDN-loads Helia when enabled.
6
- * When Helia is unavailable, falls back to in-memory SHA-256-addressed storage.
6
+ * `IPFSStore` is a plain, dependency-light class (its only real dependency
7
+ * is `ChunkStore.computeCid()` from `@johnhenry/browsermesh-sync`, for the
8
+ * same SHA-256-hex content-addressing `ChunkStore`/`CloudStorage` already
9
+ * use) -- see `createIpfsService()` below (Phase C, `mesh-service.mjs`) for
10
+ * the `MeshService` wrapper.
11
+ *
12
+ * ---------------------------------------------------------------------------
13
+ * HONEST STATUS OF "HELIA/IPFS" IN THIS FILE (read before assuming the class
14
+ * name, or the word "IPFS" in this file's own history, implies real public
15
+ * IPFS-network interop):
16
+ *
17
+ * 1. `helia`/`@helia/*` is NOT a dependency of this package, or of ANY
18
+ * package in this monorepo (verified by grepping every `package.json` in
19
+ * the repo for `helia` -- zero hits, as a dependency, peerDependency, or
20
+ * otherwise). `ensureLoaded()` only ever reaches for Helia via a runtime
21
+ * `import()` of a jsdelivr CDN URL, and only when `{enabled: true}` --
22
+ * there is no installed, offline-importable, or CI-exercised Helia
23
+ * anywhere in this codebase today. `enabled` defaults to `false`, so in
24
+ * this package's own test suite that CDN path is essentially never
25
+ * exercised -- see `peer-ipfs.test.mjs`'s own "available is false
26
+ * without Helia" test, the only Helia-adjacent behavior anyone has
27
+ * actually verified here.
28
+ * 2. Even setting (1) aside and assuming a real Helia node did load
29
+ * successfully: `add()`, `get()`, `pin()`, `unpin()`, `remove()`,
30
+ * `listCids()`, and `getStats()` below NEVER touch `this.#helia`. Read
31
+ * them -- every one reads/writes `this.#storedCids`, the same in-memory
32
+ * `Map`, unconditionally. The only place `#helia` is referenced again
33
+ * after being set is `close()`, which calls `.stop()` on it. So even a
34
+ * successfully-loaded Helia node is constructed and then never used for
35
+ * a single actual storage operation -- there is no code path in this
36
+ * class, today, that stores or retrieves a byte via Helia/IPFS.
37
+ * 3. The "CID" this class produces is a raw SHA-256 hex digest
38
+ * (`ChunkStore.computeCid()`), NOT a real IPFS CID (a multihash +
39
+ * multibase-encoded identifier, e.g. a `Qm...`/`bafy...` string) -- it's
40
+ * exactly the same format `ChunkStore`/`CloudStorage` already use
41
+ * elsewhere in this repo, not an IPFS-native one.
42
+ *
43
+ * Given all three points, this class is accurately described as: **mesh-local
44
+ * content-addressed storage with a `ChunkStore`-compatible CID format, plus
45
+ * an inert, never-actually-wired hook that would start toward a real Helia
46
+ * backend IF `helia` ever became a real dependency and `add()`/`get()`/
47
+ * `pin()`/`unpin()`/`remove()` were rewritten to call into it -- not real,
48
+ * working IPFS-network interop today.** Do not repeat "real IPFS network
49
+ * interop" as a settled fact about this file elsewhere in the codebase or in
50
+ * a PR description; it is not true of the code as it stands.
51
+ *
52
+ * WHAT THIS ACTUALLY OFFERS OVER `CloudStorage` (`cloud-storage.mjs`), given
53
+ * the above: `CloudStorage` is encrypted, `PeerRegistry`-gated, and
54
+ * automatically replicated across the mesh via `chunk-replication.mjs` --
55
+ * "put once, every authorized peer can eventually fetch it". `IPFSStore` is
56
+ * the opposite on every one of those axes: unencrypted, ungated, and
57
+ * strictly single-node -- content `add()`ed on one peer's `IPFSStore` is
58
+ * NOT visible to, replicated to, or fetchable by any other peer's
59
+ * `IPFSStore` (there is no wire protocol here at all; each `IPFSStore`
60
+ * instance is a private, local cache, and `createIpfsService()` below does
61
+ * not add one -- see its own "NO WIRE PROTOCOL" note). Its genuinely
62
+ * distinct value today is a lightweight, dependency-light local scratch
63
+ * cache with pin/unpin/storage-cap bookkeeping and an `ensureLoaded()`/
64
+ * `close()` lifecycle -- not an IPFS-network-interop story, which would
65
+ * require someone to actually add `helia` as a dependency and rewire the
66
+ * storage operations to call through to it, neither of which has happened.
67
+ *
68
+ * ---------------------------------------------------------------------------
69
+ * No browser-only imports at module level.
7
70
  *
8
71
  * Run tests:
9
- * node --import ./web/test/_setup-globals.mjs --test web/test/clawser-peer-ipfs.test.mjs
72
+ * node --import ./test/_setup-globals.mjs --test test/peer-ipfs.test.mjs
10
73
  */
11
74
 
12
75
  import { ChunkStore } from '@johnhenry/browsermesh-sync'
@@ -25,11 +88,14 @@ export const IPFS_DEFAULTS = Object.freeze({
25
88
  // ---------------------------------------------------------------------------
26
89
 
27
90
  /**
28
- * Content-addressed storage with optional Helia/IPFS backend.
29
- *
30
- * When Helia is available and enabled, data is stored in a real IPFS node.
31
- * Otherwise, data is kept in memory keyed by SHA-256 CID (same format used
32
- * by ChunkStore in browsermesh-sync).
91
+ * Content-addressed storage, with an inert `ensureLoaded()` hook toward a
92
+ * Helia/IPFS backend that is not actually wired to any storage operation
93
+ * below -- see the module doc comment's "HONEST STATUS OF HELIA/IPFS"
94
+ * section before reading this class as "real IPFS". In practice, today,
95
+ * `add`/`get`/`pin`/`unpin`/`remove`/`listCids`/`getStats` always read and
96
+ * write the in-memory `#storedCids` map, keyed by SHA-256 CID (same format
97
+ * used by `ChunkStore` in `browsermesh-sync`), regardless of whether
98
+ * `ensureLoaded()` happened to load a Helia instance.
33
99
  */
34
100
  export class IPFSStore {
35
101
  /** @type {object|null} Helia node instance (lazy-loaded) */
@@ -372,3 +438,132 @@ export class IPFSStore {
372
438
  }
373
439
  }
374
440
  }
441
+
442
+ // ---------------------------------------------------------------------------
443
+ // createIpfsService -- MeshService wiring (Phase 6 consumer, issue #123)
444
+ // ---------------------------------------------------------------------------
445
+
446
+ /**
447
+ * Build a `MeshService` descriptor (`mesh-service.mjs`, Phase C) that wires
448
+ * an `IPFSStore` onto a real `PeerNode`. One `IPFSStore` instance is
449
+ * constructed per `attach()` call, exactly like `createMeshRoutingService()`
450
+ * (`peer-routing.mjs`) constructs one `MeshRouter` per `attach()` -- the
451
+ * closest existing precedent for wrapping an already-standalone,
452
+ * `on`/`off`-based class as a `MeshService` with no rewrite of its internals.
453
+ *
454
+ * ---------------------------------------------------------------------------
455
+ * NO WIRE PROTOCOL -- unlike `createMeshRoutingService()`, this descriptor
456
+ * does NOT call `ctx.onIncomingData()` or `ctx.sendTo()` anywhere.
457
+ * `IPFSStore` has no `forwardFn`/`fetchFn`-shaped dependency to wire onto the
458
+ * network in the first place -- every one of its methods
459
+ * (`add`/`get`/`pin`/`unpin`/`remove`/`listCids`/`getStats`) is a purely
460
+ * local operation on its own in-memory `#storedCids` map (see this file's
461
+ * "HONEST STATUS OF HELIA/IPFS" header comment for the full explanation of
462
+ * why that's true even when Helia loads). Attaching this service to two
463
+ * different `PeerNode`s therefore produces two entirely independent stores:
464
+ * content `add()`ed through peer A's `api.add()` is never visible via peer
465
+ * B's `api.get()`, with no `ctx.sendTo()` call anywhere in this file able to
466
+ * change that. This is the concrete, mechanical reason this class is
467
+ * "mesh-local" rather than "mesh-wide" -- see the module doc comment's
468
+ * "WHAT THIS ACTUALLY OFFERS OVER CloudStorage" section for the fuller
469
+ * comparison.
470
+ *
471
+ * ---------------------------------------------------------------------------
472
+ * EVENTS (`ctx.emit()`, see `mesh-service.mjs`'s "Observability events"
473
+ * section for the full convention) -- `IPFSStore`'s own pre-existing
474
+ * `on`/`off` surface (`'add'`/`'remove'`/`'pin'`/`'unpin'`) is bridged
475
+ * through verbatim, prefixed `peer-ipfs:`:
476
+ *
477
+ * - `peer-ipfs:add` -- `{cid, size}`, whenever `api.add()` stores new
478
+ * (or re-stores identical, deduped) content (`IPFSStore`'s own `'add'`
479
+ * payload, unchanged).
480
+ * - `peer-ipfs:remove` -- `{cid}` (`IPFSStore`'s own `'remove'` payload).
481
+ * - `peer-ipfs:pin` -- `{cid}` (`IPFSStore`'s own `'pin'` payload).
482
+ * - `peer-ipfs:unpin` -- `{cid}` (`IPFSStore`'s own `'unpin'` payload).
483
+ *
484
+ * `IPFSStore`'s own `onLog(level, message)` callback (a NUMBER level --
485
+ * `0` error, `1` warn, `2` info -- unlike this family's usual
486
+ * `onLog(event, data)` shape) is bridged into the caller-supplied `onLog`
487
+ * as `onLog('peer-ipfs:log', {level, message})`, so a caller only ever
488
+ * implements one `onLog` shape regardless of which mesh service it's
489
+ * listening to.
490
+ *
491
+ * `teardown()` calls `store.close()` -- clearing all locally-stored content
492
+ * and shutting down `#helia` if `ensureLoaded()` ever set one -- since the
493
+ * store is entirely owned by this `attach()` call (a fresh `IPFSStore` is
494
+ * constructed every time `createIpfsService()`'s descriptor is attached; see
495
+ * `mesh-service.mjs`'s own "teardown() only reverses what attach() itself
496
+ * did" contract).
497
+ *
498
+ * No browser-only imports at module level.
499
+ */
500
+
501
+ /**
502
+ * @param {object} [opts]
503
+ * @param {boolean} [opts.enabled] - Forwarded to `new IPFSStore()` (default
504
+ * `false` -- see `IPFS_DEFAULTS`). See module doc comment before setting
505
+ * this expecting real IPFS-network behavior; it only changes whether
506
+ * `ensureLoaded()` attempts (and, per the module doc comment, still never
507
+ * actually uses) a CDN-loaded Helia instance.
508
+ * @param {number} [opts.maxStorageMb] - Forwarded to `new IPFSStore()`
509
+ * (default 100).
510
+ * @param {Function} [opts.onLog] - `(event: string, data: object) => void`,
511
+ * this family's usual shape. Receives `IPFSStore`'s own `(level, message)`
512
+ * log lines wrapped as `onLog('peer-ipfs:log', {level, message})` -- see
513
+ * module doc comment's "EVENTS" section.
514
+ * @returns {import('./mesh-service.mjs').MeshService}
515
+ */
516
+ export function createIpfsService(opts = {}) {
517
+ const { enabled, maxStorageMb, onLog } = opts
518
+ const log = onLog || (() => {})
519
+
520
+ return {
521
+ name: 'peer-ipfs',
522
+
523
+ attach(peerNode, ctx) {
524
+ const store = new IPFSStore({
525
+ enabled,
526
+ maxStorageMb,
527
+ onLog: (level, message) => log('peer-ipfs:log', { level, message }),
528
+ })
529
+
530
+ // Bridge IPFSStore's own pre-existing on()/off() events through
531
+ // ctx.emit() -- see module doc comment's "EVENTS" section.
532
+ const onAdd = (data) => ctx.emit('peer-ipfs:add', data)
533
+ const onRemove = (data) => ctx.emit('peer-ipfs:remove', data)
534
+ const onPin = (data) => ctx.emit('peer-ipfs:pin', data)
535
+ const onUnpin = (data) => ctx.emit('peer-ipfs:unpin', data)
536
+ store.on('add', onAdd)
537
+ store.on('remove', onRemove)
538
+ store.on('pin', onPin)
539
+ store.on('unpin', onUnpin)
540
+
541
+ const api = {
542
+ ensureLoaded: () => store.ensureLoaded(),
543
+ add: (data) => store.add(data),
544
+ get: (cid) => store.get(cid),
545
+ pin: (cid) => store.pin(cid),
546
+ unpin: (cid) => store.unpin(cid),
547
+ listCids: () => store.listCids(),
548
+ remove: (cid) => store.remove(cid),
549
+ getStats: () => store.getStats(),
550
+ close: () => store.close(),
551
+ toJSON: () => store.toJSON(),
552
+ isEnabled: () => store.enabled,
553
+ isLoaded: () => store.loaded,
554
+ isAvailable: () => store.available,
555
+ }
556
+
557
+ return {
558
+ api,
559
+ async teardown() {
560
+ store.off('add', onAdd)
561
+ store.off('remove', onRemove)
562
+ store.off('pin', onPin)
563
+ store.off('unpin', onUnpin)
564
+ await store.close()
565
+ },
566
+ }
567
+ },
568
+ }
569
+ }
package/src/peer-node.mjs CHANGED
@@ -477,6 +477,29 @@ export class PeerNode {
477
477
  })
478
478
  }
479
479
 
480
+ // Bonus wiring for issue #110: surface a transport's own native
481
+ // close/error signal (e.g. WebRTC's connectionState -- see
482
+ // WebRTCTransportAdapter's onClose()/onError() in
483
+ // @johnhenry/browsermesh-transport's webrtc.mjs, which both ultimately
484
+ // derive from RTCPeerConnection.connectionState -- but this is generic
485
+ // duck-typing against MeshTransport's onClose()/onError(), not
486
+ // webrtc-specific) as a PeerNode-level event, the same way onMessage()
487
+ // is already fanned out above. Before this, nothing in this file ever
488
+ // called onError()/onClose() on a session's transportInstance -- a
489
+ // transport reporting itself dead had no path to PeerNode at all.
490
+ // mesh-keepalive.mjs (issue #110) listens for these as a stronger,
491
+ // immediate complement to its own ping/pong liveness probing.
492
+ if (transportInstance && typeof transportInstance.onClose === 'function') {
493
+ transportInstance.onClose(() => {
494
+ this.#emit('peer:transport-close', { pubKey, sessionId, transport: transportType })
495
+ })
496
+ }
497
+ if (transportInstance && typeof transportInstance.onError === 'function') {
498
+ transportInstance.onError((err) => {
499
+ this.#emit('peer:transport-error', { pubKey, sessionId, transport: transportType, error: err?.message || String(err) })
500
+ })
501
+ }
502
+
480
503
  return session
481
504
  }
482
505
 
@@ -679,8 +702,16 @@ export class PeerNode {
679
702
  * Register a callback for a node event.
680
703
  *
681
704
  * Supported events:
682
- * - 'peer:connect' — fired when a peer connects
683
- * - 'peer:disconnect' — fired when a peer disconnects
705
+ * - 'peer:connect' — fired when a peer connects
706
+ * - 'peer:disconnect' — fired when a peer disconnects
707
+ * - 'peer:transport-close' — fired when a session's transportInstance
708
+ * reports itself closed via its own onClose() (duck-typed against
709
+ * MeshTransport -- e.g. WebRTC's connectionState going
710
+ * failed/disconnected/closed). Payload: `{pubKey, sessionId, transport}`.
711
+ * Only fires for transports that implement onClose(); a no-op for
712
+ * those that don't. See issue #110.
713
+ * - 'peer:transport-error' — same, for onError(). Payload:
714
+ * `{pubKey, sessionId, transport, error}`.
684
715
  * - 'boot' — fired after successful boot
685
716
  * - 'shutdown' — fired after shutdown
686
717
  *