@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,280 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* request-target.mjs -- shared rules for proxying a request on behalf of a
|
|
3
|
+
* remote peer to a locally configured origin.
|
|
4
|
+
*
|
|
5
|
+
* Everything a peer supplies (path, method, headers, body) is untrusted. The
|
|
6
|
+
* target of the upstream request is built from parsed parts: the configured
|
|
7
|
+
* origin is the base, and the peer contributes a path and query only. After
|
|
8
|
+
* resolution the origin of the result must equal the configured origin
|
|
9
|
+
* exactly, or the request is refused before anything is sent.
|
|
10
|
+
*
|
|
11
|
+
* No browser-only imports at module level.
|
|
12
|
+
*/
|
|
13
|
+
|
|
14
|
+
/** Defaults for the limits applied to proxied requests. All can be overridden per `ServerSharing`. */
|
|
15
|
+
export const PROXY_LIMITS_DEFAULTS = Object.freeze({
|
|
16
|
+
maxPathLength: 2048,
|
|
17
|
+
maxRequestBodyBytes: 1024 * 1024,
|
|
18
|
+
maxResponseBytes: 8 * 1024 * 1024,
|
|
19
|
+
maxHeaderCount: 32,
|
|
20
|
+
maxHeaderBytes: 8 * 1024,
|
|
21
|
+
requestTimeoutMs: 30_000,
|
|
22
|
+
})
|
|
23
|
+
|
|
24
|
+
/** Methods a peer may use unless the sharer narrows the list. */
|
|
25
|
+
export const DEFAULT_ALLOWED_METHODS = Object.freeze(['GET', 'HEAD', 'POST', 'PUT', 'PATCH', 'DELETE', 'OPTIONS'])
|
|
26
|
+
|
|
27
|
+
/** Request headers a peer may set unless the sharer widens the list. */
|
|
28
|
+
export const DEFAULT_ALLOWED_REQUEST_HEADERS = Object.freeze([
|
|
29
|
+
'accept',
|
|
30
|
+
'accept-language',
|
|
31
|
+
'cache-control',
|
|
32
|
+
'content-type',
|
|
33
|
+
'if-match',
|
|
34
|
+
'if-modified-since',
|
|
35
|
+
'if-none-match',
|
|
36
|
+
'if-unmodified-since',
|
|
37
|
+
'range',
|
|
38
|
+
])
|
|
39
|
+
|
|
40
|
+
/** Request headers that are never taken from a peer, whatever the allow-list says. */
|
|
41
|
+
const NEVER_FROM_PEER = new Set([
|
|
42
|
+
'host', 'connection', 'keep-alive', 'proxy-authenticate', 'proxy-authorization', 'proxy-connection',
|
|
43
|
+
'te', 'trailer', 'transfer-encoding', 'upgrade', 'content-length', 'expect',
|
|
44
|
+
'cookie', 'authorization', 'forwarded',
|
|
45
|
+
])
|
|
46
|
+
|
|
47
|
+
/** Response headers that are not passed back to the peer. */
|
|
48
|
+
const DROP_RESPONSE_HEADERS = new Set([
|
|
49
|
+
'connection', 'keep-alive', 'proxy-authenticate', 'proxy-connection', 'te', 'trailer',
|
|
50
|
+
'transfer-encoding', 'upgrade', 'content-encoding', 'content-length', 'set-cookie', 'set-cookie2',
|
|
51
|
+
])
|
|
52
|
+
|
|
53
|
+
const TOKEN = /^[!#$%&'*+.^_`|~0-9A-Za-z-]+$/
|
|
54
|
+
// eslint-disable-next-line no-control-regex
|
|
55
|
+
const BAD_HEADER_VALUE = /[\u0000-\u0008\u000a-\u001f\u007f]/
|
|
56
|
+
// eslint-disable-next-line no-control-regex
|
|
57
|
+
const BAD_PATH_CHARS = /[\u0000- \u007f-\u009f\\]/
|
|
58
|
+
|
|
59
|
+
const encoder = new TextEncoder()
|
|
60
|
+
|
|
61
|
+
/** @param {string} s @returns {number} UTF-8 byte length */
|
|
62
|
+
function byteLength(s) {
|
|
63
|
+
return encoder.encode(s).byteLength
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* Normalise the configured parts of a share into an origin string, throwing
|
|
68
|
+
* if they do not form a plain `scheme://host:port`.
|
|
69
|
+
*
|
|
70
|
+
* @param {string} protocol
|
|
71
|
+
* @param {string} hostname
|
|
72
|
+
* @param {number} port
|
|
73
|
+
* @returns {string} the URL origin
|
|
74
|
+
*/
|
|
75
|
+
export function configuredOrigin(protocol, hostname, port) {
|
|
76
|
+
if (protocol !== 'http' && protocol !== 'https') {
|
|
77
|
+
throw new Error('protocol must be http or https')
|
|
78
|
+
}
|
|
79
|
+
if (!Number.isInteger(port) || port < 1 || port > 65535) {
|
|
80
|
+
throw new Error('port must be an integer from 1 to 65535')
|
|
81
|
+
}
|
|
82
|
+
if (typeof hostname !== 'string' || hostname === '' || /[\s/\\@?#[\]]/.test(hostname.replace(/^\[.*\]$/, 'ipv6'))) {
|
|
83
|
+
throw new Error('hostname must be a plain host name or address')
|
|
84
|
+
}
|
|
85
|
+
const host = hostname.includes(':') && !hostname.startsWith('[') ? `[${hostname}]` : hostname
|
|
86
|
+
let url
|
|
87
|
+
try {
|
|
88
|
+
url = new URL(`${protocol}://${host}:${port}`)
|
|
89
|
+
} catch {
|
|
90
|
+
throw new Error('hostname must be a plain host name or address')
|
|
91
|
+
}
|
|
92
|
+
if (url.username || url.password || url.pathname !== '/' || url.search || url.hash || !url.hostname) {
|
|
93
|
+
throw new Error('hostname must be a plain host name or address')
|
|
94
|
+
}
|
|
95
|
+
return url.origin
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/**
|
|
99
|
+
* Build the target URL of a proxied request from a path a peer supplied.
|
|
100
|
+
*
|
|
101
|
+
* Accepted: a string that begins with a single `/` and contains no scheme,
|
|
102
|
+
* authority, backslash, whitespace, control character or fragment. After
|
|
103
|
+
* resolution against `origin` the result must have exactly that origin.
|
|
104
|
+
*
|
|
105
|
+
* @param {string} origin - an origin as returned by `configuredOrigin()`
|
|
106
|
+
* @param {*} rawPath - undefined, null or '' mean '/'
|
|
107
|
+
* @param {{ maxPathLength?: number }} [opts]
|
|
108
|
+
* @returns {{ ok: true, url: URL } | { ok: false, reason: string }}
|
|
109
|
+
*/
|
|
110
|
+
export function resolveTarget(origin, rawPath, { maxPathLength = PROXY_LIMITS_DEFAULTS.maxPathLength } = {}) {
|
|
111
|
+
const path = rawPath === undefined || rawPath === null || rawPath === '' ? '/' : rawPath
|
|
112
|
+
if (typeof path !== 'string') return { ok: false, reason: 'path must be a string' }
|
|
113
|
+
if (path.length > maxPathLength) return { ok: false, reason: 'path too long' }
|
|
114
|
+
if (BAD_PATH_CHARS.test(path)) return { ok: false, reason: 'path contains a forbidden character' }
|
|
115
|
+
if (path[0] !== '/') return { ok: false, reason: 'path must begin with /' }
|
|
116
|
+
if (path[1] === '/') return { ok: false, reason: 'path must not begin with //' }
|
|
117
|
+
if (path.includes('#')) return { ok: false, reason: 'path must not contain a fragment' }
|
|
118
|
+
let url
|
|
119
|
+
try {
|
|
120
|
+
url = new URL(path, origin)
|
|
121
|
+
} catch {
|
|
122
|
+
return { ok: false, reason: 'path is not a valid request target' }
|
|
123
|
+
}
|
|
124
|
+
if (url.origin !== origin || url.username || url.password) {
|
|
125
|
+
return { ok: false, reason: 'target is not on the configured origin' }
|
|
126
|
+
}
|
|
127
|
+
return { ok: true, url }
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
/**
|
|
131
|
+
* Validate a header map's shape: string names that are HTTP tokens, string
|
|
132
|
+
* values without line breaks or control characters, bounded count and size.
|
|
133
|
+
*
|
|
134
|
+
* @param {*} headers
|
|
135
|
+
* @param {{ maxHeaderCount: number, maxHeaderBytes: number }} limits
|
|
136
|
+
* @returns {{ ok: true, entries: Array<[string, string]> } | { ok: false, reason: string }}
|
|
137
|
+
*/
|
|
138
|
+
export function checkHeaderShape(headers, { maxHeaderCount, maxHeaderBytes }) {
|
|
139
|
+
if (headers === undefined || headers === null) return { ok: true, entries: [] }
|
|
140
|
+
if (typeof headers !== 'object' || Array.isArray(headers)) return { ok: false, reason: 'headers must be an object' }
|
|
141
|
+
const entries = Object.entries(headers)
|
|
142
|
+
if (entries.length > maxHeaderCount) return { ok: false, reason: 'too many headers' }
|
|
143
|
+
let size = 0
|
|
144
|
+
for (const [name, value] of entries) {
|
|
145
|
+
if (!TOKEN.test(name)) return { ok: false, reason: 'invalid header name' }
|
|
146
|
+
if (typeof value !== 'string' || BAD_HEADER_VALUE.test(value)) return { ok: false, reason: 'invalid header value' }
|
|
147
|
+
size += byteLength(name) + byteLength(value)
|
|
148
|
+
if (size > maxHeaderBytes) return { ok: false, reason: 'headers too large' }
|
|
149
|
+
}
|
|
150
|
+
return { ok: true, entries }
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
/**
|
|
154
|
+
* Select the request headers to forward: only allow-listed names, never a
|
|
155
|
+
* name in the never-from-peer set; then the headers the sharer configured
|
|
156
|
+
* are applied on top and cannot be overridden by the peer.
|
|
157
|
+
*
|
|
158
|
+
* @param {Array<[string, string]>} entries - already shape-checked
|
|
159
|
+
* @param {Set<string>} allowed - lower-case names
|
|
160
|
+
* @param {Record<string, string>} configured - headers set by the sharer
|
|
161
|
+
* @returns {Record<string, string>}
|
|
162
|
+
*/
|
|
163
|
+
export function selectRequestHeaders(entries, allowed, configured = {}) {
|
|
164
|
+
const out = {}
|
|
165
|
+
for (const [name, value] of entries) {
|
|
166
|
+
const lower = name.toLowerCase()
|
|
167
|
+
if (NEVER_FROM_PEER.has(lower) || lower.startsWith('x-forwarded-') || lower.startsWith('proxy-')) continue
|
|
168
|
+
if (!allowed.has(lower)) continue
|
|
169
|
+
out[lower] = value
|
|
170
|
+
}
|
|
171
|
+
for (const [name, value] of Object.entries(configured)) out[name.toLowerCase()] = value
|
|
172
|
+
return out
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
/**
|
|
176
|
+
* Check and normalise a request body. Accepts a string or a Uint8Array no
|
|
177
|
+
* larger than `maxBytes`.
|
|
178
|
+
*
|
|
179
|
+
* @param {*} body
|
|
180
|
+
* @param {number} maxBytes
|
|
181
|
+
* @returns {{ ok: true, body: string|Uint8Array|undefined } | { ok: false, status: number, reason: string }}
|
|
182
|
+
*/
|
|
183
|
+
export function checkRequestBody(body, maxBytes) {
|
|
184
|
+
if (body === undefined || body === null) return { ok: true, body: undefined }
|
|
185
|
+
if (typeof body === 'string') {
|
|
186
|
+
// Every UTF-16 code unit takes at least one UTF-8 byte, so a string
|
|
187
|
+
// longer than the limit is too large without encoding it.
|
|
188
|
+
if (body.length > maxBytes || byteLength(body) > maxBytes) {
|
|
189
|
+
return { ok: false, status: 413, reason: 'request body too large' }
|
|
190
|
+
}
|
|
191
|
+
return { ok: true, body }
|
|
192
|
+
}
|
|
193
|
+
if (body instanceof Uint8Array) {
|
|
194
|
+
if (body.byteLength > maxBytes) return { ok: false, status: 413, reason: 'request body too large' }
|
|
195
|
+
return { ok: true, body }
|
|
196
|
+
}
|
|
197
|
+
return { ok: false, status: 400, reason: 'request body must be a string or bytes' }
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
/**
|
|
201
|
+
* Copy response headers for the peer, leaving out hop-by-hop headers,
|
|
202
|
+
* cookies and encodings that no longer describe the body.
|
|
203
|
+
*
|
|
204
|
+
* @param {*} headers - a Headers, Map or anything with forEach
|
|
205
|
+
* @returns {Record<string, string>}
|
|
206
|
+
*/
|
|
207
|
+
export function copyResponseHeaders(headers) {
|
|
208
|
+
const out = {}
|
|
209
|
+
if (headers && typeof headers.forEach === 'function') {
|
|
210
|
+
headers.forEach((value, key) => {
|
|
211
|
+
const lower = String(key).toLowerCase()
|
|
212
|
+
if (DROP_RESPONSE_HEADERS.has(lower) || lower.startsWith('proxy-')) return
|
|
213
|
+
out[key] = value
|
|
214
|
+
})
|
|
215
|
+
}
|
|
216
|
+
return out
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
/**
|
|
220
|
+
* Read a response body as text, never holding more than `maxBytes`.
|
|
221
|
+
* A body that exceeds the bound is cancelled and reported, never truncated.
|
|
222
|
+
*
|
|
223
|
+
* @param {*} response
|
|
224
|
+
* @param {number} maxBytes
|
|
225
|
+
* @returns {Promise<{ ok: true, body: * } | { ok: false }>}
|
|
226
|
+
*/
|
|
227
|
+
export async function readBoundedBody(response, maxBytes) {
|
|
228
|
+
const declared = Number(response.headers?.get?.('content-length'))
|
|
229
|
+
if (Number.isFinite(declared) && declared > maxBytes) {
|
|
230
|
+
try { await response.body?.cancel?.() } catch { /* already closed */ }
|
|
231
|
+
return { ok: false }
|
|
232
|
+
}
|
|
233
|
+
const stream = response.body
|
|
234
|
+
if (stream && typeof stream.getReader === 'function') {
|
|
235
|
+
const reader = stream.getReader()
|
|
236
|
+
const decoder = new TextDecoder()
|
|
237
|
+
let total = 0
|
|
238
|
+
let text = ''
|
|
239
|
+
for (;;) {
|
|
240
|
+
const { done, value } = await reader.read()
|
|
241
|
+
if (done) break
|
|
242
|
+
total += value.byteLength
|
|
243
|
+
if (total > maxBytes) {
|
|
244
|
+
try { await reader.cancel() } catch { /* already closed */ }
|
|
245
|
+
return { ok: false }
|
|
246
|
+
}
|
|
247
|
+
text += decoder.decode(value, { stream: true })
|
|
248
|
+
}
|
|
249
|
+
text += decoder.decode()
|
|
250
|
+
return { ok: true, body: text }
|
|
251
|
+
}
|
|
252
|
+
if (typeof response.text === 'function') {
|
|
253
|
+
const text = await response.text()
|
|
254
|
+
if (typeof text === 'string' && byteLength(text) > maxBytes) return { ok: false }
|
|
255
|
+
return { ok: true, body: text }
|
|
256
|
+
}
|
|
257
|
+
const body = response.body
|
|
258
|
+
if (typeof body === 'string' && byteLength(body) > maxBytes) return { ok: false }
|
|
259
|
+
return { ok: true, body }
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
/**
|
|
263
|
+
* Whether a response came from somewhere other than `origin` (a fetch that
|
|
264
|
+
* followed a redirect, for example).
|
|
265
|
+
*
|
|
266
|
+
* @param {*} response
|
|
267
|
+
* @param {string} origin
|
|
268
|
+
* @returns {boolean}
|
|
269
|
+
*/
|
|
270
|
+
export function responseLeftOrigin(response, origin) {
|
|
271
|
+
if (response.redirected === true) return true
|
|
272
|
+
if (typeof response.url === 'string' && response.url !== '') {
|
|
273
|
+
try {
|
|
274
|
+
return new URL(response.url).origin !== origin
|
|
275
|
+
} catch {
|
|
276
|
+
return true
|
|
277
|
+
}
|
|
278
|
+
}
|
|
279
|
+
return false
|
|
280
|
+
}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
// An explicit envelope field distinguishes bytes from ordinary JSON objects.
|
|
2
|
+
// This works across JSON-only transports without reserving user object keys.
|
|
3
|
+
const encoder = new TextEncoder()
|
|
4
|
+
const tooLarge = () => new RangeError('mesh-rpc: body exceeds maxBodyBytes')
|
|
5
|
+
const viewOf = value => value instanceof ArrayBuffer ? new Uint8Array(value)
|
|
6
|
+
: ArrayBuffer.isView(value) ? new Uint8Array(value.buffer, value.byteOffset, value.byteLength) : null
|
|
7
|
+
|
|
8
|
+
export function encodeBody(body, limit) {
|
|
9
|
+
const bytes = viewOf(body)
|
|
10
|
+
if (bytes) {
|
|
11
|
+
if (bytes.byteLength > limit) throw tooLarge()
|
|
12
|
+
let text = ''
|
|
13
|
+
for (let i = 0; i < bytes.length; i += 8192) text += String.fromCharCode(...bytes.subarray(i, i + 8192))
|
|
14
|
+
return { body: btoa(text), bodyEncoding: 'base64' }
|
|
15
|
+
}
|
|
16
|
+
const text = JSON.stringify(body)
|
|
17
|
+
if (text !== undefined && encoder.encode(text).byteLength > limit) throw tooLarge()
|
|
18
|
+
return { body }
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export function decodeBody(message, limit) {
|
|
22
|
+
if (message.bodyEncoding === undefined) {
|
|
23
|
+
encodeBody(message.body, limit)
|
|
24
|
+
return message.body
|
|
25
|
+
}
|
|
26
|
+
if (message.bodyEncoding !== 'base64' || typeof message.body !== 'string') throw new TypeError('mesh-rpc: invalid body encoding')
|
|
27
|
+
const text = message.body
|
|
28
|
+
if (text.length > 4 * Math.ceil(limit / 3)) throw tooLarge()
|
|
29
|
+
if (text.length % 4 || !/^[A-Za-z0-9+/]*={0,2}$/.test(text)) throw new TypeError('mesh-rpc: invalid base64 body')
|
|
30
|
+
const binary = atob(text)
|
|
31
|
+
if (binary.length > limit) throw tooLarge()
|
|
32
|
+
return Uint8Array.from(binary, ch => ch.charCodeAt(0))
|
|
33
|
+
}
|
package/src/marketplace-ui.mjs
CHANGED
package/src/mesh-agent-swarm.mjs
CHANGED
|
@@ -33,7 +33,7 @@
|
|
|
33
33
|
* UNCONDITIONALLY -- it never checks whether `assignee` is this node or
|
|
34
34
|
* some other one. In the original, single-node code (a single
|
|
35
35
|
* `AgentSwarmCoordinator` + a single `agentProxy` that happens to be able
|
|
36
|
-
* to `chat()` with several locally-known `podId`s, e.g. several
|
|
36
|
+
* to `chat()` with several locally-known `podId`s, e.g. several `Pod`
|
|
37
37
|
* agents in one process), that's fine: every `assignee` is reachable
|
|
38
38
|
* through the same local `agentProxy`. In a real mesh deployment,
|
|
39
39
|
* `assignee` is frequently a REMOTE peer's podId -- so today's code, as
|
package/src/mesh-bootstrap.mjs
CHANGED
|
@@ -310,6 +310,7 @@ import { PeerNode } from './peer-node.mjs'
|
|
|
310
310
|
import { PeerRegistry } from './peer-registry.mjs'
|
|
311
311
|
import { MeshSignalingChannel } from './signaling.mjs'
|
|
312
312
|
import { createWebRTCTransportFactory } from './webrtc-negotiator.mjs'
|
|
313
|
+
import { createWshAuthorizer, attachWshLinks } from './wsh-negotiator.mjs'
|
|
313
314
|
import { createMeshSync } from './mesh-sync.mjs'
|
|
314
315
|
import { MeshRelayHost } from './mesh-relay-host.mjs'
|
|
315
316
|
import { attachService } from './mesh-service.mjs'
|
|
@@ -364,6 +365,23 @@ import { registerOrchestratorTools } from './mesh-orchestrator-tools.mjs'
|
|
|
364
365
|
* `[new ManualStrategy()]` there instead.
|
|
365
366
|
* @param {string} [options.discoveryChannelName='mesh-discovery']
|
|
366
367
|
* @param {number} [options.announceInterval]
|
|
368
|
+
* @param {object} [options.wsh] - Opt in to the sealed wsh peer link
|
|
369
|
+
* (`@johnhenry/browsermesh-transport`'s `WshPeerLinks`, which needs the
|
|
370
|
+
* optional peer `@johnhenry/wsh`). Off unless given. The node's own identity
|
|
371
|
+
* is the wsh identity. `relays` (pipe relay URLs) and any other
|
|
372
|
+
* `WshPeerLinks` option are passed through (`WebSocket`, `socketFactory`,
|
|
373
|
+
* `reconnect`, `maxFrameBytes`, ...). Also: `authorize` (default
|
|
374
|
+
* `createWshAuthorizer(registry)`: a peer may link only if the registry
|
|
375
|
+
* grants it `mesh:link:connect`), `listen` (default true: wait at this
|
|
376
|
+
* pod's rendezvous id so peers can reach it), and `require` (default false:
|
|
377
|
+
* a `wsh` endpoint is tried first and, if the caller also gave other
|
|
378
|
+
* endpoints, a failed wsh link falls through to them; when true the
|
|
379
|
+
* negotiator uses ONLY wsh for `connectToPeer()` and a failed
|
|
380
|
+
* wsh link is a failed connect, never a session on another transport.
|
|
381
|
+
* `require` governs outbound connects: inbound WebRTC offers are still
|
|
382
|
+
* answered while a WebRTC signaling bus is wired). Reach a peer with
|
|
383
|
+
* `node.connectToPeer(podId, { wsh: podId })`. The handle is `node.wsh`
|
|
384
|
+
* (`{ links, detach }`); `node.shutdown()` closes the links.
|
|
367
385
|
* @param {string[]} [options.capabilities] - Advertised in the local discovery record.
|
|
368
386
|
* @param {{send: Function, onMessage: Function, open?: Function, close?: Function}} options.signalingTransport
|
|
369
387
|
* Required. Injectable bus for WebRTC offer/answer/ICE relay -- see
|
|
@@ -420,6 +438,10 @@ import { registerOrchestratorTools } from './mesh-orchestrator-tools.mjs'
|
|
|
420
438
|
* `{ name: targetAddress }` map of services to `exposeService()` on the
|
|
421
439
|
* new `MeshRelayHost` immediately (equivalent to calling
|
|
422
440
|
* `node.relayHost.exposeService(name, targetAddress)` for each entry).
|
|
441
|
+
* @param {object} [options.relayHostOptions] - Flow-control and re-check options
|
|
442
|
+
* for the `MeshRelayHost` (`highWaterBytes`, `lowWaterBytes`,
|
|
443
|
+
* `maxBufferedBytesPerConnection`, `maxBufferedBytesPerPeer`,
|
|
444
|
+
* `maxConnectionsPerPeer`, `maxFrameBytes`, `recheckIntervalMs`).
|
|
423
445
|
* @param {string} [options.relayHostEnvelopeType] - Overrides the
|
|
424
446
|
* `envelope.type` the relay host routes/sends on `PeerNode`'s dispatch bus
|
|
425
447
|
* (default `'mesh-relay'`).
|
|
@@ -796,6 +818,7 @@ export async function createMeshNode(options = {}) {
|
|
|
796
818
|
discoveryStrategies,
|
|
797
819
|
discoveryChannelName = 'mesh-discovery',
|
|
798
820
|
announceInterval,
|
|
821
|
+
wsh,
|
|
799
822
|
capabilities = [],
|
|
800
823
|
signalingTransport,
|
|
801
824
|
iceServers,
|
|
@@ -814,6 +837,7 @@ export async function createMeshNode(options = {}) {
|
|
|
814
837
|
relayHostNetwork,
|
|
815
838
|
relayHostServices,
|
|
816
839
|
relayHostEnvelopeType,
|
|
840
|
+
relayHostOptions,
|
|
817
841
|
services,
|
|
818
842
|
servicesNetwork,
|
|
819
843
|
enableHardening = false,
|
|
@@ -972,7 +996,10 @@ export async function createMeshNode(options = {}) {
|
|
|
972
996
|
// ordering is what lets the webrtc factory's onIncomingConnection hook
|
|
973
997
|
// below close over `node` and call PeerNode.adoptIncomingSession() --
|
|
974
998
|
// resolving the callee-side session gap (see peer-node.mjs / Phase 3).
|
|
975
|
-
|
|
999
|
+
// With `wsh` given, a `wsh` endpoint is tried first; `wsh.require` makes it the only one.
|
|
1000
|
+
const transportNegotiator = new MeshTransportNegotiator(
|
|
1001
|
+
!wsh ? {} : wsh.require ? { require: 'wsh' } : { preferenceOrder: ['wsh', 'webrtc', 'wsh-wt', 'wsh-ws'] },
|
|
1002
|
+
)
|
|
976
1003
|
|
|
977
1004
|
// -- Transport hardening (opt-in) -----------------------------------------
|
|
978
1005
|
// Wrapping happens before PeerNode is constructed, but adapter
|
|
@@ -1035,6 +1062,24 @@ export async function createMeshNode(options = {}) {
|
|
|
1035
1062
|
node.signaling = signaling
|
|
1036
1063
|
node.transportNegotiator = transportNegotiator
|
|
1037
1064
|
|
|
1065
|
+
// -- wsh peer link (opt-in) -------------------------------------------------
|
|
1066
|
+
// Sealed, key-authenticated links over wsh (see wsh-negotiator.mjs). The
|
|
1067
|
+
// node's own identity is the wsh identity; a peer may link only if the
|
|
1068
|
+
// registry grants it mesh:link:connect unless `wsh.authorize` says otherwise.
|
|
1069
|
+
if (wsh) {
|
|
1070
|
+
const { WshPeerLinks } = await import('@johnhenry/browsermesh-transport')
|
|
1071
|
+
const { require: _require, listen = true, authorize, ...hubOptions } = wsh
|
|
1072
|
+
const links = new WshPeerLinks({
|
|
1073
|
+
identity: identityManager.getIdentity(podId),
|
|
1074
|
+
authorize: authorize || createWshAuthorizer(registry),
|
|
1075
|
+
onLog: (line) => onLog('mesh-bootstrap:wsh', { line }),
|
|
1076
|
+
...hubOptions,
|
|
1077
|
+
})
|
|
1078
|
+
node.wsh = attachWshLinks({ node, links, negotiator: transportNegotiator, onLog })
|
|
1079
|
+
node.on('shutdown', () => { links.close().catch(() => {}) })
|
|
1080
|
+
if (listen && !skipBoot) await links.listen()
|
|
1081
|
+
}
|
|
1082
|
+
|
|
1038
1083
|
// -- Audit chain (opt-in) --------------------------------------------------
|
|
1039
1084
|
// Attached for inspection/verification, mirroring node.sync/node.relayHost's
|
|
1040
1085
|
// convention. Left unset (not even `null`) when neither enableAudit nor
|
|
@@ -1076,6 +1121,7 @@ export async function createMeshNode(options = {}) {
|
|
|
1076
1121
|
network: relayHostNetwork,
|
|
1077
1122
|
registry,
|
|
1078
1123
|
envelopeType: relayHostEnvelopeType,
|
|
1124
|
+
...relayHostOptions,
|
|
1079
1125
|
onLog,
|
|
1080
1126
|
})
|
|
1081
1127
|
if (relayHostServices) {
|