@avatarsd-llc/libtracer-client 0.3.0 → 0.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +2 -1
- package/dist/client.d.ts +81 -42
- package/dist/client.js +108 -55
- package/dist/fwd.d.ts +73 -61
- package/dist/fwd.js +70 -57
- package/dist/index.d.ts +19 -2
- package/dist/index.js +17 -12
- package/dist/tlv.d.ts +75 -16
- package/dist/tlv.js +139 -23
- package/dist/topology.d.ts +178 -0
- package/dist/topology.js +151 -0
- package/package.json +3 -3
package/dist/topology.js
ADDED
|
@@ -0,0 +1,151 @@
|
|
|
1
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
2
|
+
// SPDX-FileCopyrightText: Copyright 2026 avatarsd LLC
|
|
3
|
+
import { TYPE } from '@avatarsd-llc/libtracer';
|
|
4
|
+
const utf8 = new TextDecoder();
|
|
5
|
+
/**
|
|
6
|
+
* @brief Characters reference/03 forbids inside a path segment (mirrors `tlv.ts`).
|
|
7
|
+
*
|
|
8
|
+
* Used to decide whether a bus peer's name could even be addressed as a hop — a ws
|
|
9
|
+
* bus names peers `<ip>:<port>`, which contains two of these.
|
|
10
|
+
*/
|
|
11
|
+
const RESERVED_SEGMENT_CHARS = /[/:.[\]*?]/;
|
|
12
|
+
/** @brief `["b","c"]` → `"/b/c"`; `[]` → `"/"` (the vantage). */
|
|
13
|
+
export function routeKey(route) {
|
|
14
|
+
return route.length === 0 ? '/' : '/' + route.join('/');
|
|
15
|
+
}
|
|
16
|
+
/** @brief The NAME strings of a `:children[]` POINT listing. */
|
|
17
|
+
function listingNames(tlv) {
|
|
18
|
+
const names = [];
|
|
19
|
+
for (const member of tlv.children ?? []) {
|
|
20
|
+
for (const field of member.children ?? []) {
|
|
21
|
+
if (field.type === TYPE.NAME)
|
|
22
|
+
names.push(utf8.decode(field.payload));
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
return names;
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* @brief Walk the reachable topology from `client`'s vantage and stitch it into a graph.
|
|
29
|
+
*
|
|
30
|
+
* Breadth-first, so a truncated walk is a complete picture of the near network rather
|
|
31
|
+
* than one deep tendril. Each node costs one `readField` for its connections plus one
|
|
32
|
+
* {@link WalkOptions.identify} call, both routed across the full path to it.
|
|
33
|
+
*
|
|
34
|
+
* An unreadable node (a dead link, a denied read) becomes a warning and a leaf — the
|
|
35
|
+
* walk continues, because one unreachable branch is not a reason to lose the rest.
|
|
36
|
+
*
|
|
37
|
+
* @param client attached to the vantage node
|
|
38
|
+
* @param opts identity resolution and bounds
|
|
39
|
+
* @returns the projection — check {@link TopologyGraph.authoritative} before trusting it
|
|
40
|
+
*/
|
|
41
|
+
export async function walkTopology(client, opts = {}) {
|
|
42
|
+
const maxDepth = opts.maxDepth ?? 8;
|
|
43
|
+
const netRoot = opts.netRoot ?? 'net';
|
|
44
|
+
const byId = new Map();
|
|
45
|
+
const edges = [];
|
|
46
|
+
const busLinks = [];
|
|
47
|
+
const warnings = [];
|
|
48
|
+
let truncated = false;
|
|
49
|
+
let anyUnidentified = false;
|
|
50
|
+
/**
|
|
51
|
+
* A connection's `:children[]` discriminates the two link shapes (ADR-0044): a BUS
|
|
52
|
+
* link synthesizes its live peers there, while a point-to-point link has no such
|
|
53
|
+
* facet and the generic member listing of a childless vertex is empty. Empty ⇒ safe
|
|
54
|
+
* to route through by name; non-empty ⇒ a bus, and routing by name would broadcast.
|
|
55
|
+
*/
|
|
56
|
+
const peersOf = async (route, name) => {
|
|
57
|
+
try {
|
|
58
|
+
return listingNames(await client.readField([...route, netRoot, name], ':children[]'));
|
|
59
|
+
}
|
|
60
|
+
catch {
|
|
61
|
+
return []; // unreadable ⇒ treat as point-to-point; the descend below will warn if it fails
|
|
62
|
+
}
|
|
63
|
+
};
|
|
64
|
+
/** Resolve `route` to a node id, creating the node on first sight. */
|
|
65
|
+
const intern = async (route) => {
|
|
66
|
+
let identity = null;
|
|
67
|
+
if (opts.identify) {
|
|
68
|
+
try {
|
|
69
|
+
identity = await opts.identify(client, route);
|
|
70
|
+
}
|
|
71
|
+
catch (err) {
|
|
72
|
+
warnings.push(`identity read failed at ${routeKey(route)}: ${String(err)}`);
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
if (identity === null)
|
|
76
|
+
anyUnidentified = true;
|
|
77
|
+
// The dedup key. With an identity, two routes to one device collapse — and a cycle
|
|
78
|
+
// closes. Without one, the ROUTE is the key, so nothing ever collapses: a cycle
|
|
79
|
+
// yields endless distinct nodes and only maxDepth stops the walk. That is the
|
|
80
|
+
// degradation, stated in code.
|
|
81
|
+
const id = identity ?? routeKey(route);
|
|
82
|
+
const existing = byId.get(id);
|
|
83
|
+
if (existing) {
|
|
84
|
+
if (!existing.routes.some((r) => routeKey(r) === routeKey(route)))
|
|
85
|
+
existing.routes.push(route);
|
|
86
|
+
return { id, fresh: false };
|
|
87
|
+
}
|
|
88
|
+
byId.set(id, { id, routes: [route], identity });
|
|
89
|
+
return { id, fresh: true };
|
|
90
|
+
};
|
|
91
|
+
const rootEntry = await intern([]);
|
|
92
|
+
const root = rootEntry.id;
|
|
93
|
+
let frontier = [{ route: [], id: root }];
|
|
94
|
+
for (let depth = 0; depth < maxDepth && frontier.length > 0; depth++) {
|
|
95
|
+
const next = [];
|
|
96
|
+
for (const { route, id } of frontier) {
|
|
97
|
+
let names;
|
|
98
|
+
try {
|
|
99
|
+
const listing = await client.readField([...route, netRoot], ':children[]');
|
|
100
|
+
names = listingNames(listing);
|
|
101
|
+
}
|
|
102
|
+
catch (err) {
|
|
103
|
+
// A node with no /net has no connections — indistinguishable here from one that
|
|
104
|
+
// refused the read. Either way it is a leaf, and either way the walk goes on.
|
|
105
|
+
warnings.push(`could not read connections at ${routeKey(route)}: ${String(err)}`);
|
|
106
|
+
continue;
|
|
107
|
+
}
|
|
108
|
+
for (const name of names) {
|
|
109
|
+
if (opts.skipLink?.(name, route))
|
|
110
|
+
continue;
|
|
111
|
+
// A bus link is a dead end: routing through its NAME broadcasts to every peer,
|
|
112
|
+
// drawing N replies for one request. Record its peers and stop. (See
|
|
113
|
+
// TopologyBusPeers for why the directed per-peer hop is not expressible either.)
|
|
114
|
+
const peers = await peersOf(route, name);
|
|
115
|
+
if (peers.length > 0) {
|
|
116
|
+
const routable = peers.every((p) => !RESERVED_SEGMENT_CHARS.test(p));
|
|
117
|
+
busLinks.push({ at: id, name, peers, routable });
|
|
118
|
+
warnings.push(`${routeKey(route)} link "${name}" is a bus with ${peers.length} peer(s); not descended` +
|
|
119
|
+
(routable ? '' : ' — its peer names are not legal path segments, so no directed hop exists'));
|
|
120
|
+
continue;
|
|
121
|
+
}
|
|
122
|
+
const childRoute = [...route, name];
|
|
123
|
+
const child = await intern(childRoute);
|
|
124
|
+
edges.push({ from: id, to: child.id, name });
|
|
125
|
+
// Only descend into a node we have not already expanded — the dedup that makes
|
|
126
|
+
// a cycle terminate. Pre-identity `fresh` is always true for a cyclic route, so
|
|
127
|
+
// this never fires and maxDepth is the sole bound.
|
|
128
|
+
if (child.fresh)
|
|
129
|
+
next.push({ route: childRoute, id: child.id });
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
frontier = next;
|
|
133
|
+
if (frontier.length > 0 && depth + 1 >= maxDepth)
|
|
134
|
+
truncated = true;
|
|
135
|
+
}
|
|
136
|
+
if (truncated) {
|
|
137
|
+
warnings.push(`walk truncated at maxDepth=${maxDepth}` +
|
|
138
|
+
(opts.identify
|
|
139
|
+
? ''
|
|
140
|
+
: ' — no identify() was supplied, so a cyclic topology cannot terminate on its own (#406)'));
|
|
141
|
+
}
|
|
142
|
+
return {
|
|
143
|
+
nodes: [...byId.values()].map((n) => ({ id: n.id, routes: n.routes, identity: n.identity })),
|
|
144
|
+
edges,
|
|
145
|
+
busLinks,
|
|
146
|
+
root,
|
|
147
|
+
truncated,
|
|
148
|
+
authoritative: !anyUnidentified,
|
|
149
|
+
warnings,
|
|
150
|
+
};
|
|
151
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@avatarsd-llc/libtracer-client",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.5.0",
|
|
4
4
|
"description": "EXPERIMENTAL libtracer client SDK (#56, ADR-0034/0035) — a path-addressed client over the cross-validated codec (@avatarsd-llc/libtracer) and an injected transport seam (e.g. @avatarsd-llc/libtracer-ws). Speaks the RFC-0004 remote operations over FWD: read / write / await / readField / subscribe, each a FWD frame whose source-routed FWD{REPLY} is decoded back. Pre-1.0 and experimental.",
|
|
5
5
|
"author": "avatarsd LLC",
|
|
6
6
|
"license": "Apache-2.0",
|
|
@@ -33,8 +33,8 @@
|
|
|
33
33
|
"access": "public"
|
|
34
34
|
},
|
|
35
35
|
"peerDependencies": {
|
|
36
|
-
"@avatarsd-llc/libtracer": ">=0.
|
|
37
|
-
"@avatarsd-llc/libtracer-ws": ">=0.
|
|
36
|
+
"@avatarsd-llc/libtracer": ">=0.5.0 <0.6.0",
|
|
37
|
+
"@avatarsd-llc/libtracer-ws": ">=0.5.0 <0.6.0"
|
|
38
38
|
},
|
|
39
39
|
"peerDependenciesMeta": {
|
|
40
40
|
"@avatarsd-llc/libtracer-ws": {
|