@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.
- package/README.md +298 -17
- package/package.json +13 -9
- package/src/apps.mjs +4 -4
- package/src/audit.mjs +2 -2
- package/src/chat.mjs +3 -3
- package/src/cloud-storage-backend.mjs +5 -7
- package/src/cloud-storage.mjs +5 -4
- package/src/compat.mjs +2 -2
- package/src/consensus.mjs +7 -7
- package/src/devtools.mjs +3 -3
- package/src/gpu.mjs +2 -2
- package/src/index.mjs +12 -0
- package/src/internal/channel-codec.mjs +179 -0
- package/src/internal/json-message.mjs +49 -0
- package/src/internal/relay-flow.mjs +178 -0
- package/src/internal/request-target.mjs +280 -0
- package/src/internal/rpc-body.mjs +33 -0
- package/src/marketplace-ui.mjs +1 -1
- package/src/mesh-agent-swarm.mjs +1 -1
- package/src/mesh-bootstrap.mjs +47 -1
- package/src/mesh-channel.mjs +716 -0
- package/src/mesh-fetch.mjs +77 -263
- package/src/mesh-hardening.mjs +8 -1
- package/src/mesh-relay-backend.mjs +116 -22
- package/src/mesh-relay-host.mjs +296 -55
- package/src/mesh-rpc.mjs +126 -265
- package/src/mesh-sync.mjs +1 -1
- package/src/orchestrator.mjs +54 -29
- package/src/payments.mjs +56 -14
- package/src/peer-agent-swarm.mjs +2 -2
- package/src/peer-agent.mjs +7 -7
- package/src/peer-compute.mjs +2 -2
- package/src/peer-encrypted-store.mjs +3 -3
- package/src/peer-escrow.mjs +2 -2
- package/src/peer-health.mjs +3 -3
- package/src/peer-node.mjs +8 -5
- package/src/peer-payments.mjs +2 -2
- package/src/peer-registry.mjs +45 -3
- package/src/peer-routing.mjs +158 -21
- package/src/peer-services.mjs +3 -3
- package/src/peer-timestamp.mjs +3 -3
- package/src/peer-torrent.mjs +112 -47
- package/src/peer-verification.mjs +3 -3
- package/src/pod-host-service.mjs +48 -7
- package/src/pod-placement.mjs +582 -0
- package/src/pod-supervisor.mjs +9 -30
- package/src/protocol-channel.mjs +327 -0
- package/src/resources.mjs +2 -2
- package/src/restart-policy.mjs +67 -0
- package/src/scheduler.mjs +3 -3
- package/src/serverless-proxy.mjs +58 -7
- package/src/silent-catch.mjs +22 -9
- package/src/tools.mjs +3 -3
- package/src/visualizations.mjs +2 -2
- 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
|
+
}
|