@johnhenry/browsermesh-apps 0.10.0 → 0.12.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 +604 -7
- package/package.json +3 -1
- package/src/chunk-replication.mjs +2 -2
- package/src/index.mjs +5 -0
- package/src/internal/wire-envelope.mjs +33 -0
- package/src/mesh-bootstrap.mjs +1 -1
- package/src/mesh-orchestrator-tools.mjs +77 -10
- package/src/mesh-orchestrator.mjs +1 -1
- package/src/mesh-relay-backend.mjs +2 -0
- package/src/mesh-relay-host.mjs +3 -0
- package/src/mesh-service.mjs +28 -6
- package/src/mesh-sync.mjs +2 -0
- package/src/mesh-torrent.mjs +3 -3
- package/src/mesh-websocket.mjs +3 -1
- package/src/orchestrator.mjs +895 -6
- package/src/peer-node-transport.mjs +114 -0
- package/src/peer-node.mjs +94 -2
- package/src/pod-host-gateway.mjs +372 -0
- package/src/pod-host-routes.mjs +630 -0
- package/src/pod-host-service.mjs +732 -0
- package/src/pod-supervisor.mjs +784 -0
- package/src/resources.mjs +107 -2
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@johnhenry/browsermesh-apps",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.12.0",
|
|
4
4
|
"description": "Application layer: marketplace, chat, payments, compute, orchestration, and agent tools for BrowserMesh",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./src/index.mjs",
|
|
@@ -28,6 +28,7 @@
|
|
|
28
28
|
},
|
|
29
29
|
"peerDependencies": {
|
|
30
30
|
"@johnhenry/browsermesh-primitives": ">=0.2.0 <1.0.0",
|
|
31
|
+
"@johnhenry/browsermesh-pod": ">=0.1.0 <1.0.0",
|
|
31
32
|
"@johnhenry/browsermesh-core": ">=0.5.0 <1.0.0",
|
|
32
33
|
"@johnhenry/browsermesh-transport": ">=0.1.0 <1.0.0",
|
|
33
34
|
"@johnhenry/browsermesh-sync": ">=0.0.0 <1.0.0",
|
|
@@ -58,6 +59,7 @@
|
|
|
58
59
|
},
|
|
59
60
|
"devDependencies": {
|
|
60
61
|
"@johnhenry/andbox": ">=0.0.1 <1.0.0",
|
|
62
|
+
"@johnhenry/browsermesh-pod": "^0.1.0",
|
|
61
63
|
"node-datachannel": "^0.33.4",
|
|
62
64
|
"fake-indexeddb": "^6.2.5",
|
|
63
65
|
"webgpu": "^0.6.1"
|
|
@@ -467,7 +467,7 @@ export function createChunkReplicationService({
|
|
|
467
467
|
return
|
|
468
468
|
}
|
|
469
469
|
|
|
470
|
-
await ctx.sendTo(fromPubKey, envelopeType, { bucketId, kind: 'chunk-fetch-response', requestId: msg.requestId, cid: msg.cid, data: toBase64(bytes) }).catch((err) => {
|
|
470
|
+
await ctx.sendTo(fromPubKey, envelopeType, { bucketId, kind: 'chunk-fetch-response', requestId: msg.requestId, cid: msg.cid, data: toBase64(bytes) }, { channel: 'bulk' }).catch((err) => {
|
|
471
471
|
log('chunk-replication:fetch-response-send-failed', { bucketId, to: fromPubKey, cid: msg.cid, error: err?.message || String(err) })
|
|
472
472
|
})
|
|
473
473
|
}
|
|
@@ -530,7 +530,7 @@ export function createChunkReplicationService({
|
|
|
530
530
|
const bytes = chunkBytesByCid.get(cid)
|
|
531
531
|
if (!bytes) continue // we don't hold this chunk ourselves -- nothing to push
|
|
532
532
|
sends.push(
|
|
533
|
-
ctx.sendTo(target, envelopeType, { bucketId, kind: 'chunk-push', requestId, cid, data: toBase64(bytes) }).catch((err) => {
|
|
533
|
+
ctx.sendTo(target, envelopeType, { bucketId, kind: 'chunk-push', requestId, cid, data: toBase64(bytes) }, { channel: 'bulk' }).catch((err) => {
|
|
534
534
|
log('chunk-replication:push-send-failed', { bucketId, to: target, cid, error: err?.message || String(err) })
|
|
535
535
|
}),
|
|
536
536
|
)
|
package/src/index.mjs
CHANGED
|
@@ -23,6 +23,7 @@ export * from './peer-files.mjs';
|
|
|
23
23
|
export * from './peer-health.mjs';
|
|
24
24
|
export * from './peer-ipfs.mjs';
|
|
25
25
|
export * from './peer-node.mjs';
|
|
26
|
+
export * from './peer-node-transport.mjs';
|
|
26
27
|
export * from './signaling.mjs';
|
|
27
28
|
export * from './webrtc-negotiator.mjs';
|
|
28
29
|
export * from './mesh-bootstrap.mjs';
|
|
@@ -61,6 +62,10 @@ export * from './serverless-peer-select.mjs';
|
|
|
61
62
|
export * from './serverless-sites.mjs';
|
|
62
63
|
export * from './observability-bridge.mjs';
|
|
63
64
|
export * from './mesh-kv.mjs';
|
|
65
|
+
export * from './pod-host-service.mjs';
|
|
66
|
+
export * from './pod-host-routes.mjs';
|
|
67
|
+
export * from './pod-host-gateway.mjs';
|
|
68
|
+
export * from './pod-supervisor.mjs';
|
|
64
69
|
export * from './peer-payments.mjs';
|
|
65
70
|
export * from './peer-registry.mjs';
|
|
66
71
|
export * from './peer-routing.mjs';
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Inbound half of the mesh wire-envelope contract.
|
|
3
|
+
*
|
|
4
|
+
* A real transport (`RTCDataChannel`, `WebSocket`) can only carry strings and
|
|
5
|
+
* binary, so a MeshService envelope (`{ type, ...payload }`) crosses it as
|
|
6
|
+
* JSON text -- see `@johnhenry/browsermesh-transport`'s `encodeWireData()`
|
|
7
|
+
* for the outbound half. Some transports parse that text back before they
|
|
8
|
+
* hand it up (`WebRTCPeerConnection` does); others hand `PeerNode` the raw
|
|
9
|
+
* string. A consumer that wants envelope objects therefore has to accept
|
|
10
|
+
* both, which is what this does.
|
|
11
|
+
*
|
|
12
|
+
* Only strings that look like a JSON object or array are parsed, so a plain
|
|
13
|
+
* text payload that merely happens to be valid JSON (`"42"`, `"true"`) is
|
|
14
|
+
* left exactly as the sender wrote it. A string that looks like JSON but
|
|
15
|
+
* does not parse is returned unchanged rather than thrown on -- one corrupt
|
|
16
|
+
* frame must never take down the shared dispatch loop.
|
|
17
|
+
*
|
|
18
|
+
* Browser-safe: no imports.
|
|
19
|
+
*
|
|
20
|
+
* @param {*} data - Whatever `PeerNode.onIncomingData()` delivered.
|
|
21
|
+
* @returns {*} The parsed object/array when `data` was a JSON object/array
|
|
22
|
+
* string; `data` itself otherwise.
|
|
23
|
+
*/
|
|
24
|
+
export function decodeWireData(data) {
|
|
25
|
+
if (typeof data !== 'string') return data
|
|
26
|
+
const first = data.trimStart()[0]
|
|
27
|
+
if (first !== '{' && first !== '[') return data
|
|
28
|
+
try {
|
|
29
|
+
return JSON.parse(data)
|
|
30
|
+
} catch {
|
|
31
|
+
return data
|
|
32
|
+
}
|
|
33
|
+
}
|
package/src/mesh-bootstrap.mjs
CHANGED
|
@@ -1476,7 +1476,7 @@ export async function createMeshNode(options = {}) {
|
|
|
1476
1476
|
// exists (and is always empty at minimum) when this flag is set, so a
|
|
1477
1477
|
// caller wanting only its own non-orchestrator BrowserTools never has to
|
|
1478
1478
|
// opt into orchestrator wiring it doesn't want. Only pre-populated with the
|
|
1479
|
-
//
|
|
1479
|
+
// 15 real Meshctl*Tools when enableOrchestrator was ALSO set -- see
|
|
1480
1480
|
// mesh-orchestrator-tools.mjs's own doc comment for exactly which of those
|
|
1481
1481
|
// route through the orchestrator service's gated wire dispatch.
|
|
1482
1482
|
if (enableAgentRuntime) {
|
|
@@ -1,9 +1,13 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* mesh-orchestrator-tools.mjs -- Phase 4 of the agent-runtime plan (issues
|
|
3
|
-
* #90/#92): registers `orchestrator.mjs`'s
|
|
3
|
+
* #90/#92): registers `orchestrator.mjs`'s 15 real `Meshctl*Tool` classes
|
|
4
4
|
* (`MeshctlPodsTool`/`MeshctlStatusTool`/`MeshctlExecTool`/`MeshctlDeployTool`/
|
|
5
5
|
* `MeshctlTopTool`/`MeshctlComputeTool`/`MeshctlExposeTool`/`MeshctlDrainTool`,
|
|
6
|
-
*
|
|
6
|
+
* plus issue #185 §8a item 4's hosted-pods control surface five --
|
|
7
|
+
* `MeshctlSpawnTool`/`MeshctlSnapshotTool`/`MeshctlRestoreTool`/
|
|
8
|
+
* `MeshctlHostedPodsTool`/`MeshctlHostsTool` -- plus item 6's supervisor two
|
|
9
|
+
* -- `MeshctlSuperviseTool`/`MeshctlSupervisedTool` -- built via that file's
|
|
10
|
+
* own `createMeshctlTools(orchestrator)` helper) into a
|
|
7
11
|
* `compat.mjs` `BrowserToolRegistry` (Phase 1), wired against a real, attached
|
|
8
12
|
* `MeshOrchestrator` (Phase 3, `mesh-orchestrator.mjs`'s `createOrchestratorService()`).
|
|
9
13
|
* Once registered, `agent-runtime.mjs`'s `createAgentRuntime({registry, llmFn})`
|
|
@@ -112,6 +116,52 @@
|
|
|
112
116
|
* protocol first (out of scope here, exactly per this plan's Phase 4 section).
|
|
113
117
|
*
|
|
114
118
|
* ---------------------------------------------------------------------------
|
|
119
|
+
* `meshctl_spawn`/`meshctl_snapshot`/`meshctl_restore`/`meshctl_hosted_pods`/
|
|
120
|
+
* `meshctl_hosts` -- issue #185 §8a item 4's hosted-pods control surface,
|
|
121
|
+
* ALSO ALWAYS WIRED STRAIGHT TO THE RAW INSTANCE, BUT FOR A DIFFERENT REASON
|
|
122
|
+
* THAN COMPUTE/EXPOSE
|
|
123
|
+
*
|
|
124
|
+
* `MeshctlSpawnTool`/`MeshctlSnapshotTool`/`MeshctlRestoreTool`/
|
|
125
|
+
* `MeshctlHostedPodsTool` call `orchestrator.spawnPod()`/`.snapshotPod()`/
|
|
126
|
+
* `.restorePod()`/`.listHostedPods()`; `MeshctlHostsTool` calls
|
|
127
|
+
* `orchestrator.listPodHosts()`. Like compute/expose, none of these five are
|
|
128
|
+
* part of `mesh-orchestrator.mjs`'s `RISKY_ACTIONS` wire protocol (still
|
|
129
|
+
* exactly `['exec', 'deploy', 'drain']`), so there is no `api.spawnPod`/etc.
|
|
130
|
+
* gated equivalent to prefer -- they are wired straight to the raw
|
|
131
|
+
* instance's own methods here regardless of which input shape is supplied.
|
|
132
|
+
*
|
|
133
|
+
* Unlike compute/expose, this is NOT a gating gap: `spawnPod()`/
|
|
134
|
+
* `snapshotPod()`/`restorePod()`/`listHostedPods()` dispatch over `pod-host-
|
|
135
|
+
* service.mjs`'s OWN mesh protocol (`createPodHostClient()` -> a real
|
|
136
|
+
* `sendTo()` round trip to the target host), and that service gates every
|
|
137
|
+
* verb itself via `ctx.registry.checkAccess(pubKey, resource, verb)` on the
|
|
138
|
+
* HOST side -- the real authorization path for hosted pods, independent of
|
|
139
|
+
* `mesh-orchestrator.mjs`'s own `RISKY_ACTIONS` gate entirely. An EACCES
|
|
140
|
+
* from a refused host surfaces through these tools exactly as it does
|
|
141
|
+
* calling `spawnPod()` directly (see `orchestrator.mjs`'s
|
|
142
|
+
* `formatPodHostError()`); nothing here re-adds or bypasses that gate.
|
|
143
|
+
* `meshctl_hosts` is the one read with no gate anywhere, by design -- it
|
|
144
|
+
* reads `describe()`-derived runtime-registry peers, and `describe()`
|
|
145
|
+
* itself is deliberately ungated (`pod-host-service.mjs`'s module doc
|
|
146
|
+
* comment: a peer must be able to find a host before asking to be granted
|
|
147
|
+
* anything on it).
|
|
148
|
+
*
|
|
149
|
+
* ---------------------------------------------------------------------------
|
|
150
|
+
* `meshctl_supervise`/`meshctl_supervised` -- issue #185 item 6, SAME
|
|
151
|
+
* REASONING AS THE FIVE ABOVE, ONE LEVEL UP
|
|
152
|
+
*
|
|
153
|
+
* `MeshctlSuperviseTool`/`MeshctlSupervisedTool` call
|
|
154
|
+
* `orchestrator.getSupervisor()` and then the returned `PodSupervisor`'s own
|
|
155
|
+
* `supervise()`/`list()` (`pod-supervisor.mjs`). `getSupervisor()` is
|
|
156
|
+
* ALWAYS wired straight to the raw instance here too -- not part of
|
|
157
|
+
* `RISKY_ACTIONS`, same as `spawnPod()` and friends. The gate a restart
|
|
158
|
+
* actually goes through is still `pod-host-service.mjs`'s `checkAccess()`,
|
|
159
|
+
* because `PodSupervisor#supervise()`/its restart path call
|
|
160
|
+
* `orchestrator.spawnPod()` internally -- "a restart is a new spawn request
|
|
161
|
+
* the host may refuse" (`pod-supervisor.mjs`'s own module doc comment)
|
|
162
|
+
* means there is nothing new to gate here either.
|
|
163
|
+
*
|
|
164
|
+
* ---------------------------------------------------------------------------
|
|
115
165
|
* No browser-only imports at module level.
|
|
116
166
|
*/
|
|
117
167
|
|
|
@@ -179,6 +229,21 @@ function buildToolFacade(orchestrator) {
|
|
|
179
229
|
// instance either way.
|
|
180
230
|
runComputeTask: (...args) => raw.runComputeTask(...args),
|
|
181
231
|
exposePod: (...args) => raw.exposePod(...args),
|
|
232
|
+
// meshctl_spawn/meshctl_snapshot/meshctl_restore/meshctl_hosted_pods/
|
|
233
|
+
// meshctl_hosts (issue #185 §8a item 4) -- gated by pod-host-
|
|
234
|
+
// service.mjs's own checkAccess() on the host side, not by this
|
|
235
|
+
// service's RISKY_ACTIONS; straight to the raw instance either way
|
|
236
|
+
// (see module doc comment's dedicated section for these five).
|
|
237
|
+
spawnPod: (...args) => raw.spawnPod(...args),
|
|
238
|
+
snapshotPod: (...args) => raw.snapshotPod(...args),
|
|
239
|
+
restorePod: (...args) => raw.restorePod(...args),
|
|
240
|
+
listHostedPods: (...args) => raw.listHostedPods(...args),
|
|
241
|
+
listPodHosts: (...args) => raw.listPodHosts(...args),
|
|
242
|
+
// meshctl_supervise/meshctl_supervised (issue #185 item 6) -- see the
|
|
243
|
+
// dedicated section above.
|
|
244
|
+
getSupervisor: (...args) => raw.getSupervisor(...args),
|
|
245
|
+
// MeshctlSpawnTool's auto host selection reads this too.
|
|
246
|
+
listComputeCandidates: (...args) => raw.listComputeCandidates(...args),
|
|
182
247
|
// MeshctlExposeTool reads `this.#orchestrator.peerNode` directly for
|
|
183
248
|
// its own default-podId fallback.
|
|
184
249
|
get peerNode() { return raw.peerNode },
|
|
@@ -200,11 +265,12 @@ function buildToolFacade(orchestrator) {
|
|
|
200
265
|
}
|
|
201
266
|
|
|
202
267
|
/**
|
|
203
|
-
* Construct all
|
|
268
|
+
* Construct all 15 `Meshctl*Tool` instances (`orchestrator.mjs`'s own
|
|
204
269
|
* `createMeshctlTools()`) against `orchestrator` and register each into
|
|
205
270
|
* `registry`. See module doc comment for the full design writeup (the two
|
|
206
|
-
* accepted `orchestrator` shapes, the exec/deploy/drain gating decision,
|
|
207
|
-
*
|
|
271
|
+
* accepted `orchestrator` shapes, the exec/deploy/drain gating decision, the
|
|
272
|
+
* compute/expose "not wired by Phase 3 at all" note, and the hosted-pods
|
|
273
|
+
* control surface's own gating section, including the supervisor pair).
|
|
208
274
|
*
|
|
209
275
|
* @param {import('./compat.mjs').BrowserToolRegistry} registry - REQUIRED.
|
|
210
276
|
* Duck-typed (`.register` a function), matching this package's established
|
|
@@ -213,9 +279,10 @@ function buildToolFacade(orchestrator) {
|
|
|
213
279
|
* @param {object} orchestrator - REQUIRED. Either `mesh-orchestrator.mjs`'s
|
|
214
280
|
* service `api` (recommended -- e.g. `node.orchestrator.api`) or a raw
|
|
215
281
|
* `MeshOrchestrator` instance -- see module doc comment.
|
|
216
|
-
* @returns {import('./orchestrator.mjs').BrowserTool[]} the
|
|
217
|
-
* instances, in `createMeshctlTools()`'s own order (pods, status,
|
|
218
|
-
* deploy, top, compute, expose, drain
|
|
282
|
+
* @returns {import('./orchestrator.mjs').BrowserTool[]} the 15 registered
|
|
283
|
+
* tool instances, in `createMeshctlTools()`'s own order (pods, status,
|
|
284
|
+
* exec, deploy, top, compute, expose, drain, spawn, snapshot, restore,
|
|
285
|
+
* hosted_pods, hosts, supervise, supervised).
|
|
219
286
|
*/
|
|
220
287
|
export function registerOrchestratorTools(registry, orchestrator) {
|
|
221
288
|
if (!registry || typeof registry.register !== 'function') {
|
|
@@ -235,11 +302,11 @@ export function registerOrchestratorTools(registry, orchestrator) {
|
|
|
235
302
|
|
|
236
303
|
/**
|
|
237
304
|
* Convenience factory: build a brand-new `BrowserToolRegistry` and
|
|
238
|
-
* pre-populate it with all
|
|
305
|
+
* pre-populate it with all 15 `Meshctl*Tool`s via `registerOrchestratorTools()`.
|
|
239
306
|
* Equivalent to `registerOrchestratorTools(new BrowserToolRegistry(), orchestrator)`,
|
|
240
307
|
* for a caller who doesn't already have a registry of their own to reuse (see
|
|
241
308
|
* `registerOrchestratorTools()` directly if you do -- e.g. to add
|
|
242
|
-
* non-orchestrator tools into the same registry alongside these
|
|
309
|
+
* non-orchestrator tools into the same registry alongside these 15).
|
|
243
310
|
*
|
|
244
311
|
* @param {object} orchestrator - See `registerOrchestratorTools()`.
|
|
245
312
|
* @returns {import('./compat.mjs').BrowserToolRegistry}
|
|
@@ -72,7 +72,7 @@
|
|
|
72
72
|
* arbitrary content deployed to this node's filesystem-adjacent skill
|
|
73
73
|
* store, and forcibly disconnecting/migrating this node's own workload,
|
|
74
74
|
* respectively) -- exactly `orchestrator.mjs`'s own header comment's
|
|
75
|
-
* "
|
|
75
|
+
* "thirteen `meshctl`-CLI-shaped agent tools", the three whose `BrowserTool`
|
|
76
76
|
* subclasses (`MeshctlExecTool`/`MeshctlDeployTool`/`MeshctlDrainTool`)
|
|
77
77
|
* already declare `get permission() { return 'network' }` (vs. `'read'` for
|
|
78
78
|
* the pods/status/top query tools). Before honoring an inbound
|
|
@@ -51,6 +51,7 @@
|
|
|
51
51
|
*/
|
|
52
52
|
|
|
53
53
|
import { createLazyRequire } from './internal/lazy-node-require.mjs'
|
|
54
|
+
import { decodeWireData } from './internal/wire-envelope.mjs'
|
|
54
55
|
|
|
55
56
|
/** Default `envelope.type` used to route relay payloads on the shared `onIncomingData()` bus. */
|
|
56
57
|
const DEFAULT_ENVELOPE_TYPE = 'mesh-relay'
|
|
@@ -222,6 +223,7 @@ function resolveMeshRelayBackendClass() {
|
|
|
222
223
|
|
|
223
224
|
/** @param {*} data */
|
|
224
225
|
async #handleIncoming(data) {
|
|
226
|
+
data = decodeWireData(data)
|
|
225
227
|
if (!data || typeof data !== 'object' || data.type !== this.#envelopeType) return
|
|
226
228
|
const { op, connId } = data
|
|
227
229
|
if (!connId || typeof connId !== 'string') return
|
package/src/mesh-relay-host.mjs
CHANGED
|
@@ -44,6 +44,8 @@
|
|
|
44
44
|
*/
|
|
45
45
|
|
|
46
46
|
/** Default `envelope.type` used to route relay payloads on the shared `onIncomingData()` bus. */
|
|
47
|
+
import { decodeWireData } from './internal/wire-envelope.mjs'
|
|
48
|
+
|
|
47
49
|
const DEFAULT_ENVELOPE_TYPE = 'mesh-relay'
|
|
48
50
|
|
|
49
51
|
// ---------------------------------------------------------------------------
|
|
@@ -193,6 +195,7 @@ export class MeshRelayHost {
|
|
|
193
195
|
* @param {*} data
|
|
194
196
|
*/
|
|
195
197
|
async #handleIncoming(fromPubKey, data) {
|
|
198
|
+
data = decodeWireData(data)
|
|
196
199
|
if (!data || typeof data !== 'object' || data.type !== this.#envelopeType) return
|
|
197
200
|
const { op, connId } = data
|
|
198
201
|
if (!connId || typeof connId !== 'string') return
|
package/src/mesh-service.mjs
CHANGED
|
@@ -27,10 +27,17 @@
|
|
|
27
27
|
* invoking `callback(pubKey, envelope)` only for envelopes whose `.type`
|
|
28
28
|
* matches `types` (a single string or an array of strings). Returns an
|
|
29
29
|
* unsubscribe function, exactly like the underlying `onIncomingData()`.
|
|
30
|
-
*
|
|
30
|
+
* A real transport carries an envelope as JSON text, so a string that
|
|
31
|
+
* is a JSON object/array is parsed before the `type` check -- `callback`
|
|
32
|
+
* always receives the parsed envelope object.
|
|
33
|
+
* - `ctx.sendTo(pubKey, type, payload, { channel })` -- wraps `peerNode.sendTo()`,
|
|
31
34
|
* merging `{ type, ...payload }` into the envelope sent, matching the
|
|
32
35
|
* `{ type: envelopeType, ...payload }` shape both `mesh-sync.mjs` and
|
|
33
|
-
* `mesh-relay-host.mjs` already send.
|
|
36
|
+
* `mesh-relay-host.mjs` already send. The optional fourth argument
|
|
37
|
+
* `{ channel: 'control'|'bulk' }` picks the transport's data channel
|
|
38
|
+
* (default `'control'`); use `'bulk'` for large payloads such as file
|
|
39
|
+
* chunks so they cannot queue in front of control traffic. Transports
|
|
40
|
+
* without a bulk lane ignore it.
|
|
34
41
|
* - `ctx.registry` -- the node's `PeerRegistry`, for `checkAccess()`.
|
|
35
42
|
* - `ctx.peerNode` -- the raw `PeerNode`, for anything not covered above.
|
|
36
43
|
* - `ctx.network` -- the `VirtualNetwork` passed to `attachService()`, if
|
|
@@ -126,6 +133,8 @@
|
|
|
126
133
|
* No browser-only imports at module level.
|
|
127
134
|
*/
|
|
128
135
|
|
|
136
|
+
import { decodeWireData } from './internal/wire-envelope.mjs'
|
|
137
|
+
|
|
129
138
|
/** Default scheme a descriptor's `createBackend` backend is registered under, if `backendScheme` is omitted. */
|
|
130
139
|
const DEFAULT_BACKEND_SCHEME = 'svc'
|
|
131
140
|
|
|
@@ -226,8 +235,12 @@ function createEventBus() {
|
|
|
226
235
|
* @property {(types: string|string[], callback: (pubKey: string, envelope: object) => void) => (() => void)} onIncomingData
|
|
227
236
|
* Envelope-type-filtered subscription. `callback` only fires for envelopes
|
|
228
237
|
* whose `.type` is in `types`. Returns an unsubscribe function.
|
|
229
|
-
* @property {(pubKey: string, type: string, payload?: object) => Promise<void>} sendTo
|
|
238
|
+
* @property {(pubKey: string, type: string, payload?: object, opts?: { channel?: 'control'|'bulk' }) => Promise<void>} sendTo
|
|
230
239
|
* Sends `{ type, ...payload }` to `pubKey` via `peerNode.sendTo()`.
|
|
240
|
+
* `opts.channel` picks the transport's data channel: `'control'` (the
|
|
241
|
+
* default) or `'bulk'` (the second, unordered channel meant for large
|
|
242
|
+
* payloads such as file chunks, so they cannot queue in front of control
|
|
243
|
+
* traffic). A transport with no bulk lane ignores it. See `PeerNode.sendTo()`.
|
|
231
244
|
* @property {(event: string, data?: object) => void} emit
|
|
232
245
|
* Publishes a curated, meaningful state-transition event for anything
|
|
233
246
|
* subscribed via `attachService()`'s returned handle (`handle.on()`/
|
|
@@ -286,7 +299,12 @@ function createServiceContext({ peerNode, network, eventBus }) {
|
|
|
286
299
|
|
|
287
300
|
onIncomingData(types, callback) {
|
|
288
301
|
const typeSet = new Set(Array.isArray(types) ? types : [types])
|
|
289
|
-
return peerNode.onIncomingData((pubKey,
|
|
302
|
+
return peerNode.onIncomingData((pubKey, rawData) => {
|
|
303
|
+
// A real transport (RTCDataChannel, WebSocket) can only carry
|
|
304
|
+
// strings/binary, so an envelope may arrive as its JSON text --
|
|
305
|
+
// parse it before the `type` check. Already-parsed objects (every
|
|
306
|
+
// in-process node, and transports that parse for us) pass through.
|
|
307
|
+
const data = decodeWireData(rawData)
|
|
290
308
|
if (!data || typeof data !== 'object' || !typeSet.has(data.type)) return
|
|
291
309
|
// `peerNode.onIncomingData()`'s own dispatch loop only catches a
|
|
292
310
|
// SYNCHRONOUS throw from this callback. Every real handler in this
|
|
@@ -308,8 +326,12 @@ function createServiceContext({ peerNode, network, eventBus }) {
|
|
|
308
326
|
})
|
|
309
327
|
},
|
|
310
328
|
|
|
311
|
-
async sendTo(pubKey, type, payload = {}) {
|
|
312
|
-
|
|
329
|
+
async sendTo(pubKey, type, payload = {}, { channel } = {}) {
|
|
330
|
+
// `channel` is forwarded only when the caller picked one, so a node
|
|
331
|
+
// (or duck-typed test double) that predates the option sees exactly
|
|
332
|
+
// the two-argument call it always did.
|
|
333
|
+
if (channel === undefined) await peerNode.sendTo(pubKey, { type, ...payload })
|
|
334
|
+
else await peerNode.sendTo(pubKey, { type, ...payload }, { channel })
|
|
313
335
|
},
|
|
314
336
|
|
|
315
337
|
emit(event, data) {
|
package/src/mesh-sync.mjs
CHANGED
|
@@ -26,6 +26,7 @@
|
|
|
26
26
|
*/
|
|
27
27
|
|
|
28
28
|
import { MeshSyncEngine, IndexedDBSyncStorage } from '@johnhenry/browsermesh-sync'
|
|
29
|
+
import { decodeWireData } from './internal/wire-envelope.mjs'
|
|
29
30
|
|
|
30
31
|
/** Default `envelope.type` used to route sync payloads on the shared `onIncomingData()` bus. */
|
|
31
32
|
const DEFAULT_ENVELOPE_TYPE = 'mesh-sync'
|
|
@@ -169,6 +170,7 @@ export class MeshSyncBinding {
|
|
|
169
170
|
* @param {*} data
|
|
170
171
|
*/
|
|
171
172
|
#handleIncoming(pubKey, data) {
|
|
173
|
+
data = decodeWireData(data)
|
|
172
174
|
if (!data || typeof data !== 'object' || data.type !== this.#envelopeType) return
|
|
173
175
|
const { docId, docType, payload } = data
|
|
174
176
|
if (!docId || !payload) return
|
package/src/mesh-torrent.mjs
CHANGED
|
@@ -609,9 +609,9 @@ export function createTorrentService({
|
|
|
609
609
|
await bucket.take(bytes.length)
|
|
610
610
|
|
|
611
611
|
let sent = true
|
|
612
|
-
// Piece data
|
|
613
|
-
//
|
|
614
|
-
//
|
|
612
|
+
// Piece data rides the bulk lane so it cannot starve control traffic
|
|
613
|
+
// (`ctx.sendTo(..., { channel: 'bulk' })`, #198). A transport with no bulk
|
|
614
|
+
// lane ignores the option.
|
|
615
615
|
await respond({ data: toBase64(bytes) }, { channel: 'bulk' }).catch((err) => {
|
|
616
616
|
sent = false
|
|
617
617
|
log('mesh-torrent:chunk-response-send-failed', { to: fromPubKey, cid: msg.cid, error: err?.message || String(err) })
|
package/src/mesh-websocket.mjs
CHANGED
|
@@ -229,6 +229,7 @@
|
|
|
229
229
|
*/
|
|
230
230
|
|
|
231
231
|
import { createLazyRequire } from './internal/lazy-node-require.mjs'
|
|
232
|
+
import { decodeWireData } from './internal/wire-envelope.mjs'
|
|
232
233
|
|
|
233
234
|
/** Default `envelope.type` used to route mesh-websocket payloads on the shared `onIncomingData()` bus. */
|
|
234
235
|
const DEFAULT_ENVELOPE_TYPE = 'mesh-websocket'
|
|
@@ -434,7 +435,8 @@ export class BrowserMeshWebSocket {
|
|
|
434
435
|
// -----------------------------------------------------------------------
|
|
435
436
|
|
|
436
437
|
#subscribe() {
|
|
437
|
-
this.#unsubscribe = this.#peerNode.onIncomingData((fromPubKey,
|
|
438
|
+
this.#unsubscribe = this.#peerNode.onIncomingData((fromPubKey, rawData) => {
|
|
439
|
+
const data = decodeWireData(rawData)
|
|
438
440
|
if (!data || typeof data !== 'object') return
|
|
439
441
|
if (data.type !== this.#envelopeType || data.connectionId !== this.#connectionId) return
|
|
440
442
|
this.#handleEnvelope(fromPubKey, data)
|