@avatarsd-llc/libtracer-client 0.1.0 → 0.4.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/dist/client.d.ts +94 -35
- package/dist/client.js +162 -57
- package/dist/fwd.d.ts +92 -47
- package/dist/fwd.js +126 -44
- package/dist/index.d.ts +17 -2
- package/dist/index.js +17 -13
- package/dist/tlv.d.ts +23 -16
- package/dist/tlv.js +34 -23
- package/package.json +3 -3
package/dist/client.d.ts
CHANGED
|
@@ -2,44 +2,90 @@ import type { Tlv } from '@avatarsd-llc/libtracer';
|
|
|
2
2
|
import type { ValueOptions, SubscriberOptions } from './tlv.js';
|
|
3
3
|
import type { FieldLevel } from './fwd.js';
|
|
4
4
|
/**
|
|
5
|
-
* The minimal connection seam the client drives.
|
|
6
|
-
*
|
|
7
|
-
*
|
|
5
|
+
* @brief The minimal connection seam the client drives.
|
|
6
|
+
*
|
|
7
|
+
* `TransportWs` from `@avatarsd-llc/libtracer-ws` satisfies this structurally
|
|
8
|
+
* (so the client never imports it — keeping the transport an optional peer).
|
|
8
9
|
*/
|
|
9
10
|
export interface ClientTransport {
|
|
10
|
-
/** Put one complete libtracer TLV frame on the wire. */
|
|
11
|
+
/** @brief Put one complete libtracer TLV frame on the wire. */
|
|
11
12
|
send(frame: Uint8Array): void;
|
|
12
|
-
/** Register (or clear with `null`) the inbound-frame receiver. */
|
|
13
|
+
/** @brief Register (or clear with `null`) the inbound-frame receiver. */
|
|
13
14
|
onFrame(receiver: ((bytes: Uint8Array) => void) | null): void;
|
|
15
|
+
/**
|
|
16
|
+
* @brief Register (or clear with `null`) the connection-closed notifier — invoked once
|
|
17
|
+
* when the underlying connection closes or errors out, with the cause when one
|
|
18
|
+
* is known. OPTIONAL: a transport without it never notifies the client of a
|
|
19
|
+
* close, so pending requests only fail by timeout.
|
|
20
|
+
*/
|
|
21
|
+
onClose?(handler: ((cause?: Error) => void) | null): void;
|
|
14
22
|
}
|
|
15
|
-
/** A delivered VALUE: its opaque payload bytes plus the decoded TLV it came from. */
|
|
23
|
+
/** @brief A delivered VALUE: its opaque payload bytes plus the decoded TLV it came from. */
|
|
16
24
|
export type ValueHandler = (value: Uint8Array, tlv: Tlv) => void;
|
|
17
|
-
/** Detach a {@link LibtracerClient.subscribe} handler (a local detach — see its docs). */
|
|
25
|
+
/** @brief Detach a {@link LibtracerClient.subscribe} handler (a local detach — see its docs). */
|
|
18
26
|
export type Unsubscribe = () => void;
|
|
19
|
-
/** Options for {@link LibtracerClient}. */
|
|
27
|
+
/** @brief Options for {@link LibtracerClient}. */
|
|
20
28
|
export interface ClientOptions {
|
|
21
29
|
/**
|
|
22
|
-
* The originator's reply endpoint, as path segments — the `src` route seeded on
|
|
30
|
+
* @brief The originator's reply endpoint, as path segments — the `src` route seeded on
|
|
23
31
|
* every outbound FWD and the SUBSCRIBER `target` on a subscribe. Over a single
|
|
24
32
|
* ws hop the responder replies back over the same link, so any 1+-segment path
|
|
25
33
|
* is fine; default `["client"]`.
|
|
26
34
|
*/
|
|
27
35
|
readonly replyEndpoint?: string[];
|
|
36
|
+
/**
|
|
37
|
+
* @brief Per-request deadline in milliseconds: a one-shot op whose FWD{REPLY} has not
|
|
38
|
+
* arrived within this window rejects with a timeout {@link Error}. Default
|
|
39
|
+
* 10 000 ms; pass `0` (or `Infinity`) to disable the deadline.
|
|
40
|
+
*/
|
|
41
|
+
readonly requestTimeoutMs?: number;
|
|
28
42
|
}
|
|
29
43
|
/**
|
|
30
|
-
* A `kind=ERROR` FWD reply, surfaced as a typed rejection.
|
|
31
|
-
*
|
|
44
|
+
* @brief A `kind=ERROR` FWD reply, surfaced as a typed rejection.
|
|
45
|
+
*
|
|
46
|
+
* `.code` is the registered u16 wire ERROR code (see {@link FWD_ERROR});
|
|
47
|
+
* `.codeName` its symbolic name.
|
|
32
48
|
*/
|
|
33
49
|
export declare class FwdError extends Error {
|
|
34
|
-
/** The wire ERROR code (a {@link FWD_ERROR} value). */
|
|
50
|
+
/** @brief The registered u16 wire ERROR code (a {@link FWD_ERROR} value). */
|
|
35
51
|
readonly code: number;
|
|
36
|
-
/** The symbolic ERROR name (e.g. `"NOT_FOUND"`, `"TIMEOUT"`). */
|
|
52
|
+
/** @brief The symbolic ERROR name (e.g. `"NOT_FOUND"`, `"TIMEOUT"`). */
|
|
37
53
|
readonly codeName: string;
|
|
38
|
-
/**
|
|
39
|
-
|
|
54
|
+
/**
|
|
55
|
+
* @brief The canonical `tr::<concept>::<error>` namespace path (RFC-0002 §A) — from
|
|
56
|
+
* the frozen registry for a registered code, or carried verbatim by a
|
|
57
|
+
* string-form (NAME identity) ERROR reply. `null` when neither is known.
|
|
58
|
+
*/
|
|
59
|
+
readonly path: string | null;
|
|
60
|
+
/**
|
|
61
|
+
* @brief Build the typed rejection from either wire identity form.
|
|
62
|
+
* @param code the registered u16 code carried in the reply's
|
|
63
|
+
* `STATUS{ ERROR{ VALUE u16 } }` per RFC-0002 (0 when absent)
|
|
64
|
+
* @param path the string-form `tr::…` identity when the reply carried a
|
|
65
|
+
* `STATUS{ ERROR{ NAME utf8-path } }` instead of a registered code
|
|
66
|
+
*/
|
|
67
|
+
constructor(code: number, path?: string | null);
|
|
40
68
|
}
|
|
41
69
|
/**
|
|
42
|
-
*
|
|
70
|
+
* @brief An inbound ADVERTISE (`0x11`) / COMPACT (`0x12`) frame reached this
|
|
71
|
+
* client, which does not implement the RFC-0004 compact (route-handle) delivery
|
|
72
|
+
* flow yet.
|
|
73
|
+
*
|
|
74
|
+
* Routed to {@link LibtracerClient.onError} so the failure is diagnosable
|
|
75
|
+
* instead of a silent drop; the sender should fall back to plain FWD delivery.
|
|
76
|
+
*/
|
|
77
|
+
export declare class CompactFlowError extends Error {
|
|
78
|
+
/** @brief The offending wire type code (`0x11` ADVERTISE or `0x12` COMPACT). */
|
|
79
|
+
readonly frameType: number;
|
|
80
|
+
/**
|
|
81
|
+
* @brief Name the offending frame kind in the error message.
|
|
82
|
+
* @param frameType the inbound frame's wire type code
|
|
83
|
+
*/
|
|
84
|
+
constructor(frameType: number);
|
|
85
|
+
}
|
|
86
|
+
/**
|
|
87
|
+
* @brief A WebSocket/transport-agnostic libtracer client over the RFC-0004
|
|
88
|
+
* `FWD` plane.
|
|
43
89
|
*
|
|
44
90
|
* Construct over any {@link ClientTransport}; issue {@link read} / {@link write} /
|
|
45
91
|
* {@link await_} / {@link readField} (resolving the responder's FWD{REPLY}), or
|
|
@@ -50,32 +96,39 @@ export declare class FwdError extends Error {
|
|
|
50
96
|
export declare class LibtracerClient {
|
|
51
97
|
private readonly transport;
|
|
52
98
|
private readonly replyEndpoint;
|
|
99
|
+
private readonly requestTimeoutMs;
|
|
53
100
|
private readonly valueHandlers;
|
|
54
101
|
private readonly pending;
|
|
55
102
|
private errorHandler;
|
|
103
|
+
private closed;
|
|
56
104
|
/**
|
|
105
|
+
* @brief Bind to the injected transport and start receiving frames.
|
|
57
106
|
* @param transport the injected connection seam (e.g. a connected `TransportWs`).
|
|
58
|
-
* @param options optional reply-endpoint
|
|
107
|
+
* @param options optional reply-endpoint / request-timeout overrides.
|
|
59
108
|
*/
|
|
60
109
|
constructor(transport: ClientTransport, options?: ClientOptions);
|
|
110
|
+
/** @brief Reject every pending request when the transport closes; fail-fast afterwards. */
|
|
111
|
+
private handleClose;
|
|
61
112
|
/**
|
|
62
|
-
* Register a handler for inbound VALUE deliveries.
|
|
63
|
-
*
|
|
64
|
-
*
|
|
113
|
+
* @brief Register a handler for inbound VALUE deliveries.
|
|
114
|
+
*
|
|
115
|
+
* A delivery is a FWD{WRITE} (delivery-is-a-write, RFC-0004 §D); a bare VALUE
|
|
116
|
+
* or a single ROUTER wrapper is also accepted. Multiple handlers may be
|
|
117
|
+
* registered; each receives every delivery.
|
|
65
118
|
*/
|
|
66
119
|
onValue(handler: ValueHandler): void;
|
|
67
|
-
/** Register the sink for inbound decode failures and other client-side errors. */
|
|
120
|
+
/** @brief Register the sink for inbound decode failures and other client-side errors. */
|
|
68
121
|
onError(handler: (err: Error) => void): void;
|
|
69
|
-
/** Decode one inbound frame: correlate a REPLY, deliver a write, or shed/deliver a VALUE. */
|
|
122
|
+
/** @brief Decode one inbound frame: correlate a REPLY, deliver a write, or shed/deliver a VALUE. */
|
|
70
123
|
private dispatch;
|
|
71
124
|
private deliver;
|
|
72
125
|
private emitError;
|
|
73
|
-
/** Send a one-shot FWD and resolve its FWD{REPLY} (FIFO correlation). */
|
|
126
|
+
/** @brief Send a one-shot FWD and resolve its FWD{REPLY} (FIFO correlation). */
|
|
74
127
|
private request;
|
|
75
|
-
/** Turn a RESULT reply into its payload TLV; throw {@link FwdError} on ERROR. */
|
|
128
|
+
/** @brief Turn a RESULT reply into its payload TLV; throw {@link FwdError} on ERROR. */
|
|
76
129
|
private result;
|
|
77
130
|
/**
|
|
78
|
-
* Read a remote vertex's value: `FWD{ op=READ, dst=path, src=<reply-ep> }`.
|
|
131
|
+
* @brief Read a remote vertex's value: `FWD{ op=READ, dst=path, src=<reply-ep> }`.
|
|
79
132
|
*
|
|
80
133
|
* @param path the destination path (a `/`-string like `"/sensor/temp"`, or segments)
|
|
81
134
|
* @returns the RESULT's value TLV (decoded)
|
|
@@ -83,7 +136,9 @@ export declare class LibtracerClient {
|
|
|
83
136
|
*/
|
|
84
137
|
read(path: string | string[]): Promise<Tlv>;
|
|
85
138
|
/**
|
|
86
|
-
* Read a remote vertex's `:field`: `FWD{ op=READ, dst=path,
|
|
139
|
+
* @brief Read a remote vertex's `:field`: `FWD{ op=READ, dst=path,
|
|
140
|
+
* FIELD=selector, src=… }`.
|
|
141
|
+
*
|
|
87
142
|
* A whole-array field (e.g. `:subscribers[]`) resolves to a POINT of the slot TLVs.
|
|
88
143
|
*
|
|
89
144
|
* @param path the destination path (string or segments)
|
|
@@ -93,7 +148,7 @@ export declare class LibtracerClient {
|
|
|
93
148
|
*/
|
|
94
149
|
readField(path: string | string[], selector: string | FieldLevel[]): Promise<Tlv>;
|
|
95
150
|
/**
|
|
96
|
-
* Write a value to a remote vertex: `FWD{ op=WRITE, dst=path, src=…, payload }`.
|
|
151
|
+
* @brief Write a value to a remote vertex: `FWD{ op=WRITE, dst=path, src=…, payload }`.
|
|
97
152
|
*
|
|
98
153
|
* @param path the destination path (string or segments)
|
|
99
154
|
* @param valueTLV a complete VALUE TLV's bytes (e.g. from {@link encodeValue})
|
|
@@ -102,9 +157,11 @@ export declare class LibtracerClient {
|
|
|
102
157
|
*/
|
|
103
158
|
write(path: string | string[], valueTLV: Uint8Array): Promise<void>;
|
|
104
159
|
/**
|
|
105
|
-
* Block for the next write to a remote vertex: `FWD{ op=AWAIT,
|
|
106
|
-
* await_timeout? }`.
|
|
107
|
-
*
|
|
160
|
+
* @brief Block for the next write to a remote vertex: `FWD{ op=AWAIT,
|
|
161
|
+
* dst=path, src=…, await_timeout? }`.
|
|
162
|
+
*
|
|
163
|
+
* Named `await_` because `await` is reserved; an `await` alias is installed
|
|
164
|
+
* on the prototype for the RFC-0004-spelled call site.
|
|
108
165
|
*
|
|
109
166
|
* @param path the destination path (string or segments)
|
|
110
167
|
* @param timeoutNs the await timeout in ns (absent ⇒ the responder's 1 s default)
|
|
@@ -113,8 +170,10 @@ export declare class LibtracerClient {
|
|
|
113
170
|
*/
|
|
114
171
|
await_(path: string | string[], timeoutNs?: bigint): Promise<Tlv>;
|
|
115
172
|
/**
|
|
116
|
-
* Subscribe to a remote producer: a `WRITE` of a `SUBSCRIBER{
|
|
117
|
-
* into `producer:subscribers[]` (RFC-0004 §C/§D —
|
|
173
|
+
* @brief Subscribe to a remote producer: a `WRITE` of a `SUBSCRIBER{
|
|
174
|
+
* target=<reply-ep> }` into `producer:subscribers[]` (RFC-0004 §C/§D —
|
|
175
|
+
* subscribe is a field-write).
|
|
176
|
+
*
|
|
118
177
|
* Once the responder acks, `handler` fires for every inbound VALUE delivery.
|
|
119
178
|
*
|
|
120
179
|
* SCOPE: this issues the SUBSCRIBER and receives VALUEs the producer directly
|
|
@@ -130,10 +189,10 @@ export declare class LibtracerClient {
|
|
|
130
189
|
* @throws {FwdError} when the subscribe-write is rejected
|
|
131
190
|
*/
|
|
132
191
|
subscribe(producerPath: string | string[], handler: ValueHandler): Promise<Unsubscribe>;
|
|
133
|
-
/** {@link encodeValue} — the exact VALUE TLV bytes, independently of any transport. */
|
|
192
|
+
/** @brief {@link encodeValue} — the exact VALUE TLV bytes, independently of any transport. */
|
|
134
193
|
static encodeValue(value: Uint8Array, opts?: ValueOptions): Uint8Array;
|
|
135
|
-
/** {@link encodePath} — the exact PATH TLV bytes. */
|
|
194
|
+
/** @brief {@link encodePath} — the exact PATH TLV bytes. */
|
|
136
195
|
static encodePath(segments: string[]): Uint8Array;
|
|
137
|
-
/** {@link encodeSubscriber} — the exact SUBSCRIBER TLV bytes. */
|
|
196
|
+
/** @brief {@link encodeSubscriber} — the exact SUBSCRIBER TLV bytes. */
|
|
138
197
|
static encodeSubscriber(targetPath: string[], opts?: SubscriberOptions): Uint8Array;
|
|
139
198
|
}
|
package/dist/client.js
CHANGED
|
@@ -1,46 +1,96 @@
|
|
|
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
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
3
|
+
/**
|
|
4
|
+
* @brief LibtracerClient — the experimental browser/Node client SDK (#56,
|
|
5
|
+
* ADR-0034).
|
|
6
|
+
*
|
|
7
|
+
* It composes the cross-validated codec (@avatarsd-llc/libtracer) over an
|
|
8
|
+
* INJECTED transport seam (`@avatarsd-llc/libtracer-ws`'s TransportWs satisfies
|
|
9
|
+
* it structurally), so it is browser/Node-agnostic and testable against an
|
|
10
|
+
* in-memory fake with no live socket.
|
|
11
|
+
*
|
|
12
|
+
* Now that RFC-0004 fills spec §3 (the remote-operation envelope ADR-0034
|
|
13
|
+
* deferred), the client speaks the path-addressed higher operations over `FWD`:
|
|
14
|
+
* - read(path) — FWD{ op=READ, dst=path, src=<reply-ep> }
|
|
15
|
+
* - write(path, valueTLV) — FWD{ op=WRITE, dst=path, src=…, payload }
|
|
16
|
+
* - await(path, ns?) — FWD{ op=AWAIT, dst=path, src=…, await_timeout? }
|
|
17
|
+
* - readField(path, sel) — FWD{ op=READ, dst=path, FIELD=sel, src=… }
|
|
18
|
+
* - subscribe(prod, h) — FWD{ op=WRITE, dst=prod, FIELD=:subscribers[],
|
|
19
|
+
* src=…, payload=SUBSCRIBER{ target=<reply-ep> } }
|
|
20
|
+
* Each one-shot op sends a FWD and resolves the FWD{REPLY} the responder
|
|
21
|
+
* source-routes back (RESULT → the value, ERROR → a typed {@link FwdError}).
|
|
22
|
+
*
|
|
23
|
+
* Reply correlation is the transport's concern (RFC-0004 §D): over a single ws
|
|
24
|
+
* link the responder replies in request order, so a simple per-link FIFO of
|
|
25
|
+
* pending requests matches each REPLY to its request. `FWD` stays pure — no
|
|
26
|
+
* end-to-end correlation id.
|
|
27
|
+
*/
|
|
25
28
|
import { TYPE, decode, CodecError } from '@avatarsd-llc/libtracer';
|
|
26
29
|
import { encodeValue, encodePath, encodeSubscriber } from './tlv.js';
|
|
27
|
-
import { FWD_OP, FWD_KIND, encodeFwd, parseFwdTlv, replyErrorCode, fwdErrorName } from './fwd.js';
|
|
30
|
+
import { FWD_OP, FWD_KIND, encodeFwd, parseFwdTlv, replyErrorCode, replyErrorPath, fwdErrorName, fwdErrorPath, fwdErrorCodeForPath, } from './fwd.js';
|
|
28
31
|
/**
|
|
29
|
-
* A `kind=ERROR` FWD reply, surfaced as a typed rejection.
|
|
30
|
-
*
|
|
32
|
+
* @brief A `kind=ERROR` FWD reply, surfaced as a typed rejection.
|
|
33
|
+
*
|
|
34
|
+
* `.code` is the registered u16 wire ERROR code (see {@link FWD_ERROR});
|
|
35
|
+
* `.codeName` its symbolic name.
|
|
31
36
|
*/
|
|
32
37
|
export class FwdError extends Error {
|
|
33
|
-
/**
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
38
|
+
/**
|
|
39
|
+
* @brief Build the typed rejection from either wire identity form.
|
|
40
|
+
* @param code the registered u16 code carried in the reply's
|
|
41
|
+
* `STATUS{ ERROR{ VALUE u16 } }` per RFC-0002 (0 when absent)
|
|
42
|
+
* @param path the string-form `tr::…` identity when the reply carried a
|
|
43
|
+
* `STATUS{ ERROR{ NAME utf8-path } }` instead of a registered code
|
|
44
|
+
*/
|
|
45
|
+
constructor(code, path) {
|
|
46
|
+
// A string-form identity that names a registered error resolves back to its
|
|
47
|
+
// code, so both wire forms surface identically (never UNKNOWN).
|
|
48
|
+
const resolved = code === 0 && path ? fwdErrorCodeForPath(path) : code;
|
|
49
|
+
const trPath = path ?? fwdErrorPath(resolved);
|
|
50
|
+
const name = resolved === 0 && trPath ? trPath : fwdErrorName(resolved);
|
|
51
|
+
super(`FWD reply ERROR ${name} (0x${resolved.toString(16).padStart(4, '0')})`);
|
|
37
52
|
this.name = 'FwdError';
|
|
38
|
-
this.code =
|
|
53
|
+
this.code = resolved;
|
|
39
54
|
this.codeName = name;
|
|
55
|
+
this.path = trPath;
|
|
40
56
|
}
|
|
41
57
|
}
|
|
58
|
+
/** @brief ADVERTISE (`0x11`) — a route-handle label binding (RFC-0004 route handles). */
|
|
59
|
+
const TYPE_ADVERTISE = 0x11;
|
|
60
|
+
/** @brief COMPACT (`0x12`) — a label-compacted delivery (RFC-0004 route handles). */
|
|
61
|
+
const TYPE_COMPACT = 0x12;
|
|
42
62
|
/**
|
|
43
|
-
*
|
|
63
|
+
* @brief An inbound ADVERTISE (`0x11`) / COMPACT (`0x12`) frame reached this
|
|
64
|
+
* client, which does not implement the RFC-0004 compact (route-handle) delivery
|
|
65
|
+
* flow yet.
|
|
66
|
+
*
|
|
67
|
+
* Routed to {@link LibtracerClient.onError} so the failure is diagnosable
|
|
68
|
+
* instead of a silent drop; the sender should fall back to plain FWD delivery.
|
|
69
|
+
*/
|
|
70
|
+
export class CompactFlowError extends Error {
|
|
71
|
+
/**
|
|
72
|
+
* @brief Name the offending frame kind in the error message.
|
|
73
|
+
* @param frameType the inbound frame's wire type code
|
|
74
|
+
*/
|
|
75
|
+
constructor(frameType) {
|
|
76
|
+
const kind = frameType === TYPE_ADVERTISE ? 'ADVERTISE' : 'COMPACT';
|
|
77
|
+
super(`inbound ${kind} (0x${frameType.toString(16).padStart(2, '0')}) dropped: ` +
|
|
78
|
+
'compact (route-handle) delivery is not supported by this client yet');
|
|
79
|
+
this.name = 'CompactFlowError';
|
|
80
|
+
this.frameType = frameType;
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
/** @brief Settle a pending entry: clear its deadline and mark it consumed. */
|
|
84
|
+
function settle(p) {
|
|
85
|
+
p.settled = true;
|
|
86
|
+
if (p.timer !== null) {
|
|
87
|
+
clearTimeout(p.timer);
|
|
88
|
+
p.timer = null;
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
/**
|
|
92
|
+
* @brief A WebSocket/transport-agnostic libtracer client over the RFC-0004
|
|
93
|
+
* `FWD` plane.
|
|
44
94
|
*
|
|
45
95
|
* Construct over any {@link ClientTransport}; issue {@link read} / {@link write} /
|
|
46
96
|
* {@link await_} / {@link readField} (resolving the responder's FWD{REPLY}), or
|
|
@@ -50,31 +100,48 @@ export class FwdError extends Error {
|
|
|
50
100
|
*/
|
|
51
101
|
export class LibtracerClient {
|
|
52
102
|
/**
|
|
103
|
+
* @brief Bind to the injected transport and start receiving frames.
|
|
53
104
|
* @param transport the injected connection seam (e.g. a connected `TransportWs`).
|
|
54
|
-
* @param options optional reply-endpoint
|
|
105
|
+
* @param options optional reply-endpoint / request-timeout overrides.
|
|
55
106
|
*/
|
|
56
107
|
constructor(transport, options = {}) {
|
|
57
108
|
this.valueHandlers = new Set();
|
|
58
109
|
this.pending = [];
|
|
59
110
|
this.errorHandler = null;
|
|
111
|
+
this.closed = null;
|
|
60
112
|
this.transport = transport;
|
|
61
113
|
this.replyEndpoint = [...(options.replyEndpoint ?? ['client'])];
|
|
114
|
+
this.requestTimeoutMs = options.requestTimeoutMs ?? 10000;
|
|
62
115
|
transport.onFrame((bytes) => this.dispatch(bytes));
|
|
116
|
+
transport.onClose?.((cause) => this.handleClose(cause));
|
|
117
|
+
}
|
|
118
|
+
/** @brief Reject every pending request when the transport closes; fail-fast afterwards. */
|
|
119
|
+
handleClose(cause) {
|
|
120
|
+
this.closed = new Error('transport closed' + (cause ? `: ${cause.message}` : ''));
|
|
121
|
+
for (const p of this.pending) {
|
|
122
|
+
if (p.settled)
|
|
123
|
+
continue;
|
|
124
|
+
settle(p);
|
|
125
|
+
p.reject(this.closed);
|
|
126
|
+
}
|
|
127
|
+
this.pending.length = 0;
|
|
63
128
|
}
|
|
64
129
|
/* ---------------------------------------------------------- inbound --- */
|
|
65
130
|
/**
|
|
66
|
-
* Register a handler for inbound VALUE deliveries.
|
|
67
|
-
*
|
|
68
|
-
*
|
|
131
|
+
* @brief Register a handler for inbound VALUE deliveries.
|
|
132
|
+
*
|
|
133
|
+
* A delivery is a FWD{WRITE} (delivery-is-a-write, RFC-0004 §D); a bare VALUE
|
|
134
|
+
* or a single ROUTER wrapper is also accepted. Multiple handlers may be
|
|
135
|
+
* registered; each receives every delivery.
|
|
69
136
|
*/
|
|
70
137
|
onValue(handler) {
|
|
71
138
|
this.valueHandlers.add(handler);
|
|
72
139
|
}
|
|
73
|
-
/** Register the sink for inbound decode failures and other client-side errors. */
|
|
140
|
+
/** @brief Register the sink for inbound decode failures and other client-side errors. */
|
|
74
141
|
onError(handler) {
|
|
75
142
|
this.errorHandler = handler;
|
|
76
143
|
}
|
|
77
|
-
/** Decode one inbound frame: correlate a REPLY, deliver a write, or shed/deliver a VALUE. */
|
|
144
|
+
/** @brief Decode one inbound frame: correlate a REPLY, deliver a write, or shed/deliver a VALUE. */
|
|
78
145
|
dispatch(bytes) {
|
|
79
146
|
let tlv;
|
|
80
147
|
try {
|
|
@@ -95,8 +162,12 @@ export class LibtracerClient {
|
|
|
95
162
|
}
|
|
96
163
|
if (parsed.op === FWD_OP.REPLY) {
|
|
97
164
|
const waiter = this.pending.shift();
|
|
98
|
-
|
|
165
|
+
// A settled waiter (timed out) still consumes its FIFO slot; its late
|
|
166
|
+
// reply is dropped so later requests stay correlated.
|
|
167
|
+
if (waiter && !waiter.settled) {
|
|
168
|
+
settle(waiter);
|
|
99
169
|
waiter.resolve(parsed);
|
|
170
|
+
}
|
|
100
171
|
return;
|
|
101
172
|
}
|
|
102
173
|
// A delivery IS a FWD{WRITE} (RFC-0004 §D): deliver its VALUE payload.
|
|
@@ -105,6 +176,12 @@ export class LibtracerClient {
|
|
|
105
176
|
}
|
|
106
177
|
return;
|
|
107
178
|
}
|
|
179
|
+
// The compact (route-handle) flow is not implemented here: surface it loudly
|
|
180
|
+
// rather than silently dropping the delivery (v0.1 client limitation).
|
|
181
|
+
if (tlv.type === TYPE_ADVERTISE || tlv.type === TYPE_COMPACT) {
|
|
182
|
+
this.emitError(new CompactFlowError(tlv.type));
|
|
183
|
+
return;
|
|
184
|
+
}
|
|
108
185
|
// Non-FWD frame: shed exactly one ROUTER envelope, then deliver a VALUE
|
|
109
186
|
// (the directly-delivered / bridge-mounted path, ADR-0034 inbound rule).
|
|
110
187
|
const data = tlv.type === TYPE.ROUTER && tlv.children.length > 0 ? tlv.children[tlv.children.length - 1] : tlv;
|
|
@@ -119,28 +196,47 @@ export class LibtracerClient {
|
|
|
119
196
|
if (this.errorHandler)
|
|
120
197
|
this.errorHandler(err);
|
|
121
198
|
}
|
|
122
|
-
/** Send a one-shot FWD and resolve its FWD{REPLY} (FIFO correlation). */
|
|
199
|
+
/** @brief Send a one-shot FWD and resolve its FWD{REPLY} (FIFO correlation). */
|
|
123
200
|
request(frame) {
|
|
124
201
|
return new Promise((resolve, reject) => {
|
|
125
|
-
this.
|
|
202
|
+
if (this.closed) {
|
|
203
|
+
reject(this.closed);
|
|
204
|
+
return;
|
|
205
|
+
}
|
|
206
|
+
const entry = { resolve, reject, settled: false, timer: null };
|
|
207
|
+
if (this.requestTimeoutMs > 0 && Number.isFinite(this.requestTimeoutMs)) {
|
|
208
|
+
entry.timer = setTimeout(() => {
|
|
209
|
+
// Leave the settled entry in the FIFO (see Pending) — its slot is
|
|
210
|
+
// consumed by the late reply, if one ever arrives.
|
|
211
|
+
settle(entry);
|
|
212
|
+
entry.reject(new Error(`request timed out after ${this.requestTimeoutMs}ms (no FWD reply)`));
|
|
213
|
+
}, this.requestTimeoutMs);
|
|
214
|
+
// Don't hold a Node event loop open for a pending deadline (no-op in browsers).
|
|
215
|
+
entry.timer.unref?.();
|
|
216
|
+
}
|
|
217
|
+
this.pending.push(entry);
|
|
126
218
|
try {
|
|
127
219
|
this.transport.send(frame);
|
|
128
220
|
}
|
|
129
221
|
catch (err) {
|
|
130
222
|
this.pending.pop();
|
|
223
|
+
settle(entry);
|
|
131
224
|
reject(err instanceof Error ? err : new Error(String(err)));
|
|
132
225
|
}
|
|
133
226
|
});
|
|
134
227
|
}
|
|
135
|
-
/** Turn a RESULT reply into its payload TLV; throw {@link FwdError} on ERROR. */
|
|
228
|
+
/** @brief Turn a RESULT reply into its payload TLV; throw {@link FwdError} on ERROR. */
|
|
136
229
|
result(reply) {
|
|
137
|
-
if (reply.kind === FWD_KIND.ERROR)
|
|
138
|
-
|
|
230
|
+
if (reply.kind === FWD_KIND.ERROR) {
|
|
231
|
+
// Registered-code and string-form (NAME tr::… path) identities both
|
|
232
|
+
// surface typed — a known path resolves back to its code, never UNKNOWN.
|
|
233
|
+
throw new FwdError(replyErrorCode(reply), replyErrorPath(reply));
|
|
234
|
+
}
|
|
139
235
|
return reply.payload;
|
|
140
236
|
}
|
|
141
237
|
/* --------------------------------------------------------- one-shot --- */
|
|
142
238
|
/**
|
|
143
|
-
* Read a remote vertex's value: `FWD{ op=READ, dst=path, src=<reply-ep> }`.
|
|
239
|
+
* @brief Read a remote vertex's value: `FWD{ op=READ, dst=path, src=<reply-ep> }`.
|
|
144
240
|
*
|
|
145
241
|
* @param path the destination path (a `/`-string like `"/sensor/temp"`, or segments)
|
|
146
242
|
* @returns the RESULT's value TLV (decoded)
|
|
@@ -154,7 +250,9 @@ export class LibtracerClient {
|
|
|
154
250
|
return value;
|
|
155
251
|
}
|
|
156
252
|
/**
|
|
157
|
-
* Read a remote vertex's `:field`: `FWD{ op=READ, dst=path,
|
|
253
|
+
* @brief Read a remote vertex's `:field`: `FWD{ op=READ, dst=path,
|
|
254
|
+
* FIELD=selector, src=… }`.
|
|
255
|
+
*
|
|
158
256
|
* A whole-array field (e.g. `:subscribers[]`) resolves to a POINT of the slot TLVs.
|
|
159
257
|
*
|
|
160
258
|
* @param path the destination path (string or segments)
|
|
@@ -170,7 +268,7 @@ export class LibtracerClient {
|
|
|
170
268
|
return value;
|
|
171
269
|
}
|
|
172
270
|
/**
|
|
173
|
-
* Write a value to a remote vertex: `FWD{ op=WRITE, dst=path, src=…, payload }`.
|
|
271
|
+
* @brief Write a value to a remote vertex: `FWD{ op=WRITE, dst=path, src=…, payload }`.
|
|
174
272
|
*
|
|
175
273
|
* @param path the destination path (string or segments)
|
|
176
274
|
* @param valueTLV a complete VALUE TLV's bytes (e.g. from {@link encodeValue})
|
|
@@ -182,9 +280,11 @@ export class LibtracerClient {
|
|
|
182
280
|
this.result(reply); // throws on ERROR; RESULT carries no payload
|
|
183
281
|
}
|
|
184
282
|
/**
|
|
185
|
-
* Block for the next write to a remote vertex: `FWD{ op=AWAIT,
|
|
186
|
-
* await_timeout? }`.
|
|
187
|
-
*
|
|
283
|
+
* @brief Block for the next write to a remote vertex: `FWD{ op=AWAIT,
|
|
284
|
+
* dst=path, src=…, await_timeout? }`.
|
|
285
|
+
*
|
|
286
|
+
* Named `await_` because `await` is reserved; an `await` alias is installed
|
|
287
|
+
* on the prototype for the RFC-0004-spelled call site.
|
|
188
288
|
*
|
|
189
289
|
* @param path the destination path (string or segments)
|
|
190
290
|
* @param timeoutNs the await timeout in ns (absent ⇒ the responder's 1 s default)
|
|
@@ -204,8 +304,10 @@ export class LibtracerClient {
|
|
|
204
304
|
return value;
|
|
205
305
|
}
|
|
206
306
|
/**
|
|
207
|
-
* Subscribe to a remote producer: a `WRITE` of a `SUBSCRIBER{
|
|
208
|
-
* into `producer:subscribers[]` (RFC-0004 §C/§D —
|
|
307
|
+
* @brief Subscribe to a remote producer: a `WRITE` of a `SUBSCRIBER{
|
|
308
|
+
* target=<reply-ep> }` into `producer:subscribers[]` (RFC-0004 §C/§D —
|
|
309
|
+
* subscribe is a field-write).
|
|
310
|
+
*
|
|
209
311
|
* Once the responder acks, `handler` fires for every inbound VALUE delivery.
|
|
210
312
|
*
|
|
211
313
|
* SCOPE: this issues the SUBSCRIBER and receives VALUEs the producer directly
|
|
@@ -245,28 +347,31 @@ export class LibtracerClient {
|
|
|
245
347
|
};
|
|
246
348
|
}
|
|
247
349
|
/* ------------------------------------------- pure builders (static) --- */
|
|
248
|
-
/** {@link encodeValue} — the exact VALUE TLV bytes, independently of any transport. */
|
|
350
|
+
/** @brief {@link encodeValue} — the exact VALUE TLV bytes, independently of any transport. */
|
|
249
351
|
static encodeValue(value, opts) {
|
|
250
352
|
return encodeValue(value, opts);
|
|
251
353
|
}
|
|
252
|
-
/** {@link encodePath} — the exact PATH TLV bytes. */
|
|
354
|
+
/** @brief {@link encodePath} — the exact PATH TLV bytes. */
|
|
253
355
|
static encodePath(segments) {
|
|
254
356
|
return encodePath(segments);
|
|
255
357
|
}
|
|
256
|
-
/** {@link encodeSubscriber} — the exact SUBSCRIBER TLV bytes. */
|
|
358
|
+
/** @brief {@link encodeSubscriber} — the exact SUBSCRIBER TLV bytes. */
|
|
257
359
|
static encodeSubscriber(targetPath, opts) {
|
|
258
360
|
return encodeSubscriber(targetPath, opts);
|
|
259
361
|
}
|
|
260
362
|
}
|
|
261
|
-
|
|
262
|
-
|
|
363
|
+
/**
|
|
364
|
+
* @brief `await` is a reserved word, so the method is `await_`; expose
|
|
365
|
+
* `client.await(...)` too (a member name may be a keyword) for the
|
|
366
|
+
* RFC-0004-spelled ergonomic call.
|
|
367
|
+
*/
|
|
263
368
|
Object.defineProperty(LibtracerClient.prototype, 'await', {
|
|
264
369
|
value: LibtracerClient.prototype.await_,
|
|
265
370
|
writable: true,
|
|
266
371
|
configurable: true,
|
|
267
372
|
enumerable: false,
|
|
268
373
|
});
|
|
269
|
-
/** Normalize a `/`-path string (or segment array) into validated segments. */
|
|
374
|
+
/** @brief Normalize a `/`-path string (or segment array) into validated segments. */
|
|
270
375
|
function splitPath(path) {
|
|
271
376
|
if (Array.isArray(path))
|
|
272
377
|
return path;
|