@johnhenry/browsermesh-apps 0.0.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/LICENSE +21 -0
- package/README.md +63 -0
- package/package.json +36 -0
- package/src/apps.mjs +1351 -0
- package/src/audit.mjs +622 -0
- package/src/chat.mjs +595 -0
- package/src/compat.mjs +30 -0
- package/src/consensus.mjs +784 -0
- package/src/devtools.mjs +255 -0
- package/src/gpu.mjs +732 -0
- package/src/index.mjs +44 -0
- package/src/marketplace-ui.mjs +245 -0
- package/src/marketplace.mjs +754 -0
- package/src/orchestrator.mjs +1575 -0
- package/src/payments.mjs +909 -0
- package/src/peer-agent-swarm.mjs +569 -0
- package/src/peer-agent.mjs +547 -0
- package/src/peer-chat.mjs +381 -0
- package/src/peer-compute.mjs +687 -0
- package/src/peer-encrypted-store.mjs +514 -0
- package/src/peer-escrow.mjs +573 -0
- package/src/peer-files.mjs +470 -0
- package/src/peer-health.mjs +727 -0
- package/src/peer-ipfs.mjs +374 -0
- package/src/peer-node.mjs +793 -0
- package/src/peer-payments.mjs +613 -0
- package/src/peer-registry.mjs +595 -0
- package/src/peer-routing.mjs +555 -0
- package/src/peer-services.mjs +478 -0
- package/src/peer-session.mjs +917 -0
- package/src/peer-terminal.mjs +500 -0
- package/src/peer-timestamp.mjs +403 -0
- package/src/peer-torrent.mjs +628 -0
- package/src/peer-verification.mjs +522 -0
- package/src/quotas.mjs +621 -0
- package/src/resources.mjs +679 -0
- package/src/scheduler.mjs +846 -0
- package/src/silent-catch.mjs +55 -0
- package/src/tools.mjs +668 -0
- package/src/visualizations.mjs +902 -0
|
@@ -0,0 +1,595 @@
|
|
|
1
|
+
import { silentCatch } from './silent-catch.mjs'
|
|
2
|
+
/**
|
|
3
|
+
* clawser-peer-registry.js -- Unified peer registry with permission management.
|
|
4
|
+
*
|
|
5
|
+
* Wraps MeshPeerManager, TrustGraph, and MeshACL into a single facade that
|
|
6
|
+
* coordinates peer lifecycle, trust, and access control. All three subsystems
|
|
7
|
+
* are accepted via dependency injection — the registry creates defaults when
|
|
8
|
+
* they are not provided.
|
|
9
|
+
*
|
|
10
|
+
* No browser-only imports at module level.
|
|
11
|
+
*
|
|
12
|
+
* Run tests:
|
|
13
|
+
* node --import ./web/test/_setup-globals.mjs --test web/test/clawser-peer-registry.test.mjs
|
|
14
|
+
*/
|
|
15
|
+
|
|
16
|
+
// ---------------------------------------------------------------------------
|
|
17
|
+
// PeerRegistry
|
|
18
|
+
// ---------------------------------------------------------------------------
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Unified peer registry combining peer management, trust, and ACL.
|
|
22
|
+
*
|
|
23
|
+
* Every peer operation coordinates across all three subsystems so callers
|
|
24
|
+
* never need to manually keep them in sync.
|
|
25
|
+
*/
|
|
26
|
+
export class PeerRegistry {
|
|
27
|
+
/** @type {import('@johnhenry/browsermesh-core').MeshPeerManager} */
|
|
28
|
+
#peerManager
|
|
29
|
+
|
|
30
|
+
/** @type {import('@johnhenry/browsermesh-core').TrustGraph} */
|
|
31
|
+
#trustGraph
|
|
32
|
+
|
|
33
|
+
/** @type {import('@johnhenry/browsermesh-core').MeshACL} */
|
|
34
|
+
#acl
|
|
35
|
+
|
|
36
|
+
/** @type {string} */
|
|
37
|
+
#localPodId
|
|
38
|
+
|
|
39
|
+
/** @type {Function} */
|
|
40
|
+
#onLog
|
|
41
|
+
/** @type {Map<string, number>} */
|
|
42
|
+
#observedTrust = new Map()
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* @param {object} opts
|
|
46
|
+
* @param {string} opts.localPodId - Owner identity used for ACL and trust edges
|
|
47
|
+
* @param {import('@johnhenry/browsermesh-core').MeshPeerManager} [opts.peerManager]
|
|
48
|
+
* @param {import('@johnhenry/browsermesh-core').TrustGraph} [opts.trustGraph]
|
|
49
|
+
* @param {import('@johnhenry/browsermesh-core').MeshACL} [opts.acl]
|
|
50
|
+
* @param {Function} [opts.onLog] - Logging callback (level, msg)
|
|
51
|
+
*/
|
|
52
|
+
constructor({ localPodId, peerManager, trustGraph, acl, onLog }) {
|
|
53
|
+
if (!localPodId || typeof localPodId !== 'string') {
|
|
54
|
+
throw new Error('localPodId is required and must be a non-empty string')
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
this.#localPodId = localPodId
|
|
58
|
+
this.#onLog = onLog || (() => {})
|
|
59
|
+
|
|
60
|
+
// Accept injected instances or create bare defaults.
|
|
61
|
+
// Callers importing from the actual modules can pass real instances;
|
|
62
|
+
// for testing, lightweight duck-typed stubs work just as well.
|
|
63
|
+
this.#peerManager = peerManager ?? this.#createDefaultPeerManager()
|
|
64
|
+
this.#trustGraph = trustGraph ?? this.#createDefaultTrustGraph()
|
|
65
|
+
this.#acl = acl ?? this.#createDefaultACL()
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
// ── Peer CRUD ───────────────────────────────────────────────────────
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* Add a peer and optionally grant initial capabilities.
|
|
72
|
+
*
|
|
73
|
+
* @param {string} pubKey - Peer fingerprint / public key hash
|
|
74
|
+
* @param {string} [label] - Human-readable name
|
|
75
|
+
* @param {string[]} [grantedCaps] - Initial capability scopes
|
|
76
|
+
* @returns {import('@johnhenry/browsermesh-core').PeerState}
|
|
77
|
+
*/
|
|
78
|
+
addPeer(pubKey, label, grantedCaps) {
|
|
79
|
+
const info = {}
|
|
80
|
+
if (label) info.label = label
|
|
81
|
+
|
|
82
|
+
const peer = this.#peerManager.addPeer(pubKey, info)
|
|
83
|
+
|
|
84
|
+
if (grantedCaps && grantedCaps.length > 0) {
|
|
85
|
+
this.grantCapabilities(pubKey, grantedCaps)
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
this.#onLog(2, `PeerRegistry: added ${pubKey}`)
|
|
89
|
+
return peer
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/**
|
|
93
|
+
* Remove a peer and clean up its trust edges and ACL entries.
|
|
94
|
+
*
|
|
95
|
+
* @param {string} pubKey
|
|
96
|
+
* @returns {boolean} true if the peer existed
|
|
97
|
+
*/
|
|
98
|
+
removePeer(pubKey) {
|
|
99
|
+
const existed = this.#peerManager.removePeer(pubKey)
|
|
100
|
+
|
|
101
|
+
// Remove trust edges originating from us to this peer
|
|
102
|
+
this.#trustGraph.removeEdge(this.#localPodId, pubKey)
|
|
103
|
+
|
|
104
|
+
// Remove ACL roster entry
|
|
105
|
+
this.#acl.revokeAll(pubKey)
|
|
106
|
+
|
|
107
|
+
if (existed) {
|
|
108
|
+
this.#onLog(2, `PeerRegistry: removed ${pubKey}`)
|
|
109
|
+
}
|
|
110
|
+
return existed
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
/**
|
|
114
|
+
* Get a single peer by public key.
|
|
115
|
+
*
|
|
116
|
+
* @param {string} pubKey
|
|
117
|
+
* @returns {import('@johnhenry/browsermesh-core').PeerState|null}
|
|
118
|
+
*/
|
|
119
|
+
getPeer(pubKey) {
|
|
120
|
+
return this.#peerManager.getPeer(pubKey)
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
/**
|
|
124
|
+
* List peers, optionally filtered by status or trust level.
|
|
125
|
+
*
|
|
126
|
+
* @param {object} [filter]
|
|
127
|
+
* @param {string} [filter.status]
|
|
128
|
+
* @param {number} [filter.minTrust]
|
|
129
|
+
* @returns {import('@johnhenry/browsermesh-core').PeerState[]}
|
|
130
|
+
*/
|
|
131
|
+
listPeers(filter) {
|
|
132
|
+
return this.#peerManager.listPeers(filter)
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
// ── Permission management ───────────────────────────────────────────
|
|
136
|
+
|
|
137
|
+
/**
|
|
138
|
+
* Assign an ACL template to a peer, replacing any previous entry.
|
|
139
|
+
*
|
|
140
|
+
* @param {string} pubKey
|
|
141
|
+
* @param {string} templateName
|
|
142
|
+
*/
|
|
143
|
+
updatePermissions(pubKey, templateName) {
|
|
144
|
+
// Remove old entry if present, then add new one
|
|
145
|
+
this.#acl.removeEntry(pubKey)
|
|
146
|
+
this.#acl.addEntry(pubKey, templateName)
|
|
147
|
+
this.#onLog(3, `PeerRegistry: set template '${templateName}' for ${pubKey}`)
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
/**
|
|
151
|
+
* Grant additional capability scopes to a peer via a dynamic ACL template.
|
|
152
|
+
* Creates a per-peer template named `_peer_{pubKey}` that merges with
|
|
153
|
+
* any existing scopes.
|
|
154
|
+
*
|
|
155
|
+
* @param {string} pubKey
|
|
156
|
+
* @param {string[]} scopes - Scopes to add (e.g. ['files:read', 'chat:write'])
|
|
157
|
+
*/
|
|
158
|
+
grantCapabilities(pubKey, scopes) {
|
|
159
|
+
const templateName = `_peer_${pubKey}`
|
|
160
|
+
const existing = this.#acl.getTemplate(templateName)
|
|
161
|
+
const merged = existing
|
|
162
|
+
? [...new Set([...existing.scopes, ...scopes])]
|
|
163
|
+
: [...scopes]
|
|
164
|
+
|
|
165
|
+
this.#acl.addTemplate(templateName, merged, `Auto-generated for ${pubKey}`)
|
|
166
|
+
|
|
167
|
+
// Ensure roster entry points to the per-peer template
|
|
168
|
+
const entry = this.#acl.getEntry(pubKey)
|
|
169
|
+
if (!entry || entry.templateName !== templateName) {
|
|
170
|
+
this.#acl.removeEntry(pubKey)
|
|
171
|
+
this.#acl.addEntry(pubKey, templateName)
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
this.#onLog(3, `PeerRegistry: granted ${scopes.join(', ')} to ${pubKey}`)
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
/**
|
|
178
|
+
* Revoke specific capability scopes from a peer.
|
|
179
|
+
* If no scopes remain, removes the per-peer template and roster entry.
|
|
180
|
+
*
|
|
181
|
+
* @param {string} pubKey
|
|
182
|
+
* @param {string[]} scopes - Scopes to remove
|
|
183
|
+
*/
|
|
184
|
+
revokeCapabilities(pubKey, scopes) {
|
|
185
|
+
const templateName = `_peer_${pubKey}`
|
|
186
|
+
const existing = this.#acl.getTemplate(templateName)
|
|
187
|
+
if (!existing) return
|
|
188
|
+
|
|
189
|
+
const remaining = existing.scopes.filter(s => !scopes.includes(s))
|
|
190
|
+
|
|
191
|
+
if (remaining.length === 0) {
|
|
192
|
+
this.#acl.removeEntry(pubKey)
|
|
193
|
+
this.#acl.removeTemplate(templateName)
|
|
194
|
+
} else {
|
|
195
|
+
this.#acl.addTemplate(templateName, remaining, `Auto-generated for ${pubKey}`)
|
|
196
|
+
// Re-sync the roster entry
|
|
197
|
+
this.#acl.removeEntry(pubKey)
|
|
198
|
+
this.#acl.addEntry(pubKey, templateName)
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
this.#onLog(3, `PeerRegistry: revoked ${scopes.join(', ')} from ${pubKey}`)
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
/**
|
|
205
|
+
* Get the current capabilities for a peer.
|
|
206
|
+
*
|
|
207
|
+
* @param {string} pubKey
|
|
208
|
+
* @returns {{ template: string|null, scopes: string[] }}
|
|
209
|
+
*/
|
|
210
|
+
getPeerCapabilities(pubKey) {
|
|
211
|
+
const entry = this.#acl.getEntry(pubKey)
|
|
212
|
+
if (!entry) return { template: null, scopes: [] }
|
|
213
|
+
|
|
214
|
+
const tpl = this.#acl.getTemplate(entry.templateName)
|
|
215
|
+
return {
|
|
216
|
+
template: entry.templateName,
|
|
217
|
+
scopes: tpl ? [...tpl.scopes] : [],
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
/**
|
|
222
|
+
* Check if a peer is allowed to perform an action on a resource.
|
|
223
|
+
*
|
|
224
|
+
* @param {string} pubKey
|
|
225
|
+
* @param {string} resource
|
|
226
|
+
* @param {string} action
|
|
227
|
+
* @returns {{ allowed: boolean, reason?: string }}
|
|
228
|
+
*/
|
|
229
|
+
checkAccess(pubKey, resource, action) {
|
|
230
|
+
return this.#acl.check(pubKey, resource, action)
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
// ── Trust management ────────────────────────────────────────────────
|
|
234
|
+
|
|
235
|
+
/**
|
|
236
|
+
* Set the trust level for a peer (from the local pod's perspective).
|
|
237
|
+
*
|
|
238
|
+
* @param {string} pubKey
|
|
239
|
+
* @param {number} level - Trust in [0.0, 1.0]
|
|
240
|
+
* @param {string[]} [scopes] - Scope tags for the trust relationship
|
|
241
|
+
*/
|
|
242
|
+
setTrust(pubKey, level, scopes) {
|
|
243
|
+
this.#trustGraph.addEdge(this.#localPodId, pubKey, level, scopes)
|
|
244
|
+
this.#onLog(3, `PeerRegistry: set trust ${level} for ${pubKey}`)
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
/**
|
|
248
|
+
* Get the trust level we have for a peer (direct or transitive).
|
|
249
|
+
*
|
|
250
|
+
* @param {string} pubKey
|
|
251
|
+
* @returns {number} Trust in [0.0, 1.0]
|
|
252
|
+
*/
|
|
253
|
+
getTrust(pubKey) {
|
|
254
|
+
return this.#trustGraph.getTrustLevel(this.#localPodId, pubKey)
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
/**
|
|
258
|
+
* Record a runtime-observed trust/reputation signal without overwriting
|
|
259
|
+
* the operator's explicit trust graph input.
|
|
260
|
+
*
|
|
261
|
+
* @param {string} pubKey
|
|
262
|
+
* @param {number} level
|
|
263
|
+
* @returns {number}
|
|
264
|
+
*/
|
|
265
|
+
recordObservedTrust(pubKey, level) {
|
|
266
|
+
const clamped = Math.max(0, Math.min(1, Number(level) || 0))
|
|
267
|
+
this.#observedTrust.set(pubKey, clamped)
|
|
268
|
+
this.#onLog(3, `PeerRegistry: observed trust ${clamped} for ${pubKey}`)
|
|
269
|
+
return clamped
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
/**
|
|
273
|
+
* @param {string} pubKey
|
|
274
|
+
* @returns {number}
|
|
275
|
+
*/
|
|
276
|
+
getObservedTrust(pubKey) {
|
|
277
|
+
return this.#observedTrust.get(pubKey) ?? 0
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
/**
|
|
281
|
+
* Combined reputation score used for route ranking. Explicit trust remains
|
|
282
|
+
* authoritative, with observed runtime quality blended in as a secondary
|
|
283
|
+
* signal.
|
|
284
|
+
*
|
|
285
|
+
* @param {string} pubKey
|
|
286
|
+
* @returns {number}
|
|
287
|
+
*/
|
|
288
|
+
getReputation(pubKey) {
|
|
289
|
+
const explicit = this.getTrust(pubKey)
|
|
290
|
+
const observed = this.getObservedTrust(pubKey)
|
|
291
|
+
return explicit > 0
|
|
292
|
+
? Math.max(0, Math.min(1, explicit * 0.7 + observed * 0.3))
|
|
293
|
+
: observed
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
/**
|
|
297
|
+
* Check whether a peer is trusted, optionally within a scope.
|
|
298
|
+
*
|
|
299
|
+
* @param {string} pubKey
|
|
300
|
+
* @param {string|null} [scope]
|
|
301
|
+
* @param {number} [minLevel=0.25]
|
|
302
|
+
* @returns {boolean}
|
|
303
|
+
*/
|
|
304
|
+
isTrusted(pubKey, scope, minLevel) {
|
|
305
|
+
return this.#trustGraph.isTrusted(this.#localPodId, pubKey, scope, minLevel)
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
// ── Connection lifecycle ────────────────────────────────────────────
|
|
309
|
+
|
|
310
|
+
/**
|
|
311
|
+
* Connect to a peer (delegates to MeshPeerManager).
|
|
312
|
+
*
|
|
313
|
+
* @param {string} pubKey
|
|
314
|
+
* @param {object} [opts]
|
|
315
|
+
* @param {string} [opts.transport]
|
|
316
|
+
* @param {string} [opts.endpoint]
|
|
317
|
+
* @returns {import('@johnhenry/browsermesh-core').PeerState}
|
|
318
|
+
*/
|
|
319
|
+
connect(pubKey, opts) {
|
|
320
|
+
return this.#peerManager.connect(pubKey, opts)
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
/**
|
|
324
|
+
* Disconnect a peer.
|
|
325
|
+
*
|
|
326
|
+
* @param {string} pubKey
|
|
327
|
+
*/
|
|
328
|
+
disconnect(pubKey) {
|
|
329
|
+
this.#peerManager.disconnect(pubKey)
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
/**
|
|
333
|
+
* Disconnect all peers.
|
|
334
|
+
*/
|
|
335
|
+
disconnectAll() {
|
|
336
|
+
this.#peerManager.disconnectAll()
|
|
337
|
+
}
|
|
338
|
+
|
|
339
|
+
// ── Events ──────────────────────────────────────────────────────────
|
|
340
|
+
|
|
341
|
+
/**
|
|
342
|
+
* Register a callback for peer connection events.
|
|
343
|
+
* @param {Function} cb
|
|
344
|
+
*/
|
|
345
|
+
onPeerConnect(cb) {
|
|
346
|
+
this.#peerManager.onPeerConnect(cb)
|
|
347
|
+
}
|
|
348
|
+
|
|
349
|
+
/**
|
|
350
|
+
* Register a callback for peer disconnection events.
|
|
351
|
+
* @param {Function} cb
|
|
352
|
+
*/
|
|
353
|
+
onPeerDisconnect(cb) {
|
|
354
|
+
this.#peerManager.onPeerDisconnect(cb)
|
|
355
|
+
}
|
|
356
|
+
|
|
357
|
+
// ── Stats ───────────────────────────────────────────────────────────
|
|
358
|
+
|
|
359
|
+
/**
|
|
360
|
+
* Get aggregate connection statistics.
|
|
361
|
+
*
|
|
362
|
+
* @returns {{ total: number, connected: number, disconnected: number, connecting: number }}
|
|
363
|
+
*/
|
|
364
|
+
getStats() {
|
|
365
|
+
return this.#peerManager.getStats()
|
|
366
|
+
}
|
|
367
|
+
|
|
368
|
+
/** @returns {number} */
|
|
369
|
+
get size() {
|
|
370
|
+
return this.#peerManager.size
|
|
371
|
+
}
|
|
372
|
+
|
|
373
|
+
// ── Persistence ─────────────────────────────────────────────────────
|
|
374
|
+
|
|
375
|
+
/**
|
|
376
|
+
* Serialize the full registry state.
|
|
377
|
+
*
|
|
378
|
+
* @returns {object}
|
|
379
|
+
*/
|
|
380
|
+
toJSON() {
|
|
381
|
+
return {
|
|
382
|
+
localPodId: this.#localPodId,
|
|
383
|
+
peers: this.#peerManager.toJSON(),
|
|
384
|
+
trust: this.#trustGraph.toJSON(),
|
|
385
|
+
observedTrust: Object.fromEntries(this.#observedTrust),
|
|
386
|
+
acl: this.#acl.toJSON(),
|
|
387
|
+
}
|
|
388
|
+
}
|
|
389
|
+
|
|
390
|
+
/**
|
|
391
|
+
* Restore a PeerRegistry from serialized data.
|
|
392
|
+
* Requires the same subsystem constructors to be available; accepts
|
|
393
|
+
* factory functions for creating typed instances from JSON.
|
|
394
|
+
*
|
|
395
|
+
* @param {object} data
|
|
396
|
+
* @param {object} [factories] - Optional constructors for subsystems
|
|
397
|
+
* @param {Function} [factories.PeerManager] - MeshPeerManager class
|
|
398
|
+
* @param {Function} [factories.TrustGraph] - TrustGraph class
|
|
399
|
+
* @param {Function} [factories.ACL] - MeshACL class
|
|
400
|
+
* @returns {PeerRegistry}
|
|
401
|
+
*/
|
|
402
|
+
static fromJSON(data, factories = {}) {
|
|
403
|
+
const PeerManager = factories.PeerManager
|
|
404
|
+
const Trust = factories.TrustGraph
|
|
405
|
+
const ACL = factories.ACL
|
|
406
|
+
|
|
407
|
+
const peerManager = PeerManager ? PeerManager.fromJSON(data.peers) : undefined
|
|
408
|
+
const trustGraph = Trust ? Trust.fromJSON(data.trust) : undefined
|
|
409
|
+
const acl = ACL ? ACL.fromJSON(data.acl) : undefined
|
|
410
|
+
|
|
411
|
+
const registry = new PeerRegistry({
|
|
412
|
+
localPodId: data.localPodId,
|
|
413
|
+
peerManager,
|
|
414
|
+
trustGraph,
|
|
415
|
+
acl,
|
|
416
|
+
})
|
|
417
|
+
for (const [pubKey, level] of Object.entries(data.observedTrust || {})) {
|
|
418
|
+
registry.#observedTrust.set(pubKey, level)
|
|
419
|
+
}
|
|
420
|
+
return registry
|
|
421
|
+
}
|
|
422
|
+
|
|
423
|
+
// ── Internal helpers ────────────────────────────────────────────────
|
|
424
|
+
|
|
425
|
+
/**
|
|
426
|
+
* Create a minimal duck-typed MeshPeerManager when none is injected.
|
|
427
|
+
* @returns {object}
|
|
428
|
+
*/
|
|
429
|
+
#createDefaultPeerManager() {
|
|
430
|
+
const peers = new Map()
|
|
431
|
+
const callbacks = { connect: [], disconnect: [] }
|
|
432
|
+
|
|
433
|
+
const fire = (event, data) => {
|
|
434
|
+
for (const cb of [...(callbacks[event] || [])]) {
|
|
435
|
+
try { cb(data) } catch (e) { silentCatch('clawser-peer-registry', 'swallow', e) }
|
|
436
|
+
}
|
|
437
|
+
}
|
|
438
|
+
|
|
439
|
+
return {
|
|
440
|
+
addPeer(fingerprint, info = {}) {
|
|
441
|
+
const existing = peers.get(fingerprint)
|
|
442
|
+
if (existing) {
|
|
443
|
+
Object.assign(existing, info)
|
|
444
|
+
return existing
|
|
445
|
+
}
|
|
446
|
+
const peer = { fingerprint, status: 'disconnected', ...info }
|
|
447
|
+
peers.set(fingerprint, peer)
|
|
448
|
+
return peer
|
|
449
|
+
},
|
|
450
|
+
removePeer(fingerprint) { return peers.delete(fingerprint) },
|
|
451
|
+
getPeer(fingerprint) { return peers.get(fingerprint) || null },
|
|
452
|
+
listPeers(filter = {}) {
|
|
453
|
+
let list = [...peers.values()]
|
|
454
|
+
if (filter.status) list = list.filter(p => p.status === filter.status)
|
|
455
|
+
if (filter.minTrust !== undefined) list = list.filter(p => (p.trustLevel || 0) >= filter.minTrust)
|
|
456
|
+
return list
|
|
457
|
+
},
|
|
458
|
+
connect(fingerprint, opts = {}) {
|
|
459
|
+
if (!peers.has(fingerprint)) this.addPeer(fingerprint, opts)
|
|
460
|
+
const peer = peers.get(fingerprint)
|
|
461
|
+
const oldStatus = peer.status
|
|
462
|
+
peer.status = 'connected'
|
|
463
|
+
peer.transport = opts.transport || null
|
|
464
|
+
peer.endpoint = opts.endpoint || null
|
|
465
|
+
if (oldStatus !== 'connected' && oldStatus !== 'authenticated') fire('connect', peer)
|
|
466
|
+
return peer
|
|
467
|
+
},
|
|
468
|
+
disconnect(fingerprint) {
|
|
469
|
+
const peer = peers.get(fingerprint)
|
|
470
|
+
if (!peer) return
|
|
471
|
+
const old = peer.status
|
|
472
|
+
peer.status = 'disconnected'
|
|
473
|
+
peer.transport = null
|
|
474
|
+
if (old !== 'disconnected') fire('disconnect', peer)
|
|
475
|
+
},
|
|
476
|
+
disconnectAll() { for (const fp of peers.keys()) this.disconnect(fp) },
|
|
477
|
+
onPeerConnect(cb) { callbacks.connect.push(cb) },
|
|
478
|
+
onPeerDisconnect(cb) { callbacks.disconnect.push(cb) },
|
|
479
|
+
offPeerConnect(cb) {
|
|
480
|
+
const idx = callbacks.connect.indexOf(cb)
|
|
481
|
+
if (idx >= 0) callbacks.connect.splice(idx, 1)
|
|
482
|
+
},
|
|
483
|
+
offPeerDisconnect(cb) {
|
|
484
|
+
const idx = callbacks.disconnect.indexOf(cb)
|
|
485
|
+
if (idx >= 0) callbacks.disconnect.splice(idx, 1)
|
|
486
|
+
},
|
|
487
|
+
clearListeners() { callbacks.connect.length = 0; callbacks.disconnect.length = 0 },
|
|
488
|
+
getStats() {
|
|
489
|
+
const all = [...peers.values()]
|
|
490
|
+
return {
|
|
491
|
+
total: all.length,
|
|
492
|
+
connected: all.filter(p => p.status === 'connected' || p.status === 'authenticated').length,
|
|
493
|
+
disconnected: all.filter(p => p.status === 'disconnected').length,
|
|
494
|
+
connecting: all.filter(p => p.status === 'connecting').length,
|
|
495
|
+
}
|
|
496
|
+
},
|
|
497
|
+
get size() { return peers.size },
|
|
498
|
+
toJSON() { return [...peers.values()] },
|
|
499
|
+
}
|
|
500
|
+
}
|
|
501
|
+
|
|
502
|
+
/**
|
|
503
|
+
* Create a minimal duck-typed TrustGraph when none is injected.
|
|
504
|
+
* @returns {object}
|
|
505
|
+
*/
|
|
506
|
+
#createDefaultTrustGraph() {
|
|
507
|
+
const edges = []
|
|
508
|
+
|
|
509
|
+
return {
|
|
510
|
+
addEdge(fromId, toId, level, scopes = []) {
|
|
511
|
+
const idx = edges.findIndex(e => e.from === fromId && e.to === toId)
|
|
512
|
+
if (idx >= 0) edges.splice(idx, 1)
|
|
513
|
+
edges.push({ from: fromId, to: toId, value: level, scopes: [...scopes] })
|
|
514
|
+
},
|
|
515
|
+
removeEdge(fromId, toId) {
|
|
516
|
+
const idx = edges.findIndex(e => e.from === fromId && e.to === toId)
|
|
517
|
+
if (idx >= 0) { edges.splice(idx, 1); return true }
|
|
518
|
+
return false
|
|
519
|
+
},
|
|
520
|
+
getTrustLevel(fromId, toId) {
|
|
521
|
+
const e = edges.find(e => e.from === fromId && e.to === toId)
|
|
522
|
+
return e ? e.value : 0
|
|
523
|
+
},
|
|
524
|
+
isTrusted(fromId, toId, scope, minLevel = 0.25) {
|
|
525
|
+
const e = edges.find(e => e.from === fromId && e.to === toId)
|
|
526
|
+
if (!e || e.value < minLevel) return false
|
|
527
|
+
if (scope && e.scopes.length > 0 && !e.scopes.includes(scope)) return false
|
|
528
|
+
return true
|
|
529
|
+
},
|
|
530
|
+
toJSON() { return edges.map(e => ({ ...e, scopes: [...e.scopes] })) },
|
|
531
|
+
}
|
|
532
|
+
}
|
|
533
|
+
|
|
534
|
+
/**
|
|
535
|
+
* Create a minimal duck-typed MeshACL when none is injected.
|
|
536
|
+
* @returns {object}
|
|
537
|
+
*/
|
|
538
|
+
#createDefaultACL() {
|
|
539
|
+
const owner = this.#localPodId
|
|
540
|
+
const templates = new Map()
|
|
541
|
+
const roster = new Map()
|
|
542
|
+
|
|
543
|
+
// Seed default templates
|
|
544
|
+
templates.set('guest', { name: 'guest', scopes: ['chat:read', 'files:read'] })
|
|
545
|
+
templates.set('collaborator', { name: 'collaborator', scopes: ['chat:*', 'files:read', 'files:write', 'compute:submit'] })
|
|
546
|
+
templates.set('admin', { name: 'admin', scopes: ['*:*'] })
|
|
547
|
+
|
|
548
|
+
const matchScope = (pattern, scope) => {
|
|
549
|
+
if (pattern === '*:*') return true
|
|
550
|
+
const [pRes, pAct] = pattern.split(':')
|
|
551
|
+
const [sRes, sAct] = scope.split(':')
|
|
552
|
+
return (pRes === '*' || pRes === sRes) && (pAct === '*' || pAct === sAct)
|
|
553
|
+
}
|
|
554
|
+
|
|
555
|
+
return {
|
|
556
|
+
addTemplate(name, scopes, description) {
|
|
557
|
+
const t = { name, scopes: [...scopes], description }
|
|
558
|
+
templates.set(name, t)
|
|
559
|
+
return t
|
|
560
|
+
},
|
|
561
|
+
removeTemplate(name) { return templates.delete(name) },
|
|
562
|
+
getTemplate(name) { return templates.get(name) || null },
|
|
563
|
+
addEntry(identity, templateName, opts = {}) {
|
|
564
|
+
if (!templates.has(templateName)) throw new Error(`Unknown template: ${templateName}`)
|
|
565
|
+
const entry = { identity, templateName, ...opts }
|
|
566
|
+
roster.set(identity, entry)
|
|
567
|
+
return entry
|
|
568
|
+
},
|
|
569
|
+
removeEntry(identity) { return roster.delete(identity) },
|
|
570
|
+
getEntry(identity) { return roster.get(identity) || null },
|
|
571
|
+
check(identity, resource, action) {
|
|
572
|
+
if (identity === owner) return { allowed: true, reason: 'owner' }
|
|
573
|
+
const entry = roster.get(identity)
|
|
574
|
+
if (!entry) return { allowed: false, reason: 'not_in_roster' }
|
|
575
|
+
const tpl = templates.get(entry.templateName)
|
|
576
|
+
if (!tpl) return { allowed: false, reason: 'template_missing' }
|
|
577
|
+
const scope = `${resource}:${action}`
|
|
578
|
+
if (tpl.scopes.some(s => matchScope(s, scope))) return { allowed: true }
|
|
579
|
+
return { allowed: false, reason: 'scope_denied' }
|
|
580
|
+
},
|
|
581
|
+
revokeAll(identity) {
|
|
582
|
+
const had = roster.has(identity)
|
|
583
|
+
roster.delete(identity)
|
|
584
|
+
return had ? 1 : 0
|
|
585
|
+
},
|
|
586
|
+
toJSON() {
|
|
587
|
+
return {
|
|
588
|
+
owner,
|
|
589
|
+
templates: [...templates.values()].filter(t => !['guest', 'collaborator', 'admin'].includes(t.name)),
|
|
590
|
+
roster: [...roster.values()],
|
|
591
|
+
}
|
|
592
|
+
},
|
|
593
|
+
}
|
|
594
|
+
}
|
|
595
|
+
}
|