@bcts/dcbor 1.0.0-alpha.10
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 +48 -0
- package/README.md +13 -0
- package/dist/index.cjs +9151 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +3107 -0
- package/dist/index.d.cts.map +1 -0
- package/dist/index.d.mts +3107 -0
- package/dist/index.d.mts.map +1 -0
- package/dist/index.iife.js +9155 -0
- package/dist/index.iife.js.map +1 -0
- package/dist/index.mjs +9027 -0
- package/dist/index.mjs.map +1 -0
- package/package.json +80 -0
- package/src/.claude-flow/metrics/agent-metrics.json +1 -0
- package/src/.claude-flow/metrics/performance.json +87 -0
- package/src/.claude-flow/metrics/task-metrics.json +10 -0
- package/src/byte-string.ts +300 -0
- package/src/cbor-codable.ts +170 -0
- package/src/cbor-tagged-codable.ts +72 -0
- package/src/cbor-tagged-decodable.ts +184 -0
- package/src/cbor-tagged-encodable.ts +138 -0
- package/src/cbor-tagged.ts +104 -0
- package/src/cbor.ts +869 -0
- package/src/conveniences.ts +840 -0
- package/src/date.ts +553 -0
- package/src/decode.ts +276 -0
- package/src/diag.ts +462 -0
- package/src/dump.ts +277 -0
- package/src/error.ts +259 -0
- package/src/exact.ts +714 -0
- package/src/float.ts +279 -0
- package/src/global.d.ts +34 -0
- package/src/globals.d.ts +0 -0
- package/src/index.ts +180 -0
- package/src/map.ts +308 -0
- package/src/prelude.ts +70 -0
- package/src/set.ts +515 -0
- package/src/simple.ts +153 -0
- package/src/stdlib.ts +55 -0
- package/src/string-util.ts +55 -0
- package/src/tag.ts +53 -0
- package/src/tags-store.ts +294 -0
- package/src/tags.ts +231 -0
- package/src/varint.ts +124 -0
- package/src/walk.ts +516 -0
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,3107 @@
|
|
|
1
|
+
//#region src/map.d.ts
|
|
2
|
+
|
|
3
|
+
interface MapEntry {
|
|
4
|
+
readonly key: Cbor;
|
|
5
|
+
readonly value: Cbor;
|
|
6
|
+
}
|
|
7
|
+
/**
|
|
8
|
+
* A deterministic CBOR map implementation.
|
|
9
|
+
*
|
|
10
|
+
* Maps are always encoded with keys sorted lexicographically by their
|
|
11
|
+
* encoded CBOR representation, ensuring deterministic encoding.
|
|
12
|
+
*/
|
|
13
|
+
declare class CborMap {
|
|
14
|
+
#private;
|
|
15
|
+
/**
|
|
16
|
+
* Creates a new, empty CBOR Map.
|
|
17
|
+
* Optionally initializes from a JavaScript Map.
|
|
18
|
+
*/
|
|
19
|
+
constructor(map?: Map<unknown, unknown>);
|
|
20
|
+
/**
|
|
21
|
+
* Creates a new, empty CBOR Map.
|
|
22
|
+
* Matches Rust's Map::new().
|
|
23
|
+
*/
|
|
24
|
+
static new(): CborMap;
|
|
25
|
+
/**
|
|
26
|
+
* Inserts a key-value pair into the map.
|
|
27
|
+
* Matches Rust's Map::insert().
|
|
28
|
+
*/
|
|
29
|
+
set<K extends CborInput, V extends CborInput>(key: K, value: V): void;
|
|
30
|
+
/**
|
|
31
|
+
* Alias for set() to match Rust's insert() method.
|
|
32
|
+
*/
|
|
33
|
+
insert<K extends CborInput, V extends CborInput>(key: K, value: V): void;
|
|
34
|
+
/**
|
|
35
|
+
* Get a value from the map, given a key.
|
|
36
|
+
* Returns undefined if the key is not present in the map.
|
|
37
|
+
* Matches Rust's Map::get().
|
|
38
|
+
*/
|
|
39
|
+
get<K extends CborInput, V>(key: K): V | undefined;
|
|
40
|
+
/**
|
|
41
|
+
* Get a value from the map, given a key.
|
|
42
|
+
* Throws an error if the key is not present.
|
|
43
|
+
* Matches Rust's Map::extract().
|
|
44
|
+
*/
|
|
45
|
+
extract<K extends CborInput, V>(key: K): V;
|
|
46
|
+
/**
|
|
47
|
+
* Tests if the map contains a key.
|
|
48
|
+
* Matches Rust's Map::contains_key().
|
|
49
|
+
*/
|
|
50
|
+
containsKey<K extends CborInput>(key: K): boolean;
|
|
51
|
+
delete<K extends CborInput>(key: K): boolean;
|
|
52
|
+
has<K extends CborInput>(key: K): boolean;
|
|
53
|
+
clear(): void;
|
|
54
|
+
/**
|
|
55
|
+
* Returns the number of entries in the map.
|
|
56
|
+
* Matches Rust's Map::len().
|
|
57
|
+
*/
|
|
58
|
+
get length(): number;
|
|
59
|
+
/**
|
|
60
|
+
* Alias for length to match JavaScript Map API.
|
|
61
|
+
* Also matches Rust's Map::len().
|
|
62
|
+
*/
|
|
63
|
+
get size(): number;
|
|
64
|
+
/**
|
|
65
|
+
* Returns the number of entries in the map.
|
|
66
|
+
* Matches Rust's Map::len().
|
|
67
|
+
*/
|
|
68
|
+
len(): number;
|
|
69
|
+
/**
|
|
70
|
+
* Checks if the map is empty.
|
|
71
|
+
* Matches Rust's Map::is_empty().
|
|
72
|
+
*/
|
|
73
|
+
isEmpty(): boolean;
|
|
74
|
+
/**
|
|
75
|
+
* Get the entries of the map as an array.
|
|
76
|
+
* Keys are sorted in lexicographic order of their encoded CBOR bytes.
|
|
77
|
+
*/
|
|
78
|
+
get entriesArray(): MapEntry[];
|
|
79
|
+
/**
|
|
80
|
+
* Gets an iterator over the entries of the CBOR map, sorted by key.
|
|
81
|
+
* Key sorting order is lexicographic by the key's binary-encoded CBOR.
|
|
82
|
+
* Matches Rust's Map::iter().
|
|
83
|
+
*/
|
|
84
|
+
iter(): MapEntry[];
|
|
85
|
+
/**
|
|
86
|
+
* Returns an iterator of [key, value] tuples for JavaScript Map API compatibility.
|
|
87
|
+
* This matches the standard JavaScript Map.entries() method behavior.
|
|
88
|
+
*/
|
|
89
|
+
entries(): IterableIterator<[Cbor, Cbor]>;
|
|
90
|
+
/**
|
|
91
|
+
* Inserts the next key-value pair into the map during decoding.
|
|
92
|
+
* This is used for efficient map building during CBOR decoding.
|
|
93
|
+
* Throws if the key is not in ascending order or is a duplicate.
|
|
94
|
+
* Matches Rust's Map::insert_next().
|
|
95
|
+
*/
|
|
96
|
+
setNext<K extends CborInput, V extends CborInput>(key: K, value: V): void;
|
|
97
|
+
get debug(): string;
|
|
98
|
+
get diagnostic(): string;
|
|
99
|
+
private static entryDebug;
|
|
100
|
+
private static formatDebug;
|
|
101
|
+
private static entryDiagnostic;
|
|
102
|
+
[Symbol.iterator](): Iterator<[Cbor, Cbor]>;
|
|
103
|
+
toMap<K, V>(): Map<K, V>;
|
|
104
|
+
}
|
|
105
|
+
//#endregion
|
|
106
|
+
//#region src/simple.d.ts
|
|
107
|
+
/**
|
|
108
|
+
* CBOR Simple Values (Major Type 7).
|
|
109
|
+
*
|
|
110
|
+
* @module simple
|
|
111
|
+
*/
|
|
112
|
+
/**
|
|
113
|
+
* Represents CBOR simple values (major type 7).
|
|
114
|
+
*
|
|
115
|
+
* In CBOR, simple values are a special category that includes booleans (`true`
|
|
116
|
+
* and `false`), `null`, and floating point numbers.
|
|
117
|
+
*
|
|
118
|
+
* Per Section 2.4 of the dCBOR specification, only these specific simple
|
|
119
|
+
* values are valid in dCBOR. All other major type 7 values (such as undefined
|
|
120
|
+
* or other simple values) are invalid and will be rejected by dCBOR decoders.
|
|
121
|
+
*
|
|
122
|
+
* When encoding floating point values, dCBOR follows specific numeric
|
|
123
|
+
* reduction rules detailed in Section 2.3 of the dCBOR specification,
|
|
124
|
+
* including
|
|
125
|
+
* - Integral floating point values must be reduced to integers when possible
|
|
126
|
+
* - NaN values must be normalized to the canonical form `f97e00`
|
|
127
|
+
*/
|
|
128
|
+
type Simple = {
|
|
129
|
+
readonly type: "False";
|
|
130
|
+
} | {
|
|
131
|
+
readonly type: "True";
|
|
132
|
+
} | {
|
|
133
|
+
readonly type: "Null";
|
|
134
|
+
} | {
|
|
135
|
+
readonly type: "Float";
|
|
136
|
+
readonly value: number;
|
|
137
|
+
};
|
|
138
|
+
/**
|
|
139
|
+
* Returns the standard name of the simple value as a string.
|
|
140
|
+
*
|
|
141
|
+
* For `False`, `True`, and `Null`, this returns their lowercase string
|
|
142
|
+
* representation. For `Float` values, it returns their numeric representation.
|
|
143
|
+
*/
|
|
144
|
+
declare const simpleName: (simple: Simple) => string;
|
|
145
|
+
/**
|
|
146
|
+
* Checks if the simple value is the NaN (Not a Number) representation.
|
|
147
|
+
*/
|
|
148
|
+
declare const isNaN: (simple: Simple) => boolean;
|
|
149
|
+
//#endregion
|
|
150
|
+
//#region src/tag.d.ts
|
|
151
|
+
/**
|
|
152
|
+
* A CBOR tag with an optional name.
|
|
153
|
+
*
|
|
154
|
+
* Tags consist of a numeric value and an optional human-readable name.
|
|
155
|
+
*/
|
|
156
|
+
interface Tag {
|
|
157
|
+
/** The numeric tag value */
|
|
158
|
+
readonly value: CborNumber;
|
|
159
|
+
/** Optional human-readable name for the tag */
|
|
160
|
+
readonly name?: string;
|
|
161
|
+
}
|
|
162
|
+
/**
|
|
163
|
+
* Create a new Tag.
|
|
164
|
+
*
|
|
165
|
+
* @param value - The numeric tag value
|
|
166
|
+
* @param name - Optional human-readable name
|
|
167
|
+
* @returns A new Tag object
|
|
168
|
+
*
|
|
169
|
+
* @example
|
|
170
|
+
* ```typescript
|
|
171
|
+
* const dateTag = createTag(1, 'date');
|
|
172
|
+
* const customTag = createTag(12345, 'myCustomTag');
|
|
173
|
+
* ```
|
|
174
|
+
*/
|
|
175
|
+
declare const createTag: (value: CborNumber, name?: string) => Tag;
|
|
176
|
+
//#endregion
|
|
177
|
+
//#region src/byte-string.d.ts
|
|
178
|
+
/**
|
|
179
|
+
* Represents a CBOR byte string (major type 2).
|
|
180
|
+
*
|
|
181
|
+
* Use Cases:
|
|
182
|
+
* - Binary data such as images, audio, or other non-text content
|
|
183
|
+
* - Cryptographic values like hashes, signatures, and public keys
|
|
184
|
+
* - Embedded CBOR (wrapped with tag 24)
|
|
185
|
+
* - Other serialized data formats embedded in CBOR
|
|
186
|
+
*
|
|
187
|
+
* @example
|
|
188
|
+
* ```typescript
|
|
189
|
+
* // Creating a byte string from various sources
|
|
190
|
+
* const bytes1 = new ByteString(new Uint8Array([1, 2, 3, 4]));
|
|
191
|
+
* const bytes2 = ByteString.from([5, 6, 7, 8]);
|
|
192
|
+
* const bytes3 = ByteString.from(new Uint8Array([9, 10, 11, 12]));
|
|
193
|
+
*
|
|
194
|
+
* // Converting to and from CBOR
|
|
195
|
+
* const cborValue = bytes1.toCbor();
|
|
196
|
+
*
|
|
197
|
+
* // ByteString provides Uint8Array-like operations
|
|
198
|
+
* const bytes = new ByteString(new Uint8Array([1, 2]));
|
|
199
|
+
* bytes.extend(new Uint8Array([3, 4]));
|
|
200
|
+
* assert(bytes.len() === 4);
|
|
201
|
+
* assert.deepEqual(bytes.data(), new Uint8Array([1, 2, 3, 4]));
|
|
202
|
+
* ```
|
|
203
|
+
*/
|
|
204
|
+
declare class ByteString {
|
|
205
|
+
#private;
|
|
206
|
+
/**
|
|
207
|
+
* Creates a new `ByteString` from a Uint8Array or array of bytes.
|
|
208
|
+
*
|
|
209
|
+
* @param data - The byte data
|
|
210
|
+
*
|
|
211
|
+
* @example
|
|
212
|
+
* ```typescript
|
|
213
|
+
* // From a Uint8Array
|
|
214
|
+
* const bytes1 = new ByteString(new Uint8Array([1, 2, 3, 4]));
|
|
215
|
+
*
|
|
216
|
+
* // From a number array
|
|
217
|
+
* const bytes2 = new ByteString(new Uint8Array([5, 6, 7, 8]));
|
|
218
|
+
* ```
|
|
219
|
+
*/
|
|
220
|
+
constructor(data: Uint8Array | number[]);
|
|
221
|
+
/**
|
|
222
|
+
* Creates a new `ByteString` from various input types.
|
|
223
|
+
*
|
|
224
|
+
* @param data - Uint8Array, number array, or string
|
|
225
|
+
* @returns New ByteString instance
|
|
226
|
+
*
|
|
227
|
+
* @example
|
|
228
|
+
* ```typescript
|
|
229
|
+
* const bytes1 = ByteString.from([1, 2, 3, 4]);
|
|
230
|
+
* const bytes2 = ByteString.from(new Uint8Array([5, 6, 7, 8]));
|
|
231
|
+
* const bytes3 = ByteString.from("hello");
|
|
232
|
+
* ```
|
|
233
|
+
*/
|
|
234
|
+
static from(data: Uint8Array | number[] | string): ByteString;
|
|
235
|
+
/**
|
|
236
|
+
* Returns a reference to the underlying byte data.
|
|
237
|
+
*
|
|
238
|
+
* @returns The raw bytes
|
|
239
|
+
*
|
|
240
|
+
* @example
|
|
241
|
+
* ```typescript
|
|
242
|
+
* const bytes = new ByteString(new Uint8Array([1, 2, 3, 4]));
|
|
243
|
+
* assert.deepEqual(bytes.data(), new Uint8Array([1, 2, 3, 4]));
|
|
244
|
+
*
|
|
245
|
+
* // You can use standard slice operations on the result
|
|
246
|
+
* assert.deepEqual(bytes.data().slice(1, 3), new Uint8Array([2, 3]));
|
|
247
|
+
* ```
|
|
248
|
+
*/
|
|
249
|
+
data(): Uint8Array;
|
|
250
|
+
/**
|
|
251
|
+
* Returns the length of the byte string in bytes.
|
|
252
|
+
*
|
|
253
|
+
* @returns Number of bytes
|
|
254
|
+
*
|
|
255
|
+
* @example
|
|
256
|
+
* ```typescript
|
|
257
|
+
* const empty = new ByteString(new Uint8Array([]));
|
|
258
|
+
* assert(empty.len() === 0);
|
|
259
|
+
*
|
|
260
|
+
* const bytes = new ByteString(new Uint8Array([1, 2, 3, 4]));
|
|
261
|
+
* assert(bytes.len() === 4);
|
|
262
|
+
* ```
|
|
263
|
+
*/
|
|
264
|
+
len(): number;
|
|
265
|
+
/**
|
|
266
|
+
* Returns `true` if the byte string contains no bytes.
|
|
267
|
+
*
|
|
268
|
+
* @returns true if empty
|
|
269
|
+
*
|
|
270
|
+
* @example
|
|
271
|
+
* ```typescript
|
|
272
|
+
* const empty = new ByteString(new Uint8Array([]));
|
|
273
|
+
* assert(empty.isEmpty());
|
|
274
|
+
*
|
|
275
|
+
* const bytes = new ByteString(new Uint8Array([1, 2, 3, 4]));
|
|
276
|
+
* assert(!bytes.isEmpty());
|
|
277
|
+
* ```
|
|
278
|
+
*/
|
|
279
|
+
isEmpty(): boolean;
|
|
280
|
+
/**
|
|
281
|
+
* Extends the byte string with additional bytes.
|
|
282
|
+
*
|
|
283
|
+
* @param other - Bytes to append
|
|
284
|
+
*
|
|
285
|
+
* @example
|
|
286
|
+
* ```typescript
|
|
287
|
+
* const bytes = new ByteString(new Uint8Array([1, 2]));
|
|
288
|
+
* bytes.extend(new Uint8Array([3, 4]));
|
|
289
|
+
* assert.deepEqual(bytes.data(), new Uint8Array([1, 2, 3, 4]));
|
|
290
|
+
*
|
|
291
|
+
* // You can extend with different types
|
|
292
|
+
* bytes.extend([5, 6]);
|
|
293
|
+
* assert.deepEqual(bytes.data(), new Uint8Array([1, 2, 3, 4, 5, 6]));
|
|
294
|
+
* ```
|
|
295
|
+
*/
|
|
296
|
+
extend(other: Uint8Array | number[]): void;
|
|
297
|
+
/**
|
|
298
|
+
* Creates a new Uint8Array containing a copy of the byte string's data.
|
|
299
|
+
*
|
|
300
|
+
* @returns Copy of the data
|
|
301
|
+
*
|
|
302
|
+
* @example
|
|
303
|
+
* ```typescript
|
|
304
|
+
* const bytes = new ByteString(new Uint8Array([1, 2, 3, 4]));
|
|
305
|
+
* const arr = bytes.toUint8Array();
|
|
306
|
+
* assert.deepEqual(arr, new Uint8Array([1, 2, 3, 4]));
|
|
307
|
+
*
|
|
308
|
+
* // The returned array is a clone, so you can modify it independently
|
|
309
|
+
* const arr2 = bytes.toUint8Array();
|
|
310
|
+
* arr2[0] = 99;
|
|
311
|
+
* assert.deepEqual(bytes.data(), new Uint8Array([1, 2, 3, 4])); // original unchanged
|
|
312
|
+
* ```
|
|
313
|
+
*/
|
|
314
|
+
toUint8Array(): Uint8Array;
|
|
315
|
+
/**
|
|
316
|
+
* Returns an iterator over the bytes in the byte string.
|
|
317
|
+
*
|
|
318
|
+
* @returns Iterator yielding each byte
|
|
319
|
+
*
|
|
320
|
+
* @example
|
|
321
|
+
* ```typescript
|
|
322
|
+
* const bytes = new ByteString(new Uint8Array([1, 2, 3]));
|
|
323
|
+
* const iter = bytes.iter();
|
|
324
|
+
*
|
|
325
|
+
* assert(iter.next().value === 1);
|
|
326
|
+
* assert(iter.next().value === 2);
|
|
327
|
+
* assert(iter.next().value === 3);
|
|
328
|
+
* assert(iter.next().done);
|
|
329
|
+
*
|
|
330
|
+
* // You can also use for loops
|
|
331
|
+
* let sum = 0;
|
|
332
|
+
* for (const byte of bytes) {
|
|
333
|
+
* sum += byte;
|
|
334
|
+
* }
|
|
335
|
+
* assert(sum === 6);
|
|
336
|
+
* ```
|
|
337
|
+
*/
|
|
338
|
+
iter(): Iterator<number>;
|
|
339
|
+
/**
|
|
340
|
+
* Makes ByteString iterable.
|
|
341
|
+
*/
|
|
342
|
+
[Symbol.iterator](): Iterator<number>;
|
|
343
|
+
/**
|
|
344
|
+
* Converts the ByteString to a CBOR value.
|
|
345
|
+
*
|
|
346
|
+
* @returns CBOR byte string
|
|
347
|
+
*
|
|
348
|
+
* @example
|
|
349
|
+
* ```typescript
|
|
350
|
+
* const bytes = new ByteString(new Uint8Array([1, 2, 3, 4]));
|
|
351
|
+
* const cborValue = bytes.toCbor();
|
|
352
|
+
* ```
|
|
353
|
+
*/
|
|
354
|
+
toCbor(): Cbor;
|
|
355
|
+
/**
|
|
356
|
+
* Attempts to convert a CBOR value into a ByteString.
|
|
357
|
+
*
|
|
358
|
+
* @param cbor - CBOR value
|
|
359
|
+
* @returns ByteString if successful
|
|
360
|
+
* @throws Error if the CBOR value is not a byte string
|
|
361
|
+
*
|
|
362
|
+
* @example
|
|
363
|
+
* ```typescript
|
|
364
|
+
* const cborValue = toCbor(new Uint8Array([1, 2, 3, 4]));
|
|
365
|
+
* const bytes = ByteString.fromCbor(cborValue);
|
|
366
|
+
* assert.deepEqual(bytes.data(), new Uint8Array([1, 2, 3, 4]));
|
|
367
|
+
*
|
|
368
|
+
* // Converting from a different CBOR type throws
|
|
369
|
+
* const cborInt = toCbor(42);
|
|
370
|
+
* try {
|
|
371
|
+
* ByteString.fromCbor(cborInt); // throws
|
|
372
|
+
* } catch(e) {
|
|
373
|
+
* // Error: Wrong type
|
|
374
|
+
* }
|
|
375
|
+
* ```
|
|
376
|
+
*/
|
|
377
|
+
static fromCbor(cbor: Cbor): ByteString;
|
|
378
|
+
/**
|
|
379
|
+
* Get element at index.
|
|
380
|
+
*
|
|
381
|
+
* @param index - Index to access
|
|
382
|
+
* @returns Byte at index or undefined
|
|
383
|
+
*/
|
|
384
|
+
at(index: number): number | undefined;
|
|
385
|
+
/**
|
|
386
|
+
* Equality comparison.
|
|
387
|
+
*
|
|
388
|
+
* @param other - ByteString to compare with
|
|
389
|
+
* @returns true if equal
|
|
390
|
+
*/
|
|
391
|
+
equals(other: ByteString): boolean;
|
|
392
|
+
/**
|
|
393
|
+
* Clone this ByteString.
|
|
394
|
+
*
|
|
395
|
+
* @returns New ByteString with copied data
|
|
396
|
+
*/
|
|
397
|
+
clone(): ByteString;
|
|
398
|
+
}
|
|
399
|
+
//#endregion
|
|
400
|
+
//#region src/cbor-tagged-encodable.d.ts
|
|
401
|
+
/**
|
|
402
|
+
* Interface for types that can be encoded to CBOR with a specific tag.
|
|
403
|
+
*
|
|
404
|
+
* This interface extends `CborTagged` to provide methods for encoding a value
|
|
405
|
+
* with its associated tag. Types that implement this interface define how they
|
|
406
|
+
* should be represented in CBOR format, both with and without their tag.
|
|
407
|
+
*
|
|
408
|
+
* @example
|
|
409
|
+
* ```typescript
|
|
410
|
+
* // Define a Date type
|
|
411
|
+
* class Date implements CborTaggedEncodable {
|
|
412
|
+
* constructor(private timestamp: number) {}
|
|
413
|
+
*
|
|
414
|
+
* cborTags(): Tag[] {
|
|
415
|
+
* return [createTag(1, 'date')]; // Standard date tag
|
|
416
|
+
* }
|
|
417
|
+
*
|
|
418
|
+
* untaggedCbor(): Cbor {
|
|
419
|
+
* // Date content is represented as a number
|
|
420
|
+
* return cbor(this.timestamp);
|
|
421
|
+
* }
|
|
422
|
+
*
|
|
423
|
+
* taggedCbor(): Cbor {
|
|
424
|
+
* const tags = this.cborTags();
|
|
425
|
+
* return {
|
|
426
|
+
* isCbor: true,
|
|
427
|
+
* type: MajorType.Tagged,
|
|
428
|
+
* tag: tags[0].value,
|
|
429
|
+
* value: this.untaggedCbor()
|
|
430
|
+
* };
|
|
431
|
+
* }
|
|
432
|
+
*
|
|
433
|
+
* taggedCborData(): Uint8Array {
|
|
434
|
+
* return cborData(this.taggedCbor());
|
|
435
|
+
* }
|
|
436
|
+
* }
|
|
437
|
+
*
|
|
438
|
+
* // Create a date and encode it
|
|
439
|
+
* const date = new Date(1609459200);
|
|
440
|
+
*
|
|
441
|
+
* // Get the untagged CBOR (just the timestamp)
|
|
442
|
+
* const untagged = date.untaggedCbor();
|
|
443
|
+
*
|
|
444
|
+
* // Get the tagged CBOR (with tag 1)
|
|
445
|
+
* const tagged = date.taggedCbor();
|
|
446
|
+
*
|
|
447
|
+
* // Get binary representation
|
|
448
|
+
* const data = date.taggedCborData();
|
|
449
|
+
* ```
|
|
450
|
+
*/
|
|
451
|
+
interface CborTaggedEncodable extends CborTagged {
|
|
452
|
+
/**
|
|
453
|
+
* Returns the untagged CBOR encoding of this instance.
|
|
454
|
+
*
|
|
455
|
+
* This method defines how the value itself (without its tag) should
|
|
456
|
+
* be represented in CBOR format.
|
|
457
|
+
*
|
|
458
|
+
* @returns Untagged CBOR representation
|
|
459
|
+
*/
|
|
460
|
+
untaggedCbor(): Cbor;
|
|
461
|
+
/**
|
|
462
|
+
* Returns the tagged CBOR encoding of this instance.
|
|
463
|
+
*
|
|
464
|
+
* This method wraps the result of `untaggedCbor()` with the first tag
|
|
465
|
+
* from `cborTags()`, which is considered the "preferred" tag for the
|
|
466
|
+
* type.
|
|
467
|
+
*
|
|
468
|
+
* Even if a type supports multiple tags for backward-compatible decoding
|
|
469
|
+
* via `cborTags()`, only the first (preferred) tag is used for encoding.
|
|
470
|
+
* This ensures consistency in newly created data while maintaining the
|
|
471
|
+
* ability to read older formats.
|
|
472
|
+
*
|
|
473
|
+
* In most cases, you don't need to override this method.
|
|
474
|
+
*
|
|
475
|
+
* @returns Tagged CBOR representation
|
|
476
|
+
*/
|
|
477
|
+
taggedCbor(): Cbor;
|
|
478
|
+
/**
|
|
479
|
+
* Returns the tagged value in CBOR binary representation.
|
|
480
|
+
*
|
|
481
|
+
* This is a convenience method that converts the result of `taggedCbor()`
|
|
482
|
+
* to binary format.
|
|
483
|
+
*
|
|
484
|
+
* @returns Binary CBOR representation
|
|
485
|
+
*/
|
|
486
|
+
taggedCborData?(): Uint8Array;
|
|
487
|
+
}
|
|
488
|
+
/**
|
|
489
|
+
* Helper function to create tagged CBOR from an encodable object.
|
|
490
|
+
*
|
|
491
|
+
* Uses the first tag from cborTags().
|
|
492
|
+
*
|
|
493
|
+
* @param encodable - Object implementing CborTaggedEncodable
|
|
494
|
+
* @returns Tagged CBOR value
|
|
495
|
+
*/
|
|
496
|
+
declare const createTaggedCbor: (encodable: CborTaggedEncodable) => Cbor;
|
|
497
|
+
//#endregion
|
|
498
|
+
//#region src/cbor-tagged-decodable.d.ts
|
|
499
|
+
/**
|
|
500
|
+
* Interface for types that can be decoded from CBOR with a specific tag.
|
|
501
|
+
*
|
|
502
|
+
* This interface extends `CborTagged` to provide methods for
|
|
503
|
+
* decoding tagged CBOR data into TypeScript types. It handles verification that
|
|
504
|
+
* the CBOR data has the expected tag(s) and provides utilities for both
|
|
505
|
+
* tagged and untagged decoding.
|
|
506
|
+
*
|
|
507
|
+
* @example
|
|
508
|
+
* ```typescript
|
|
509
|
+
* // Define a Date type
|
|
510
|
+
* class Date implements CborTaggedDecodable<Date> {
|
|
511
|
+
* constructor(public timestamp: number) {}
|
|
512
|
+
*
|
|
513
|
+
* cborTags(): Tag[] {
|
|
514
|
+
* return [createTag(1, 'date')]; // Standard date tag
|
|
515
|
+
* }
|
|
516
|
+
*
|
|
517
|
+
* fromUntaggedCbor(cbor: Cbor): Date {
|
|
518
|
+
* // Convert the untagged CBOR to a number
|
|
519
|
+
* if (cbor.type !== MajorType.Unsigned) {
|
|
520
|
+
* throw new Error('Wrong type');
|
|
521
|
+
* }
|
|
522
|
+
* const timestamp = typeof cbor.value === 'bigint' ? Number(cbor.value) : cbor.value;
|
|
523
|
+
* return new Date(timestamp);
|
|
524
|
+
* }
|
|
525
|
+
*
|
|
526
|
+
* fromTaggedCbor(cbor: Cbor): Date {
|
|
527
|
+
* if (cbor.type !== MajorType.Tagged) {
|
|
528
|
+
* throw new Error('Wrong type');
|
|
529
|
+
* }
|
|
530
|
+
*
|
|
531
|
+
* const tags = this.cborTags();
|
|
532
|
+
* const tagValues = tags.map(t => t.value);
|
|
533
|
+
* if (!tagValues.includes(cbor.tag as number)) {
|
|
534
|
+
* throw new Error(`Wrong tag: expected ${tagValues[0]}, got ${cbor.tag}`);
|
|
535
|
+
* }
|
|
536
|
+
*
|
|
537
|
+
* return this.fromUntaggedCbor(cbor.value);
|
|
538
|
+
* }
|
|
539
|
+
*
|
|
540
|
+
* static fromTaggedCborData(data: Uint8Array): Date {
|
|
541
|
+
* const cbor = decodeCbor(data);
|
|
542
|
+
* return new Date(0).fromTaggedCbor(cbor);
|
|
543
|
+
* }
|
|
544
|
+
*
|
|
545
|
+
* static fromUntaggedCborData(data: Uint8Array): Date {
|
|
546
|
+
* const cbor = decodeCbor(data);
|
|
547
|
+
* return new Date(0).fromUntaggedCbor(cbor);
|
|
548
|
+
* }
|
|
549
|
+
* }
|
|
550
|
+
*
|
|
551
|
+
* // Create tagged CBOR data
|
|
552
|
+
* const taggedCbor = {
|
|
553
|
+
* isCbor: true,
|
|
554
|
+
* type: MajorType.Tagged,
|
|
555
|
+
* tag: 1,
|
|
556
|
+
* value: cbor(1609459200)
|
|
557
|
+
* };
|
|
558
|
+
*
|
|
559
|
+
* // Decode using the interface
|
|
560
|
+
* const date = new Date(0).fromTaggedCbor(taggedCbor);
|
|
561
|
+
* assert(date.timestamp === 1609459200);
|
|
562
|
+
* ```
|
|
563
|
+
*/
|
|
564
|
+
interface CborTaggedDecodable<T> extends CborTagged {
|
|
565
|
+
/**
|
|
566
|
+
* Creates an instance of this type by decoding it from untagged CBOR.
|
|
567
|
+
*
|
|
568
|
+
* This method defines how to interpret the CBOR content (without
|
|
569
|
+
* considering the tag) and convert it to the implementing type.
|
|
570
|
+
*
|
|
571
|
+
* @param cbor - Untagged CBOR value
|
|
572
|
+
* @returns Decoded instance
|
|
573
|
+
* @throws Error if the CBOR value cannot be decoded
|
|
574
|
+
*/
|
|
575
|
+
fromUntaggedCbor(cbor: Cbor): T;
|
|
576
|
+
/**
|
|
577
|
+
* Creates an instance of this type by decoding it from tagged CBOR.
|
|
578
|
+
*
|
|
579
|
+
* This method first verifies that the CBOR value has one of the expected
|
|
580
|
+
* tags (as defined by `cborTags()`), then delegates to
|
|
581
|
+
* `fromUntaggedCbor()` to decode the content.
|
|
582
|
+
*
|
|
583
|
+
* For backward compatibility, this method accepts any tag from the
|
|
584
|
+
* `cborTags()` array, not just the first one. This allows new
|
|
585
|
+
* versions of types to still accept data tagged with older/alternative
|
|
586
|
+
* tag values.
|
|
587
|
+
*
|
|
588
|
+
* In most cases, you don't need to override this method.
|
|
589
|
+
*
|
|
590
|
+
* @param cbor - Tagged CBOR value
|
|
591
|
+
* @returns Decoded instance
|
|
592
|
+
* @throws Error if the CBOR value has the wrong tag or cannot be decoded
|
|
593
|
+
*/
|
|
594
|
+
fromTaggedCbor(cbor: Cbor): T;
|
|
595
|
+
/**
|
|
596
|
+
* Creates an instance of this type by decoding it from binary encoded
|
|
597
|
+
* tagged CBOR.
|
|
598
|
+
*
|
|
599
|
+
* This is a convenience method that first parses the binary data into a
|
|
600
|
+
* CBOR value, then uses `fromTaggedCbor()` to decode it.
|
|
601
|
+
*
|
|
602
|
+
* @param data - Binary CBOR data
|
|
603
|
+
* @returns Decoded instance
|
|
604
|
+
* @throws Error if the data cannot be parsed or decoded
|
|
605
|
+
*/
|
|
606
|
+
fromTaggedCborData?(data: Uint8Array): T;
|
|
607
|
+
/**
|
|
608
|
+
* Creates an instance of this type by decoding it from binary encoded
|
|
609
|
+
* untagged CBOR.
|
|
610
|
+
*
|
|
611
|
+
* This is a convenience method that first parses the binary data into a
|
|
612
|
+
* CBOR value, then uses `fromUntaggedCbor()` to decode it.
|
|
613
|
+
*
|
|
614
|
+
* @param data - Binary CBOR data
|
|
615
|
+
* @returns Decoded instance
|
|
616
|
+
* @throws Error if the data cannot be parsed or decoded
|
|
617
|
+
*/
|
|
618
|
+
fromUntaggedCborData?(data: Uint8Array): T;
|
|
619
|
+
}
|
|
620
|
+
/**
|
|
621
|
+
* Helper function to validate that a CBOR value has one of the expected tags.
|
|
622
|
+
*
|
|
623
|
+
* @param cbor - CBOR value to validate
|
|
624
|
+
* @param expectedTags - Array of valid tags
|
|
625
|
+
* @returns The matching tag
|
|
626
|
+
* @throws Error if the value is not tagged or has an unexpected tag
|
|
627
|
+
*/
|
|
628
|
+
declare const validateTag: (cbor: Cbor, expectedTags: Tag[]) => Tag;
|
|
629
|
+
/**
|
|
630
|
+
* Helper function to extract the content from a tagged CBOR value.
|
|
631
|
+
*
|
|
632
|
+
* @param cbor - Tagged CBOR value
|
|
633
|
+
* @returns The untagged content
|
|
634
|
+
* @throws Error if the value is not tagged
|
|
635
|
+
*/
|
|
636
|
+
declare const extractTaggedContent: (cbor: Cbor) => Cbor;
|
|
637
|
+
//#endregion
|
|
638
|
+
//#region src/cbor-tagged-codable.d.ts
|
|
639
|
+
/**
|
|
640
|
+
* Interface for types that can be both encoded to and decoded from CBOR with a
|
|
641
|
+
* specific tag.
|
|
642
|
+
*
|
|
643
|
+
* This interface is automatically implemented for any type that implements both
|
|
644
|
+
* `CborTaggedEncodable` and `CborTaggedDecodable`. It serves as a convenience
|
|
645
|
+
* marker to indicate full-tagged CBOR serialization support.
|
|
646
|
+
*
|
|
647
|
+
* @example
|
|
648
|
+
* ```typescript
|
|
649
|
+
* // Define a Date type
|
|
650
|
+
* class Date implements CborTaggedCodable<Date> {
|
|
651
|
+
* constructor(public timestamp: number) {}
|
|
652
|
+
*
|
|
653
|
+
* cborTags(): Tag[] {
|
|
654
|
+
* return [createTag(1, 'date')]; // Standard date tag
|
|
655
|
+
* }
|
|
656
|
+
*
|
|
657
|
+
* // Implement encoding to tagged CBOR
|
|
658
|
+
* untaggedCbor(): Cbor {
|
|
659
|
+
* return cbor(this.timestamp);
|
|
660
|
+
* }
|
|
661
|
+
*
|
|
662
|
+
* taggedCbor(): Cbor {
|
|
663
|
+
* const tags = this.cborTags();
|
|
664
|
+
* return {
|
|
665
|
+
* isCbor: true,
|
|
666
|
+
* type: MajorType.Tagged,
|
|
667
|
+
* tag: tags[0].value,
|
|
668
|
+
* value: this.untaggedCbor()
|
|
669
|
+
* };
|
|
670
|
+
* }
|
|
671
|
+
*
|
|
672
|
+
* // Implement decoding from tagged CBOR
|
|
673
|
+
* fromUntaggedCbor(cbor: Cbor): Date {
|
|
674
|
+
* const timestamp = cbor.value as number;
|
|
675
|
+
* return new Date(timestamp);
|
|
676
|
+
* }
|
|
677
|
+
*
|
|
678
|
+
* fromTaggedCbor(cbor: Cbor): Date {
|
|
679
|
+
* if (cbor.type !== MajorType.Tagged) {
|
|
680
|
+
* throw new Error('Wrong type');
|
|
681
|
+
* }
|
|
682
|
+
* return this.fromUntaggedCbor(cbor.value);
|
|
683
|
+
* }
|
|
684
|
+
* }
|
|
685
|
+
*
|
|
686
|
+
* // The CborTaggedCodable interface is automatically implemented
|
|
687
|
+
* // Create a date and demonstrate round-trip conversion
|
|
688
|
+
* const original = new Date(1609459200);
|
|
689
|
+
* const cborValue = original.taggedCbor();
|
|
690
|
+
* const roundtrip = new Date(0).fromTaggedCbor(cborValue);
|
|
691
|
+
* assert(original.timestamp === roundtrip.timestamp);
|
|
692
|
+
* ```
|
|
693
|
+
*/
|
|
694
|
+
interface CborTaggedCodable<T> extends CborTaggedEncodable, CborTaggedDecodable<T> {}
|
|
695
|
+
//#endregion
|
|
696
|
+
//#region src/cbor-tagged.d.ts
|
|
697
|
+
/**
|
|
698
|
+
* Interface for types that have associated CBOR tags.
|
|
699
|
+
*
|
|
700
|
+
* In CBOR, tags provide semantic information about how to interpret data
|
|
701
|
+
* items. This interface defines which CBOR tag(s) are associated with a particular
|
|
702
|
+
* TypeScript type.
|
|
703
|
+
*
|
|
704
|
+
* Implementing this interface is a prerequisite for implementing
|
|
705
|
+
* `CborTaggedEncodable` and `CborTaggedDecodable`.
|
|
706
|
+
*
|
|
707
|
+
* ## Multiple Tags for Backward Compatibility
|
|
708
|
+
*
|
|
709
|
+
* The `cborTags()` method returns an array of tags, enabling support for
|
|
710
|
+
* backward compatibility with older tag versions:
|
|
711
|
+
*
|
|
712
|
+
* - **When encoding**: Only the first tag in the array is used for
|
|
713
|
+
* serialization
|
|
714
|
+
* - **When decoding**: Any of the tags in the array will be accepted
|
|
715
|
+
*
|
|
716
|
+
* This design solves several real-world problems:
|
|
717
|
+
*
|
|
718
|
+
* 1. **IANA Registration Simplification**: If you initially choose a tag in
|
|
719
|
+
* the Specification Required range (24-32767) and later want to move to the
|
|
720
|
+
* simpler First Come, First Served range (32768+), you can migrate while
|
|
721
|
+
* maintaining compatibility with existing data.
|
|
722
|
+
*
|
|
723
|
+
* 2. **Protocol Evolution**: As your protocol evolves, you can introduce new
|
|
724
|
+
* preferred tags while still supporting data encoded with older tags.
|
|
725
|
+
*
|
|
726
|
+
* 3. **Versioning**: Different tags can represent different versions of your
|
|
727
|
+
* data format while sharing the same TypeScript type for handling.
|
|
728
|
+
*
|
|
729
|
+
* @example
|
|
730
|
+
* ```typescript
|
|
731
|
+
* // Single tag
|
|
732
|
+
* class Date implements CborTagged {
|
|
733
|
+
* cborTags(): Tag[] {
|
|
734
|
+
* return [createTag(1, 'date')];
|
|
735
|
+
* }
|
|
736
|
+
* }
|
|
737
|
+
*
|
|
738
|
+
* // Multiple tags for backward compatibility
|
|
739
|
+
* class Seed implements CborTagged {
|
|
740
|
+
* cborTags(): Tag[] {
|
|
741
|
+
* return [
|
|
742
|
+
* createTag(40300, 'seed'), // Primary tag (used for encoding)
|
|
743
|
+
* createTag(300, 'seed-legacy'), // Legacy tag (accepted for decoding)
|
|
744
|
+
* ];
|
|
745
|
+
* }
|
|
746
|
+
* }
|
|
747
|
+
* ```
|
|
748
|
+
*/
|
|
749
|
+
interface CborTagged {
|
|
750
|
+
/**
|
|
751
|
+
* Returns the CBOR tags associated with this type.
|
|
752
|
+
*
|
|
753
|
+
* This method should return an array of tags in order of preference:
|
|
754
|
+
*
|
|
755
|
+
* - The first tag in the array is the "preferred" tag and will be used
|
|
756
|
+
* when encoding values of this type via
|
|
757
|
+
* `CborTaggedEncodable.taggedCbor()`.
|
|
758
|
+
*
|
|
759
|
+
* - All tags in the array are considered equivalent for decoding. When
|
|
760
|
+
* `CborTaggedDecodable.fromTaggedCbor()` is called, any tag in this
|
|
761
|
+
* array will be accepted as valid for this type.
|
|
762
|
+
*
|
|
763
|
+
* This design enables backward compatibility: you can introduce a new tag
|
|
764
|
+
* (placed first in the array) while still supporting older tags for
|
|
765
|
+
* decoding.
|
|
766
|
+
*
|
|
767
|
+
* For standard CBOR tags, you can use predefined tag constants from the
|
|
768
|
+
* `tags` module, or create custom tags with `createTag()`.
|
|
769
|
+
*/
|
|
770
|
+
cborTags(): Tag[];
|
|
771
|
+
}
|
|
772
|
+
//#endregion
|
|
773
|
+
//#region src/date.d.ts
|
|
774
|
+
/**
|
|
775
|
+
* A CBOR-friendly representation of a date and time.
|
|
776
|
+
*
|
|
777
|
+
* The `CborDate` type provides a wrapper around JavaScript's native `Date` that
|
|
778
|
+
* supports encoding and decoding to/from CBOR with tag 1, following the CBOR
|
|
779
|
+
* date/time standard specified in RFC 8949.
|
|
780
|
+
*
|
|
781
|
+
* When encoded to CBOR, dates are represented as tag 1 followed by a numeric
|
|
782
|
+
* value representing the number of seconds since (or before) the Unix epoch
|
|
783
|
+
* (1970-01-01T00:00:00Z). The numeric value can be a positive or negative
|
|
784
|
+
* integer, or a floating-point value for dates with fractional seconds.
|
|
785
|
+
*
|
|
786
|
+
* # Features
|
|
787
|
+
*
|
|
788
|
+
* - Supports UTC dates with optional fractional seconds
|
|
789
|
+
* - Provides convenient constructors for common date creation patterns
|
|
790
|
+
* - Implements the `CborTagged`, `CborTaggedEncodable`, and
|
|
791
|
+
* `CborTaggedDecodable` interfaces
|
|
792
|
+
* - Supports arithmetic operations with durations and between dates
|
|
793
|
+
*
|
|
794
|
+
* @example
|
|
795
|
+
* ```typescript
|
|
796
|
+
* import { CborDate } from './date';
|
|
797
|
+
*
|
|
798
|
+
* // Create a date from a timestamp (seconds since Unix epoch)
|
|
799
|
+
* const date = CborDate.fromTimestamp(1675854714.0);
|
|
800
|
+
*
|
|
801
|
+
* // Create a date from year, month, day
|
|
802
|
+
* const date2 = CborDate.fromYmd(2023, 2, 8);
|
|
803
|
+
*
|
|
804
|
+
* // Convert to CBOR
|
|
805
|
+
* const cborValue = date.taggedCbor();
|
|
806
|
+
*
|
|
807
|
+
* // Decode from CBOR
|
|
808
|
+
* const decoded = CborDate.fromTaggedCbor(cborValue);
|
|
809
|
+
* ```
|
|
810
|
+
*/
|
|
811
|
+
declare class CborDate implements CborTagged, CborTaggedEncodable, CborTaggedDecodable<CborDate> {
|
|
812
|
+
#private;
|
|
813
|
+
/**
|
|
814
|
+
* Creates a new `CborDate` from the given JavaScript `Date`.
|
|
815
|
+
*
|
|
816
|
+
* This method creates a new `CborDate` instance by wrapping a
|
|
817
|
+
* JavaScript `Date`.
|
|
818
|
+
*
|
|
819
|
+
* @param dateTime - A `Date` instance to wrap
|
|
820
|
+
*
|
|
821
|
+
* @returns A new `CborDate` instance
|
|
822
|
+
*
|
|
823
|
+
* @example
|
|
824
|
+
* ```typescript
|
|
825
|
+
* const datetime = new Date();
|
|
826
|
+
* const date = CborDate.fromDatetime(datetime);
|
|
827
|
+
* ```
|
|
828
|
+
*/
|
|
829
|
+
static fromDatetime(dateTime: Date): CborDate;
|
|
830
|
+
/**
|
|
831
|
+
* Creates a new `CborDate` from year, month, and day components.
|
|
832
|
+
*
|
|
833
|
+
* This method creates a new `CborDate` with the time set to 00:00:00 UTC.
|
|
834
|
+
*
|
|
835
|
+
* @param year - The year component (e.g., 2023)
|
|
836
|
+
* @param month - The month component (1-12)
|
|
837
|
+
* @param day - The day component (1-31)
|
|
838
|
+
*
|
|
839
|
+
* @returns A new `CborDate` instance
|
|
840
|
+
*
|
|
841
|
+
* @example
|
|
842
|
+
* ```typescript
|
|
843
|
+
* // Create February 8, 2023
|
|
844
|
+
* const date = CborDate.fromYmd(2023, 2, 8);
|
|
845
|
+
* ```
|
|
846
|
+
*
|
|
847
|
+
* @throws Error if the provided components do not form a valid date.
|
|
848
|
+
*/
|
|
849
|
+
static fromYmd(year: number, month: number, day: number): CborDate;
|
|
850
|
+
/**
|
|
851
|
+
* Creates a new `CborDate` from year, month, day, hour, minute, and second
|
|
852
|
+
* components.
|
|
853
|
+
*
|
|
854
|
+
* @param year - The year component (e.g., 2023)
|
|
855
|
+
* @param month - The month component (1-12)
|
|
856
|
+
* @param day - The day component (1-31)
|
|
857
|
+
* @param hour - The hour component (0-23)
|
|
858
|
+
* @param minute - The minute component (0-59)
|
|
859
|
+
* @param second - The second component (0-59)
|
|
860
|
+
*
|
|
861
|
+
* @returns A new `CborDate` instance
|
|
862
|
+
*
|
|
863
|
+
* @example
|
|
864
|
+
* ```typescript
|
|
865
|
+
* // Create February 8, 2023, 15:30:45 UTC
|
|
866
|
+
* const date = CborDate.fromYmdHms(2023, 2, 8, 15, 30, 45);
|
|
867
|
+
* ```
|
|
868
|
+
*
|
|
869
|
+
* @throws Error if the provided components do not form a valid date and time.
|
|
870
|
+
*/
|
|
871
|
+
static fromYmdHms(year: number, month: number, day: number, hour: number, minute: number, second: number): CborDate;
|
|
872
|
+
/**
|
|
873
|
+
* Creates a new `CborDate` from seconds since (or before) the Unix epoch.
|
|
874
|
+
*
|
|
875
|
+
* This method creates a new `CborDate` representing the specified number of
|
|
876
|
+
* seconds since the Unix epoch (1970-01-01T00:00:00Z). Negative values
|
|
877
|
+
* represent times before the epoch.
|
|
878
|
+
*
|
|
879
|
+
* @param secondsSinceUnixEpoch - Seconds from the Unix epoch (positive or
|
|
880
|
+
* negative), which can include a fractional part for sub-second
|
|
881
|
+
* precision
|
|
882
|
+
*
|
|
883
|
+
* @returns A new `CborDate` instance
|
|
884
|
+
*
|
|
885
|
+
* @example
|
|
886
|
+
* ```typescript
|
|
887
|
+
* // Create a date from a timestamp
|
|
888
|
+
* const date = CborDate.fromTimestamp(1675854714.0);
|
|
889
|
+
*
|
|
890
|
+
* // Create a date one second before the Unix epoch
|
|
891
|
+
* const beforeEpoch = CborDate.fromTimestamp(-1.0);
|
|
892
|
+
*
|
|
893
|
+
* // Create a date with fractional seconds
|
|
894
|
+
* const withFraction = CborDate.fromTimestamp(1675854714.5);
|
|
895
|
+
* ```
|
|
896
|
+
*/
|
|
897
|
+
static fromTimestamp(secondsSinceUnixEpoch: number): CborDate;
|
|
898
|
+
/**
|
|
899
|
+
* Creates a new `CborDate` from a string containing an ISO-8601 (RFC-3339)
|
|
900
|
+
* date (with or without time).
|
|
901
|
+
*
|
|
902
|
+
* This method parses a string representation of a date or date-time in
|
|
903
|
+
* ISO-8601/RFC-3339 format and creates a new `CborDate` instance. It
|
|
904
|
+
* supports both full date-time strings (e.g., "2023-02-08T15:30:45Z")
|
|
905
|
+
* and date-only strings (e.g., "2023-02-08").
|
|
906
|
+
*
|
|
907
|
+
* @param value - A string containing a date or date-time in ISO-8601/RFC-3339
|
|
908
|
+
* format
|
|
909
|
+
*
|
|
910
|
+
* @returns A new `CborDate` instance if parsing succeeds
|
|
911
|
+
*
|
|
912
|
+
* @throws Error if the string cannot be parsed as a valid date or date-time
|
|
913
|
+
*
|
|
914
|
+
* @example
|
|
915
|
+
* ```typescript
|
|
916
|
+
* // Parse a date-time string
|
|
917
|
+
* const date = CborDate.fromString("2023-02-08T15:30:45Z");
|
|
918
|
+
*
|
|
919
|
+
* // Parse a date-only string (time will be set to 00:00:00)
|
|
920
|
+
* const date2 = CborDate.fromString("2023-02-08");
|
|
921
|
+
* ```
|
|
922
|
+
*/
|
|
923
|
+
static fromString(value: string): CborDate;
|
|
924
|
+
/**
|
|
925
|
+
* Creates a new `CborDate` containing the current date and time.
|
|
926
|
+
*
|
|
927
|
+
* @returns A new `CborDate` instance representing the current UTC date and time
|
|
928
|
+
*
|
|
929
|
+
* @example
|
|
930
|
+
* ```typescript
|
|
931
|
+
* const now = CborDate.now();
|
|
932
|
+
* ```
|
|
933
|
+
*/
|
|
934
|
+
static now(): CborDate;
|
|
935
|
+
/**
|
|
936
|
+
* Creates a new `CborDate` containing the current date and time plus the given
|
|
937
|
+
* duration.
|
|
938
|
+
*
|
|
939
|
+
* @param durationMs - The duration in milliseconds to add to the current time
|
|
940
|
+
*
|
|
941
|
+
* @returns A new `CborDate` instance representing the current UTC date and time plus
|
|
942
|
+
* the duration
|
|
943
|
+
*
|
|
944
|
+
* @example
|
|
945
|
+
* ```typescript
|
|
946
|
+
* // Get a date 1 hour from now
|
|
947
|
+
* const oneHourLater = CborDate.withDurationFromNow(3600 * 1000);
|
|
948
|
+
* ```
|
|
949
|
+
*/
|
|
950
|
+
static withDurationFromNow(durationMs: number): CborDate;
|
|
951
|
+
/**
|
|
952
|
+
* Returns the underlying JavaScript `Date` object.
|
|
953
|
+
*
|
|
954
|
+
* This method provides access to the wrapped JavaScript `Date`
|
|
955
|
+
* instance.
|
|
956
|
+
*
|
|
957
|
+
* @returns The wrapped `Date` instance
|
|
958
|
+
*
|
|
959
|
+
* @example
|
|
960
|
+
* ```typescript
|
|
961
|
+
* const date = CborDate.now();
|
|
962
|
+
* const datetime = date.datetime();
|
|
963
|
+
* const year = datetime.getFullYear();
|
|
964
|
+
* ```
|
|
965
|
+
*/
|
|
966
|
+
datetime(): Date;
|
|
967
|
+
/**
|
|
968
|
+
* Returns the `CborDate` as the number of seconds since the Unix epoch.
|
|
969
|
+
*
|
|
970
|
+
* This method converts the date to a floating-point number representing
|
|
971
|
+
* the number of seconds since the Unix epoch (1970-01-01T00:00:00Z).
|
|
972
|
+
* Negative values represent times before the epoch. The fractional
|
|
973
|
+
* part represents sub-second precision.
|
|
974
|
+
*
|
|
975
|
+
* @returns Seconds since the Unix epoch as a `number`
|
|
976
|
+
*
|
|
977
|
+
* @example
|
|
978
|
+
* ```typescript
|
|
979
|
+
* const date = CborDate.fromYmd(2023, 2, 8);
|
|
980
|
+
* const timestamp = date.timestamp();
|
|
981
|
+
* ```
|
|
982
|
+
*/
|
|
983
|
+
timestamp(): number;
|
|
984
|
+
/**
|
|
985
|
+
* Add seconds to this date.
|
|
986
|
+
*
|
|
987
|
+
* @param seconds - Seconds to add (can be fractional)
|
|
988
|
+
* @returns New CborDate instance
|
|
989
|
+
*
|
|
990
|
+
* @example
|
|
991
|
+
* ```typescript
|
|
992
|
+
* const date = CborDate.fromYmd(2022, 3, 21);
|
|
993
|
+
* const tomorrow = date.add(24 * 60 * 60);
|
|
994
|
+
* ```
|
|
995
|
+
*/
|
|
996
|
+
add(seconds: number): CborDate;
|
|
997
|
+
/**
|
|
998
|
+
* Subtract seconds from this date.
|
|
999
|
+
*
|
|
1000
|
+
* @param seconds - Seconds to subtract (can be fractional)
|
|
1001
|
+
* @returns New CborDate instance
|
|
1002
|
+
*
|
|
1003
|
+
* @example
|
|
1004
|
+
* ```typescript
|
|
1005
|
+
* const date = CborDate.fromYmd(2022, 3, 21);
|
|
1006
|
+
* const yesterday = date.subtract(24 * 60 * 60);
|
|
1007
|
+
* ```
|
|
1008
|
+
*/
|
|
1009
|
+
subtract(seconds: number): CborDate;
|
|
1010
|
+
/**
|
|
1011
|
+
* Get the difference in seconds between this date and another.
|
|
1012
|
+
*
|
|
1013
|
+
* @param other - Other CborDate to compare with
|
|
1014
|
+
* @returns Difference in seconds (this - other)
|
|
1015
|
+
*
|
|
1016
|
+
* @example
|
|
1017
|
+
* ```typescript
|
|
1018
|
+
* const date1 = CborDate.fromYmd(2022, 3, 22);
|
|
1019
|
+
* const date2 = CborDate.fromYmd(2022, 3, 21);
|
|
1020
|
+
* const diff = date1.difference(date2);
|
|
1021
|
+
* // Returns 86400 (one day in seconds)
|
|
1022
|
+
* ```
|
|
1023
|
+
*/
|
|
1024
|
+
difference(other: CborDate): number;
|
|
1025
|
+
/**
|
|
1026
|
+
* Implementation of the `CborTagged` interface for `CborDate`.
|
|
1027
|
+
*
|
|
1028
|
+
* This implementation specifies that `CborDate` values are tagged with CBOR tag 1,
|
|
1029
|
+
* which is the standard CBOR tag for date/time values represented as seconds
|
|
1030
|
+
* since the Unix epoch per RFC 8949.
|
|
1031
|
+
*
|
|
1032
|
+
* @returns A vector containing tag 1
|
|
1033
|
+
*/
|
|
1034
|
+
cborTags(): Tag[];
|
|
1035
|
+
/**
|
|
1036
|
+
* Implementation of the `CborTaggedEncodable` interface for `CborDate`.
|
|
1037
|
+
*
|
|
1038
|
+
* Converts this `CborDate` to an untagged CBOR value.
|
|
1039
|
+
*
|
|
1040
|
+
* The date is converted to a numeric value representing the number of
|
|
1041
|
+
* seconds since the Unix epoch. This value may be an integer or a
|
|
1042
|
+
* floating-point number, depending on whether the date has fractional
|
|
1043
|
+
* seconds.
|
|
1044
|
+
*
|
|
1045
|
+
* @returns A CBOR value representing the timestamp
|
|
1046
|
+
*/
|
|
1047
|
+
untaggedCbor(): Cbor;
|
|
1048
|
+
/**
|
|
1049
|
+
* Converts this `CborDate` to a tagged CBOR value with tag 1.
|
|
1050
|
+
*
|
|
1051
|
+
* @returns Tagged CBOR value
|
|
1052
|
+
*/
|
|
1053
|
+
taggedCbor(): Cbor;
|
|
1054
|
+
/**
|
|
1055
|
+
* Implementation of the `CborTaggedDecodable` interface for `CborDate`.
|
|
1056
|
+
*
|
|
1057
|
+
* Creates a `CborDate` from an untagged CBOR value.
|
|
1058
|
+
*
|
|
1059
|
+
* The CBOR value must be a numeric value (integer or floating-point)
|
|
1060
|
+
* representing the number of seconds since the Unix epoch.
|
|
1061
|
+
*
|
|
1062
|
+
* @param cbor - The untagged CBOR value
|
|
1063
|
+
*
|
|
1064
|
+
* @returns This CborDate instance (mutated)
|
|
1065
|
+
*
|
|
1066
|
+
* @throws Error if the CBOR value is not a valid timestamp
|
|
1067
|
+
*/
|
|
1068
|
+
fromUntaggedCbor(cbor: Cbor): CborDate;
|
|
1069
|
+
/**
|
|
1070
|
+
* Creates a `CborDate` from a tagged CBOR value with tag 1.
|
|
1071
|
+
*
|
|
1072
|
+
* @param cbor - Tagged CBOR value
|
|
1073
|
+
*
|
|
1074
|
+
* @returns This CborDate instance (mutated)
|
|
1075
|
+
*
|
|
1076
|
+
* @throws Error if the CBOR value has the wrong tag or cannot be decoded
|
|
1077
|
+
*/
|
|
1078
|
+
fromTaggedCbor(cbor: Cbor): CborDate;
|
|
1079
|
+
/**
|
|
1080
|
+
* Static method to create a CborDate from tagged CBOR.
|
|
1081
|
+
*
|
|
1082
|
+
* @param cbor - Tagged CBOR value
|
|
1083
|
+
* @returns New CborDate instance
|
|
1084
|
+
*/
|
|
1085
|
+
static fromTaggedCbor(cbor: Cbor): CborDate;
|
|
1086
|
+
/**
|
|
1087
|
+
* Static method to create a CborDate from untagged CBOR.
|
|
1088
|
+
*
|
|
1089
|
+
* @param cbor - Untagged CBOR value
|
|
1090
|
+
* @returns New CborDate instance
|
|
1091
|
+
*/
|
|
1092
|
+
static fromUntaggedCbor(cbor: Cbor): CborDate;
|
|
1093
|
+
/**
|
|
1094
|
+
* Implementation of the `toString` method for `CborDate`.
|
|
1095
|
+
*
|
|
1096
|
+
* This implementation provides a string representation of a `CborDate` in ISO-8601
|
|
1097
|
+
* format. For dates with time exactly at midnight (00:00:00), only the date
|
|
1098
|
+
* part is shown. For other times, a full date-time string is shown.
|
|
1099
|
+
*
|
|
1100
|
+
* @returns String representation in ISO-8601 format
|
|
1101
|
+
*
|
|
1102
|
+
* @example
|
|
1103
|
+
* ```typescript
|
|
1104
|
+
* // A date at midnight will display as just the date
|
|
1105
|
+
* const date = CborDate.fromYmd(2023, 2, 8);
|
|
1106
|
+
* // Returns "2023-02-08"
|
|
1107
|
+
* console.log(date.toString());
|
|
1108
|
+
*
|
|
1109
|
+
* // A date with time will display as date and time
|
|
1110
|
+
* const date2 = CborDate.fromYmdHms(2023, 2, 8, 15, 30, 45);
|
|
1111
|
+
* // Returns "2023-02-08T15:30:45.000Z"
|
|
1112
|
+
* console.log(date2.toString());
|
|
1113
|
+
* ```
|
|
1114
|
+
*/
|
|
1115
|
+
toString(): string;
|
|
1116
|
+
/**
|
|
1117
|
+
* Compare two dates for equality.
|
|
1118
|
+
*
|
|
1119
|
+
* @param other - Other CborDate to compare
|
|
1120
|
+
* @returns true if dates represent the same moment in time
|
|
1121
|
+
*/
|
|
1122
|
+
equals(other: CborDate): boolean;
|
|
1123
|
+
/**
|
|
1124
|
+
* Compare two dates.
|
|
1125
|
+
*
|
|
1126
|
+
* @param other - Other CborDate to compare
|
|
1127
|
+
* @returns -1 if this < other, 0 if equal, 1 if this > other
|
|
1128
|
+
*/
|
|
1129
|
+
compare(other: CborDate): number;
|
|
1130
|
+
/**
|
|
1131
|
+
* Convert to JSON (returns ISO 8601 string).
|
|
1132
|
+
*
|
|
1133
|
+
* @returns ISO 8601 string
|
|
1134
|
+
*/
|
|
1135
|
+
toJSON(): string;
|
|
1136
|
+
private constructor();
|
|
1137
|
+
}
|
|
1138
|
+
//#endregion
|
|
1139
|
+
//#region src/tags-store.d.ts
|
|
1140
|
+
/**
|
|
1141
|
+
* Function type for custom CBOR value summarizers.
|
|
1142
|
+
*
|
|
1143
|
+
* Summarizers provide custom string representations for tagged values.
|
|
1144
|
+
*
|
|
1145
|
+
* @param cbor - The CBOR value to summarize
|
|
1146
|
+
* @param flat - If true, produce single-line output
|
|
1147
|
+
* @returns String summary of the value
|
|
1148
|
+
*/
|
|
1149
|
+
type CborSummarizer = (cbor: Cbor, flat: boolean) => string;
|
|
1150
|
+
/**
|
|
1151
|
+
* Interface for tag store operations.
|
|
1152
|
+
*/
|
|
1153
|
+
interface TagsStoreTrait {
|
|
1154
|
+
/**
|
|
1155
|
+
* Get the assigned name for a tag, if any.
|
|
1156
|
+
*
|
|
1157
|
+
* @param tag - The tag to look up
|
|
1158
|
+
* @returns The assigned name, or undefined if no name is registered
|
|
1159
|
+
*/
|
|
1160
|
+
assignedNameForTag(tag: Tag): string | undefined;
|
|
1161
|
+
/**
|
|
1162
|
+
* Get a display name for a tag.
|
|
1163
|
+
*
|
|
1164
|
+
* @param tag - The tag to get a name for
|
|
1165
|
+
* @returns The assigned name if available, otherwise the tag value as a string
|
|
1166
|
+
*/
|
|
1167
|
+
nameForTag(tag: Tag): string;
|
|
1168
|
+
/**
|
|
1169
|
+
* Look up a tag by its numeric value.
|
|
1170
|
+
*
|
|
1171
|
+
* @param value - The numeric tag value
|
|
1172
|
+
* @returns The Tag object if found, undefined otherwise
|
|
1173
|
+
*/
|
|
1174
|
+
tagForValue(value: CborNumber): Tag | undefined;
|
|
1175
|
+
/**
|
|
1176
|
+
* Look up a tag by its name.
|
|
1177
|
+
*
|
|
1178
|
+
* @param name - The tag name
|
|
1179
|
+
* @returns The Tag object if found, undefined otherwise
|
|
1180
|
+
*/
|
|
1181
|
+
tagForName(name: string): Tag | undefined;
|
|
1182
|
+
/**
|
|
1183
|
+
* Get a display name for a tag value.
|
|
1184
|
+
*
|
|
1185
|
+
* @param value - The numeric tag value
|
|
1186
|
+
* @returns The tag name if registered, otherwise the value as a string
|
|
1187
|
+
*/
|
|
1188
|
+
nameForValue(value: CborNumber): string;
|
|
1189
|
+
/**
|
|
1190
|
+
* Get a custom summarizer function for a tag, if registered.
|
|
1191
|
+
*
|
|
1192
|
+
* @param tag - The numeric tag value
|
|
1193
|
+
* @returns The summarizer function if registered, undefined otherwise
|
|
1194
|
+
*/
|
|
1195
|
+
summarizer(tag: CborNumber): CborSummarizer | undefined;
|
|
1196
|
+
}
|
|
1197
|
+
/**
|
|
1198
|
+
* Tag registry implementation.
|
|
1199
|
+
*
|
|
1200
|
+
* Stores tags with their names and optional summarizer functions.
|
|
1201
|
+
*/
|
|
1202
|
+
declare class TagsStore implements TagsStoreTrait {
|
|
1203
|
+
#private;
|
|
1204
|
+
constructor();
|
|
1205
|
+
/**
|
|
1206
|
+
* Insert a tag into the registry.
|
|
1207
|
+
*
|
|
1208
|
+
* @param tag - The tag to register
|
|
1209
|
+
*
|
|
1210
|
+
* @example
|
|
1211
|
+
* ```typescript
|
|
1212
|
+
* const store = new TagsStore();
|
|
1213
|
+
* store.insert(createTag(12345, 'myCustomTag'));
|
|
1214
|
+
* ```
|
|
1215
|
+
*/
|
|
1216
|
+
insert(tag: Tag): void;
|
|
1217
|
+
/**
|
|
1218
|
+
* Insert multiple tags into the registry.
|
|
1219
|
+
* Matches Rust's insert_all() method.
|
|
1220
|
+
*
|
|
1221
|
+
* @param tags - Array of tags to register
|
|
1222
|
+
*
|
|
1223
|
+
* @example
|
|
1224
|
+
* ```typescript
|
|
1225
|
+
* const store = new TagsStore();
|
|
1226
|
+
* store.insertAll([
|
|
1227
|
+
* createTag(1, 'date'),
|
|
1228
|
+
* createTag(100, 'custom')
|
|
1229
|
+
* ]);
|
|
1230
|
+
* ```
|
|
1231
|
+
*/
|
|
1232
|
+
insertAll(tags: Tag[]): void;
|
|
1233
|
+
/**
|
|
1234
|
+
* Register a custom summarizer function for a tag.
|
|
1235
|
+
*
|
|
1236
|
+
* @param tagValue - The numeric tag value
|
|
1237
|
+
* @param summarizer - The summarizer function
|
|
1238
|
+
*
|
|
1239
|
+
* @example
|
|
1240
|
+
* ```typescript
|
|
1241
|
+
* store.setSummarizer(1, (cbor, flat) => {
|
|
1242
|
+
* // Custom date formatting
|
|
1243
|
+
* return `Date(${extractCbor(cbor)})`;
|
|
1244
|
+
* });
|
|
1245
|
+
* ```
|
|
1246
|
+
*/
|
|
1247
|
+
setSummarizer(tagValue: CborNumber, summarizer: CborSummarizer): void;
|
|
1248
|
+
/**
|
|
1249
|
+
* Remove a tag from the registry.
|
|
1250
|
+
*
|
|
1251
|
+
* @param tagValue - The numeric tag value to remove
|
|
1252
|
+
* @returns true if a tag was removed, false otherwise
|
|
1253
|
+
*/
|
|
1254
|
+
remove(tagValue: CborNumber): boolean;
|
|
1255
|
+
assignedNameForTag(tag: Tag): string | undefined;
|
|
1256
|
+
nameForTag(tag: Tag): string;
|
|
1257
|
+
tagForValue(value: CborNumber): Tag | undefined;
|
|
1258
|
+
tagForName(name: string): Tag | undefined;
|
|
1259
|
+
nameForValue(value: CborNumber): string;
|
|
1260
|
+
summarizer(tag: CborNumber): CborSummarizer | undefined;
|
|
1261
|
+
/**
|
|
1262
|
+
* Get all registered tags.
|
|
1263
|
+
*
|
|
1264
|
+
* @returns Array of all registered tags
|
|
1265
|
+
*/
|
|
1266
|
+
getAllTags(): Tag[];
|
|
1267
|
+
/**
|
|
1268
|
+
* Clear all registered tags and summarizers.
|
|
1269
|
+
*/
|
|
1270
|
+
clear(): void;
|
|
1271
|
+
/**
|
|
1272
|
+
* Get the number of registered tags.
|
|
1273
|
+
*
|
|
1274
|
+
* @returns Number of tags in the registry
|
|
1275
|
+
*/
|
|
1276
|
+
get size(): number;
|
|
1277
|
+
}
|
|
1278
|
+
/**
|
|
1279
|
+
* Get the global tags store instance.
|
|
1280
|
+
*
|
|
1281
|
+
* Creates the instance on first access.
|
|
1282
|
+
*
|
|
1283
|
+
* @returns The global TagsStore instance
|
|
1284
|
+
*
|
|
1285
|
+
* @example
|
|
1286
|
+
* ```typescript
|
|
1287
|
+
* const store = getGlobalTagsStore();
|
|
1288
|
+
* store.insert(createTag(999, 'myTag'));
|
|
1289
|
+
* ```
|
|
1290
|
+
*/
|
|
1291
|
+
declare const getGlobalTagsStore: () => TagsStore;
|
|
1292
|
+
//#endregion
|
|
1293
|
+
//#region src/walk.d.ts
|
|
1294
|
+
/**
|
|
1295
|
+
* Types of edges in the CBOR tree traversal.
|
|
1296
|
+
*/
|
|
1297
|
+
declare enum EdgeType {
|
|
1298
|
+
/** No specific edge type (root element) */
|
|
1299
|
+
None = "none",
|
|
1300
|
+
/** Element within an array */
|
|
1301
|
+
ArrayElement = "array_element",
|
|
1302
|
+
/** Key-value pair in a map (semantic unit) */
|
|
1303
|
+
MapKeyValue = "map_key_value",
|
|
1304
|
+
/** Key within a map */
|
|
1305
|
+
MapKey = "map_key",
|
|
1306
|
+
/** Value within a map */
|
|
1307
|
+
MapValue = "map_value",
|
|
1308
|
+
/** Content of a tagged value */
|
|
1309
|
+
TaggedContent = "tagged_content",
|
|
1310
|
+
}
|
|
1311
|
+
/**
|
|
1312
|
+
* Discriminated union for edge type information.
|
|
1313
|
+
*/
|
|
1314
|
+
type EdgeTypeVariant = {
|
|
1315
|
+
type: EdgeType.None;
|
|
1316
|
+
} | {
|
|
1317
|
+
type: EdgeType.ArrayElement;
|
|
1318
|
+
index: number;
|
|
1319
|
+
} | {
|
|
1320
|
+
type: EdgeType.MapKeyValue;
|
|
1321
|
+
} | {
|
|
1322
|
+
type: EdgeType.MapKey;
|
|
1323
|
+
} | {
|
|
1324
|
+
type: EdgeType.MapValue;
|
|
1325
|
+
} | {
|
|
1326
|
+
type: EdgeType.TaggedContent;
|
|
1327
|
+
};
|
|
1328
|
+
/**
|
|
1329
|
+
* Returns a short text label for the edge type, or undefined if no label is needed.
|
|
1330
|
+
*
|
|
1331
|
+
* This is primarily used for tree formatting to identify relationships between elements.
|
|
1332
|
+
*
|
|
1333
|
+
* @param edge - The edge type variant to get a label for
|
|
1334
|
+
* @returns Short label string, or undefined for None edge type
|
|
1335
|
+
*
|
|
1336
|
+
* @example
|
|
1337
|
+
* ```typescript
|
|
1338
|
+
* edgeLabel({ type: EdgeType.ArrayElement, index: 0 }); // Returns "arr[0]"
|
|
1339
|
+
* edgeLabel({ type: EdgeType.MapKeyValue }); // Returns "kv"
|
|
1340
|
+
* edgeLabel({ type: EdgeType.MapKey }); // Returns "key"
|
|
1341
|
+
* edgeLabel({ type: EdgeType.MapValue }); // Returns "val"
|
|
1342
|
+
* edgeLabel({ type: EdgeType.TaggedContent }); // Returns "content"
|
|
1343
|
+
* edgeLabel({ type: EdgeType.None }); // Returns undefined
|
|
1344
|
+
* ```
|
|
1345
|
+
*/
|
|
1346
|
+
declare const edgeLabel: (edge: EdgeTypeVariant) => string | undefined;
|
|
1347
|
+
/**
|
|
1348
|
+
* Element visited during tree traversal.
|
|
1349
|
+
* Can be either a single CBOR value or a key-value pair from a map.
|
|
1350
|
+
*/
|
|
1351
|
+
type WalkElement = {
|
|
1352
|
+
type: "single";
|
|
1353
|
+
cbor: Cbor;
|
|
1354
|
+
} | {
|
|
1355
|
+
type: "keyvalue";
|
|
1356
|
+
key: Cbor;
|
|
1357
|
+
value: Cbor;
|
|
1358
|
+
};
|
|
1359
|
+
/**
|
|
1360
|
+
* Helper functions for WalkElement
|
|
1361
|
+
*/
|
|
1362
|
+
/**
|
|
1363
|
+
* Returns the single CBOR element if this is a 'single' variant.
|
|
1364
|
+
*
|
|
1365
|
+
* @param element - The walk element to extract from
|
|
1366
|
+
* @returns The CBOR value if single, undefined otherwise
|
|
1367
|
+
*
|
|
1368
|
+
* @example
|
|
1369
|
+
* ```typescript
|
|
1370
|
+
* const element: WalkElement = { type: 'single', cbor: someCbor };
|
|
1371
|
+
* const cbor = asSingle(element); // Returns someCbor
|
|
1372
|
+
* ```
|
|
1373
|
+
*/
|
|
1374
|
+
declare const asSingle: (element: WalkElement) => Cbor | undefined;
|
|
1375
|
+
/**
|
|
1376
|
+
* Returns the key-value pair if this is a 'keyvalue' variant.
|
|
1377
|
+
*
|
|
1378
|
+
* @param element - The walk element to extract from
|
|
1379
|
+
* @returns Tuple of [key, value] if keyvalue, undefined otherwise
|
|
1380
|
+
*
|
|
1381
|
+
* @example
|
|
1382
|
+
* ```typescript
|
|
1383
|
+
* const element: WalkElement = { type: 'keyvalue', key: keyValue, value: valValue };
|
|
1384
|
+
* const pair = asKeyValue(element); // Returns [keyValue, valValue]
|
|
1385
|
+
* ```
|
|
1386
|
+
*/
|
|
1387
|
+
declare const asKeyValue: (element: WalkElement) => [Cbor, Cbor] | undefined;
|
|
1388
|
+
/**
|
|
1389
|
+
* Visitor function type with state threading.
|
|
1390
|
+
*
|
|
1391
|
+
* @template State - The type of state passed through the traversal
|
|
1392
|
+
* @param element - The element being visited
|
|
1393
|
+
* @param level - The depth level in the tree (0 = root)
|
|
1394
|
+
* @param edge - Information about the edge leading to this element
|
|
1395
|
+
* @param state - Current state value
|
|
1396
|
+
* @returns Tuple of [newState, stopDescent] where:
|
|
1397
|
+
* - newState: The updated state to pass to subsequent visits
|
|
1398
|
+
* - stopDescent: If true, don't descend into children of this element
|
|
1399
|
+
*/
|
|
1400
|
+
type Visitor<State> = (element: WalkElement, level: number, edge: EdgeTypeVariant, state: State) => [State, boolean];
|
|
1401
|
+
/**
|
|
1402
|
+
* Helper: Count all elements in a CBOR tree.
|
|
1403
|
+
*
|
|
1404
|
+
* @param cbor - The CBOR value to count
|
|
1405
|
+
* @returns Total number of elements visited
|
|
1406
|
+
*
|
|
1407
|
+
* @example
|
|
1408
|
+
* ```typescript
|
|
1409
|
+
* const structure = cbor([1, 2, [3, 4]]);
|
|
1410
|
+
* const count = countElements(structure);
|
|
1411
|
+
* console.log(count); // 6 (array, 1, 2, inner array, 3, 4)
|
|
1412
|
+
* ```
|
|
1413
|
+
*/
|
|
1414
|
+
declare const countElements: (cbor: Cbor) => number;
|
|
1415
|
+
/**
|
|
1416
|
+
* Helper: Collect all elements at a specific depth level.
|
|
1417
|
+
*
|
|
1418
|
+
* @param cbor - The CBOR value to traverse
|
|
1419
|
+
* @param targetLevel - The depth level to collect (0 = root)
|
|
1420
|
+
* @returns Array of CBOR values at the target level
|
|
1421
|
+
*
|
|
1422
|
+
* @example
|
|
1423
|
+
* ```typescript
|
|
1424
|
+
* const structure = cbor([[1, 2], [3, 4]]);
|
|
1425
|
+
* const level1 = collectAtLevel(structure, 1);
|
|
1426
|
+
* // Returns: [[1, 2], [3, 4]]
|
|
1427
|
+
* const level2 = collectAtLevel(structure, 2);
|
|
1428
|
+
* // Returns: [1, 2, 3, 4]
|
|
1429
|
+
* ```
|
|
1430
|
+
*/
|
|
1431
|
+
declare const collectAtLevel: (cbor: Cbor, targetLevel: number) => Cbor[];
|
|
1432
|
+
/**
|
|
1433
|
+
* Helper: Find first element matching a predicate.
|
|
1434
|
+
*
|
|
1435
|
+
* @template T - Type of extracted value
|
|
1436
|
+
* @param cbor - The CBOR value to search
|
|
1437
|
+
* @param predicate - Function to test each element
|
|
1438
|
+
* @returns First matching element, or undefined if not found
|
|
1439
|
+
*
|
|
1440
|
+
* @example
|
|
1441
|
+
* ```typescript
|
|
1442
|
+
* const structure = cbor({ users: [
|
|
1443
|
+
* { name: 'Alice', age: 30 },
|
|
1444
|
+
* { name: 'Bob', age: 25 }
|
|
1445
|
+
* ]});
|
|
1446
|
+
*
|
|
1447
|
+
* const bob = findFirst(structure, (element) => {
|
|
1448
|
+
* if (element.type === 'single' &&
|
|
1449
|
+
* element.cbor.type === MajorType.Text &&
|
|
1450
|
+
* element.cbor.value === 'Bob') {
|
|
1451
|
+
* return true;
|
|
1452
|
+
* }
|
|
1453
|
+
* return false;
|
|
1454
|
+
* });
|
|
1455
|
+
* ```
|
|
1456
|
+
*/
|
|
1457
|
+
declare const findFirst: (cbor: Cbor, predicate: (element: WalkElement) => boolean) => Cbor | undefined;
|
|
1458
|
+
/**
|
|
1459
|
+
* Helper: Collect all text strings in a CBOR tree.
|
|
1460
|
+
*
|
|
1461
|
+
* @param cbor - The CBOR value to traverse
|
|
1462
|
+
* @returns Array of all text string values found
|
|
1463
|
+
*
|
|
1464
|
+
* @example
|
|
1465
|
+
* ```typescript
|
|
1466
|
+
* const doc = cbor({
|
|
1467
|
+
* title: 'Document',
|
|
1468
|
+
* tags: ['urgent', 'draft'],
|
|
1469
|
+
* author: { name: 'Alice' }
|
|
1470
|
+
* });
|
|
1471
|
+
*
|
|
1472
|
+
* const texts = collectAllText(doc);
|
|
1473
|
+
* // Returns: ['Document', 'urgent', 'draft', 'Alice']
|
|
1474
|
+
* ```
|
|
1475
|
+
*/
|
|
1476
|
+
declare const collectAllText: (cbor: Cbor) => string[];
|
|
1477
|
+
/**
|
|
1478
|
+
* Helper: Get the maximum depth of a CBOR tree.
|
|
1479
|
+
*
|
|
1480
|
+
* @param cbor - The CBOR value to measure
|
|
1481
|
+
* @returns Maximum depth (0 for leaf values, 1+ for containers)
|
|
1482
|
+
*
|
|
1483
|
+
* @example
|
|
1484
|
+
* ```typescript
|
|
1485
|
+
* const flat = cbor([1, 2, 3]);
|
|
1486
|
+
* console.log(maxDepth(flat)); // 1
|
|
1487
|
+
*
|
|
1488
|
+
* const nested = cbor([[[1]]]);
|
|
1489
|
+
* console.log(maxDepth(nested)); // 3
|
|
1490
|
+
* ```
|
|
1491
|
+
*/
|
|
1492
|
+
declare const maxDepth: (cbor: Cbor) => number;
|
|
1493
|
+
//#endregion
|
|
1494
|
+
//#region src/cbor.d.ts
|
|
1495
|
+
declare const MajorType: {
|
|
1496
|
+
readonly Unsigned: 0;
|
|
1497
|
+
readonly Negative: 1;
|
|
1498
|
+
readonly ByteString: 2;
|
|
1499
|
+
readonly Text: 3;
|
|
1500
|
+
readonly Array: 4;
|
|
1501
|
+
readonly Map: 5;
|
|
1502
|
+
readonly Tagged: 6;
|
|
1503
|
+
readonly Simple: 7;
|
|
1504
|
+
};
|
|
1505
|
+
type MajorType = (typeof MajorType)[keyof typeof MajorType];
|
|
1506
|
+
/**
|
|
1507
|
+
* Numeric type that can be encoded in CBOR.
|
|
1508
|
+
*
|
|
1509
|
+
* Supports both standard JavaScript numbers and BigInt for large integers.
|
|
1510
|
+
* Numbers are automatically encoded as either unsigned or negative integers
|
|
1511
|
+
* depending on their value, following dCBOR canonical encoding rules.
|
|
1512
|
+
*
|
|
1513
|
+
* @example
|
|
1514
|
+
* ```typescript
|
|
1515
|
+
* const smallNum: CborNumber = 42;
|
|
1516
|
+
* const largeNum: CborNumber = 9007199254740992n;
|
|
1517
|
+
* ```
|
|
1518
|
+
*/
|
|
1519
|
+
type CborNumber = number | bigint;
|
|
1520
|
+
/**
|
|
1521
|
+
* Type for values that can be converted to CBOR.
|
|
1522
|
+
*
|
|
1523
|
+
* This is a comprehensive union type representing all values that can be encoded
|
|
1524
|
+
* as CBOR using the `cbor()` function. It includes:
|
|
1525
|
+
* - Already-encoded CBOR values (`Cbor`)
|
|
1526
|
+
* - Primitive types: numbers, bigints, strings, booleans, null, undefined
|
|
1527
|
+
* - Binary data: `Uint8Array`, `ByteString`
|
|
1528
|
+
* - Dates: `CborDate`
|
|
1529
|
+
* - Collections: `CborMap`, arrays, JavaScript `Map`, JavaScript `Set`
|
|
1530
|
+
* - Objects: Plain objects are converted to CBOR maps
|
|
1531
|
+
*
|
|
1532
|
+
* Matches Rust's `From<T>` trait implementations for CBOR.
|
|
1533
|
+
*
|
|
1534
|
+
* @example
|
|
1535
|
+
* ```typescript
|
|
1536
|
+
* cbor(42); // number
|
|
1537
|
+
* cbor("hello"); // string
|
|
1538
|
+
* cbor([1, 2, 3]); // array
|
|
1539
|
+
* cbor(new Map([["key", "value"]])); // Map
|
|
1540
|
+
* cbor({ name: "Alice", age: 30 }); // plain object -> CborMap
|
|
1541
|
+
* ```
|
|
1542
|
+
*/
|
|
1543
|
+
type CborInput = Cbor | CborNumber | string | boolean | null | undefined | Uint8Array | ByteString | CborDate | CborMap | CborInput[] | Map<unknown, unknown> | Set<unknown> | Record<string, unknown>;
|
|
1544
|
+
interface CborUnsignedType {
|
|
1545
|
+
readonly isCbor: true;
|
|
1546
|
+
readonly type: typeof MajorType.Unsigned;
|
|
1547
|
+
readonly value: CborNumber;
|
|
1548
|
+
}
|
|
1549
|
+
interface CborNegativeType {
|
|
1550
|
+
readonly isCbor: true;
|
|
1551
|
+
readonly type: typeof MajorType.Negative;
|
|
1552
|
+
readonly value: CborNumber;
|
|
1553
|
+
}
|
|
1554
|
+
interface CborByteStringType {
|
|
1555
|
+
readonly isCbor: true;
|
|
1556
|
+
readonly type: typeof MajorType.ByteString;
|
|
1557
|
+
readonly value: Uint8Array;
|
|
1558
|
+
}
|
|
1559
|
+
interface CborTextType {
|
|
1560
|
+
readonly isCbor: true;
|
|
1561
|
+
readonly type: typeof MajorType.Text;
|
|
1562
|
+
readonly value: string;
|
|
1563
|
+
}
|
|
1564
|
+
interface CborArrayType {
|
|
1565
|
+
readonly isCbor: true;
|
|
1566
|
+
readonly type: typeof MajorType.Array;
|
|
1567
|
+
readonly value: readonly Cbor[];
|
|
1568
|
+
}
|
|
1569
|
+
interface CborMapType {
|
|
1570
|
+
readonly isCbor: true;
|
|
1571
|
+
readonly type: typeof MajorType.Map;
|
|
1572
|
+
readonly value: CborMap;
|
|
1573
|
+
}
|
|
1574
|
+
interface CborTaggedType {
|
|
1575
|
+
readonly isCbor: true;
|
|
1576
|
+
readonly type: typeof MajorType.Tagged;
|
|
1577
|
+
readonly tag: CborNumber;
|
|
1578
|
+
readonly value: Cbor;
|
|
1579
|
+
}
|
|
1580
|
+
interface CborSimpleType {
|
|
1581
|
+
readonly isCbor: true;
|
|
1582
|
+
readonly type: typeof MajorType.Simple;
|
|
1583
|
+
readonly value: Simple;
|
|
1584
|
+
}
|
|
1585
|
+
interface CborMethods {
|
|
1586
|
+
toData(): Uint8Array;
|
|
1587
|
+
toHex(): string;
|
|
1588
|
+
toHexAnnotated(tagsStore?: TagsStore): string;
|
|
1589
|
+
toString(): string;
|
|
1590
|
+
toDebugString(): string;
|
|
1591
|
+
toDiagnostic(): string;
|
|
1592
|
+
toDiagnosticAnnotated(): string;
|
|
1593
|
+
isByteString(): boolean;
|
|
1594
|
+
isText(): boolean;
|
|
1595
|
+
isArray(): boolean;
|
|
1596
|
+
isMap(): boolean;
|
|
1597
|
+
isTagged(): boolean;
|
|
1598
|
+
isSimple(): boolean;
|
|
1599
|
+
isBool(): boolean;
|
|
1600
|
+
isTrue(): boolean;
|
|
1601
|
+
isFalse(): boolean;
|
|
1602
|
+
isNull(): boolean;
|
|
1603
|
+
isNumber(): boolean;
|
|
1604
|
+
isInteger(): boolean;
|
|
1605
|
+
isUnsigned(): boolean;
|
|
1606
|
+
isNegative(): boolean;
|
|
1607
|
+
isNaN(): boolean;
|
|
1608
|
+
isFloat(): boolean;
|
|
1609
|
+
asByteString(): Uint8Array | undefined;
|
|
1610
|
+
asText(): string | undefined;
|
|
1611
|
+
asArray(): readonly Cbor[] | undefined;
|
|
1612
|
+
asMap(): CborMap | undefined;
|
|
1613
|
+
asTagged(): [Tag, Cbor] | undefined;
|
|
1614
|
+
asBool(): boolean | undefined;
|
|
1615
|
+
asInteger(): (number | bigint) | undefined;
|
|
1616
|
+
asNumber(): (number | bigint) | undefined;
|
|
1617
|
+
asSimpleValue(): Simple | undefined;
|
|
1618
|
+
/**
|
|
1619
|
+
* Convert to byte string, throwing if type doesn't match.
|
|
1620
|
+
* @throws {TypeError} If value is not a byte string type
|
|
1621
|
+
*/
|
|
1622
|
+
toByteString(): Uint8Array;
|
|
1623
|
+
/**
|
|
1624
|
+
* Convert to text string, throwing if type doesn't match.
|
|
1625
|
+
* @throws {TypeError} If value is not a text string type
|
|
1626
|
+
*/
|
|
1627
|
+
toText(): string;
|
|
1628
|
+
/**
|
|
1629
|
+
* Convert to array, throwing if type doesn't match.
|
|
1630
|
+
* @throws {TypeError} If value is not an array type
|
|
1631
|
+
*/
|
|
1632
|
+
toArray(): readonly Cbor[];
|
|
1633
|
+
/**
|
|
1634
|
+
* Convert to map, throwing if type doesn't match.
|
|
1635
|
+
* @throws {TypeError} If value is not a map type
|
|
1636
|
+
*/
|
|
1637
|
+
toMap(): CborMap;
|
|
1638
|
+
/**
|
|
1639
|
+
* Convert to tagged value, throwing if type doesn't match.
|
|
1640
|
+
* @throws {TypeError} If value is not a tagged type
|
|
1641
|
+
*/
|
|
1642
|
+
toTagged(): [Tag, Cbor];
|
|
1643
|
+
/**
|
|
1644
|
+
* Convert to boolean, throwing if type doesn't match.
|
|
1645
|
+
* @throws {TypeError} If value is not a boolean (True/False) type
|
|
1646
|
+
*/
|
|
1647
|
+
toBool(): boolean;
|
|
1648
|
+
/**
|
|
1649
|
+
* Convert to integer, throwing if type doesn't match.
|
|
1650
|
+
* @throws {TypeError} If value is not an integer (Unsigned or Negative) type
|
|
1651
|
+
*/
|
|
1652
|
+
toInteger(): number | bigint;
|
|
1653
|
+
/**
|
|
1654
|
+
* Convert to number, throwing if type doesn't match.
|
|
1655
|
+
* @throws {TypeError} If value is not a numeric (Unsigned, Negative, or Float) type
|
|
1656
|
+
*/
|
|
1657
|
+
toNumber(): number | bigint;
|
|
1658
|
+
/**
|
|
1659
|
+
* Convert to simple value, throwing if type doesn't match.
|
|
1660
|
+
* @throws {TypeError} If value is not a simple type
|
|
1661
|
+
*/
|
|
1662
|
+
toSimpleValue(): Simple;
|
|
1663
|
+
/**
|
|
1664
|
+
* Expect specific tag and return content, throwing if tag doesn't match.
|
|
1665
|
+
* @param tag - Expected tag value
|
|
1666
|
+
* @throws {CborError} With type 'WrongType' if value is not tagged, or 'Custom' if tag doesn't match
|
|
1667
|
+
*/
|
|
1668
|
+
expectTag(tag: CborNumber | Tag): Cbor;
|
|
1669
|
+
/**
|
|
1670
|
+
* Walk the CBOR structure with a visitor function.
|
|
1671
|
+
* @param initialState - Initial state for the visitor
|
|
1672
|
+
* @param visitor - Visitor function called for each element
|
|
1673
|
+
*/
|
|
1674
|
+
walk<State>(initialState: State, visitor: Visitor<State>): State;
|
|
1675
|
+
/**
|
|
1676
|
+
* Validate that value has one of the expected tags.
|
|
1677
|
+
* @param expectedTags - Array of expected tag values
|
|
1678
|
+
* @throws {CborError} With type 'WrongType' if value is not tagged, or 'Custom' if tag doesn't match any expected value
|
|
1679
|
+
*/
|
|
1680
|
+
validateTag(expectedTags: Tag[]): Tag;
|
|
1681
|
+
/**
|
|
1682
|
+
* Remove one level of tagging, returning the inner content.
|
|
1683
|
+
*/
|
|
1684
|
+
untagged(): Cbor;
|
|
1685
|
+
}
|
|
1686
|
+
type Cbor = (CborUnsignedType | CborNegativeType | CborByteStringType | CborTextType | CborArrayType | CborMapType | CborTaggedType | CborSimpleType) & CborMethods;
|
|
1687
|
+
/**
|
|
1688
|
+
* Convert any value to a CBOR representation.
|
|
1689
|
+
* Matches Rust's `From` trait implementations for CBOR.
|
|
1690
|
+
*/
|
|
1691
|
+
declare const cbor: (value: CborInput) => Cbor;
|
|
1692
|
+
/**
|
|
1693
|
+
* Encode a CBOR value to binary data.
|
|
1694
|
+
* Matches Rust's `CBOR::to_cbor_data()` method.
|
|
1695
|
+
*/
|
|
1696
|
+
declare const cborData: (value: CborInput) => Uint8Array;
|
|
1697
|
+
declare const toByteString: (data: Uint8Array) => Cbor;
|
|
1698
|
+
declare const toByteStringFromHex: (hex: string) => Cbor;
|
|
1699
|
+
declare const toTaggedValue: (tag: CborNumber | Tag, item: CborInput) => Cbor;
|
|
1700
|
+
/**
|
|
1701
|
+
* CBOR constants and helper methods.
|
|
1702
|
+
*
|
|
1703
|
+
* Provides constants for common simple values (False, True, Null) and static methods
|
|
1704
|
+
* matching the Rust CBOR API for encoding/decoding.
|
|
1705
|
+
*/
|
|
1706
|
+
declare const Cbor: {
|
|
1707
|
+
False: {
|
|
1708
|
+
isCbor: true;
|
|
1709
|
+
type: 7;
|
|
1710
|
+
value: {
|
|
1711
|
+
type: "False";
|
|
1712
|
+
};
|
|
1713
|
+
} & CborMethods;
|
|
1714
|
+
True: {
|
|
1715
|
+
isCbor: true;
|
|
1716
|
+
type: 7;
|
|
1717
|
+
value: {
|
|
1718
|
+
type: "True";
|
|
1719
|
+
};
|
|
1720
|
+
} & CborMethods;
|
|
1721
|
+
Null: {
|
|
1722
|
+
isCbor: true;
|
|
1723
|
+
type: 7;
|
|
1724
|
+
value: {
|
|
1725
|
+
type: "Null";
|
|
1726
|
+
};
|
|
1727
|
+
} & CborMethods;
|
|
1728
|
+
NaN: {
|
|
1729
|
+
isCbor: true;
|
|
1730
|
+
type: 7;
|
|
1731
|
+
value: {
|
|
1732
|
+
type: "Float";
|
|
1733
|
+
value: number;
|
|
1734
|
+
};
|
|
1735
|
+
} & CborMethods;
|
|
1736
|
+
/**
|
|
1737
|
+
* Creates a CBOR value from any JavaScript value.
|
|
1738
|
+
*
|
|
1739
|
+
* Matches Rust's `CBOR::from()` behavior for various types.
|
|
1740
|
+
*
|
|
1741
|
+
* @param value - Any JavaScript value (number, string, boolean, null, array, object, etc.)
|
|
1742
|
+
* @returns A CBOR symbolic representation with instance methods
|
|
1743
|
+
*/
|
|
1744
|
+
from(value: CborInput): Cbor;
|
|
1745
|
+
/**
|
|
1746
|
+
* Decodes binary data into CBOR symbolic representation.
|
|
1747
|
+
*
|
|
1748
|
+
* Matches Rust's `CBOR::try_from_data()` method.
|
|
1749
|
+
*
|
|
1750
|
+
* @param data - The binary data to decode
|
|
1751
|
+
* @returns A CBOR value with instance methods
|
|
1752
|
+
* @throws Error if the data is not valid CBOR or violates dCBOR encoding rules
|
|
1753
|
+
*/
|
|
1754
|
+
tryFromData(data: Uint8Array): Cbor;
|
|
1755
|
+
/**
|
|
1756
|
+
* Decodes a hexadecimal string into CBOR symbolic representation.
|
|
1757
|
+
*
|
|
1758
|
+
* Matches Rust's `CBOR::try_from_hex()` method.
|
|
1759
|
+
*
|
|
1760
|
+
* @param hex - A string containing hexadecimal characters
|
|
1761
|
+
* @returns A CBOR value with instance methods
|
|
1762
|
+
* @throws Error if the hex string is invalid or the resulting data is not valid dCBOR
|
|
1763
|
+
*/
|
|
1764
|
+
tryFromHex(hex: string): Cbor;
|
|
1765
|
+
};
|
|
1766
|
+
//#endregion
|
|
1767
|
+
//#region src/decode.d.ts
|
|
1768
|
+
declare function decodeCbor(data: Uint8Array): Cbor;
|
|
1769
|
+
//#endregion
|
|
1770
|
+
//#region src/set.d.ts
|
|
1771
|
+
/**
|
|
1772
|
+
* CBOR Set type with tag(258) encoding.
|
|
1773
|
+
*
|
|
1774
|
+
* Internally uses a CborMap to ensure unique elements with deterministic ordering.
|
|
1775
|
+
* Elements are ordered by their CBOR encoding (lexicographic byte order).
|
|
1776
|
+
*
|
|
1777
|
+
* @example
|
|
1778
|
+
* ```typescript
|
|
1779
|
+
* // Create set
|
|
1780
|
+
* const set = CborSet.fromArray([1, 2, 3]);
|
|
1781
|
+
* const set2 = CborSet.fromSet(new Set([1, 2, 3]));
|
|
1782
|
+
*
|
|
1783
|
+
* // Add elements
|
|
1784
|
+
* set.insert(4);
|
|
1785
|
+
* set.insert(2); // Duplicate, no effect
|
|
1786
|
+
*
|
|
1787
|
+
* // Check membership
|
|
1788
|
+
* console.log(set.contains(2)); // true
|
|
1789
|
+
* console.log(set.contains(99)); // false
|
|
1790
|
+
*
|
|
1791
|
+
* // Encode to CBOR
|
|
1792
|
+
* const tagged = set.taggedCbor();
|
|
1793
|
+
* ```
|
|
1794
|
+
*/
|
|
1795
|
+
declare class CborSet implements CborTaggedEncodable, CborTaggedDecodable<CborSet> {
|
|
1796
|
+
#private;
|
|
1797
|
+
constructor();
|
|
1798
|
+
/**
|
|
1799
|
+
* Create CborSet from array.
|
|
1800
|
+
*
|
|
1801
|
+
* Duplicates are automatically removed.
|
|
1802
|
+
*
|
|
1803
|
+
* @param items - Array of items to add to the set
|
|
1804
|
+
* @returns New CborSet instance
|
|
1805
|
+
*
|
|
1806
|
+
* @example
|
|
1807
|
+
* ```typescript
|
|
1808
|
+
* const set = CborSet.fromArray([1, 2, 3, 2, 1]);
|
|
1809
|
+
* console.log(set.size); // 3
|
|
1810
|
+
* ```
|
|
1811
|
+
*/
|
|
1812
|
+
static fromArray<T extends CborInput>(items: T[]): CborSet;
|
|
1813
|
+
/**
|
|
1814
|
+
* Create CborSet from JavaScript Set.
|
|
1815
|
+
*
|
|
1816
|
+
* @param items - JavaScript Set of items
|
|
1817
|
+
* @returns New CborSet instance
|
|
1818
|
+
*
|
|
1819
|
+
* @example
|
|
1820
|
+
* ```typescript
|
|
1821
|
+
* const jsSet = new Set([1, 2, 3]);
|
|
1822
|
+
* const cborSet = CborSet.fromSet(jsSet);
|
|
1823
|
+
* ```
|
|
1824
|
+
*/
|
|
1825
|
+
static fromSet<T extends CborInput>(items: Set<T>): CborSet;
|
|
1826
|
+
/**
|
|
1827
|
+
* Create CborSet from iterable.
|
|
1828
|
+
*
|
|
1829
|
+
* @param items - Iterable of items
|
|
1830
|
+
* @returns New CborSet instance
|
|
1831
|
+
*/
|
|
1832
|
+
static fromIterable<T extends CborInput>(items: Iterable<T>): CborSet;
|
|
1833
|
+
/**
|
|
1834
|
+
* Insert an element into the set.
|
|
1835
|
+
*
|
|
1836
|
+
* If the element already exists, has no effect.
|
|
1837
|
+
*
|
|
1838
|
+
* @param value - Value to insert
|
|
1839
|
+
*
|
|
1840
|
+
* @example
|
|
1841
|
+
* ```typescript
|
|
1842
|
+
* const set = new CborSet();
|
|
1843
|
+
* set.insert(1);
|
|
1844
|
+
* set.insert(2);
|
|
1845
|
+
* set.insert(1); // No effect, already exists
|
|
1846
|
+
* ```
|
|
1847
|
+
*/
|
|
1848
|
+
insert(value: CborInput): void;
|
|
1849
|
+
/**
|
|
1850
|
+
* Check if set contains an element.
|
|
1851
|
+
*
|
|
1852
|
+
* @param value - Value to check
|
|
1853
|
+
* @returns true if element is in the set
|
|
1854
|
+
*
|
|
1855
|
+
* @example
|
|
1856
|
+
* ```typescript
|
|
1857
|
+
* const set = CborSet.fromArray([1, 2, 3]);
|
|
1858
|
+
* console.log(set.contains(2)); // true
|
|
1859
|
+
* console.log(set.contains(99)); // false
|
|
1860
|
+
* ```
|
|
1861
|
+
*/
|
|
1862
|
+
contains(value: CborInput): boolean;
|
|
1863
|
+
/**
|
|
1864
|
+
* Remove an element from the set.
|
|
1865
|
+
*
|
|
1866
|
+
* @param value - Value to remove
|
|
1867
|
+
* @returns true if element was removed, false if not found
|
|
1868
|
+
*
|
|
1869
|
+
* @example
|
|
1870
|
+
* ```typescript
|
|
1871
|
+
* const set = CborSet.fromArray([1, 2, 3]);
|
|
1872
|
+
* set.delete(2); // Returns true
|
|
1873
|
+
* set.delete(99); // Returns false
|
|
1874
|
+
* ```
|
|
1875
|
+
*/
|
|
1876
|
+
delete(value: CborInput): boolean;
|
|
1877
|
+
/**
|
|
1878
|
+
* Remove all elements from the set.
|
|
1879
|
+
*/
|
|
1880
|
+
clear(): void;
|
|
1881
|
+
/**
|
|
1882
|
+
* Get the number of elements in the set.
|
|
1883
|
+
*
|
|
1884
|
+
* @returns Number of elements
|
|
1885
|
+
*/
|
|
1886
|
+
get size(): number;
|
|
1887
|
+
/**
|
|
1888
|
+
* Check if the set is empty.
|
|
1889
|
+
*
|
|
1890
|
+
* @returns true if set has no elements
|
|
1891
|
+
*/
|
|
1892
|
+
isEmpty(): boolean;
|
|
1893
|
+
/**
|
|
1894
|
+
* Create a new set containing elements in this set or the other set.
|
|
1895
|
+
*
|
|
1896
|
+
* @param other - Other set
|
|
1897
|
+
* @returns New set with union of elements
|
|
1898
|
+
*
|
|
1899
|
+
* @example
|
|
1900
|
+
* ```typescript
|
|
1901
|
+
* const set1 = CborSet.fromArray([1, 2, 3]);
|
|
1902
|
+
* const set2 = CborSet.fromArray([3, 4, 5]);
|
|
1903
|
+
* const union = set1.union(set2);
|
|
1904
|
+
* // union contains [1, 2, 3, 4, 5]
|
|
1905
|
+
* ```
|
|
1906
|
+
*/
|
|
1907
|
+
union(other: CborSet): CborSet;
|
|
1908
|
+
/**
|
|
1909
|
+
* Create a new set containing elements in both this set and the other set.
|
|
1910
|
+
*
|
|
1911
|
+
* @param other - Other set
|
|
1912
|
+
* @returns New set with intersection of elements
|
|
1913
|
+
*
|
|
1914
|
+
* @example
|
|
1915
|
+
* ```typescript
|
|
1916
|
+
* const set1 = CborSet.fromArray([1, 2, 3]);
|
|
1917
|
+
* const set2 = CborSet.fromArray([2, 3, 4]);
|
|
1918
|
+
* const intersection = set1.intersection(set2);
|
|
1919
|
+
* // intersection contains [2, 3]
|
|
1920
|
+
* ```
|
|
1921
|
+
*/
|
|
1922
|
+
intersection(other: CborSet): CborSet;
|
|
1923
|
+
/**
|
|
1924
|
+
* Create a new set containing elements in this set but not in the other set.
|
|
1925
|
+
*
|
|
1926
|
+
* @param other - Other set
|
|
1927
|
+
* @returns New set with difference of elements
|
|
1928
|
+
*
|
|
1929
|
+
* @example
|
|
1930
|
+
* ```typescript
|
|
1931
|
+
* const set1 = CborSet.fromArray([1, 2, 3]);
|
|
1932
|
+
* const set2 = CborSet.fromArray([2, 3, 4]);
|
|
1933
|
+
* const diff = set1.difference(set2);
|
|
1934
|
+
* // diff contains [1]
|
|
1935
|
+
* ```
|
|
1936
|
+
*/
|
|
1937
|
+
difference(other: CborSet): CborSet;
|
|
1938
|
+
/**
|
|
1939
|
+
* Check if this set is a subset of another set.
|
|
1940
|
+
*
|
|
1941
|
+
* @param other - Other set
|
|
1942
|
+
* @returns true if all elements of this set are in the other set
|
|
1943
|
+
*/
|
|
1944
|
+
isSubsetOf(other: CborSet): boolean;
|
|
1945
|
+
/**
|
|
1946
|
+
* Check if this set is a superset of another set.
|
|
1947
|
+
*
|
|
1948
|
+
* @param other - Other set
|
|
1949
|
+
* @returns true if all elements of the other set are in this set
|
|
1950
|
+
*/
|
|
1951
|
+
isSupersetOf(other: CborSet): boolean;
|
|
1952
|
+
/**
|
|
1953
|
+
* Iterate over elements in the set.
|
|
1954
|
+
*
|
|
1955
|
+
* Elements are returned in deterministic order (by CBOR encoding).
|
|
1956
|
+
*
|
|
1957
|
+
* @example
|
|
1958
|
+
* ```typescript
|
|
1959
|
+
* const set = CborSet.fromArray([3, 1, 2]);
|
|
1960
|
+
* for (const value of set) {
|
|
1961
|
+
* console.log(extractCbor(value));
|
|
1962
|
+
* }
|
|
1963
|
+
* ```
|
|
1964
|
+
*/
|
|
1965
|
+
[Symbol.iterator](): Iterator<Cbor>;
|
|
1966
|
+
/**
|
|
1967
|
+
* Get all values as an array.
|
|
1968
|
+
*
|
|
1969
|
+
* @returns Array of CBOR values in deterministic order
|
|
1970
|
+
*
|
|
1971
|
+
* @example
|
|
1972
|
+
* ```typescript
|
|
1973
|
+
* const set = CborSet.fromArray([3, 1, 2]);
|
|
1974
|
+
* const values = set.values();
|
|
1975
|
+
* // Values in deterministic order
|
|
1976
|
+
* ```
|
|
1977
|
+
*/
|
|
1978
|
+
values(): Cbor[];
|
|
1979
|
+
/**
|
|
1980
|
+
* Execute a function for each element.
|
|
1981
|
+
*
|
|
1982
|
+
* @param callback - Function to call for each element
|
|
1983
|
+
*
|
|
1984
|
+
* @example
|
|
1985
|
+
* ```typescript
|
|
1986
|
+
* set.forEach(value => {
|
|
1987
|
+
* console.log(extractCbor(value));
|
|
1988
|
+
* });
|
|
1989
|
+
* ```
|
|
1990
|
+
*/
|
|
1991
|
+
forEach(callback: (value: Cbor) => void): void;
|
|
1992
|
+
cborTags(): Tag[];
|
|
1993
|
+
untaggedCbor(): Cbor;
|
|
1994
|
+
taggedCbor(): Cbor;
|
|
1995
|
+
fromUntaggedCbor(c: Cbor): CborSet;
|
|
1996
|
+
fromTaggedCbor(c: Cbor): CborSet;
|
|
1997
|
+
/**
|
|
1998
|
+
* Decode a CborSet from tagged CBOR (static method).
|
|
1999
|
+
*
|
|
2000
|
+
* @param cbor - Tagged CBOR value with tag(258)
|
|
2001
|
+
* @returns Decoded CborSet instance
|
|
2002
|
+
*/
|
|
2003
|
+
static fromTaggedCborStatic(cbor: Cbor): CborSet;
|
|
2004
|
+
/**
|
|
2005
|
+
* Convert to CBOR array (untagged).
|
|
2006
|
+
*
|
|
2007
|
+
* @returns CBOR array
|
|
2008
|
+
*/
|
|
2009
|
+
toCbor(): Cbor;
|
|
2010
|
+
/**
|
|
2011
|
+
* Convert to CBOR bytes (tagged).
|
|
2012
|
+
*
|
|
2013
|
+
* @returns Encoded CBOR bytes
|
|
2014
|
+
*/
|
|
2015
|
+
toBytes(): Uint8Array;
|
|
2016
|
+
/**
|
|
2017
|
+
* Convert to JavaScript Set.
|
|
2018
|
+
*
|
|
2019
|
+
* @returns JavaScript Set with extracted values
|
|
2020
|
+
*
|
|
2021
|
+
* @example
|
|
2022
|
+
* ```typescript
|
|
2023
|
+
* const cborSet = CborSet.fromArray([1, 2, 3]);
|
|
2024
|
+
* const jsSet = cborSet.toSet();
|
|
2025
|
+
* console.log(jsSet.has(1)); // true
|
|
2026
|
+
* ```
|
|
2027
|
+
*/
|
|
2028
|
+
toSet(): Set<unknown>;
|
|
2029
|
+
/**
|
|
2030
|
+
* Convert to JavaScript Array.
|
|
2031
|
+
*
|
|
2032
|
+
* @returns Array with extracted values
|
|
2033
|
+
*/
|
|
2034
|
+
toArray(): unknown[];
|
|
2035
|
+
/**
|
|
2036
|
+
* Get diagnostic notation for the set.
|
|
2037
|
+
*
|
|
2038
|
+
* @returns String representation
|
|
2039
|
+
*
|
|
2040
|
+
* @example
|
|
2041
|
+
* ```typescript
|
|
2042
|
+
* const set = CborSet.fromArray([1, 2, 3]);
|
|
2043
|
+
* console.log(set.diagnostic); // "[1, 2, 3]"
|
|
2044
|
+
* ```
|
|
2045
|
+
*/
|
|
2046
|
+
get diagnostic(): string;
|
|
2047
|
+
/**
|
|
2048
|
+
* Convert to string (same as diagnostic).
|
|
2049
|
+
*
|
|
2050
|
+
* @returns String representation
|
|
2051
|
+
*/
|
|
2052
|
+
toString(): string;
|
|
2053
|
+
/**
|
|
2054
|
+
* Convert to JSON (returns array of values).
|
|
2055
|
+
*
|
|
2056
|
+
* @returns Array for JSON serialization
|
|
2057
|
+
*/
|
|
2058
|
+
toJSON(): unknown[];
|
|
2059
|
+
}
|
|
2060
|
+
//#endregion
|
|
2061
|
+
//#region src/tags.d.ts
|
|
2062
|
+
/**
|
|
2063
|
+
* Tag 0: Standard date/time string (RFC 3339)
|
|
2064
|
+
*/
|
|
2065
|
+
declare const TAG_DATE_TIME_STRING = 0;
|
|
2066
|
+
/**
|
|
2067
|
+
* Tag 1: Epoch-based date/time (seconds since 1970-01-01T00:00:00Z)
|
|
2068
|
+
*/
|
|
2069
|
+
declare const TAG_EPOCH_DATE_TIME = 1;
|
|
2070
|
+
/**
|
|
2071
|
+
* Tag 100: Epoch-based date (days since 1970-01-01)
|
|
2072
|
+
*/
|
|
2073
|
+
declare const TAG_EPOCH_DATE = 100;
|
|
2074
|
+
/**
|
|
2075
|
+
* Tag 2: Positive bignum (unsigned arbitrary-precision integer)
|
|
2076
|
+
*/
|
|
2077
|
+
declare const TAG_POSITIVE_BIGNUM = 2;
|
|
2078
|
+
/**
|
|
2079
|
+
* Tag 3: Negative bignum (signed arbitrary-precision integer)
|
|
2080
|
+
*/
|
|
2081
|
+
declare const TAG_NEGATIVE_BIGNUM = 3;
|
|
2082
|
+
/**
|
|
2083
|
+
* Tag 4: Decimal fraction [exponent, mantissa]
|
|
2084
|
+
*/
|
|
2085
|
+
declare const TAG_DECIMAL_FRACTION = 4;
|
|
2086
|
+
/**
|
|
2087
|
+
* Tag 5: Bigfloat [exponent, mantissa]
|
|
2088
|
+
*/
|
|
2089
|
+
declare const TAG_BIGFLOAT = 5;
|
|
2090
|
+
/**
|
|
2091
|
+
* Tag 21: Expected conversion to base64url encoding
|
|
2092
|
+
*/
|
|
2093
|
+
declare const TAG_BASE64URL = 21;
|
|
2094
|
+
/**
|
|
2095
|
+
* Tag 22: Expected conversion to base64 encoding
|
|
2096
|
+
*/
|
|
2097
|
+
declare const TAG_BASE64 = 22;
|
|
2098
|
+
/**
|
|
2099
|
+
* Tag 23: Expected conversion to base16 encoding
|
|
2100
|
+
*/
|
|
2101
|
+
declare const TAG_BASE16 = 23;
|
|
2102
|
+
/**
|
|
2103
|
+
* Tag 24: Encoded CBOR data item
|
|
2104
|
+
*/
|
|
2105
|
+
declare const TAG_ENCODED_CBOR = 24;
|
|
2106
|
+
/**
|
|
2107
|
+
* Tag 32: URI (text string)
|
|
2108
|
+
*/
|
|
2109
|
+
declare const TAG_URI = 32;
|
|
2110
|
+
/**
|
|
2111
|
+
* Tag 33: base64url-encoded text
|
|
2112
|
+
*/
|
|
2113
|
+
declare const TAG_BASE64URL_TEXT = 33;
|
|
2114
|
+
/**
|
|
2115
|
+
* Tag 34: base64-encoded text
|
|
2116
|
+
*/
|
|
2117
|
+
declare const TAG_BASE64_TEXT = 34;
|
|
2118
|
+
/**
|
|
2119
|
+
* Tag 35: Regular expression (PCRE/ECMA262)
|
|
2120
|
+
*/
|
|
2121
|
+
declare const TAG_REGEXP = 35;
|
|
2122
|
+
/**
|
|
2123
|
+
* Tag 36: MIME message
|
|
2124
|
+
*/
|
|
2125
|
+
declare const TAG_MIME_MESSAGE = 36;
|
|
2126
|
+
/**
|
|
2127
|
+
* Tag 37: Binary UUID
|
|
2128
|
+
*/
|
|
2129
|
+
declare const TAG_UUID = 37;
|
|
2130
|
+
/**
|
|
2131
|
+
* Tag 256: string reference (namespace)
|
|
2132
|
+
*/
|
|
2133
|
+
declare const TAG_STRING_REF_NAMESPACE = 256;
|
|
2134
|
+
/**
|
|
2135
|
+
* Tag 257: binary UUID reference
|
|
2136
|
+
*/
|
|
2137
|
+
declare const TAG_BINARY_UUID = 257;
|
|
2138
|
+
/**
|
|
2139
|
+
* Tag 258: Set of values (array with no duplicates)
|
|
2140
|
+
*/
|
|
2141
|
+
declare const TAG_SET = 258;
|
|
2142
|
+
/**
|
|
2143
|
+
* Tag 55799: Self-describe CBOR (magic number 0xd9d9f7)
|
|
2144
|
+
*/
|
|
2145
|
+
declare const TAG_SELF_DESCRIBE_CBOR = 55799;
|
|
2146
|
+
declare const TAG_DATE = 1;
|
|
2147
|
+
declare const TAG_NAME_DATE = "date";
|
|
2148
|
+
/**
|
|
2149
|
+
* Register standard tags in a specific tags store.
|
|
2150
|
+
* Matches Rust's register_tags_in() function.
|
|
2151
|
+
*
|
|
2152
|
+
* @param tagsStore - The tags store to register tags into
|
|
2153
|
+
*/
|
|
2154
|
+
declare const registerTagsIn: (tagsStore: TagsStore) => void;
|
|
2155
|
+
/**
|
|
2156
|
+
* Register standard tags in the global tags store.
|
|
2157
|
+
* Matches Rust's register_tags() function.
|
|
2158
|
+
*
|
|
2159
|
+
* This function is idempotent - calling it multiple times is safe.
|
|
2160
|
+
*/
|
|
2161
|
+
declare const registerTags: () => void;
|
|
2162
|
+
/**
|
|
2163
|
+
* Converts an array of tag values to their corresponding Tag objects.
|
|
2164
|
+
* Matches Rust's tags_for_values() function.
|
|
2165
|
+
*
|
|
2166
|
+
* This function looks up each tag value in the global tag registry and returns
|
|
2167
|
+
* an array of complete Tag objects. For any tag values that aren't
|
|
2168
|
+
* registered in the global registry, it creates a basic Tag with just the
|
|
2169
|
+
* value (no name).
|
|
2170
|
+
*
|
|
2171
|
+
* @param values - Array of numeric tag values to convert
|
|
2172
|
+
* @returns Array of Tag objects corresponding to the input values
|
|
2173
|
+
*
|
|
2174
|
+
* @example
|
|
2175
|
+
* ```typescript
|
|
2176
|
+
* // Register some tags first
|
|
2177
|
+
* registerTags();
|
|
2178
|
+
*
|
|
2179
|
+
* // Convert tag values to Tag objects
|
|
2180
|
+
* const tags = tagsForValues([1, 42, 999]);
|
|
2181
|
+
*
|
|
2182
|
+
* // The first tag (value 1) should be registered as "date"
|
|
2183
|
+
* console.log(tags[0].value); // 1
|
|
2184
|
+
* console.log(tags[0].name); // "date"
|
|
2185
|
+
*
|
|
2186
|
+
* // Unregistered tags will have a value but no name
|
|
2187
|
+
* console.log(tags[1].value); // 42
|
|
2188
|
+
* console.log(tags[2].value); // 999
|
|
2189
|
+
* ```
|
|
2190
|
+
*/
|
|
2191
|
+
declare const tagsForValues: (values: (number | bigint)[]) => Tag[];
|
|
2192
|
+
//#endregion
|
|
2193
|
+
//#region src/diag.d.ts
|
|
2194
|
+
/**
|
|
2195
|
+
* Options for diagnostic formatting.
|
|
2196
|
+
*/
|
|
2197
|
+
interface DiagFormatOpts {
|
|
2198
|
+
/**
|
|
2199
|
+
* Add tag names as annotations.
|
|
2200
|
+
* When true, tagged values are displayed as "tagName(content)" instead of "tagValue(content)".
|
|
2201
|
+
*
|
|
2202
|
+
* @default false
|
|
2203
|
+
*/
|
|
2204
|
+
annotate?: boolean;
|
|
2205
|
+
/**
|
|
2206
|
+
* Use custom summarizers for tagged values.
|
|
2207
|
+
* When true, calls registered summarizers for tagged values.
|
|
2208
|
+
*
|
|
2209
|
+
* @default false
|
|
2210
|
+
*/
|
|
2211
|
+
summarize?: boolean;
|
|
2212
|
+
/**
|
|
2213
|
+
* Single-line (flat) output.
|
|
2214
|
+
* When true, arrays and maps are formatted without line breaks.
|
|
2215
|
+
*
|
|
2216
|
+
* @default false
|
|
2217
|
+
*/
|
|
2218
|
+
flat?: boolean;
|
|
2219
|
+
/**
|
|
2220
|
+
* Tag store to use for tag name resolution.
|
|
2221
|
+
* - TagsStore instance: Use specific store
|
|
2222
|
+
* - 'global': Use global singleton store
|
|
2223
|
+
* - 'none': Don't use any store (show tag numbers)
|
|
2224
|
+
*
|
|
2225
|
+
* @default 'global'
|
|
2226
|
+
*/
|
|
2227
|
+
tags?: TagsStore | "global" | "none";
|
|
2228
|
+
/**
|
|
2229
|
+
* Current indentation level (internal use for recursion).
|
|
2230
|
+
* @internal
|
|
2231
|
+
*/
|
|
2232
|
+
indent?: number;
|
|
2233
|
+
/**
|
|
2234
|
+
* Indentation string (spaces per level).
|
|
2235
|
+
* @internal
|
|
2236
|
+
*/
|
|
2237
|
+
indentString?: string;
|
|
2238
|
+
}
|
|
2239
|
+
/**
|
|
2240
|
+
* Format CBOR value as diagnostic notation with options.
|
|
2241
|
+
*
|
|
2242
|
+
* @param cbor - CBOR value to format
|
|
2243
|
+
* @param opts - Formatting options
|
|
2244
|
+
* @returns Diagnostic string
|
|
2245
|
+
*
|
|
2246
|
+
* @example
|
|
2247
|
+
* ```typescript
|
|
2248
|
+
* const value = cbor({ name: 'Alice', age: 30 });
|
|
2249
|
+
* console.log(diagnosticOpt(value, { flat: true }));
|
|
2250
|
+
* // {\"name\": \"Alice\", \"age\": 30}
|
|
2251
|
+
*
|
|
2252
|
+
* const tagged = createTaggedCbor({ ... });
|
|
2253
|
+
* console.log(diagnosticOpt(tagged, { annotate: true }));
|
|
2254
|
+
* // date(1234567890)
|
|
2255
|
+
* ```
|
|
2256
|
+
*/
|
|
2257
|
+
declare function diagnosticOpt(cbor: Cbor, opts?: DiagFormatOpts): string;
|
|
2258
|
+
/**
|
|
2259
|
+
* Format CBOR value using custom summarizers for tagged values.
|
|
2260
|
+
*
|
|
2261
|
+
* If a summarizer is registered for a tagged value, uses that instead of
|
|
2262
|
+
* showing the full content.
|
|
2263
|
+
*
|
|
2264
|
+
* @param cbor - CBOR value to format
|
|
2265
|
+
* @returns Summarized diagnostic string
|
|
2266
|
+
*
|
|
2267
|
+
* @example
|
|
2268
|
+
* ```typescript
|
|
2269
|
+
* // If a summarizer is registered for tag 123:
|
|
2270
|
+
* const tagged = cbor({ type: MajorType.Tagged, tag: 123, value: ... });
|
|
2271
|
+
* console.log(summary(tagged));
|
|
2272
|
+
* // "custom-summary" (instead of full content)
|
|
2273
|
+
* ```
|
|
2274
|
+
*/
|
|
2275
|
+
declare function summary(cbor: Cbor): string;
|
|
2276
|
+
//#endregion
|
|
2277
|
+
//#region src/dump.d.ts
|
|
2278
|
+
/**
|
|
2279
|
+
* Options for hex formatting.
|
|
2280
|
+
*/
|
|
2281
|
+
interface HexFormatOpts {
|
|
2282
|
+
/** Whether to annotate the hex dump with semantic information */
|
|
2283
|
+
annotate?: boolean;
|
|
2284
|
+
/** Optional tags store for resolving tag names */
|
|
2285
|
+
tagsStore?: TagsStore;
|
|
2286
|
+
}
|
|
2287
|
+
/**
|
|
2288
|
+
* Convert bytes to hex string.
|
|
2289
|
+
*/
|
|
2290
|
+
declare const bytesToHex: (bytes: Uint8Array) => string;
|
|
2291
|
+
/**
|
|
2292
|
+
* Convert hex string to bytes.
|
|
2293
|
+
*/
|
|
2294
|
+
declare const hexToBytes: (hexString: string) => Uint8Array;
|
|
2295
|
+
/**
|
|
2296
|
+
* Returns the encoded hexadecimal representation of CBOR with options.
|
|
2297
|
+
*
|
|
2298
|
+
* Optionally annotates the output, e.g., breaking the output up into
|
|
2299
|
+
* semantically meaningful lines, formatting dates, and adding names of
|
|
2300
|
+
* known tags.
|
|
2301
|
+
*
|
|
2302
|
+
* @param cbor - CBOR value to convert
|
|
2303
|
+
* @param opts - Formatting options
|
|
2304
|
+
* @returns Hex string (possibly annotated)
|
|
2305
|
+
*/
|
|
2306
|
+
declare const hexOpt: (cbor: Cbor, opts?: HexFormatOpts) => string;
|
|
2307
|
+
//#endregion
|
|
2308
|
+
//#region src/cbor-codable.d.ts
|
|
2309
|
+
/**
|
|
2310
|
+
* Interface for types that can be encoded to CBOR.
|
|
2311
|
+
*
|
|
2312
|
+
* This interface provides convenient methods for converting
|
|
2313
|
+
* instances into CBOR objects and binary data.
|
|
2314
|
+
*
|
|
2315
|
+
* @example
|
|
2316
|
+
* ```typescript
|
|
2317
|
+
* // Custom type that can convert to CBOR
|
|
2318
|
+
* class Person implements CborEncodable {
|
|
2319
|
+
* constructor (public name: string, public age: number) {}
|
|
2320
|
+
*
|
|
2321
|
+
* toCbor(): Cbor {
|
|
2322
|
+
* const map = new CborMap();
|
|
2323
|
+
* map.set(cbor('name'), cbor(this.name));
|
|
2324
|
+
* map.set(cbor('age'), cbor(this.age));
|
|
2325
|
+
* return cbor(map);
|
|
2326
|
+
* }
|
|
2327
|
+
*
|
|
2328
|
+
* toCborData(): Uint8Array {
|
|
2329
|
+
* return cborData(this.toCbor());
|
|
2330
|
+
* }
|
|
2331
|
+
* }
|
|
2332
|
+
*
|
|
2333
|
+
* // Use the interface
|
|
2334
|
+
* const person = new Person('Alice', 30);
|
|
2335
|
+
*
|
|
2336
|
+
* // Convert to CBOR
|
|
2337
|
+
* const cborValue = person.toCbor();
|
|
2338
|
+
*
|
|
2339
|
+
* // Convert directly to binary CBOR data
|
|
2340
|
+
* const data = person.toCborData();
|
|
2341
|
+
* ```
|
|
2342
|
+
*/
|
|
2343
|
+
interface CborEncodable {
|
|
2344
|
+
/**
|
|
2345
|
+
* Converts this value to a CBOR object.
|
|
2346
|
+
*
|
|
2347
|
+
* @returns CBOR representation
|
|
2348
|
+
*/
|
|
2349
|
+
toCbor(): Cbor;
|
|
2350
|
+
/**
|
|
2351
|
+
* Converts this value directly to binary CBOR data.
|
|
2352
|
+
*
|
|
2353
|
+
* This is a shorthand for `cborData(this.toCbor())`.
|
|
2354
|
+
*
|
|
2355
|
+
* @returns Binary CBOR data
|
|
2356
|
+
*/
|
|
2357
|
+
toCborData(): Uint8Array;
|
|
2358
|
+
}
|
|
2359
|
+
/**
|
|
2360
|
+
* Interface for types that can be decoded from CBOR.
|
|
2361
|
+
*
|
|
2362
|
+
* This interface serves as a marker to indicate that a type
|
|
2363
|
+
* supports being created from CBOR data.
|
|
2364
|
+
*
|
|
2365
|
+
* @typeParam T - The type being decoded
|
|
2366
|
+
*
|
|
2367
|
+
* @example
|
|
2368
|
+
* ```typescript
|
|
2369
|
+
* // Custom type that can be decoded from CBOR
|
|
2370
|
+
* class Person implements CborDecodable<Person> {
|
|
2371
|
+
* constructor(public name: string = '', public age: number = 0) {}
|
|
2372
|
+
*
|
|
2373
|
+
* static fromCbor(cbor: Cbor): Person {
|
|
2374
|
+
* if (cbor.type !== MajorType.Map) {
|
|
2375
|
+
* throw new Error('Expected a CBOR map');
|
|
2376
|
+
* }
|
|
2377
|
+
* const map = cbor.value as CborMap;
|
|
2378
|
+
* const name = extractCbor(map.get(cbor('name'))!) as string;
|
|
2379
|
+
* const age = extractCbor(map.get(cbor('age'))!) as number;
|
|
2380
|
+
* return new Person(name, age);
|
|
2381
|
+
* }
|
|
2382
|
+
*
|
|
2383
|
+
* tryFromCbor(cbor: Cbor): Person {
|
|
2384
|
+
* return Person.fromCbor(cbor);
|
|
2385
|
+
* }
|
|
2386
|
+
* }
|
|
2387
|
+
*
|
|
2388
|
+
* // Parse from CBOR
|
|
2389
|
+
* const cborMap = ...; // some CBOR map
|
|
2390
|
+
* const person = Person.fromCbor(cborMap);
|
|
2391
|
+
* ```
|
|
2392
|
+
*/
|
|
2393
|
+
interface CborDecodable<T> {
|
|
2394
|
+
/**
|
|
2395
|
+
* Try to create an instance from a CBOR value.
|
|
2396
|
+
*
|
|
2397
|
+
* @param cbor - CBOR value to decode
|
|
2398
|
+
* @returns Decoded instance
|
|
2399
|
+
* @throws Error if decoding fails
|
|
2400
|
+
*/
|
|
2401
|
+
tryFromCbor(cbor: Cbor): T;
|
|
2402
|
+
}
|
|
2403
|
+
/**
|
|
2404
|
+
* Interface for types that can be both encoded to and decoded from CBOR.
|
|
2405
|
+
*
|
|
2406
|
+
* This interface is a convenience marker for types that implement both
|
|
2407
|
+
* `CborEncodable` and `CborDecodable`. It serves to indicate full CBOR
|
|
2408
|
+
* serialization support.
|
|
2409
|
+
*
|
|
2410
|
+
* @typeParam T - The type being encoded/decoded
|
|
2411
|
+
*
|
|
2412
|
+
* @example
|
|
2413
|
+
* ```typescript
|
|
2414
|
+
* // Custom type that implements both conversion directions
|
|
2415
|
+
* class Person implements CborCodable<Person> {
|
|
2416
|
+
* constructor(public name: string = '', public age: number = 0) {}
|
|
2417
|
+
*
|
|
2418
|
+
* // Implement encoding to CBOR
|
|
2419
|
+
* toCbor(): Cbor {
|
|
2420
|
+
* const map = new CborMap();
|
|
2421
|
+
* map.set(cbor('name'), cbor(this.name));
|
|
2422
|
+
* map.set(cbor('age'), cbor(this.age));
|
|
2423
|
+
* return cbor(map);
|
|
2424
|
+
* }
|
|
2425
|
+
*
|
|
2426
|
+
* toCborData(): Uint8Array {
|
|
2427
|
+
* return cborData(this.toCbor());
|
|
2428
|
+
* }
|
|
2429
|
+
*
|
|
2430
|
+
* // Implement decoding from CBOR
|
|
2431
|
+
* static fromCbor(cbor: Cbor): Person {
|
|
2432
|
+
* if (cbor.type !== MajorType.Map) {
|
|
2433
|
+
* throw new Error('Expected a CBOR map');
|
|
2434
|
+
* }
|
|
2435
|
+
* const map = cbor.value as CborMap;
|
|
2436
|
+
* const name = extractCbor(map.get(cbor('name'))!) as string;
|
|
2437
|
+
* const age = extractCbor(map.get(cbor('age'))!) as number;
|
|
2438
|
+
* return new Person(name, age);
|
|
2439
|
+
* }
|
|
2440
|
+
*
|
|
2441
|
+
* tryFromCbor(cbor: Cbor): Person {
|
|
2442
|
+
* return Person.fromCbor(cbor);
|
|
2443
|
+
* }
|
|
2444
|
+
* }
|
|
2445
|
+
*
|
|
2446
|
+
* // Person now implements CborCodable
|
|
2447
|
+
* const person = new Person('Alice', 30);
|
|
2448
|
+
* const cborValue = person.toCbor(); // Using CborEncodable
|
|
2449
|
+
*
|
|
2450
|
+
* // Create a round-trip copy
|
|
2451
|
+
* const personCopy = Person.fromCbor(cborValue); // Using CborDecodable
|
|
2452
|
+
* ```
|
|
2453
|
+
*/
|
|
2454
|
+
interface CborCodable<T> extends CborEncodable, CborDecodable<T> {}
|
|
2455
|
+
//#endregion
|
|
2456
|
+
//#region src/error.d.ts
|
|
2457
|
+
/**
|
|
2458
|
+
* A comprehensive set of errors that can occur during CBOR encoding and
|
|
2459
|
+
* decoding operations with special focus on enforcing the deterministic
|
|
2460
|
+
* encoding rules specified in the dCBOR specification.
|
|
2461
|
+
*
|
|
2462
|
+
* The dCBOR implementation validates all encoded CBOR against the
|
|
2463
|
+
* deterministic encoding requirements of RFC 8949 §4.2.1, plus additional
|
|
2464
|
+
* constraints defined in the dCBOR application profile. These errors represent
|
|
2465
|
+
* all the possible validation failures and decoding issues that can arise.
|
|
2466
|
+
*/
|
|
2467
|
+
type Error =
|
|
2468
|
+
/**
|
|
2469
|
+
* The CBOR data ended prematurely during decoding, before a complete CBOR
|
|
2470
|
+
* item could be decoded. This typically happens when a CBOR item's
|
|
2471
|
+
* structure indicates more data than is actually present.
|
|
2472
|
+
*/
|
|
2473
|
+
{
|
|
2474
|
+
readonly type: "Underrun";
|
|
2475
|
+
}
|
|
2476
|
+
/**
|
|
2477
|
+
* An unsupported or invalid value was encountered in a CBOR header byte.
|
|
2478
|
+
* The parameter contains the unsupported header byte value.
|
|
2479
|
+
* This can occur when decoding CBOR that uses unsupported features or is
|
|
2480
|
+
* malformed.
|
|
2481
|
+
*/ | {
|
|
2482
|
+
readonly type: "UnsupportedHeaderValue";
|
|
2483
|
+
readonly value: number;
|
|
2484
|
+
}
|
|
2485
|
+
/**
|
|
2486
|
+
* A CBOR numeric value was encoded in a non-canonical form, violating the
|
|
2487
|
+
* deterministic encoding requirement of dCBOR (per Section 2.3 of the
|
|
2488
|
+
* dCBOR specification).
|
|
2489
|
+
*
|
|
2490
|
+
* This error is triggered when:
|
|
2491
|
+
* - An integer is not encoded in its shortest possible form
|
|
2492
|
+
* - A floating point value that could be represented as an integer was not
|
|
2493
|
+
* reduced
|
|
2494
|
+
* - A NaN value was not encoded in its canonical form (`f97e00`)
|
|
2495
|
+
*/ | {
|
|
2496
|
+
readonly type: "NonCanonicalNumeric";
|
|
2497
|
+
}
|
|
2498
|
+
/**
|
|
2499
|
+
* An invalid CBOR simple value was encountered during decoding.
|
|
2500
|
+
*
|
|
2501
|
+
* Per Section 2.4 of the dCBOR specification, only `false`, `true`,
|
|
2502
|
+
* `null`, and floating point values are valid simple values in dCBOR.
|
|
2503
|
+
* All other major type 7 values are invalid.
|
|
2504
|
+
*/ | {
|
|
2505
|
+
readonly type: "InvalidSimpleValue";
|
|
2506
|
+
}
|
|
2507
|
+
/**
|
|
2508
|
+
* A CBOR text string was not valid UTF-8. The parameter contains the
|
|
2509
|
+
* specific error message.
|
|
2510
|
+
*
|
|
2511
|
+
* All CBOR text strings (major type 3) must be valid UTF-8 per RFC 8949.
|
|
2512
|
+
*/ | {
|
|
2513
|
+
readonly type: "InvalidString";
|
|
2514
|
+
readonly message: string;
|
|
2515
|
+
}
|
|
2516
|
+
/**
|
|
2517
|
+
* A CBOR text string was not encoded in Unicode Canonical Normalization
|
|
2518
|
+
* Form C (NFC).
|
|
2519
|
+
*
|
|
2520
|
+
* Per Section 2.5 of the dCBOR specification, all text strings must be in
|
|
2521
|
+
* NFC form, and decoders must reject any encoded text strings that are
|
|
2522
|
+
* not in NFC.
|
|
2523
|
+
*/ | {
|
|
2524
|
+
readonly type: "NonCanonicalString";
|
|
2525
|
+
}
|
|
2526
|
+
/**
|
|
2527
|
+
* The decoded CBOR item didn't consume all input data.
|
|
2528
|
+
* The parameter contains the number of unused bytes.
|
|
2529
|
+
*
|
|
2530
|
+
* This error occurs when decoding functions expect exactly one CBOR item
|
|
2531
|
+
* but the input contains additional data after a valid CBOR item.
|
|
2532
|
+
*/ | {
|
|
2533
|
+
readonly type: "UnusedData";
|
|
2534
|
+
readonly count: number;
|
|
2535
|
+
}
|
|
2536
|
+
/**
|
|
2537
|
+
* The keys in a decoded CBOR map were not in the canonical lexicographic order
|
|
2538
|
+
* of their encoding.
|
|
2539
|
+
*
|
|
2540
|
+
* Per the CDE specification and Section 2.1 of dCBOR, map keys must be in
|
|
2541
|
+
* ascending lexicographic order of their encoded representation for
|
|
2542
|
+
* deterministic encoding.
|
|
2543
|
+
*/ | {
|
|
2544
|
+
readonly type: "MisorderedMapKey";
|
|
2545
|
+
}
|
|
2546
|
+
/**
|
|
2547
|
+
* A decoded CBOR map contains duplicate keys, which is invalid.
|
|
2548
|
+
*
|
|
2549
|
+
* Per Section 2.2 of the dCBOR specification, CBOR maps must not contain
|
|
2550
|
+
* duplicate keys, and decoders must reject encoded maps with duplicate
|
|
2551
|
+
* keys.
|
|
2552
|
+
*/ | {
|
|
2553
|
+
readonly type: "DuplicateMapKey";
|
|
2554
|
+
}
|
|
2555
|
+
/**
|
|
2556
|
+
* A requested key was not found in a CBOR map during data extraction.
|
|
2557
|
+
*/ | {
|
|
2558
|
+
readonly type: "MissingMapKey";
|
|
2559
|
+
}
|
|
2560
|
+
/**
|
|
2561
|
+
* A CBOR numeric value could not be represented in the specified target
|
|
2562
|
+
* numeric type.
|
|
2563
|
+
*
|
|
2564
|
+
* This occurs when attempting to convert a CBOR number to a numeric
|
|
2565
|
+
* type that is too small to represent the value without loss of
|
|
2566
|
+
* precision.
|
|
2567
|
+
*/ | {
|
|
2568
|
+
readonly type: "OutOfRange";
|
|
2569
|
+
}
|
|
2570
|
+
/**
|
|
2571
|
+
* The CBOR value is not of the expected type for a conversion or
|
|
2572
|
+
* operation.
|
|
2573
|
+
*
|
|
2574
|
+
* This occurs when attempting to convert a CBOR value to a type that
|
|
2575
|
+
* doesn't match the actual CBOR item's type (e.g., trying to convert a
|
|
2576
|
+
* string to an integer).
|
|
2577
|
+
*/ | {
|
|
2578
|
+
readonly type: "WrongType";
|
|
2579
|
+
}
|
|
2580
|
+
/**
|
|
2581
|
+
* The CBOR tagged value had a different tag than expected.
|
|
2582
|
+
* Contains the expected tag and the actual tag found.
|
|
2583
|
+
*/ | {
|
|
2584
|
+
readonly type: "WrongTag";
|
|
2585
|
+
readonly expected: Tag;
|
|
2586
|
+
readonly actual: Tag;
|
|
2587
|
+
}
|
|
2588
|
+
/**
|
|
2589
|
+
* Invalid UTF‑8 in a text string.
|
|
2590
|
+
*/ | {
|
|
2591
|
+
readonly type: "InvalidUtf8";
|
|
2592
|
+
readonly message: string;
|
|
2593
|
+
}
|
|
2594
|
+
/**
|
|
2595
|
+
* Invalid ISO 8601 date format.
|
|
2596
|
+
*/ | {
|
|
2597
|
+
readonly type: "InvalidDate";
|
|
2598
|
+
readonly message: string;
|
|
2599
|
+
}
|
|
2600
|
+
/**
|
|
2601
|
+
* Custom error message.
|
|
2602
|
+
*/ | {
|
|
2603
|
+
readonly type: "Custom";
|
|
2604
|
+
readonly message: string;
|
|
2605
|
+
};
|
|
2606
|
+
/**
|
|
2607
|
+
* Create a custom error with a message.
|
|
2608
|
+
*
|
|
2609
|
+
* Matches Rust's `Error::msg()` method.
|
|
2610
|
+
*/
|
|
2611
|
+
declare const errorMsg: (message: string) => Error;
|
|
2612
|
+
/**
|
|
2613
|
+
* Convert an Error to a display string.
|
|
2614
|
+
*
|
|
2615
|
+
* Matches Rust's `Display` trait / `to_string()` method.
|
|
2616
|
+
*/
|
|
2617
|
+
declare const errorToString: (error: Error) => string;
|
|
2618
|
+
/**
|
|
2619
|
+
* Result type matching Rust's `Result<T, Error>`.
|
|
2620
|
+
*
|
|
2621
|
+
* In TypeScript, we use a discriminated union for Result instead of
|
|
2622
|
+
* try/catch for better type safety and Rust compatibility.
|
|
2623
|
+
*/
|
|
2624
|
+
type Result<T> = {
|
|
2625
|
+
ok: true;
|
|
2626
|
+
value: T;
|
|
2627
|
+
} | {
|
|
2628
|
+
ok: false;
|
|
2629
|
+
error: Error;
|
|
2630
|
+
};
|
|
2631
|
+
/**
|
|
2632
|
+
* Create a successful Result.
|
|
2633
|
+
*/
|
|
2634
|
+
declare const Ok: <T>(value: T) => Result<T>;
|
|
2635
|
+
/**
|
|
2636
|
+
* Create a failed Result.
|
|
2637
|
+
*/
|
|
2638
|
+
declare const Err: <T>(error: Error) => Result<T>;
|
|
2639
|
+
/**
|
|
2640
|
+
* Typed error class for all CBOR-related errors.
|
|
2641
|
+
*
|
|
2642
|
+
* Wraps the discriminated union Error type in a JavaScript Error object
|
|
2643
|
+
* for proper error handling with stack traces.
|
|
2644
|
+
*
|
|
2645
|
+
* @example
|
|
2646
|
+
* ```typescript
|
|
2647
|
+
* throw new CborError({ type: 'Underrun' });
|
|
2648
|
+
* throw new CborError({ type: 'WrongTag', expected: tag1, actual: tag2 });
|
|
2649
|
+
* ```
|
|
2650
|
+
*/
|
|
2651
|
+
declare class CborError extends Error {
|
|
2652
|
+
/**
|
|
2653
|
+
* The structured error information.
|
|
2654
|
+
*/
|
|
2655
|
+
readonly errorType: Error;
|
|
2656
|
+
/**
|
|
2657
|
+
* Create a new CborError.
|
|
2658
|
+
*
|
|
2659
|
+
* @param errorType - The discriminated union error type
|
|
2660
|
+
* @param message - Optional custom message (defaults to errorToString(errorType))
|
|
2661
|
+
*/
|
|
2662
|
+
constructor(errorType: Error, message?: string);
|
|
2663
|
+
/**
|
|
2664
|
+
* Check if an error is a CborError.
|
|
2665
|
+
*
|
|
2666
|
+
* @param error - Error to check
|
|
2667
|
+
* @returns True if error is a CborError
|
|
2668
|
+
*/
|
|
2669
|
+
static isCborError(error: unknown): error is CborError;
|
|
2670
|
+
}
|
|
2671
|
+
//#endregion
|
|
2672
|
+
//#region src/float.d.ts
|
|
2673
|
+
/**
|
|
2674
|
+
* Check if a number has a fractional part.
|
|
2675
|
+
*/
|
|
2676
|
+
declare const hasFractionalPart: (n: number) => boolean;
|
|
2677
|
+
//#endregion
|
|
2678
|
+
//#region src/varint.d.ts
|
|
2679
|
+
declare const encodeVarInt: (value: CborNumber, majorType: MajorType) => Uint8Array;
|
|
2680
|
+
declare const decodeVarIntData: (dataView: DataView, offset: number) => {
|
|
2681
|
+
majorType: MajorType;
|
|
2682
|
+
value: CborNumber;
|
|
2683
|
+
offset: number;
|
|
2684
|
+
};
|
|
2685
|
+
declare const decodeVarInt: (data: Uint8Array) => {
|
|
2686
|
+
majorType: MajorType;
|
|
2687
|
+
value: CborNumber;
|
|
2688
|
+
offset: number;
|
|
2689
|
+
};
|
|
2690
|
+
//#endregion
|
|
2691
|
+
//#region src/conveniences.d.ts
|
|
2692
|
+
/**
|
|
2693
|
+
* Extract native JavaScript value from CBOR.
|
|
2694
|
+
* Converts CBOR types to their JavaScript equivalents.
|
|
2695
|
+
*/
|
|
2696
|
+
declare const extractCbor: (cbor: Cbor | Uint8Array) => unknown;
|
|
2697
|
+
/**
|
|
2698
|
+
* Check if CBOR value is an unsigned integer.
|
|
2699
|
+
*
|
|
2700
|
+
* @param cbor - CBOR value to check
|
|
2701
|
+
* @returns True if value is unsigned integer
|
|
2702
|
+
*
|
|
2703
|
+
* @example
|
|
2704
|
+
* ```typescript
|
|
2705
|
+
* if (isUnsigned(value)) {
|
|
2706
|
+
* console.log('Unsigned:', value.value);
|
|
2707
|
+
* }
|
|
2708
|
+
* ```
|
|
2709
|
+
*/
|
|
2710
|
+
declare const isUnsigned: (cbor: Cbor) => cbor is CborUnsignedType & CborMethods;
|
|
2711
|
+
/**
|
|
2712
|
+
* Check if CBOR value is a negative integer.
|
|
2713
|
+
*
|
|
2714
|
+
* @param cbor - CBOR value to check
|
|
2715
|
+
* @returns True if value is negative integer
|
|
2716
|
+
*/
|
|
2717
|
+
declare const isNegative: (cbor: Cbor) => cbor is CborNegativeType & CborMethods;
|
|
2718
|
+
/**
|
|
2719
|
+
* Check if CBOR value is any integer (unsigned or negative).
|
|
2720
|
+
*
|
|
2721
|
+
* @param cbor - CBOR value to check
|
|
2722
|
+
* @returns True if value is an integer
|
|
2723
|
+
*/
|
|
2724
|
+
declare const isInteger: (cbor: Cbor) => cbor is (CborUnsignedType | CborNegativeType) & CborMethods;
|
|
2725
|
+
/**
|
|
2726
|
+
* Check if CBOR value is a byte string.
|
|
2727
|
+
*
|
|
2728
|
+
* @param cbor - CBOR value to check
|
|
2729
|
+
* @returns True if value is byte string
|
|
2730
|
+
*/
|
|
2731
|
+
declare const isBytes: (cbor: Cbor) => cbor is CborByteStringType & CborMethods;
|
|
2732
|
+
/**
|
|
2733
|
+
* Check if CBOR value is a text string.
|
|
2734
|
+
*
|
|
2735
|
+
* @param cbor - CBOR value to check
|
|
2736
|
+
* @returns True if value is text string
|
|
2737
|
+
*/
|
|
2738
|
+
declare const isText: (cbor: Cbor) => cbor is CborTextType & CborMethods;
|
|
2739
|
+
/**
|
|
2740
|
+
* Check if CBOR value is an array.
|
|
2741
|
+
*
|
|
2742
|
+
* @param cbor - CBOR value to check
|
|
2743
|
+
* @returns True if value is array
|
|
2744
|
+
*/
|
|
2745
|
+
declare const isArray: (cbor: Cbor) => cbor is CborArrayType & CborMethods;
|
|
2746
|
+
/**
|
|
2747
|
+
* Check if CBOR value is a map.
|
|
2748
|
+
*
|
|
2749
|
+
* @param cbor - CBOR value to check
|
|
2750
|
+
* @returns True if value is map
|
|
2751
|
+
*/
|
|
2752
|
+
declare const isMap: (cbor: Cbor) => cbor is CborMapType & CborMethods;
|
|
2753
|
+
/**
|
|
2754
|
+
* Check if CBOR value is tagged.
|
|
2755
|
+
*
|
|
2756
|
+
* @param cbor - CBOR value to check
|
|
2757
|
+
* @returns True if value is tagged
|
|
2758
|
+
*/
|
|
2759
|
+
declare const isTagged: (cbor: Cbor) => cbor is CborTaggedType & CborMethods;
|
|
2760
|
+
/**
|
|
2761
|
+
* Check if CBOR value is a simple value.
|
|
2762
|
+
*
|
|
2763
|
+
* @param cbor - CBOR value to check
|
|
2764
|
+
* @returns True if value is simple
|
|
2765
|
+
*/
|
|
2766
|
+
declare const isSimple: (cbor: Cbor) => cbor is CborSimpleType & CborMethods;
|
|
2767
|
+
/**
|
|
2768
|
+
* Check if CBOR value is a boolean (true or false).
|
|
2769
|
+
*
|
|
2770
|
+
* @param cbor - CBOR value to check
|
|
2771
|
+
* @returns True if value is boolean
|
|
2772
|
+
*/
|
|
2773
|
+
declare const isBoolean: (cbor: Cbor) => cbor is CborSimpleType & CborMethods & {
|
|
2774
|
+
readonly value: {
|
|
2775
|
+
readonly type: "False";
|
|
2776
|
+
} | {
|
|
2777
|
+
readonly type: "True";
|
|
2778
|
+
};
|
|
2779
|
+
};
|
|
2780
|
+
/**
|
|
2781
|
+
* Check if CBOR value is null.
|
|
2782
|
+
*
|
|
2783
|
+
* @param cbor - CBOR value to check
|
|
2784
|
+
* @returns True if value is null
|
|
2785
|
+
*/
|
|
2786
|
+
declare const isNull: (cbor: Cbor) => cbor is CborSimpleType & CborMethods & {
|
|
2787
|
+
readonly value: {
|
|
2788
|
+
readonly type: "Null";
|
|
2789
|
+
};
|
|
2790
|
+
};
|
|
2791
|
+
/**
|
|
2792
|
+
* Check if CBOR value is a float (f16, f32, or f64).
|
|
2793
|
+
*
|
|
2794
|
+
* @param cbor - CBOR value to check
|
|
2795
|
+
* @returns True if value is float
|
|
2796
|
+
*/
|
|
2797
|
+
declare const isFloat: (cbor: Cbor) => cbor is CborSimpleType & CborMethods & {
|
|
2798
|
+
readonly value: {
|
|
2799
|
+
readonly type: "Float";
|
|
2800
|
+
readonly value: number;
|
|
2801
|
+
};
|
|
2802
|
+
};
|
|
2803
|
+
/**
|
|
2804
|
+
* Extract unsigned integer value if type matches.
|
|
2805
|
+
*
|
|
2806
|
+
* @param cbor - CBOR value
|
|
2807
|
+
* @returns Unsigned integer or undefined
|
|
2808
|
+
*/
|
|
2809
|
+
declare const asUnsigned: (cbor: Cbor) => number | bigint | undefined;
|
|
2810
|
+
/**
|
|
2811
|
+
* Extract negative integer value if type matches.
|
|
2812
|
+
*
|
|
2813
|
+
* @param cbor - CBOR value
|
|
2814
|
+
* @returns Negative integer or undefined
|
|
2815
|
+
*/
|
|
2816
|
+
declare const asNegative: (cbor: Cbor) => number | bigint | undefined;
|
|
2817
|
+
/**
|
|
2818
|
+
* Extract any integer value (unsigned or negative) if type matches.
|
|
2819
|
+
*
|
|
2820
|
+
* @param cbor - CBOR value
|
|
2821
|
+
* @returns Integer or undefined
|
|
2822
|
+
*/
|
|
2823
|
+
declare const asInteger: (cbor: Cbor) => number | bigint | undefined;
|
|
2824
|
+
/**
|
|
2825
|
+
* Extract byte string value if type matches.
|
|
2826
|
+
*
|
|
2827
|
+
* @param cbor - CBOR value
|
|
2828
|
+
* @returns Byte string or undefined
|
|
2829
|
+
*/
|
|
2830
|
+
declare const asBytes: (cbor: Cbor) => Uint8Array | undefined;
|
|
2831
|
+
/**
|
|
2832
|
+
* Extract text string value if type matches.
|
|
2833
|
+
*
|
|
2834
|
+
* @param cbor - CBOR value
|
|
2835
|
+
* @returns Text string or undefined
|
|
2836
|
+
*/
|
|
2837
|
+
declare const asText: (cbor: Cbor) => string | undefined;
|
|
2838
|
+
/**
|
|
2839
|
+
* Extract array value if type matches.
|
|
2840
|
+
*
|
|
2841
|
+
* @param cbor - CBOR value
|
|
2842
|
+
* @returns Array or undefined
|
|
2843
|
+
*/
|
|
2844
|
+
declare const asArray: (cbor: Cbor) => readonly Cbor[] | undefined;
|
|
2845
|
+
/**
|
|
2846
|
+
* Extract map value if type matches.
|
|
2847
|
+
*
|
|
2848
|
+
* @param cbor - CBOR value
|
|
2849
|
+
* @returns Map or undefined
|
|
2850
|
+
*/
|
|
2851
|
+
declare const asMap: (cbor: Cbor) => CborMap | undefined;
|
|
2852
|
+
/**
|
|
2853
|
+
* Extract boolean value if type matches.
|
|
2854
|
+
*
|
|
2855
|
+
* @param cbor - CBOR value
|
|
2856
|
+
* @returns Boolean or undefined
|
|
2857
|
+
*/
|
|
2858
|
+
declare const asBoolean: (cbor: Cbor) => boolean | undefined;
|
|
2859
|
+
/**
|
|
2860
|
+
* Extract float value if type matches.
|
|
2861
|
+
*
|
|
2862
|
+
* @param cbor - CBOR value
|
|
2863
|
+
* @returns Float or undefined
|
|
2864
|
+
*/
|
|
2865
|
+
declare const asFloat: (cbor: Cbor) => number | undefined;
|
|
2866
|
+
/**
|
|
2867
|
+
* Extract any numeric value (integer or float).
|
|
2868
|
+
*
|
|
2869
|
+
* @param cbor - CBOR value
|
|
2870
|
+
* @returns Number or undefined
|
|
2871
|
+
*/
|
|
2872
|
+
declare const asNumber: (cbor: Cbor) => CborNumber | undefined;
|
|
2873
|
+
/**
|
|
2874
|
+
* Extract unsigned integer value, throwing if type doesn't match.
|
|
2875
|
+
*
|
|
2876
|
+
* @param cbor - CBOR value
|
|
2877
|
+
* @returns Unsigned integer
|
|
2878
|
+
* @throws {CborError} With type 'WrongType' if cbor is not an unsigned integer
|
|
2879
|
+
*/
|
|
2880
|
+
declare const expectUnsigned: (cbor: Cbor) => number | bigint;
|
|
2881
|
+
/**
|
|
2882
|
+
* Extract negative integer value, throwing if type doesn't match.
|
|
2883
|
+
*
|
|
2884
|
+
* @param cbor - CBOR value
|
|
2885
|
+
* @returns Negative integer
|
|
2886
|
+
* @throws {CborError} With type 'WrongType' if cbor is not a negative integer
|
|
2887
|
+
*/
|
|
2888
|
+
declare const expectNegative: (cbor: Cbor) => number | bigint;
|
|
2889
|
+
/**
|
|
2890
|
+
* Extract any integer value, throwing if type doesn't match.
|
|
2891
|
+
*
|
|
2892
|
+
* @param cbor - CBOR value
|
|
2893
|
+
* @returns Integer
|
|
2894
|
+
* @throws {CborError} With type 'WrongType' if cbor is not an integer
|
|
2895
|
+
*/
|
|
2896
|
+
declare const expectInteger: (cbor: Cbor) => number | bigint;
|
|
2897
|
+
/**
|
|
2898
|
+
* Extract byte string value, throwing if type doesn't match.
|
|
2899
|
+
*
|
|
2900
|
+
* @param cbor - CBOR value
|
|
2901
|
+
* @returns Byte string
|
|
2902
|
+
* @throws {CborError} With type 'WrongType' if cbor is not a byte string
|
|
2903
|
+
*/
|
|
2904
|
+
declare const expectBytes: (cbor: Cbor) => Uint8Array;
|
|
2905
|
+
/**
|
|
2906
|
+
* Extract text string value, throwing if type doesn't match.
|
|
2907
|
+
*
|
|
2908
|
+
* @param cbor - CBOR value
|
|
2909
|
+
* @returns Text string
|
|
2910
|
+
* @throws {CborError} With type 'WrongType' if cbor is not a text string
|
|
2911
|
+
*/
|
|
2912
|
+
declare const expectText: (cbor: Cbor) => string;
|
|
2913
|
+
/**
|
|
2914
|
+
* Extract array value, throwing if type doesn't match.
|
|
2915
|
+
*
|
|
2916
|
+
* @param cbor - CBOR value
|
|
2917
|
+
* @returns Array
|
|
2918
|
+
* @throws {CborError} With type 'WrongType' if cbor is not an array
|
|
2919
|
+
*/
|
|
2920
|
+
declare const expectArray: (cbor: Cbor) => readonly Cbor[];
|
|
2921
|
+
/**
|
|
2922
|
+
* Extract map value, throwing if type doesn't match.
|
|
2923
|
+
*
|
|
2924
|
+
* @param cbor - CBOR value
|
|
2925
|
+
* @returns Map
|
|
2926
|
+
* @throws {CborError} With type 'WrongType' if cbor is not a map
|
|
2927
|
+
*/
|
|
2928
|
+
declare const expectMap: (cbor: Cbor) => CborMap;
|
|
2929
|
+
/**
|
|
2930
|
+
* Extract boolean value, throwing if type doesn't match.
|
|
2931
|
+
*
|
|
2932
|
+
* @param cbor - CBOR value
|
|
2933
|
+
* @returns Boolean
|
|
2934
|
+
* @throws {CborError} With type 'WrongType' if cbor is not a boolean
|
|
2935
|
+
*/
|
|
2936
|
+
declare const expectBoolean: (cbor: Cbor) => boolean;
|
|
2937
|
+
/**
|
|
2938
|
+
* Extract float value, throwing if type doesn't match.
|
|
2939
|
+
*
|
|
2940
|
+
* @param cbor - CBOR value
|
|
2941
|
+
* @returns Float
|
|
2942
|
+
* @throws {CborError} With type 'WrongType' if cbor is not a float
|
|
2943
|
+
*/
|
|
2944
|
+
declare const expectFloat: (cbor: Cbor) => number;
|
|
2945
|
+
/**
|
|
2946
|
+
* Extract any numeric value, throwing if type doesn't match.
|
|
2947
|
+
*
|
|
2948
|
+
* @param cbor - CBOR value
|
|
2949
|
+
* @returns Number
|
|
2950
|
+
* @throws {CborError} With type 'WrongType' if cbor is not a number
|
|
2951
|
+
*/
|
|
2952
|
+
declare const expectNumber: (cbor: Cbor) => CborNumber;
|
|
2953
|
+
/**
|
|
2954
|
+
* Get array item at index.
|
|
2955
|
+
*
|
|
2956
|
+
* @param cbor - CBOR value (must be array)
|
|
2957
|
+
* @param index - Array index
|
|
2958
|
+
* @returns Item at index or undefined
|
|
2959
|
+
*/
|
|
2960
|
+
declare const arrayItem: (cbor: Cbor, index: number) => Cbor | undefined;
|
|
2961
|
+
/**
|
|
2962
|
+
* Get array length.
|
|
2963
|
+
*
|
|
2964
|
+
* @param cbor - CBOR value (must be array)
|
|
2965
|
+
* @returns Array length or undefined
|
|
2966
|
+
*/
|
|
2967
|
+
declare const arrayLength: (cbor: Cbor) => number | undefined;
|
|
2968
|
+
/**
|
|
2969
|
+
* Check if array is empty.
|
|
2970
|
+
*
|
|
2971
|
+
* @param cbor - CBOR value (must be array)
|
|
2972
|
+
* @returns True if empty, false if not empty, undefined if not array
|
|
2973
|
+
*/
|
|
2974
|
+
declare const arrayIsEmpty: (cbor: Cbor) => boolean | undefined;
|
|
2975
|
+
/**
|
|
2976
|
+
* Get map value by key.
|
|
2977
|
+
*
|
|
2978
|
+
* @param cbor - CBOR value (must be map)
|
|
2979
|
+
* @param key - Map key
|
|
2980
|
+
* @returns Value for key or undefined
|
|
2981
|
+
*/
|
|
2982
|
+
declare function mapValue<K extends CborInput, V>(cbor: Cbor, key: K): V | undefined;
|
|
2983
|
+
/**
|
|
2984
|
+
* Check if map has key.
|
|
2985
|
+
*
|
|
2986
|
+
* @param cbor - CBOR value (must be map)
|
|
2987
|
+
* @param key - Map key
|
|
2988
|
+
* @returns True if key exists, false otherwise, undefined if not map
|
|
2989
|
+
*/
|
|
2990
|
+
declare function mapHas<K extends CborInput>(cbor: Cbor, key: K): boolean | undefined;
|
|
2991
|
+
/**
|
|
2992
|
+
* Get all map keys.
|
|
2993
|
+
*
|
|
2994
|
+
* @param cbor - CBOR value (must be map)
|
|
2995
|
+
* @returns Array of keys or undefined
|
|
2996
|
+
*/
|
|
2997
|
+
declare const mapKeys: (cbor: Cbor) => Cbor[] | undefined;
|
|
2998
|
+
/**
|
|
2999
|
+
* Get all map values.
|
|
3000
|
+
*
|
|
3001
|
+
* @param cbor - CBOR value (must be map)
|
|
3002
|
+
* @returns Array of values or undefined
|
|
3003
|
+
*/
|
|
3004
|
+
declare const mapValues: (cbor: Cbor) => Cbor[] | undefined;
|
|
3005
|
+
/**
|
|
3006
|
+
* Get map size.
|
|
3007
|
+
*
|
|
3008
|
+
* @param cbor - CBOR value (must be map)
|
|
3009
|
+
* @returns Map size or undefined
|
|
3010
|
+
*/
|
|
3011
|
+
declare const mapSize: (cbor: Cbor) => number | undefined;
|
|
3012
|
+
/**
|
|
3013
|
+
* Check if map is empty.
|
|
3014
|
+
*
|
|
3015
|
+
* @param cbor - CBOR value (must be map)
|
|
3016
|
+
* @returns True if empty, false if not empty, undefined if not map
|
|
3017
|
+
*/
|
|
3018
|
+
declare const mapIsEmpty: (cbor: Cbor) => boolean | undefined;
|
|
3019
|
+
/**
|
|
3020
|
+
* Get tag value from tagged CBOR.
|
|
3021
|
+
*
|
|
3022
|
+
* @param cbor - CBOR value (must be tagged)
|
|
3023
|
+
* @returns Tag value or undefined
|
|
3024
|
+
*/
|
|
3025
|
+
declare const tagValue: (cbor: Cbor) => number | bigint | undefined;
|
|
3026
|
+
/**
|
|
3027
|
+
* Get content from tagged CBOR.
|
|
3028
|
+
*
|
|
3029
|
+
* @param cbor - CBOR value (must be tagged)
|
|
3030
|
+
* @returns Tagged content or undefined
|
|
3031
|
+
*/
|
|
3032
|
+
declare const tagContent: (cbor: Cbor) => Cbor | undefined;
|
|
3033
|
+
/**
|
|
3034
|
+
* Check if CBOR has a specific tag.
|
|
3035
|
+
*
|
|
3036
|
+
* @param cbor - CBOR value
|
|
3037
|
+
* @param tag - Tag value to check
|
|
3038
|
+
* @returns True if has tag, false otherwise
|
|
3039
|
+
*/
|
|
3040
|
+
declare const hasTag: (cbor: Cbor, tag: number | bigint) => boolean;
|
|
3041
|
+
/**
|
|
3042
|
+
* Extract content if has specific tag.
|
|
3043
|
+
*
|
|
3044
|
+
* @param cbor - CBOR value
|
|
3045
|
+
* @param tag - Expected tag value
|
|
3046
|
+
* @returns Tagged content or undefined
|
|
3047
|
+
*/
|
|
3048
|
+
declare const getTaggedContent: (cbor: Cbor, tag: number | bigint) => Cbor | undefined;
|
|
3049
|
+
/**
|
|
3050
|
+
* Extract content if has specific tag, throwing if not.
|
|
3051
|
+
*
|
|
3052
|
+
* @param cbor - CBOR value
|
|
3053
|
+
* @param tag - Expected tag value
|
|
3054
|
+
* @returns Tagged content
|
|
3055
|
+
* @throws {CborError} With type 'WrongType' if cbor is not tagged with the expected tag
|
|
3056
|
+
*/
|
|
3057
|
+
declare const expectTaggedContent: (cbor: Cbor, tag: number | bigint) => Cbor;
|
|
3058
|
+
/**
|
|
3059
|
+
* Extract tagged value as tuple [Tag, Cbor] if CBOR is tagged.
|
|
3060
|
+
* This is used by envelope for decoding.
|
|
3061
|
+
*
|
|
3062
|
+
* @param cbor - CBOR value
|
|
3063
|
+
* @returns [Tag, Cbor] tuple or undefined
|
|
3064
|
+
*/
|
|
3065
|
+
declare const asTaggedValue: (cbor: Cbor) => [Tag, Cbor] | undefined;
|
|
3066
|
+
/**
|
|
3067
|
+
* Alias for asBytes - extract byte string value if type matches.
|
|
3068
|
+
* Named asByteString for envelope compatibility.
|
|
3069
|
+
*
|
|
3070
|
+
* @param cbor - CBOR value
|
|
3071
|
+
* @returns Byte string or undefined
|
|
3072
|
+
*/
|
|
3073
|
+
declare const asByteString: (cbor: Cbor) => Uint8Array | undefined;
|
|
3074
|
+
/**
|
|
3075
|
+
* A wrapper around CBOR arrays that provides a get(index) method
|
|
3076
|
+
* for envelope compatibility.
|
|
3077
|
+
*/
|
|
3078
|
+
interface CborArrayWrapper {
|
|
3079
|
+
readonly length: number;
|
|
3080
|
+
get(index: number): Cbor | undefined;
|
|
3081
|
+
[Symbol.iterator](): Iterator<Cbor>;
|
|
3082
|
+
}
|
|
3083
|
+
/**
|
|
3084
|
+
* Extract array value with get() method for envelope compatibility.
|
|
3085
|
+
*
|
|
3086
|
+
* @param cbor - CBOR value
|
|
3087
|
+
* @returns Array wrapper with get() method or undefined
|
|
3088
|
+
*/
|
|
3089
|
+
declare const asCborArray: (cbor: Cbor) => CborArrayWrapper | undefined;
|
|
3090
|
+
/**
|
|
3091
|
+
* Alias for asMap - extract map value if type matches.
|
|
3092
|
+
* Named asCborMap for envelope compatibility.
|
|
3093
|
+
*
|
|
3094
|
+
* @param cbor - CBOR value
|
|
3095
|
+
* @returns Map or undefined
|
|
3096
|
+
*/
|
|
3097
|
+
declare const asCborMap: (cbor: Cbor) => CborMap | undefined;
|
|
3098
|
+
/**
|
|
3099
|
+
* Check if CBOR value is any numeric type (unsigned, negative, or float).
|
|
3100
|
+
*
|
|
3101
|
+
* @param cbor - CBOR value
|
|
3102
|
+
* @returns True if value is numeric
|
|
3103
|
+
*/
|
|
3104
|
+
declare const isNumber: (cbor: Cbor) => boolean;
|
|
3105
|
+
//#endregion
|
|
3106
|
+
export { ByteString, Cbor, type CborArrayType, type CborArrayWrapper, type CborByteStringType, type CborCodable, CborDate, type CborDecodable, type CborEncodable, CborError, type CborInput, CborMap, type CborMapType, type CborMethods, type CborNegativeType, type CborNumber, CborSet, type CborSimpleType, type CborSummarizer, type CborTagged, type CborTaggedCodable, type CborTaggedDecodable, type CborTaggedEncodable, type CborTaggedType, type CborTextType, type CborUnsignedType, type DiagFormatOpts, type EdgeType, type EdgeTypeVariant, Err, type Error, type HexFormatOpts, MajorType, type MapEntry, Ok, type Result, type Simple, TAG_BASE16, TAG_BASE64, TAG_BASE64URL, TAG_BASE64URL_TEXT, TAG_BASE64_TEXT, TAG_BIGFLOAT, TAG_BINARY_UUID, TAG_DATE, TAG_DATE_TIME_STRING, TAG_DECIMAL_FRACTION, TAG_ENCODED_CBOR, TAG_EPOCH_DATE, TAG_EPOCH_DATE_TIME, TAG_MIME_MESSAGE, TAG_NAME_DATE, TAG_NEGATIVE_BIGNUM, TAG_POSITIVE_BIGNUM, TAG_REGEXP, TAG_SELF_DESCRIBE_CBOR, TAG_SET, TAG_STRING_REF_NAMESPACE, TAG_URI, TAG_UUID, type Tag, TagsStore, type TagsStoreTrait, type Visitor, type WalkElement, arrayIsEmpty, arrayItem, arrayLength, asArray, asBoolean, asByteString, asBytes, asCborArray, asCborMap, asFloat, asInteger, asKeyValue, asMap, asNegative, asNumber, asSingle, asTaggedValue, asText, asUnsigned, bytesToHex, cbor, cborData, collectAllText, collectAtLevel, countElements, createTag, createTaggedCbor, decodeCbor, decodeVarInt, decodeVarIntData, diagnosticOpt, edgeLabel, encodeVarInt, errorMsg, errorToString, expectArray, expectBoolean, expectBoolean as tryIntoBool, expectBytes, expectBytes as tryIntoByteString, expectFloat, expectInteger, expectMap, expectNegative, expectNumber, expectTaggedContent, expectTaggedContent as tryExpectedTaggedValue, expectText, expectText as tryIntoText, expectUnsigned, extractCbor, extractTaggedContent, findFirst, getGlobalTagsStore, getTaggedContent, hasFractionalPart, hasTag, hexOpt, hexToBytes, isArray, isBoolean, isBytes, isFloat, isInteger, isMap, isNaN, isNegative, isNull, isNumber, isSimple, isTagged, isText, isUnsigned, mapHas, mapIsEmpty, mapKeys, mapSize, mapValue, mapValues, maxDepth, registerTags, registerTagsIn, simpleName, summary, tagContent, tagValue, tagsForValues, toByteString, toByteStringFromHex, toTaggedValue, validateTag };
|
|
3107
|
+
//# sourceMappingURL=index.d.mts.map
|