@cbortech/cbor 0.26.2 → 0.26.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.ja.md +78 -2
- package/README.md +81 -2
- package/dist/ast/CborAppSeqResult.d.ts +1 -0
- package/dist/ast/CborArray.d.ts +1 -0
- package/dist/ast/CborEmbeddedCBOR.d.ts +1 -0
- package/dist/ast/CborIndefiniteByteString.d.ts +1 -1
- package/dist/ast/CborItem.d.ts +8 -0
- package/dist/ast/CborMap.d.ts +1 -0
- package/dist/ast/CborTag.d.ts +1 -0
- package/dist/ast/CborTextString.d.ts +9 -0
- package/dist/ast/index.cjs +1 -1
- package/dist/ast/index.js +1 -1
- package/dist/cbor.d.ts +31 -1
- package/dist/cdn/serialize-utils.d.ts +9 -3
- package/dist/index.cjs +3 -3
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.js +58 -2
- package/dist/index.js.map +1 -1
- package/dist/mapEntries-DDJxbotH.cjs +15 -0
- package/dist/mapEntries-DDJxbotH.cjs.map +1 -0
- package/dist/{mapEntries-KwZz9nq2.js → mapEntries-hyNVtz5Z.js} +823 -710
- package/dist/mapEntries-hyNVtz5Z.js.map +1 -0
- package/dist/types.d.ts +108 -0
- package/package.json +5 -6
- package/dist/mapEntries-CZLpI8fS.cjs +0 -14
- package/dist/mapEntries-CZLpI8fS.cjs.map +0 -1
- package/dist/mapEntries-KwZz9nq2.js.map +0 -1
package/README.ja.md
CHANGED
|
@@ -2,7 +2,6 @@
|
|
|
2
2
|
|
|
3
3
|
[](https://www.npmjs.com/package/@cbortech/cbor)
|
|
4
4
|

|
|
5
|
-
[](https://bundlejs.com/?q=%40cbortech%2Fcbor)
|
|
6
5
|
[](https://www.npmjs.com/package/@cbortech/cbor)
|
|
7
6
|
[](./LICENSE)
|
|
8
7
|

|
|
@@ -29,6 +28,9 @@ npm install @cbortech/cbor
|
|
|
29
28
|
npm install -g @cbortech/cbor-cli
|
|
30
29
|
```
|
|
31
30
|
|
|
31
|
+
エディターで利用する場合は、本パッケージを用いて作られた
|
|
32
|
+
[VS Code extension](https://marketplace.visualstudio.com/items?itemName=cbortech.vscode-cdn-extension)もお試しください。
|
|
33
|
+
|
|
32
34
|
## インポート
|
|
33
35
|
|
|
34
36
|
```ts
|
|
@@ -226,6 +228,32 @@ console.log(text);
|
|
|
226
228
|
// }
|
|
227
229
|
```
|
|
228
230
|
|
|
231
|
+
### リーフコンテナを 1 行にまとめる
|
|
232
|
+
|
|
233
|
+
`inlineLeafContainers` は、要素に配列・マップを含まず(タグで包まれて
|
|
234
|
+
いる場合も含みます)、かつすべての要素が改行なしでシリアライズされる
|
|
235
|
+
コンテナを 1 行のまま出力します。ネストした
|
|
236
|
+
リーフコンテナはそれぞれ個別に 1 行へまとまるため、行列のようなデータが
|
|
237
|
+
読みやすくなります。`indent` 指定時に適用されます。
|
|
238
|
+
|
|
239
|
+
```ts
|
|
240
|
+
import { CBOR } from '@cbortech/cbor';
|
|
241
|
+
|
|
242
|
+
const text = CBOR.format('{"m": [[1,2],[3,4]], "s": (_ "a", "b")}', {
|
|
243
|
+
indent: 2,
|
|
244
|
+
inlineLeafContainers: true,
|
|
245
|
+
});
|
|
246
|
+
|
|
247
|
+
console.log(text);
|
|
248
|
+
// {
|
|
249
|
+
// "m": [
|
|
250
|
+
// [1, 2],
|
|
251
|
+
// [3, 4]
|
|
252
|
+
// ],
|
|
253
|
+
// "s": (_ "a", "b")
|
|
254
|
+
// }
|
|
255
|
+
```
|
|
256
|
+
|
|
229
257
|
### テキスト文字列を分割して整形する
|
|
230
258
|
|
|
231
259
|
`splitNewline` を使うと、長いテキスト文字列を改行文字の位置で CDN の文字列連結として
|
|
@@ -269,6 +297,25 @@ console.log(text);
|
|
|
269
297
|
// }
|
|
270
298
|
```
|
|
271
299
|
|
|
300
|
+
### raw テキスト文字列を保持する
|
|
301
|
+
|
|
302
|
+
デフォルトでは、`CBOR.format()` はバッククォートによる raw 文字列リテラル
|
|
303
|
+
(`` `...` ``、` ``...`` ` など)をダブルクォート形式に変換します。
|
|
304
|
+
`preserveRawString` を指定すると、元のソース表記のまま再出力します。保持された
|
|
305
|
+
raw 文字列はそのまま(verbatim)出力され、再エスケープ・再インデント・
|
|
306
|
+
`splitCdn` / `splitNewline` による分割の対象になりません。
|
|
307
|
+
(`` h`...` `` のような raw バイト文字列形式は `preserveByteString` の対象です。)
|
|
308
|
+
|
|
309
|
+
```ts
|
|
310
|
+
import { CBOR } from '@cbortech/cbor';
|
|
311
|
+
|
|
312
|
+
CBOR.format('`\\d+`');
|
|
313
|
+
// '"\\\\d+"'
|
|
314
|
+
|
|
315
|
+
CBOR.format('`\\d+`', { preserveRawString: true });
|
|
316
|
+
// '`\\d+`'
|
|
317
|
+
```
|
|
318
|
+
|
|
272
319
|
### `+` による文字列連結を保持する
|
|
273
320
|
|
|
274
321
|
注意: `+` による文字列連結構文は draft-26 で削除されました。この節は legacy
|
|
@@ -299,6 +346,35 @@ CBOR.format("h'68' + b64'aQ'", {
|
|
|
299
346
|
// "h'68' + b64'aQ'"
|
|
300
347
|
```
|
|
301
348
|
|
|
349
|
+
### CBOR / CDN / hex dump のバリデーション
|
|
350
|
+
|
|
351
|
+
`validate` は入力の well-formedness と validity を、例外を投げずにチェックします。
|
|
352
|
+
重複したマップキーなど回復可能な違反は例外にせず `warnings` に集約され、真に不正な
|
|
353
|
+
データのみ `error` として報告されます(CDN の構文エラーの場合は位置情報を保持した
|
|
354
|
+
`CdnSyntaxError`)。未登録のオプション拡張に一致する app-string prefix などの
|
|
355
|
+
情報ヒントは `valid` に影響せず、`hints` に分けて集約されます。`type` で入力形式を
|
|
356
|
+
指定します: `'cbor'`(デフォルト)、`'cdn'`、`'hex'`。
|
|
357
|
+
|
|
358
|
+
```ts
|
|
359
|
+
import { CBOR } from '@cbortech/cbor';
|
|
360
|
+
|
|
361
|
+
// CBOR バイト列 — キー "a" の重複は回復可能な違反
|
|
362
|
+
CBOR.validate(new Uint8Array([0xa2, 0x61, 0x61, 0x01, 0x61, 0x61, 0x02]), {
|
|
363
|
+
type: 'cbor',
|
|
364
|
+
});
|
|
365
|
+
// { valid: false, count: 1, warnings: [{ message: 'duplicate map key at offset 4', offset: 4 }], hints: [] }
|
|
366
|
+
|
|
367
|
+
// CDN テキスト — well-formed な入力
|
|
368
|
+
CBOR.validate('{"a": 1}', { type: 'cdn' });
|
|
369
|
+
// { valid: true, count: 1, warnings: [], hints: [] }
|
|
370
|
+
|
|
371
|
+
// アノテーション付き hex dump テキスト — 長さ3の配列なのに要素が2つしかない
|
|
372
|
+
CBOR.validate('83 -- Array of length 3\n 01 -- 1\n 02 -- 2', {
|
|
373
|
+
type: 'hex',
|
|
374
|
+
});
|
|
375
|
+
// { valid: false, count: 0, warnings: [], hints: [], error: Error(...) }
|
|
376
|
+
```
|
|
377
|
+
|
|
302
378
|
## AST を扱う
|
|
303
379
|
|
|
304
380
|
`CBOR.fromCBOR()`、`CBOR.fromCDN()`、`CBOR.fromJS()` は CBOR item を返します。
|
|
@@ -483,7 +559,7 @@ import { CBOR } from '@cbortech/cbor';
|
|
|
483
559
|
|
|
484
560
|
const v = CBOR.fromCDN("ilbs<<'Hello ', 'world'>>");
|
|
485
561
|
console.log(v.toCDN({ appStrings: false }));
|
|
486
|
-
// (_ 'Hello ',
|
|
562
|
+
// (_ 'Hello ','world')
|
|
487
563
|
```
|
|
488
564
|
|
|
489
565
|
> [!NOTE]
|
package/README.md
CHANGED
|
@@ -2,7 +2,6 @@
|
|
|
2
2
|
|
|
3
3
|
[](https://www.npmjs.com/package/@cbortech/cbor)
|
|
4
4
|

|
|
5
|
-
[](https://bundlejs.com/?q=%40cbortech%2Fcbor)
|
|
6
5
|
[](https://www.npmjs.com/package/@cbortech/cbor)
|
|
7
6
|
[](./LICENSE)
|
|
8
7
|

|
|
@@ -31,6 +30,10 @@ available as [@cbortech/cbor-cli](https://www.npmjs.com/package/@cbortech/cbor-c
|
|
|
31
30
|
npm install -g @cbortech/cbor-cli
|
|
32
31
|
```
|
|
33
32
|
|
|
33
|
+
For editor integration, try the companion
|
|
34
|
+
[VS Code extension](https://marketplace.visualstudio.com/items?itemName=cbortech.vscode-cdn-extension),
|
|
35
|
+
which is built with this package.
|
|
36
|
+
|
|
34
37
|
## Import
|
|
35
38
|
|
|
36
39
|
```ts
|
|
@@ -228,6 +231,32 @@ console.log(text);
|
|
|
228
231
|
// }
|
|
229
232
|
```
|
|
230
233
|
|
|
234
|
+
### Keep leaf containers on one line
|
|
235
|
+
|
|
236
|
+
`inlineLeafContainers` keeps a container on a single line when none of its
|
|
237
|
+
entries contains an array or map (even wrapped in a tag) and every entry
|
|
238
|
+
serializes without a line break. Nested leaf containers still collapse
|
|
239
|
+
individually, so matrix-like data stays readable. It is applied when
|
|
240
|
+
`indent` is specified.
|
|
241
|
+
|
|
242
|
+
```ts
|
|
243
|
+
import { CBOR } from '@cbortech/cbor';
|
|
244
|
+
|
|
245
|
+
const text = CBOR.format('{"m": [[1,2],[3,4]], "s": (_ "a", "b")}', {
|
|
246
|
+
indent: 2,
|
|
247
|
+
inlineLeafContainers: true,
|
|
248
|
+
});
|
|
249
|
+
|
|
250
|
+
console.log(text);
|
|
251
|
+
// {
|
|
252
|
+
// "m": [
|
|
253
|
+
// [1, 2],
|
|
254
|
+
// [3, 4]
|
|
255
|
+
// ],
|
|
256
|
+
// "s": (_ "a", "b")
|
|
257
|
+
// }
|
|
258
|
+
```
|
|
259
|
+
|
|
231
260
|
### Split text strings while formatting
|
|
232
261
|
|
|
233
262
|
`splitNewline` splits long text strings at newline characters using CDN
|
|
@@ -272,6 +301,25 @@ console.log(text);
|
|
|
272
301
|
// }
|
|
273
302
|
```
|
|
274
303
|
|
|
304
|
+
### Preserve raw text strings
|
|
305
|
+
|
|
306
|
+
By default, `CBOR.format()` converts raw backtick string literals
|
|
307
|
+
(`` `...` ``, ` ``...`` `, …) to double-quoted form. `preserveRawString`
|
|
308
|
+
re-emits them using their original source text instead. Preserved raw
|
|
309
|
+
strings are emitted verbatim: they are never re-escaped, re-indented, or
|
|
310
|
+
split by `splitCdn` / `splitNewline`. (Raw byte string forms such as
|
|
311
|
+
`` h`...` `` are covered by `preserveByteString`.)
|
|
312
|
+
|
|
313
|
+
```ts
|
|
314
|
+
import { CBOR } from '@cbortech/cbor';
|
|
315
|
+
|
|
316
|
+
CBOR.format('`\\d+`');
|
|
317
|
+
// '"\\\\d+"'
|
|
318
|
+
|
|
319
|
+
CBOR.format('`\\d+`', { preserveRawString: true });
|
|
320
|
+
// '`\\d+`'
|
|
321
|
+
```
|
|
322
|
+
|
|
275
323
|
### Preserve `+` string concatenation
|
|
276
324
|
|
|
277
325
|
Note: `+` string concatenation was removed in draft-26. This section is for
|
|
@@ -303,6 +351,37 @@ CBOR.format("h'68' + b64'aQ'", {
|
|
|
303
351
|
// "h'68' + b64'aQ'"
|
|
304
352
|
```
|
|
305
353
|
|
|
354
|
+
### Validate CBOR / CDN / hex dump
|
|
355
|
+
|
|
356
|
+
`validate` checks input for well-formedness and validity without throwing.
|
|
357
|
+
Recoverable violations (e.g. duplicate map keys) are collected into
|
|
358
|
+
`warnings` instead of stopping decoding; truly malformed data is reported via
|
|
359
|
+
`error` instead (for CDN syntax errors, a `CdnSyntaxError` with its position
|
|
360
|
+
fields intact). Informational hints — e.g. an app-string prefix that matches
|
|
361
|
+
a known optional extension which isn't registered — never affect `valid` and
|
|
362
|
+
are collected separately into `hints`. `type` selects the input format:
|
|
363
|
+
`'cbor'` (default), `'cdn'`, or `'hex'`.
|
|
364
|
+
|
|
365
|
+
```ts
|
|
366
|
+
import { CBOR } from '@cbortech/cbor';
|
|
367
|
+
|
|
368
|
+
// CBOR bytes — duplicate map key "a" is a recoverable violation
|
|
369
|
+
CBOR.validate(new Uint8Array([0xa2, 0x61, 0x61, 0x01, 0x61, 0x61, 0x02]), {
|
|
370
|
+
type: 'cbor',
|
|
371
|
+
});
|
|
372
|
+
// { valid: false, count: 1, warnings: [{ message: 'duplicate map key at offset 4', offset: 4 }], hints: [] }
|
|
373
|
+
|
|
374
|
+
// CDN text — well-formed input
|
|
375
|
+
CBOR.validate('{"a": 1}', { type: 'cdn' });
|
|
376
|
+
// { valid: true, count: 1, warnings: [], hints: [] }
|
|
377
|
+
|
|
378
|
+
// Annotated hex dump text — truncated array (length 3, only 2 elements present)
|
|
379
|
+
CBOR.validate('83 -- Array of length 3\n 01 -- 1\n 02 -- 2', {
|
|
380
|
+
type: 'hex',
|
|
381
|
+
});
|
|
382
|
+
// { valid: false, count: 0, warnings: [], hints: [], error: Error(...) }
|
|
383
|
+
```
|
|
384
|
+
|
|
306
385
|
## Working With The AST
|
|
307
386
|
|
|
308
387
|
`CBOR.fromCBOR()`, `CBOR.fromCDN()`, and `CBOR.fromJS()` return a CBOR item.
|
|
@@ -488,7 +567,7 @@ import { CBOR } from '@cbortech/cbor';
|
|
|
488
567
|
|
|
489
568
|
const v = CBOR.fromCDN("ilbs<<'Hello ', 'world'>>");
|
|
490
569
|
console.log(v.toCDN({ appStrings: false }));
|
|
491
|
-
// (_ 'Hello ',
|
|
570
|
+
// (_ 'Hello ','world')
|
|
492
571
|
```
|
|
493
572
|
|
|
494
573
|
> [!NOTE]
|
|
@@ -17,6 +17,7 @@ export declare class CborAppSeqResult extends CborItem {
|
|
|
17
17
|
readonly inner: CborItem;
|
|
18
18
|
readonly ednSource: string;
|
|
19
19
|
constructor(inner: CborItem, ednSource: string);
|
|
20
|
+
get _containsCdnContainer(): boolean;
|
|
20
21
|
_encodeTo(writer: CborWriter, options?: ToCBOROptions): void;
|
|
21
22
|
_toCDN(options: ToCDNOptions | undefined, depth: number): string;
|
|
22
23
|
_toJS(options?: ToJSOptions): unknown;
|
package/dist/ast/CborArray.d.ts
CHANGED
|
@@ -10,6 +10,7 @@ export declare class CborArray extends CborItem {
|
|
|
10
10
|
indefiniteLength?: boolean;
|
|
11
11
|
encodingWidth?: EncodingWidth;
|
|
12
12
|
});
|
|
13
|
+
get _containsCdnContainer(): boolean;
|
|
13
14
|
_encodeTo(writer: CborWriter, options?: ToCBOROptions): void;
|
|
14
15
|
_toCDN(options: ToCDNOptions | undefined, depth: number): string;
|
|
15
16
|
_toHexDump(depth: number, options?: ToCDNOptions): AnnotatedLine[];
|
|
@@ -17,6 +17,7 @@ export declare class CborEmbeddedCBOR extends CborItem {
|
|
|
17
17
|
constructor(items: CborItem[], options?: {
|
|
18
18
|
encodingWidth?: EncodingWidth;
|
|
19
19
|
});
|
|
20
|
+
get _containsCdnContainer(): boolean;
|
|
20
21
|
/** The raw concatenated CBOR bytes of all contained items. */
|
|
21
22
|
private _content;
|
|
22
23
|
_encodeTo(writer: CborWriter, options?: ToCBOROptions): void;
|
|
@@ -8,7 +8,7 @@ export declare class CborIndefiniteByteString extends CborItem {
|
|
|
8
8
|
readonly chunks: CborByteString[];
|
|
9
9
|
constructor(chunks: CborByteString[]);
|
|
10
10
|
_encodeTo(writer: CborWriter, options?: ToCBOROptions): void;
|
|
11
|
-
_toCDN(options: ToCDNOptions | undefined,
|
|
11
|
+
_toCDN(options: ToCDNOptions | undefined, depth: number): string;
|
|
12
12
|
_toHexDump(depth: number, options?: ToCDNOptions): AnnotatedLine[];
|
|
13
13
|
_toJS(_options?: ToJSOptions): unknown;
|
|
14
14
|
}
|
package/dist/ast/CborItem.d.ts
CHANGED
|
@@ -43,6 +43,14 @@ export declare abstract class CborItem {
|
|
|
43
43
|
* @internal
|
|
44
44
|
*/
|
|
45
45
|
_defaults?: CBOROptions;
|
|
46
|
+
/**
|
|
47
|
+
* @internal
|
|
48
|
+
* True when this node is, or contains through wrapper nodes (tags,
|
|
49
|
+
* embedded CBOR, app-sequence results), an array or map.
|
|
50
|
+
* `inlineLeafContainers` never inlines a container whose entries contain
|
|
51
|
+
* another container, even one that renders on a single line.
|
|
52
|
+
*/
|
|
53
|
+
get _containsCdnContainer(): boolean;
|
|
46
54
|
/** Serialize this node to CBOR binary. */
|
|
47
55
|
toCBOR(options?: ToCBOROptions): Uint8Array;
|
|
48
56
|
/** Serialize this node to a CDN text string. */
|
package/dist/ast/CborMap.d.ts
CHANGED
|
@@ -10,6 +10,7 @@ export declare class CborMap extends CborItem {
|
|
|
10
10
|
indefiniteLength?: boolean;
|
|
11
11
|
encodingWidth?: EncodingWidth;
|
|
12
12
|
});
|
|
13
|
+
get _containsCdnContainer(): boolean;
|
|
13
14
|
_encodeTo(writer: CborWriter, options?: ToCBOROptions): void;
|
|
14
15
|
_toCDN(options: ToCDNOptions | undefined, depth: number): string;
|
|
15
16
|
_toHexDump(depth: number, options?: ToCDNOptions): AnnotatedLine[];
|
package/dist/ast/CborTag.d.ts
CHANGED
|
@@ -9,6 +9,7 @@ export declare class CborTag extends CborItem {
|
|
|
9
9
|
constructor(tag: number | bigint, content: CborItem, options?: {
|
|
10
10
|
encodingWidth?: EncodingWidth;
|
|
11
11
|
});
|
|
12
|
+
get _containsCdnContainer(): boolean;
|
|
12
13
|
_encodeTo(writer: CborWriter, options?: ToCBOROptions): void;
|
|
13
14
|
_toCDN(options: ToCDNOptions | undefined, depth: number): string;
|
|
14
15
|
_toHexDump(depth: number, options?: ToCDNOptions): AnnotatedLine[];
|
|
@@ -8,9 +8,18 @@ export declare class CborTextString extends CborItem {
|
|
|
8
8
|
encodingWidth: EncodingWidth | undefined;
|
|
9
9
|
/** Part boundaries of the original `+` concatenation chain, if any. */
|
|
10
10
|
readonly ednParts: readonly string[] | undefined;
|
|
11
|
+
/** Original raw-string source text, when parsed from a single backtick literal. */
|
|
12
|
+
readonly ednSource: string | undefined;
|
|
13
|
+
/**
|
|
14
|
+
* Original source text per `ednParts` entry, aligned by index; `undefined`
|
|
15
|
+
* for parts that were not raw backtick literals.
|
|
16
|
+
*/
|
|
17
|
+
readonly ednPartSources: readonly (string | undefined)[] | undefined;
|
|
11
18
|
constructor(value: string, options?: {
|
|
12
19
|
encodingWidth?: EncodingWidth;
|
|
13
20
|
ednParts?: readonly string[];
|
|
21
|
+
ednSource?: string;
|
|
22
|
+
ednPartSources?: readonly (string | undefined)[];
|
|
14
23
|
});
|
|
15
24
|
_encodeTo(writer: CborWriter, _options?: ToCBOROptions): void;
|
|
16
25
|
_toCDN(options: ToCDNOptions | undefined, depth: number): string;
|
package/dist/ast/index.cjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
Object.defineProperty(exports,Symbol.toStringTag,{value:`Module`});const e=require("../mapEntries-
|
|
1
|
+
Object.defineProperty(exports,Symbol.toStringTag,{value:`Module`});const e=require("../mapEntries-DDJxbotH.cjs");exports.CborArray=e.C,exports.CborBigNint=e.v,exports.CborBigUint=e.y,exports.CborByteString=e.E,exports.CborEmbeddedCBOR=e.b,exports.CborFloat=e.O,exports.CborIndefiniteByteString=e.T,exports.CborIndefiniteTextString=e.w,exports.CborItem=e.j,exports.CborMap=e.S,exports.CborNint=e.k,exports.CborSimple=e.x,exports.CborTag=e.D,exports.CborTextString=e.g,exports.CborUint=e.A;
|
package/dist/ast/index.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import { A as e, C as t, D as n, E as r, O as i, S as a, T as o, b as s, g as c, j as l, k as u, v as d, w as f, x as p, y as m } from "../mapEntries-
|
|
1
|
+
import { A as e, C as t, D as n, E as r, O as i, S as a, T as o, b as s, g as c, j as l, k as u, v as d, w as f, x as p, y as m } from "../mapEntries-hyNVtz5Z.js";
|
|
2
2
|
export { t as CborArray, d as CborBigNint, m as CborBigUint, r as CborByteString, s as CborEmbeddedCBOR, i as CborFloat, o as CborIndefiniteByteString, f as CborIndefiniteTextString, l as CborItem, a as CborMap, u as CborNint, p as CborSimple, n as CborTag, c as CborTextString, e as CborUint };
|
package/dist/cbor.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { CborItem } from './ast/CborItem';
|
|
2
|
-
import { CBOROptions, FromCBOROptions, FromCBORSeqOptions, FromCDNOptions, FromCDNSeqOptions, FromHexDumpOptions, FromJSOptions, ToCBOROptions, ToCDNOptions, ToHexDumpOptions, ToJSOptions, CBOR_OMIT } from './types';
|
|
2
|
+
import { CBOROptions, FromCBOROptions, FromCBORSeqOptions, FromCDNOptions, FromCDNSeqOptions, FromHexDumpOptions, FromJSOptions, ToCBOROptions, ToCDNOptions, ToHexDumpOptions, ToJSOptions, ValidateOptions, ValidateResult, CBOR_OMIT } from './types';
|
|
3
3
|
import { dt_as_Date as _dt_as_Date } from './extensions/dt';
|
|
4
4
|
import { MapEntries as _MapEntries } from './mapEntries';
|
|
5
5
|
import { Simple as _Simple } from './simple';
|
|
@@ -69,6 +69,11 @@ export declare class CBOR {
|
|
|
69
69
|
decompile(input: ArrayBufferView | ArrayBufferLike, options?: FromCBORSeqOptions & ToCDNOptions): string;
|
|
70
70
|
toHex(input: ArrayBufferView | ArrayBufferLike, options?: FromCBORSeqOptions & ToHexDumpOptions): string;
|
|
71
71
|
fromHex(text: string, options?: FromHexDumpOptions & ToCBOROptions): Uint8Array;
|
|
72
|
+
/**
|
|
73
|
+
* Check CBOR / CDN / hex dump input for well-formedness and validity,
|
|
74
|
+
* without throwing.
|
|
75
|
+
*/
|
|
76
|
+
validate(input: ArrayBufferView | ArrayBufferLike | string, options?: ValidateOptions): ValidateResult;
|
|
72
77
|
/** @deprecated Use `decompile()` instead. */
|
|
73
78
|
cborToCborEdn(input: ArrayBufferView | ArrayBufferLike, options?: FromCBOROptions & ToCDNOptions): string;
|
|
74
79
|
/** @deprecated Use `decompile()` instead. */
|
|
@@ -149,6 +154,31 @@ export declare class CBOR {
|
|
|
149
154
|
* Multi-item dumps produce a CBOR Sequence (RFC 8742): concatenated items.
|
|
150
155
|
*/
|
|
151
156
|
static fromHex(text: string, options?: FromHexDumpOptions & ToCBOROptions): Uint8Array;
|
|
157
|
+
/**
|
|
158
|
+
* Check CBOR / CDN / hex dump input for well-formedness and validity,
|
|
159
|
+
* without throwing.
|
|
160
|
+
*
|
|
161
|
+
* Decodes/parses the input as a sequence (CBOR Sequence per RFC 8742, or a
|
|
162
|
+
* CDN Sequence) in non-strict mode: recoverable violations are collected
|
|
163
|
+
* into `warnings` instead of stopping decoding, while malformed input
|
|
164
|
+
* (e.g. truncated data, hard syntax errors — including a CDN Sequence
|
|
165
|
+
* abandoned after a hard syntax error) is reported via `error`.
|
|
166
|
+
* Informational hints about optional extensions that aren't registered
|
|
167
|
+
* (`ParseWarning.hint`) are not treated as violations; they are collected
|
|
168
|
+
* separately into `hints`.
|
|
169
|
+
*
|
|
170
|
+
* @example
|
|
171
|
+
* const result = CBOR.validate(bytes);
|
|
172
|
+
* if (!result.valid) {
|
|
173
|
+
* if (result.error) console.error(`invalid: ${result.error.message}`);
|
|
174
|
+
* for (const w of result.warnings) console.warn(w.message);
|
|
175
|
+
* }
|
|
176
|
+
*
|
|
177
|
+
* @example
|
|
178
|
+
* // CDN text input
|
|
179
|
+
* CBOR.validate('{"a": 1}', { type: 'cdn' });
|
|
180
|
+
*/
|
|
181
|
+
static validate(input: ArrayBufferView | ArrayBufferLike | string, options?: ValidateOptions): ValidateResult;
|
|
152
182
|
/**
|
|
153
183
|
* Convert CBOR binary data directly to a CDN text string.
|
|
154
184
|
*
|
|
@@ -51,7 +51,8 @@ export declare function resolveSeparators(options: ToCDNOptions | undefined, com
|
|
|
51
51
|
colSep: string;
|
|
52
52
|
};
|
|
53
53
|
/**
|
|
54
|
-
* Shared CDN serialization for bracketed containers (CborArray / CborMap
|
|
54
|
+
* Shared CDN serialization for bracketed containers (CborArray / CborMap /
|
|
55
|
+
* indefinite-length string chunks `(_ ...)`):
|
|
55
56
|
* encoding-indicator / `_` prefix resolution, single-line vs multi-line
|
|
56
57
|
* selection (comments force multi-line), separators, and per-entry
|
|
57
58
|
* leading/trailing plus container dangling comments.
|
|
@@ -66,14 +67,19 @@ export declare function serializeContainer(p: {
|
|
|
66
67
|
node: Commented;
|
|
67
68
|
options: ToCDNOptions | undefined;
|
|
68
69
|
depth: number;
|
|
69
|
-
openChar: '[' | '{';
|
|
70
|
-
closeChar: ']' | '}';
|
|
70
|
+
openChar: '[' | '{' | '(';
|
|
71
|
+
closeChar: ']' | '}' | ')';
|
|
71
72
|
count: number;
|
|
72
73
|
indefiniteLength: boolean;
|
|
73
74
|
encodingWidth: EncodingWidth | undefined;
|
|
74
75
|
hasEntryComments: () => boolean;
|
|
75
76
|
/** Render entry `i` at child depth (`item` or `key: value`). */
|
|
76
77
|
renderEntry: (i: number, colSep: string) => string;
|
|
78
|
+
/**
|
|
79
|
+
* Whether entry `i` contains no nested array/map, so it may stay on the
|
|
80
|
+
* container's line under `inlineLeafContainers`. Omitted = always a leaf.
|
|
81
|
+
*/
|
|
82
|
+
entryIsLeaf?: (i: number) => boolean;
|
|
77
83
|
/** Node whose leading comments are emitted above entry `i` (item / map key). */
|
|
78
84
|
entryLeadingNode: (i: number) => Commented;
|
|
79
85
|
/** Pre-formatted trailing comment text for entry `i` (starts with ' ', or ''). */
|
package/dist/index.cjs
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
Object.defineProperties(exports,{__esModule:{value:!0},[Symbol.toStringTag]:{value:`Module`}});const e=require("./tokenizer-EciPlN0n.cjs"),t=require("./mapEntries-
|
|
1
|
+
Object.defineProperties(exports,{__esModule:{value:!0},[Symbol.toStringTag]:{value:`Module`}});const e=require("./tokenizer-EciPlN0n.cjs"),t=require("./mapEntries-DDJxbotH.cjs");var n=`ABCDEFGHIJKLMNOPQRSTUVWXYZ234567`,r=`0123456789ABCDEFGHIJKLMNOPQRSTUV`;function i(e){let t=e.length;for(;t>0&&e.charCodeAt(t-1)===61;)t--;return e.slice(0,t)}function a(e,t,n){let r=i(e).toUpperCase(),a=r.length%8;if(a===1||a===3||a===6)throw SyntaxError(`invalid base32 length: ${r.length} characters`);let o=new Uint8Array(128).fill(255);for(let e=0;e<t.length;e++)o[t.charCodeAt(e)]=e;let s=new Uint8Array(Math.floor(r.length*5/8)),c=0,l=0,u=0;for(let e of r){let t=e.charCodeAt(0),n=t<128?o[t]:255;if(n===255)throw SyntaxError(`invalid character in byte string: ${JSON.stringify(e)}`);c=c<<5|n,l+=5,l>=8&&(l-=8,s[u++]=c>>l&255)}if(l>0&&c&(1<<l)-1){let e=`non-zero trailing bits in base32 input`;if(n)n(e);else throw SyntaxError(e)}return s}var o={appStringPrefixes:[`b32`],parseAppString(e,r,i){return new t.E(a(t.o(r),n,i),{ednEncoding:`base32`})}},s={appStringPrefixes:[`h32`],parseAppString(e,n,i){return new t.E(a(t.o(n),r,i),{ednEncoding:`base32hex`})}};function c(e,t){if(e.length!==t.length)return!1;for(let n=0;n<e.length;n++)if(e[n]!==t[n])return!1;return!0}var l={appStringPrefixes:[`same`],preserveAppSeqSource:!0,parseAppSequence(e,t,n){if(t.length===0)throw SyntaxError(`same<<...>> requires at least one item`);let r=t[0],i=r.toCBOR();for(let e=1;e<t.length;e++)if(!c(i,t[e].toCBOR())){let t=`same<<...>>: item ${e} produces different CBOR bytes than item 0`;if(n)n(t);else throw SyntaxError(t)}return r}},u=class n{static OMIT=t.N;static TAG=t.P;static Tag=t.I;static Simple=t.M;static MapEntries=t.t;static dt_as_Date=t.h;#e;constructor(e){this.#e=e??{}}#t(e){return{...this.#e,...e??{}}}fromCBOR(e,t){let r=n.fromCBOR(e,this.#t(t));return r._defaults=this.#e,r}fromCDN(e,t){let r=n.fromCDN(e,this.#t(t));return r._defaults=this.#e,r}fromEDN(e,t){return this.fromCDN(e,t)}fromJS(e,t){let r=n.fromJS(e,this.#t(t));return r._defaults=this.#e,r}fromHexDump(e,t){let r=n.fromHexDump(e,this.#t(t));return r._defaults=this.#e,r}*fromCBORSeq(e,t){for(let r of n.fromCBORSeq(e,this.#t(t)))r._defaults=this.#e,yield r}*fromCDNSeq(e,t){for(let r of n.fromCDNSeq(e,this.#t(t)))r._defaults=this.#e,yield r}*fromHexDumpSeq(e,t){for(let r of n.fromHexDumpSeq(e,this.#t(t)))r._defaults=this.#e,yield r}decode(e,t){return n.decode(e,this.#t(t))}*decodeSeq(e,t){yield*n.decodeSeq(e,this.#t(t))}*parseSeq(e,t){yield*n.parseSeq(e,this.#t(t))}encode(e,t){return n.encode(e,this.#t(t))}compile(e,t){return n.compile(e,this.#t(t))}decompile(e,t){return n.decompile(e,this.#t(t))}toHex(e,t){return n.toHex(e,this.#t(t))}fromHex(e,t){return n.fromHex(e,this.#t(t))}validate(e,t){return n.validate(e,this.#t(t))}cborToCborEdn(e,t){return this.cborToCdn(e,t)}cborToCdn(e,t){let r=this.#t(t),i=n.fromCBOR(e,r);return i._defaults=this.#e,i.toCDN(r)}cborEdnToCbor(e,t){return this.cdnToCbor(e,t)}cdnToCbor(e,t){let r=this.#t(t);return n.fromCDN(e,r).toCBOR(r)}parse(e,t){if(typeof t==`function`){let r=this.#t({reviver:t});return n.fromCDN(e,r).toJS(r)}let r=this.#t(t);return n.fromCDN(e,r).toJS(r)}stringify(e,t,r){if(typeof t==`function`||Array.isArray(t)||t===null||t===void 0&&r!==void 0){let i={...this.#e};return t===null?i.replacer=void 0:(typeof t==`function`||Array.isArray(t))&&(i.replacer=t),r!==void 0&&(i.indent=_(r)),n.stringify(e,i)}return n.stringify(e,this.#t(t??void 0))}format(e,t){return n.format(e,this.#t(t))}static fromCBOR(e,n){return t.d(e,n)}static fromCDN(e,n){return t._(e,n)}static fromEDN(e,t){return n.fromCDN(e,t)}static*fromHexDumpSeq(e,t){let r=[],i=d(e).trim().split(/\s+/).filter(Boolean);for(let e of i)if(/^[0-9A-Fa-f]{2}$/.test(e))r.push(parseInt(e,16));else if(/^[0-9A-Fa-f]+$/.test(e)&&e.length%2==0)for(let t=0;t<e.length;t+=2)r.push(parseInt(e.slice(t,t+2),16));else throw SyntaxError(`Invalid hex token in dump: ${JSON.stringify(e)}`);yield*n.fromCBORSeq(new Uint8Array(r),t)}static*fromCBORSeq(e,n){let r=e instanceof ArrayBuffer||typeof SharedArrayBuffer<`u`&&e instanceof SharedArrayBuffer?new Uint8Array(e):new Uint8Array(e.buffer,e.byteOffset,e.byteLength),i=0;for(;i<r.byteLength;){let e=t.d(r,{...n,offset:i,allowTrailing:!0});yield e,i=e.end}}static*fromCDNSeq(n,r){let i=!!r?.preserveComments,a=0,o=!0;for(;;){let{offset:s,hadSeparator:c,commaOffset:l}=g(n,a,r,i?o?`all`:`after-newline`:`none`);if(o&&l>=0){let e=`leading comma in CDN sequence`;if(r?.strict!==!1)throw SyntaxError(e);m(e,l,r)}if(s>=n.length||i&&h(n,s)&&g(n,s,r).offset>=n.length)break;if(!o&&!c){let e=`CDN sequence items must be separated by whitespace, comma, or comment`;if(r?.strict!==!1)throw SyntaxError(e);m(e,s,r)}a=s;let u;try{u=t._(n,{...r,offset:a,allowTrailing:!0,_skipRS:!0})}catch(t){if(r?.strict!==!1)throw t;m(t instanceof Error?t.message:String(t),a,r,!0,t instanceof e.o?t:void 0);break}yield u,a=u.end,o=!1}}static fromJS(e,n){return t.r(e,n)}static fromHexDump(e,n){let r=[],i=d(e).trim().split(/\s+/).filter(Boolean);for(let e of i)if(/^[0-9A-Fa-f]{2}$/.test(e))r.push(parseInt(e,16));else if(/^[0-9A-Fa-f]+$/.test(e)&&e.length%2==0)for(let t=0;t<e.length;t+=2)r.push(parseInt(e.slice(t,t+2),16));else throw SyntaxError(`Invalid hex token in dump: ${JSON.stringify(e)}`);return t.d(new Uint8Array(r),n)}static decode(e,t){return n.fromCBOR(e,t).toJS(t)}static*decodeSeq(e,t){for(let r of n.fromCBORSeq(e,t))yield r.toJS(t)}static*parseSeq(e,t){for(let r of n.fromCDNSeq(e,t))yield r.toJS(t)}static encode(e,t){return n.fromJS(e,t).toCBOR(t)}static compile(e,t){let r=[...n.fromCDNSeq(e,t)].map(e=>e.toCBOR(t)),i=r.reduce((e,t)=>e+t.length,0),a=new Uint8Array(i),o=0;for(let e of r)a.set(e,o),o+=e.length;return a}static decompile(e,t){return[...n.fromCBORSeq(e,t)].map(e=>e.toCDN(t)).join(`
|
|
2
2
|
`)}static toHex(e,t){return[...n.fromCBORSeq(e,t)].map(e=>e.toHexDump(t)).join(`
|
|
3
|
-
`)}static fromHex(e,t){let r=[...n.fromHexDumpSeq(e,t)].map(e=>e.toCBOR(t)),i=r.reduce((e,t)=>e+t.length,0),a=new Uint8Array(i),o=0;for(let e of r)a.set(e,o),o+=e.length;return a}static cborToCdn(e,t){return n.fromCBOR(e,t).toCDN(t)}static cborToCborEdn(e,t){return n.fromCBOR(e,t).toCDN(t)}static cdnToCbor(e,t){return n.fromCDN(e,t).toCBOR(t)}static cborEdnToCbor(e,t){return n.fromCDN(e,t).toCBOR(t)}static parse(e,t){return typeof t==`function`?n.fromCDN(e).toJS({reviver:t}):n.fromCDN(e,t).toJS(t)}static stringify(e,n,r){if(typeof n==`function`||Array.isArray(n)||n===null||n===void 0&&r!==void 0){let i=typeof n==`function`||Array.isArray(n)?n:void 0,a=_(r);if(i){let n=t.n(e,i);return n===void 0||n===t.N?void 0:t.r(n).toCDN(a===void 0?void 0:{indent:a})}return t.r(e).toCDN(a===void 0?void 0:{indent:a})}let i=n;if(i?.replacer){let n=t.n(e,i.replacer,i.extensions,i.undefinedOmits,i.builtinExtensions);if(n===void 0||n===t.N)return;let{replacer:r,...a}=i;return t.r(n,Object.keys(a).length>0?a:void 0).toCDN(i)}return t.r(e,i).toCDN(i)}static format(e,t){return n.fromCDN(e,t).toCDN(t)}};function d(e){let t=``,n=0;for(;n<e.length;){let r=e[n],i=e[n+1]??``;if(r===`-`&&i===`-`){n=f(e,n+2),t+=` `;continue}if(r===`—`){n=f(e,n+1),t+=` `;continue}if(r===`#`){n=f(e,n+1),t+=` `;continue}if(r===`/`&&i===`/`){n=f(e,n+2),t+=` `;continue}if(r===`/`&&i===`*`){let r=e.indexOf(`*/`,n+2);if(r<0)throw SyntaxError(`Unterminated comment in hex dump`);t+=p(e.slice(n,r+2)),n=r+2;continue}if(r===`/`){let r=e.indexOf(`/`,n+1);if(r<0)throw SyntaxError(`Unterminated comment in hex dump`);t+=p(e.slice(n,r+1)),n=r+1;continue}t+=r,n++}return t}function f(e,t){let n=e.indexOf(`
|
|
4
|
-
`,t);return n<0?e.length:n}function p(e){return e.replace(/[^\r\n]/g,` `)}function m(e,t,n,r,i){let a=i?.offset??t,o={message:e,offset:a};r&&(o.fatal=!0),i?.offset!==void 0&&(o.line=i.line,o.column=i.column,o.endOffset=i.endOffset),n?.onWarning?n.onWarning(o):n?.silent||console.warn(`CDN sequence warning at offset ${a}: ${e}`)}function h(e,t){let n=e[t];return n===`#`||n===`/`}function g(e,t,n,r=`none`){let i=t,a=!1,o=!1,s=!1,c=-1,l=()=>r===`all`||r===`after-newline`&&s;for(;i<e.length;){let t=e[i];if(t===` `||t===` `||t===`\r`||t===``){a=!0,i++;continue}if(t===`
|
|
3
|
+
`)}static fromHex(e,t){let r=[...n.fromHexDumpSeq(e,t)].map(e=>e.toCBOR(t)),i=r.reduce((e,t)=>e+t.length,0),a=new Uint8Array(i),o=0;for(let e of r)a.set(e,o),o+=e.length;return a}static validate(t,r){let i=[],a=[],o,s={strict:!1,extensions:r?.extensions,builtinExtensions:r?.builtinExtensions,onWarning:e=>{if(`hint`in e&&e.hint){a.push(e);return}if(`fatal`in e&&e.fatal){o=e;return}i.push(e)}},c=0;try{let e=r?.type??`cbor`;if(e===`cdn`){let e={...s,unresolvedExtension:r?.unresolvedExtension};for(let r of n.fromCDNSeq(t,e))c++}else if(e===`hex`)for(let e of n.fromHexDumpSeq(t,s))c++;else for(let e of n.fromCBORSeq(t,s))c++}catch(e){return{valid:!1,count:c,warnings:i,hints:a,error:e instanceof Error?e:Error(String(e))}}if(o){let t=o.cause instanceof Error?o.cause:new e.o(o.message,{offset:o.offset});return{valid:!1,count:c,warnings:i,hints:a,error:t}}return{valid:i.length===0,count:c,warnings:i,hints:a}}static cborToCdn(e,t){return n.fromCBOR(e,t).toCDN(t)}static cborToCborEdn(e,t){return n.fromCBOR(e,t).toCDN(t)}static cdnToCbor(e,t){return n.fromCDN(e,t).toCBOR(t)}static cborEdnToCbor(e,t){return n.fromCDN(e,t).toCBOR(t)}static parse(e,t){return typeof t==`function`?n.fromCDN(e).toJS({reviver:t}):n.fromCDN(e,t).toJS(t)}static stringify(e,n,r){if(typeof n==`function`||Array.isArray(n)||n===null||n===void 0&&r!==void 0){let i=typeof n==`function`||Array.isArray(n)?n:void 0,a=_(r);if(i){let n=t.n(e,i);return n===void 0||n===t.N?void 0:t.r(n).toCDN(a===void 0?void 0:{indent:a})}return t.r(e).toCDN(a===void 0?void 0:{indent:a})}let i=n;if(i?.replacer){let n=t.n(e,i.replacer,i.extensions,i.undefinedOmits,i.builtinExtensions);if(n===void 0||n===t.N)return;let{replacer:r,...a}=i;return t.r(n,Object.keys(a).length>0?a:void 0).toCDN(i)}return t.r(e,i).toCDN(i)}static format(e,t){return n.fromCDN(e,t).toCDN(t)}};function d(e){let t=``,n=0;for(;n<e.length;){let r=e[n],i=e[n+1]??``;if(r===`-`&&i===`-`){n=f(e,n+2),t+=` `;continue}if(r===`—`){n=f(e,n+1),t+=` `;continue}if(r===`#`){n=f(e,n+1),t+=` `;continue}if(r===`/`&&i===`/`){n=f(e,n+2),t+=` `;continue}if(r===`/`&&i===`*`){let r=e.indexOf(`*/`,n+2);if(r<0)throw SyntaxError(`Unterminated comment in hex dump`);t+=p(e.slice(n,r+2)),n=r+2;continue}if(r===`/`){let r=e.indexOf(`/`,n+1);if(r<0)throw SyntaxError(`Unterminated comment in hex dump`);t+=p(e.slice(n,r+1)),n=r+1;continue}t+=r,n++}return t}function f(e,t){let n=e.indexOf(`
|
|
4
|
+
`,t);return n<0?e.length:n}function p(e){return e.replace(/[^\r\n]/g,` `)}function m(e,t,n,r,i){let a=i?.offset??t,o={message:e,offset:a};r&&(o.fatal=!0),i&&(o.cause=i),i?.offset!==void 0&&(o.line=i.line,o.column=i.column,o.endOffset=i.endOffset),n?.onWarning?n.onWarning(o):n?.silent||console.warn(`CDN sequence warning at offset ${a}: ${e}`)}function h(e,t){let n=e[t];return n===`#`||n===`/`}function g(e,t,n,r=`none`){let i=t,a=!1,o=!1,s=!1,c=-1,l=()=>r===`all`||r===`after-newline`&&s;for(;i<e.length;){let t=e[i];if(t===` `||t===` `||t===`\r`||t===``){a=!0,i++;continue}if(t===`
|
|
5
5
|
`){a=!0,s=!0,i++;continue}if(t===`#`){if(a=!0,l())break;let t=e.indexOf(`
|
|
6
6
|
`,i+1);i=t<0?e.length:t+1,s=!0;continue}if(t===`/`&&e[i+1]===`/`){if(a=!0,l())break;let t=e.indexOf(`
|
|
7
7
|
`,i+2);i=t<0?e.length:t+1,s=!0;continue}if(t===`/`&&e[i+1]===`*`){if(a=!0,l())break;let t=e.indexOf(`*/`,i+2);if(t<0){let t=`unterminated /* comment in CDN sequence`;if(n?.strict!==!1)throw SyntaxError(t);m(t,i,n,!0),i=e.length}else e.slice(i,t).includes(`
|