@avatarsd-llc/libtracer 0.0.1
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/LICENSE +201 -0
- package/README.md +65 -0
- package/dist/codec.d.mts +182 -0
- package/dist/codec.mjs +460 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.js +10 -0
- package/dist/wire.d.ts +2 -0
- package/dist/wire.js +9 -0
- package/package.json +49 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright [yyyy] [name of copyright owner]
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
200
|
+
See the License for the specific language governing permissions and
|
|
201
|
+
limitations under the License.
|
package/README.md
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
# @avatarsd-llc/libtracer
|
|
2
|
+
|
|
3
|
+
The TypeScript core for [libtracer](https://github.com/avatarsd-llc/libtracer) —
|
|
4
|
+
a native, pure-TypeScript implementation of the in-process protocol core (the
|
|
5
|
+
L2/L3 wire codec today). It carries **no transports**; those live in separate
|
|
6
|
+
per-transport packages (e.g. `@avatarsd-llc/libtracer-ws`). The codec is gated
|
|
7
|
+
byte-for-byte against the shared conformance vectors and cross-matched with the
|
|
8
|
+
C++ and Rust cores (ADR-0028).
|
|
9
|
+
|
|
10
|
+
## Install
|
|
11
|
+
|
|
12
|
+
```sh
|
|
13
|
+
npm install @avatarsd-llc/libtracer
|
|
14
|
+
```
|
|
15
|
+
|
|
16
|
+
ESM only; Node ≥ 18 (or any modern bundler/browser).
|
|
17
|
+
|
|
18
|
+
## Use
|
|
19
|
+
|
|
20
|
+
```ts
|
|
21
|
+
// Barrel entry — everything the core exports.
|
|
22
|
+
import { decode, encode, TYPE } from '@avatarsd-llc/libtracer';
|
|
23
|
+
|
|
24
|
+
// Or import just the wire layer for tree-shaking (subpath entry).
|
|
25
|
+
import { decode, encode } from '@avatarsd-llc/libtracer/wire';
|
|
26
|
+
|
|
27
|
+
const tlv = decode(bytes);
|
|
28
|
+
const reencoded = encode(tlv); // byte-for-byte round-trip
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
### Subpath / layer entries
|
|
32
|
+
|
|
33
|
+
The package ships a tree-shakeable `exports` map. Today only the wire layer is
|
|
34
|
+
implemented in TS, so the live entries are:
|
|
35
|
+
|
|
36
|
+
| Subpath | Layer | Status |
|
|
37
|
+
| -------- | ----------- | ----------- |
|
|
38
|
+
| `.` | barrel | implemented |
|
|
39
|
+
| `./wire` | L2/L3 codec | implemented |
|
|
40
|
+
|
|
41
|
+
`./mem` (L0), `./view` (L1) and `./graph` (L4) are **reserved** for when those
|
|
42
|
+
layers are ported to TS — see
|
|
43
|
+
[ADR-0033](../../../../docs/adr/0033-npm-subpackage-monorepo.md). The subpath
|
|
44
|
+
mechanism (not one-package-per-layer) is the chosen way to slice modules.
|
|
45
|
+
|
|
46
|
+
## Development
|
|
47
|
+
|
|
48
|
+
This package is one member of the `bindings/typescript` npm workspace.
|
|
49
|
+
|
|
50
|
+
```sh
|
|
51
|
+
# from bindings/typescript (the workspace root)
|
|
52
|
+
npm install
|
|
53
|
+
npm run build # tsc -> dist/ (all workspaces)
|
|
54
|
+
npm test # conformance harness over the shared vectors
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
The conformance harness (`conformance/harness.mjs`) and perf bench
|
|
58
|
+
(`bench/perf.mjs`) import the raw `src/codec.mjs` directly and need **no build
|
|
59
|
+
step** — that keeps the polyglot conformance driver
|
|
60
|
+
(`tests/conformance/run-all.py`) dependency-free.
|
|
61
|
+
|
|
62
|
+
## Releasing
|
|
63
|
+
|
|
64
|
+
Tag the repo with `ts-vX.Y.Z` to trigger publish (see
|
|
65
|
+
`.github/workflows/publish-npm.yml` once added).
|
package/dist/codec.d.mts
ADDED
|
@@ -0,0 +1,182 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* CRC-32C (Castagnoli): reflected poly 0x82F63B78, init/xor 0xFFFFFFFF.
|
|
3
|
+
*
|
|
4
|
+
* @param {Uint8Array} data
|
|
5
|
+
* @returns {number} unsigned 32-bit
|
|
6
|
+
*/
|
|
7
|
+
export function crc32c(data: Uint8Array): number;
|
|
8
|
+
/**
|
|
9
|
+
* CRC-16-CCITT (FALSE): poly 0x1021, MSB-first, init 0xFFFF, no final xor.
|
|
10
|
+
*
|
|
11
|
+
* @param {Uint8Array} data
|
|
12
|
+
* @returns {number} unsigned 16-bit
|
|
13
|
+
*/
|
|
14
|
+
export function crc16ccitt(data: Uint8Array): number;
|
|
15
|
+
/**
|
|
16
|
+
* Decode exactly one TLV that fills `input` into a TLV tree. Nesting is parsed
|
|
17
|
+
* ITERATIVELY with an explicit stack (recursion is forbidden) and capped at
|
|
18
|
+
* {@link MAX_DEPTH}. Trailing bytes after the root => FRAME_INVALID.
|
|
19
|
+
*
|
|
20
|
+
* @param {Uint8Array} input
|
|
21
|
+
* @returns {Tlv}
|
|
22
|
+
*/
|
|
23
|
+
export function decode(input: Uint8Array): Tlv;
|
|
24
|
+
/**
|
|
25
|
+
* Encode a TLV tree back to wire bytes, recomputing the trailer CRC from the
|
|
26
|
+
* body (+ timestamp bytes) when opt.CR is set. Mirrors `encode` in frame.cpp.
|
|
27
|
+
*
|
|
28
|
+
* @param {Tlv} tlv
|
|
29
|
+
* @returns {Uint8Array}
|
|
30
|
+
*/
|
|
31
|
+
export function encode(tlv: Tlv): Uint8Array;
|
|
32
|
+
/**
|
|
33
|
+
* Structural + byte-content equality of two TLV trees.
|
|
34
|
+
*
|
|
35
|
+
* @param {Tlv} a
|
|
36
|
+
* @param {Tlv} b
|
|
37
|
+
* @returns {boolean}
|
|
38
|
+
*/
|
|
39
|
+
export function equal(a: Tlv, b: Tlv): boolean;
|
|
40
|
+
/**
|
|
41
|
+
* Core type-code registry (0x01-0x10). 0x05 is retired (was LIST, ADR-0003).
|
|
42
|
+
* 0x0E SPEC is the in-band vertex-creation spec. 0x0F FWD and 0x10 FIELD are the
|
|
43
|
+
* remote-operation frames (RFC-0004 / ADR-0035, the v1 fast-track range
|
|
44
|
+
* 0x0F-0x1F). All are structured (opt.PL=1) and handled generically by the codec.
|
|
45
|
+
*/
|
|
46
|
+
export type TYPE = number;
|
|
47
|
+
/**
|
|
48
|
+
* libtracer core wire codec — a pure-TypeScript/JavaScript port of the C++
|
|
49
|
+
* reference codec in `core/src/frame.cpp`. It must match that implementation
|
|
50
|
+
* byte-for-byte; correctness is gated by the SHARED conformance vectors under
|
|
51
|
+
* `tests/conformance/vectors/v1/`.
|
|
52
|
+
*
|
|
53
|
+
* The wire format is documented in `docs/reference/01-data-format.md`:
|
|
54
|
+
* header (4 or 6 bytes) + payload + optional trailer (timestamp then CRC).
|
|
55
|
+
*
|
|
56
|
+
* Authored as ESM JavaScript (not `.ts`) so it runs directly under Node with no
|
|
57
|
+
* build step — that keeps the polyglot conformance driver (`node harness.mjs`)
|
|
58
|
+
* dependency-free. TypeScript declarations are generated from the JSDoc by
|
|
59
|
+
* `tsc` for the published `@avatarsd-llc/libtracer` package.
|
|
60
|
+
*/
|
|
61
|
+
/**
|
|
62
|
+
* Core type-code registry (0x01-0x10). 0x05 is retired (was LIST, ADR-0003).
|
|
63
|
+
* 0x0E SPEC is the in-band vertex-creation spec. 0x0F FWD and 0x10 FIELD are the
|
|
64
|
+
* remote-operation frames (RFC-0004 / ADR-0035, the v1 fast-track range
|
|
65
|
+
* 0x0F-0x1F). All are structured (opt.PL=1) and handled generically by the codec.
|
|
66
|
+
*
|
|
67
|
+
* @readonly
|
|
68
|
+
* @enum {number}
|
|
69
|
+
*/
|
|
70
|
+
export const TYPE: Readonly<{
|
|
71
|
+
VALUE: 1;
|
|
72
|
+
NAME: 2;
|
|
73
|
+
DESCRIPTION: 3;
|
|
74
|
+
SUBSCRIBER: 4;
|
|
75
|
+
PATH: 6;
|
|
76
|
+
POINT: 7;
|
|
77
|
+
ERROR: 8;
|
|
78
|
+
STATUS: 9;
|
|
79
|
+
ACL: 10;
|
|
80
|
+
SETTINGS: 11;
|
|
81
|
+
TIME: 12;
|
|
82
|
+
ROUTER: 13;
|
|
83
|
+
SPEC: 14;
|
|
84
|
+
FWD: 15;
|
|
85
|
+
FIELD: 16;
|
|
86
|
+
}>;
|
|
87
|
+
/**
|
|
88
|
+
* Decode failure reasons, named after the C++ `tr::wire::error_t` enum.
|
|
89
|
+
*/
|
|
90
|
+
export type ERROR = string;
|
|
91
|
+
/**
|
|
92
|
+
* Decode failure reasons, named after the C++ `tr::wire::error_t` enum.
|
|
93
|
+
*
|
|
94
|
+
* @readonly
|
|
95
|
+
* @enum {string}
|
|
96
|
+
*/
|
|
97
|
+
export const ERROR: Readonly<{
|
|
98
|
+
FRAME_TRUNCATED: "FRAME_TRUNCATED";
|
|
99
|
+
FRAME_INVALID: "FRAME_INVALID";
|
|
100
|
+
FRAME_CRC_FAIL: "FRAME_CRC_FAIL";
|
|
101
|
+
TLV_NESTING_TOO_DEEP: "TLV_NESTING_TOO_DEEP";
|
|
102
|
+
}>;
|
|
103
|
+
/** The iterative-parser depth cap (docs/reference/01 §iterative parsing). */
|
|
104
|
+
export const MAX_DEPTH: 32;
|
|
105
|
+
/**
|
|
106
|
+
* A decode/encode error carrying one of {@link ERROR} as `.code`.
|
|
107
|
+
*/
|
|
108
|
+
export class CodecError extends Error {
|
|
109
|
+
/** @param {string} code one of {@link ERROR} */
|
|
110
|
+
constructor(code: string);
|
|
111
|
+
/** @type {string} */
|
|
112
|
+
code: string;
|
|
113
|
+
}
|
|
114
|
+
/**
|
|
115
|
+
* The 1-byte `opt` field, unpacked. Bits MSB->LSB: R | PL | TS | CR | LL | CW | TF | R.
|
|
116
|
+
*/
|
|
117
|
+
export type Opt = {
|
|
118
|
+
/**
|
|
119
|
+
* bit 6: payload is structured (children, not opaque)
|
|
120
|
+
*/
|
|
121
|
+
pl: boolean;
|
|
122
|
+
/**
|
|
123
|
+
* bit 5: trailer carries a timestamp
|
|
124
|
+
*/
|
|
125
|
+
ts: boolean;
|
|
126
|
+
/**
|
|
127
|
+
* bit 4: trailer carries a CRC
|
|
128
|
+
*/
|
|
129
|
+
cr: boolean;
|
|
130
|
+
/**
|
|
131
|
+
* bit 3: length width (false = u16, true = u32)
|
|
132
|
+
*/
|
|
133
|
+
ll: boolean;
|
|
134
|
+
/**
|
|
135
|
+
* bit 2: CRC width (false = CRC-32C, true = CRC-16-CCITT)
|
|
136
|
+
*/
|
|
137
|
+
cw: boolean;
|
|
138
|
+
/**
|
|
139
|
+
* bit 1: timestamp form (false = abs u64, true = rel i32)
|
|
140
|
+
*/
|
|
141
|
+
tf: boolean;
|
|
142
|
+
};
|
|
143
|
+
/**
|
|
144
|
+
* A decoded wire timestamp. Absolute form is a u64 (BigInt) of ns since the Unix
|
|
145
|
+
* epoch; relative form is a signed i32 ns offset from the parent timestamp.
|
|
146
|
+
*/
|
|
147
|
+
export type Timestamp = {
|
|
148
|
+
/**
|
|
149
|
+
* false = absolute u64 ns; true = relative i32 ns
|
|
150
|
+
*/
|
|
151
|
+
relative: boolean;
|
|
152
|
+
/**
|
|
153
|
+
* ns; BigInt to preserve the full u64 range exactly
|
|
154
|
+
*/
|
|
155
|
+
value: bigint;
|
|
156
|
+
};
|
|
157
|
+
/**
|
|
158
|
+
* A decoded CRC. 16-bit values are zero-extended into `value`.
|
|
159
|
+
*/
|
|
160
|
+
export type Crc = {
|
|
161
|
+
width: "CRC32C" | "CRC16_CCITT";
|
|
162
|
+
value: number;
|
|
163
|
+
};
|
|
164
|
+
export type Trailer = {
|
|
165
|
+
ts: Timestamp | null;
|
|
166
|
+
crc: Crc | null;
|
|
167
|
+
};
|
|
168
|
+
/**
|
|
169
|
+
* A decoded TLV. For opaque TLVs (opt.PL=0) `payload` holds the bytes and
|
|
170
|
+
* `children` is empty; for structured TLVs (opt.PL=1) `children` holds the
|
|
171
|
+
* parsed sub-TLVs and `payload` is empty.
|
|
172
|
+
*/
|
|
173
|
+
export type Tlv = {
|
|
174
|
+
/**
|
|
175
|
+
* a {@link TYPE} code (or a user/unknown code)
|
|
176
|
+
*/
|
|
177
|
+
type: number;
|
|
178
|
+
opt: Opt;
|
|
179
|
+
payload: Uint8Array;
|
|
180
|
+
children: Tlv[];
|
|
181
|
+
trailer: Trailer | null;
|
|
182
|
+
};
|
package/dist/codec.mjs
ADDED
|
@@ -0,0 +1,460 @@
|
|
|
1
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
2
|
+
// SPDX-FileCopyrightText: Copyright 2026 avatarsd LLC
|
|
3
|
+
/**
|
|
4
|
+
* libtracer core wire codec — a pure-TypeScript/JavaScript port of the C++
|
|
5
|
+
* reference codec in `core/src/frame.cpp`. It must match that implementation
|
|
6
|
+
* byte-for-byte; correctness is gated by the SHARED conformance vectors under
|
|
7
|
+
* `tests/conformance/vectors/v1/`.
|
|
8
|
+
*
|
|
9
|
+
* The wire format is documented in `docs/reference/01-data-format.md`:
|
|
10
|
+
* header (4 or 6 bytes) + payload + optional trailer (timestamp then CRC).
|
|
11
|
+
*
|
|
12
|
+
* Authored as ESM JavaScript (not `.ts`) so it runs directly under Node with no
|
|
13
|
+
* build step — that keeps the polyglot conformance driver (`node harness.mjs`)
|
|
14
|
+
* dependency-free. TypeScript declarations are generated from the JSDoc by
|
|
15
|
+
* `tsc` for the published `@avatarsd-llc/libtracer` package.
|
|
16
|
+
*/
|
|
17
|
+
/**
|
|
18
|
+
* Core type-code registry (0x01-0x10). 0x05 is retired (was LIST, ADR-0003).
|
|
19
|
+
* 0x0E SPEC is the in-band vertex-creation spec. 0x0F FWD and 0x10 FIELD are the
|
|
20
|
+
* remote-operation frames (RFC-0004 / ADR-0035, the v1 fast-track range
|
|
21
|
+
* 0x0F-0x1F). All are structured (opt.PL=1) and handled generically by the codec.
|
|
22
|
+
*
|
|
23
|
+
* @readonly
|
|
24
|
+
* @enum {number}
|
|
25
|
+
*/
|
|
26
|
+
export const TYPE = Object.freeze({
|
|
27
|
+
VALUE: 0x01,
|
|
28
|
+
NAME: 0x02,
|
|
29
|
+
DESCRIPTION: 0x03,
|
|
30
|
+
SUBSCRIBER: 0x04,
|
|
31
|
+
PATH: 0x06,
|
|
32
|
+
POINT: 0x07,
|
|
33
|
+
ERROR: 0x08,
|
|
34
|
+
STATUS: 0x09,
|
|
35
|
+
ACL: 0x0a,
|
|
36
|
+
SETTINGS: 0x0b,
|
|
37
|
+
TIME: 0x0c,
|
|
38
|
+
ROUTER: 0x0d,
|
|
39
|
+
SPEC: 0x0e,
|
|
40
|
+
FWD: 0x0f,
|
|
41
|
+
FIELD: 0x10,
|
|
42
|
+
});
|
|
43
|
+
/**
|
|
44
|
+
* Decode failure reasons, named after the C++ `tr::wire::error_t` enum.
|
|
45
|
+
*
|
|
46
|
+
* @readonly
|
|
47
|
+
* @enum {string}
|
|
48
|
+
*/
|
|
49
|
+
export const ERROR = Object.freeze({
|
|
50
|
+
FRAME_TRUNCATED: 'FRAME_TRUNCATED', // ran out of bytes
|
|
51
|
+
FRAME_INVALID: 'FRAME_INVALID', // reserved bit, type 0x00, trailing bytes
|
|
52
|
+
FRAME_CRC_FAIL: 'FRAME_CRC_FAIL', // trailer CRC mismatch
|
|
53
|
+
TLV_NESTING_TOO_DEEP: 'TLV_NESTING_TOO_DEEP', // depth cap exceeded
|
|
54
|
+
});
|
|
55
|
+
/** The iterative-parser depth cap (docs/reference/01 §iterative parsing). */
|
|
56
|
+
export const MAX_DEPTH = 32;
|
|
57
|
+
/** Reserved bits 7 and 0; non-zero => FRAME_INVALID. */
|
|
58
|
+
const RESERVED_MASK = 129;
|
|
59
|
+
/**
|
|
60
|
+
* A decode/encode error carrying one of {@link ERROR} as `.code`.
|
|
61
|
+
*/
|
|
62
|
+
export class CodecError extends Error {
|
|
63
|
+
/** @param {string} code one of {@link ERROR} */
|
|
64
|
+
constructor(code) {
|
|
65
|
+
super(code);
|
|
66
|
+
this.name = 'CodecError';
|
|
67
|
+
/** @type {string} */
|
|
68
|
+
this.code = code;
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* The 1-byte `opt` field, unpacked. Bits MSB->LSB: R | PL | TS | CR | LL | CW | TF | R.
|
|
73
|
+
*
|
|
74
|
+
* @typedef {object} Opt
|
|
75
|
+
* @property {boolean} pl bit 6: payload is structured (children, not opaque)
|
|
76
|
+
* @property {boolean} ts bit 5: trailer carries a timestamp
|
|
77
|
+
* @property {boolean} cr bit 4: trailer carries a CRC
|
|
78
|
+
* @property {boolean} ll bit 3: length width (false = u16, true = u32)
|
|
79
|
+
* @property {boolean} cw bit 2: CRC width (false = CRC-32C, true = CRC-16-CCITT)
|
|
80
|
+
* @property {boolean} tf bit 1: timestamp form (false = abs u64, true = rel i32)
|
|
81
|
+
*/
|
|
82
|
+
/**
|
|
83
|
+
* A decoded wire timestamp. Absolute form is a u64 (BigInt) of ns since the Unix
|
|
84
|
+
* epoch; relative form is a signed i32 ns offset from the parent timestamp.
|
|
85
|
+
*
|
|
86
|
+
* @typedef {object} Timestamp
|
|
87
|
+
* @property {boolean} relative false = absolute u64 ns; true = relative i32 ns
|
|
88
|
+
* @property {bigint} value ns; BigInt to preserve the full u64 range exactly
|
|
89
|
+
*/
|
|
90
|
+
/**
|
|
91
|
+
* A decoded CRC. 16-bit values are zero-extended into `value`.
|
|
92
|
+
*
|
|
93
|
+
* @typedef {object} Crc
|
|
94
|
+
* @property {'CRC32C'|'CRC16_CCITT'} width
|
|
95
|
+
* @property {number} value
|
|
96
|
+
*/
|
|
97
|
+
/**
|
|
98
|
+
* @typedef {object} Trailer
|
|
99
|
+
* @property {Timestamp|null} ts
|
|
100
|
+
* @property {Crc|null} crc
|
|
101
|
+
*/
|
|
102
|
+
/**
|
|
103
|
+
* A decoded TLV. For opaque TLVs (opt.PL=0) `payload` holds the bytes and
|
|
104
|
+
* `children` is empty; for structured TLVs (opt.PL=1) `children` holds the
|
|
105
|
+
* parsed sub-TLVs and `payload` is empty.
|
|
106
|
+
*
|
|
107
|
+
* @typedef {object} Tlv
|
|
108
|
+
* @property {number} type a {@link TYPE} code (or a user/unknown code)
|
|
109
|
+
* @property {Opt} opt
|
|
110
|
+
* @property {Uint8Array} payload
|
|
111
|
+
* @property {Tlv[]} children
|
|
112
|
+
* @property {Trailer|null} trailer
|
|
113
|
+
*/
|
|
114
|
+
/* ------------------------------------------------------------------ opt --- */
|
|
115
|
+
/**
|
|
116
|
+
* @param {number} b raw opt byte
|
|
117
|
+
* @returns {boolean} true if a reserved bit (7 or 0) is set
|
|
118
|
+
*/
|
|
119
|
+
function reservedSet(b) {
|
|
120
|
+
return (b & RESERVED_MASK) !== 0;
|
|
121
|
+
}
|
|
122
|
+
/**
|
|
123
|
+
* @param {number} b raw opt byte
|
|
124
|
+
* @returns {Opt}
|
|
125
|
+
*/
|
|
126
|
+
function decodeOpt(b) {
|
|
127
|
+
return {
|
|
128
|
+
pl: (b & 0x40) !== 0,
|
|
129
|
+
ts: (b & 0x20) !== 0,
|
|
130
|
+
cr: (b & 0x10) !== 0,
|
|
131
|
+
ll: (b & 0x08) !== 0,
|
|
132
|
+
cw: (b & 0x04) !== 0,
|
|
133
|
+
tf: (b & 0x02) !== 0,
|
|
134
|
+
};
|
|
135
|
+
}
|
|
136
|
+
/**
|
|
137
|
+
* @param {Opt} o
|
|
138
|
+
* @returns {number} packed opt byte
|
|
139
|
+
*/
|
|
140
|
+
function encodeOpt(o) {
|
|
141
|
+
return ((o.pl ? 0x40 : 0) |
|
|
142
|
+
(o.ts ? 0x20 : 0) |
|
|
143
|
+
(o.cr ? 0x10 : 0) |
|
|
144
|
+
(o.ll ? 0x08 : 0) |
|
|
145
|
+
(o.cw ? 0x04 : 0) |
|
|
146
|
+
(o.tf ? 0x02 : 0));
|
|
147
|
+
}
|
|
148
|
+
/* --------------------------------------------------------------- endian --- */
|
|
149
|
+
/**
|
|
150
|
+
* Read `n` (<= 4) little-endian bytes at `off` as an unsigned Number.
|
|
151
|
+
*
|
|
152
|
+
* @param {Uint8Array} b
|
|
153
|
+
* @param {number} off
|
|
154
|
+
* @param {number} n
|
|
155
|
+
* @returns {number}
|
|
156
|
+
*/
|
|
157
|
+
function readLe(b, off, n) {
|
|
158
|
+
let v = 0;
|
|
159
|
+
for (let i = 0; i < n; i++)
|
|
160
|
+
v += b[off + i] * 2 ** (8 * i);
|
|
161
|
+
return v;
|
|
162
|
+
}
|
|
163
|
+
/**
|
|
164
|
+
* Read 8 little-endian bytes at `off` as an unsigned BigInt.
|
|
165
|
+
*
|
|
166
|
+
* @param {Uint8Array} b
|
|
167
|
+
* @param {number} off
|
|
168
|
+
* @returns {bigint}
|
|
169
|
+
*/
|
|
170
|
+
function readLe64(b, off) {
|
|
171
|
+
let v = 0n;
|
|
172
|
+
for (let i = 0; i < 8; i++)
|
|
173
|
+
v += BigInt(b[off + i]) << BigInt(8 * i);
|
|
174
|
+
return v;
|
|
175
|
+
}
|
|
176
|
+
/**
|
|
177
|
+
* Append `n` (<= 4) little-endian bytes of unsigned Number `v`.
|
|
178
|
+
*
|
|
179
|
+
* @param {number[]} out
|
|
180
|
+
* @param {number} v
|
|
181
|
+
* @param {number} n
|
|
182
|
+
*/
|
|
183
|
+
function writeLe(out, v, n) {
|
|
184
|
+
for (let i = 0; i < n; i++)
|
|
185
|
+
out.push((v >>> (8 * i)) & 0xff);
|
|
186
|
+
}
|
|
187
|
+
/**
|
|
188
|
+
* Append 8 little-endian bytes of unsigned BigInt `v`.
|
|
189
|
+
*
|
|
190
|
+
* @param {number[]} out
|
|
191
|
+
* @param {bigint} v
|
|
192
|
+
*/
|
|
193
|
+
function writeLe64(out, v) {
|
|
194
|
+
let x = BigInt.asUintN(64, v);
|
|
195
|
+
for (let i = 0; i < 8; i++) {
|
|
196
|
+
out.push(Number(x & 0xffn));
|
|
197
|
+
x >>= 8n;
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
/* ------------------------------------------------------------------ crc --- */
|
|
201
|
+
const CRC32C_TABLE = (() => {
|
|
202
|
+
const t = new Uint32Array(256);
|
|
203
|
+
for (let i = 0; i < 256; i++) {
|
|
204
|
+
let c = i;
|
|
205
|
+
for (let k = 0; k < 8; k++)
|
|
206
|
+
c = c & 1 ? 0x82f63b78 ^ (c >>> 1) : c >>> 1;
|
|
207
|
+
t[i] = c >>> 0;
|
|
208
|
+
}
|
|
209
|
+
return t;
|
|
210
|
+
})();
|
|
211
|
+
const CRC16_TABLE = (() => {
|
|
212
|
+
const t = new Uint16Array(256);
|
|
213
|
+
for (let i = 0; i < 256; i++) {
|
|
214
|
+
let c = (i << 8) & 0xffff;
|
|
215
|
+
for (let k = 0; k < 8; k++)
|
|
216
|
+
c = c & 0x8000 ? ((c << 1) ^ 0x1021) & 0xffff : (c << 1) & 0xffff;
|
|
217
|
+
t[i] = c;
|
|
218
|
+
}
|
|
219
|
+
return t;
|
|
220
|
+
})();
|
|
221
|
+
/**
|
|
222
|
+
* CRC-32C (Castagnoli): reflected poly 0x82F63B78, init/xor 0xFFFFFFFF.
|
|
223
|
+
*
|
|
224
|
+
* @param {Uint8Array} data
|
|
225
|
+
* @returns {number} unsigned 32-bit
|
|
226
|
+
*/
|
|
227
|
+
export function crc32c(data) {
|
|
228
|
+
let c = 0xffffffff;
|
|
229
|
+
for (let i = 0; i < data.length; i++)
|
|
230
|
+
c = CRC32C_TABLE[(c ^ data[i]) & 0xff] ^ (c >>> 8);
|
|
231
|
+
return (c ^ 0xffffffff) >>> 0;
|
|
232
|
+
}
|
|
233
|
+
/**
|
|
234
|
+
* CRC-16-CCITT (FALSE): poly 0x1021, MSB-first, init 0xFFFF, no final xor.
|
|
235
|
+
*
|
|
236
|
+
* @param {Uint8Array} data
|
|
237
|
+
* @returns {number} unsigned 16-bit
|
|
238
|
+
*/
|
|
239
|
+
export function crc16ccitt(data) {
|
|
240
|
+
let c = 0xffff;
|
|
241
|
+
for (let i = 0; i < data.length; i++) {
|
|
242
|
+
const idx = ((c >> 8) ^ data[i]) & 0xff;
|
|
243
|
+
c = (CRC16_TABLE[idx] ^ (c << 8)) & 0xffff;
|
|
244
|
+
}
|
|
245
|
+
return c;
|
|
246
|
+
}
|
|
247
|
+
/* --------------------------------------------------------------- decode --- */
|
|
248
|
+
/**
|
|
249
|
+
* Parse exactly one TLV at the front of `buf`, WITHOUT recursing into children.
|
|
250
|
+
* Mirrors `parse_one` in frame.cpp.
|
|
251
|
+
*
|
|
252
|
+
* @param {Uint8Array} buf
|
|
253
|
+
* @returns {{tlv: Tlv, total: number, children: Uint8Array}}
|
|
254
|
+
*/
|
|
255
|
+
function parseOne(buf) {
|
|
256
|
+
if (buf.length < 4)
|
|
257
|
+
throw new CodecError(ERROR.FRAME_TRUNCATED);
|
|
258
|
+
const typeB = buf[0];
|
|
259
|
+
const optB = buf[1];
|
|
260
|
+
if (typeB === 0x00)
|
|
261
|
+
throw new CodecError(ERROR.FRAME_INVALID);
|
|
262
|
+
if (reservedSet(optB))
|
|
263
|
+
throw new CodecError(ERROR.FRAME_INVALID);
|
|
264
|
+
const opt = decodeOpt(optB);
|
|
265
|
+
const header = opt.ll ? 6 : 4;
|
|
266
|
+
if (buf.length < header)
|
|
267
|
+
throw new CodecError(ERROR.FRAME_TRUNCATED);
|
|
268
|
+
const length = readLe(buf, 2, opt.ll ? 4 : 2);
|
|
269
|
+
const tsSize = opt.ts ? (opt.tf ? 4 : 8) : 0;
|
|
270
|
+
const crcSize = opt.cr ? (opt.cw ? 2 : 4) : 0;
|
|
271
|
+
const total = header + length + tsSize + crcSize;
|
|
272
|
+
if (buf.length < total)
|
|
273
|
+
throw new CodecError(ERROR.FRAME_TRUNCATED);
|
|
274
|
+
/** @type {Tlv} */
|
|
275
|
+
const tlv = {
|
|
276
|
+
type: typeB,
|
|
277
|
+
opt,
|
|
278
|
+
payload: new Uint8Array(0),
|
|
279
|
+
children: [],
|
|
280
|
+
trailer: null,
|
|
281
|
+
};
|
|
282
|
+
const payload = buf.subarray(header, header + length);
|
|
283
|
+
if (opt.ts || opt.cr) {
|
|
284
|
+
/** @type {Trailer} */
|
|
285
|
+
const trailer = { ts: null, crc: null };
|
|
286
|
+
if (opt.ts) {
|
|
287
|
+
if (opt.tf) {
|
|
288
|
+
// signed i32
|
|
289
|
+
const raw = readLe(buf, header + length, 4);
|
|
290
|
+
trailer.ts = { relative: true, value: BigInt(raw | 0) };
|
|
291
|
+
}
|
|
292
|
+
else {
|
|
293
|
+
trailer.ts = { relative: false, value: readLe64(buf, header + length) };
|
|
294
|
+
}
|
|
295
|
+
}
|
|
296
|
+
if (opt.cr) {
|
|
297
|
+
const crcOff = header + length + tsSize;
|
|
298
|
+
// CRC covers payload bytes followed by timestamp bytes (NOT the header).
|
|
299
|
+
const covered = buf.subarray(header, header + length + tsSize);
|
|
300
|
+
if (opt.cw) {
|
|
301
|
+
const value = readLe(buf, crcOff, 2);
|
|
302
|
+
if (crc16ccitt(covered) !== value)
|
|
303
|
+
throw new CodecError(ERROR.FRAME_CRC_FAIL);
|
|
304
|
+
trailer.crc = { width: 'CRC16_CCITT', value };
|
|
305
|
+
}
|
|
306
|
+
else {
|
|
307
|
+
const value = readLe(buf, crcOff, 4);
|
|
308
|
+
if (crc32c(covered) !== value)
|
|
309
|
+
throw new CodecError(ERROR.FRAME_CRC_FAIL);
|
|
310
|
+
trailer.crc = { width: 'CRC32C', value };
|
|
311
|
+
}
|
|
312
|
+
}
|
|
313
|
+
tlv.trailer = trailer;
|
|
314
|
+
}
|
|
315
|
+
// For a structured TLV (opt.pl) decode() walks `children` (the payload region)
|
|
316
|
+
// with an explicit stack; for an opaque TLV the bytes live in tlv.payload.
|
|
317
|
+
if (!opt.pl)
|
|
318
|
+
tlv.payload = payload;
|
|
319
|
+
return { tlv, total, children: payload };
|
|
320
|
+
}
|
|
321
|
+
/**
|
|
322
|
+
* Decode exactly one TLV that fills `input` into a TLV tree. Nesting is parsed
|
|
323
|
+
* ITERATIVELY with an explicit stack (recursion is forbidden) and capped at
|
|
324
|
+
* {@link MAX_DEPTH}. Trailing bytes after the root => FRAME_INVALID.
|
|
325
|
+
*
|
|
326
|
+
* @param {Uint8Array} input
|
|
327
|
+
* @returns {Tlv}
|
|
328
|
+
*/
|
|
329
|
+
export function decode(input) {
|
|
330
|
+
const root = parseOne(input);
|
|
331
|
+
if (root.total !== input.length)
|
|
332
|
+
throw new CodecError(ERROR.FRAME_INVALID); // trailing bytes
|
|
333
|
+
if (!root.tlv.opt.pl)
|
|
334
|
+
return root.tlv; // opaque root: done
|
|
335
|
+
// An open structured node whose children are still being parsed. `pos` is the
|
|
336
|
+
// cursor within `payload`; `total` advances the parent's cursor when it closes.
|
|
337
|
+
const stack = [{ node: root.tlv, payload: root.children, pos: 0, total: root.total }];
|
|
338
|
+
for (;;) {
|
|
339
|
+
const top = stack[stack.length - 1];
|
|
340
|
+
if (top.pos === top.payload.length) {
|
|
341
|
+
// Node complete — pop and graft onto its parent (or return if root).
|
|
342
|
+
const done = top;
|
|
343
|
+
stack.pop();
|
|
344
|
+
if (stack.length === 0)
|
|
345
|
+
return done.node;
|
|
346
|
+
const parent = stack[stack.length - 1];
|
|
347
|
+
parent.node.children.push(done.node);
|
|
348
|
+
parent.pos += done.total;
|
|
349
|
+
continue;
|
|
350
|
+
}
|
|
351
|
+
// A child of `top` sits at depth == stack.length; reject at the cap.
|
|
352
|
+
if (stack.length >= MAX_DEPTH)
|
|
353
|
+
throw new CodecError(ERROR.TLV_NESTING_TOO_DEEP);
|
|
354
|
+
const child = parseOne(top.payload.subarray(top.pos));
|
|
355
|
+
if (child.tlv.opt.pl) {
|
|
356
|
+
stack.push({ node: child.tlv, payload: child.children, pos: 0, total: child.total });
|
|
357
|
+
}
|
|
358
|
+
else {
|
|
359
|
+
top.node.children.push(child.tlv);
|
|
360
|
+
top.pos += child.total;
|
|
361
|
+
}
|
|
362
|
+
}
|
|
363
|
+
}
|
|
364
|
+
/* --------------------------------------------------------------- encode --- */
|
|
365
|
+
/**
|
|
366
|
+
* Encode a TLV tree back to wire bytes, recomputing the trailer CRC from the
|
|
367
|
+
* body (+ timestamp bytes) when opt.CR is set. Mirrors `encode` in frame.cpp.
|
|
368
|
+
*
|
|
369
|
+
* @param {Tlv} tlv
|
|
370
|
+
* @returns {Uint8Array}
|
|
371
|
+
*/
|
|
372
|
+
export function encode(tlv) {
|
|
373
|
+
/** @type {number[]} */
|
|
374
|
+
let body = [];
|
|
375
|
+
if (tlv.opt.pl) {
|
|
376
|
+
for (const child of tlv.children) {
|
|
377
|
+
const cb = encode(child);
|
|
378
|
+
for (let i = 0; i < cb.length; i++)
|
|
379
|
+
body.push(cb[i]);
|
|
380
|
+
}
|
|
381
|
+
}
|
|
382
|
+
else {
|
|
383
|
+
for (let i = 0; i < tlv.payload.length; i++)
|
|
384
|
+
body.push(tlv.payload[i]);
|
|
385
|
+
}
|
|
386
|
+
/** @type {number[]} */
|
|
387
|
+
const out = [];
|
|
388
|
+
out.push(tlv.type & 0xff);
|
|
389
|
+
out.push(encodeOpt(tlv.opt));
|
|
390
|
+
writeLe(out, body.length, tlv.opt.ll ? 4 : 2);
|
|
391
|
+
for (let i = 0; i < body.length; i++)
|
|
392
|
+
out.push(body[i]);
|
|
393
|
+
/** @type {number[]} */
|
|
394
|
+
const tsBytes = [];
|
|
395
|
+
if (tlv.opt.ts) {
|
|
396
|
+
const t = tlv.trailer && tlv.trailer.ts ? tlv.trailer.ts : { relative: tlv.opt.tf, value: 0n };
|
|
397
|
+
if (tlv.opt.tf) {
|
|
398
|
+
writeLe(tsBytes, Number(BigInt.asUintN(32, BigInt(t.value))), 4);
|
|
399
|
+
}
|
|
400
|
+
else {
|
|
401
|
+
writeLe64(tsBytes, BigInt(t.value));
|
|
402
|
+
}
|
|
403
|
+
for (let i = 0; i < tsBytes.length; i++)
|
|
404
|
+
out.push(tsBytes[i]);
|
|
405
|
+
}
|
|
406
|
+
if (tlv.opt.cr) {
|
|
407
|
+
const covered = Uint8Array.from(body.concat(tsBytes));
|
|
408
|
+
if (tlv.opt.cw) {
|
|
409
|
+
writeLe(out, crc16ccitt(covered), 2);
|
|
410
|
+
}
|
|
411
|
+
else {
|
|
412
|
+
writeLe(out, crc32c(covered), 4);
|
|
413
|
+
}
|
|
414
|
+
}
|
|
415
|
+
return Uint8Array.from(out);
|
|
416
|
+
}
|
|
417
|
+
/**
|
|
418
|
+
* Structural + byte-content equality of two TLV trees.
|
|
419
|
+
*
|
|
420
|
+
* @param {Tlv} a
|
|
421
|
+
* @param {Tlv} b
|
|
422
|
+
* @returns {boolean}
|
|
423
|
+
*/
|
|
424
|
+
export function equal(a, b) {
|
|
425
|
+
if (a.type !== b.type)
|
|
426
|
+
return false;
|
|
427
|
+
if (encodeOpt(a.opt) !== encodeOpt(b.opt))
|
|
428
|
+
return false;
|
|
429
|
+
if (!trailerEqual(a.trailer, b.trailer))
|
|
430
|
+
return false;
|
|
431
|
+
if (a.payload.length !== b.payload.length)
|
|
432
|
+
return false;
|
|
433
|
+
for (let i = 0; i < a.payload.length; i++)
|
|
434
|
+
if (a.payload[i] !== b.payload[i])
|
|
435
|
+
return false;
|
|
436
|
+
if (a.children.length !== b.children.length)
|
|
437
|
+
return false;
|
|
438
|
+
for (let i = 0; i < a.children.length; i++)
|
|
439
|
+
if (!equal(a.children[i], b.children[i]))
|
|
440
|
+
return false;
|
|
441
|
+
return true;
|
|
442
|
+
}
|
|
443
|
+
/**
|
|
444
|
+
* @param {Trailer|null} a
|
|
445
|
+
* @param {Trailer|null} b
|
|
446
|
+
* @returns {boolean}
|
|
447
|
+
*/
|
|
448
|
+
function trailerEqual(a, b) {
|
|
449
|
+
if (a === null || b === null)
|
|
450
|
+
return a === b;
|
|
451
|
+
const tsEq = a.ts === null || b.ts === null
|
|
452
|
+
? a.ts === b.ts
|
|
453
|
+
: a.ts.relative === b.ts.relative && BigInt(a.ts.value) === BigInt(b.ts.value);
|
|
454
|
+
if (!tsEq)
|
|
455
|
+
return false;
|
|
456
|
+
const crcEq = a.crc === null || b.crc === null
|
|
457
|
+
? a.crc === b.crc
|
|
458
|
+
: a.crc.width === b.crc.width && a.crc.value === b.crc.value;
|
|
459
|
+
return crcEq;
|
|
460
|
+
}
|
package/dist/index.d.ts
ADDED
package/dist/index.js
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
2
|
+
// SPDX-FileCopyrightText: Copyright 2026 avatarsd LLC
|
|
3
|
+
// Barrel entry (`@avatarsd-llc/libtracer`) — re-exports every layer the TS core
|
|
4
|
+
// implements. For tree-shaking, the wire codec is also reachable directly at the
|
|
5
|
+
// layer-scoped subpath `@avatarsd-llc/libtracer/wire` (see ./wire.ts and
|
|
6
|
+
// docs/adr/0033-npm-subpackage-monorepo.md).
|
|
7
|
+
export const SPEC_VERSION = 1;
|
|
8
|
+
// The L2/L3 wire codec — a pure-TS/JS port of the C++ reference (core/src/frame.cpp),
|
|
9
|
+
// gated by the shared conformance vectors. Re-exported from ./wire.ts (the subpath entry).
|
|
10
|
+
export { TYPE, ERROR, MAX_DEPTH, CodecError, decode, encode, equal, crc32c, crc16ccitt, } from './wire.js';
|
package/dist/wire.d.ts
ADDED
package/dist/wire.js
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
2
|
+
// SPDX-FileCopyrightText: Copyright 2026 avatarsd LLC
|
|
3
|
+
// The L2/L3 wire codec layer (`tr::wire` in the C++ reference) — exposed as the
|
|
4
|
+
// tree-shakeable subpath entry `@avatarsd-llc/libtracer/wire`. It is a pure-TS
|
|
5
|
+
// port of core/src/frame.cpp, gated byte-for-byte by the shared conformance
|
|
6
|
+
// vectors. This is the only layer the TS core implements today; the L0/L1/L4
|
|
7
|
+
// subpaths (./mem, ./view, ./graph) are reserved for when those layers land in
|
|
8
|
+
// TS — see docs/adr/0033-npm-subpackage-monorepo.md.
|
|
9
|
+
export { TYPE, ERROR, MAX_DEPTH, CodecError, decode, encode, equal, crc32c, crc16ccitt, } from './codec.mjs';
|
package/package.json
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@avatarsd-llc/libtracer",
|
|
3
|
+
"version": "0.0.1",
|
|
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
|
+
"author": "avatarsd LLC",
|
|
6
|
+
"license": "Apache-2.0",
|
|
7
|
+
"repository": {
|
|
8
|
+
"type": "git",
|
|
9
|
+
"url": "https://github.com/avatarsd-llc/libtracer.git",
|
|
10
|
+
"directory": "bindings/typescript/packages/core"
|
|
11
|
+
},
|
|
12
|
+
"homepage": "https://github.com/avatarsd-llc/libtracer#readme",
|
|
13
|
+
"bugs": {
|
|
14
|
+
"url": "https://github.com/avatarsd-llc/libtracer/issues"
|
|
15
|
+
},
|
|
16
|
+
"keywords": ["protocol", "iot", "sensor", "graph", "tlv"],
|
|
17
|
+
"type": "module",
|
|
18
|
+
"engines": {
|
|
19
|
+
"node": ">=18"
|
|
20
|
+
},
|
|
21
|
+
"sideEffects": false,
|
|
22
|
+
"main": "dist/index.js",
|
|
23
|
+
"types": "dist/index.d.ts",
|
|
24
|
+
"exports": {
|
|
25
|
+
".": {
|
|
26
|
+
"types": "./dist/index.d.ts",
|
|
27
|
+
"default": "./dist/index.js"
|
|
28
|
+
},
|
|
29
|
+
"./wire": {
|
|
30
|
+
"types": "./dist/wire.d.ts",
|
|
31
|
+
"default": "./dist/wire.js"
|
|
32
|
+
},
|
|
33
|
+
"./package.json": "./package.json"
|
|
34
|
+
},
|
|
35
|
+
"files": ["dist", "README.md", "LICENSE"],
|
|
36
|
+
"publishConfig": {
|
|
37
|
+
"access": "public"
|
|
38
|
+
},
|
|
39
|
+
"scripts": {
|
|
40
|
+
"build": "tsc",
|
|
41
|
+
"conformance": "node conformance/harness.mjs ../../../../tests/conformance/vectors/v1",
|
|
42
|
+
"bench": "node bench/perf.mjs",
|
|
43
|
+
"test": "npm run conformance",
|
|
44
|
+
"prepublishOnly": "npm run build"
|
|
45
|
+
},
|
|
46
|
+
"devDependencies": {
|
|
47
|
+
"typescript": "^5.4.0"
|
|
48
|
+
}
|
|
49
|
+
}
|