@cbortech/cbor 0.23.0 → 0.23.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/README.ja.md +6 -3
- package/README.md +7 -4
- package/dist/ast/CborArray.d.ts +17 -0
- package/dist/ast/CborBignum.d.ts +36 -0
- package/dist/ast/CborByteString.d.ts +20 -0
- package/dist/ast/CborEllipsis.d.ts +11 -0
- package/dist/ast/CborEmbeddedCBOR.d.ts +22 -0
- package/dist/ast/CborFloat.d.ts +27 -0
- package/dist/ast/CborIndefiniteByteString.d.ts +13 -0
- package/dist/ast/CborIndefiniteTextString.d.ts +13 -0
- package/dist/ast/CborItem.d.ts +91 -0
- package/dist/ast/CborMap.d.ts +17 -0
- package/dist/ast/CborNint.d.ts +27 -0
- package/dist/ast/CborSimple.d.ts +22 -0
- package/dist/ast/CborTag.d.ts +16 -0
- package/dist/ast/CborTextString.d.ts +15 -0
- package/dist/ast/CborUint.d.ts +14 -0
- package/dist/ast/CborUnresolvedAppExt.d.ts +16 -0
- package/dist/ast/index.cjs +1 -0
- package/dist/ast/index.d.ts +15 -0
- package/dist/ast/index.js +2 -0
- package/dist/cbor/constants.d.ts +14 -0
- package/dist/cbor/decoder.d.ts +11 -0
- package/dist/cbor/encode.d.ts +35 -0
- package/dist/cbor/encoder.d.ts +7 -0
- package/dist/cbor.d.ts +129 -0
- package/dist/edn/parser.d.ts +7 -0
- package/dist/edn/serialize-utils.d.ts +48 -0
- package/dist/edn/serializer.d.ts +7 -0
- package/dist/edn/tokenizer.d.ts +176 -0
- package/dist/extensions/bignum.d.ts +3 -0
- package/dist/extensions/builtins.d.ts +2 -0
- package/dist/extensions/cbordata.d.ts +4 -0
- package/dist/extensions/cri.d.ts +32 -0
- package/dist/extensions/dt.d.ts +109 -0
- package/dist/extensions/hash.d.ts +17 -0
- package/dist/extensions/ip.d.ts +41 -0
- package/dist/extensions/types.d.ts +70 -0
- package/dist/index.cjs +2 -31
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +8 -695
- package/dist/index.js +30 -3913
- package/dist/index.js.map +1 -1
- package/dist/js/fromJS.d.ts +25 -0
- package/dist/js/toJS.d.ts +6 -0
- package/dist/mapEntries-C73nWM8o.cjs +31 -0
- package/dist/mapEntries-C73nWM8o.cjs.map +1 -0
- package/dist/mapEntries-CSjvgq1X.js +3887 -0
- package/dist/mapEntries-CSjvgq1X.js.map +1 -0
- package/dist/mapEntries.d.ts +3 -0
- package/dist/simple.d.ts +25 -0
- package/dist/tag.d.ts +45 -0
- package/dist/types.d.ts +352 -0
- package/dist/utils/float16.d.ts +38 -0
- package/dist/utils/hexfloat.d.ts +27 -0
- package/dist/utils/ip.d.ts +9 -0
- package/package.json +12 -1
package/dist/simple.d.ts
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Wrapper for unrecognised CBOR simple values (0–255, excluding false/true/
|
|
3
|
+
* null/undefined). Returned by CborSimple.toJS() so that fromJS() can
|
|
4
|
+
* reconstruct the original CborSimple node and preserve the round-trip.
|
|
5
|
+
*
|
|
6
|
+
* Also serves as a namespace for simple-value utilities.
|
|
7
|
+
*
|
|
8
|
+
* @example
|
|
9
|
+
* const v = CBOR.fromEDN('simple(19)').toJS();
|
|
10
|
+
* Simple.is(v); // true
|
|
11
|
+
* Simple.get(v); // 19
|
|
12
|
+
*
|
|
13
|
+
* const node = CBOR.fromJS(new Simple(19));
|
|
14
|
+
* node.toEDN(); // "simple(19)"
|
|
15
|
+
*/
|
|
16
|
+
export declare class Simple {
|
|
17
|
+
readonly value: number;
|
|
18
|
+
constructor(value: number);
|
|
19
|
+
valueOf(): number;
|
|
20
|
+
toJSON(): never;
|
|
21
|
+
/** Return true if value is a Simple instance. */
|
|
22
|
+
static is(value: unknown): value is Simple;
|
|
23
|
+
/** Return the simple number if value is a Simple instance, otherwise undefined. */
|
|
24
|
+
static get(value: unknown): number | undefined;
|
|
25
|
+
}
|
package/dist/tag.d.ts
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
export declare const CBOR_TAG: unique symbol;
|
|
2
|
+
export declare class Null {
|
|
3
|
+
valueOf(): null;
|
|
4
|
+
toJSON(): null;
|
|
5
|
+
}
|
|
6
|
+
export declare class Undefined {
|
|
7
|
+
valueOf(): undefined;
|
|
8
|
+
toJSON(): undefined;
|
|
9
|
+
}
|
|
10
|
+
/** @internal */
|
|
11
|
+
export declare function getCborTag(value: unknown): bigint | undefined;
|
|
12
|
+
/** @internal */
|
|
13
|
+
export declare function setCborTag(value: unknown, tag: bigint): object;
|
|
14
|
+
/** @internal */
|
|
15
|
+
export declare function removeCborTag(value: unknown): unknown;
|
|
16
|
+
/** @internal */
|
|
17
|
+
export declare function getCborTaggedValue(value: unknown): unknown;
|
|
18
|
+
/**
|
|
19
|
+
* Namespace for CBOR tag annotation utilities.
|
|
20
|
+
*
|
|
21
|
+
* @example
|
|
22
|
+
* const v = CBOR.fromEDN('42("hello")').toJS();
|
|
23
|
+
* Tag.get(v); // 42n
|
|
24
|
+
* Tag.getValue(v); // "hello"
|
|
25
|
+
*
|
|
26
|
+
* const tagged = Tag.set([1, 2, 3], 100n);
|
|
27
|
+
* Tag.remove(tagged); // [1, 2, 3]
|
|
28
|
+
*/
|
|
29
|
+
export declare class Tag {
|
|
30
|
+
private constructor();
|
|
31
|
+
/** Unique symbol used to attach a CBOR tag number to a JS value. */
|
|
32
|
+
static readonly symbol: typeof CBOR_TAG;
|
|
33
|
+
/** Wrapper class for tagged `null` values. */
|
|
34
|
+
static readonly Null: typeof Null;
|
|
35
|
+
/** Wrapper class for tagged `undefined` values. */
|
|
36
|
+
static readonly Undefined: typeof Undefined;
|
|
37
|
+
/** Return the CBOR tag number attached to `value`, or `undefined` if none. */
|
|
38
|
+
static get(value: unknown): bigint | undefined;
|
|
39
|
+
/** Attach a CBOR tag number to `value` and return the annotated value. */
|
|
40
|
+
static set(value: unknown, tag: bigint): object;
|
|
41
|
+
/** Remove the `[Tag.symbol]` annotation from `value` and return the plain value. */
|
|
42
|
+
static remove(value: unknown): unknown;
|
|
43
|
+
/** Return the underlying plain JS value held inside a tagged wrapper. */
|
|
44
|
+
static getValue(value: unknown): unknown;
|
|
45
|
+
}
|
package/dist/types.d.ts
ADDED
|
@@ -0,0 +1,352 @@
|
|
|
1
|
+
import { CborExtension } from './extensions/types';
|
|
2
|
+
/**
|
|
3
|
+
* Shared option types and plugin interfaces.
|
|
4
|
+
*/
|
|
5
|
+
/**
|
|
6
|
+
* Sentinel returned from a replacer or reviver to omit the key/element from
|
|
7
|
+
* the output. Use this instead of returning `undefined` when `undefinedOmits`
|
|
8
|
+
* is `false` (the default) and you need to drop a specific entry.
|
|
9
|
+
*
|
|
10
|
+
* Accessible as `CBOR.OMIT` on the main class.
|
|
11
|
+
*/
|
|
12
|
+
export declare const CBOR_OMIT: unique symbol;
|
|
13
|
+
export type { CborExtension } from './extensions/types';
|
|
14
|
+
export interface ToHexDumpOptions {
|
|
15
|
+
/**
|
|
16
|
+
* Indentation per nesting level.
|
|
17
|
+
* - `number`: number of spaces (e.g. `3` → `" "`)
|
|
18
|
+
* - `string`: literal indent string (e.g. `'\t'`)
|
|
19
|
+
* @default 3
|
|
20
|
+
*/
|
|
21
|
+
indent?: number | string;
|
|
22
|
+
/** Comment marker used in the hex dump. Default: `'--'` */
|
|
23
|
+
commentStyle?: '--' | '#';
|
|
24
|
+
}
|
|
25
|
+
export interface ToJSOptions {
|
|
26
|
+
/**
|
|
27
|
+
* How to represent CBOR integer values (major type 0 / 1) in JavaScript.
|
|
28
|
+
* - `'auto'`: `number` when the value is within the safe integer range
|
|
29
|
+
* (±`Number.MAX_SAFE_INTEGER`), `bigint` otherwise.
|
|
30
|
+
* - `'number'`: always `number` (precision may be lost for large values).
|
|
31
|
+
* - `'bigint'`: always `bigint`.
|
|
32
|
+
* @default 'auto'
|
|
33
|
+
*/
|
|
34
|
+
integerAs?: 'auto' | 'number' | 'bigint';
|
|
35
|
+
/**
|
|
36
|
+
* How to represent CBOR map values when converting to JavaScript.
|
|
37
|
+
* - `'auto'`: text-string-only keys → `Record<string, unknown>`,
|
|
38
|
+
* other key types → `Map<unknown, unknown>`.
|
|
39
|
+
* Duplicate keys are silently overwritten (last value wins).
|
|
40
|
+
* - `'object'`: always `Record<string, unknown>` — non-string keys are
|
|
41
|
+
* converted via `String()`. Duplicate keys are overwritten (last wins).
|
|
42
|
+
* - `'entries'`: always `MapEntries` (a typed `Array` subclass) — preserves all
|
|
43
|
+
* entries including duplicate keys (§2.6.2 of draft-ietf-cbor-edn-literals-20).
|
|
44
|
+
* `fromJS()` recognises `MapEntries` instances and converts them back to `CborMap`.
|
|
45
|
+
* @default 'auto'
|
|
46
|
+
*/
|
|
47
|
+
mapAs?: 'auto' | 'object' | 'entries';
|
|
48
|
+
/**
|
|
49
|
+
* Post-conversion reviver function, applied bottom-up after the CBOR value
|
|
50
|
+
* has been converted to JavaScript.
|
|
51
|
+
*
|
|
52
|
+
* Called for every key/value pair — including map entries with non-string
|
|
53
|
+
* keys — and finally for the root value with key `''`.
|
|
54
|
+
* Return `CBOR.OMIT` to remove the entry from its parent container.
|
|
55
|
+
* When `undefinedOmits` is `true`, returning `undefined` also removes the
|
|
56
|
+
* entry (matching `JSON.parse` behavior).
|
|
57
|
+
*
|
|
58
|
+
* Note: this option is honoured by `CborItem.toJS()` and the `CBOR.*`
|
|
59
|
+
* shortcut methods. Calling `_toJS()` directly bypasses it.
|
|
60
|
+
*/
|
|
61
|
+
reviver?: (this: unknown, key: unknown, value: unknown) => unknown;
|
|
62
|
+
/**
|
|
63
|
+
* When `true`, a reviver returning `undefined` removes the entry from its
|
|
64
|
+
* parent container, matching `JSON.parse` behavior.
|
|
65
|
+
* When `false` (default), only `CBOR.OMIT` removes an entry; returning
|
|
66
|
+
* `undefined` keeps the entry as CBOR `undefined` (simple 23).
|
|
67
|
+
* @default false
|
|
68
|
+
*/
|
|
69
|
+
undefinedOmits?: boolean;
|
|
70
|
+
}
|
|
71
|
+
export interface ToCBOROptions {
|
|
72
|
+
}
|
|
73
|
+
export interface FromCBOROptions {
|
|
74
|
+
/**
|
|
75
|
+
* Byte offset within the supplied input at which CBOR decoding starts.
|
|
76
|
+
* Useful for reading one item from a CBOR Sequence.
|
|
77
|
+
*
|
|
78
|
+
* @default 0
|
|
79
|
+
*/
|
|
80
|
+
offset?: number;
|
|
81
|
+
/**
|
|
82
|
+
* Allow bytes after the decoded item.
|
|
83
|
+
*
|
|
84
|
+
* When `false`, decoding still requires the item to consume the remaining
|
|
85
|
+
* input, preserving the historical single-item behaviour. Set this to `true`
|
|
86
|
+
* when using `CborItem.end` to continue decoding a CBOR Sequence.
|
|
87
|
+
*
|
|
88
|
+
* @example
|
|
89
|
+
* // Read two items from a CBOR Sequence, validating that the second is last.
|
|
90
|
+
* const first = CBOR.fromCBOR(bytes, { allowTrailing: true });
|
|
91
|
+
* const second = CBOR.fromCBOR(bytes, { offset: first.end });
|
|
92
|
+
*
|
|
93
|
+
* @default false
|
|
94
|
+
*/
|
|
95
|
+
allowTrailing?: boolean;
|
|
96
|
+
/**
|
|
97
|
+
* Extension plugins applied during CBOR decoding.
|
|
98
|
+
* Extensions with `parseTag()` are invoked when a tagged item is
|
|
99
|
+
* encountered; returning a non-`undefined` value replaces the default
|
|
100
|
+
* `CborTag` node.
|
|
101
|
+
*/
|
|
102
|
+
extensions?: CborExtension[];
|
|
103
|
+
}
|
|
104
|
+
/**
|
|
105
|
+
* Options for parsing an annotated hex dump.
|
|
106
|
+
*/
|
|
107
|
+
export interface FromHexDumpOptions {
|
|
108
|
+
/**
|
|
109
|
+
* Extension plugins applied during CBOR decoding.
|
|
110
|
+
* Extensions with `parseTag()` are invoked when a tagged item is encountered;
|
|
111
|
+
* returning a non-`undefined` value replaces the default `CborTag` node.
|
|
112
|
+
*/
|
|
113
|
+
extensions?: CborExtension[];
|
|
114
|
+
}
|
|
115
|
+
export interface FromEDNOptions {
|
|
116
|
+
/**
|
|
117
|
+
* Character offset within the supplied text at which CBOR-EDN parsing starts.
|
|
118
|
+
* Leading whitespace/comments at or after this offset are skipped as usual.
|
|
119
|
+
*
|
|
120
|
+
* @default 0
|
|
121
|
+
*/
|
|
122
|
+
offset?: number;
|
|
123
|
+
/**
|
|
124
|
+
* Allow tokens after the parsed item.
|
|
125
|
+
*
|
|
126
|
+
* When `false`, parsing still requires the item to consume the remaining
|
|
127
|
+
* input, preserving the historical single-item behaviour. Set this to `true`
|
|
128
|
+
* when using `CborItem.end` to continue parsing a CBOR-EDN sequence.
|
|
129
|
+
* Top-level comma separators are not skipped by `fromEDN()` itself; handle
|
|
130
|
+
* them in sequence-level code before passing the next `offset`. For example,
|
|
131
|
+
* after parsing `1, 2`, the first item's `end` points just before the comma;
|
|
132
|
+
* advance past that comma before parsing the next item.
|
|
133
|
+
*
|
|
134
|
+
* @example
|
|
135
|
+
* // Read two whitespace-separated items, validating that the second is last.
|
|
136
|
+
* const first = CBOR.fromEDN(text, { allowTrailing: true });
|
|
137
|
+
* const second = CBOR.fromEDN(text, { offset: first.end });
|
|
138
|
+
*
|
|
139
|
+
* @default false
|
|
140
|
+
*/
|
|
141
|
+
allowTrailing?: boolean;
|
|
142
|
+
/**
|
|
143
|
+
* Extension plugins for EDN parsing.
|
|
144
|
+
* Each extension declares which app-string prefixes (and, in future, tag
|
|
145
|
+
* numbers) it handles via `appStringPrefixes` / `tagNumbers`, and provides
|
|
146
|
+
* callback methods that return `CborItem`-subclassed objects controlling
|
|
147
|
+
* subsequent serialisation.
|
|
148
|
+
*
|
|
149
|
+
* User-supplied extensions take priority over the built-in `dt`/`DT`
|
|
150
|
+
* extension for the same prefix.
|
|
151
|
+
*/
|
|
152
|
+
extensions?: CborExtension[];
|
|
153
|
+
/**
|
|
154
|
+
* How to handle unrecognised application-extension identifiers
|
|
155
|
+
* (§4.1 draft-ietf-cbor-edn-literals-20).
|
|
156
|
+
*
|
|
157
|
+
* - `'cpa999'`: wrap the literal in a `CPA999` tag
|
|
158
|
+
* (`CborUnresolvedAppExt`) instead of failing. The resulting node
|
|
159
|
+
* round-trips through `toEDN()` back to the original notation.
|
|
160
|
+
* - `'error'`: throw `SyntaxError` for unknown prefixes.
|
|
161
|
+
* @default 'cpa999'
|
|
162
|
+
*/
|
|
163
|
+
unresolvedExtension?: 'cpa999' | 'error';
|
|
164
|
+
/**
|
|
165
|
+
* When `true`, byte-string chunks in text string concatenation
|
|
166
|
+
* (`"a" + h'...'`) that are not valid UTF-8 are decoded with the Unicode
|
|
167
|
+
* replacement character (U+FFFD) instead of throwing a `SyntaxError`.
|
|
168
|
+
*
|
|
169
|
+
* The CBOR text string type (RFC 8949 §3.1) requires valid UTF-8;
|
|
170
|
+
* enabling this option produces non-conformant output and should only be
|
|
171
|
+
* used when interoperating with lenient producers.
|
|
172
|
+
*
|
|
173
|
+
* @default false
|
|
174
|
+
*/
|
|
175
|
+
allowInvalidUtf8?: boolean;
|
|
176
|
+
/**
|
|
177
|
+
* Preserve comments found between CBOR-EDN values and attach them to the AST.
|
|
178
|
+
*
|
|
179
|
+
* Comments are metadata only: they are ignored by CBOR binary encoding and
|
|
180
|
+
* JavaScript conversion. Use together with `ToEDNOptions.preserveComments`
|
|
181
|
+
* to include them when formatting back to EDN.
|
|
182
|
+
*
|
|
183
|
+
* @default false
|
|
184
|
+
*/
|
|
185
|
+
preserveComments?: boolean;
|
|
186
|
+
}
|
|
187
|
+
export interface FromJSOptions {
|
|
188
|
+
/**
|
|
189
|
+
* Extension plugins applied during `fromJS()`.
|
|
190
|
+
* Extensions with `fromJS()` are given first chance to convert each value.
|
|
191
|
+
*/
|
|
192
|
+
extensions?: CborExtension[];
|
|
193
|
+
/**
|
|
194
|
+
* How to encode integer-valued JS `number`s.
|
|
195
|
+
* - `'int'`: encode as CborUint / CborNint
|
|
196
|
+
* - `'float'`: always encode as CborFloat
|
|
197
|
+
* @default 'int'
|
|
198
|
+
*/
|
|
199
|
+
encodeIntegerAs?: 'int' | 'float';
|
|
200
|
+
/**
|
|
201
|
+
* How to encode `Uint8Array` values.
|
|
202
|
+
* - `'bytes'`: encode as CborByteString
|
|
203
|
+
* - `'array'`: encode as CborArray of CborUint
|
|
204
|
+
* @default 'bytes'
|
|
205
|
+
*/
|
|
206
|
+
uint8ArrayAs?: 'bytes' | 'array';
|
|
207
|
+
/**
|
|
208
|
+
* Pre-encoding replacer function or key allowlist, applied before the
|
|
209
|
+
* JavaScript value is converted to a CBOR AST node.
|
|
210
|
+
*
|
|
211
|
+
* - Function: called for every key/value pair (including `MapEntries`
|
|
212
|
+
* entries with non-string keys). Return `CBOR.OMIT` to remove the entry.
|
|
213
|
+
* When `undefinedOmits` is `true`, returning `undefined` also removes it.
|
|
214
|
+
* - Array of strings/numbers: allowlist of object keys to include.
|
|
215
|
+
* `MapEntries` entries retain all entries; their values are recursively
|
|
216
|
+
* filtered.
|
|
217
|
+
*
|
|
218
|
+
* Note: this option is honoured by `fromJS()` and the `CBOR.*` shortcut
|
|
219
|
+
* methods.
|
|
220
|
+
*/
|
|
221
|
+
replacer?: ((this: unknown, key: unknown, value: unknown) => unknown) | (string | number)[];
|
|
222
|
+
/**
|
|
223
|
+
* When `true`, a replacer returning `undefined` removes the entry from the
|
|
224
|
+
* output, matching `JSON.stringify` behavior.
|
|
225
|
+
* When `false` (default), only `CBOR.OMIT` removes an entry; returning
|
|
226
|
+
* `undefined` keeps the entry as CBOR `undefined` (simple 23).
|
|
227
|
+
* @default false
|
|
228
|
+
*/
|
|
229
|
+
undefinedOmits?: boolean;
|
|
230
|
+
}
|
|
231
|
+
export interface ToEDNOptions {
|
|
232
|
+
/**
|
|
233
|
+
* Indentation for pretty-printing.
|
|
234
|
+
* - `number`: number of spaces
|
|
235
|
+
* - `string`: literal indent string (e.g. `'\t'`)
|
|
236
|
+
* - omit for single-line output
|
|
237
|
+
*/
|
|
238
|
+
indent?: number | string;
|
|
239
|
+
/**
|
|
240
|
+
* Emit comments previously captured by `FromEDNOptions.preserveComments`.
|
|
241
|
+
*
|
|
242
|
+
* When enabled for containers, comment-bearing arrays/maps are emitted in
|
|
243
|
+
* multi-line form even if `indent` is omitted.
|
|
244
|
+
*
|
|
245
|
+
* @default false
|
|
246
|
+
*/
|
|
247
|
+
preserveComments?: boolean;
|
|
248
|
+
/**
|
|
249
|
+
* Re-emit byte string literals parsed from EDN using their original source
|
|
250
|
+
* text when available.
|
|
251
|
+
*
|
|
252
|
+
* This preserves the spelling and interior layout of non-concatenated
|
|
253
|
+
* `h'...'`, `b64'...'`, `b32'...'`, `h32'...'`, raw-backtick byte strings,
|
|
254
|
+
* and single-quoted byte strings, including comments inside those literals.
|
|
255
|
+
* Byte strings produced by `+` concatenation are normalised as usual.
|
|
256
|
+
*
|
|
257
|
+
* When enabled, this takes precedence over `bstrEncoding` and `sqstr` for
|
|
258
|
+
* byte strings that carry original EDN source text.
|
|
259
|
+
*
|
|
260
|
+
* @default false
|
|
261
|
+
*/
|
|
262
|
+
preserveByteString?: boolean;
|
|
263
|
+
/**
|
|
264
|
+
* Whether to emit commas between array/map elements.
|
|
265
|
+
* - `'comma'`: emit commas (`[1, 2, 3]`)
|
|
266
|
+
* - `'none'`: omit commas, use spaces only (`[1 2 3]`)
|
|
267
|
+
* - `'trailing'`: emit commas including a trailing comma after the last element
|
|
268
|
+
* @default 'comma'
|
|
269
|
+
*/
|
|
270
|
+
commas?: 'comma' | 'none' | 'trailing';
|
|
271
|
+
/**
|
|
272
|
+
/**
|
|
273
|
+
* Fallback binary encoding for byte string literals when sqstr is not applicable.
|
|
274
|
+
* - `'hex'`: `h'...'`
|
|
275
|
+
* - `'base64'`: `b64'...'`
|
|
276
|
+
* - `'base64url'`: `b64'...'` (base64url alphabet)
|
|
277
|
+
* - `'base32'`: `b32'...'`
|
|
278
|
+
* - `'base32hex'`: `h32'...'`
|
|
279
|
+
* @default 'hex'
|
|
280
|
+
*/
|
|
281
|
+
bstrEncoding?: 'hex' | 'base64' | 'base64url' | 'base32' | 'base32hex';
|
|
282
|
+
/**
|
|
283
|
+
* Whether to prefer single-quoted string form (`sqstr`) for byte strings.
|
|
284
|
+
* - `'printable-string'`: emit `'...'` when the bytes are valid UTF-8 and
|
|
285
|
+
* contain no control characters; fall back to `bstrEncoding` otherwise.
|
|
286
|
+
* - `'string'`: emit `'...'` when the bytes are valid UTF-8;
|
|
287
|
+
* fall back to `bstrEncoding` otherwise.
|
|
288
|
+
* - `'none'`: never emit sqstr; always use `bstrEncoding`.
|
|
289
|
+
* @default 'printable-string'
|
|
290
|
+
*/
|
|
291
|
+
sqstr?: 'printable-string' | 'string' | 'none';
|
|
292
|
+
/**
|
|
293
|
+
* Whether to use application-string / app-sequence notation for built-in
|
|
294
|
+
* extensions (e.g. `dt'...'`, `DT'...'`, `ip'...'`, `IP'...'`).
|
|
295
|
+
* - `true`: emit extension notation (`DT'2023-01-01T12:00:00Z'`)
|
|
296
|
+
* - `false`: emit raw CBOR notation (`1(-14159024)`, `52(h'c000022a')`)
|
|
297
|
+
* @default true
|
|
298
|
+
*/
|
|
299
|
+
appStrings?: boolean;
|
|
300
|
+
/**
|
|
301
|
+
* Numeric format for integer values in EDN output.
|
|
302
|
+
* - `'decimal'`: standard decimal notation (e.g. `42`, `-14159024`)
|
|
303
|
+
* - `'hex'`: hexadecimal notation (e.g. `0x2a`, `-0xd83130`)
|
|
304
|
+
* - `'octal'`: octal notation (e.g. `0o52`, `-0o67061560`)
|
|
305
|
+
* - `'binary'`: binary notation (e.g. `0b101010`, `-0b110110000011000100110000`)
|
|
306
|
+
* @default 'decimal'
|
|
307
|
+
*/
|
|
308
|
+
intFormat?: 'decimal' | 'hex' | 'octal' | 'binary';
|
|
309
|
+
/**
|
|
310
|
+
* Numeric format for floating-point values in EDN output.
|
|
311
|
+
* - `'decimal'`: standard decimal notation (e.g. `1.5`, `145544.0_3`)
|
|
312
|
+
* - `'hex'`: C99-style hex float notation (e.g. `0x1.8p+0`, `0x1.1c54p+17_3`)
|
|
313
|
+
* @default 'decimal'
|
|
314
|
+
*/
|
|
315
|
+
floatFormat?: 'decimal' | 'hex';
|
|
316
|
+
/**
|
|
317
|
+
* Split long text strings using EDN string concatenation syntax (`"a" + "b"`).
|
|
318
|
+
* Only effective when `indent` is specified.
|
|
319
|
+
*
|
|
320
|
+
* - `'newline'`: split at newline characters
|
|
321
|
+
* - `'cboredn'`: split according to CBOR-EDN structure when the string content
|
|
322
|
+
* is parseable as CBOR-EDN (JSON superset)
|
|
323
|
+
*
|
|
324
|
+
* When both are specified, `'cboredn'` is tried first; `'newline'` is used
|
|
325
|
+
* only when the string content is not parseable as CBOR-EDN.
|
|
326
|
+
*/
|
|
327
|
+
textStringFormat?: ('newline' | 'cboredn')[];
|
|
328
|
+
}
|
|
329
|
+
export interface CborComment {
|
|
330
|
+
kind: 'line' | 'block';
|
|
331
|
+
marker: '#' | '//' | '/*' | '/';
|
|
332
|
+
text: string;
|
|
333
|
+
start: number;
|
|
334
|
+
end: number;
|
|
335
|
+
line: number;
|
|
336
|
+
col: number;
|
|
337
|
+
}
|
|
338
|
+
export interface CborComments {
|
|
339
|
+
leading?: CborComment[];
|
|
340
|
+
trailing?: CborComment[];
|
|
341
|
+
dangling?: CborComment[];
|
|
342
|
+
}
|
|
343
|
+
/**
|
|
344
|
+
* Combined options for the `CBOR` constructor.
|
|
345
|
+
*
|
|
346
|
+
* These defaults are applied to every subsequent method call on the instance.
|
|
347
|
+
* Per-call options always take precedence over these defaults.
|
|
348
|
+
*
|
|
349
|
+
* Note: `encodeIntegerAs` (from {@link FromJSOptions}) and `integerAs` (from
|
|
350
|
+
* {@link ToJSOptions}) are distinct fields and do not conflict.
|
|
351
|
+
*/
|
|
352
|
+
export type CBOROptions = FromEDNOptions & FromJSOptions & ToCBOROptions & ToEDNOptions & ToJSOptions & ToHexDumpOptions;
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Float16 (IEEE 754 binary16) encode/decode utilities.
|
|
3
|
+
*
|
|
4
|
+
* Uses native DataView.getFloat16 / setFloat16 when both are available,
|
|
5
|
+
* and falls back to a manual bit-manipulation implementation otherwise.
|
|
6
|
+
*
|
|
7
|
+
* binary16 format:
|
|
8
|
+
* bit 15 : sign
|
|
9
|
+
* bits 14-10: exponent (5 bits, bias = 15)
|
|
10
|
+
* bits 9- 0: mantissa (10 bits)
|
|
11
|
+
*/
|
|
12
|
+
export declare const hasNativeFloat16: boolean;
|
|
13
|
+
/**
|
|
14
|
+
* float64 → float16 bit pattern (16-bit unsigned integer).
|
|
15
|
+
*
|
|
16
|
+
* Operates directly on float64 bits to avoid double-rounding artifacts that
|
|
17
|
+
* arise from a float64 → float32 → float16 two-step conversion.
|
|
18
|
+
* Implements IEEE 754 round-to-nearest-ties-to-even (RN-TE).
|
|
19
|
+
*
|
|
20
|
+
* Exported for testing (manual vs native consistency checks).
|
|
21
|
+
*/
|
|
22
|
+
export declare function float64ToFloat16Bits(value: number): number;
|
|
23
|
+
/**
|
|
24
|
+
* float16 bit pattern → float64.
|
|
25
|
+
* Exported for testing purposes.
|
|
26
|
+
*/
|
|
27
|
+
export declare function float16BitsToFloat64(bits: number): number;
|
|
28
|
+
/**
|
|
29
|
+
* Write a float16 value at the given offset in a DataView.
|
|
30
|
+
*/
|
|
31
|
+
type WriteFloat16 = (view: DataView, offset: number, value: number, littleEndian: boolean) => void;
|
|
32
|
+
/**
|
|
33
|
+
* Read a float16 value from the given offset in a DataView, returned as float64.
|
|
34
|
+
*/
|
|
35
|
+
type ReadFloat16 = (view: DataView, offset: number, littleEndian: boolean) => number;
|
|
36
|
+
export declare const writeFloat16: WriteFloat16;
|
|
37
|
+
export declare const readFloat16: ReadFloat16;
|
|
38
|
+
export {};
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Hex float (C99-style, e.g. `0x1.8p+0`) encode/decode utilities.
|
|
3
|
+
*
|
|
4
|
+
* Hex float format:
|
|
5
|
+
* [-] 0x [hex digits] [. [hex digits]] p [+-] [decimal exponent]
|
|
6
|
+
*
|
|
7
|
+
* This notation appears in CBOR-EDN (draft-ietf-cbor-edn-literals) as an
|
|
8
|
+
* alternative representation for floating-point values (major type 7).
|
|
9
|
+
*/
|
|
10
|
+
/**
|
|
11
|
+
* Parse a hex float literal (e.g. `0x4711p+03`, `0x1.8p+0`, `-0x1.fp-2`)
|
|
12
|
+
* to a JS number.
|
|
13
|
+
*
|
|
14
|
+
* Assumes the string has already been stripped of any encoding-indicator
|
|
15
|
+
* suffix (`_1`, `_2`, `_3`).
|
|
16
|
+
*/
|
|
17
|
+
export declare function parseHexFloat(s: string): number;
|
|
18
|
+
/**
|
|
19
|
+
* Convert a JS number to a normalized hex float string compatible with
|
|
20
|
+
* CBOR-EDN diagnostic notation.
|
|
21
|
+
*
|
|
22
|
+
* - Normal values: `0x1.[hex fraction]p[+-][exp]` (e.g. `0x1.8p+0` for 1.5)
|
|
23
|
+
* - Subnormal values: `0x0.[hex fraction]p-1022`
|
|
24
|
+
* - Zero: `0x0p+0` / `-0x0p+0`
|
|
25
|
+
* - Non-finite values (NaN, ±Infinity) are returned unchanged as EDN tokens.
|
|
26
|
+
*/
|
|
27
|
+
export declare function floatToHexFloat(v: number): string;
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Shared IPv4 / IPv6 address parsing and formatting utilities.
|
|
3
|
+
* Used by the "ip"/"IP" extension (RFC 9164) and the "cri"/"CRI" extension
|
|
4
|
+
* (draft-ietf-core-href).
|
|
5
|
+
*/
|
|
6
|
+
export declare function parseIPv4(str: string): Uint8Array;
|
|
7
|
+
export declare function parseIPv6(str: string): Uint8Array;
|
|
8
|
+
export declare function formatIPv4(bytes: Uint8Array): string;
|
|
9
|
+
export declare function formatIPv6(bytes: Uint8Array): string;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cbortech/cbor",
|
|
3
|
-
"version": "0.23.
|
|
3
|
+
"version": "0.23.1",
|
|
4
4
|
"description": "Convert between CBOR, CBOR-EDN, and JavaScript values",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"cbor",
|
|
@@ -40,6 +40,16 @@
|
|
|
40
40
|
"types": "./dist/index.d.ts",
|
|
41
41
|
"default": "./dist/index.cjs"
|
|
42
42
|
}
|
|
43
|
+
},
|
|
44
|
+
"./ast": {
|
|
45
|
+
"import": {
|
|
46
|
+
"types": "./dist/ast/index.d.ts",
|
|
47
|
+
"default": "./dist/ast/index.js"
|
|
48
|
+
},
|
|
49
|
+
"require": {
|
|
50
|
+
"types": "./dist/ast/index.d.ts",
|
|
51
|
+
"default": "./dist/ast/index.cjs"
|
|
52
|
+
}
|
|
43
53
|
}
|
|
44
54
|
},
|
|
45
55
|
"files": [
|
|
@@ -54,6 +64,7 @@
|
|
|
54
64
|
"dev": "npm run build && npm start",
|
|
55
65
|
"clean": "node -e \"['dist','coverage'].forEach(d=>require('fs').rmSync(d,{recursive:true,force:true}))\"",
|
|
56
66
|
"test": "vitest run",
|
|
67
|
+
"test:exports": "npm run build && node scripts/check-package-exports.mjs",
|
|
57
68
|
"test:vectors": "vitest run --config vitest.vectors.config.ts",
|
|
58
69
|
"test:node": "vitest run",
|
|
59
70
|
"test:chromium": "BROWSER=chromium vitest run --config vitest.browser.config.ts",
|