@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,902 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* clawser-mesh-visualizations.js -- Trust & Topology Visualization Data.
|
|
3
|
+
*
|
|
4
|
+
* Produces structured data for rendering trust graphs and mesh topology.
|
|
5
|
+
* UI-agnostic — outputs JSON-friendly objects suitable for any renderer
|
|
6
|
+
* (Canvas, SVG, D3, etc.).
|
|
7
|
+
*
|
|
8
|
+
* - TrustGraphLayout: positions nodes and edges for trust graph rendering.
|
|
9
|
+
* - TrustHeatmap: aggregates trust levels into a grid/matrix.
|
|
10
|
+
* - TopologySnapshot: captures current mesh topology state.
|
|
11
|
+
* - TopologyLayout: assigns positions to topology nodes.
|
|
12
|
+
* - TopologyDiff: computes changes between two topology snapshots.
|
|
13
|
+
* - VisualizationExporter: serializes layouts for external renderers.
|
|
14
|
+
*
|
|
15
|
+
* No browser-only imports at module level.
|
|
16
|
+
*
|
|
17
|
+
* Run tests:
|
|
18
|
+
* node --import ./web/test/_setup-globals.mjs --test web/test/clawser-mesh-visualizations.test.mjs
|
|
19
|
+
*/
|
|
20
|
+
|
|
21
|
+
// ---------------------------------------------------------------------------
|
|
22
|
+
// Wire Constants
|
|
23
|
+
// ---------------------------------------------------------------------------
|
|
24
|
+
|
|
25
|
+
/** Wire type for topology snapshot broadcast. */
|
|
26
|
+
export const TOPOLOGY_SNAPSHOT = 0xE6;
|
|
27
|
+
|
|
28
|
+
/** Wire type for topology diff broadcast. */
|
|
29
|
+
export const TOPOLOGY_DIFF = 0xE7;
|
|
30
|
+
|
|
31
|
+
// ---------------------------------------------------------------------------
|
|
32
|
+
// TrustGraphLayout
|
|
33
|
+
// ---------------------------------------------------------------------------
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* Computes positions for trust graph visualization.
|
|
37
|
+
* Uses a simple force-directed layout approximation.
|
|
38
|
+
*/
|
|
39
|
+
export class TrustGraphLayout {
|
|
40
|
+
/** @type {{ id: string, x: number, y: number, label: string, trustLevel: number }[]} */
|
|
41
|
+
#nodes = [];
|
|
42
|
+
|
|
43
|
+
/** @type {{ from: string, to: string, weight: number, label?: string }[]} */
|
|
44
|
+
#edges = [];
|
|
45
|
+
|
|
46
|
+
/** @type {number} */
|
|
47
|
+
#width;
|
|
48
|
+
|
|
49
|
+
/** @type {number} */
|
|
50
|
+
#height;
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* @param {object} [opts]
|
|
54
|
+
* @param {number} [opts.width=800]
|
|
55
|
+
* @param {number} [opts.height=600]
|
|
56
|
+
*/
|
|
57
|
+
constructor({ width = 800, height = 600 } = {}) {
|
|
58
|
+
this.#width = width;
|
|
59
|
+
this.#height = height;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
get width() { return this.#width; }
|
|
63
|
+
get height() { return this.#height; }
|
|
64
|
+
get nodeCount() { return this.#nodes.length; }
|
|
65
|
+
get edgeCount() { return this.#edges.length; }
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* Add a node to the graph.
|
|
69
|
+
* @param {string} id
|
|
70
|
+
* @param {object} [opts]
|
|
71
|
+
* @param {string} [opts.label]
|
|
72
|
+
* @param {number} [opts.trustLevel=0.5]
|
|
73
|
+
* @param {number} [opts.x] - Fixed x position (auto-placed if omitted)
|
|
74
|
+
* @param {number} [opts.y] - Fixed y position (auto-placed if omitted)
|
|
75
|
+
*/
|
|
76
|
+
addNode(id, { label, trustLevel = 0.5, x, y } = {}) {
|
|
77
|
+
if (!id || typeof id !== 'string') {
|
|
78
|
+
throw new Error('id is required and must be a non-empty string');
|
|
79
|
+
}
|
|
80
|
+
if (this.#nodes.some(n => n.id === id)) {
|
|
81
|
+
throw new Error(`Duplicate node id: ${id}`);
|
|
82
|
+
}
|
|
83
|
+
this.#nodes.push({
|
|
84
|
+
id,
|
|
85
|
+
label: label || id,
|
|
86
|
+
trustLevel: Math.max(0, Math.min(1, trustLevel)),
|
|
87
|
+
x: x ?? Math.random() * this.#width,
|
|
88
|
+
y: y ?? Math.random() * this.#height,
|
|
89
|
+
});
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/**
|
|
93
|
+
* Add an edge between two nodes.
|
|
94
|
+
* @param {string} from
|
|
95
|
+
* @param {string} to
|
|
96
|
+
* @param {object} [opts]
|
|
97
|
+
* @param {number} [opts.weight=1.0] Trust weight
|
|
98
|
+
* @param {string} [opts.label]
|
|
99
|
+
*/
|
|
100
|
+
addEdge(from, to, { weight = 1.0, label } = {}) {
|
|
101
|
+
if (!this.#nodes.some(n => n.id === from)) {
|
|
102
|
+
throw new Error(`Unknown source node: ${from}`);
|
|
103
|
+
}
|
|
104
|
+
if (!this.#nodes.some(n => n.id === to)) {
|
|
105
|
+
throw new Error(`Unknown target node: ${to}`);
|
|
106
|
+
}
|
|
107
|
+
this.#edges.push({ from, to, weight, label });
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
/**
|
|
111
|
+
* Remove a node and its associated edges.
|
|
112
|
+
* @param {string} id
|
|
113
|
+
*/
|
|
114
|
+
removeNode(id) {
|
|
115
|
+
const idx = this.#nodes.findIndex(n => n.id === id);
|
|
116
|
+
if (idx === -1) throw new Error(`Node not found: ${id}`);
|
|
117
|
+
this.#nodes.splice(idx, 1);
|
|
118
|
+
this.#edges = this.#edges.filter(e => e.from !== id && e.to !== id);
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
/**
|
|
122
|
+
* Run a simple spring-layout iteration.
|
|
123
|
+
* Moves nodes toward attractive equilibrium.
|
|
124
|
+
* @param {number} [iterations=50]
|
|
125
|
+
* @param {number} [repulsion=5000]
|
|
126
|
+
* @param {number} [attraction=0.01]
|
|
127
|
+
*/
|
|
128
|
+
layout(iterations = 50, repulsion = 5000, attraction = 0.01) {
|
|
129
|
+
for (let iter = 0; iter < iterations; iter++) {
|
|
130
|
+
// Repulsive forces between all node pairs
|
|
131
|
+
for (let i = 0; i < this.#nodes.length; i++) {
|
|
132
|
+
for (let j = i + 1; j < this.#nodes.length; j++) {
|
|
133
|
+
const a = this.#nodes[i];
|
|
134
|
+
const b = this.#nodes[j];
|
|
135
|
+
const dx = b.x - a.x;
|
|
136
|
+
const dy = b.y - a.y;
|
|
137
|
+
const dist = Math.sqrt(dx * dx + dy * dy) || 1;
|
|
138
|
+
const force = repulsion / (dist * dist);
|
|
139
|
+
const fx = (dx / dist) * force;
|
|
140
|
+
const fy = (dy / dist) * force;
|
|
141
|
+
a.x -= fx;
|
|
142
|
+
a.y -= fy;
|
|
143
|
+
b.x += fx;
|
|
144
|
+
b.y += fy;
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
// Attractive forces along edges
|
|
149
|
+
for (const edge of this.#edges) {
|
|
150
|
+
const a = this.#nodes.find(n => n.id === edge.from);
|
|
151
|
+
const b = this.#nodes.find(n => n.id === edge.to);
|
|
152
|
+
if (!a || !b) continue;
|
|
153
|
+
const dx = b.x - a.x;
|
|
154
|
+
const dy = b.y - a.y;
|
|
155
|
+
const dist = Math.sqrt(dx * dx + dy * dy) || 1;
|
|
156
|
+
const force = dist * attraction * edge.weight;
|
|
157
|
+
const fx = (dx / dist) * force;
|
|
158
|
+
const fy = (dy / dist) * force;
|
|
159
|
+
a.x += fx;
|
|
160
|
+
a.y += fy;
|
|
161
|
+
b.x -= fx;
|
|
162
|
+
b.y -= fy;
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
// Center gravity
|
|
166
|
+
const cx = this.#width / 2;
|
|
167
|
+
const cy = this.#height / 2;
|
|
168
|
+
for (const n of this.#nodes) {
|
|
169
|
+
n.x += (cx - n.x) * 0.01;
|
|
170
|
+
n.y += (cy - n.y) * 0.01;
|
|
171
|
+
// Clamp to bounds
|
|
172
|
+
n.x = Math.max(20, Math.min(this.#width - 20, n.x));
|
|
173
|
+
n.y = Math.max(20, Math.min(this.#height - 20, n.y));
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
/** Get node by ID. */
|
|
179
|
+
getNode(id) {
|
|
180
|
+
return this.#nodes.find(n => n.id === id) || null;
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
/** Get all nodes (copy). */
|
|
184
|
+
getNodes() {
|
|
185
|
+
return this.#nodes.map(n => ({ ...n }));
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
/** Get all edges (copy). */
|
|
189
|
+
getEdges() {
|
|
190
|
+
return this.#edges.map(e => ({ ...e }));
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
/** Get edges connected to a node. */
|
|
194
|
+
getEdgesFor(nodeId) {
|
|
195
|
+
return this.#edges.filter(e => e.from === nodeId || e.to === nodeId);
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
toJSON() {
|
|
199
|
+
return {
|
|
200
|
+
width: this.#width,
|
|
201
|
+
height: this.#height,
|
|
202
|
+
nodes: this.getNodes(),
|
|
203
|
+
edges: this.getEdges(),
|
|
204
|
+
};
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
static fromJSON(data) {
|
|
208
|
+
const layout = new TrustGraphLayout({ width: data.width, height: data.height });
|
|
209
|
+
for (const n of data.nodes) {
|
|
210
|
+
layout.addNode(n.id, { label: n.label, trustLevel: n.trustLevel, x: n.x, y: n.y });
|
|
211
|
+
}
|
|
212
|
+
for (const e of data.edges) {
|
|
213
|
+
layout.addEdge(e.from, e.to, { weight: e.weight, label: e.label });
|
|
214
|
+
}
|
|
215
|
+
return layout;
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
// ---------------------------------------------------------------------------
|
|
220
|
+
// TrustHeatmap
|
|
221
|
+
// ---------------------------------------------------------------------------
|
|
222
|
+
|
|
223
|
+
/**
|
|
224
|
+
* Aggregates trust relationships into a matrix for heatmap rendering.
|
|
225
|
+
*/
|
|
226
|
+
export class TrustHeatmap {
|
|
227
|
+
/** @type {string[]} */
|
|
228
|
+
#podIds = [];
|
|
229
|
+
|
|
230
|
+
/** @type {Map<string, Map<string, number>>} from → (to → level) */
|
|
231
|
+
#matrix = new Map();
|
|
232
|
+
|
|
233
|
+
/**
|
|
234
|
+
* Add a pod to the heatmap.
|
|
235
|
+
* @param {string} podId
|
|
236
|
+
*/
|
|
237
|
+
addPod(podId) {
|
|
238
|
+
if (!this.#podIds.includes(podId)) {
|
|
239
|
+
this.#podIds.push(podId);
|
|
240
|
+
this.#matrix.set(podId, new Map());
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
/**
|
|
245
|
+
* Set trust level between two pods.
|
|
246
|
+
* @param {string} from
|
|
247
|
+
* @param {string} to
|
|
248
|
+
* @param {number} level - Trust level [0.0, 1.0]
|
|
249
|
+
*/
|
|
250
|
+
setTrust(from, to, level) {
|
|
251
|
+
this.addPod(from);
|
|
252
|
+
this.addPod(to);
|
|
253
|
+
this.#matrix.get(from).set(to, Math.max(0, Math.min(1, level)));
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
/**
|
|
257
|
+
* Get trust level between two pods.
|
|
258
|
+
* @param {string} from
|
|
259
|
+
* @param {string} to
|
|
260
|
+
* @returns {number} Trust level (0 if not set)
|
|
261
|
+
*/
|
|
262
|
+
getTrust(from, to) {
|
|
263
|
+
return this.#matrix.get(from)?.get(to) ?? 0;
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
/** Get the ordered list of pod IDs (rows/columns). */
|
|
267
|
+
getPodIds() {
|
|
268
|
+
return [...this.#podIds];
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
/** Get the number of pods. */
|
|
272
|
+
get size() {
|
|
273
|
+
return this.#podIds.length;
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
/**
|
|
277
|
+
* Export as a 2D number array [rows][cols].
|
|
278
|
+
* @returns {number[][]}
|
|
279
|
+
*/
|
|
280
|
+
toMatrix() {
|
|
281
|
+
return this.#podIds.map(from =>
|
|
282
|
+
this.#podIds.map(to => this.getTrust(from, to))
|
|
283
|
+
);
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
/**
|
|
287
|
+
* Get row (outbound trust from a pod).
|
|
288
|
+
* @param {string} podId
|
|
289
|
+
* @returns {Object} { podId: level }
|
|
290
|
+
*/
|
|
291
|
+
getRow(podId) {
|
|
292
|
+
const row = {};
|
|
293
|
+
for (const to of this.#podIds) {
|
|
294
|
+
row[to] = this.getTrust(podId, to);
|
|
295
|
+
}
|
|
296
|
+
return row;
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
/**
|
|
300
|
+
* Get column (inbound trust to a pod).
|
|
301
|
+
* @param {string} podId
|
|
302
|
+
* @returns {Object} { podId: level }
|
|
303
|
+
*/
|
|
304
|
+
getColumn(podId) {
|
|
305
|
+
const col = {};
|
|
306
|
+
for (const from of this.#podIds) {
|
|
307
|
+
col[from] = this.getTrust(from, podId);
|
|
308
|
+
}
|
|
309
|
+
return col;
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
/**
|
|
313
|
+
* Compute average inbound trust for a pod.
|
|
314
|
+
* @param {string} podId
|
|
315
|
+
* @returns {number}
|
|
316
|
+
*/
|
|
317
|
+
averageInbound(podId) {
|
|
318
|
+
const others = this.#podIds.filter(p => p !== podId);
|
|
319
|
+
if (others.length === 0) return 0;
|
|
320
|
+
const sum = others.reduce((s, from) => s + this.getTrust(from, podId), 0);
|
|
321
|
+
return sum / others.length;
|
|
322
|
+
}
|
|
323
|
+
|
|
324
|
+
toJSON() {
|
|
325
|
+
return {
|
|
326
|
+
podIds: [...this.#podIds],
|
|
327
|
+
matrix: this.toMatrix(),
|
|
328
|
+
};
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
static fromJSON(data) {
|
|
332
|
+
const hm = new TrustHeatmap();
|
|
333
|
+
for (const id of data.podIds) hm.addPod(id);
|
|
334
|
+
for (let i = 0; i < data.podIds.length; i++) {
|
|
335
|
+
for (let j = 0; j < data.podIds.length; j++) {
|
|
336
|
+
if (data.matrix[i][j] > 0) {
|
|
337
|
+
hm.setTrust(data.podIds[i], data.podIds[j], data.matrix[i][j]);
|
|
338
|
+
}
|
|
339
|
+
}
|
|
340
|
+
}
|
|
341
|
+
return hm;
|
|
342
|
+
}
|
|
343
|
+
}
|
|
344
|
+
|
|
345
|
+
// ---------------------------------------------------------------------------
|
|
346
|
+
// TopologySnapshot
|
|
347
|
+
// ---------------------------------------------------------------------------
|
|
348
|
+
|
|
349
|
+
/**
|
|
350
|
+
* Captures current mesh topology state at a point in time.
|
|
351
|
+
*/
|
|
352
|
+
export class TopologySnapshot {
|
|
353
|
+
/**
|
|
354
|
+
* @param {object} opts
|
|
355
|
+
* @param {string} [opts.id]
|
|
356
|
+
* @param {number} [opts.timestamp]
|
|
357
|
+
* @param {{ id: string, label?: string, type?: string, status?: string, x?: number, y?: number }[]} [opts.nodes]
|
|
358
|
+
* @param {{ from: string, to: string, transport?: string, latency?: number, status?: string }[]} [opts.links]
|
|
359
|
+
* @param {Object} [opts.metadata]
|
|
360
|
+
*/
|
|
361
|
+
constructor({
|
|
362
|
+
id,
|
|
363
|
+
timestamp,
|
|
364
|
+
nodes = [],
|
|
365
|
+
links = [],
|
|
366
|
+
metadata = {},
|
|
367
|
+
} = {}) {
|
|
368
|
+
this.id = id || `topo_${Date.now()}`;
|
|
369
|
+
this.timestamp = timestamp || Date.now();
|
|
370
|
+
this.nodes = nodes.map(n => ({ ...n }));
|
|
371
|
+
this.links = links.map(l => ({ ...l }));
|
|
372
|
+
this.metadata = { ...metadata };
|
|
373
|
+
}
|
|
374
|
+
|
|
375
|
+
/** Number of nodes. */
|
|
376
|
+
get nodeCount() {
|
|
377
|
+
return this.nodes.length;
|
|
378
|
+
}
|
|
379
|
+
|
|
380
|
+
/** Number of links. */
|
|
381
|
+
get linkCount() {
|
|
382
|
+
return this.links.length;
|
|
383
|
+
}
|
|
384
|
+
|
|
385
|
+
/** Find node by ID. */
|
|
386
|
+
getNode(id) {
|
|
387
|
+
return this.nodes.find(n => n.id === id) || null;
|
|
388
|
+
}
|
|
389
|
+
|
|
390
|
+
/** Get links for a specific node. */
|
|
391
|
+
getLinksFor(nodeId) {
|
|
392
|
+
return this.links.filter(l => l.from === nodeId || l.to === nodeId);
|
|
393
|
+
}
|
|
394
|
+
|
|
395
|
+
/** Get the set of unique transport types in use. */
|
|
396
|
+
getTransportTypes() {
|
|
397
|
+
const types = new Set();
|
|
398
|
+
for (const l of this.links) {
|
|
399
|
+
if (l.transport) types.add(l.transport);
|
|
400
|
+
}
|
|
401
|
+
return [...types];
|
|
402
|
+
}
|
|
403
|
+
|
|
404
|
+
/** Compute average latency across all links. */
|
|
405
|
+
averageLatency() {
|
|
406
|
+
const withLatency = this.links.filter(l => typeof l.latency === 'number');
|
|
407
|
+
if (withLatency.length === 0) return 0;
|
|
408
|
+
return withLatency.reduce((s, l) => s + l.latency, 0) / withLatency.length;
|
|
409
|
+
}
|
|
410
|
+
|
|
411
|
+
/** Get nodes with a specific status. */
|
|
412
|
+
getNodesByStatus(status) {
|
|
413
|
+
return this.nodes.filter(n => n.status === status);
|
|
414
|
+
}
|
|
415
|
+
|
|
416
|
+
toJSON() {
|
|
417
|
+
return {
|
|
418
|
+
id: this.id,
|
|
419
|
+
timestamp: this.timestamp,
|
|
420
|
+
nodes: this.nodes.map(n => ({ ...n })),
|
|
421
|
+
links: this.links.map(l => ({ ...l })),
|
|
422
|
+
metadata: { ...this.metadata },
|
|
423
|
+
};
|
|
424
|
+
}
|
|
425
|
+
|
|
426
|
+
static fromJSON(data) {
|
|
427
|
+
return new TopologySnapshot(data);
|
|
428
|
+
}
|
|
429
|
+
}
|
|
430
|
+
|
|
431
|
+
// ---------------------------------------------------------------------------
|
|
432
|
+
// TopologyLayout
|
|
433
|
+
// ---------------------------------------------------------------------------
|
|
434
|
+
|
|
435
|
+
/**
|
|
436
|
+
* Assigns positions to topology nodes using layout algorithms.
|
|
437
|
+
*/
|
|
438
|
+
export class TopologyLayout {
|
|
439
|
+
/** @type {number} */
|
|
440
|
+
#width;
|
|
441
|
+
/** @type {number} */
|
|
442
|
+
#height;
|
|
443
|
+
|
|
444
|
+
/**
|
|
445
|
+
* @param {object} [opts]
|
|
446
|
+
* @param {number} [opts.width=800]
|
|
447
|
+
* @param {number} [opts.height=600]
|
|
448
|
+
*/
|
|
449
|
+
constructor({ width = 800, height = 600 } = {}) {
|
|
450
|
+
this.#width = width;
|
|
451
|
+
this.#height = height;
|
|
452
|
+
}
|
|
453
|
+
|
|
454
|
+
get width() { return this.#width; }
|
|
455
|
+
get height() { return this.#height; }
|
|
456
|
+
|
|
457
|
+
/**
|
|
458
|
+
* Apply circular layout: nodes evenly distributed on a circle.
|
|
459
|
+
* @param {TopologySnapshot} snapshot
|
|
460
|
+
* @returns {TopologySnapshot} New snapshot with positions
|
|
461
|
+
*/
|
|
462
|
+
circular(snapshot) {
|
|
463
|
+
const cx = this.#width / 2;
|
|
464
|
+
const cy = this.#height / 2;
|
|
465
|
+
const radius = Math.min(cx, cy) * 0.8;
|
|
466
|
+
const n = snapshot.nodes.length;
|
|
467
|
+
|
|
468
|
+
const positioned = snapshot.nodes.map((node, i) => ({
|
|
469
|
+
...node,
|
|
470
|
+
x: cx + radius * Math.cos((2 * Math.PI * i) / n),
|
|
471
|
+
y: cy + radius * Math.sin((2 * Math.PI * i) / n),
|
|
472
|
+
}));
|
|
473
|
+
|
|
474
|
+
return new TopologySnapshot({
|
|
475
|
+
...snapshot.toJSON(),
|
|
476
|
+
nodes: positioned,
|
|
477
|
+
});
|
|
478
|
+
}
|
|
479
|
+
|
|
480
|
+
/**
|
|
481
|
+
* Apply grid layout: nodes placed in rows and columns.
|
|
482
|
+
* @param {TopologySnapshot} snapshot
|
|
483
|
+
* @param {number} [cols] - Columns (auto-computed if omitted)
|
|
484
|
+
* @returns {TopologySnapshot} New snapshot with positions
|
|
485
|
+
*/
|
|
486
|
+
grid(snapshot, cols) {
|
|
487
|
+
const n = snapshot.nodes.length;
|
|
488
|
+
const c = cols || Math.ceil(Math.sqrt(n));
|
|
489
|
+
const cellW = this.#width / (c + 1);
|
|
490
|
+
const cellH = this.#height / (Math.ceil(n / c) + 1);
|
|
491
|
+
|
|
492
|
+
const positioned = snapshot.nodes.map((node, i) => ({
|
|
493
|
+
...node,
|
|
494
|
+
x: ((i % c) + 1) * cellW,
|
|
495
|
+
y: (Math.floor(i / c) + 1) * cellH,
|
|
496
|
+
}));
|
|
497
|
+
|
|
498
|
+
return new TopologySnapshot({
|
|
499
|
+
...snapshot.toJSON(),
|
|
500
|
+
nodes: positioned,
|
|
501
|
+
});
|
|
502
|
+
}
|
|
503
|
+
|
|
504
|
+
/**
|
|
505
|
+
* Apply hierarchical layout: nodes arranged by connection depth.
|
|
506
|
+
* Root nodes (no incoming links) are at the top.
|
|
507
|
+
* @param {TopologySnapshot} snapshot
|
|
508
|
+
* @returns {TopologySnapshot}
|
|
509
|
+
*/
|
|
510
|
+
hierarchical(snapshot) {
|
|
511
|
+
// Compute depth for each node via BFS from roots
|
|
512
|
+
const inbound = new Map();
|
|
513
|
+
for (const l of snapshot.links) {
|
|
514
|
+
if (!inbound.has(l.to)) inbound.set(l.to, []);
|
|
515
|
+
inbound.get(l.to).push(l.from);
|
|
516
|
+
}
|
|
517
|
+
|
|
518
|
+
// Roots: nodes with no inbound links
|
|
519
|
+
const roots = snapshot.nodes.filter(n => !inbound.has(n.id) || inbound.get(n.id).length === 0);
|
|
520
|
+
const depths = new Map();
|
|
521
|
+
const queue = roots.map(r => r.id);
|
|
522
|
+
for (const r of queue) depths.set(r, 0);
|
|
523
|
+
|
|
524
|
+
let head = 0;
|
|
525
|
+
while (head < queue.length) {
|
|
526
|
+
const current = queue[head++];
|
|
527
|
+
const d = depths.get(current);
|
|
528
|
+
for (const l of snapshot.links.filter(l => l.from === current)) {
|
|
529
|
+
if (!depths.has(l.to)) {
|
|
530
|
+
depths.set(l.to, d + 1);
|
|
531
|
+
queue.push(l.to);
|
|
532
|
+
}
|
|
533
|
+
}
|
|
534
|
+
}
|
|
535
|
+
|
|
536
|
+
// Assign depth 0 to any unvisited nodes
|
|
537
|
+
for (const n of snapshot.nodes) {
|
|
538
|
+
if (!depths.has(n.id)) depths.set(n.id, 0);
|
|
539
|
+
}
|
|
540
|
+
|
|
541
|
+
const maxDepth = Math.max(...depths.values(), 0);
|
|
542
|
+
const levelHeight = this.#height / (maxDepth + 2);
|
|
543
|
+
|
|
544
|
+
// Group by level
|
|
545
|
+
const levels = new Map();
|
|
546
|
+
for (const [id, d] of depths) {
|
|
547
|
+
if (!levels.has(d)) levels.set(d, []);
|
|
548
|
+
levels.get(d).push(id);
|
|
549
|
+
}
|
|
550
|
+
|
|
551
|
+
const positioned = snapshot.nodes.map(node => {
|
|
552
|
+
const d = depths.get(node.id);
|
|
553
|
+
const levelNodes = levels.get(d);
|
|
554
|
+
const idx = levelNodes.indexOf(node.id);
|
|
555
|
+
const levelWidth = this.#width / (levelNodes.length + 1);
|
|
556
|
+
return {
|
|
557
|
+
...node,
|
|
558
|
+
x: (idx + 1) * levelWidth,
|
|
559
|
+
y: (d + 1) * levelHeight,
|
|
560
|
+
};
|
|
561
|
+
});
|
|
562
|
+
|
|
563
|
+
return new TopologySnapshot({
|
|
564
|
+
...snapshot.toJSON(),
|
|
565
|
+
nodes: positioned,
|
|
566
|
+
});
|
|
567
|
+
}
|
|
568
|
+
}
|
|
569
|
+
|
|
570
|
+
// ---------------------------------------------------------------------------
|
|
571
|
+
// TopologyDiff
|
|
572
|
+
// ---------------------------------------------------------------------------
|
|
573
|
+
|
|
574
|
+
/**
|
|
575
|
+
* Computes changes between two topology snapshots.
|
|
576
|
+
*/
|
|
577
|
+
export class TopologyDiff {
|
|
578
|
+
/**
|
|
579
|
+
* @param {object} opts
|
|
580
|
+
* @param {{ id: string }[]} [opts.addedNodes]
|
|
581
|
+
* @param {{ id: string }[]} [opts.removedNodes]
|
|
582
|
+
* @param {{ id: string, changes: Object }[]} [opts.changedNodes]
|
|
583
|
+
* @param {{ from: string, to: string }[]} [opts.addedLinks]
|
|
584
|
+
* @param {{ from: string, to: string }[]} [opts.removedLinks]
|
|
585
|
+
* @param {{ from: string, to: string, changes: Object }[]} [opts.changedLinks]
|
|
586
|
+
* @param {number} [opts.timestamp]
|
|
587
|
+
*/
|
|
588
|
+
constructor({
|
|
589
|
+
addedNodes = [],
|
|
590
|
+
removedNodes = [],
|
|
591
|
+
changedNodes = [],
|
|
592
|
+
addedLinks = [],
|
|
593
|
+
removedLinks = [],
|
|
594
|
+
changedLinks = [],
|
|
595
|
+
timestamp,
|
|
596
|
+
} = {}) {
|
|
597
|
+
this.addedNodes = addedNodes;
|
|
598
|
+
this.removedNodes = removedNodes;
|
|
599
|
+
this.changedNodes = changedNodes;
|
|
600
|
+
this.addedLinks = addedLinks;
|
|
601
|
+
this.removedLinks = removedLinks;
|
|
602
|
+
this.changedLinks = changedLinks;
|
|
603
|
+
this.timestamp = timestamp || Date.now();
|
|
604
|
+
}
|
|
605
|
+
|
|
606
|
+
/** True if no changes exist. */
|
|
607
|
+
get isEmpty() {
|
|
608
|
+
return this.addedNodes.length === 0 &&
|
|
609
|
+
this.removedNodes.length === 0 &&
|
|
610
|
+
this.changedNodes.length === 0 &&
|
|
611
|
+
this.addedLinks.length === 0 &&
|
|
612
|
+
this.removedLinks.length === 0 &&
|
|
613
|
+
this.changedLinks.length === 0;
|
|
614
|
+
}
|
|
615
|
+
|
|
616
|
+
/** Total number of changes. */
|
|
617
|
+
get changeCount() {
|
|
618
|
+
return this.addedNodes.length + this.removedNodes.length +
|
|
619
|
+
this.changedNodes.length + this.addedLinks.length +
|
|
620
|
+
this.removedLinks.length + this.changedLinks.length;
|
|
621
|
+
}
|
|
622
|
+
|
|
623
|
+
/**
|
|
624
|
+
* Compute diff between two snapshots.
|
|
625
|
+
* @param {TopologySnapshot} before
|
|
626
|
+
* @param {TopologySnapshot} after
|
|
627
|
+
* @returns {TopologyDiff}
|
|
628
|
+
*/
|
|
629
|
+
static compute(before, after) {
|
|
630
|
+
const beforeNodeIds = new Set(before.nodes.map(n => n.id));
|
|
631
|
+
const afterNodeIds = new Set(after.nodes.map(n => n.id));
|
|
632
|
+
|
|
633
|
+
const addedNodes = after.nodes.filter(n => !beforeNodeIds.has(n.id));
|
|
634
|
+
const removedNodes = before.nodes.filter(n => !afterNodeIds.has(n.id));
|
|
635
|
+
|
|
636
|
+
const changedNodes = [];
|
|
637
|
+
for (const an of after.nodes) {
|
|
638
|
+
if (!beforeNodeIds.has(an.id)) continue;
|
|
639
|
+
const bn = before.nodes.find(n => n.id === an.id);
|
|
640
|
+
const changes = {};
|
|
641
|
+
for (const key of Object.keys(an)) {
|
|
642
|
+
if (key === 'id') continue;
|
|
643
|
+
if (JSON.stringify(an[key]) !== JSON.stringify(bn[key])) {
|
|
644
|
+
changes[key] = { from: bn[key], to: an[key] };
|
|
645
|
+
}
|
|
646
|
+
}
|
|
647
|
+
if (Object.keys(changes).length > 0) {
|
|
648
|
+
changedNodes.push({ id: an.id, changes });
|
|
649
|
+
}
|
|
650
|
+
}
|
|
651
|
+
|
|
652
|
+
// Link comparison (identify by from+to)
|
|
653
|
+
const linkKey = l => `${l.from}->${l.to}`;
|
|
654
|
+
const beforeLinks = new Map(before.links.map(l => [linkKey(l), l]));
|
|
655
|
+
const afterLinks = new Map(after.links.map(l => [linkKey(l), l]));
|
|
656
|
+
|
|
657
|
+
const addedLinks = after.links.filter(l => !beforeLinks.has(linkKey(l)));
|
|
658
|
+
const removedLinks = before.links.filter(l => !afterLinks.has(linkKey(l)));
|
|
659
|
+
|
|
660
|
+
const changedLinks = [];
|
|
661
|
+
for (const [key, al] of afterLinks) {
|
|
662
|
+
const bl = beforeLinks.get(key);
|
|
663
|
+
if (!bl) continue;
|
|
664
|
+
const changes = {};
|
|
665
|
+
for (const k of Object.keys(al)) {
|
|
666
|
+
if (k === 'from' || k === 'to') continue;
|
|
667
|
+
if (JSON.stringify(al[k]) !== JSON.stringify(bl[k])) {
|
|
668
|
+
changes[k] = { from: bl[k], to: al[k] };
|
|
669
|
+
}
|
|
670
|
+
}
|
|
671
|
+
if (Object.keys(changes).length > 0) {
|
|
672
|
+
changedLinks.push({ from: al.from, to: al.to, changes });
|
|
673
|
+
}
|
|
674
|
+
}
|
|
675
|
+
|
|
676
|
+
return new TopologyDiff({
|
|
677
|
+
addedNodes, removedNodes, changedNodes,
|
|
678
|
+
addedLinks, removedLinks, changedLinks,
|
|
679
|
+
});
|
|
680
|
+
}
|
|
681
|
+
|
|
682
|
+
toJSON() {
|
|
683
|
+
return {
|
|
684
|
+
addedNodes: this.addedNodes,
|
|
685
|
+
removedNodes: this.removedNodes,
|
|
686
|
+
changedNodes: this.changedNodes,
|
|
687
|
+
addedLinks: this.addedLinks,
|
|
688
|
+
removedLinks: this.removedLinks,
|
|
689
|
+
changedLinks: this.changedLinks,
|
|
690
|
+
timestamp: this.timestamp,
|
|
691
|
+
};
|
|
692
|
+
}
|
|
693
|
+
|
|
694
|
+
static fromJSON(data) {
|
|
695
|
+
return new TopologyDiff(data);
|
|
696
|
+
}
|
|
697
|
+
}
|
|
698
|
+
|
|
699
|
+
// ---------------------------------------------------------------------------
|
|
700
|
+
// VisualizationExporter
|
|
701
|
+
// ---------------------------------------------------------------------------
|
|
702
|
+
|
|
703
|
+
/**
|
|
704
|
+
* Serializes visualization layouts for external renderers.
|
|
705
|
+
*/
|
|
706
|
+
// ---------------------------------------------------------------------------
|
|
707
|
+
// TopologyBroadcaster
|
|
708
|
+
// ---------------------------------------------------------------------------
|
|
709
|
+
|
|
710
|
+
/**
|
|
711
|
+
* Broadcasts and receives topology snapshots and diffs over the mesh wire protocol.
|
|
712
|
+
*/
|
|
713
|
+
export class TopologyBroadcaster {
|
|
714
|
+
/** @type {string} */
|
|
715
|
+
#localPodId;
|
|
716
|
+
|
|
717
|
+
/** @type {Function} (targetId, msg) => void */
|
|
718
|
+
#sendFn;
|
|
719
|
+
|
|
720
|
+
/** @type {Function[]} */
|
|
721
|
+
#snapshotListeners = [];
|
|
722
|
+
|
|
723
|
+
/** @type {Function[]} */
|
|
724
|
+
#diffListeners = [];
|
|
725
|
+
|
|
726
|
+
/**
|
|
727
|
+
* @param {object} opts
|
|
728
|
+
* @param {string} opts.localPodId
|
|
729
|
+
* @param {Function} opts.sendFn - Send function: (targetId, msg) => {}
|
|
730
|
+
*/
|
|
731
|
+
constructor({ localPodId, sendFn }) {
|
|
732
|
+
if (!localPodId || typeof localPodId !== 'string') {
|
|
733
|
+
throw new Error('localPodId is required and must be a non-empty string');
|
|
734
|
+
}
|
|
735
|
+
if (typeof sendFn !== 'function') {
|
|
736
|
+
throw new Error('sendFn is required and must be a function');
|
|
737
|
+
}
|
|
738
|
+
this.#localPodId = localPodId;
|
|
739
|
+
this.#sendFn = sendFn;
|
|
740
|
+
}
|
|
741
|
+
|
|
742
|
+
/**
|
|
743
|
+
* Handle an incoming wire message.
|
|
744
|
+
* @param {string} fromId - Sender pod ID
|
|
745
|
+
* @param {object} msg - Message with `type` field
|
|
746
|
+
*/
|
|
747
|
+
handleMessage(fromId, msg) {
|
|
748
|
+
switch (msg.type) {
|
|
749
|
+
case TOPOLOGY_SNAPSHOT: {
|
|
750
|
+
if (!msg.snapshot) break;
|
|
751
|
+
const snapshot = TopologySnapshot.fromJSON(msg.snapshot);
|
|
752
|
+
for (const cb of this.#snapshotListeners) cb(fromId, snapshot);
|
|
753
|
+
break;
|
|
754
|
+
}
|
|
755
|
+
|
|
756
|
+
case TOPOLOGY_DIFF: {
|
|
757
|
+
if (!msg.diff) break;
|
|
758
|
+
const diff = TopologyDiff.fromJSON(msg.diff);
|
|
759
|
+
for (const cb of this.#diffListeners) cb(fromId, diff);
|
|
760
|
+
break;
|
|
761
|
+
}
|
|
762
|
+
|
|
763
|
+
default:
|
|
764
|
+
break;
|
|
765
|
+
}
|
|
766
|
+
}
|
|
767
|
+
|
|
768
|
+
/**
|
|
769
|
+
* Broadcast a topology snapshot to listed peers.
|
|
770
|
+
* @param {string[]} peerIds
|
|
771
|
+
* @param {TopologySnapshot} snapshot
|
|
772
|
+
*/
|
|
773
|
+
broadcastSnapshot(peerIds, snapshot) {
|
|
774
|
+
const payload = { type: TOPOLOGY_SNAPSHOT, snapshot: snapshot.toJSON() };
|
|
775
|
+
for (const peerId of peerIds) {
|
|
776
|
+
this.#sendFn(peerId, payload);
|
|
777
|
+
}
|
|
778
|
+
}
|
|
779
|
+
|
|
780
|
+
/**
|
|
781
|
+
* Broadcast a topology diff to listed peers.
|
|
782
|
+
* @param {string[]} peerIds
|
|
783
|
+
* @param {TopologyDiff} diff
|
|
784
|
+
*/
|
|
785
|
+
broadcastDiff(peerIds, diff) {
|
|
786
|
+
const payload = { type: TOPOLOGY_DIFF, diff: diff.toJSON() };
|
|
787
|
+
for (const peerId of peerIds) {
|
|
788
|
+
this.#sendFn(peerId, payload);
|
|
789
|
+
}
|
|
790
|
+
}
|
|
791
|
+
|
|
792
|
+
/**
|
|
793
|
+
* Register a listener for incoming topology snapshots.
|
|
794
|
+
* @param {Function} cb - (fromId, TopologySnapshot) => void
|
|
795
|
+
*/
|
|
796
|
+
onSnapshot(cb) {
|
|
797
|
+
this.#snapshotListeners.push(cb);
|
|
798
|
+
}
|
|
799
|
+
|
|
800
|
+
/**
|
|
801
|
+
* Remove a snapshot listener.
|
|
802
|
+
* @param {Function} cb
|
|
803
|
+
* @returns {boolean} True if the listener was found and removed
|
|
804
|
+
*/
|
|
805
|
+
offSnapshot(cb) {
|
|
806
|
+
const idx = this.#snapshotListeners.indexOf(cb);
|
|
807
|
+
if (idx !== -1) { this.#snapshotListeners.splice(idx, 1); return true; }
|
|
808
|
+
return false;
|
|
809
|
+
}
|
|
810
|
+
|
|
811
|
+
/**
|
|
812
|
+
* Register a listener for incoming topology diffs.
|
|
813
|
+
* @param {Function} cb - (fromId, TopologyDiff) => void
|
|
814
|
+
*/
|
|
815
|
+
onDiff(cb) {
|
|
816
|
+
this.#diffListeners.push(cb);
|
|
817
|
+
}
|
|
818
|
+
|
|
819
|
+
/**
|
|
820
|
+
* Remove a diff listener.
|
|
821
|
+
* @param {Function} cb
|
|
822
|
+
* @returns {boolean} True if the listener was found and removed
|
|
823
|
+
*/
|
|
824
|
+
offDiff(cb) {
|
|
825
|
+
const idx = this.#diffListeners.indexOf(cb);
|
|
826
|
+
if (idx !== -1) { this.#diffListeners.splice(idx, 1); return true; }
|
|
827
|
+
return false;
|
|
828
|
+
}
|
|
829
|
+
}
|
|
830
|
+
|
|
831
|
+
// ---------------------------------------------------------------------------
|
|
832
|
+
// VisualizationExporter
|
|
833
|
+
// ---------------------------------------------------------------------------
|
|
834
|
+
|
|
835
|
+
/**
|
|
836
|
+
* Serializes visualization layouts for external renderers.
|
|
837
|
+
*/
|
|
838
|
+
export class VisualizationExporter {
|
|
839
|
+
/**
|
|
840
|
+
* Export a TrustGraphLayout as a node-link JSON format.
|
|
841
|
+
* @param {TrustGraphLayout} layout
|
|
842
|
+
* @returns {Object}
|
|
843
|
+
*/
|
|
844
|
+
exportTrustGraph(layout) {
|
|
845
|
+
return {
|
|
846
|
+
type: 'trust-graph',
|
|
847
|
+
...layout.toJSON(),
|
|
848
|
+
exportedAt: Date.now(),
|
|
849
|
+
};
|
|
850
|
+
}
|
|
851
|
+
|
|
852
|
+
/**
|
|
853
|
+
* Export a TrustHeatmap as a matrix format.
|
|
854
|
+
* @param {TrustHeatmap} heatmap
|
|
855
|
+
* @returns {Object}
|
|
856
|
+
*/
|
|
857
|
+
exportHeatmap(heatmap) {
|
|
858
|
+
return {
|
|
859
|
+
type: 'trust-heatmap',
|
|
860
|
+
...heatmap.toJSON(),
|
|
861
|
+
exportedAt: Date.now(),
|
|
862
|
+
};
|
|
863
|
+
}
|
|
864
|
+
|
|
865
|
+
/**
|
|
866
|
+
* Export a TopologySnapshot as renderable data.
|
|
867
|
+
* @param {TopologySnapshot} snapshot
|
|
868
|
+
* @param {TopologyLayout} [layoutEngine]
|
|
869
|
+
* @param {string} [layoutType='circular']
|
|
870
|
+
* @returns {Object}
|
|
871
|
+
*/
|
|
872
|
+
exportTopology(snapshot, layoutEngine, layoutType = 'circular') {
|
|
873
|
+
let positioned = snapshot;
|
|
874
|
+
if (layoutEngine) {
|
|
875
|
+
switch (layoutType) {
|
|
876
|
+
case 'circular': positioned = layoutEngine.circular(snapshot); break;
|
|
877
|
+
case 'grid': positioned = layoutEngine.grid(snapshot); break;
|
|
878
|
+
case 'hierarchical': positioned = layoutEngine.hierarchical(snapshot); break;
|
|
879
|
+
default: positioned = layoutEngine.circular(snapshot);
|
|
880
|
+
}
|
|
881
|
+
}
|
|
882
|
+
return {
|
|
883
|
+
type: 'topology',
|
|
884
|
+
...positioned.toJSON(),
|
|
885
|
+
layout: layoutType,
|
|
886
|
+
exportedAt: Date.now(),
|
|
887
|
+
};
|
|
888
|
+
}
|
|
889
|
+
|
|
890
|
+
/**
|
|
891
|
+
* Export a TopologyDiff as renderable change data.
|
|
892
|
+
* @param {TopologyDiff} diff
|
|
893
|
+
* @returns {Object}
|
|
894
|
+
*/
|
|
895
|
+
exportDiff(diff) {
|
|
896
|
+
return {
|
|
897
|
+
type: 'topology-diff',
|
|
898
|
+
...diff.toJSON(),
|
|
899
|
+
exportedAt: Date.now(),
|
|
900
|
+
};
|
|
901
|
+
}
|
|
902
|
+
}
|