@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/tlv.d.ts
CHANGED
|
@@ -1,27 +1,30 @@
|
|
|
1
1
|
import type { Opt, Tlv } from '@avatarsd-llc/libtracer';
|
|
2
2
|
/**
|
|
3
|
-
* A fully-cleared option byte (all flags false), with selected flags
|
|
3
|
+
* @brief A fully-cleared option byte (all flags false), with selected flags
|
|
4
|
+
* overridden.
|
|
5
|
+
*
|
|
4
6
|
* Shared by the payload builders here and the FWD/FIELD builders in `./fwd.ts`.
|
|
5
7
|
*
|
|
6
8
|
* @param over flags to set true (e.g. `{ pl: true }` for a structured TLV)
|
|
7
9
|
* @returns a complete {@link Opt}
|
|
8
10
|
*/
|
|
9
11
|
export declare function opt(over?: Partial<Opt>): Opt;
|
|
10
|
-
/** Options for {@link encodeValue}. Mirror the wire opt/trailer fields the codec pins. */
|
|
12
|
+
/** @brief Options for {@link encodeValue}. Mirror the wire opt/trailer fields the codec pins. */
|
|
11
13
|
export interface ValueOptions {
|
|
12
|
-
/** Use the 32-bit length field (opt.LL=1, 6-byte header). Default: 16-bit. */
|
|
14
|
+
/** @brief Use the 32-bit length field (opt.LL=1, 6-byte header). Default: 16-bit. */
|
|
13
15
|
longLength?: boolean;
|
|
14
|
-
/** Emit a CRC trailer over the payload (+ timestamp bytes). Default: none. */
|
|
16
|
+
/** @brief Emit a CRC trailer over the payload (+ timestamp bytes). Default: none. */
|
|
15
17
|
crc?: boolean;
|
|
16
|
-
/** With {@link ValueOptions.crc}, use CRC-16-CCITT (opt.CW=1) instead of CRC-32C. */
|
|
18
|
+
/** @brief With {@link ValueOptions.crc}, use CRC-16-CCITT (opt.CW=1) instead of CRC-32C. */
|
|
17
19
|
crc16?: boolean;
|
|
18
|
-
/** Absolute u64 wire timestamp (ns since epoch) → opt.TS=1, TF=0 (absolute). */
|
|
20
|
+
/** @brief Absolute u64 wire timestamp (ns since epoch) → opt.TS=1, TF=0 (absolute). */
|
|
19
21
|
timestampNs?: bigint;
|
|
20
|
-
/** Relative i32 wire timestamp (ns) → opt.TS=1, TF=1 (relative). Ignored if {@link ValueOptions.timestampNs} is set. */
|
|
22
|
+
/** @brief Relative i32 wire timestamp (ns) → opt.TS=1, TF=1 (relative). Ignored if {@link ValueOptions.timestampNs} is set. */
|
|
21
23
|
timestampRelNs?: number;
|
|
22
24
|
}
|
|
23
25
|
/**
|
|
24
|
-
* Build a VALUE TLV (`type=0x01`) carrying an opaque application payload.
|
|
26
|
+
* @brief Build a VALUE TLV (`type=0x01`) carrying an opaque application payload.
|
|
27
|
+
*
|
|
25
28
|
* Vector-pinned: `value-bool-true`, `value-ll-u32`, `value-ts-abs`.
|
|
26
29
|
*
|
|
27
30
|
* @param value the opaque payload bytes (the publisher/subscriber agree on the shape out-of-band)
|
|
@@ -30,8 +33,8 @@ export interface ValueOptions {
|
|
|
30
33
|
*/
|
|
31
34
|
export declare function encodeValue(value: Uint8Array, opts?: ValueOptions): Uint8Array;
|
|
32
35
|
/**
|
|
33
|
-
* Build a NAME TLV (`type=0x02`) for one path segment, after validating
|
|
34
|
-
* against the addressing rules (1..64 UTF-8 bytes, no reserved characters).
|
|
36
|
+
* @brief Build a NAME TLV (`type=0x02`) for one path segment, after validating
|
|
37
|
+
* it against the addressing rules (1..64 UTF-8 bytes, no reserved characters).
|
|
35
38
|
*
|
|
36
39
|
* @param segment the segment text
|
|
37
40
|
* @returns a NAME TLV node (no trailer)
|
|
@@ -39,22 +42,78 @@ export declare function encodeValue(value: Uint8Array, opts?: ValueOptions): Uin
|
|
|
39
42
|
*/
|
|
40
43
|
export declare function nameTlv(segment: string): Tlv;
|
|
41
44
|
/**
|
|
42
|
-
* Build a PATH TLV (`type=0x06`, PL=1) from path segments — a NAME child
|
|
43
|
-
* segment.
|
|
45
|
+
* @brief Build a PATH TLV (`type=0x06`, PL=1) from path segments — a NAME child
|
|
46
|
+
* per segment.
|
|
47
|
+
*
|
|
48
|
+
* Vector-pinned: `path-sensor-temp`; normative byte layout: spec/v1.md §3.1.
|
|
44
49
|
*
|
|
45
50
|
* @param segments the path segments, e.g. `["sensor", "temp"]` for `/sensor/temp`
|
|
46
51
|
* @returns the encoded PATH TLV bytes
|
|
47
52
|
* @throws {RangeError} on an empty path or an invalid segment
|
|
48
53
|
*/
|
|
49
54
|
export declare function encodePath(segments: string[]): Uint8Array;
|
|
50
|
-
/** The PATH TLV node (shared by {@link encodePath}, {@link encodeSubscriber}, and `./fwd.ts`). */
|
|
55
|
+
/** @brief The PATH TLV node (shared by {@link encodePath}, {@link encodeSubscriber}, and `./fwd.ts`). */
|
|
51
56
|
export declare function pathTlv(segments: string[]): Tlv;
|
|
52
|
-
/**
|
|
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;
|
|
109
|
+
/** @brief Options for {@link encodeSubscriber}. Optional QoS/ACL/id children are deferred (ADR-0034). */
|
|
53
110
|
export interface SubscriberOptions {
|
|
54
111
|
}
|
|
55
112
|
/**
|
|
56
|
-
* Build a SUBSCRIBER TLV (`type=0x04`, PL=1) wrapping a target PATH —
|
|
57
|
-
* payload of a subscribe-write (reference/04 §Subscribe).
|
|
113
|
+
* @brief Build a SUBSCRIBER TLV (`type=0x04`, PL=1) wrapping a target PATH —
|
|
114
|
+
* the payload of a subscribe-write (reference/04 §Subscribe).
|
|
115
|
+
*
|
|
116
|
+
* Vector-pinned:
|
|
58
117
|
* `subscriber-path` = `SUBSCRIBER{ PATH{ NAME "sensor", NAME "temp" } }`.
|
|
59
118
|
*
|
|
60
119
|
* The `targetPath` is the SUBSCRIBER's `target_path` child: where the producer
|
package/dist/tlv.js
CHANGED
|
@@ -1,25 +1,31 @@
|
|
|
1
1
|
// SPDX-License-Identifier: Apache-2.0
|
|
2
2
|
// SPDX-FileCopyrightText: Copyright 2026 avatarsd LLC
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
3
|
+
/**
|
|
4
|
+
* @brief Pure, side-effect-free TLV builders for the libtracer client SDK
|
|
5
|
+
* (#56, ADR-0034).
|
|
6
|
+
*
|
|
7
|
+
* Each function produces the EXACT wire bytes the shared conformance vectors
|
|
8
|
+
* pin (`tests/conformance/vectors/v1/`) via the cross-validated core codec
|
|
9
|
+
* (`@avatarsd-llc/libtracer`) — nothing here invents wire structure:
|
|
10
|
+
*
|
|
11
|
+
* - encodeValue -> VALUE TLV (0x01) : value-bool-true / value-ll-u32 / value-ts-abs
|
|
12
|
+
* - encodePath -> PATH TLV (0x06, PL=1) : path-sensor-temp + spec/v1.md §3.1
|
|
13
|
+
* - encodeSubscriber -> SUBSCRIBER TLV (0x04) : subscriber-path (the subscribe-write payload)
|
|
14
|
+
*
|
|
15
|
+
* These are the payload TLVs. The path-ADDRESSED request envelope (verb +
|
|
16
|
+
* destination vertex:field) is the FWD/FIELD frame RFC-0004 (spec §3) fixes —
|
|
17
|
+
* it is built in `./fwd.ts`, not here; these payloads ride inside it.
|
|
18
|
+
*/
|
|
15
19
|
import { TYPE, encode } from '@avatarsd-llc/libtracer';
|
|
16
|
-
/** Path-segment constraints from reference/03-addressing.md §path syntax. */
|
|
20
|
+
/** @brief Path-segment constraints from reference/03-addressing.md §path syntax. */
|
|
17
21
|
const MAX_SEGMENT_BYTES = 64;
|
|
18
|
-
/** Reserved characters that MUST NOT appear inside a NAME segment (reference/03, /05 §NAME). */
|
|
22
|
+
/** @brief Reserved characters that MUST NOT appear inside a NAME segment (reference/03, /05 §NAME). */
|
|
19
23
|
const RESERVED_SEGMENT_CHARS = /[/:.[\]*?]/;
|
|
20
24
|
const utf8 = new TextEncoder();
|
|
21
25
|
/**
|
|
22
|
-
* A fully-cleared option byte (all flags false), with selected flags
|
|
26
|
+
* @brief A fully-cleared option byte (all flags false), with selected flags
|
|
27
|
+
* overridden.
|
|
28
|
+
*
|
|
23
29
|
* Shared by the payload builders here and the FWD/FIELD builders in `./fwd.ts`.
|
|
24
30
|
*
|
|
25
31
|
* @param over flags to set true (e.g. `{ pl: true }` for a structured TLV)
|
|
@@ -29,7 +35,8 @@ export function opt(over = {}) {
|
|
|
29
35
|
return { pl: false, ts: false, cr: false, ll: false, cw: false, tf: false, ...over };
|
|
30
36
|
}
|
|
31
37
|
/**
|
|
32
|
-
* Build a VALUE TLV (`type=0x01`) carrying an opaque application payload.
|
|
38
|
+
* @brief Build a VALUE TLV (`type=0x01`) carrying an opaque application payload.
|
|
39
|
+
*
|
|
33
40
|
* Vector-pinned: `value-bool-true`, `value-ll-u32`, `value-ts-abs`.
|
|
34
41
|
*
|
|
35
42
|
* @param value the opaque payload bytes (the publisher/subscriber agree on the shape out-of-band)
|
|
@@ -57,8 +64,8 @@ export function encodeValue(value, opts = {}) {
|
|
|
57
64
|
return encode(tlv);
|
|
58
65
|
}
|
|
59
66
|
/**
|
|
60
|
-
* Build a NAME TLV (`type=0x02`) for one path segment, after validating
|
|
61
|
-
* against the addressing rules (1..64 UTF-8 bytes, no reserved characters).
|
|
67
|
+
* @brief Build a NAME TLV (`type=0x02`) for one path segment, after validating
|
|
68
|
+
* it against the addressing rules (1..64 UTF-8 bytes, no reserved characters).
|
|
62
69
|
*
|
|
63
70
|
* @param segment the segment text
|
|
64
71
|
* @returns a NAME TLV node (no trailer)
|
|
@@ -75,8 +82,10 @@ export function nameTlv(segment) {
|
|
|
75
82
|
return { type: TYPE.NAME, opt: opt(), payload: bytes, children: [], trailer: null };
|
|
76
83
|
}
|
|
77
84
|
/**
|
|
78
|
-
* Build a PATH TLV (`type=0x06`, PL=1) from path segments — a NAME child
|
|
79
|
-
* segment.
|
|
85
|
+
* @brief Build a PATH TLV (`type=0x06`, PL=1) from path segments — a NAME child
|
|
86
|
+
* per segment.
|
|
87
|
+
*
|
|
88
|
+
* Vector-pinned: `path-sensor-temp`; normative byte layout: spec/v1.md §3.1.
|
|
80
89
|
*
|
|
81
90
|
* @param segments the path segments, e.g. `["sensor", "temp"]` for `/sensor/temp`
|
|
82
91
|
* @returns the encoded PATH TLV bytes
|
|
@@ -85,7 +94,7 @@ export function nameTlv(segment) {
|
|
|
85
94
|
export function encodePath(segments) {
|
|
86
95
|
return encode(pathTlv(segments));
|
|
87
96
|
}
|
|
88
|
-
/** The PATH TLV node (shared by {@link encodePath}, {@link encodeSubscriber}, and `./fwd.ts`). */
|
|
97
|
+
/** @brief The PATH TLV node (shared by {@link encodePath}, {@link encodeSubscriber}, and `./fwd.ts`). */
|
|
89
98
|
export function pathTlv(segments) {
|
|
90
99
|
if (segments.length < 1)
|
|
91
100
|
throw new RangeError('a path must have at least one segment');
|
|
@@ -100,8 +109,115 @@ export function pathTlv(segments) {
|
|
|
100
109
|
};
|
|
101
110
|
}
|
|
102
111
|
/**
|
|
103
|
-
* Build a
|
|
104
|
-
*
|
|
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
|
+
}
|
|
216
|
+
/**
|
|
217
|
+
* @brief Build a SUBSCRIBER TLV (`type=0x04`, PL=1) wrapping a target PATH —
|
|
218
|
+
* the payload of a subscribe-write (reference/04 §Subscribe).
|
|
219
|
+
*
|
|
220
|
+
* Vector-pinned:
|
|
105
221
|
* `subscriber-path` = `SUBSCRIBER{ PATH{ NAME "sensor", NAME "temp" } }`.
|
|
106
222
|
*
|
|
107
223
|
* The `targetPath` is the SUBSCRIBER's `target_path` child: where the producer
|
|
@@ -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>;
|