@johnhenry/browsermesh-apps 0.1.1 → 0.3.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/README.md +443 -1
- package/package.json +18 -3
- package/src/agent-runtime.mjs +342 -0
- package/src/chunk-replication.mjs +751 -0
- package/src/cloud-storage-backend.mjs +932 -0
- package/src/cloud-storage.mjs +799 -0
- package/src/compat.mjs +98 -0
- package/src/gpu-kernel.mjs +242 -0
- package/src/gpu.mjs +101 -7
- package/src/grant-log.mjs +840 -0
- package/src/index.mjs +32 -2
- package/src/kernel-mesh.mjs +56 -0
- package/src/key-distribution.mjs +525 -0
- package/src/manifest-sync.mjs +363 -0
- package/src/mesh-agent-swarm.mjs +376 -0
- package/src/mesh-bootstrap.mjs +1481 -0
- package/src/mesh-compute.mjs +397 -0
- package/src/mesh-dht.mjs +182 -0
- package/src/mesh-fetch.mjs +246 -0
- package/src/mesh-hardening.mjs +226 -0
- package/src/mesh-health.mjs +282 -0
- package/src/mesh-keepalive.mjs +353 -0
- package/src/mesh-kv.mjs +673 -0
- package/src/mesh-orchestrator-tools.mjs +251 -0
- package/src/mesh-orchestrator.mjs +343 -0
- package/src/mesh-relay-backend.mjs +289 -0
- package/src/mesh-relay-host.mjs +362 -0
- package/src/mesh-rpc.mjs +300 -0
- package/src/mesh-service.mjs +403 -0
- package/src/mesh-swarm.mjs +677 -0
- package/src/mesh-sync.mjs +253 -0
- package/src/mesh-timestamp.mjs +265 -0
- package/src/mesh-torrent.mjs +683 -0
- package/src/mesh-verification.mjs +367 -0
- package/src/mesh-websocket.mjs +722 -0
- package/src/observability-bridge.mjs +453 -0
- package/src/orchestrator.mjs +18 -5
- package/src/peer-agent-swarm.mjs +12 -1
- package/src/peer-chat.mjs +230 -61
- package/src/peer-compute.mjs +66 -3
- package/src/peer-escrow.mjs +350 -1
- package/src/peer-files.mjs +362 -168
- package/src/peer-ipfs.mjs +205 -10
- package/src/peer-node.mjs +127 -19
- package/src/peer-registry.mjs +242 -18
- package/src/peer-routing.mjs +369 -5
- package/src/peer-terminal.mjs +400 -130
- package/src/signaling.mjs +231 -0
- package/src/webrtc-negotiator.mjs +209 -0
- package/src/peer-session.mjs +0 -937
package/src/peer-ipfs.mjs
CHANGED
|
@@ -1,12 +1,75 @@
|
|
|
1
1
|
/**
|
|
2
|
-
|
|
3
|
-
*
|
|
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
|
-
*
|
|
6
|
-
*
|
|
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 ./
|
|
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
|
|
29
|
-
*
|
|
30
|
-
*
|
|
31
|
-
*
|
|
32
|
-
*
|
|
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
|
@@ -366,24 +366,110 @@ export class PeerNode {
|
|
|
366
366
|
|
|
367
367
|
this.#registry.connect(pubKey, connectOpts)
|
|
368
368
|
|
|
369
|
-
// Create session
|
|
369
|
+
// Create session (and wire its inbound data fan-out) via the same
|
|
370
|
+
// helper `adoptIncomingSession()` uses for the callee side, so both
|
|
371
|
+
// sides of a connection end up with identically-shaped bookkeeping.
|
|
372
|
+
const session = this.#createSession(pubKey, transportType, transport)
|
|
373
|
+
|
|
374
|
+
this.#onLog('peer-node:session:created', {
|
|
375
|
+
sessionId: session.sessionId,
|
|
376
|
+
pubKey,
|
|
377
|
+
transport: transportType,
|
|
378
|
+
})
|
|
379
|
+
|
|
380
|
+
// Log to audit chain
|
|
381
|
+
await this.#audit('peer-node:connect', {
|
|
382
|
+
sessionId: session.sessionId,
|
|
383
|
+
pubKey,
|
|
384
|
+
transport: transportType,
|
|
385
|
+
})
|
|
386
|
+
|
|
387
|
+
return { ...session, transportInstance: undefined }
|
|
388
|
+
}
|
|
389
|
+
|
|
390
|
+
/**
|
|
391
|
+
* Adopt a `PeerNode`-level session for a transport connection this node
|
|
392
|
+
* did not initiate itself -- the callee side of an inbound connection
|
|
393
|
+
* (e.g. a WebRTC peer that only ever answered an offer, and never called
|
|
394
|
+
* `connectToPeer()`).
|
|
395
|
+
*
|
|
396
|
+
* `connectToPeer()` is the only place that used to create session
|
|
397
|
+
* bookkeeping (the entry in `#sessions`, and the inbound-data fan-out
|
|
398
|
+
* `onIncomingData()` subscribers rely on), which meant a peer that was
|
|
399
|
+
* only ever connected *to* had a live, open transport (visible e.g. via
|
|
400
|
+
* `node.meshManager.getConnection(remotePodId)`) but no `PeerNode`-level
|
|
401
|
+
* session -- `sendTo()` would throw "no active session" and its inbound
|
|
402
|
+
* bytes never reached `onIncomingData()` subscribers. This method gives
|
|
403
|
+
* the callee side the same session bookkeeping the caller side already
|
|
404
|
+
* got, so `sendTo()` / `hasActiveSession()` / `onIncomingData()` all work
|
|
405
|
+
* symmetrically regardless of which side dialed.
|
|
406
|
+
*
|
|
407
|
+
* Intended to be called by transport-negotiator adapter factories (see
|
|
408
|
+
* `webrtc-negotiator.mjs`'s `onIncomingConnection` hook), not directly by
|
|
409
|
+
* application code.
|
|
410
|
+
*
|
|
411
|
+
* @param {string} pubKey - Remote peer's fingerprint / public key hash
|
|
412
|
+
* @param {object} transportInstance - An already-open MeshTransport-shaped
|
|
413
|
+
* object: must implement `send(data)`, should implement `onMessage(cb)`.
|
|
414
|
+
* @param {string} transportType - e.g. `'webrtc'`
|
|
415
|
+
* @returns {Promise<object>} Session info (same shape as `connectToPeer()`'s)
|
|
416
|
+
*/
|
|
417
|
+
async adoptIncomingSession(pubKey, transportInstance, transportType) {
|
|
418
|
+
this.#ensureRunning('adoptIncomingSession')
|
|
419
|
+
if (!pubKey || typeof pubKey !== 'string') {
|
|
420
|
+
throw new Error('adoptIncomingSession: pubKey is required')
|
|
421
|
+
}
|
|
422
|
+
if (!transportInstance || typeof transportInstance.send !== 'function') {
|
|
423
|
+
throw new Error('adoptIncomingSession: transportInstance must implement send(data)')
|
|
424
|
+
}
|
|
425
|
+
|
|
426
|
+
this.#registry.connect(pubKey, { transport: transportType })
|
|
427
|
+
|
|
428
|
+
const session = this.#createSession(pubKey, transportType, transportInstance)
|
|
429
|
+
|
|
430
|
+
this.#onLog('peer-node:session:adopted', {
|
|
431
|
+
sessionId: session.sessionId,
|
|
432
|
+
pubKey,
|
|
433
|
+
transport: transportType,
|
|
434
|
+
})
|
|
435
|
+
|
|
436
|
+
await this.#audit('peer-node:connect', {
|
|
437
|
+
sessionId: session.sessionId,
|
|
438
|
+
pubKey,
|
|
439
|
+
transport: transportType,
|
|
440
|
+
direction: 'inbound',
|
|
441
|
+
})
|
|
442
|
+
|
|
443
|
+
return { ...session, transportInstance: undefined }
|
|
444
|
+
}
|
|
445
|
+
|
|
446
|
+
/**
|
|
447
|
+
* Shared session-bookkeeping helper for both `connectToPeer()` (caller
|
|
448
|
+
* side) and `adoptIncomingSession()` (callee side): creates the
|
|
449
|
+
* `#sessions` entry and wires inbound data fan-out to `onIncomingData()`
|
|
450
|
+
* subscribers -- this is what backs `ClawserPod.onMessage` so workspace
|
|
451
|
+
* consumers can route incoming sync/deploy envelopes by `envelope.type`.
|
|
452
|
+
*
|
|
453
|
+
* @param {string} pubKey
|
|
454
|
+
* @param {string|null} transportType
|
|
455
|
+
* @param {object|null} transportInstance
|
|
456
|
+
* @returns {object} The new session entry (includes `transportInstance`;
|
|
457
|
+
* callers strip it before returning to their own caller)
|
|
458
|
+
*/
|
|
459
|
+
#createSession(pubKey, transportType, transportInstance) {
|
|
370
460
|
const sessionId = crypto.randomUUID()
|
|
371
461
|
const session = {
|
|
372
462
|
sessionId,
|
|
373
463
|
pubKey,
|
|
374
464
|
transport: transportType,
|
|
375
|
-
transportInstance
|
|
465
|
+
transportInstance,
|
|
376
466
|
connectedAt: Date.now(),
|
|
377
467
|
state: 'active',
|
|
378
468
|
}
|
|
379
469
|
this.#sessions.set(sessionId, session)
|
|
380
470
|
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
// This is what backs `ClawserPod.onMessage` so workspace consumers
|
|
384
|
-
// can route incoming sync/deploy envelopes by `envelope.type`.
|
|
385
|
-
if (transport && typeof transport.onMessage === 'function') {
|
|
386
|
-
transport.onMessage((data) => {
|
|
471
|
+
if (transportInstance && typeof transportInstance.onMessage === 'function') {
|
|
472
|
+
transportInstance.onMessage((data) => {
|
|
387
473
|
for (const cb of this.#dataListeners) {
|
|
388
474
|
try { cb(pubKey, data, { sessionId, transport: transportType }) }
|
|
389
475
|
catch (err) { this.#onLog('peer-node:data-listener-error', { error: err?.message || String(err) }) }
|
|
@@ -391,16 +477,30 @@ export class PeerNode {
|
|
|
391
477
|
})
|
|
392
478
|
}
|
|
393
479
|
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
//
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
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
|
+
}
|
|
402
502
|
|
|
403
|
-
return
|
|
503
|
+
return session
|
|
404
504
|
}
|
|
405
505
|
|
|
406
506
|
/**
|
|
@@ -602,8 +702,16 @@ export class PeerNode {
|
|
|
602
702
|
* Register a callback for a node event.
|
|
603
703
|
*
|
|
604
704
|
* Supported events:
|
|
605
|
-
* - 'peer:connect'
|
|
606
|
-
* - 'peer:disconnect'
|
|
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}`.
|
|
607
715
|
* - 'boot' — fired after successful boot
|
|
608
716
|
* - 'shutdown' — fired after shutdown
|
|
609
717
|
*
|