@cbortech/cbor 0.25.0 → 0.25.2

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.
Files changed (42) hide show
  1. package/README.ja.md +98 -18
  2. package/README.md +99 -18
  3. package/dist/ast/CborAppSeqResult.d.ts +21 -0
  4. package/dist/ast/CborArray.d.ts +2 -2
  5. package/dist/ast/CborByteString.d.ts +2 -2
  6. package/dist/ast/CborEmbeddedCBOR.d.ts +2 -1
  7. package/dist/ast/CborFloat.d.ts +8 -1
  8. package/dist/ast/CborIndefiniteByteString.d.ts +2 -1
  9. package/dist/ast/CborIndefiniteTextString.d.ts +2 -1
  10. package/dist/ast/CborItem.d.ts +36 -3
  11. package/dist/ast/CborMap.d.ts +2 -2
  12. package/dist/ast/CborNint.d.ts +2 -2
  13. package/dist/ast/CborSimple.d.ts +2 -1
  14. package/dist/ast/CborTag.d.ts +2 -2
  15. package/dist/ast/CborTextString.d.ts +2 -2
  16. package/dist/ast/CborUint.d.ts +2 -2
  17. package/dist/ast/index.cjs +1 -1
  18. package/dist/ast/index.js +1 -1
  19. package/dist/cbor/encode.d.ts +38 -4
  20. package/dist/cdn/serialize-utils.d.ts +17 -4
  21. package/dist/cdn/test-vectors/runner.d.ts +22 -0
  22. package/dist/cdn/tokenizer.d.ts +49 -22
  23. package/dist/extensions/b32.d.ts +5 -0
  24. package/dist/extensions/dt.d.ts +1 -10
  25. package/dist/extensions/float.d.ts +7 -0
  26. package/dist/extensions/same.d.ts +7 -0
  27. package/dist/extensions/types.d.ts +26 -4
  28. package/dist/index.cjs +5 -2
  29. package/dist/index.cjs.map +1 -1
  30. package/dist/index.d.ts +4 -1
  31. package/dist/index.js +233 -84
  32. package/dist/index.js.map +1 -1
  33. package/dist/{mapEntries-Da-2HMRf.js → mapEntries-BMMPg2FB.js} +1562 -1159
  34. package/dist/mapEntries-BMMPg2FB.js.map +1 -0
  35. package/dist/mapEntries-DZKHc3VY.cjs +39 -0
  36. package/dist/mapEntries-DZKHc3VY.cjs.map +1 -0
  37. package/dist/types.d.ts +101 -6
  38. package/dist/utils/strip-comments.d.ts +12 -0
  39. package/package.json +13 -10
  40. package/dist/mapEntries-CNxwMt7o.cjs +0 -31
  41. package/dist/mapEntries-CNxwMt7o.cjs.map +0 -1
  42. package/dist/mapEntries-Da-2HMRf.js.map +0 -1
package/README.ja.md CHANGED
@@ -326,37 +326,117 @@ console.log(text);
326
326
 
327
327
  ## オプション extension
328
328
 
