@johnhenry/browsermesh-apps 0.15.0 → 0.17.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 (55) hide show
  1. package/README.md +298 -17
  2. package/package.json +13 -9
  3. package/src/apps.mjs +4 -4
  4. package/src/audit.mjs +2 -2
  5. package/src/chat.mjs +3 -3
  6. package/src/cloud-storage-backend.mjs +5 -7
  7. package/src/cloud-storage.mjs +5 -4
  8. package/src/compat.mjs +2 -2
  9. package/src/consensus.mjs +7 -7
  10. package/src/devtools.mjs +3 -3
  11. package/src/gpu.mjs +2 -2
  12. package/src/index.mjs +12 -0
  13. package/src/internal/channel-codec.mjs +179 -0
  14. package/src/internal/json-message.mjs +49 -0
  15. package/src/internal/relay-flow.mjs +178 -0
  16. package/src/internal/request-target.mjs +280 -0
  17. package/src/internal/rpc-body.mjs +33 -0
  18. package/src/marketplace-ui.mjs +1 -1
  19. package/src/mesh-agent-swarm.mjs +1 -1
  20. package/src/mesh-bootstrap.mjs +47 -1
  21. package/src/mesh-channel.mjs +716 -0
  22. package/src/mesh-fetch.mjs +77 -263
  23. package/src/mesh-hardening.mjs +8 -1
  24. package/src/mesh-relay-backend.mjs +116 -22
  25. package/src/mesh-relay-host.mjs +296 -55
  26. package/src/mesh-rpc.mjs +126 -265
  27. package/src/mesh-sync.mjs +1 -1
  28. package/src/orchestrator.mjs +54 -29
  29. package/src/payments.mjs +56 -14
  30. package/src/peer-agent-swarm.mjs +2 -2
  31. package/src/peer-agent.mjs +7 -7
  32. package/src/peer-compute.mjs +2 -2
  33. package/src/peer-encrypted-store.mjs +3 -3
  34. package/src/peer-escrow.mjs +2 -2
  35. package/src/peer-health.mjs +3 -3
  36. package/src/peer-node.mjs +8 -5
  37. package/src/peer-payments.mjs +2 -2
  38. package/src/peer-registry.mjs +45 -3
  39. package/src/peer-routing.mjs +158 -21
  40. package/src/peer-services.mjs +3 -3
  41. package/src/peer-timestamp.mjs +3 -3
  42. package/src/peer-torrent.mjs +112 -47
  43. package/src/peer-verification.mjs +3 -3
  44. package/src/pod-host-service.mjs +48 -7
  45. package/src/pod-placement.mjs +582 -0
  46. package/src/pod-supervisor.mjs +9 -30
  47. package/src/protocol-channel.mjs +327 -0
  48. package/src/resources.mjs +2 -2
  49. package/src/restart-policy.mjs +67 -0
  50. package/src/scheduler.mjs +3 -3
  51. package/src/serverless-proxy.mjs +58 -7
  52. package/src/silent-catch.mjs +22 -9
  53. package/src/tools.mjs +3 -3
  54. package/src/visualizations.mjs +2 -2
  55. package/src/wsh-negotiator.mjs +97 -0
