@ac-kit/format-cbor 0.1.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/LICENSE +28 -0
- package/README.md +68 -0
- package/dist/ast.d.ts +44 -0
- package/dist/ast.d.ts.map +1 -0
- package/dist/ast.js +2 -0
- package/dist/ast.js.map +1 -0
- package/dist/binary/decoder.d.ts +52 -0
- package/dist/binary/decoder.d.ts.map +1 -0
- package/dist/binary/decoder.js +225 -0
- package/dist/binary/decoder.js.map +1 -0
- package/dist/binary/encoder.d.ts +42 -0
- package/dist/binary/encoder.d.ts.map +1 -0
- package/dist/binary/encoder.js +162 -0
- package/dist/binary/encoder.js.map +1 -0
- package/dist/errors.d.ts +8 -0
- package/dist/errors.d.ts.map +1 -0
- package/dist/errors.js +11 -0
- package/dist/errors.js.map +1 -0
- package/dist/index.d.ts +13 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +13 -0
- package/dist/index.js.map +1 -0
- package/dist/json-bridge.d.ts +26 -0
- package/dist/json-bridge.d.ts.map +1 -0
- package/dist/json-bridge.js +77 -0
- package/dist/json-bridge.js.map +1 -0
- package/dist/notation/diag-node.d.ts +56 -0
- package/dist/notation/diag-node.d.ts.map +1 -0
- package/dist/notation/diag-node.js +2 -0
- package/dist/notation/diag-node.js.map +1 -0
- package/dist/notation/diag-to-value.d.ts +8 -0
- package/dist/notation/diag-to-value.d.ts.map +1 -0
- package/dist/notation/diag-to-value.js +37 -0
- package/dist/notation/diag-to-value.js.map +1 -0
- package/dist/notation/lexer.d.ts +15 -0
- package/dist/notation/lexer.d.ts.map +1 -0
- package/dist/notation/lexer.js +285 -0
- package/dist/notation/lexer.js.map +1 -0
- package/dist/notation/parser.d.ts +12 -0
- package/dist/notation/parser.d.ts.map +1 -0
- package/dist/notation/parser.js +232 -0
- package/dist/notation/parser.js.map +1 -0
- package/dist/notation/printer.d.ts +13 -0
- package/dist/notation/printer.d.ts.map +1 -0
- package/dist/notation/printer.js +99 -0
- package/dist/notation/printer.js.map +1 -0
- package/dist/notation/token.d.ts +28 -0
- package/dist/notation/token.d.ts.map +1 -0
- package/dist/notation/token.js +18 -0
- package/dist/notation/token.js.map +1 -0
- package/dist/walk.d.ts +8 -0
- package/dist/walk.d.ts.map +1 -0
- package/dist/walk.js +19 -0
- package/dist/walk.js.map +1 -0
- package/package.json +67 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
BSD 3-Clause License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024-2026 Anthony Champagne and contributors
|
|
4
|
+
|
|
5
|
+
Redistribution and use in source and binary forms, with or without
|
|
6
|
+
modification, are permitted provided that the following conditions are met:
|
|
7
|
+
|
|
8
|
+
1. Redistributions of source code must retain the above copyright notice, this
|
|
9
|
+
list of conditions and the following disclaimer.
|
|
10
|
+
|
|
11
|
+
2. Redistributions in binary form must reproduce the above copyright notice,
|
|
12
|
+
this list of conditions and the following disclaimer in the documentation
|
|
13
|
+
and/or other materials provided with the distribution.
|
|
14
|
+
|
|
15
|
+
3. Neither the name of the copyright holder nor the names of its
|
|
16
|
+
contributors may be used to endorse or promote products derived from
|
|
17
|
+
this software without specific prior written permission.
|
|
18
|
+
|
|
19
|
+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
20
|
+
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
21
|
+
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
22
|
+
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
|
23
|
+
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
24
|
+
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
25
|
+
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
|
26
|
+
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
|
27
|
+
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
28
|
+
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
package/README.md
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
# @ac-kit/format-cbor
|
|
2
|
+
|
|
3
|
+
A CBOR (RFC 8949) codec: a binary encoder/decoder (including an incremental
|
|
4
|
+
decoder for streams), a diagnostic-notation lexer/parser/printer (RFC 8949 §8,
|
|
5
|
+
extended with comments from RFC 8610 Appendix G, "EDN"), and a JSON bridge (RFC
|
|
6
|
+
8949 §6). One value model — {@link DataValue} — is shared by all three, the
|
|
7
|
+
second real example (alongside `@ac-kit/format-asn1*`) of a single schema/AST
|
|
8
|
+
feeding multiple independent wire formats.
|
|
9
|
+
|
|
10
|
+
```ts
|
|
11
|
+
import {
|
|
12
|
+
decodeCbor,
|
|
13
|
+
encodeCbor,
|
|
14
|
+
parseCborNotation,
|
|
15
|
+
printDataValue,
|
|
16
|
+
diagToValue,
|
|
17
|
+
dataValueToJson,
|
|
18
|
+
} from "@ac-kit/format-cbor";
|
|
19
|
+
|
|
20
|
+
const value = parseCborNotation('{"a": 1, "b": [2, 3]}');
|
|
21
|
+
const bytes = encodeCbor(diagToValue(value)); // -> binary CBOR
|
|
22
|
+
decodeCbor(bytes); // -> back to a DataValue
|
|
23
|
+
printDataValue(decodeCbor(bytes)); // -> '{"a": 1, "b": [2, 3]}'
|
|
24
|
+
dataValueToJson(decodeCbor(bytes)); // -> a plain JSON value
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
## Why no separate CST
|
|
28
|
+
|
|
29
|
+
Unlike `@ac-kit/format-asn1-notation`, this package has no CST/AST split.
|
|
30
|
+
ASN.1's notation has real syntactic redundancy (tag class keywords,
|
|
31
|
+
parenthesization, `IMPLICIT`/`EXPLICIT`) that a CST preserves and an AST elides;
|
|
32
|
+
CBOR diagnostic notation doesn't — it's already close to a literal rendering of
|
|
33
|
+
the value tree. `DiagNode` (in `notation/diag-node.ts`) is that value tree with
|
|
34
|
+
`span` and EDN comments attached directly, and `diagToValue()` is the one
|
|
35
|
+
lowering step to a plain `DataValue`. See `notation/diag-node.ts` for the
|
|
36
|
+
reasoning in full.
|
|
37
|
+
|
|
38
|
+
## Incremental decoding
|
|
39
|
+
|
|
40
|
+
`decodeCborItem` models the "Lexer as a streaming incremental decoder" shape
|
|
41
|
+
directly on `@ac-kit/format-core`'s `DecodeResult` (`incomplete`/`decoded`/
|
|
42
|
+
`fatal`) rather than eagerly tokenizing a fully-buffered string, the way
|
|
43
|
+
`@ac-kit/format-cron` and `@ac-kit/format-regex`'s lexers do — CBOR items are
|
|
44
|
+
self-delimiting but their total length isn't known until fully parsed, which is
|
|
45
|
+
exactly the case that needs this shape. `CborItemDecoder` adapts it to
|
|
46
|
+
`@ac-kit/format-core`'s `Decoder` contract; drive it with `decodeAll` for a
|
|
47
|
+
complete buffer (a CBOR sequence already fully in memory) or `DecodeStream` for
|
|
48
|
+
one arriving in chunks over time.
|
|
49
|
+
|
|
50
|
+
## Scope
|
|
51
|
+
|
|
52
|
+
Implemented: all 8 major types, definite- and indefinite-length decoding,
|
|
53
|
+
half/single/double-precision float decoding, tags (generic — no semantic
|
|
54
|
+
interpretation of specific tag numbers), simple values, the diagnostic
|
|
55
|
+
notation's base grammar plus EDN comments.
|
|
56
|
+
|
|
57
|
+
Not implemented (deliberately, not silently): indefinite-length _encoding_ (the
|
|
58
|
+
encoder always produces definite-length output — still valid CBOR), bignum
|
|
59
|
+
promotion for integers outside `-2^64..2^64-1` (throws instead),
|
|
60
|
+
deterministic/canonical encoding (RFC 8949 §4.2), semantic tag interpretation
|
|
61
|
+
(dates, bignums, encoding hints — tags decode as generic `{tag, value}` pairs),
|
|
62
|
+
and most EDN extensions beyond comments (hex/octal/binary numbers, concatenated
|
|
63
|
+
strings, embedded-CBOR `<<...>>`, unprefixed text-as-bytes, base32/base32hex
|
|
64
|
+
byte strings).
|
|
65
|
+
|
|
66
|
+
See the
|
|
67
|
+
[generated API docs](https://anthochamp.github.io/node-essentials/api/format-cbor/)
|
|
68
|
+
for the full reference.
|
package/dist/ast.d.ts
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The CBOR generic data model (RFC 8949 §2) — the single value tree shared by
|
|
3
|
+
* the binary codec, the diagnostic-notation codec, and the JSON bridge.
|
|
4
|
+
*
|
|
5
|
+
* Integers and floating-point values are distinct even when numerically equal
|
|
6
|
+
* (RFC 8949 §2), so `int` always carries a `bigint` and `float` always carries
|
|
7
|
+
* a JS `number` (binary64) — never conflated.
|
|
8
|
+
*/
|
|
9
|
+
export type DataValue = {
|
|
10
|
+
readonly kind: "int";
|
|
11
|
+
readonly value: bigint;
|
|
12
|
+
} | {
|
|
13
|
+
readonly kind: "float";
|
|
14
|
+
readonly value: number;
|
|
15
|
+
} | {
|
|
16
|
+
readonly kind: "bytes";
|
|
17
|
+
readonly value: Uint8Array;
|
|
18
|
+
} | {
|
|
19
|
+
readonly kind: "text";
|
|
20
|
+
readonly value: string;
|
|
21
|
+
} | {
|
|
22
|
+
readonly kind: "array";
|
|
23
|
+
readonly items: readonly DataValue[];
|
|
24
|
+
} | {
|
|
25
|
+
readonly kind: "map";
|
|
26
|
+
readonly entries: readonly (readonly [DataValue, DataValue])[];
|
|
27
|
+
} | {
|
|
28
|
+
readonly kind: "tag";
|
|
29
|
+
readonly tag: bigint;
|
|
30
|
+
readonly value: DataValue;
|
|
31
|
+
} | {
|
|
32
|
+
readonly kind: "bool";
|
|
33
|
+
readonly value: boolean;
|
|
34
|
+
} | {
|
|
35
|
+
readonly kind: "null";
|
|
36
|
+
} | {
|
|
37
|
+
readonly kind: "undefined";
|
|
38
|
+
}
|
|
39
|
+
/** An unassigned simple value (RFC 8949 §3.3), 0-19 or 32-255. */
|
|
40
|
+
| {
|
|
41
|
+
readonly kind: "simple";
|
|
42
|
+
readonly value: number;
|
|
43
|
+
};
|
|
44
|
+
//# sourceMappingURL=ast.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ast.d.ts","sourceRoot":"","sources":["../src/ast.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AACH,MAAM,MAAM,SAAS,GAClB;IAAE,QAAQ,CAAC,IAAI,EAAE,KAAK,CAAC;IAAC,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAA;CAAE,GAChD;IAAE,QAAQ,CAAC,IAAI,EAAE,OAAO,CAAC;IAAC,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAA;CAAE,GAClD;IAAE,QAAQ,CAAC,IAAI,EAAE,OAAO,CAAC;IAAC,QAAQ,CAAC,KAAK,EAAE,UAAU,CAAA;CAAE,GACtD;IAAE,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IAAC,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAA;CAAE,GACjD;IAAE,QAAQ,CAAC,IAAI,EAAE,OAAO,CAAC;IAAC,QAAQ,CAAC,KAAK,EAAE,SAAS,SAAS,EAAE,CAAA;CAAE,GAChE;IACA,QAAQ,CAAC,IAAI,EAAE,KAAK,CAAC;IACrB,QAAQ,CAAC,OAAO,EAAE,SAAS,CAAC,SAAS,CAAC,SAAS,EAAE,SAAS,CAAC,CAAC,EAAE,CAAC;CAC9D,GACD;IAAE,QAAQ,CAAC,IAAI,EAAE,KAAK,CAAC;IAAC,QAAQ,CAAC,GAAG,EAAE,MAAM,CAAC;IAAC,QAAQ,CAAC,KAAK,EAAE,SAAS,CAAA;CAAE,GACzE;IAAE,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IAAC,QAAQ,CAAC,KAAK,EAAE,OAAO,CAAA;CAAE,GAClD;IAAE,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAA;CAAE,GACzB;IAAE,QAAQ,CAAC,IAAI,EAAE,WAAW,CAAA;CAAE;AAChC,kEAAkE;GAChE;IAAE,QAAQ,CAAC,IAAI,EAAE,QAAQ,CAAC;IAAC,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAA;CAAE,CAAC"}
|
package/dist/ast.js
ADDED
package/dist/ast.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ast.js","sourceRoot":"","sources":["../src/ast.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import type { Decoder, DecodeResult } from "@ac-kit/format-core";
|
|
2
|
+
import type { DataValue } from "../ast.js";
|
|
3
|
+
import { CborDecodeError } from "../errors.js";
|
|
4
|
+
/** Outcome of {@link decodeCborItem}. */
|
|
5
|
+
export type CborItemDecodeResult = {
|
|
6
|
+
readonly status: "incomplete";
|
|
7
|
+
} | {
|
|
8
|
+
readonly status: "item";
|
|
9
|
+
readonly value: DataValue;
|
|
10
|
+
readonly consumed: number;
|
|
11
|
+
} | {
|
|
12
|
+
readonly status: "error";
|
|
13
|
+
readonly error: CborDecodeError;
|
|
14
|
+
/** Bytes the reader had consumed when the item was rejected. */
|
|
15
|
+
readonly consumed: number;
|
|
16
|
+
};
|
|
17
|
+
/**
|
|
18
|
+
* Attempts to decode one top-level CBOR data item starting at `offset`.
|
|
19
|
+
*
|
|
20
|
+
* Modeled on `@ac-kit/format-core`'s incremental `DecodeResult` (this package's
|
|
21
|
+
* answer to whether a "Lexer" should be a streaming class or an eager
|
|
22
|
+
* tokenize-to-array function — CBOR is self-delimiting but an item's total
|
|
23
|
+
* length isn't known until it is fully parsed, which is exactly the shape that
|
|
24
|
+
* needs an incremental result instead of an eager array). Unlike
|
|
25
|
+
* `DecodeResult`, `incomplete` carries no `needAtLeast` — the recursive descent
|
|
26
|
+
* has no cheap way to compute one; a caller retries once more bytes arrive.
|
|
27
|
+
* {@link CborItemDecoder} adapts this to the `Decoder` contract directly, for
|
|
28
|
+
* driving via `decodeAll`/`DecodeStream`.
|
|
29
|
+
*/
|
|
30
|
+
export declare function decodeCborItem(buffer: Uint8Array, offset?: number): CborItemDecodeResult;
|
|
31
|
+
/**
|
|
32
|
+
* Decodes exactly one CBOR data item from `bytes`; no trailing bytes are
|
|
33
|
+
* allowed.
|
|
34
|
+
*/
|
|
35
|
+
export declare function decodeCbor(bytes: Uint8Array): DataValue;
|
|
36
|
+
/**
|
|
37
|
+
* Adapts {@link decodeCborItem} to `@ac-kit/format-core`'s `Decoder` contract,
|
|
38
|
+
* for decoding a CBOR sequence (RFC 8742) via `decodeAll` (a complete buffer)
|
|
39
|
+
* or `DecodeStream` (chunks arriving over time). Replaces a hand-rolled
|
|
40
|
+
* `CborSequenceDecoder` that reimplemented the same accumulate-and-dispatch
|
|
41
|
+
* loop `DecodeDriver` now provides generically.
|
|
42
|
+
*
|
|
43
|
+
* `error` results are recoverable whenever the reader made progress before
|
|
44
|
+
* rejecting the item: `consumed` bytes are dropped and the next item is tried,
|
|
45
|
+
* which is what lets one bad item in a sequence not end the whole stream. An
|
|
46
|
+
* error at offset zero is reported as `fatal`, since retrying from an unmoved
|
|
47
|
+
* position could not terminate.
|
|
48
|
+
*/
|
|
49
|
+
export declare class CborItemDecoder implements Decoder<DataValue, Uint8Array, CborDecodeError> {
|
|
50
|
+
decode(view: Uint8Array): DecodeResult<DataValue, CborDecodeError>;
|
|
51
|
+
}
|
|
52
|
+
//# sourceMappingURL=decoder.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"decoder.d.ts","sourceRoot":"","sources":["../../src/binary/decoder.ts"],"names":[],"mappings":"AASA,OAAO,KAAK,EAAE,OAAO,EAAE,YAAY,EAAE,MAAM,qBAAqB,CAAC;AAGjE,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,WAAW,CAAC;AAC3C,OAAO,EAAE,eAAe,EAAE,MAAM,cAAc,CAAC;AA8K/C,yCAAyC;AACzC,MAAM,MAAM,oBAAoB,GAC7B;IAAE,QAAQ,CAAC,MAAM,EAAE,YAAY,CAAA;CAAE,GACjC;IACA,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAC;IACxB,QAAQ,CAAC,KAAK,EAAE,SAAS,CAAC;IAC1B,QAAQ,CAAC,QAAQ,EAAE,MAAM,CAAC;CACzB,GACD;IACA,QAAQ,CAAC,MAAM,EAAE,OAAO,CAAC;IACzB,QAAQ,CAAC,KAAK,EAAE,eAAe,CAAC;IAChC,gEAAgE;IAChE,QAAQ,CAAC,QAAQ,EAAE,MAAM,CAAC;CACzB,CAAC;AAEL;;;;;;;;;;;;GAYG;AACH,wBAAgB,cAAc,CAC7B,MAAM,EAAE,UAAU,EAClB,MAAM,SAAI,GACR,oBAAoB,CAgBtB;AAED;;;GAGG;AACH,wBAAgB,UAAU,CAAC,KAAK,EAAE,UAAU,GAAG,SAAS,CAcvD;AAED;;;;;;;;;;;;GAYG;AACH,qBAAa,eAAgB,YAAW,OAAO,CAC9C,SAAS,EACT,UAAU,EACV,eAAe,CACf;IACA,MAAM,CAAC,IAAI,EAAE,UAAU,GAAG,YAAY,CAAC,SAAS,EAAE,eAAe,CAAC,CAoBjE;CACD"}
|
|
@@ -0,0 +1,225 @@
|
|
|
1
|
+
import { ByteReader, concatBytes, decodeText, getBigUint64Be, getFloat32Be, getFloat64Be, getUint32Be, } from "@ac-kit/core";
|
|
2
|
+
import { Float16 } from "@ac-kit/math-numbers";
|
|
3
|
+
import { CborDecodeError } from "../errors.js";
|
|
4
|
+
const BREAK_BYTE = 0xff;
|
|
5
|
+
function readArgument(reader, additionalInfo) {
|
|
6
|
+
if (additionalInfo < 24)
|
|
7
|
+
return BigInt(additionalInfo);
|
|
8
|
+
const byteLength = additionalInfo === 24
|
|
9
|
+
? 1
|
|
10
|
+
: additionalInfo === 25
|
|
11
|
+
? 2
|
|
12
|
+
: additionalInfo === 26
|
|
13
|
+
? 4
|
|
14
|
+
: 8;
|
|
15
|
+
if (additionalInfo > 27) {
|
|
16
|
+
throw new CborDecodeError(`Reserved additional information value ${additionalInfo}`);
|
|
17
|
+
}
|
|
18
|
+
return getBigUint64Be(reader.read(byteLength), 0, byteLength);
|
|
19
|
+
}
|
|
20
|
+
function decodeByteString(reader, additionalInfo) {
|
|
21
|
+
if (additionalInfo === 31) {
|
|
22
|
+
const chunks = [];
|
|
23
|
+
while (reader.peekByte() !== BREAK_BYTE) {
|
|
24
|
+
const initialByte = reader.readByte();
|
|
25
|
+
const chunkAdditionalInfo = initialByte & 0x1f;
|
|
26
|
+
if (initialByte >>> 5 !== 2 || chunkAdditionalInfo === 31) {
|
|
27
|
+
throw new CborDecodeError("Indefinite-length byte string chunk must be a definite-length byte string");
|
|
28
|
+
}
|
|
29
|
+
chunks.push(new Uint8Array(reader.read(Number(readArgument(reader, chunkAdditionalInfo)))));
|
|
30
|
+
}
|
|
31
|
+
reader.readByte();
|
|
32
|
+
return concatBytes(chunks);
|
|
33
|
+
}
|
|
34
|
+
return new Uint8Array(reader.read(Number(readArgument(reader, additionalInfo))));
|
|
35
|
+
}
|
|
36
|
+
function decodeTextString(reader, additionalInfo) {
|
|
37
|
+
if (additionalInfo === 31) {
|
|
38
|
+
const chunks = [];
|
|
39
|
+
while (reader.peekByte() !== BREAK_BYTE) {
|
|
40
|
+
const initialByte = reader.readByte();
|
|
41
|
+
const chunkAdditionalInfo = initialByte & 0x1f;
|
|
42
|
+
if (initialByte >>> 5 !== 3 || chunkAdditionalInfo === 31) {
|
|
43
|
+
throw new CborDecodeError("Indefinite-length text string chunk must be a definite-length text string");
|
|
44
|
+
}
|
|
45
|
+
chunks.push(new Uint8Array(reader.read(Number(readArgument(reader, chunkAdditionalInfo)))));
|
|
46
|
+
}
|
|
47
|
+
reader.readByte();
|
|
48
|
+
return decodeText(concatBytes(chunks), "utf-8", { fatal: true });
|
|
49
|
+
}
|
|
50
|
+
return decodeText(reader.read(Number(readArgument(reader, additionalInfo))), "utf-8", { fatal: true });
|
|
51
|
+
}
|
|
52
|
+
function decodeArray(reader, additionalInfo) {
|
|
53
|
+
const items = [];
|
|
54
|
+
if (additionalInfo === 31) {
|
|
55
|
+
while (reader.peekByte() !== BREAK_BYTE)
|
|
56
|
+
items.push(decodeItem(reader));
|
|
57
|
+
reader.readByte();
|
|
58
|
+
}
|
|
59
|
+
else {
|
|
60
|
+
const length = Number(readArgument(reader, additionalInfo));
|
|
61
|
+
for (let index = 0; index < length; index++)
|
|
62
|
+
items.push(decodeItem(reader));
|
|
63
|
+
}
|
|
64
|
+
return { kind: "array", items };
|
|
65
|
+
}
|
|
66
|
+
function decodeMap(reader, additionalInfo) {
|
|
67
|
+
const entries = [];
|
|
68
|
+
if (additionalInfo === 31) {
|
|
69
|
+
while (reader.peekByte() !== BREAK_BYTE) {
|
|
70
|
+
entries.push([decodeItem(reader), decodeItem(reader)]);
|
|
71
|
+
}
|
|
72
|
+
reader.readByte();
|
|
73
|
+
}
|
|
74
|
+
else {
|
|
75
|
+
const count = Number(readArgument(reader, additionalInfo));
|
|
76
|
+
for (let index = 0; index < count; index++) {
|
|
77
|
+
entries.push([decodeItem(reader), decodeItem(reader)]);
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
return { kind: "map", entries };
|
|
81
|
+
}
|
|
82
|
+
function decodeSimpleOrFloat(reader, additionalInfo) {
|
|
83
|
+
if (additionalInfo < 20)
|
|
84
|
+
return { kind: "simple", value: additionalInfo };
|
|
85
|
+
switch (additionalInfo) {
|
|
86
|
+
case 20:
|
|
87
|
+
return { kind: "bool", value: false };
|
|
88
|
+
case 21:
|
|
89
|
+
return { kind: "bool", value: true };
|
|
90
|
+
case 22:
|
|
91
|
+
return { kind: "null" };
|
|
92
|
+
case 23:
|
|
93
|
+
return { kind: "undefined" };
|
|
94
|
+
case 24: {
|
|
95
|
+
const value = reader.readByte();
|
|
96
|
+
if (value < 32) {
|
|
97
|
+
throw new CborDecodeError(`Two-byte simple value encoding must encode a value >= 32, got ${value}`);
|
|
98
|
+
}
|
|
99
|
+
return { kind: "simple", value };
|
|
100
|
+
}
|
|
101
|
+
case 25:
|
|
102
|
+
return {
|
|
103
|
+
kind: "float",
|
|
104
|
+
// Raw 16-bit unsigned read (no float reinterpretation needed yet); Float16
|
|
105
|
+
// itself does the half-precision-to-binary64 unpacking.
|
|
106
|
+
value: Float16.fromBits(getUint32Be(reader.read(2), 0, 2)).valueOf(),
|
|
107
|
+
};
|
|
108
|
+
case 26:
|
|
109
|
+
return { kind: "float", value: getFloat32Be(reader.read(4), 0) };
|
|
110
|
+
case 27:
|
|
111
|
+
return { kind: "float", value: getFloat64Be(reader.read(8), 0) };
|
|
112
|
+
default:
|
|
113
|
+
throw new CborDecodeError(`Reserved additional information value ${additionalInfo} for major type 7`);
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
function decodeItem(reader) {
|
|
117
|
+
const initialByte = reader.readByte();
|
|
118
|
+
const majorType = initialByte >>> 5;
|
|
119
|
+
const additionalInfo = initialByte & 0x1f;
|
|
120
|
+
switch (majorType) {
|
|
121
|
+
case 0:
|
|
122
|
+
return { kind: "int", value: readArgument(reader, additionalInfo) };
|
|
123
|
+
case 1:
|
|
124
|
+
return { kind: "int", value: -1n - readArgument(reader, additionalInfo) };
|
|
125
|
+
case 2:
|
|
126
|
+
return { kind: "bytes", value: decodeByteString(reader, additionalInfo) };
|
|
127
|
+
case 3:
|
|
128
|
+
return { kind: "text", value: decodeTextString(reader, additionalInfo) };
|
|
129
|
+
case 4:
|
|
130
|
+
return decodeArray(reader, additionalInfo);
|
|
131
|
+
case 5:
|
|
132
|
+
return decodeMap(reader, additionalInfo);
|
|
133
|
+
case 6: {
|
|
134
|
+
const tag = readArgument(reader, additionalInfo);
|
|
135
|
+
return { kind: "tag", tag, value: decodeItem(reader) };
|
|
136
|
+
}
|
|
137
|
+
default:
|
|
138
|
+
return decodeSimpleOrFloat(reader, additionalInfo);
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
/**
|
|
142
|
+
* Attempts to decode one top-level CBOR data item starting at `offset`.
|
|
143
|
+
*
|
|
144
|
+
* Modeled on `@ac-kit/format-core`'s incremental `DecodeResult` (this package's
|
|
145
|
+
* answer to whether a "Lexer" should be a streaming class or an eager
|
|
146
|
+
* tokenize-to-array function — CBOR is self-delimiting but an item's total
|
|
147
|
+
* length isn't known until it is fully parsed, which is exactly the shape that
|
|
148
|
+
* needs an incremental result instead of an eager array). Unlike
|
|
149
|
+
* `DecodeResult`, `incomplete` carries no `needAtLeast` — the recursive descent
|
|
150
|
+
* has no cheap way to compute one; a caller retries once more bytes arrive.
|
|
151
|
+
* {@link CborItemDecoder} adapts this to the `Decoder` contract directly, for
|
|
152
|
+
* driving via `decodeAll`/`DecodeStream`.
|
|
153
|
+
*/
|
|
154
|
+
export function decodeCborItem(buffer, offset = 0) {
|
|
155
|
+
const reader = new ByteReader(buffer, offset);
|
|
156
|
+
try {
|
|
157
|
+
const value = decodeItem(reader);
|
|
158
|
+
return { status: "item", value, consumed: reader.position - offset };
|
|
159
|
+
}
|
|
160
|
+
catch (error) {
|
|
161
|
+
if (error instanceof RangeError)
|
|
162
|
+
return { status: "incomplete" };
|
|
163
|
+
if (error instanceof CborDecodeError) {
|
|
164
|
+
return {
|
|
165
|
+
status: "error",
|
|
166
|
+
error,
|
|
167
|
+
consumed: reader.position - offset,
|
|
168
|
+
};
|
|
169
|
+
}
|
|
170
|
+
throw error;
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
/**
|
|
174
|
+
* Decodes exactly one CBOR data item from `bytes`; no trailing bytes are
|
|
175
|
+
* allowed.
|
|
176
|
+
*/
|
|
177
|
+
export function decodeCbor(bytes) {
|
|
178
|
+
const result = decodeCborItem(bytes);
|
|
179
|
+
if (result.status === "incomplete") {
|
|
180
|
+
throw new CborDecodeError("Unexpected end of input while decoding a CBOR data item");
|
|
181
|
+
}
|
|
182
|
+
if (result.status === "error")
|
|
183
|
+
throw result.error;
|
|
184
|
+
if (result.consumed !== bytes.length) {
|
|
185
|
+
throw new CborDecodeError(`${bytes.length - result.consumed} trailing byte(s) after a complete CBOR data item`);
|
|
186
|
+
}
|
|
187
|
+
return result.value;
|
|
188
|
+
}
|
|
189
|
+
/**
|
|
190
|
+
* Adapts {@link decodeCborItem} to `@ac-kit/format-core`'s `Decoder` contract,
|
|
191
|
+
* for decoding a CBOR sequence (RFC 8742) via `decodeAll` (a complete buffer)
|
|
192
|
+
* or `DecodeStream` (chunks arriving over time). Replaces a hand-rolled
|
|
193
|
+
* `CborSequenceDecoder` that reimplemented the same accumulate-and-dispatch
|
|
194
|
+
* loop `DecodeDriver` now provides generically.
|
|
195
|
+
*
|
|
196
|
+
* `error` results are recoverable whenever the reader made progress before
|
|
197
|
+
* rejecting the item: `consumed` bytes are dropped and the next item is tried,
|
|
198
|
+
* which is what lets one bad item in a sequence not end the whole stream. An
|
|
199
|
+
* error at offset zero is reported as `fatal`, since retrying from an unmoved
|
|
200
|
+
* position could not terminate.
|
|
201
|
+
*/
|
|
202
|
+
export class CborItemDecoder {
|
|
203
|
+
decode(view) {
|
|
204
|
+
const result = decodeCborItem(view, 0);
|
|
205
|
+
switch (result.status) {
|
|
206
|
+
case "item":
|
|
207
|
+
return {
|
|
208
|
+
status: "decoded",
|
|
209
|
+
value: result.value,
|
|
210
|
+
consumed: result.consumed,
|
|
211
|
+
};
|
|
212
|
+
case "error":
|
|
213
|
+
return result.consumed > 0
|
|
214
|
+
? {
|
|
215
|
+
status: "error",
|
|
216
|
+
error: result.error,
|
|
217
|
+
consumed: result.consumed,
|
|
218
|
+
}
|
|
219
|
+
: { status: "fatal", error: result.error };
|
|
220
|
+
default:
|
|
221
|
+
return { status: "incomplete" };
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
//# sourceMappingURL=decoder.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":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,CACxB,yDAAyD,CACzD,CAAC;IACH,CAAC;IACD,IAAI,MAAM,CAAC,MAAM,KAAK,OAAO;QAAE,MAAM,MAAM,CAAC,KAAK,CAAC;IAClD,IAAI,MAAM,CAAC,QAAQ,KAAK,KAAK,CAAC,MAAM,EAAE,CAAC;QACtC,MAAM,IAAI,eAAe,CACxB,GAAG,KAAK,CAAC,MAAM,GAAG,MAAM,CAAC,QAAQ,mDAAmD,CACpF,CAAC;IACH,CAAC;IACD,OAAO,MAAM,CAAC,KAAK,CAAC;AACrB,CAAC;AAED;;;;;;;;;;;;GAYG;AACH,MAAM,OAAO,eAAe;IAK3B,MAAM,CAAC,IAAgB;QACtB,MAAM,MAAM,GAAG,cAAc,CAAC,IAAI,EAAE,CAAC,CAAC,CAAC;QACvC,QAAQ,MAAM,CAAC,MAAM,EAAE,CAAC;YACvB,KAAK,MAAM;gBACV,OAAO;oBACN,MAAM,EAAE,SAAS;oBACjB,KAAK,EAAE,MAAM,CAAC,KAAK;oBACnB,QAAQ,EAAE,MAAM,CAAC,QAAQ;iBACzB,CAAC;YACH,KAAK,OAAO;gBACX,OAAO,MAAM,CAAC,QAAQ,GAAG,CAAC;oBACzB,CAAC,CAAC;wBACA,MAAM,EAAE,OAAO;wBACf,KAAK,EAAE,MAAM,CAAC,KAAK;wBACnB,QAAQ,EAAE,MAAM,CAAC,QAAQ;qBACzB;oBACF,CAAC,CAAC,EAAE,MAAM,EAAE,OAAO,EAAE,KAAK,EAAE,MAAM,CAAC,KAAK,EAAE,CAAC;YAC7C;gBACC,OAAO,EAAE,MAAM,EAAE,YAAY,EAAE,CAAC;QAClC,CAAC;IACF,CAAC;CACD"}
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import type { Encoder } from "@ac-kit/format-core";
|
|
2
|
+
import type { DataValue } from "../ast.js";
|
|
3
|
+
import { CborError } from "../errors.js";
|
|
4
|
+
/**
|
|
5
|
+
* A {@link DataValue} that cannot be represented by this encoder's supported
|
|
6
|
+
* subset.
|
|
7
|
+
*/
|
|
8
|
+
export declare class CborEncodeError extends CborError {
|
|
9
|
+
constructor(message: string);
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* Encodes a {@link DataValue} to CBOR bytes.
|
|
13
|
+
*
|
|
14
|
+
* Always uses definite-length encoding and always encodes `float` as
|
|
15
|
+
* double-precision (major type 7, additional information 27) — both valid per
|
|
16
|
+
* RFC 8949 but not necessarily the shortest possible encoding (§4.1's
|
|
17
|
+
* "preferred serialization" is not fully implemented; only argument widths are
|
|
18
|
+
* minimized). Integers outside `-2^64..2^64-1` and simple values 20-23 throw
|
|
19
|
+
* {@link CborEncodeError} rather than silently promoting to a bignum tag —
|
|
20
|
+
* construct a `{kind: "tag", tag: 2n | 3n, value: {kind: "bytes", ...}}` node
|
|
21
|
+
* directly if a bignum is needed.
|
|
22
|
+
*/
|
|
23
|
+
export declare function encodeCbor(value: DataValue): Uint8Array;
|
|
24
|
+
/**
|
|
25
|
+
* Encodes a {@link DataValue} to the chunks it is built from, without the final
|
|
26
|
+
* concatenation.
|
|
27
|
+
*
|
|
28
|
+
* Every nested item already appends its own chunk, so a caller writing to a
|
|
29
|
+
* vectored sink can hand these straight over instead of paying a copy to join
|
|
30
|
+
* them and another to split them again.
|
|
31
|
+
*/
|
|
32
|
+
export declare function encodeCborParts(value: DataValue): Uint8Array[];
|
|
33
|
+
/**
|
|
34
|
+
* The {@link encodeCbor} grammar behind `@ac-kit/format-core`'s `Encoder`
|
|
35
|
+
* contract, for driving an `EncodeStream` or pairing with
|
|
36
|
+
* {@link CborItemDecoder} as a `Codec`.
|
|
37
|
+
*
|
|
38
|
+
* Returns the item vectored, which is what the contract's `View | readonly
|
|
39
|
+
* View[]` return exists for.
|
|
40
|
+
*/
|
|
41
|
+
export declare function createCborItemEncoder(): Encoder<DataValue>;
|
|
42
|
+
//# sourceMappingURL=encoder.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"encoder.d.ts","sourceRoot":"","sources":["../../src/binary/encoder.ts"],"names":[],"mappings":"AAQA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,qBAAqB,CAAC;AAEnD,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,WAAW,CAAC;AAC3C,OAAO,EAAE,SAAS,EAAE,MAAM,cAAc,CAAC;AAEzC;;;GAGG;AACH,qBAAa,eAAgB,SAAQ,SAAS;IAC7C,YAAY,OAAO,EAAE,MAAM,EAG1B;CACD;AAwHD;;;;;;;;;;;GAWG;AACH,wBAAgB,UAAU,CAAC,KAAK,EAAE,SAAS,GAAG,UAAU,CAEvD;AAED;;;;;;;GAOG;AACH,wBAAgB,eAAe,CAAC,KAAK,EAAE,SAAS,GAAG,UAAU,EAAE,CAI9D;AAED;;;;;;;GAOG;AACH,wBAAgB,qBAAqB,IAAI,OAAO,CAAC,SAAS,CAAC,CAE1D"}
|
|
@@ -0,0 +1,162 @@
|
|
|
1
|
+
import { concatBytes, encodeTextUtf8, MASK_64N, setBigUint64Be, setFloat64Be, setUint32Be, } from "@ac-kit/core";
|
|
2
|
+
import { CborError } from "../errors.js";
|
|
3
|
+
/**
|
|
4
|
+
* A {@link DataValue} that cannot be represented by this encoder's supported
|
|
5
|
+
* subset.
|
|
6
|
+
*/
|
|
7
|
+
export class CborEncodeError extends CborError {
|
|
8
|
+
constructor(message) {
|
|
9
|
+
super(message);
|
|
10
|
+
this.name = "CborEncodeError";
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
const MAX_ARGUMENT = MASK_64N;
|
|
14
|
+
/**
|
|
15
|
+
* Encodes a CBOR head (initial byte + argument), always using the shortest form
|
|
16
|
+
* for `argument` (RFC 8949 §4.1 preferred serialization).
|
|
17
|
+
*/
|
|
18
|
+
function encodeHead(chunks, majorType, argument) {
|
|
19
|
+
if (argument < 0n || argument > MAX_ARGUMENT) {
|
|
20
|
+
throw new CborEncodeError(`Argument ${argument} is outside CBOR's representable range (0..2^64-1)`);
|
|
21
|
+
}
|
|
22
|
+
const prefix = majorType << 5;
|
|
23
|
+
if (argument < 24n) {
|
|
24
|
+
chunks.push(Uint8Array.of(prefix | Number(argument)));
|
|
25
|
+
}
|
|
26
|
+
else if (argument < 256n) {
|
|
27
|
+
chunks.push(Uint8Array.of(prefix | 24, Number(argument)));
|
|
28
|
+
}
|
|
29
|
+
else if (argument < 65536n) {
|
|
30
|
+
const bytes = new Uint8Array(3);
|
|
31
|
+
bytes[0] = prefix | 25;
|
|
32
|
+
const value = Number(argument);
|
|
33
|
+
bytes[1] = (value >>> 8) & 0xff;
|
|
34
|
+
bytes[2] = value & 0xff;
|
|
35
|
+
chunks.push(bytes);
|
|
36
|
+
}
|
|
37
|
+
else if (argument < 4294967296n) {
|
|
38
|
+
const bytes = new Uint8Array(5);
|
|
39
|
+
bytes[0] = prefix | 26;
|
|
40
|
+
setUint32Be(bytes, 1, Number(argument));
|
|
41
|
+
chunks.push(bytes);
|
|
42
|
+
}
|
|
43
|
+
else {
|
|
44
|
+
const bytes = new Uint8Array(9);
|
|
45
|
+
bytes[0] = prefix | 27;
|
|
46
|
+
setBigUint64Be(bytes, 1, argument);
|
|
47
|
+
chunks.push(bytes);
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
function encodeItem(chunks, value) {
|
|
51
|
+
switch (value.kind) {
|
|
52
|
+
case "int": {
|
|
53
|
+
if (value.value >= 0n) {
|
|
54
|
+
encodeHead(chunks, 0, value.value);
|
|
55
|
+
}
|
|
56
|
+
else {
|
|
57
|
+
encodeHead(chunks, 1, -1n - value.value);
|
|
58
|
+
}
|
|
59
|
+
return;
|
|
60
|
+
}
|
|
61
|
+
case "float": {
|
|
62
|
+
const bytes = new Uint8Array(9);
|
|
63
|
+
bytes[0] = (7 << 5) | 27;
|
|
64
|
+
setFloat64Be(bytes, 1, value.value);
|
|
65
|
+
chunks.push(bytes);
|
|
66
|
+
return;
|
|
67
|
+
}
|
|
68
|
+
case "bytes": {
|
|
69
|
+
encodeHead(chunks, 2, BigInt(value.value.length));
|
|
70
|
+
chunks.push(value.value);
|
|
71
|
+
return;
|
|
72
|
+
}
|
|
73
|
+
case "text": {
|
|
74
|
+
const encoded = encodeTextUtf8(value.value);
|
|
75
|
+
encodeHead(chunks, 3, BigInt(encoded.length));
|
|
76
|
+
chunks.push(encoded);
|
|
77
|
+
return;
|
|
78
|
+
}
|
|
79
|
+
case "array": {
|
|
80
|
+
encodeHead(chunks, 4, BigInt(value.items.length));
|
|
81
|
+
for (const item of value.items)
|
|
82
|
+
encodeItem(chunks, item);
|
|
83
|
+
return;
|
|
84
|
+
}
|
|
85
|
+
case "map": {
|
|
86
|
+
encodeHead(chunks, 5, BigInt(value.entries.length));
|
|
87
|
+
for (const [key, entryValue] of value.entries) {
|
|
88
|
+
encodeItem(chunks, key);
|
|
89
|
+
encodeItem(chunks, entryValue);
|
|
90
|
+
}
|
|
91
|
+
return;
|
|
92
|
+
}
|
|
93
|
+
case "tag": {
|
|
94
|
+
encodeHead(chunks, 6, value.tag);
|
|
95
|
+
encodeItem(chunks, value.value);
|
|
96
|
+
return;
|
|
97
|
+
}
|
|
98
|
+
case "bool":
|
|
99
|
+
chunks.push(Uint8Array.of((7 << 5) | (value.value ? 21 : 20)));
|
|
100
|
+
return;
|
|
101
|
+
case "null":
|
|
102
|
+
chunks.push(Uint8Array.of((7 << 5) | 22));
|
|
103
|
+
return;
|
|
104
|
+
case "undefined":
|
|
105
|
+
chunks.push(Uint8Array.of((7 << 5) | 23));
|
|
106
|
+
return;
|
|
107
|
+
case "simple": {
|
|
108
|
+
if (value.value < 0 ||
|
|
109
|
+
value.value > 255 ||
|
|
110
|
+
(value.value >= 20 && value.value <= 23)) {
|
|
111
|
+
throw new CborEncodeError(`Simple value ${value.value} is out of range or reserved for a named value (false/true/null/undefined)`);
|
|
112
|
+
}
|
|
113
|
+
if (value.value < 24) {
|
|
114
|
+
chunks.push(Uint8Array.of((7 << 5) | value.value));
|
|
115
|
+
}
|
|
116
|
+
else {
|
|
117
|
+
chunks.push(Uint8Array.of((7 << 5) | 24, value.value));
|
|
118
|
+
}
|
|
119
|
+
return;
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
/**
|
|
124
|
+
* Encodes a {@link DataValue} to CBOR bytes.
|
|
125
|
+
*
|
|
126
|
+
* Always uses definite-length encoding and always encodes `float` as
|
|
127
|
+
* double-precision (major type 7, additional information 27) — both valid per
|
|
128
|
+
* RFC 8949 but not necessarily the shortest possible encoding (§4.1's
|
|
129
|
+
* "preferred serialization" is not fully implemented; only argument widths are
|
|
130
|
+
* minimized). Integers outside `-2^64..2^64-1` and simple values 20-23 throw
|
|
131
|
+
* {@link CborEncodeError} rather than silently promoting to a bignum tag —
|
|
132
|
+
* construct a `{kind: "tag", tag: 2n | 3n, value: {kind: "bytes", ...}}` node
|
|
133
|
+
* directly if a bignum is needed.
|
|
134
|
+
*/
|
|
135
|
+
export function encodeCbor(value) {
|
|
136
|
+
return concatBytes(encodeCborParts(value));
|
|
137
|
+
}
|
|
138
|
+
/**
|
|
139
|
+
* Encodes a {@link DataValue} to the chunks it is built from, without the final
|
|
140
|
+
* concatenation.
|
|
141
|
+
*
|
|
142
|
+
* Every nested item already appends its own chunk, so a caller writing to a
|
|
143
|
+
* vectored sink can hand these straight over instead of paying a copy to join
|
|
144
|
+
* them and another to split them again.
|
|
145
|
+
*/
|
|
146
|
+
export function encodeCborParts(value) {
|
|
147
|
+
const chunks = [];
|
|
148
|
+
encodeItem(chunks, value);
|
|
149
|
+
return chunks;
|
|
150
|
+
}
|
|
151
|
+
/**
|
|
152
|
+
* The {@link encodeCbor} grammar behind `@ac-kit/format-core`'s `Encoder`
|
|
153
|
+
* contract, for driving an `EncodeStream` or pairing with
|
|
154
|
+
* {@link CborItemDecoder} as a `Codec`.
|
|
155
|
+
*
|
|
156
|
+
* Returns the item vectored, which is what the contract's `View | readonly
|
|
157
|
+
* View[]` return exists for.
|
|
158
|
+
*/
|
|
159
|
+
export function createCborItemEncoder() {
|
|
160
|
+
return { encode: encodeCborParts };
|
|
161
|
+
}
|
|
162
|
+
//# sourceMappingURL=encoder.js.map
|