329
+ このパッケージには、デフォルト有効ではないものの本体に同梱されている
330
+ extension があります。必要なものを `import` し、
331
+ `extensions` オプションに渡して使います。
332
+
333
+ ### b32 / h32
334
+
335
+ [RFC 4648](https://www.rfc-editor.org/rfc/rfc4648) の Base32 エンコードによるバイト列リテラルです。
336
+ [RFC 8949](https://www.rfc-editor.org/rfc/rfc8949) §8 に記載があり、[draft-ietf-cbor-edn-literals](https://datatracker.ietf.org/doc/draft-ietf-cbor-edn-literals/25/) でも触れられています。
337
+
338
+ - `b32` — §6 Base32(`A–Z 2–7` アルファベット)
339
+ - `h32` — §7 Base32Hex(`0–9 A–V` アルファベット)
340
+
341
+ ```ts
342
+ import { CBOR, b32, h32 } from '@cbortech/cbor';
343
+
344
+ const v1 = CBOR.fromCDN("b32'AEBAGBA'", { extensions: [b32] });
345
+ console.log(v1.toCDN({ appStrings: false }));
346
+ // h'01020304'
347
+
348
+ const v2 = CBOR.fromCDN("h32'00P00'", { extensions: [h32] });
349
+ console.log(v2.toCDN({ appStrings: false }));
350
+ // h'003200'
351
+ ```
352
+
353
+ ### float
354
+
355
+ 16 進数のビットパターンを IEEE 754 浮動小数点値として解釈します。
356
+ [draft-bormann-cbor-edn-app-ext](https://datatracker.ietf.org/doc/draft-bormann-cbor-edn-app-ext/)
357
+ に記載があり、[cbor-test-vectors](https://github.com/cbor-wg/cbor-test-vectors) でも使われています。
358
+
359
+ ```ts
360
+ import { CBOR, float } from '@cbortech/cbor';
361
+
362
+ const v = CBOR.fromCDN("float'7e00'", { extensions: [float] });
363
+ console.log(v.toCDN({ appStrings: false }));
364
+ // NaN
365
+
366
+ // バイト列から解釈する場合
367
+ const v2 = CBOR.fromCDN("float<<h'3f800000'>>", { extensions: [float] });
368
+ console.log(v2.toCDN({ appStrings: false }));
369
+ // 1.0_2
370
+ ```
371
+
372
+ ### same
373
+
374
+ `same<<expr, expr, ...>>` は、シーケンス内のすべての要素が同一の CBOR
375
+ バイト列になることを検証し、最初の要素を返す extension です。
376
+ [draft-bormann-cbor-edn-app-ext](https://datatracker.ietf.org/doc/draft-bormann-cbor-edn-app-ext/)
377
+ に記載があります。
378
+
379
+ ```ts
380
+ import { CBOR, same } from '@cbortech/cbor';
381
+
382
+ // すべての要素が同じバイト列かを検証し、最初の要素を返す
383
+ const v = CBOR.fromCDN("same<<h'0102', h'0102'>>", { extensions: [same] });
384
+ console.log(v.toCDN({ appStrings: false }));
385
+ // h'0102'
386
+
387
+ // 要素が 1 つでも有効(常にパスする)
388
+ const v2 = CBOR.fromCDN('same<<42>>', { extensions: [same] });
389
+ console.log(v2.toCDN({ appStrings: false }));
390
+ // 42
391
+ ```
392
+
393
+ ---
394
+
329
395
  追加の application extension は別パッケージとして公開されています。必要なものを
330
396
  インストールし、`extensions` オプションに渡して使います。
331
397
 
332
- `hash` は CDN の仕様に含まれる application extension ですが、利用には
333
- 外部パッケージが必要です。そのため、このパッケージ本体には含めず、
334
- `@cbortech/hash-extension` として提供しています。
398
+ ### hash
335
399
 
336
- `uuid` は CDN の仕様には定められていない、このライブラリ独自の
337
- application extension です。仕様上の標準機能と区別するため、
338
- `@cbortech/uuid-extension` として別パッケージで提供しています。
400
+ `hash` は [draft-ietf-cbor-edn-literals](https://datatracker.ietf.org/doc/draft-ietf-cbor-edn-literals/25/) §3.3 で定義された標準の application extension です。
401
+ ハッシュアルゴリズムと値を `hash'algorithm:value'` の形式で表現します。
402
+ 実装には外部の暗号ライブラリが必要なため、[@cbortech/hash-extension](https://www.npmjs.com/package/@cbortech/hash-extension) として
403
+ 別パッケージで提供しています。
339
404
 
340
405
  ```bash
341
- npm install @cbortech/hash-extension @cbortech/uuid-extension
406
+ npm install @cbortech/hash-extension
342
407
  ```
343
408
 
344
409
  ```ts
345
410
  import { CBOR } from '@cbortech/cbor';
346
- import hashExtension from '@cbortech/hash-extension';
347
- import uuidExtension from '@cbortech/uuid-extension';
411
+ import { hash } from '@cbortech/hash-extension';
348
412
 
349
- const cbor = new CBOR({
350
- extensions: [hashExtension, uuidExtension],
351
- });
413
+ const cbor = new CBOR({ extensions: [hash] });
414
+
415
+ const digest = cbor.parse(
416
+ "hash'sha-256:47DEQpj8HBSa+/TImW+5JCeuQeRkm5NMpJWZG3hSuFU='"
417
+ );
418
+ // Uint8Array(32) [227, 176, 196, 66, 152, 252, 28, 20, 154, 251, 244, 200,
419
+ // 153, 111, 185, 36, 39, 174, 65, 228, 100, 155, 147, 76,
420
+ // 164, 149, 153, 27, 120, 82, 184, 85]
421
+ ```
422
+
423
+ ### uuid
424
+
425
+ `uuid` はこのライブラリ独自の application extension です。
426
+ [@cbortech/uuid-extension](https://www.npmjs.com/package/@cbortech/uuid-extension) として別パッケージで提供しています。
427
+
428
+ ```bash
429
+ npm install @cbortech/uuid-extension
430
+ ```
431
+
432
+ ```ts
433
+ import { CBOR } from '@cbortech/cbor';
434
+ import { uuid } from '@cbortech/uuid-extension';
352
435
 
353
- const digest = cbor.fromCDN("hash'foo'");
354
- console.log(digest.toCDN());
355
- // hash'foo'
436
+ const cbor = new CBOR({ extensions: [uuid] });
356
437
 
357
- const id = cbor.fromCDN("uuid'550e8400-e29b-41d4-a716-446655440000'");
358
- console.log(id.toCDN());
359
- // uuid'550e8400-e29b-41d4-a716-446655440000'
438
+ const id = cbor.parse("uuid'550e8400-e29b-41d4-a716-446655440000'");
439
+ // Uint8Array(16) [85, 14, 132, 0, 226, 155, 65, 212, 167, 22, 68, 102, 85, 68, 0, 0]
360
440
  ```
361
441
 
362
442
  ## タグ
package/README.md CHANGED
@@ -330,37 +330,118 @@ console.log(text);
330
330
 
331
331
  ## Optional Extensions
332
332
 
333
+ This package includes several bundled extensions that are not enabled by
334
+ default. Import what you need and pass it through the `extensions` option.
335
+
336
+ ### b32 / h32
337
+
338
+ Byte-string literals using [RFC 4648](https://www.rfc-editor.org/rfc/rfc4648)
339
+ Base32 encoding. These prefixes are described in §8 of
340
+ [RFC 8949](https://www.rfc-editor.org/rfc/rfc8949) and also mentioned in
341
+ [draft-ietf-cbor-edn-literals](https://datatracker.ietf.org/doc/draft-ietf-cbor-edn-literals/25/).
342
+
343
+ - `b32` — §6 Base32 (`A–Z 2–7` alphabet)
344
+ - `h32` — §7 Base32Hex (`0–9 A–V` alphabet)
345
+
346
+ ```ts
347
+ import { CBOR, b32, h32 } from '@cbortech/cbor';
348
+
349
+ const v1 = CBOR.fromCDN("b32'AEBAGBA'", { extensions: [b32] });
350
+ console.log(v1.toCDN({ appStrings: false }));
351
+ // h'01020304'
352
+
353
+ const v2 = CBOR.fromCDN("h32'00P00'", { extensions: [h32] });
354
+ console.log(v2.toCDN({ appStrings: false }));
355
+ // h'003200'
356
+ ```
357
+
358
+ ### float
359
+
360
+ Interprets a hex bit-pattern as an IEEE 754 floating-point value. This
361
+ extension is described in
362
+ [draft-bormann-cbor-edn-app-ext](https://datatracker.ietf.org/doc/draft-bormann-cbor-edn-app-ext/)
363
+ and also used in [cbor-test-vectors](https://github.com/cbor-wg/cbor-test-vectors).
364
+
365
+ ```ts
366
+ import { CBOR, float } from '@cbortech/cbor';
367
+
368
+ const v = CBOR.fromCDN("float'7e00'", { extensions: [float] });
369
+ console.log(v.toCDN({ appStrings: false }));
370
+ // NaN
371
+
372
+ // Interpret bytes as float bits
373
+ const v2 = CBOR.fromCDN("float<<h'3f800000'>>", { extensions: [float] });
374
+ console.log(v2.toCDN({ appStrings: false }));
375
+ // 1.0_2
376
+ ```
377
+
378
+ ### same
379
+
380
+ `same<<expr, expr, ...>>` verifies that every item in the sequence encodes to
381
+ identical CBOR bytes and returns the first item. This extension is described in
382
+ [draft-bormann-cbor-edn-app-ext](https://datatracker.ietf.org/doc/draft-bormann-cbor-edn-app-ext/).
383
+
384
+ ```ts
385
+ import { CBOR, same } from '@cbortech/cbor';
386
+
387
+ const v = CBOR.fromCDN("same<<h'0102', h'0102'>>", { extensions: [same] });
388
+ console.log(v.toCDN({ appStrings: false }));
389
+ // h'0102'
390
+
391
+ // A single-item sequence always passes
392
+ const v2 = CBOR.fromCDN('same<<42>>', { extensions: [same] });
393
+ console.log(v2.toCDN({ appStrings: false }));
394
+ // 42
395
+ ```
396
+
397
+ ---
398
+
333
399
  Additional application extensions are published as separate packages. Install
334
400
  the ones you need and pass them through the `extensions` option.
335
401
 
336
- `hash` is an application extension defined by the CDN specification, but
337
- it requires an external package. For that reason, it is not bundled with this
338
- package and is provided separately as `@cbortech/hash-extension`.
402
+ ### hash
339
403
 
340
- `uuid` is a library-specific application extension that is not defined by the
341
- CDN specification. To keep it distinct from standard CDN features, it
342
- is provided separately as `@cbortech/uuid-extension`.
404
+ `hash` is an application extension defined in §3.3 of
405
+ [draft-ietf-cbor-edn-literals](https://datatracker.ietf.org/doc/draft-ietf-cbor-edn-literals/25/).
406
+ It represents cryptographic hash values in the form `hash'algorithm:value'`.
407
+ Because it requires an external cryptographic library, it is provided separately
408
+ as [@cbortech/hash-extension](https://www.npmjs.com/package/@cbortech/hash-extension).
343
409
 
344
410
  ```bash
345
- npm install @cbortech/hash-extension @cbortech/uuid-extension
411
+ npm install @cbortech/hash-extension
346
412
  ```
347
413
 
348
414
  ```ts
349
415
  import { CBOR } from '@cbortech/cbor';
350
- import hashExtension from '@cbortech/hash-extension';
351
- import uuidExtension from '@cbortech/uuid-extension';
416
+ import { hash } from '@cbortech/hash-extension';
352
417
 
353
- const cbor = new CBOR({
354
- extensions: [hashExtension, uuidExtension],
355
- });
418
+ const cbor = new CBOR({ extensions: [hash] });
419
+
420
+ const digest = cbor.parse(
421
+ "hash'sha-256:47DEQpj8HBSa+/TImW+5JCeuQeRkm5NMpJWZG3hSuFU='"
422
+ );
423
+ // Uint8Array(32) [227, 176, 196, 66, 152, 252, 28, 20, 154, 251, 244, 200,
424
+ // 153, 111, 185, 36, 39, 174, 65, 228, 100, 155, 147, 76,
425
+ // 164, 149, 153, 27, 120, 82, 184, 85]
426
+ ```
427
+
428
+ ### uuid
429
+
430
+ `uuid` is a library-specific application extension, provided separately as
431
+ [@cbortech/uuid-extension](https://www.npmjs.com/package/@cbortech/uuid-extension).
432
+
433
+ ```bash
434
+ npm install @cbortech/uuid-extension
435
+ ```
436
+
437
+ ```ts
438
+ import { CBOR } from '@cbortech/cbor';
439
+ import { uuid } from '@cbortech/uuid-extension';
356
440
 
357
- const digest = cbor.fromCDN("hash'foo'");
358
- console.log(digest.toCDN());
359
- // hash'foo'
441
+ const cbor = new CBOR({ extensions: [uuid] });
360
442
 
361
- const id = cbor.fromCDN("uuid'550e8400-e29b-41d4-a716-446655440000'");
362
- console.log(id.toCDN());
363
- // uuid'550e8400-e29b-41d4-a716-446655440000'
443
+ const id = cbor.parse("uuid'550e8400-e29b-41d4-a716-446655440000'");
444
+ // Uint8Array(16) [85, 14, 132, 0, 226, 155, 65, 212, 167, 22, 68, 102, 85, 68, 0, 0]
364
445
  ```
365
446
 
366
447
  ## Tags
@@ -0,0 +1,21 @@
1
+ import { ToCDNOptions, ToCBOROptions, ToJSOptions } from '../types';
2
+ import { CborItem } from './CborItem';
3
+ import { CborWriter } from '../cbor/encode';
4
+ /**
5
+ * Wraps a resolved app-sequence result and preserves the original EDN source
6
+ * text for round-trip fidelity.
7
+ *
8
+ * When `appStrings !== false`, `_toCDN` returns the stored source text verbatim.
9
+ * Otherwise it delegates to the wrapped item so the caller gets a plain value.
10
+ *
11
+ * CBOR encoding and JS conversion always delegate to the inner item so the
12
+ * wrapper is fully transparent for those operations.
13
+ */
14
+ export declare class CborAppSeqResult extends CborItem {
15
+ readonly inner: CborItem;
16
+ readonly ednSource: string;
17
+ constructor(inner: CborItem, ednSource: string);
18
+ _encodeTo(writer: CborWriter, options?: ToCBOROptions): void;
19
+ _toCDN(options: ToCDNOptions | undefined, depth: number): string;
20
+ _toJS(options?: ToJSOptions): unknown;
21
+ }
@@ -1,6 +1,6 @@
1
1
  import { ToCDNOptions, ToJSOptions, ToCBOROptions } from '../types';
2
2
  import { CborItem, AnnotatedLine } from './CborItem';
3
- import { EncodingWidth } from '../cbor/encode';
3
+ import { CborWriter, EncodingWidth } from '../cbor/encode';
4
4
  /** CBOR Major Type 4 — array (definite- or indefinite-length). */
5
5
  export declare class CborArray extends CborItem {
6
6
  readonly items: CborItem[];
@@ -10,7 +10,7 @@ export declare class CborArray extends CborItem {
10
10
  indefiniteLength?: boolean;
11
11
  encodingWidth?: EncodingWidth;
12
12
  });
13
- _toCBOR(options?: ToCBOROptions): Uint8Array;
13
+ _encodeTo(writer: CborWriter, options?: ToCBOROptions): void;
14
14
  _toCDN(options: ToCDNOptions | undefined, depth: number): string;
15
15
  _toHexDump(depth: number, options?: ToCDNOptions): AnnotatedLine[];
16
16
  _toJS(options?: ToJSOptions): unknown;
@@ -1,6 +1,6 @@
1
1
  import { ToCDNOptions, ToJSOptions, ToCBOROptions } from '../types';
2
2
  import { CborItem } from './CborItem';
3
- import { EncodingWidth } from '../cbor/encode';
3
+ import { CborWriter, EncodingWidth } from '../cbor/encode';
4
4
  /** CBOR Major Type 2 — definite-length byte string. */
5
5
  export declare class CborByteString extends CborItem {
6
6
  readonly indefiniteLength: false;
@@ -14,7 +14,7 @@ export declare class CborByteString extends CborItem {
14
14
  encodingWidth?: EncodingWidth;
15
15
  ednSource?: string;
16
16
  });
17
- _toCBOR(_options?: ToCBOROptions): Uint8Array;
17
+ _encodeTo(writer: CborWriter, _options?: ToCBOROptions): void;
18
18
  _toCDN(options: ToCDNOptions | undefined, _depth: number): string;
19
19
  _toJS(_options?: ToJSOptions): unknown;
20
20
  }
@@ -1,5 +1,6 @@
1
1
  import { ToCDNOptions, ToJSOptions, ToCBOROptions } from '../types';
2
2
  import { CborItem, AnnotatedLine } from './CborItem';
3
+ import { CborWriter } from '../cbor/encode';
3
4
  /**
4
5
  * CBOR Sequence Literal (§2.5.6) — `<<item, item, ...>>`.
5
6
  *
@@ -15,7 +16,7 @@ export declare class CborEmbeddedCBOR extends CborItem {
15
16
  constructor(items: CborItem[]);
16
17
  /** The raw concatenated CBOR bytes of all contained items. */
17
18
  private _content;
18
- _toCBOR(options?: ToCBOROptions): Uint8Array;
19
+ _encodeTo(writer: CborWriter, options?: ToCBOROptions): void;
19
20
  _toCDN(options: ToCDNOptions | undefined, depth: number): string;
20
21
  _toHexDump(depth: number, options?: ToCDNOptions): AnnotatedLine[];
21
22
  _toJS(_options?: ToJSOptions): unknown;
@@ -1,5 +1,6 @@
1
1
  import { ToCDNOptions, ToJSOptions, ToCBOROptions } from '../types';
2
2
  import { CborItem } from './CborItem';
3
+ import { CborWriter } from '../cbor/encode';
3
4
  export type FloatPrecision = 'half' | 'single' | 'double';
4
5
  /**
5
6
  * CBOR Major Type 7 — IEEE 754 floating-point number.
@@ -18,10 +19,16 @@ export declare class CborFloat extends CborItem {
18
19
  * - `undefined`: encoder auto-selects the smallest lossless size.
19
20
  */
20
21
  readonly precision: FloatPrecision | undefined;
22
+ /**
23
+ * Original app-string source (e.g. `float'7e00'`), set by the parser when
24
+ * this float is the result of a `float'...'` app-string. Used by toCDN()
25
+ * to round-trip the literal when `appStrings` is not false.
26
+ */
27
+ ednSource?: string;
21
28
  constructor(value: number, options?: {
22
29
  precision?: FloatPrecision;
23
30
  });
24
- _toCBOR(_options?: ToCBOROptions): Uint8Array;
31
+ _encodeTo(writer: CborWriter, _options?: ToCBOROptions): void;
25
32
  _toCDN(options: ToCDNOptions | undefined, _depth: number): string;
26
33
  _toJS(_options?: ToJSOptions): unknown;
27
34
  }
@@ -1,12 +1,13 @@
1
1
  import { ToCDNOptions, ToJSOptions, ToCBOROptions } from '../types';
2
2
  import { CborItem, AnnotatedLine } from './CborItem';
3
3
  import { CborByteString } from './CborByteString';
4
+ import { CborWriter } from '../cbor/encode';
4
5
  /** CBOR Major Type 2 — indefinite-length byte string (chunked). */
5
6
  export declare class CborIndefiniteByteString extends CborItem {
6
7
  readonly indefiniteLength: true;
7
8
  readonly chunks: CborByteString[];
8
9
  constructor(chunks: CborByteString[]);
9
- _toCBOR(options?: ToCBOROptions): Uint8Array;
10
+ _encodeTo(writer: CborWriter, options?: ToCBOROptions): void;
10
11
  _toCDN(options: ToCDNOptions | undefined, _depth: number): string;
11
12
  _toHexDump(depth: number, options?: ToCDNOptions): AnnotatedLine[];
12
13
  _toJS(_options?: ToJSOptions): unknown;
@@ -1,12 +1,13 @@
1
1
  import { ToCDNOptions, ToJSOptions, ToCBOROptions } from '../types';
2
2
  import { CborItem, AnnotatedLine } from './CborItem';
3
3
  import { CborTextString } from './CborTextString';
4
+ import { CborWriter } from '../cbor/encode';
4
5
  /** CBOR Major Type 3 — indefinite-length UTF-8 text string (chunked). */
5
6
  export declare class CborIndefiniteTextString extends CborItem {
6
7
  readonly indefiniteLength: true;
7
8
  readonly chunks: CborTextString[];
8
9
  constructor(chunks: CborTextString[]);
9
- _toCBOR(options?: ToCBOROptions): Uint8Array;
10
+ _encodeTo(writer: CborWriter, options?: ToCBOROptions): void;
10
11
  _toCDN(options: ToCDNOptions | undefined, _depth: number): string;
11
12
  _toHexDump(depth: number, options?: ToCDNOptions): AnnotatedLine[];
12
13
  _toJS(_options?: ToJSOptions): unknown;
@@ -1,4 +1,5 @@
1
- import { CBOROptions, ToCDNOptions, ToJSOptions, ToHexDumpOptions, ToCBOROptions, CborComments } from '../types';
1
+ import { CBOROptions, ToCDNOptions, ToJSOptions, ToHexDumpOptions, ToCBOROptions, CborComments, DecodeWarning, ParseWarning } from '../types';
2
+ import { CborWriter } from '../cbor/encode';
2
3
  /** @internal One line of an annotated hex dump. */
3
4
  export interface AnnotatedLine {
4
5
  depth: number;
@@ -30,6 +31,12 @@ export declare abstract class CborItem {
30
31
  * They do not affect CBOR bytes or JS conversion.
31
32
  */
32
33
  comments?: CborComments;
34
+ /**
35
+ * Validity violations detected while decoding or parsing this node.
36
+ * Populated when `strict: false` is set in `FromCBOROptions` or
37
+ * `FromCDNOptions`.
38
+ */
39
+ warnings?: (DecodeWarning | ParseWarning)[];
33
40
  /**
34
41
  * Default options bound by a {@link CBOR} instance factory method.
35
42
  * Per-call options always take precedence.
@@ -70,8 +77,34 @@ export declare abstract class CborItem {
70
77
  * // FF -- "break"
71
78
  */
72
79
  toHexDump(options?: ToHexDumpOptions): string;
73
- /** @internal Subclass CBOR encoding implementation. */
74
- abstract _toCBOR(options?: ToCBOROptions): Uint8Array;
80
+ /**
81
+ * @internal
82
+ * Encode this node into `writer`, honoring `_toCBOR()` overrides.
83
+ *
84
+ * This is the entry point used by `toCBOR()` and by container nodes when
85
+ * recursing into children. A subclass that overrides `_toCBOR()` (e.g. to
86
+ * emit a pre-computed bit pattern) is authoritative even when one of its
87
+ * built-in base classes implements `_encodeTo()`.
88
+ */
89
+ _encode(writer: CborWriter, options?: ToCBOROptions): void;
90
+ /**
91
+ * @internal
92
+ * Write this node's CBOR encoding into `writer`.
93
+ *
94
+ * Built-in nodes override this so that an entire encode pass shares one
95
+ * growing buffer (no per-node Uint8Array allocations or re-copies).
96
+ * Container implementations must recurse via `child._encode()`, never
97
+ * `child._encodeTo()`, so that `_toCBOR()` overrides are honored.
98
+ */
99
+ _encodeTo(writer: CborWriter, options?: ToCBOROptions): void;
100
+ /**
101
+ * @internal
102
+ * Subclass CBOR encoding implementation.
103
+ * The default builds the bytes via `_encodeTo()`; subclasses may instead
104
+ * override this method directly when producing a standalone byte array is
105
+ * more natural (e.g. emitting a pre-computed bit pattern).
106
+ */
107
+ _toCBOR(options?: ToCBOROptions): Uint8Array;
75
108
  /**
76
109
  * @internal
77
110
  * Depth-aware CDN serialization.
@@ -1,6 +1,6 @@
1
1
  import { ToCDNOptions, ToJSOptions, ToCBOROptions } from '../types';
2
2
  import { CborItem, AnnotatedLine } from './CborItem';
3
- import { EncodingWidth } from '../cbor/encode';
3
+ import { CborWriter, EncodingWidth } from '../cbor/encode';
4
4
  /** CBOR Major Type 5 — map (definite- or indefinite-length). */
5
5
  export declare class CborMap extends CborItem {
6
6
  readonly entries: [CborItem, CborItem][];
@@ -10,7 +10,7 @@ export declare class CborMap extends CborItem {
10
10
  indefiniteLength?: boolean;
11
11
  encodingWidth?: EncodingWidth;
12
12
  });
13
- _toCBOR(options?: ToCBOROptions): Uint8Array;
13
+ _encodeTo(writer: CborWriter, options?: ToCBOROptions): void;
14
14
  _toCDN(options: ToCDNOptions | undefined, depth: number): string;
15
15
  _toHexDump(depth: number, options?: ToCDNOptions): AnnotatedLine[];
16
16
  _toJS(options?: ToJSOptions): unknown;
@@ -1,6 +1,6 @@
1
1
  import { ToCDNOptions, ToJSOptions, ToCBOROptions } from '../types';
2
2
  import { CborItem } from './CborItem';
3
- import { EncodingWidth } from '../cbor/encode';
3
+ import { CborWriter, EncodingWidth } from '../cbor/encode';
4
4
  /**
5
5
  * CBOR Major Type 1 — negative integer (−2^64 … −1).
6
6
  *
@@ -21,7 +21,7 @@ export declare class CborNint extends CborItem {
21
21
  });
22
22
  /** The actual decoded negative value (−1 − argument). */
23
23
  get value(): bigint;
24
- _toCBOR(_options?: ToCBOROptions): Uint8Array;
24
+ _encodeTo(writer: CborWriter, _options?: ToCBOROptions): void;
25
25
  _toCDN(options: ToCDNOptions | undefined, _depth: number): string;
26
26
  _toJS(options?: ToJSOptions): unknown;
27
27
  }
@@ -1,5 +1,6 @@
1
1
  import { ToCDNOptions, ToJSOptions, ToCBOROptions } from '../types';
2
2
  import { CborItem } from './CborItem';
3
+ import { CborWriter } from '../cbor/encode';
3
4
  /**
4
5
  * CBOR Major Type 7 — simple value (0–255).
5
6
  *
@@ -16,7 +17,7 @@ export declare class CborSimple extends CborItem {
16
17
  static readonly TRUE: CborSimple;
17
18
  static readonly NULL: CborSimple;
18
19
  static readonly UNDEFINED: CborSimple;
19
- _toCBOR(_options?: ToCBOROptions): Uint8Array;
20
+ _encodeTo(writer: CborWriter, _options?: ToCBOROptions): void;
20
21
  _toCDN(_options: ToCDNOptions | undefined, _depth: number): string;
21
22
  _toJS(_options?: ToJSOptions): unknown;
22
23
  }
@@ -1,6 +1,6 @@
1
1
  import { ToCDNOptions, ToJSOptions, ToCBOROptions } from '../types';
2
2
  import { CborItem, AnnotatedLine } from './CborItem';
3
- import { EncodingWidth } from '../cbor/encode';
3
+ import { CborWriter, EncodingWidth } from '../cbor/encode';
4
4
  /** CBOR Major Type 6 — tagged data item. */
5
5
  export declare class CborTag extends CborItem {
6
6
  readonly tag: bigint;
@@ -9,7 +9,7 @@ export declare class CborTag extends CborItem {
9
9
  constructor(tag: number | bigint, content: CborItem, options?: {
10
10
  encodingWidth?: EncodingWidth;
11
11
  });
12
- _toCBOR(options?: ToCBOROptions): Uint8Array;
12
+ _encodeTo(writer: CborWriter, options?: ToCBOROptions): void;
13
13
  _toCDN(options: ToCDNOptions | undefined, depth: number): string;
14
14
  _toHexDump(depth: number, options?: ToCDNOptions): AnnotatedLine[];
15
15
  _toJS(options?: ToJSOptions): unknown;
@@ -1,6 +1,6 @@
1
1
  import { ToCDNOptions, ToJSOptions, ToCBOROptions } from '../types';
2
2
  import { CborItem } from './CborItem';
3
- import { EncodingWidth } from '../cbor/encode';
3
+ import { CborWriter, EncodingWidth } from '../cbor/encode';
4
4
  /** CBOR Major Type 3 — definite-length UTF-8 text string. */
5
5
  export declare class CborTextString extends CborItem {
6
6
  readonly indefiniteLength: false;
@@ -9,7 +9,7 @@ export declare class CborTextString extends CborItem {
9
9
  constructor(value: string, options?: {
10
10
  encodingWidth?: EncodingWidth;
11
11
  });
12
- _toCBOR(_options?: ToCBOROptions): Uint8Array;
12
+ _encodeTo(writer: CborWriter, _options?: ToCBOROptions): void;
13
13
  _toCDN(options: ToCDNOptions | undefined, depth: number): string;
14
14
  _toJS(_options?: ToJSOptions): unknown;
15
15
  }
@@ -1,6 +1,6 @@
1
1
  import { ToCDNOptions, ToJSOptions, ToCBOROptions } from '../types';
2
2
  import { CborItem } from './CborItem';
3
- import { EncodingWidth } from '../cbor/encode';
3
+ import { CborWriter, EncodingWidth } from '../cbor/encode';
4
4
  /** CBOR Major Type 0 — unsigned integer (0 … 2^64−1). */
5
5
  export declare class CborUint extends CborItem {
6
6
  readonly value: bigint;
@@ -8,7 +8,7 @@ export declare class CborUint extends CborItem {
8
8
  constructor(value: number | bigint, options?: {
9
9
  encodingWidth?: EncodingWidth;
10
10
  });
11
- _toCBOR(_options?: ToCBOROptions): Uint8Array;
11
+ _encodeTo(writer: CborWriter, _options?: ToCBOROptions): void;
12
12
  _toCDN(options: ToCDNOptions | undefined, _depth: number): string;
13
13
  _toJS(options?: ToJSOptions): unknown;
14
14
  }
@@ -1 +1 @@
1
- Object.defineProperty(exports,Symbol.toStringTag,{value:`Module`});const e=require(`../mapEntries-CNxwMt7o.cjs`);exports.CborArray=e.p,exports.CborBigNint=e.c,exports.CborBigUint=e.l,exports.CborByteString=e.g,exports.CborEmbeddedCBOR=e.u,exports.CborFloat=e.v,exports.CborIndefiniteByteString=e.h,exports.CborIndefiniteTextString=e.m,exports.CborItem=e.x,exports.CborMap=e.f,exports.CborNint=e.y,exports.CborSimple=e.d,exports.CborTag=e._,exports.CborTextString=e.o,exports.CborUint=e.b;
1
+ Object.defineProperty(exports,Symbol.toStringTag,{value:`Module`});const e=require("../mapEntries-DZKHc3VY.cjs");exports.CborArray=e.p,exports.CborBigNint=e.c,exports.CborBigUint=e.l,exports.CborByteString=e.g,exports.CborEmbeddedCBOR=e.u,exports.CborFloat=e.v,exports.CborIndefiniteByteString=e.h,exports.CborIndefiniteTextString=e.m,exports.CborItem=e.x,exports.CborMap=e.f,exports.CborNint=e.y,exports.CborSimple=e.d,exports.CborTag=e._,exports.CborTextString=e.o,exports.CborUint=e.b;
package/dist/ast/index.js CHANGED
@@ -1,2 +1,2 @@
1
- import { _ as e, b as t, c as n, d as r, f as i, g as a, h as o, l as s, m as c, o as l, p as u, u as d, v as f, x as p, y as m } from "../mapEntries-Da-2HMRf.js";
1
+ import { _ as e, b as t, c as n, d as r, f as i, g as a, h as o, l as s, m as c, o as l, p as u, u as d, v as f, x as p, y as m } from "../mapEntries-BMMPg2FB.js";
2
2
  export { u as CborArray, n as CborBigNint, s as CborBigUint, a as CborByteString, d as CborEmbeddedCBOR, f as CborFloat, o as CborIndefiniteByteString, c as CborIndefiniteTextString, p as CborItem, i as CborMap, m as CborNint, r as CborSimple, e as CborTag, l as CborTextString, t as CborUint };
@@ -9,15 +9,49 @@ import { FloatPrecision } from '../ast/CborFloat';
9
9
  */
10
10
  export type EncodingWidth = 'i' | 0 | 1 | 2 | 3;
11
11
  /**
12
- * Encode a CBOR initial byte + argument into a Uint8Array.
12
+ * Growable byte buffer used by the CBOR encoder.
13
+ *
14
+ * AST nodes write themselves into a single shared writer via `_encodeTo`,
15
+ * so an encode pass performs one buffer copy at the end instead of
16
+ * re-copying every child's bytes at each nesting level.
17
+ *
18
+ * @internal
19
+ */
20
+ export declare class CborWriter {
21
+ private buf;
22
+ private len;
23
+ constructor(initialCapacity?: number);
24
+ private _ensure;
25
+ writeByte(b: number): void;
26
+ writeBytes(bytes: Uint8Array): void;
27
+ writeUint16(value: number): void;
28
+ writeUint32(value: number): void;
29
+ writeBigUint64(value: bigint): void;
30
+ writeFloat16(value: number): void;
31
+ writeFloat32(value: number): void;
32
+ writeFloat64(value: number): void;
33
+ /** Copy of the bytes written so far. */
34
+ finish(): Uint8Array;
35
+ }
36
+ /** Maximum CBOR argument value representable with the given encoding width. */
37
+ export declare function maxForEncodingWidth(ew: EncodingWidth): bigint;
38
+ /**
39
+ * Write a CBOR initial byte + argument into `w`.
13
40
  *
14
41
  * When `encodingWidth` is provided the argument is always written using that
15
42
  * many additional bytes (AI = 24 + encodingWidth), even if the value would fit
16
43
  * in fewer bytes. Without it the smallest valid encoding is chosen.
44
+ *
45
+ * `value` may be a number (common for lengths and counts — avoids a BigInt
46
+ * allocation per head) or a bigint (required for full uint64 range).
47
+ */
48
+ export declare function writeHeadTo(w: CborWriter, mt: number, value: number | bigint, encodingWidth?: EncodingWidth): void;
49
+ /**
50
+ * Encode a CBOR initial byte + argument into a Uint8Array.
51
+ * Convenience wrapper around {@link writeHeadTo} for callers that need the
52
+ * bytes directly (e.g. hex dumps).
17
53
  */
18
- export declare function writeHead(mt: number, value: bigint, encodingWidth?: EncodingWidth): Uint8Array;
19
- /** Concatenate multiple Uint8Arrays into one. */
20
- export declare function concat(parts: Uint8Array[]): Uint8Array;
54
+ export declare function writeHead(mt: number, value: number | bigint, encodingWidth?: EncodingWidth): Uint8Array;
21
55
  /**
22
56
  * Returns true if `value` can be exactly represented as a float16 without
23
57
  * precision loss (including -0, Infinity, -Infinity, NaN identity).