@@ -0,0 +1,97 @@
1
+ /**
2
+ * wsh-negotiator.mjs -- wires `@johnhenry/browsermesh-transport`'s wsh peer
3
+ * links (`WshPeerLinks`: sealed, key-authenticated mesh links over wsh) into
4
+ * `PeerNode`, the way `webrtc-negotiator.mjs` wires WebRTC.
5
+ *
6
+ * - `createWshAuthorizer(registry)` is the `authorize` callback for the
7
+ * LISTENING side: a peer may link to this pod only if `PeerRegistry.
8
+ * checkAccess(podId, 'mesh:link', 'connect')` allows it (the ACL scope
9
+ * `mesh:link:connect`; `grantCapabilities()` / `revokeCapabilities()` /
10
+ * `MeshACL` wildcards all apply). It runs after the peer's key signature
11
+ * verified and before any mesh frame; default deny.
12
+ * - `createWshAdapterFactory(links)` is the `'wsh'` adapter factory for
13
+ * `MeshTransportNegotiator.registerAdapter()`. As with `'webrtc'`, the
14
+ * endpoint is the remote peer's pod id. A link to a different pod than the
15
+ * one asked for cannot exist: the dialler pins the host key to that pod id.
16
+ * - `attachWshLinks({ node, links, negotiator })` registers the adapter and
17
+ * gives the callee side of an inbound link the same PeerNode session
18
+ * bookkeeping `connectToPeer()` gives the caller side
19
+ * (`adoptIncomingSession()`), keyed by the pod id derived from the key wsh
20
+ * authenticated -- so `fromPubKey` in `onIncomingData()` is that key and
21
+ * never a field a peer wrote.
22
+ *
23
+ * `createMeshNode({ wsh: { relays, ... } })` does all of this. `links` is
24
+ * duck-typed (no import of the transport package here), so this module loads
25
+ * without it.
26
+ *
27
+ * No browser-only imports at module level.
28
+ */
29
+
30
+ /** The ACL resource a peer needs `connect` on to link to this pod. */
31
+ export const WSH_LINK_RESOURCE = 'mesh:link'
32
+ /** The ACL action. */
33
+ export const WSH_LINK_ACTION = 'connect'
34
+
35
+ /**
36
+ * `authorize` for `WshPeerLinks`, backed by a `PeerRegistry`.
37
+ *
38
+ * @param {import('./peer-registry.mjs').PeerRegistry} registry
39
+ * @param {object} [opts]
40
+ * @param {string} [opts.resource='mesh:link']
41
+ * @param {string} [opts.action='connect']
42
+ * @param {boolean} [opts.requireKnownPeer=false] - Also require the peer to be in the registry's peer list.
43
+ * @returns {(peer: { podId: string }) => boolean}
44
+ */
45
+ export function createWshAuthorizer(registry, { resource = WSH_LINK_RESOURCE, action = WSH_LINK_ACTION, requireKnownPeer = false } = {}) {
46
+ if (!registry || typeof registry.checkAccess !== 'function') throw new TypeError('createWshAuthorizer: a PeerRegistry (with checkAccess) is required')
47
+ return ({ podId }) => {
48
+ if (requireKnownPeer && !registry.getPeer(podId)) return false
49
+ return registry.checkAccess(podId, resource, action)?.allowed === true
50
+ }
51
+ }
52
+
53
+ /**
54
+ * `'wsh'` adapter factory for `MeshTransportNegotiator`.
55
+ *
56
+ * @param {{ link: (podId: string) => object }} links - a `WshPeerLinks`
57
+ * @returns {(endpoint: string, auth?: object) => Promise<object>}
58
+ */
59
+ export function createWshAdapterFactory(links) {
60
+ if (!links || typeof links.link !== 'function') throw new TypeError('createWshAdapterFactory: a WshPeerLinks is required')
61
+ return async (endpoint) => {
62
+ const podId = typeof endpoint === 'string' ? endpoint : endpoint?.podId
63
+ const link = links.link(podId)
64
+ try {
65
+ await link.connect()
66
+ } catch (err) {
67
+ link.dispose?.()
68
+ throw err
69
+ }
70
+ return link
71
+ }
72
+ }
73
+
74
+ /**
75
+ * Register the wsh adapter on `negotiator` and adopt inbound links as PeerNode sessions.
76
+ *
77
+ * @param {object} opts
78
+ * @param {import('./peer-node.mjs').PeerNode} opts.node
79
+ * @param {object} opts.links - a `WshPeerLinks`
80
+ * @param {import('@johnhenry/browsermesh-transport').MeshTransportNegotiator} opts.negotiator
81
+ * @param {(event: string, data: object) => void} [opts.onLog]
82
+ * @returns {{ links: object, detach: () => void }}
83
+ */
84
+ export function attachWshLinks({ node, links, negotiator, onLog }) {
85
+ if (!node || typeof node.adoptIncomingSession !== 'function') throw new TypeError('attachWshLinks: a PeerNode is required')
86
+ if (!negotiator || typeof negotiator.registerAdapter !== 'function') throw new TypeError('attachWshLinks: a MeshTransportNegotiator is required')
87
+ const log = onLog || (() => {})
88
+ negotiator.registerAdapter('wsh', createWshAdapterFactory(links))
89
+ const off = links.on('peer-up', ({ podId, initiator }) => {
90
+ // A link this node dialled is adopted by connectToPeer() itself.
91
+ if (initiator === 'local' || node.hasActiveSession(podId)) return
92
+ node.adoptIncomingSession(podId, links.link(podId), 'wsh').catch((err) => {
93
+ log('wsh-negotiator:adopt-incoming-session-failed', { podId, error: err?.message || String(err) })
94
+ })
95
+ })
96
+ return { links, detach: off }
97
+ }