@avatarsd-llc/libtracer 0.8.0 → 0.9.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/codec.d.mts +16 -1
- package/dist/codec.mjs +58 -10
- package/package.json +1 -1
package/dist/codec.d.mts
CHANGED
|
@@ -28,8 +28,23 @@ export function decode(input: Uint8Array): Tlv;
|
|
|
28
28
|
* @brief Encode a TLV tree back to wire bytes, recomputing the trailer CRC from
|
|
29
29
|
* the body (+ timestamp bytes) when opt.CR is set. Mirrors `encode` in frame.cpp.
|
|
30
30
|
*
|
|
31
|
+
* Encoding is SYMMETRIC with {@link decode}: the grammar's one per-type structural rule — a
|
|
32
|
+
* PATH_REF body is a fixed-stride 8-byte record array, so opt.PL and opt.LL are both
|
|
33
|
+
* forbidden and the length is a bounded multiple of 8 (RFC-0024 §4.2/§4.3) — is applied here
|
|
34
|
+
* through the same predicate the decoder calls, so this core cannot mint a frame it would
|
|
35
|
+
* itself answer with FRAME_INVALID (#1004; the C++ core closed the same asymmetry in #886).
|
|
36
|
+
* This package exports no PATH_REF builder, so a caller composes the object literal directly
|
|
37
|
+
* and this is the only door.
|
|
38
|
+
*
|
|
39
|
+
* **Postcondition.** This function has no error channel, so refusal is spelled *emits
|
|
40
|
+
* nothing*: an ill-formed PATH_REF anywhere in the tree makes the whole call return an EMPTY
|
|
41
|
+
* `Uint8Array`. A refused TLV refuses its ancestors rather than being dropped into a frame
|
|
42
|
+
* that DOES decode, one component short. Empty is unambiguous — an accepted TLV always
|
|
43
|
+
* carries at least its 4-byte header, so no well-formed tree encodes to nothing. Well-formed
|
|
44
|
+
* input is untouched and stays byte-identical to what it produced before.
|
|
45
|
+
*
|
|
31
46
|
* @param {Tlv} tlv
|
|
32
|
-
* @returns {Uint8Array}
|
|
47
|
+
* @returns {Uint8Array} the frame bytes, or an EMPTY array when the tree is refused
|
|
33
48
|
*/
|
|
34
49
|
export function encode(tlv: Tlv): Uint8Array;
|
|
35
50
|
/**
|
package/dist/codec.mjs
CHANGED
|
@@ -69,6 +69,34 @@ export const ERROR = Object.freeze({
|
|
|
69
69
|
*/
|
|
70
70
|
TLV_NESTING_TOO_DEEP: 'TLV_NESTING_TOO_DEEP',
|
|
71
71
|
});
|
|
72
|
+
/**
|
|
73
|
+
* @brief The purely STRUCTURAL PATH_REF rules of RFC-0024 §4.2/§4.3 — the grammar's only
|
|
74
|
+
* per-type check, and the ONE home of it in this module: both {@link decode} and
|
|
75
|
+
* {@link encode} call this rather than spelling the four clauses twice.
|
|
76
|
+
*
|
|
77
|
+
* A PATH_REF header that fails this is FRAME_INVALID, exactly as a set reserved bit is. All
|
|
78
|
+
* four clauses are shape, not meaning — what an element MEANS is node-scoped, so no codec
|
|
79
|
+
* can validate one:
|
|
80
|
+
*
|
|
81
|
+
* - `opt.PL` MUST be 0 — the body is a fixed-stride record array, not child TLVs; a generic
|
|
82
|
+
* PL=1 walker would read the first four body bytes as a TLV header and mis-frame the body.
|
|
83
|
+
* - `opt.LL` MUST be 0 — the u32 length width buys nothing under a 2040-byte cap, so it is
|
|
84
|
+
* forbidden rather than merely unused.
|
|
85
|
+
* - `length % 8 == 0` — a body that is not a whole number of elements has no reading, because
|
|
86
|
+
* the element count is not on the wire at all: it IS `length / 8`.
|
|
87
|
+
* - `length <= 2040` — the §4.3 element-count bound, in bytes.
|
|
88
|
+
*
|
|
89
|
+
* @param {boolean} pl opt.PL
|
|
90
|
+
* @param {boolean} ll opt.LL
|
|
91
|
+
* @param {number} bodyLen the body length in bytes
|
|
92
|
+
* @returns {boolean} true if the header is structurally a well-formed PATH_REF
|
|
93
|
+
*/
|
|
94
|
+
function pathRefBodyValid(pl, ll, bodyLen) {
|
|
95
|
+
return (!pl &&
|
|
96
|
+
!ll &&
|
|
97
|
+
bodyLen % PATH_REF_ELEMENT_BYTES === 0 &&
|
|
98
|
+
bodyLen <= MAX_PATH_REF_ELEMENTS * PATH_REF_ELEMENT_BYTES);
|
|
99
|
+
}
|
|
72
100
|
/** @brief Reserved bits 7 and 0; non-zero => FRAME_INVALID. */
|
|
73
101
|
const RESERVED_MASK = 129;
|
|
74
102
|
/**
|
|
@@ -295,15 +323,10 @@ function parseOne(buf) {
|
|
|
295
323
|
if (buf.length < header)
|
|
296
324
|
throw new CodecError(ERROR.FRAME_TRUNCATED);
|
|
297
325
|
const length = readLe(buf, 2, opt.ll ? 4 : 2);
|
|
298
|
-
// The one per-type structural rule (RFC-0024 §4.2/§4.3)
|
|
299
|
-
//
|
|
300
|
-
//
|
|
301
|
-
|
|
302
|
-
if (typeB === TYPE.PATH_REF &&
|
|
303
|
-
(opt.pl ||
|
|
304
|
-
opt.ll ||
|
|
305
|
-
length % PATH_REF_ELEMENT_BYTES !== 0 ||
|
|
306
|
-
length > MAX_PATH_REF_ELEMENTS * PATH_REF_ELEMENT_BYTES)) {
|
|
326
|
+
// The one per-type structural rule (RFC-0024 §4.2/§4.3), through the single predicate
|
|
327
|
+
// encode() now shares — see pathRefBodyValid for the four clauses and why they are shape
|
|
328
|
+
// rather than meaning.
|
|
329
|
+
if (typeB === TYPE.PATH_REF && !pathRefBodyValid(opt.pl, opt.ll, length)) {
|
|
307
330
|
throw new CodecError(ERROR.FRAME_INVALID);
|
|
308
331
|
}
|
|
309
332
|
const tsSize = opt.ts ? (opt.tf ? 4 : 8) : 0;
|
|
@@ -406,15 +429,40 @@ export function decode(input) {
|
|
|
406
429
|
* @brief Encode a TLV tree back to wire bytes, recomputing the trailer CRC from
|
|
407
430
|
* the body (+ timestamp bytes) when opt.CR is set. Mirrors `encode` in frame.cpp.
|
|
408
431
|
*
|
|
432
|
+
* Encoding is SYMMETRIC with {@link decode}: the grammar's one per-type structural rule — a
|
|
433
|
+
* PATH_REF body is a fixed-stride 8-byte record array, so opt.PL and opt.LL are both
|
|
434
|
+
* forbidden and the length is a bounded multiple of 8 (RFC-0024 §4.2/§4.3) — is applied here
|
|
435
|
+
* through the same predicate the decoder calls, so this core cannot mint a frame it would
|
|
436
|
+
* itself answer with FRAME_INVALID (#1004; the C++ core closed the same asymmetry in #886).
|
|
437
|
+
* This package exports no PATH_REF builder, so a caller composes the object literal directly
|
|
438
|
+
* and this is the only door.
|
|
439
|
+
*
|
|
440
|
+
* **Postcondition.** This function has no error channel, so refusal is spelled *emits
|
|
441
|
+
* nothing*: an ill-formed PATH_REF anywhere in the tree makes the whole call return an EMPTY
|
|
442
|
+
* `Uint8Array`. A refused TLV refuses its ancestors rather than being dropped into a frame
|
|
443
|
+
* that DOES decode, one component short. Empty is unambiguous — an accepted TLV always
|
|
444
|
+
* carries at least its 4-byte header, so no well-formed tree encodes to nothing. Well-formed
|
|
445
|
+
* input is untouched and stays byte-identical to what it produced before.
|
|
446
|
+
*
|
|
409
447
|
* @param {Tlv} tlv
|
|
410
|
-
* @returns {Uint8Array}
|
|
448
|
+
* @returns {Uint8Array} the frame bytes, or an EMPTY array when the tree is refused
|
|
411
449
|
*/
|
|
412
450
|
export function encode(tlv) {
|
|
451
|
+
// The emitted type byte is `type & 0xff` (below), so the guard reads the byte the wire will
|
|
452
|
+
// actually carry. A PATH_REF body is never structured, so `payload` IS the body length: an
|
|
453
|
+
// opt.PL PATH_REF fails the PL clause before the children branch below ever runs.
|
|
454
|
+
if ((tlv.type & 0xff) === TYPE.PATH_REF &&
|
|
455
|
+
!pathRefBodyValid(tlv.opt.pl, tlv.opt.ll, tlv.payload.length)) {
|
|
456
|
+
return new Uint8Array(0);
|
|
457
|
+
}
|
|
413
458
|
/** @type {number[]} */
|
|
414
459
|
let body = [];
|
|
415
460
|
if (tlv.opt.pl) {
|
|
416
461
|
for (const child of tlv.children) {
|
|
417
462
|
const cb = encode(child);
|
|
463
|
+
// A refused child refuses the parent (see the postcondition above).
|
|
464
|
+
if (cb.length === 0)
|
|
465
|
+
return new Uint8Array(0);
|
|
418
466
|
for (let i = 0; i < cb.length; i++)
|
|
419
467
|
body.push(cb[i]);
|
|
420
468
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@avatarsd-llc/libtracer",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.9.0",
|
|
4
4
|
"description": "TypeScript core for libtracer — a lightweight decentralized graph-based networking protocol. Native pure-TS implementation (in-process codec; no transports), conformance-gated against the shared vectors.",
|
|
5
5
|
"author": "avatarsd LLC",
|
|
6
6
|
"license": "Apache-2.0",
|