@johnhenry/browsermesh-apps 0.16.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 +268 -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/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-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 +3 -1
- 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
package/src/mesh-hardening.mjs
CHANGED
|
@@ -254,5 +254,12 @@ export async function createHardenedNegotiator({ negotiator, retryOptions, onLog
|
|
|
254
254
|
return instrumentTransport(transport, transportMetrics)
|
|
255
255
|
}
|
|
256
256
|
|
|
257
|
-
return {
|
|
257
|
+
return {
|
|
258
|
+
negotiate,
|
|
259
|
+
metrics,
|
|
260
|
+
failovers,
|
|
261
|
+
retries,
|
|
262
|
+
/** The wrapped negotiator's `require` (see `MeshTransportNegotiator`), so PeerNode still fails closed. */
|
|
263
|
+
get required() { return negotiator.required ?? null },
|
|
264
|
+
}
|
|
258
265
|
}
|
|
@@ -52,6 +52,7 @@
|
|
|
52
52
|
|
|
53
53
|
import { createLazyRequire } from './internal/lazy-node-require.mjs'
|
|
54
54
|
import { decodeWireData } from './internal/wire-envelope.mjs'
|
|
55
|
+
import { CreditReceiver, CreditSender, resolveFlowOptions } from './internal/relay-flow.mjs'
|
|
55
56
|
|
|
56
57
|
/** Default `envelope.type` used to route relay payloads on the shared `onIncomingData()` bus. */
|
|
57
58
|
const DEFAULT_ENVELOPE_TYPE = 'mesh-relay'
|
|
@@ -127,9 +128,12 @@ function resolveMeshRelayBackendClass() {
|
|
|
127
128
|
/** @type {Map<string, {resolve: Function, reject: Function, service: string}>} connId -> pending connect */
|
|
128
129
|
#pending = new Map()
|
|
129
130
|
|
|
130
|
-
/** @type {Map<string, import('@johnhenry/browsermesh-netway').StreamSocket>} connId -> relay-side
|
|
131
|
+
/** @type {Map<string, {socket: import('@johnhenry/browsermesh-netway').StreamSocket, tx: CreditSender, rx: CreditReceiver, closed: boolean}>} connId -> relay-side connection */
|
|
131
132
|
#connections = new Map()
|
|
132
133
|
|
|
134
|
+
/** @type {Readonly<import('./internal/relay-flow.mjs').RELAY_FLOW_DEFAULTS>} */
|
|
135
|
+
#flow
|
|
136
|
+
|
|
133
137
|
/** @type {(() => void)|null} */
|
|
134
138
|
#unsubscribeIncoming = null
|
|
135
139
|
|
|
@@ -140,14 +144,19 @@ function resolveMeshRelayBackendClass() {
|
|
|
140
144
|
* @param {string} [opts.envelopeType='mesh-relay']
|
|
141
145
|
* @param {number} [opts.connectTimeoutMs=15000] - `0` disables the timeout.
|
|
142
146
|
* @param {Function} [opts.onLog]
|
|
147
|
+
* @param {number} [opts.highWaterBytes=262144] - Receive window per connection; the host pauses at it.
|
|
148
|
+
* @param {number} [opts.lowWaterBytes=65536]
|
|
149
|
+
* @param {number} [opts.maxBufferedBytesPerConnection=1048576] - Hard cap on bytes received and not yet consumed.
|
|
150
|
+
* @param {number} [opts.maxFrameBytes=65536]
|
|
143
151
|
*/
|
|
144
|
-
constructor({ node, relayPeerPubKey, envelopeType, connectTimeoutMs, onLog } = {}) {
|
|
152
|
+
constructor({ node, relayPeerPubKey, envelopeType, connectTimeoutMs, onLog, ...flow } = {}) {
|
|
145
153
|
super()
|
|
146
154
|
if (!node) throw new Error('node is required')
|
|
147
155
|
if (!relayPeerPubKey || typeof relayPeerPubKey !== 'string') {
|
|
148
156
|
throw new Error('relayPeerPubKey is required')
|
|
149
157
|
}
|
|
150
158
|
|
|
159
|
+
this.#flow = resolveFlowOptions(flow)
|
|
151
160
|
this.#node = node
|
|
152
161
|
this.#relayPeerPubKey = relayPeerPubKey
|
|
153
162
|
this.#envelopeType = envelopeType || DEFAULT_ENVELOPE_TYPE
|
|
@@ -197,7 +206,7 @@ function resolveMeshRelayBackendClass() {
|
|
|
197
206
|
this.#pending.set(connId, { resolve, reject, service: host })
|
|
198
207
|
})
|
|
199
208
|
|
|
200
|
-
await this.#send({ op: 'connect', connId, service: host })
|
|
209
|
+
await this.#send({ op: 'connect', connId, service: host, window: this.#flow.highWaterBytes })
|
|
201
210
|
|
|
202
211
|
if (!this.#connectTimeoutMs) return pending
|
|
203
212
|
|
|
@@ -234,8 +243,28 @@ function resolveMeshRelayBackendClass() {
|
|
|
234
243
|
this.#pending.delete(connId)
|
|
235
244
|
|
|
236
245
|
const [userSocket, relaySocket] = StreamSocket.createPair()
|
|
237
|
-
|
|
238
|
-
|
|
246
|
+
// The host advertises a window only if it understands credit; an older
|
|
247
|
+
// host is sent no credit frames and is held to the buffer bound only.
|
|
248
|
+
const creditAware = Number.isInteger(data.window) && data.window > 0
|
|
249
|
+
const conn = {
|
|
250
|
+
socket: relaySocket,
|
|
251
|
+
closed: false,
|
|
252
|
+
tx: new CreditSender({
|
|
253
|
+
window: creditAware ? Math.min(data.window, this.#flow.maxBufferedBytesPerConnection) : 0,
|
|
254
|
+
lowWater: this.#flow.lowWaterBytes,
|
|
255
|
+
}),
|
|
256
|
+
rx: new CreditReceiver({
|
|
257
|
+
window: this.#flow.highWaterBytes,
|
|
258
|
+
lowWater: this.#flow.lowWaterBytes,
|
|
259
|
+
maxBuffered: this.#flow.maxBufferedBytesPerConnection,
|
|
260
|
+
creditAware,
|
|
261
|
+
grant: (bytes) => {
|
|
262
|
+
if (!conn.closed) this.#send({ op: 'credit', connId, bytes }).catch(() => {})
|
|
263
|
+
},
|
|
264
|
+
}),
|
|
265
|
+
}
|
|
266
|
+
this.#connections.set(connId, conn)
|
|
267
|
+
this.#pumpToRemote(connId, conn)
|
|
239
268
|
pending.resolve(userSocket)
|
|
240
269
|
return
|
|
241
270
|
}
|
|
@@ -252,23 +281,85 @@ function resolveMeshRelayBackendClass() {
|
|
|
252
281
|
|
|
253
282
|
// 'data' / 'close' only act on connIds this backend itself originated
|
|
254
283
|
// and that are still open -- unrecognized connIds are ignored.
|
|
255
|
-
const
|
|
256
|
-
if (!
|
|
284
|
+
const conn = this.#connections.get(connId)
|
|
285
|
+
if (!conn) return
|
|
257
286
|
|
|
258
287
|
if (op === 'data') {
|
|
259
288
|
if (typeof data.data !== 'string') return
|
|
289
|
+
const approx = Math.floor((data.data.length * 3) / 4)
|
|
290
|
+
if (approx > this.#flow.maxBufferedBytesPerConnection
|
|
291
|
+
|| (conn.rx.creditAware && approx > this.#flow.maxFrameBytes + 3)) {
|
|
292
|
+
this.#closeConnection(connId, conn, { notifyHost: true, reason: 'buffer limit exceeded' })
|
|
293
|
+
return
|
|
294
|
+
}
|
|
295
|
+
const bytes = fromBase64(data.data)
|
|
296
|
+
const verdict = conn.rx.receive(bytes.length)
|
|
297
|
+
if (verdict !== 'ok') {
|
|
298
|
+
this.#closeConnection(connId, conn, {
|
|
299
|
+
notifyHost: true,
|
|
300
|
+
reason: verdict === 'violation' ? 'flow control violation' : 'buffer limit exceeded',
|
|
301
|
+
})
|
|
302
|
+
return
|
|
303
|
+
}
|
|
260
304
|
try {
|
|
261
|
-
await
|
|
305
|
+
await conn.socket.write(bytes)
|
|
262
306
|
} catch (err) {
|
|
263
|
-
this.#onLog('mesh-relay-backend:data:write-failed', { connId, error: err?.message || String(err) })
|
|
307
|
+
if (!conn.closed) this.#onLog('mesh-relay-backend:data:write-failed', { connId, error: err?.message || String(err) })
|
|
308
|
+
} finally {
|
|
309
|
+
conn.rx.consumed(bytes.length)
|
|
310
|
+
}
|
|
311
|
+
return
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
if (op === 'credit') {
|
|
315
|
+
if (conn.tx.enabled && !conn.tx.credit(data.bytes)) {
|
|
316
|
+
this.#closeConnection(connId, conn, { notifyHost: true, reason: 'flow control violation' })
|
|
264
317
|
}
|
|
265
318
|
return
|
|
266
319
|
}
|
|
267
320
|
|
|
268
321
|
if (op === 'close') {
|
|
269
|
-
this.#
|
|
270
|
-
|
|
322
|
+
await this.#closeConnection(connId, conn, {
|
|
323
|
+
notifyHost: false,
|
|
324
|
+
reason: typeof data.reason === 'string' ? data.reason.slice(0, 200) : 'closed by host',
|
|
325
|
+
})
|
|
326
|
+
}
|
|
327
|
+
}
|
|
328
|
+
|
|
329
|
+
/**
|
|
330
|
+
* Close a relayed connection: both endpoints of the local socket pair see
|
|
331
|
+
* end of stream and `closeReason`.
|
|
332
|
+
*
|
|
333
|
+
* @param {string} connId
|
|
334
|
+
* @param {{socket: object, tx: CreditSender, closed: boolean}} conn
|
|
335
|
+
* @param {{notifyHost: boolean, reason: string}} opts
|
|
336
|
+
*/
|
|
337
|
+
async #closeConnection(connId, conn, { notifyHost, reason }) {
|
|
338
|
+
if (conn.closed) return
|
|
339
|
+
conn.closed = true
|
|
340
|
+
this.#connections.delete(connId)
|
|
341
|
+
conn.tx.close()
|
|
342
|
+
try { await conn.socket.close(reason) } catch { /* already closed */ }
|
|
343
|
+
if (notifyHost) await this.#send({ op: 'close', connId, reason }).catch(() => {})
|
|
344
|
+
}
|
|
345
|
+
|
|
346
|
+
/**
|
|
347
|
+
* Counters for bytes held for relayed connections.
|
|
348
|
+
* `bufferedBytes`: received and not yet accepted by the user-facing socket,
|
|
349
|
+
* plus bytes the user wrote that the relay has not yet read (bytes held in
|
|
350
|
+
* the user-facing socket's own receive window are not counted);
|
|
351
|
+
* `inFlightBytes`: sent to the host and not yet credited back.
|
|
352
|
+
*
|
|
353
|
+
* @returns {{ connections: number, bufferedBytes: number, inFlightBytes: number }}
|
|
354
|
+
*/
|
|
355
|
+
stats() {
|
|
356
|
+
let bufferedBytes = 0
|
|
357
|
+
let inFlightBytes = 0
|
|
358
|
+
for (const conn of this.#connections.values()) {
|
|
359
|
+
bufferedBytes += conn.rx.outstanding + (conn.socket.readableLength ?? 0)
|
|
360
|
+
inFlightBytes += conn.tx.inFlight
|
|
271
361
|
}
|
|
362
|
+
return { connections: this.#connections.size, bufferedBytes, inFlightBytes }
|
|
272
363
|
}
|
|
273
364
|
|
|
274
365
|
/**
|
|
@@ -279,20 +370,24 @@ function resolveMeshRelayBackendClass() {
|
|
|
279
370
|
* @param {string} connId
|
|
280
371
|
* @param {import('@johnhenry/browsermesh-netway').StreamSocket} relaySocket
|
|
281
372
|
*/
|
|
282
|
-
#pumpToRemote(connId,
|
|
373
|
+
#pumpToRemote(connId, conn) {
|
|
283
374
|
(async () => {
|
|
284
375
|
try {
|
|
285
|
-
while (
|
|
286
|
-
const chunk = await
|
|
376
|
+
outer: while (!conn.closed) {
|
|
377
|
+
const chunk = await conn.socket.read()
|
|
287
378
|
if (chunk === null) break
|
|
288
|
-
|
|
379
|
+
for (let off = 0; off < chunk.length;) {
|
|
380
|
+
const want = Math.min(chunk.length - off, this.#flow.maxFrameBytes)
|
|
381
|
+
const n = await conn.tx.reserve(want)
|
|
382
|
+
if (n === 0 || conn.closed) break outer
|
|
383
|
+
await this.#send({ op: 'data', connId, data: toBase64(chunk.subarray(off, off + n)) })
|
|
384
|
+
off += n
|
|
385
|
+
}
|
|
289
386
|
}
|
|
290
387
|
} catch (err) {
|
|
291
|
-
this.#onLog('mesh-relay-backend:pump:error', { connId, error: err?.message || String(err) })
|
|
292
|
-
}
|
|
293
|
-
if (this.#connections.delete(connId)) {
|
|
294
|
-
await this.#send({ op: 'close', connId }).catch(() => {})
|
|
388
|
+
if (!conn.closed) this.#onLog('mesh-relay-backend:pump:error', { connId, error: err?.message || String(err) })
|
|
295
389
|
}
|
|
390
|
+
await this.#closeConnection(connId, conn, { notifyHost: true, reason: 'closed by client' })
|
|
296
391
|
})()
|
|
297
392
|
}
|
|
298
393
|
|
|
@@ -316,9 +411,8 @@ function resolveMeshRelayBackendClass() {
|
|
|
316
411
|
this.#unsubscribeIncoming()
|
|
317
412
|
this.#unsubscribeIncoming = null
|
|
318
413
|
}
|
|
319
|
-
for (const [connId,
|
|
320
|
-
this.#
|
|
321
|
-
try { await socket.close() } catch { /* already closed */ }
|
|
414
|
+
for (const [connId, conn] of [...this.#connections]) {
|
|
415
|
+
await this.#closeConnection(connId, conn, { notifyHost: false, reason: 'relay backend closed' })
|
|
322
416
|
}
|
|
323
417
|
for (const [connId, pending] of [...this.#pending]) {
|
|
324
418
|
this.#pending.delete(connId)
|