@peers-app/peers-ui 0.33.20 → 0.33.21
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/dist/graph/ring-layout.d.ts +62 -0
- package/dist/graph/ring-layout.js +100 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/dist/screens/network-viewer/graph/edge-route.d.ts +33 -0
- package/dist/screens/network-viewer/graph/edge-route.js +133 -0
- package/dist/screens/network-viewer/graph/label-placement.d.ts +54 -0
- package/dist/screens/network-viewer/graph/label-placement.js +134 -0
- package/dist/screens/network-viewer/graph/network-graph-card.d.ts +30 -0
- package/dist/screens/network-viewer/graph/network-graph-card.js +91 -0
- package/dist/screens/network-viewer/graph/network-graph-controls.d.ts +26 -0
- package/dist/screens/network-viewer/graph/network-graph-controls.js +80 -0
- package/dist/screens/network-viewer/graph/network-graph-model.d.ts +282 -0
- package/dist/screens/network-viewer/graph/network-graph-model.js +553 -0
- package/dist/screens/network-viewer/graph/network-graph-settings.d.ts +27 -0
- package/dist/screens/network-viewer/graph/network-graph-settings.js +81 -0
- package/dist/screens/network-viewer/graph/network-graph.d.ts +17 -0
- package/dist/screens/network-viewer/graph/network-graph.js +419 -0
- package/dist/screens/network-viewer/network-viewer.d.ts +3 -2
- package/dist/screens/network-viewer/network-viewer.js +65 -8
- package/dist/tabs-layout/linger.d.ts +75 -0
- package/dist/tabs-layout/linger.js +121 -0
- package/dist/tabs-layout/network-status-details.d.ts +27 -0
- package/dist/tabs-layout/network-status-details.js +203 -0
- package/dist/tabs-layout/network-status-format.d.ts +22 -0
- package/dist/tabs-layout/network-status-format.js +58 -0
- package/dist/tabs-layout/network-status-indicator.d.ts +1 -13
- package/dist/tabs-layout/network-status-indicator.js +27 -133
- package/dist/tabs-layout/network-status-model.d.ts +71 -0
- package/dist/tabs-layout/network-status-model.js +155 -0
- package/dist/tabs-layout/use-network-details-data.d.ts +28 -0
- package/dist/tabs-layout/use-network-details-data.js +90 -0
- package/dist/tabs-layout/use-syncing-look.d.ts +18 -0
- package/dist/tabs-layout/use-syncing-look.js +50 -0
- package/dist/theme.css +635 -0
- package/package.json +3 -3
|
@@ -0,0 +1,553 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.IDLE_BYTES_PER_SECOND = exports.LatencyHistory = exports.FAILING_ERROR_RATE = exports.SLOW_ERROR_RATE = exports.FAILING_LATENCY_MS = exports.SLOW_LATENCY_MS = void 0;
|
|
4
|
+
exports.shortId = shortId;
|
|
5
|
+
exports.isServiceConnection = isServiceConnection;
|
|
6
|
+
exports.isServiceDevice = isServiceDevice;
|
|
7
|
+
exports.transportOf = transportOf;
|
|
8
|
+
exports.buildNetworkGraph = buildNetworkGraph;
|
|
9
|
+
exports.neighborsFor = neighborsFor;
|
|
10
|
+
exports.offlineNodes = offlineNodes;
|
|
11
|
+
exports.viewEdge = viewEdge;
|
|
12
|
+
exports.edgeHealth = edgeHealth;
|
|
13
|
+
exports.trafficStep = trafficStep;
|
|
14
|
+
exports.formatRate = formatRate;
|
|
15
|
+
exports.formatLatency = formatLatency;
|
|
16
|
+
exports.formatErrors = formatErrors;
|
|
17
|
+
exports.trustName = trustName;
|
|
18
|
+
exports.ownerLabel = ownerLabel;
|
|
19
|
+
exports.graphName = graphName;
|
|
20
|
+
exports.routeOf = routeOf;
|
|
21
|
+
exports.syncStateWord = syncStateWord;
|
|
22
|
+
exports.listNames = listNames;
|
|
23
|
+
exports.describeChoices = describeChoices;
|
|
24
|
+
exports.syncSummary = syncSummary;
|
|
25
|
+
const peers_sdk_1 = require("@peers-app/peers-sdk");
|
|
26
|
+
const BYTES_PER_MB = 1024 * 1024;
|
|
27
|
+
/** A device id cut down to something that fits a label. */
|
|
28
|
+
function shortId(deviceId) {
|
|
29
|
+
return deviceId.length > 8 ? `…${deviceId.slice(-6)}` : deviceId;
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* Whether a connection is to a Peers Services relay (peers.app) and not to a device that syncs:
|
|
33
|
+
* it shares no group and its address is a web address.
|
|
34
|
+
*/
|
|
35
|
+
function isServiceConnection(connection) {
|
|
36
|
+
return (connection.sharedGroups.length === 0 && /^https?:\/\//i.test(connection.connectionAddress ?? ""));
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* Whether a known, unconnected device is a Peers Services relay (peers.app). A relay reports its
|
|
40
|
+
* web address as its user name, and each Services restart brings a new device id, so a long-used
|
|
41
|
+
* device knows dozens of relays that are gone for good. The graph leaves them off its offline
|
|
42
|
+
* ring; the List's Disconnected Devices table still has them.
|
|
43
|
+
*/
|
|
44
|
+
function isServiceDevice(device) {
|
|
45
|
+
return /^https?:\/\//i.test(device.userName ?? "");
|
|
46
|
+
}
|
|
47
|
+
function hostOf(address) {
|
|
48
|
+
try {
|
|
49
|
+
return address ? new URL(address).host || undefined : undefined;
|
|
50
|
+
}
|
|
51
|
+
catch {
|
|
52
|
+
return undefined;
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* How a connection travels, from its address: WebRTC, WebSocket or WebTransport.
|
|
57
|
+
*
|
|
58
|
+
* @param address - The connection's address, such as `wrtc://…` or `wss://…`.
|
|
59
|
+
* @returns A label, or undefined when the address says nothing.
|
|
60
|
+
*/
|
|
61
|
+
function transportOf(address) {
|
|
62
|
+
const scheme = /^([a-z][a-z0-9+.-]*):/i.exec(address ?? "")?.[1]?.toLowerCase();
|
|
63
|
+
if (!scheme)
|
|
64
|
+
return undefined;
|
|
65
|
+
if (scheme === "wrtc" || scheme === "webrtc")
|
|
66
|
+
return "WebRTC";
|
|
67
|
+
if (scheme === "ws" || scheme === "wss" || scheme === "http" || scheme === "https") {
|
|
68
|
+
return "WebSocket";
|
|
69
|
+
}
|
|
70
|
+
if (scheme === "wt" || scheme === "webtransport")
|
|
71
|
+
return "WebTransport";
|
|
72
|
+
return scheme;
|
|
73
|
+
}
|
|
74
|
+
/** The topology groups, or the same shape built from group sync status when there's no topology. */
|
|
75
|
+
function topologyGroups(input) {
|
|
76
|
+
if (input.topology)
|
|
77
|
+
return input.topology.groups;
|
|
78
|
+
return input.groupSync.map((group) => ({
|
|
79
|
+
groupId: group.groupId,
|
|
80
|
+
groupName: group.groupName,
|
|
81
|
+
connections: group.connectedDevices,
|
|
82
|
+
preferredDeviceIds: group.preferredDeviceIds,
|
|
83
|
+
preferredByDeviceIds: [],
|
|
84
|
+
neighbors: [],
|
|
85
|
+
}));
|
|
86
|
+
}
|
|
87
|
+
/**
|
|
88
|
+
* Build the graph: this device, the devices it's connected to, the devices those are connected
|
|
89
|
+
* to, and every connection among them with each group's election result.
|
|
90
|
+
*
|
|
91
|
+
* @param input - The host's diagnostics answers.
|
|
92
|
+
*/
|
|
93
|
+
function buildNetworkGraph(input) {
|
|
94
|
+
const selfId = input.overview.localDeviceId;
|
|
95
|
+
const nodes = new Map();
|
|
96
|
+
const edges = new Map();
|
|
97
|
+
nodes.set(selfId, {
|
|
98
|
+
key: selfId,
|
|
99
|
+
kind: "self",
|
|
100
|
+
name: input.selfName || input.topology?.devices[selfId]?.deviceName || "This device",
|
|
101
|
+
userId: input.overview.localUserId,
|
|
102
|
+
});
|
|
103
|
+
const kindOf = (userId) => userId && userId === input.overview.localUserId ? "mine" : "other";
|
|
104
|
+
const ensureNode = (deviceId) => {
|
|
105
|
+
let node = nodes.get(deviceId);
|
|
106
|
+
if (!node) {
|
|
107
|
+
const known = input.topology?.devices[deviceId];
|
|
108
|
+
node = {
|
|
109
|
+
key: deviceId,
|
|
110
|
+
kind: kindOf(known?.userId),
|
|
111
|
+
name: known?.deviceName || shortId(deviceId),
|
|
112
|
+
owner: known?.userName,
|
|
113
|
+
userId: known?.userId,
|
|
114
|
+
};
|
|
115
|
+
nodes.set(deviceId, node);
|
|
116
|
+
}
|
|
117
|
+
return node;
|
|
118
|
+
};
|
|
119
|
+
const ensureEdge = (from, to) => {
|
|
120
|
+
const own = from === selfId || to === selfId;
|
|
121
|
+
const [a, b] = own
|
|
122
|
+
? [selfId, from === selfId ? to : from]
|
|
123
|
+
: from < to
|
|
124
|
+
? [from, to]
|
|
125
|
+
: [to, from];
|
|
126
|
+
const key = `${a}|${b}`;
|
|
127
|
+
let edge = edges.get(key);
|
|
128
|
+
if (!edge) {
|
|
129
|
+
edge = { key, a, b, own, groups: [] };
|
|
130
|
+
edges.set(key, edge);
|
|
131
|
+
}
|
|
132
|
+
return edge;
|
|
133
|
+
};
|
|
134
|
+
/** Record that `from` reports a connection to `to` in a group, and whether it chose it. */
|
|
135
|
+
const report = (from, to, group, chose, metrics) => {
|
|
136
|
+
const edge = ensureEdge(from, to);
|
|
137
|
+
let entry = edge.groups.find((item) => item.groupId === group.groupId);
|
|
138
|
+
if (!entry) {
|
|
139
|
+
entry = { ...group, aPrefersB: false, bPrefersA: false };
|
|
140
|
+
edge.groups.push(entry);
|
|
141
|
+
}
|
|
142
|
+
if (chose && from === edge.a)
|
|
143
|
+
entry.aPrefersB = true;
|
|
144
|
+
if (chose && from === edge.b)
|
|
145
|
+
entry.bPrefersA = true;
|
|
146
|
+
// This device measures its own connections; for the rest, the worse of the two reports.
|
|
147
|
+
if (!edge.own || from === selfId) {
|
|
148
|
+
edge.latencyMs = Math.max(edge.latencyMs ?? 0, metrics.latencyMs);
|
|
149
|
+
edge.errorRate = Math.max(edge.errorRate ?? 0, metrics.errorRate);
|
|
150
|
+
}
|
|
151
|
+
};
|
|
152
|
+
for (const connection of input.connections) {
|
|
153
|
+
const service = isServiceConnection(connection);
|
|
154
|
+
nodes.set(connection.deviceId, {
|
|
155
|
+
key: connection.deviceId,
|
|
156
|
+
kind: service ? "service" : kindOf(connection.userId),
|
|
157
|
+
name: connection.deviceName ||
|
|
158
|
+
(service ? hostOf(connection.connectionAddress) : undefined) ||
|
|
159
|
+
shortId(connection.deviceId),
|
|
160
|
+
owner: service ? undefined : connection.userName,
|
|
161
|
+
userId: connection.userId,
|
|
162
|
+
connection,
|
|
163
|
+
});
|
|
164
|
+
const edge = ensureEdge(selfId, connection.deviceId);
|
|
165
|
+
edge.latencyMs = connection.latencyMs;
|
|
166
|
+
edge.errorRate = connection.errorRate;
|
|
167
|
+
edge.sendBps = (connection.sendRateMBps ?? 0) * BYTES_PER_MB;
|
|
168
|
+
edge.receiveBps = (connection.receiveRateMBps ?? 0) * BYTES_PER_MB;
|
|
169
|
+
}
|
|
170
|
+
for (const group of topologyGroups(input)) {
|
|
171
|
+
const named = { groupId: group.groupId, groupName: group.groupName };
|
|
172
|
+
for (const connection of group.connections) {
|
|
173
|
+
// A group can list a connection a moment before or after the connection list does.
|
|
174
|
+
if (connection.closed || !nodes.get(connection.deviceId)?.connection)
|
|
175
|
+
continue;
|
|
176
|
+
report(selfId, connection.deviceId, named, group.preferredDeviceIds.includes(connection.deviceId), connection);
|
|
177
|
+
}
|
|
178
|
+
for (const deviceId of group.preferredByDeviceIds) {
|
|
179
|
+
const entry = edges
|
|
180
|
+
.get(`${selfId}|${deviceId}`)
|
|
181
|
+
?.groups.find((item) => item.groupId === group.groupId);
|
|
182
|
+
if (entry)
|
|
183
|
+
entry.bPrefersA = true;
|
|
184
|
+
}
|
|
185
|
+
for (const neighbor of group.neighbors) {
|
|
186
|
+
const node = nodes.get(neighbor.deviceId);
|
|
187
|
+
if (!node?.connection)
|
|
188
|
+
continue;
|
|
189
|
+
node.reportAgeMs = Math.min(node.reportAgeMs ?? Number.POSITIVE_INFINITY, neighbor.snapshotAgeMs);
|
|
190
|
+
for (const connection of neighbor.connections) {
|
|
191
|
+
if (connection.closed || connection.deviceId === neighbor.deviceId)
|
|
192
|
+
continue;
|
|
193
|
+
const chose = neighbor.preferredDeviceIds.includes(connection.deviceId);
|
|
194
|
+
if (connection.deviceId === selfId) {
|
|
195
|
+
const entry = edges
|
|
196
|
+
.get(`${selfId}|${neighbor.deviceId}`)
|
|
197
|
+
?.groups.find((item) => item.groupId === group.groupId);
|
|
198
|
+
if (entry && chose)
|
|
199
|
+
entry.bPrefersA = true;
|
|
200
|
+
continue;
|
|
201
|
+
}
|
|
202
|
+
ensureNode(connection.deviceId);
|
|
203
|
+
report(neighbor.deviceId, connection.deviceId, named, chose, connection);
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
for (const device of input.disconnected) {
|
|
208
|
+
if (nodes.has(device.deviceId) || isServiceDevice(device))
|
|
209
|
+
continue;
|
|
210
|
+
nodes.set(device.deviceId, {
|
|
211
|
+
key: device.deviceId,
|
|
212
|
+
kind: kindOf(device.userId),
|
|
213
|
+
name: device.name || shortId(device.deviceId),
|
|
214
|
+
owner: device.userName,
|
|
215
|
+
userId: device.userId,
|
|
216
|
+
offline: true,
|
|
217
|
+
lastSeen: device.lastSeen ? new Date(device.lastSeen) : undefined,
|
|
218
|
+
});
|
|
219
|
+
}
|
|
220
|
+
const syncById = new Map(input.groupSync.map((group) => [group.groupId, group]));
|
|
221
|
+
const groups = input.overview.groups.map((group) => ({
|
|
222
|
+
groupId: group.groupId,
|
|
223
|
+
groupName: group.groupName,
|
|
224
|
+
syncState: syncById.get(group.groupId)?.syncStatus.state,
|
|
225
|
+
totalMembers: group.totalMembers,
|
|
226
|
+
}));
|
|
227
|
+
return { selfId, nodes, edges: [...edges.values()], groups, hasTopology: !!input.topology };
|
|
228
|
+
}
|
|
229
|
+
const KIND_ORDER = { self: 0, service: 1, mine: 2, other: 3 };
|
|
230
|
+
/**
|
|
231
|
+
* A connected node's neighbors in the order the layout places them: services first, then the
|
|
232
|
+
* person's own devices, then others by name. Offline devices have no connections, so they
|
|
233
|
+
* aren't anyone's neighbor; see {@link offlineNodes}.
|
|
234
|
+
*
|
|
235
|
+
* @param graph - The graph.
|
|
236
|
+
* @param shows - The view's settings.
|
|
237
|
+
* @returns The neighbors of a node, for the ring layout.
|
|
238
|
+
*/
|
|
239
|
+
function neighborsFor(graph, shows) {
|
|
240
|
+
const near = new Map();
|
|
241
|
+
const visible = (node) => !!node && !node.offline && (node.kind !== "service" || shows.service);
|
|
242
|
+
const join = (a, b) => {
|
|
243
|
+
near.set(a, (near.get(a) ?? new Set()).add(b));
|
|
244
|
+
near.set(b, (near.get(b) ?? new Set()).add(a));
|
|
245
|
+
};
|
|
246
|
+
for (const edge of graph.edges) {
|
|
247
|
+
if (visible(graph.nodes.get(edge.a)) && visible(graph.nodes.get(edge.b)))
|
|
248
|
+
join(edge.a, edge.b);
|
|
249
|
+
}
|
|
250
|
+
const order = (a, b) => {
|
|
251
|
+
const na = graph.nodes.get(a);
|
|
252
|
+
const nb = graph.nodes.get(b);
|
|
253
|
+
return (KIND_ORDER[na.kind] - KIND_ORDER[nb.kind] ||
|
|
254
|
+
na.name.localeCompare(nb.name) ||
|
|
255
|
+
a.localeCompare(b));
|
|
256
|
+
};
|
|
257
|
+
return (key) => [...(near.get(key) ?? [])].sort(order);
|
|
258
|
+
}
|
|
259
|
+
/**
|
|
260
|
+
* The known devices that aren't connected, most recently seen first, for the outer ring.
|
|
261
|
+
*
|
|
262
|
+
* @param graph - The graph.
|
|
263
|
+
* @param max - The most to draw.
|
|
264
|
+
* @returns The devices to draw, and how many more there are.
|
|
265
|
+
*/
|
|
266
|
+
function offlineNodes(graph, max) {
|
|
267
|
+
const all = [...graph.nodes.values()]
|
|
268
|
+
.filter((node) => node.offline)
|
|
269
|
+
.sort((a, b) => (b.lastSeen?.getTime() ?? 0) - (a.lastSeen?.getTime() ?? 0) || a.key.localeCompare(b.key));
|
|
270
|
+
return { shown: all.slice(0, max), more: Math.max(0, all.length - max) };
|
|
271
|
+
}
|
|
272
|
+
/**
|
|
273
|
+
* Apply the group filter to a connection. With no group selected every group counts. With some
|
|
274
|
+
* selected, only they do, and a connection none of them reports is out of the filter. A
|
|
275
|
+
* connection no group reports at all (a service relay) is never filtered out.
|
|
276
|
+
*
|
|
277
|
+
* @param edge - The connection.
|
|
278
|
+
* @param selectedGroupIds - The selected groups; empty for all.
|
|
279
|
+
*/
|
|
280
|
+
function viewEdge(edge, selectedGroupIds) {
|
|
281
|
+
const groups = selectedGroupIds.length
|
|
282
|
+
? edge.groups.filter((group) => selectedGroupIds.includes(group.groupId))
|
|
283
|
+
: edge.groups;
|
|
284
|
+
return {
|
|
285
|
+
inFilter: !selectedGroupIds.length || !edge.groups.length || groups.length > 0,
|
|
286
|
+
aPrefersB: groups.some((group) => group.aPrefersB),
|
|
287
|
+
bPrefersA: groups.some((group) => group.bPrefersA),
|
|
288
|
+
groups,
|
|
289
|
+
};
|
|
290
|
+
}
|
|
291
|
+
// ---- Health and traffic. ----
|
|
292
|
+
/** Latency at or above this is slow whatever the connection usually does, in milliseconds. */
|
|
293
|
+
exports.SLOW_LATENCY_MS = 1000;
|
|
294
|
+
/** Latency at or above this is failing, in milliseconds. */
|
|
295
|
+
exports.FAILING_LATENCY_MS = 5000;
|
|
296
|
+
/** Share of failed calls at or above which a connection is slow. */
|
|
297
|
+
exports.SLOW_ERROR_RATE = 0.05;
|
|
298
|
+
/** Share of failed calls at or above which a connection is failing. */
|
|
299
|
+
exports.FAILING_ERROR_RATE = 0.25;
|
|
300
|
+
/**
|
|
301
|
+
* Judge a connection. It's failing when most calls time out or a quarter fail, and slow when
|
|
302
|
+
* calls fail now and then, latency is very high, or latency is at least twice what this
|
|
303
|
+
* connection usually manages (and at least 150 ms above it). A far-away device with a steady
|
|
304
|
+
* 300 ms is normal; the same device at 700 ms is slow.
|
|
305
|
+
*
|
|
306
|
+
* @param latencyMs - Current latency.
|
|
307
|
+
* @param errorRate - Current share of failed calls, 0 to 1.
|
|
308
|
+
* @param usualLatencyMs - What this connection usually manages, when enough has been seen.
|
|
309
|
+
*/
|
|
310
|
+
function edgeHealth(latencyMs, errorRate, usualLatencyMs) {
|
|
311
|
+
const latency = latencyMs ?? 0;
|
|
312
|
+
const errors = errorRate ?? 0;
|
|
313
|
+
if (errors >= exports.FAILING_ERROR_RATE || latency >= exports.FAILING_LATENCY_MS)
|
|
314
|
+
return "failing";
|
|
315
|
+
if (errors >= exports.SLOW_ERROR_RATE || latency >= exports.SLOW_LATENCY_MS)
|
|
316
|
+
return "slow";
|
|
317
|
+
if (usualLatencyMs !== undefined &&
|
|
318
|
+
latency >= usualLatencyMs * 2 &&
|
|
319
|
+
latency - usualLatencyMs >= 150) {
|
|
320
|
+
return "slow";
|
|
321
|
+
}
|
|
322
|
+
return "ok";
|
|
323
|
+
}
|
|
324
|
+
/**
|
|
325
|
+
* What each connection's latency usually is, from what the viewer has seen while open: the
|
|
326
|
+
* lower quartile of the last ten minutes, once there are ten readings.
|
|
327
|
+
*/
|
|
328
|
+
class LatencyHistory {
|
|
329
|
+
readings = new Map();
|
|
330
|
+
/** Keep readings this long, in milliseconds. */
|
|
331
|
+
static WINDOW_MS = 10 * 60_000;
|
|
332
|
+
/** Readings needed before {@link usual} answers. */
|
|
333
|
+
static MIN_READINGS = 10;
|
|
334
|
+
/**
|
|
335
|
+
* Add a reading.
|
|
336
|
+
*
|
|
337
|
+
* @param key - The connection.
|
|
338
|
+
* @param latencyMs - Its latency now.
|
|
339
|
+
* @param now - The time, in epoch milliseconds.
|
|
340
|
+
*/
|
|
341
|
+
record(key, latencyMs, now) {
|
|
342
|
+
const list = this.readings.get(key) ?? [];
|
|
343
|
+
list.push({ at: now, latencyMs });
|
|
344
|
+
while (list.length && now - list[0].at > LatencyHistory.WINDOW_MS)
|
|
345
|
+
list.shift();
|
|
346
|
+
this.readings.set(key, list);
|
|
347
|
+
}
|
|
348
|
+
/**
|
|
349
|
+
* What a connection usually manages.
|
|
350
|
+
*
|
|
351
|
+
* @param key - The connection.
|
|
352
|
+
* @returns Its usual latency, or undefined until enough readings exist.
|
|
353
|
+
*/
|
|
354
|
+
usual(key) {
|
|
355
|
+
const list = this.readings.get(key);
|
|
356
|
+
if (!list || list.length < LatencyHistory.MIN_READINGS)
|
|
357
|
+
return undefined;
|
|
358
|
+
const sorted = list.map((reading) => reading.latencyMs).sort((a, b) => a - b);
|
|
359
|
+
return sorted[Math.floor((sorted.length - 1) / 4)];
|
|
360
|
+
}
|
|
361
|
+
/** Forget connections that are gone. */
|
|
362
|
+
keepOnly(keys) {
|
|
363
|
+
for (const key of this.readings.keys()) {
|
|
364
|
+
if (!keys.has(key))
|
|
365
|
+
this.readings.delete(key);
|
|
366
|
+
}
|
|
367
|
+
}
|
|
368
|
+
}
|
|
369
|
+
exports.LatencyHistory = LatencyHistory;
|
|
370
|
+
/**
|
|
371
|
+
* Rates under this many bytes a second count as idle: an open connection always carries a few
|
|
372
|
+
* keepalive bytes, and drawing those would put dots on every line.
|
|
373
|
+
*/
|
|
374
|
+
exports.IDLE_BYTES_PER_SECOND = 256;
|
|
375
|
+
/**
|
|
376
|
+
* Sort a rate into the three dot sizes: under 10 KB/s, under 1 MB/s, and more.
|
|
377
|
+
*
|
|
378
|
+
* @param bytesPerSecond - The rate.
|
|
379
|
+
* @returns 0 when nothing is moving.
|
|
380
|
+
*/
|
|
381
|
+
function trafficStep(bytesPerSecond) {
|
|
382
|
+
const rate = bytesPerSecond ?? 0;
|
|
383
|
+
if (rate < exports.IDLE_BYTES_PER_SECOND)
|
|
384
|
+
return 0;
|
|
385
|
+
if (rate < 10 * 1024)
|
|
386
|
+
return 1;
|
|
387
|
+
if (rate < BYTES_PER_MB)
|
|
388
|
+
return 2;
|
|
389
|
+
return 3;
|
|
390
|
+
}
|
|
391
|
+
/**
|
|
392
|
+
* A rate for people: `idle`, `0.3 KB/s`, `40 KB/s`, `1.2 MB/s`.
|
|
393
|
+
*
|
|
394
|
+
* @param bytesPerSecond - The rate.
|
|
395
|
+
*/
|
|
396
|
+
function formatRate(bytesPerSecond) {
|
|
397
|
+
const rate = bytesPerSecond ?? 0;
|
|
398
|
+
if (rate < exports.IDLE_BYTES_PER_SECOND)
|
|
399
|
+
return "idle";
|
|
400
|
+
const kb = rate / 1024;
|
|
401
|
+
if (kb < 10)
|
|
402
|
+
return `${kb.toFixed(1)} KB/s`;
|
|
403
|
+
if (kb < 1024)
|
|
404
|
+
return `${Math.round(kb)} KB/s`;
|
|
405
|
+
return `${(kb / 1024).toFixed(1)} MB/s`;
|
|
406
|
+
}
|
|
407
|
+
/** Latency for people: `379 ms`. */
|
|
408
|
+
function formatLatency(latencyMs) {
|
|
409
|
+
return `${Math.round(latencyMs ?? 0)} ms`;
|
|
410
|
+
}
|
|
411
|
+
/** An error rate for people: `no errors`, `6.3% errors`. */
|
|
412
|
+
function formatErrors(errorRate) {
|
|
413
|
+
return errorRate ? `${(errorRate * 100).toFixed(1)}% errors` : "no errors";
|
|
414
|
+
}
|
|
415
|
+
// ---- Words for the card. ----
|
|
416
|
+
const TRUST_NAMES = [
|
|
417
|
+
[peers_sdk_1.TrustLevel.Self, "Self"],
|
|
418
|
+
[peers_sdk_1.TrustLevel.Trusted, "Trusted"],
|
|
419
|
+
[peers_sdk_1.TrustLevel.Known, "Known"],
|
|
420
|
+
[peers_sdk_1.TrustLevel.NewDevice, "New device"],
|
|
421
|
+
[peers_sdk_1.TrustLevel.NewUser, "New user"],
|
|
422
|
+
[peers_sdk_1.TrustLevel.Unknown, "Unknown"],
|
|
423
|
+
[peers_sdk_1.TrustLevel.Untrusted, "Untrusted"],
|
|
424
|
+
[peers_sdk_1.TrustLevel.Malicious, "Malicious"],
|
|
425
|
+
];
|
|
426
|
+
/**
|
|
427
|
+
* A trust level's name: the highest named level at or below it, as the trust badge reads it.
|
|
428
|
+
*
|
|
429
|
+
* @param level - The level; missing reads as Unknown.
|
|
430
|
+
*/
|
|
431
|
+
function trustName(level) {
|
|
432
|
+
const value = level ?? peers_sdk_1.TrustLevel.Unknown;
|
|
433
|
+
return TRUST_NAMES.find(([named]) => value >= named)?.[1] ?? "Malicious";
|
|
434
|
+
}
|
|
435
|
+
/**
|
|
436
|
+
* Whose a device is, for the line under its name: `Yours`, `Blair's`, `Its own account`.
|
|
437
|
+
*
|
|
438
|
+
* @param node - The device.
|
|
439
|
+
*/
|
|
440
|
+
function ownerLabel(node) {
|
|
441
|
+
if (node.kind === "self")
|
|
442
|
+
return "This device · yours";
|
|
443
|
+
if (node.kind === "service")
|
|
444
|
+
return "Peers Services relay";
|
|
445
|
+
if (node.kind === "mine")
|
|
446
|
+
return "Yours";
|
|
447
|
+
if (hasOwnAccount(node))
|
|
448
|
+
return "Its own account";
|
|
449
|
+
return node.owner ? `${node.owner}'s` : "Someone else's";
|
|
450
|
+
}
|
|
451
|
+
/**
|
|
452
|
+
* Whether a device is signed in to an account named after it, as a kiosk or hub often is:
|
|
453
|
+
* `Kitchen` on the account `Kitchen`, or `Bike Computer` on `Mark's Bike Computer`.
|
|
454
|
+
*/
|
|
455
|
+
function hasOwnAccount(node) {
|
|
456
|
+
return !!node.owner && (node.owner === node.name || node.owner.endsWith(` ${node.name}`));
|
|
457
|
+
}
|
|
458
|
+
/**
|
|
459
|
+
* What to call a device on the graph: its name, with its owner first when it's someone else's
|
|
460
|
+
* (`Blair · Laptop`), so two people's laptops can be told apart. A device on an account named
|
|
461
|
+
* after it keeps just its name.
|
|
462
|
+
*
|
|
463
|
+
* @param node - The device.
|
|
464
|
+
*/
|
|
465
|
+
function graphName(node) {
|
|
466
|
+
if (node.kind !== "other" || !node.owner || hasOwnAccount(node))
|
|
467
|
+
return node.name;
|
|
468
|
+
return `${node.owner} · ${node.name}`;
|
|
469
|
+
}
|
|
470
|
+
/**
|
|
471
|
+
* How a connection travels, for people: `WebRTC`, `WebSocket · through another device`.
|
|
472
|
+
*
|
|
473
|
+
* @param address - The connection's address.
|
|
474
|
+
* @param pathType - Whether it's direct or relayed through another device.
|
|
475
|
+
*/
|
|
476
|
+
function routeOf(address, pathType) {
|
|
477
|
+
const transport = transportOf(address);
|
|
478
|
+
const path = pathType === "indirect" ? "through another device" : undefined;
|
|
479
|
+
return [transport, path].filter(Boolean).join(" · ") || "Unknown";
|
|
480
|
+
}
|
|
481
|
+
/**
|
|
482
|
+
* A group's sync state in a word: `Synced`, `Syncing`, `Sync error`, or `Unknown`.
|
|
483
|
+
*
|
|
484
|
+
* @param state - The state the host reports, if any.
|
|
485
|
+
*/
|
|
486
|
+
function syncStateWord(state) {
|
|
487
|
+
if (state === "syncing")
|
|
488
|
+
return "Syncing";
|
|
489
|
+
if (state === "error")
|
|
490
|
+
return "Sync error";
|
|
491
|
+
return state ? "Synced" : "Unknown";
|
|
492
|
+
}
|
|
493
|
+
/** Join names the way a sentence does: `A`, `A and B`, `A, B and C`. */
|
|
494
|
+
function listNames(names) {
|
|
495
|
+
if (names.length <= 1)
|
|
496
|
+
return names[0] ?? "";
|
|
497
|
+
return `${names.slice(0, -1).join(", ")} and ${names[names.length - 1]}`;
|
|
498
|
+
}
|
|
499
|
+
/**
|
|
500
|
+
* Say which groups chose a connection, and which way, one line per kind of choice.
|
|
501
|
+
*
|
|
502
|
+
* @param view - The connection under the current filter.
|
|
503
|
+
* @param aName - What to call `a`.
|
|
504
|
+
* @param bName - What to call `b`.
|
|
505
|
+
*/
|
|
506
|
+
function describeChoices(view, aName, bName) {
|
|
507
|
+
const names = (pick) => listNames(view.groups.filter(pick).map((group) => group.groupName));
|
|
508
|
+
const both = names((group) => group.aPrefersB && group.bPrefersA);
|
|
509
|
+
const aOnly = names((group) => group.aPrefersB && !group.bPrefersA);
|
|
510
|
+
const bOnly = names((group) => !group.aPrefersB && group.bPrefersA);
|
|
511
|
+
const neither = names((group) => !group.aPrefersB && !group.bPrefersA);
|
|
512
|
+
const lines = [];
|
|
513
|
+
if (both)
|
|
514
|
+
lines.push(`They chose each other in ${both}.`);
|
|
515
|
+
if (aOnly)
|
|
516
|
+
lines.push(`${aName} chose ${bName} in ${aOnly}.`);
|
|
517
|
+
if (bOnly)
|
|
518
|
+
lines.push(`${bName} chose ${aName} in ${bOnly}.`);
|
|
519
|
+
if (neither)
|
|
520
|
+
lines.push(`Connected, not chosen, in ${neither}.`);
|
|
521
|
+
if (!lines.length)
|
|
522
|
+
lines.push("No group syncs over this connection.");
|
|
523
|
+
return lines;
|
|
524
|
+
}
|
|
525
|
+
/**
|
|
526
|
+
* Who a device syncs with and who chose it, under the current filter.
|
|
527
|
+
*
|
|
528
|
+
* @param graph - The graph.
|
|
529
|
+
* @param deviceId - The device.
|
|
530
|
+
* @param selectedGroupIds - The selected groups; empty for all.
|
|
531
|
+
*/
|
|
532
|
+
function syncSummary(graph, deviceId, selectedGroupIds) {
|
|
533
|
+
const chose = [];
|
|
534
|
+
const chosenBy = [];
|
|
535
|
+
const connectedOnly = [];
|
|
536
|
+
for (const edge of graph.edges) {
|
|
537
|
+
if (edge.a !== deviceId && edge.b !== deviceId)
|
|
538
|
+
continue;
|
|
539
|
+
const view = viewEdge(edge, selectedGroupIds);
|
|
540
|
+
if (!view.inFilter || !edge.groups.length)
|
|
541
|
+
continue;
|
|
542
|
+
const other = graph.nodes.get(edge.a === deviceId ? edge.b : edge.a)?.name ?? "";
|
|
543
|
+
const outward = edge.a === deviceId ? view.aPrefersB : view.bPrefersA;
|
|
544
|
+
const inward = edge.a === deviceId ? view.bPrefersA : view.aPrefersB;
|
|
545
|
+
if (outward)
|
|
546
|
+
chose.push(other);
|
|
547
|
+
if (inward)
|
|
548
|
+
chosenBy.push(other);
|
|
549
|
+
if (!outward && !inward)
|
|
550
|
+
connectedOnly.push(other);
|
|
551
|
+
}
|
|
552
|
+
return { chose: chose.sort(), chosenBy: chosenBy.sort(), connectedOnly: connectedOnly.sort() };
|
|
553
|
+
}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import type { NetGraphShows } from "./network-graph-model";
|
|
2
|
+
/** Which of the Network Viewer's two views is showing. */
|
|
3
|
+
export type NetworkViewerView = "graph" | "list";
|
|
4
|
+
/** The view showing: the graph unless changed. */
|
|
5
|
+
export declare const networkViewerView: import("@peers-app/peers-sdk").Observable<NetworkViewerView>;
|
|
6
|
+
/** How far out the graph goes, and whether offline devices and services are drawn. */
|
|
7
|
+
export declare const networkGraphShows: import("@peers-app/peers-sdk").Observable<NetGraphShows>;
|
|
8
|
+
/** The groups the graph is filtered to; empty for all of them. */
|
|
9
|
+
export declare const networkGraphGroupIds: import("@peers-app/peers-sdk").Observable<readonly string[]>;
|
|
10
|
+
/**
|
|
11
|
+
* Switch between the graph and the list.
|
|
12
|
+
*
|
|
13
|
+
* @param view - The view to show.
|
|
14
|
+
*/
|
|
15
|
+
export declare function setNetworkViewerView(view: NetworkViewerView): void;
|
|
16
|
+
/**
|
|
17
|
+
* Change how far out the graph goes or what it draws.
|
|
18
|
+
*
|
|
19
|
+
* @param change - The settings to change.
|
|
20
|
+
*/
|
|
21
|
+
export declare function setNetworkGraphShows(change: Partial<NetGraphShows>): void;
|
|
22
|
+
/**
|
|
23
|
+
* Filter the graph to some groups.
|
|
24
|
+
*
|
|
25
|
+
* @param groupIds - The groups; empty for all.
|
|
26
|
+
*/
|
|
27
|
+
export declare function setNetworkGraphGroupIds(groupIds: readonly string[]): void;
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.networkGraphGroupIds = exports.networkGraphShows = exports.networkViewerView = void 0;
|
|
4
|
+
exports.setNetworkViewerView = setNetworkViewerView;
|
|
5
|
+
exports.setNetworkGraphShows = setNetworkGraphShows;
|
|
6
|
+
exports.setNetworkGraphGroupIds = setNetworkGraphGroupIds;
|
|
7
|
+
const peers_sdk_1 = require("@peers-app/peers-sdk");
|
|
8
|
+
// The Network Viewer's own settings. This browser remembers them; they're a convenience, so a
|
|
9
|
+
// missing or blocked store just means the defaults.
|
|
10
|
+
const KEY = "peers:network-viewer";
|
|
11
|
+
const DEFAULTS = {
|
|
12
|
+
view: "graph",
|
|
13
|
+
shows: { steps: 2, offline: false, service: true },
|
|
14
|
+
groupIds: [],
|
|
15
|
+
};
|
|
16
|
+
function read() {
|
|
17
|
+
try {
|
|
18
|
+
const value = JSON.parse(localStorage.getItem(KEY) ?? "null");
|
|
19
|
+
return {
|
|
20
|
+
view: value?.view === "list" ? "list" : "graph",
|
|
21
|
+
shows: {
|
|
22
|
+
steps: value?.shows?.steps === 1 ? 1 : 2,
|
|
23
|
+
offline: value?.shows?.offline === true,
|
|
24
|
+
service: value?.shows?.service !== false,
|
|
25
|
+
},
|
|
26
|
+
groupIds: Array.isArray(value?.groupIds)
|
|
27
|
+
? value.groupIds.filter((id) => typeof id === "string")
|
|
28
|
+
: [],
|
|
29
|
+
};
|
|
30
|
+
}
|
|
31
|
+
catch {
|
|
32
|
+
return DEFAULTS;
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
const stored = read();
|
|
36
|
+
/** The view showing: the graph unless changed. */
|
|
37
|
+
exports.networkViewerView = (0, peers_sdk_1.observable)(stored.view);
|
|
38
|
+
/** How far out the graph goes, and whether offline devices and services are drawn. */
|
|
39
|
+
exports.networkGraphShows = (0, peers_sdk_1.observable)(stored.shows);
|
|
40
|
+
/** The groups the graph is filtered to; empty for all of them. */
|
|
41
|
+
exports.networkGraphGroupIds = (0, peers_sdk_1.observable)(stored.groupIds);
|
|
42
|
+
function keep() {
|
|
43
|
+
try {
|
|
44
|
+
const value = {
|
|
45
|
+
view: (0, exports.networkViewerView)(),
|
|
46
|
+
shows: (0, exports.networkGraphShows)(),
|
|
47
|
+
groupIds: [...(0, exports.networkGraphGroupIds)()],
|
|
48
|
+
};
|
|
49
|
+
localStorage.setItem(KEY, JSON.stringify(value));
|
|
50
|
+
}
|
|
51
|
+
catch {
|
|
52
|
+
// Storage can be unavailable.
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* Switch between the graph and the list.
|
|
57
|
+
*
|
|
58
|
+
* @param view - The view to show.
|
|
59
|
+
*/
|
|
60
|
+
function setNetworkViewerView(view) {
|
|
61
|
+
(0, exports.networkViewerView)(view);
|
|
62
|
+
keep();
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* Change how far out the graph goes or what it draws.
|
|
66
|
+
*
|
|
67
|
+
* @param change - The settings to change.
|
|
68
|
+
*/
|
|
69
|
+
function setNetworkGraphShows(change) {
|
|
70
|
+
(0, exports.networkGraphShows)({ ...(0, exports.networkGraphShows)(), ...change });
|
|
71
|
+
keep();
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* Filter the graph to some groups.
|
|
75
|
+
*
|
|
76
|
+
* @param groupIds - The groups; empty for all.
|
|
77
|
+
*/
|
|
78
|
+
function setNetworkGraphGroupIds(groupIds) {
|
|
79
|
+
(0, exports.networkGraphGroupIds)(groupIds);
|
|
80
|
+
keep();
|
|
81
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import type { INetworkOverview } from "@peers-app/peers-sdk";
|
|
2
|
+
import { type NetworkGraphCardActions } from "./network-graph-card";
|
|
3
|
+
import { type NetGraph } from "./network-graph-model";
|
|
4
|
+
/**
|
|
5
|
+
* The Graph view's stage: this device at the center, its connections around it and theirs
|
|
6
|
+
* further out, with the card for whatever is selected.
|
|
7
|
+
*
|
|
8
|
+
* Arrows point at the device that was chosen to sync with. A dotted line is a connection no
|
|
9
|
+
* group chose. Dots move the way the data does, bigger for more of it. Hovering a device or a
|
|
10
|
+
* connection brings out its neighbors; clicking keeps it selected.
|
|
11
|
+
*/
|
|
12
|
+
export declare function NetworkGraphStage(props: {
|
|
13
|
+
graph: NetGraph;
|
|
14
|
+
overview: INetworkOverview;
|
|
15
|
+
desktop: boolean;
|
|
16
|
+
actions: Omit<NetworkGraphCardActions, "onSelectNode">;
|
|
17
|
+
}): import("react/jsx-runtime").JSX.Element;
|