@avatarsd-llc/libtracer-client 0.4.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 +21 -0
- package/dist/client.js +30 -0
- package/dist/index.d.ts +4 -2
- package/dist/index.js +2 -1
- package/dist/tlv.d.ts +52 -0
- package/dist/tlv.js +105 -0
- package/dist/topology.d.ts +178 -0
- package/dist/topology.js +151 -0
- package/package.json +3 -3
package/README.md
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
# @avatarsd-llc/libtracer-client (EXPERIMENTAL)
|
|
2
2
|
|
|
3
|
-
The libtracer **client SDK** the
|
|
3
|
+
The libtracer **client SDK** that the web-UI of the originating production
|
|
4
|
+
firmware (an ESP32-C6 smart-agriculture node) builds against
|
|
4
5
|
([#56](https://github.com/avatarsd-llc/libtracer/issues/56),
|
|
5
6
|
[ADR-0034](../../../../docs/adr/0034-typescript-client-sdk.md) /
|
|
6
7
|
[ADR-0035](../../../../docs/adr/0035-implementing-rfc-0004-remote-operation-addressing.md)).
|
package/dist/client.d.ts
CHANGED
|
@@ -156,6 +156,27 @@ export declare class LibtracerClient {
|
|
|
156
156
|
* @throws {FwdError} when the responder replies `kind=ERROR`
|
|
157
157
|
*/
|
|
158
158
|
write(path: string | string[], valueTLV: Uint8Array): Promise<void>;
|
|
159
|
+
/**
|
|
160
|
+
* @brief Write into a remote vertex's FIELD: `FWD{ op=WRITE, dst=path, field=selector,
|
|
161
|
+
* src=…, payload }` — the write counterpart of {@link readField}.
|
|
162
|
+
*
|
|
163
|
+
* The append form (`":children[]"` — a whole-array ELEMENT selector with no index) is
|
|
164
|
+
* how a vertex is CREATED in band: `writeField('/net', ':children[]', encodeConnSpec(…))`
|
|
165
|
+
* brings a transport link up on a remote device (ADR-0017 / ADR-0027, reference/13 §2),
|
|
166
|
+
* and `writeField('/sensor/temp', ':subscribers[]', encodeSubscriber(…))` binds a
|
|
167
|
+
* consumer-initiated subscription (ADR-0026). Neither was expressible from this client
|
|
168
|
+
* before: {@link write} takes no selector, so every field-addressed write — the entire
|
|
169
|
+
* in-band formation plane — was out of reach.
|
|
170
|
+
*
|
|
171
|
+
* Vector-pinned by `fwd-write-subscriber-field`.
|
|
172
|
+
*
|
|
173
|
+
* @param path the destination path (string or segments)
|
|
174
|
+
* @param selector the field selector (e.g. `":children[]"`, or parsed {@link FieldLevel}s)
|
|
175
|
+
* @param valueTLV a complete payload TLV's bytes (e.g. from {@link encodeConnSpec})
|
|
176
|
+
* @returns once the responder acks `kind=RESULT`
|
|
177
|
+
* @throws {FwdError} when the responder replies `kind=ERROR`
|
|
178
|
+
*/
|
|
179
|
+
writeField(path: string | string[], selector: string | FieldLevel[], valueTLV: Uint8Array): Promise<void>;
|
|
159
180
|
/**
|
|
160
181
|
* @brief Block for the next write to a remote vertex: `FWD{ op=AWAIT,
|
|
161
182
|
* dst=path, src=…, await_timeout? }`.
|
package/dist/client.js
CHANGED
|
@@ -279,6 +279,36 @@ export class LibtracerClient {
|
|
|
279
279
|
const reply = await this.request(encodeFwd({ op: FWD_OP.WRITE, dst: splitPath(path), src: this.replyEndpoint, payload: valueTLV }));
|
|
280
280
|
this.result(reply); // throws on ERROR; RESULT carries no payload
|
|
281
281
|
}
|
|
282
|
+
/**
|
|
283
|
+
* @brief Write into a remote vertex's FIELD: `FWD{ op=WRITE, dst=path, field=selector,
|
|
284
|
+
* src=…, payload }` — the write counterpart of {@link readField}.
|
|
285
|
+
*
|
|
286
|
+
* The append form (`":children[]"` — a whole-array ELEMENT selector with no index) is
|
|
287
|
+
* how a vertex is CREATED in band: `writeField('/net', ':children[]', encodeConnSpec(…))`
|
|
288
|
+
* brings a transport link up on a remote device (ADR-0017 / ADR-0027, reference/13 §2),
|
|
289
|
+
* and `writeField('/sensor/temp', ':subscribers[]', encodeSubscriber(…))` binds a
|
|
290
|
+
* consumer-initiated subscription (ADR-0026). Neither was expressible from this client
|
|
291
|
+
* before: {@link write} takes no selector, so every field-addressed write — the entire
|
|
292
|
+
* in-band formation plane — was out of reach.
|
|
293
|
+
*
|
|
294
|
+
* Vector-pinned by `fwd-write-subscriber-field`.
|
|
295
|
+
*
|
|
296
|
+
* @param path the destination path (string or segments)
|
|
297
|
+
* @param selector the field selector (e.g. `":children[]"`, or parsed {@link FieldLevel}s)
|
|
298
|
+
* @param valueTLV a complete payload TLV's bytes (e.g. from {@link encodeConnSpec})
|
|
299
|
+
* @returns once the responder acks `kind=RESULT`
|
|
300
|
+
* @throws {FwdError} when the responder replies `kind=ERROR`
|
|
301
|
+
*/
|
|
302
|
+
async writeField(path, selector, valueTLV) {
|
|
303
|
+
const reply = await this.request(encodeFwd({
|
|
304
|
+
op: FWD_OP.WRITE,
|
|
305
|
+
dst: splitPath(path),
|
|
306
|
+
field: selector,
|
|
307
|
+
src: this.replyEndpoint,
|
|
308
|
+
payload: valueTLV,
|
|
309
|
+
}));
|
|
310
|
+
this.result(reply); // throws on ERROR; RESULT carries no payload
|
|
311
|
+
}
|
|
282
312
|
/**
|
|
283
313
|
* @brief Block for the next write to a remote vertex: `FWD{ op=AWAIT,
|
|
284
314
|
* dst=path, src=…, await_timeout? }`.
|
package/dist/index.d.ts
CHANGED
|
@@ -16,7 +16,9 @@
|
|
|
16
16
|
export declare const CLIENT_EXPERIMENTAL: true;
|
|
17
17
|
export { LibtracerClient, FwdError, CompactFlowError } from './client.js';
|
|
18
18
|
export type { ClientTransport, ValueHandler, Unsubscribe, ClientOptions, } from './client.js';
|
|
19
|
-
export { encodeValue, encodePath, encodeSubscriber } from './tlv.js';
|
|
20
|
-
export type { ValueOptions, SubscriberOptions } from './tlv.js';
|
|
19
|
+
export { encodeValue, encodePath, encodeSubscriber, encodeConnSpec } from './tlv.js';
|
|
20
|
+
export type { ValueOptions, SubscriberOptions, ConnSpecOptions, ConnRole } from './tlv.js';
|
|
21
|
+
export { walkTopology, routeKey } from './topology.js';
|
|
22
|
+
export type { TopologyGraph, TopologyNode, TopologyEdge, TopologyBusPeers, WalkOptions, } from './topology.js';
|
|
21
23
|
export { FWD_OP, FWD_KIND, FWD_ERROR, FWD_ERROR_PATH, fwdErrorName, fwdErrorPath, fwdErrorCodeForPath, encodeFwd, encodeField, parseField, decodeFwd, parseFwdTlv, replyErrorCode, replyErrorPath, } from './fwd.js';
|
|
22
24
|
export type { FwdRequest, ParsedFwd, FieldLevel } from './fwd.js';
|
package/dist/index.js
CHANGED
|
@@ -17,5 +17,6 @@
|
|
|
17
17
|
*/
|
|
18
18
|
export const CLIENT_EXPERIMENTAL = true;
|
|
19
19
|
export { LibtracerClient, FwdError, CompactFlowError } from './client.js';
|
|
20
|
-
export { encodeValue, encodePath, encodeSubscriber } from './tlv.js';
|
|
20
|
+
export { encodeValue, encodePath, encodeSubscriber, encodeConnSpec } from './tlv.js';
|
|
21
|
+
export { walkTopology, routeKey } from './topology.js';
|
|
21
22
|
export { FWD_OP, FWD_KIND, FWD_ERROR, FWD_ERROR_PATH, fwdErrorName, fwdErrorPath, fwdErrorCodeForPath, encodeFwd, encodeField, parseField, decodeFwd, parseFwdTlv, replyErrorCode, replyErrorPath, } from './fwd.js';
|
package/dist/tlv.d.ts
CHANGED
|
@@ -54,6 +54,58 @@ export declare function nameTlv(segment: string): Tlv;
|
|
|
54
54
|
export declare function encodePath(segments: string[]): Uint8Array;
|
|
55
55
|
/** @brief The PATH TLV node (shared by {@link encodePath}, {@link encodeSubscriber}, and `./fwd.ts`). */
|
|
56
56
|
export declare function pathTlv(segments: string[]): Tlv;
|
|
57
|
+
/** @brief The link direction of a connection (`role`), per ADR-0027 / `conn_role_t`. */
|
|
58
|
+
export type ConnRole = 'dial' | 'listen';
|
|
59
|
+
/** @brief The connection SPEC a {@link encodeConnSpec} call describes (ADR-0027 / reference/13). */
|
|
60
|
+
export interface ConnSpecOptions {
|
|
61
|
+
/** @brief The device-catalog child type: `client` (DIAL default) or `listener` (LISTEN default). */
|
|
62
|
+
readonly type: 'client' | 'listener';
|
|
63
|
+
/** @brief The connection NAME — the `/net/<name>` segment AND the routing key a `dst` hops through. */
|
|
64
|
+
readonly name: string;
|
|
65
|
+
/** @brief The link direction; overrides the type's default. */
|
|
66
|
+
readonly role: ConnRole;
|
|
67
|
+
/** @brief Peer port (DIAL) / bind port (LISTEN). Required — 0 is rejected by every built-in factory. */
|
|
68
|
+
readonly port: number;
|
|
69
|
+
/** @brief The transport-factory selector (`ws`, `tcp`, `udp`, …). Omit only for a pre-staged link. */
|
|
70
|
+
readonly kind?: string;
|
|
71
|
+
/** @brief Peer IPv4 dotted-quad. Required for DIAL; the built-ins are `inet_pton`-only (no DNS). */
|
|
72
|
+
readonly addr?: string;
|
|
73
|
+
/** @brief ws-private (LISTEN): expose the ADR-0044 bus facet, so `:children[]` lists live peers. */
|
|
74
|
+
readonly peerNamed?: boolean;
|
|
75
|
+
/** @brief ws-private (LISTEN): concurrent-peer admission cap (0/omitted = unbounded). */
|
|
76
|
+
readonly maxPeers?: number;
|
|
77
|
+
}
|
|
78
|
+
/**
|
|
79
|
+
* @brief Build a connection-creation SPEC TLV (`type=0x0e`, PL=1) — the payload of the
|
|
80
|
+
* in-band `write /net:children[] += SPEC{…}` that brings a transport link up (ADR-0027,
|
|
81
|
+
* reference/13 §2).
|
|
82
|
+
*
|
|
83
|
+
* This is the wire form of the formation write a third party (typically a web UI holding
|
|
84
|
+
* delegated admin) issues on a device to create a link — the mechanism that makes a
|
|
85
|
+
* device-to-device connection with no third party in the data path.
|
|
86
|
+
*
|
|
87
|
+
* ```
|
|
88
|
+
* SPEC{ NAME "type" NAME <type>, NAME "name" NAME <name>,
|
|
89
|
+
* NAME "config" SETTINGS{ NAME "role" VALUE u8 (0=DIAL, 1=LISTEN),
|
|
90
|
+
* NAME "port" VALUE u16 (LE),
|
|
91
|
+
* [ NAME "kind" NAME <kind> ],
|
|
92
|
+
* [ NAME "addr" NAME <addr> ],
|
|
93
|
+
* [ NAME "peer_named" VALUE u8 ],
|
|
94
|
+
* [ NAME "max_peers" VALUE u32 (LE) ] } }
|
|
95
|
+
* ```
|
|
96
|
+
*
|
|
97
|
+
* Key order matters only for readability — the C++ `config_reader_t` walk is
|
|
98
|
+
* order-insensitive and ignores unknown keys (forward-compat). `peer_named` / `max_peers`
|
|
99
|
+
* are **ws-private** keys parsed by the ws factory itself (ADR-0043 §5); they are ignored
|
|
100
|
+
* by other kinds and on a DIAL.
|
|
101
|
+
*
|
|
102
|
+
* Byte-pinned against the C++ emitter in `test/conn-spec.test.mjs`.
|
|
103
|
+
*
|
|
104
|
+
* @param o the connection to describe
|
|
105
|
+
* @returns the encoded SPEC TLV bytes — the payload of a `:children[]` field-write
|
|
106
|
+
* @throws {RangeError} on a DIAL with no `addr`, or a text field over 64 UTF-8 bytes
|
|
107
|
+
*/
|
|
108
|
+
export declare function encodeConnSpec(o: ConnSpecOptions): Uint8Array;
|
|
57
109
|
/** @brief Options for {@link encodeSubscriber}. Optional QoS/ACL/id children are deferred (ADR-0034). */
|
|
58
110
|
export interface SubscriberOptions {
|
|
59
111
|
}
|
package/dist/tlv.js
CHANGED
|
@@ -108,6 +108,111 @@ export function pathTlv(segments) {
|
|
|
108
108
|
trailer: null,
|
|
109
109
|
};
|
|
110
110
|
}
|
|
111
|
+
/**
|
|
112
|
+
* @brief Build a NAME TLV (`type=0x02`) for FREE TEXT — a SETTINGS key or a string
|
|
113
|
+
* config value — WITHOUT the path-segment validation {@link nameTlv} applies.
|
|
114
|
+
*
|
|
115
|
+
* A NAME TLV is the wire's string carrier in two unrelated roles: a PATH's
|
|
116
|
+
* addressing segment, and a SETTINGS record's key / string value. Only the first is
|
|
117
|
+
* bound by the reference/03 segment rules — a config value legitimately holds the
|
|
118
|
+
* reserved characters an address may not, most obviously an `addr` dotted-quad
|
|
119
|
+
* (`"127.0.0.1"`), which {@link nameTlv} would reject outright. The C++ emitter draws
|
|
120
|
+
* the same line: `emit_name` writes the bytes and validates nothing.
|
|
121
|
+
*
|
|
122
|
+
* Still bounded by the 64-byte NAME budget the addressing rules set.
|
|
123
|
+
*
|
|
124
|
+
* @param text the key or value text
|
|
125
|
+
* @returns a NAME TLV node (no trailer)
|
|
126
|
+
* @throws {RangeError} if the text is empty or over 64 UTF-8 bytes
|
|
127
|
+
*/
|
|
128
|
+
function textTlv(text) {
|
|
129
|
+
const bytes = utf8.encode(text);
|
|
130
|
+
if (bytes.length < 1 || bytes.length > MAX_SEGMENT_BYTES) {
|
|
131
|
+
throw new RangeError(`NAME text ${JSON.stringify(text)} must be 1..${MAX_SEGMENT_BYTES} UTF-8 bytes (got ${bytes.length})`);
|
|
132
|
+
}
|
|
133
|
+
return { type: TYPE.NAME, opt: opt(), payload: bytes, children: [], trailer: null };
|
|
134
|
+
}
|
|
135
|
+
/** @brief A little-endian VALUE TLV (`type=0x01`) of `width` bytes — a SETTINGS integer value. */
|
|
136
|
+
function valueLe(v, width) {
|
|
137
|
+
const bytes = new Uint8Array(width);
|
|
138
|
+
let rest = v;
|
|
139
|
+
for (let i = 0; i < width; i++) {
|
|
140
|
+
bytes[i] = rest & 0xff;
|
|
141
|
+
rest = Math.floor(rest / 256);
|
|
142
|
+
}
|
|
143
|
+
return { type: TYPE.VALUE, opt: opt(), payload: bytes, children: [], trailer: null };
|
|
144
|
+
}
|
|
145
|
+
/**
|
|
146
|
+
* @brief Build a connection-creation SPEC TLV (`type=0x0e`, PL=1) — the payload of the
|
|
147
|
+
* in-band `write /net:children[] += SPEC{…}` that brings a transport link up (ADR-0027,
|
|
148
|
+
* reference/13 §2).
|
|
149
|
+
*
|
|
150
|
+
* This is the wire form of the formation write a third party (typically a web UI holding
|
|
151
|
+
* delegated admin) issues on a device to create a link — the mechanism that makes a
|
|
152
|
+
* device-to-device connection with no third party in the data path.
|
|
153
|
+
*
|
|
154
|
+
* ```
|
|
155
|
+
* SPEC{ NAME "type" NAME <type>, NAME "name" NAME <name>,
|
|
156
|
+
* NAME "config" SETTINGS{ NAME "role" VALUE u8 (0=DIAL, 1=LISTEN),
|
|
157
|
+
* NAME "port" VALUE u16 (LE),
|
|
158
|
+
* [ NAME "kind" NAME <kind> ],
|
|
159
|
+
* [ NAME "addr" NAME <addr> ],
|
|
160
|
+
* [ NAME "peer_named" VALUE u8 ],
|
|
161
|
+
* [ NAME "max_peers" VALUE u32 (LE) ] } }
|
|
162
|
+
* ```
|
|
163
|
+
*
|
|
164
|
+
* Key order matters only for readability — the C++ `config_reader_t` walk is
|
|
165
|
+
* order-insensitive and ignores unknown keys (forward-compat). `peer_named` / `max_peers`
|
|
166
|
+
* are **ws-private** keys parsed by the ws factory itself (ADR-0043 §5); they are ignored
|
|
167
|
+
* by other kinds and on a DIAL.
|
|
168
|
+
*
|
|
169
|
+
* Byte-pinned against the C++ emitter in `test/conn-spec.test.mjs`.
|
|
170
|
+
*
|
|
171
|
+
* @param o the connection to describe
|
|
172
|
+
* @returns the encoded SPEC TLV bytes — the payload of a `:children[]` field-write
|
|
173
|
+
* @throws {RangeError} on a DIAL with no `addr`, or a text field over 64 UTF-8 bytes
|
|
174
|
+
*/
|
|
175
|
+
export function encodeConnSpec(o) {
|
|
176
|
+
if (o.role === 'dial' && !o.addr) {
|
|
177
|
+
throw new RangeError('a DIAL connection requires an addr (the built-in factories reject it as TYPE_MISMATCH)');
|
|
178
|
+
}
|
|
179
|
+
const cfg = [
|
|
180
|
+
textTlv('role'),
|
|
181
|
+
valueLe(o.role === 'listen' ? 1 : 0, 1),
|
|
182
|
+
textTlv('port'),
|
|
183
|
+
valueLe(o.port, 2),
|
|
184
|
+
];
|
|
185
|
+
if (o.kind !== undefined)
|
|
186
|
+
cfg.push(textTlv('kind'), textTlv(o.kind));
|
|
187
|
+
if (o.addr !== undefined)
|
|
188
|
+
cfg.push(textTlv('addr'), textTlv(o.addr));
|
|
189
|
+
if (o.peerNamed !== undefined)
|
|
190
|
+
cfg.push(textTlv('peer_named'), valueLe(o.peerNamed ? 1 : 0, 1));
|
|
191
|
+
if (o.maxPeers !== undefined)
|
|
192
|
+
cfg.push(textTlv('max_peers'), valueLe(o.maxPeers, 4));
|
|
193
|
+
const settings = {
|
|
194
|
+
type: TYPE.SETTINGS,
|
|
195
|
+
opt: opt({ pl: true }),
|
|
196
|
+
payload: new Uint8Array(0),
|
|
197
|
+
children: cfg,
|
|
198
|
+
trailer: null,
|
|
199
|
+
};
|
|
200
|
+
const spec = {
|
|
201
|
+
type: TYPE.SPEC,
|
|
202
|
+
opt: opt({ pl: true }),
|
|
203
|
+
payload: new Uint8Array(0),
|
|
204
|
+
children: [
|
|
205
|
+
textTlv('type'),
|
|
206
|
+
textTlv(o.type),
|
|
207
|
+
textTlv('name'),
|
|
208
|
+
textTlv(o.name),
|
|
209
|
+
textTlv('config'),
|
|
210
|
+
settings,
|
|
211
|
+
],
|
|
212
|
+
trailer: null,
|
|
213
|
+
};
|
|
214
|
+
return encode(spec);
|
|
215
|
+
}
|
|
111
216
|
/**
|
|
112
217
|
* @brief Build a SUBSCRIBER TLV (`type=0x04`, PL=1) wrapping a target PATH —
|
|
113
218
|
* the payload of a subscribe-write (reference/04 §Subscribe).
|
|
@@ -0,0 +1,178 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @brief Topology walk — the client-side projection of the decentralized graph
|
|
3
|
+
* (#409, ADR-0044 pt 3).
|
|
4
|
+
*
|
|
5
|
+
* A libtracer node's graph is rooted **at the node you are asking**. There is no
|
|
6
|
+
* global root: the whole-network view is a **projection assembled by a client**, by
|
|
7
|
+
* descending through transport vertices and composing routes
|
|
8
|
+
* ([reference/03](../../../../docs/reference/03-addressing.md): *"global scope is a
|
|
9
|
+
* logical view assembled by composing routes"*). ADR-0044 is emphatic that this is
|
|
10
|
+
* the client's job and never the core's:
|
|
11
|
+
*
|
|
12
|
+
* > separate paths stay separate; libtracer never matches device identities across
|
|
13
|
+
* > paths, at any layer, on any node. The deduplicated "real graph" is client/app
|
|
14
|
+
* > logic, keyed by an identity **it** chooses.
|
|
15
|
+
*
|
|
16
|
+
* This module is that logic. It walks `:children[]` under each node's `/net`, treats
|
|
17
|
+
* every connection NAME as a routable next hop, and stitches what it finds into a
|
|
18
|
+
* node/edge model a renderer can draw.
|
|
19
|
+
*
|
|
20
|
+
* ## Addressing (the part that surprises people)
|
|
21
|
+
*
|
|
22
|
+
* A connection's **routing key is its bare NAME**, not its `/net/<name>` vertex key.
|
|
23
|
+
* `/net/b` is where you *read* the link's state; `b` is the first `dst` segment that
|
|
24
|
+
* *routes through* it. So the walk composes routes as `[] → ['b'] → ['b','c']`, and
|
|
25
|
+
* reads each node's connections at `[...route, 'net']:children[]`. (reference/03 and
|
|
26
|
+
* /07 currently document a `/net/`-prefixed form that does not resolve — see #419.)
|
|
27
|
+
*
|
|
28
|
+
* ## Termination — read this before trusting a result
|
|
29
|
+
*
|
|
30
|
+
* **Without a node identity, this walk cannot terminate on its own.** A physical
|
|
31
|
+
* cycle (a→b→c→a) yields infinitely many *distinct* routes — `/b`, `/b/c`, `/b/c/a`,
|
|
32
|
+
* `/b/c/a/b`, … — and libtracer will never tell you that `/b` and `/c/a/b` are the
|
|
33
|
+
* same device. The router does not help either: the `ERROR{tr::path::invalid}` on a
|
|
34
|
+
* revisit that reference/03 promises **does not exist** (#420); loop-freedom holds
|
|
35
|
+
* only because a `dst` is consumed monotonically, which protects a *delivery*, not a
|
|
36
|
+
* *walk*.
|
|
37
|
+
*
|
|
38
|
+
* So termination has exactly two sources, and only one of them is sound:
|
|
39
|
+
*
|
|
40
|
+
* - **`identify` supplied** → nodes collapse by identity, the cycle closes, and the
|
|
41
|
+
* result is marked {@link TopologyGraph.authoritative}. This needs the node
|
|
42
|
+
* identity facet (#406 / RFC-0011), which does not exist yet.
|
|
43
|
+
* - **no `identify`** → the walk is bounded by {@link WalkOptions.maxDepth} and
|
|
44
|
+
* nothing else. Each route becomes its own node, one device appears many times,
|
|
45
|
+
* and the result is **non-authoritative**: a shape, not a map.
|
|
46
|
+
*
|
|
47
|
+
* The degraded mode is deliberately kept honest rather than made to look right by a
|
|
48
|
+
* heuristic. Guessing that two routes are one device — by matching a seeded name, a
|
|
49
|
+
* peer address, a value — would be exactly the identity-matching ADR-0044 forbids the
|
|
50
|
+
* core from doing, moved into the client and dressed up. When the answer is unknown,
|
|
51
|
+
* this says so.
|
|
52
|
+
*/
|
|
53
|
+
import type { LibtracerClient } from './client.js';
|
|
54
|
+
/** @brief One node of the stitched graph — a device, or (pre-identity) one route to one. */
|
|
55
|
+
export interface TopologyNode {
|
|
56
|
+
/** @brief Stable within a single walk: the identity when known, else the route. */
|
|
57
|
+
readonly id: string;
|
|
58
|
+
/**
|
|
59
|
+
* @brief Every route by which this node was reached, from the walk's vantage.
|
|
60
|
+
*
|
|
61
|
+
* More than one route means the walk **proved** they are the same device — which
|
|
62
|
+
* only ever happens when {@link WalkOptions.identify} is supplied. `[]` is the
|
|
63
|
+
* vantage node itself.
|
|
64
|
+
*/
|
|
65
|
+
readonly routes: string[][];
|
|
66
|
+
/** @brief The node's identity, or `null` when the walk could not read one. */
|
|
67
|
+
readonly identity: string | null;
|
|
68
|
+
}
|
|
69
|
+
/** @brief One connection, as seen from the node that owns it. */
|
|
70
|
+
export interface TopologyEdge {
|
|
71
|
+
/** @brief The {@link TopologyNode.id} owning the connection. */
|
|
72
|
+
readonly from: string;
|
|
73
|
+
/** @brief The {@link TopologyNode.id} the connection reaches. */
|
|
74
|
+
readonly to: string;
|
|
75
|
+
/** @brief The connection NAME at `from` — also the `dst` segment that routes here. */
|
|
76
|
+
readonly name: string;
|
|
77
|
+
}
|
|
78
|
+
/**
|
|
79
|
+
* @brief A connection the walk found but could not descend: a BUS link with live peers.
|
|
80
|
+
*
|
|
81
|
+
* A bus link (ADR-0044 — a transport serving many peers, e.g. a `peer_named` ws
|
|
82
|
+
* listener or a CAN segment) is a **dead end for this walk**, for a reason worth
|
|
83
|
+
* stating precisely because it looks like a bug otherwise:
|
|
84
|
+
*
|
|
85
|
+
* - Routing a `dst` through the **connection NAME** reaches the transport's `send()`,
|
|
86
|
+
* which **broadcasts to every peer**. One request then draws N replies, which
|
|
87
|
+
* corrupts a client's reply correlation — so descending by name is not merely
|
|
88
|
+
* imprecise, it is actively wrong.
|
|
89
|
+
* - The directed hop is the **enumerated peer name** (the registry's peer fallback).
|
|
90
|
+
* That works for a CAN bus, whose peers are named `n5`/`n7` — but a ws bus names its
|
|
91
|
+
* peers `<ip>:<port>`, and both `:` and `.` are **reserved characters that may not
|
|
92
|
+
* appear in a path segment** (reference/03). So a ws bus's peers are enumerable but
|
|
93
|
+
* **not addressable** by the path grammar, from any conforming client.
|
|
94
|
+
*
|
|
95
|
+
* The peers are therefore reported here — the walk knows they exist and can name them
|
|
96
|
+
* — but they are not nodes, because nothing behind them can be read.
|
|
97
|
+
*/
|
|
98
|
+
export interface TopologyBusPeers {
|
|
99
|
+
/** @brief The {@link TopologyNode.id} owning the bus connection. */
|
|
100
|
+
readonly at: string;
|
|
101
|
+
/** @brief The connection NAME of the bus link. */
|
|
102
|
+
readonly name: string;
|
|
103
|
+
/** @brief The peers it currently hears, as the transport names them. */
|
|
104
|
+
readonly peers: string[];
|
|
105
|
+
/** @brief True when every peer name is a legal path segment, so a directed hop is expressible. */
|
|
106
|
+
readonly routable: boolean;
|
|
107
|
+
}
|
|
108
|
+
/** @brief The assembled projection. */
|
|
109
|
+
export interface TopologyGraph {
|
|
110
|
+
readonly nodes: TopologyNode[];
|
|
111
|
+
readonly edges: TopologyEdge[];
|
|
112
|
+
/** @brief Bus links found but not descended, with the peers they hear (see {@link TopologyBusPeers}). */
|
|
113
|
+
readonly busLinks: TopologyBusPeers[];
|
|
114
|
+
/** @brief The {@link TopologyNode.id} of the vantage — the node the client is attached to. */
|
|
115
|
+
readonly root: string;
|
|
116
|
+
/** @brief The walk hit {@link WalkOptions.maxDepth} and stopped early: the graph is incomplete. */
|
|
117
|
+
readonly truncated: boolean;
|
|
118
|
+
/**
|
|
119
|
+
* @brief True only if EVERY node reported an identity, so nodes are really devices.
|
|
120
|
+
*
|
|
121
|
+
* When false the graph is a **shape, not a map**: one device may appear as several
|
|
122
|
+
* nodes, and a cycle appears as an unbounded chain truncated by `maxDepth`. Do not
|
|
123
|
+
* present a non-authoritative graph as the network's topology.
|
|
124
|
+
*/
|
|
125
|
+
readonly authoritative: boolean;
|
|
126
|
+
/** @brief Non-fatal problems (an unreadable node, a failed identity read). */
|
|
127
|
+
readonly warnings: string[];
|
|
128
|
+
}
|
|
129
|
+
/** @brief How a walk resolves node identity and how far it goes. */
|
|
130
|
+
export interface WalkOptions {
|
|
131
|
+
/**
|
|
132
|
+
* @brief Hop limit. Default 8.
|
|
133
|
+
*
|
|
134
|
+
* WITHOUT {@link WalkOptions.identify} this is the ONLY thing that stops the walk on
|
|
135
|
+
* a cyclic topology — keep it modest. A route may not exceed the protocol's 32-segment
|
|
136
|
+
* PATH cap in any case, and every hop costs a round trip across the whole route.
|
|
137
|
+
*/
|
|
138
|
+
readonly maxDepth?: number;
|
|
139
|
+
/** @brief The transport-vertex parent segment. Default `"net"`. */
|
|
140
|
+
readonly netRoot?: string;
|
|
141
|
+
/**
|
|
142
|
+
* @brief Read the identity of the node at `route`, or `null` if it has none.
|
|
143
|
+
*
|
|
144
|
+
* The dedup key ADR-0044 pt 3 leaves to the client. Supplying it is what makes a
|
|
145
|
+
* walk terminate on a cycle and what makes the result authoritative. The intended
|
|
146
|
+
* implementation reads the `:identity` facet (#406 / RFC-0011) and returns the
|
|
147
|
+
* public key; a keyless node returns `null` and stays route-distinct.
|
|
148
|
+
*
|
|
149
|
+
* A `null` for SOME nodes is handled: those stay route-distinct and the graph is
|
|
150
|
+
* reported non-authoritative.
|
|
151
|
+
*/
|
|
152
|
+
readonly identify?: (client: LibtracerClient, route: string[]) => Promise<string | null>;
|
|
153
|
+
/**
|
|
154
|
+
* @brief Return true to NOT descend through connection `name` at `route`.
|
|
155
|
+
*
|
|
156
|
+
* The walk cannot tell an inter-node link from the link it arrived on — every
|
|
157
|
+
* connection is just a NAME — so a caller that knows its own edge should skip it
|
|
158
|
+
* (e.g. a setup-edge web UI skipping the `ctrl` link back to itself).
|
|
159
|
+
*/
|
|
160
|
+
readonly skipLink?: (name: string, route: string[]) => boolean;
|
|
161
|
+
}
|
|
162
|
+
/** @brief `["b","c"]` → `"/b/c"`; `[]` → `"/"` (the vantage). */
|
|
163
|
+
export declare function routeKey(route: string[]): string;
|
|
164
|
+
/**
|
|
165
|
+
* @brief Walk the reachable topology from `client`'s vantage and stitch it into a graph.
|
|
166
|
+
*
|
|
167
|
+
* Breadth-first, so a truncated walk is a complete picture of the near network rather
|
|
168
|
+
* than one deep tendril. Each node costs one `readField` for its connections plus one
|
|
169
|
+
* {@link WalkOptions.identify} call, both routed across the full path to it.
|
|
170
|
+
*
|
|
171
|
+
* An unreadable node (a dead link, a denied read) becomes a warning and a leaf — the
|
|
172
|
+
* walk continues, because one unreachable branch is not a reason to lose the rest.
|
|
173
|
+
*
|
|
174
|
+
* @param client attached to the vantage node
|
|
175
|
+
* @param opts identity resolution and bounds
|
|
176
|
+
* @returns the projection — check {@link TopologyGraph.authoritative} before trusting it
|
|
177
|
+
*/
|
|
178
|
+
export declare function walkTopology(client: LibtracerClient, opts?: WalkOptions): Promise<TopologyGraph>;
|
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": {
|