@cbortech/cbor 0.24.0 → 0.25.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.ja.md +127 -49
- package/README.md +128 -49
- package/dist/ast/CborAppSeqResult.d.ts +20 -0
- package/dist/ast/CborArray.d.ts +3 -3
- package/dist/ast/CborBignum.d.ts +5 -5
- package/dist/ast/CborByteString.d.ts +2 -2
- package/dist/ast/CborEllipsis.d.ts +2 -2
- package/dist/ast/CborEmbeddedCBOR.d.ts +3 -3
- package/dist/ast/CborFloat.d.ts +8 -2
- package/dist/ast/CborIndefiniteByteString.d.ts +3 -3
- package/dist/ast/CborIndefiniteTextString.d.ts +3 -3
- package/dist/ast/CborItem.d.ts +22 -10
- package/dist/ast/CborMap.d.ts +3 -3
- package/dist/ast/CborNint.d.ts +2 -2
- package/dist/ast/CborSimple.d.ts +2 -2
- package/dist/ast/CborTag.d.ts +3 -3
- package/dist/ast/CborTextString.d.ts +2 -2
- package/dist/ast/CborUint.d.ts +2 -2
- package/dist/ast/CborUnresolvedAppExt.d.ts +2 -2
- package/dist/ast/index.cjs +1 -1
- package/dist/ast/index.js +2 -2
- package/dist/cbor.d.ts +44 -26
- package/dist/cdn/parser.d.ts +7 -0
- package/dist/{edn → cdn}/serialize-utils.d.ts +20 -7
- package/dist/cdn/serializer.d.ts +7 -0
- package/dist/cdn/test-vectors/runner.d.ts +22 -0
- package/dist/{edn → cdn}/tokenizer.d.ts +28 -25
- package/dist/extensions/b32.d.ts +5 -0
- package/dist/extensions/cri.d.ts +7 -7
- package/dist/extensions/dt.d.ts +10 -19
- package/dist/extensions/float.d.ts +7 -0
- package/dist/extensions/ip.d.ts +8 -8
- package/dist/extensions/same.d.ts +7 -0
- package/dist/extensions/types.d.ts +30 -8
- package/dist/index.cjs +5 -2
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +4 -1
- package/dist/index.js +244 -77
- package/dist/index.js.map +1 -1
- package/dist/mapEntries-BccT62HT.cjs +36 -0
- package/dist/mapEntries-BccT62HT.cjs.map +1 -0
- package/dist/{mapEntries-D5MWtXqq.js → mapEntries-ZR8QJ0Yj.js} +1157 -853
- package/dist/mapEntries-ZR8QJ0Yj.js.map +1 -0
- package/dist/simple.d.ts +2 -2
- package/dist/tag.d.ts +1 -1
- package/dist/types.d.ts +141 -30
- package/dist/utils/hexfloat.d.ts +2 -2
- package/dist/utils/strip-comments.d.ts +12 -0
- package/package.json +13 -11
- package/dist/edn/parser.d.ts +0 -7
- package/dist/edn/serializer.d.ts +0 -7
- package/dist/mapEntries-D5MWtXqq.js.map +0 -1
- package/dist/mapEntries-bihZ3yks.cjs +0 -31
- package/dist/mapEntries-bihZ3yks.cjs.map +0 -1
package/README.ja.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# @cbortech/cbor
|
|
2
2
|
|
|
3
|
-
[CBOR](#準拠している仕様)、[
|
|
3
|
+
[CBOR](#準拠している仕様)、[CDN](#準拠している仕様)、JavaScript 値を相互変換するための TypeScript ライブラリです。
|
|
4
4
|
|
|
5
5
|
このパッケージは `CBOR` ファサードに加えて、extension の実装に必要な CBOR AST ノードクラス用の entrypoint を公開します。
|
|
6
6
|
低レベルのパーサー、エンコーダー内部は、ドキュメント上の公開 API には含めていません。
|
|
@@ -52,31 +52,29 @@ console.log(value);
|
|
|
52
52
|
// { hello: 'world', n: 42 }
|
|
53
53
|
```
|
|
54
54
|
|
|
55
|
-
### CBOR バイト列から
|
|
55
|
+
### CBOR バイト列から CDN へ
|
|
56
56
|
|
|
57
57
|
```ts
|
|
58
58
|
import { CBOR } from '@cbortech/cbor';
|
|
59
59
|
|
|
60
|
-
const text = CBOR.fromCBOR(
|
|
61
|
-
new Uint8Array([0x83, 0x01, 0x02, 0x03])
|
|
62
|
-
).toEDN();
|
|
60
|
+
const text = CBOR.fromCBOR(new Uint8Array([0x83, 0x01, 0x02, 0x03])).toCDN();
|
|
63
61
|
|
|
64
62
|
console.log(text);
|
|
65
63
|
// [1,2,3]
|
|
66
64
|
```
|
|
67
65
|
|
|
68
|
-
###
|
|
66
|
+
### CDN から CBOR バイト列へ
|
|
69
67
|
|
|
70
68
|
```ts
|
|
71
69
|
import { CBOR } from '@cbortech/cbor';
|
|
72
70
|
|
|
73
|
-
const bytes = CBOR.
|
|
71
|
+
const bytes = CBOR.fromCDN('[1, 2, 3]').toCBOR();
|
|
74
72
|
|
|
75
73
|
console.log(bytes);
|
|
76
74
|
// Uint8Array([0x83, 0x01, 0x02, 0x03])
|
|
77
75
|
```
|
|
78
76
|
|
|
79
|
-
### JavaScript から
|
|
77
|
+
### JavaScript から CDN へ
|
|
80
78
|
|
|
81
79
|
```ts
|
|
82
80
|
import { CBOR } from '@cbortech/cbor';
|
|
@@ -87,7 +85,7 @@ console.log(text);
|
|
|
87
85
|
// {"a":1,"b":true,"c":null}
|
|
88
86
|
```
|
|
89
87
|
|
|
90
|
-
### 読みやすい
|
|
88
|
+
### 読みやすい CDN を出力する
|
|
91
89
|
|
|
92
90
|
```ts
|
|
93
91
|
import { CBOR } from '@cbortech/cbor';
|
|
@@ -105,7 +103,7 @@ console.log(text);
|
|
|
105
103
|
// }
|
|
106
104
|
```
|
|
107
105
|
|
|
108
|
-
###
|
|
106
|
+
### CDN から JavaScript へ
|
|
109
107
|
|
|
110
108
|
```ts
|
|
111
109
|
import { CBOR } from '@cbortech/cbor';
|
|
@@ -116,7 +114,7 @@ console.log(value);
|
|
|
116
114
|
// [1, Uint8Array(...), true, null]
|
|
117
115
|
```
|
|
118
116
|
|
|
119
|
-
###
|
|
117
|
+
### CDN を正規化する
|
|
120
118
|
|
|
121
119
|
```ts
|
|
122
120
|
import { CBOR } from '@cbortech/cbor';
|
|
@@ -135,7 +133,7 @@ console.log(text);
|
|
|
135
133
|
|
|
136
134
|
### テキスト文字列を分割して整形する
|
|
137
135
|
|
|
138
|
-
`textStringFormat` を使うと、長いテキスト文字列を
|
|
136
|
+
`textStringFormat` を使うと、長いテキスト文字列を CDN の文字列連結として分割できます。
|
|
139
137
|
このオプションは `indent` を指定したときに適用されます。
|
|
140
138
|
|
|
141
139
|
```ts
|
|
@@ -154,19 +152,19 @@ console.log(text);
|
|
|
154
152
|
// }
|
|
155
153
|
```
|
|
156
154
|
|
|
157
|
-
文字列の中身が
|
|
155
|
+
文字列の中身が CDN や JSON 風の内容なら、`cdn` を使えます。
|
|
158
156
|
|
|
159
157
|
```ts
|
|
160
158
|
import { CBOR } from '@cbortech/cbor';
|
|
161
159
|
|
|
162
|
-
const text = CBOR.format('{"
|
|
160
|
+
const text = CBOR.format('{"cdn": "[1,2,3]"}', {
|
|
163
161
|
indent: 2,
|
|
164
|
-
textStringFormat: ['
|
|
162
|
+
textStringFormat: ['cdn'],
|
|
165
163
|
});
|
|
166
164
|
|
|
167
165
|
console.log(text);
|
|
168
166
|
// {
|
|
169
|
-
// "
|
|
167
|
+
// "cdn": "[" +
|
|
170
168
|
// "1," +
|
|
171
169
|
// "2," +
|
|
172
170
|
// "3" +
|
|
@@ -176,7 +174,7 @@ console.log(text);
|
|
|
176
174
|
|
|
177
175
|
## AST を扱う
|
|
178
176
|
|
|
179
|
-
`CBOR.fromCBOR()`、`CBOR.
|
|
177
|
+
`CBOR.fromCBOR()`、`CBOR.fromCDN()`、`CBOR.fromJS()` は CBOR item を返します。
|
|
180
178
|
`CborTextString`、`CborByteString`、`CborArray`、`CborTag` などの具体的なノードクラスは、
|
|
181
179
|
extension 向けに `@cbortech/cbor/ast` から export されています。すべての item は次のメソッドを持ちます。
|
|
182
180
|
|
|
@@ -184,11 +182,11 @@ extension 向けに `@cbortech/cbor/ast` から export されています。す
|
|
|
184
182
|
import { CBOR } from '@cbortech/cbor';
|
|
185
183
|
import { CborItem } from '@cbortech/cbor/ast';
|
|
186
184
|
|
|
187
|
-
const item = CBOR.
|
|
185
|
+
const item = CBOR.fromCDN('{ "x": 1 }');
|
|
188
186
|
item satisfies CborItem;
|
|
189
187
|
|
|
190
188
|
const bytes = item.toCBOR();
|
|
191
|
-
const text = item.
|
|
189
|
+
const text = item.toCDN();
|
|
192
190
|
const value = item.toJS();
|
|
193
191
|
```
|
|
194
192
|
|
|
@@ -197,23 +195,23 @@ const value = item.toJS();
|
|
|
197
195
|
```ts
|
|
198
196
|
import { CBOR } from '@cbortech/cbor';
|
|
199
197
|
|
|
200
|
-
const item = CBOR.
|
|
198
|
+
const item = CBOR.fromCDN('[_ 1, 2, 3]');
|
|
201
199
|
|
|
202
|
-
console.log(item.
|
|
200
|
+
console.log(item.toCDN());
|
|
203
201
|
// [_ 1,2,3]
|
|
204
202
|
|
|
205
203
|
console.log(item.toCBOR());
|
|
206
204
|
// Uint8Array(...)
|
|
207
205
|
```
|
|
208
206
|
|
|
209
|
-
### CBOR を AST としてデコードし、
|
|
207
|
+
### CBOR を AST としてデコードし、CDN として確認する
|
|
210
208
|
|
|
211
209
|
```ts
|
|
212
210
|
import { CBOR } from '@cbortech/cbor';
|
|
213
211
|
|
|
214
212
|
const item = CBOR.fromCBOR(new Uint8Array([0x83, 0x01, 0x02, 0x03]));
|
|
215
213
|
|
|
216
|
-
console.log(item.
|
|
214
|
+
console.log(item.toCDN());
|
|
217
215
|
// [1,2,3]
|
|
218
216
|
|
|
219
217
|
console.log(item.toJS());
|
|
@@ -301,7 +299,7 @@ console.log(cbor.stringify({ value }));
|
|
|
301
299
|
|
|
302
300
|
## 日時
|
|
303
301
|
|
|
304
|
-
|
|
302
|
+
CDN の `dt'...'` と `DT'...'` リテラルは、デフォルトでパースできます。
|
|
305
303
|
JavaScript の `Date` オブジェクトとして扱いたい場合は `CBOR.dt_as_Date` を追加します。
|
|
306
304
|
|
|
307
305
|
```ts
|
|
@@ -328,37 +326,117 @@ console.log(text);
|
|
|
328
326
|
|
|
329
327
|
## オプション extension
|
|
330
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
|
+
|
|
331
395
|
追加の application extension は別パッケージとして公開されています。必要なものを
|
|
332
396
|
インストールし、`extensions` オプションに渡して使います。
|
|
333
397
|
|
|
334
|
-
|
|
335
|
-
外部パッケージが必要です。そのため、このパッケージ本体には含めず、
|
|
336
|
-
`@cbortech/hash-extension` として提供しています。
|
|
398
|
+
### hash
|
|
337
399
|
|
|
338
|
-
`
|
|
339
|
-
|
|
340
|
-
|
|
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
|
+
別パッケージで提供しています。
|
|
341
404
|
|
|
342
405
|
```bash
|
|
343
|
-
npm install @cbortech/hash-extension
|
|
406
|
+
npm install @cbortech/hash-extension
|
|
344
407
|
```
|
|
345
408
|
|
|
346
409
|
```ts
|
|
347
410
|
import { CBOR } from '@cbortech/cbor';
|
|
348
|
-
import
|
|
349
|
-
import uuidExtension from '@cbortech/uuid-extension';
|
|
411
|
+
import { hash } from '@cbortech/hash-extension';
|
|
350
412
|
|
|
351
|
-
const cbor = new CBOR({
|
|
352
|
-
|
|
353
|
-
|
|
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';
|
|
354
435
|
|
|
355
|
-
const
|
|
356
|
-
console.log(digest.toEDN());
|
|
357
|
-
// hash'foo'
|
|
436
|
+
const cbor = new CBOR({ extensions: [uuid] });
|
|
358
437
|
|
|
359
|
-
const id = cbor.
|
|
360
|
-
|
|
361
|
-
// 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]
|
|
362
440
|
```
|
|
363
441
|
|
|
364
442
|
## タグ
|
|
@@ -471,7 +549,7 @@ CBOR item は注釈付き hex dump の生成とパースに対応しています
|
|
|
471
549
|
```ts
|
|
472
550
|
import { CBOR } from '@cbortech/cbor';
|
|
473
551
|
|
|
474
|
-
const item = CBOR.
|
|
552
|
+
const item = CBOR.fromCDN('[_ 1, [2, 3]]');
|
|
475
553
|
const dump = item.toHexDump();
|
|
476
554
|
|
|
477
555
|
console.log(dump);
|
|
@@ -489,7 +567,7 @@ const item = CBOR.fromHexDump(`
|
|
|
489
567
|
03 -- 3
|
|
490
568
|
`);
|
|
491
569
|
|
|
492
|
-
console.log(item.
|
|
570
|
+
console.log(item.toCDN());
|
|
493
571
|
// [1,2,3]
|
|
494
572
|
```
|
|
495
573
|
|
|
@@ -512,19 +590,19 @@ console.log(item.toEDN());
|
|
|
512
590
|
このライブラリは次の仕様を対象にしています。
|
|
513
591
|
|
|
514
592
|
- [CBOR, RFC 8949](https://www.rfc-editor.org/rfc/rfc8949)
|
|
515
|
-
- [
|
|
593
|
+
- [Concise Diagnostic Notation (CDN), draft-ietf-cbor-edn-literals-25](https://datatracker.ietf.org/doc/draft-ietf-cbor-edn-literals/25/)
|
|
516
594
|
|
|
517
|
-
|
|
595
|
+
CDN は、CBOR データを人間が読み書きしやすいテキストとして表現するための記法です。
|
|
518
596
|
サンプル、テストベクター、デバッグ、fixture、設定ファイルに近い用途など、CBOR のバイト列をそのまま扱うと読みにくい場面で役立ちます。
|
|
519
597
|
|
|
520
598
|
通常の配列、マップ、文字列、数値、真偽値、null は JSON に近い見た目で書けます。
|
|
521
599
|
一方で、CBOR 固有の byte string、tag、simple value、不定長 item、文字列以外の map key、
|
|
522
600
|
`dt'2026-05-06T00:00:00Z'` のような application literal も表現できます。
|
|
523
601
|
|
|
524
|
-
|
|
525
|
-
特別な変換なしに
|
|
602
|
+
CDN は JSON / JSONC の上位互換なので、通常の JSON データやコメント付きの JSON 風データも、
|
|
603
|
+
特別な変換なしに CDN としてパース・整形できます。
|
|
526
604
|
|
|
527
|
-
|
|
605
|
+
CDN はまだ広く普及した RFC ではなく、Internet-Draft として策定中の仕様です。
|
|
528
606
|
|
|
529
607
|
## ライセンス
|
|
530
608
|
|
package/README.md
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
# @cbortech/cbor
|
|
2
2
|
|
|
3
3
|
TypeScript library for converting between [CBOR](#specifications),
|
|
4
|
-
[
|
|
4
|
+
[CDN](#specifications), and JavaScript values.
|
|
5
5
|
|
|
6
6
|
This package exposes the `CBOR` facade plus a separate AST entrypoint for the
|
|
7
7
|
CBOR node classes needed by extensions. Lower-level parser and encoder internals
|
|
@@ -54,31 +54,29 @@ console.log(value);
|
|
|
54
54
|
// { hello: 'world', n: 42 }
|
|
55
55
|
```
|
|
56
56
|
|
|
57
|
-
### CBOR bytes to
|
|
57
|
+
### CBOR bytes to CDN
|
|
58
58
|
|
|
59
59
|
```ts
|
|
60
60
|
import { CBOR } from '@cbortech/cbor';
|
|
61
61
|
|
|
62
|
-
const text = CBOR.fromCBOR(
|
|
63
|
-
new Uint8Array([0x83, 0x01, 0x02, 0x03])
|
|
64
|
-
).toEDN();
|
|
62
|
+
const text = CBOR.fromCBOR(new Uint8Array([0x83, 0x01, 0x02, 0x03])).toCDN();
|
|
65
63
|
|
|
66
64
|
console.log(text);
|
|
67
65
|
// [1,2,3]
|
|
68
66
|
```
|
|
69
67
|
|
|
70
|
-
###
|
|
68
|
+
### CDN to CBOR bytes
|
|
71
69
|
|
|
72
70
|
```ts
|
|
73
71
|
import { CBOR } from '@cbortech/cbor';
|
|
74
72
|
|
|
75
|
-
const bytes = CBOR.
|
|
73
|
+
const bytes = CBOR.fromCDN('[1, 2, 3]').toCBOR();
|
|
76
74
|
|
|
77
75
|
console.log(bytes);
|
|
78
76
|
// Uint8Array([0x83, 0x01, 0x02, 0x03])
|
|
79
77
|
```
|
|
80
78
|
|
|
81
|
-
### JavaScript to
|
|
79
|
+
### JavaScript to CDN
|
|
82
80
|
|
|
83
81
|
```ts
|
|
84
82
|
import { CBOR } from '@cbortech/cbor';
|
|
@@ -89,7 +87,7 @@ console.log(text);
|
|
|
89
87
|
// {"a":1,"b":true,"c":null}
|
|
90
88
|
```
|
|
91
89
|
|
|
92
|
-
### Pretty
|
|
90
|
+
### Pretty CDN
|
|
93
91
|
|
|
94
92
|
```ts
|
|
95
93
|
import { CBOR } from '@cbortech/cbor';
|
|
@@ -107,7 +105,7 @@ console.log(text);
|
|
|
107
105
|
// }
|
|
108
106
|
```
|
|
109
107
|
|
|
110
|
-
###
|
|
108
|
+
### CDN to JavaScript
|
|
111
109
|
|
|
112
110
|
```ts
|
|
113
111
|
import { CBOR } from '@cbortech/cbor';
|
|
@@ -118,7 +116,7 @@ console.log(value);
|
|
|
118
116
|
// [1, Uint8Array(...), true, null]
|
|
119
117
|
```
|
|
120
118
|
|
|
121
|
-
### Normalize
|
|
119
|
+
### Normalize CDN
|
|
122
120
|
|
|
123
121
|
```ts
|
|
124
122
|
import { CBOR } from '@cbortech/cbor';
|
|
@@ -137,7 +135,7 @@ console.log(text);
|
|
|
137
135
|
|
|
138
136
|
### Split text strings while formatting
|
|
139
137
|
|
|
140
|
-
`textStringFormat` can split long text strings with
|
|
138
|
+
`textStringFormat` can split long text strings with CDN string concatenation.
|
|
141
139
|
It is applied when `indent` is specified.
|
|
142
140
|
|
|
143
141
|
```ts
|
|
@@ -156,19 +154,19 @@ console.log(text);
|
|
|
156
154
|
// }
|
|
157
155
|
```
|
|
158
156
|
|
|
159
|
-
For strings that contain
|
|
157
|
+
For strings that contain CDN or JSON-like content, use `cdn`.
|
|
160
158
|
|
|
161
159
|
```ts
|
|
162
160
|
import { CBOR } from '@cbortech/cbor';
|
|
163
161
|
|
|
164
|
-
const text = CBOR.format('{"
|
|
162
|
+
const text = CBOR.format('{"cdn": "[1,2,3]"}', {
|
|
165
163
|
indent: 2,
|
|
166
|
-
textStringFormat: ['
|
|
164
|
+
textStringFormat: ['cdn'],
|
|
167
165
|
});
|
|
168
166
|
|
|
169
167
|
console.log(text);
|
|
170
168
|
// {
|
|
171
|
-
// "
|
|
169
|
+
// "cdn": "[" +
|
|
172
170
|
// "1," +
|
|
173
171
|
// "2," +
|
|
174
172
|
// "3" +
|
|
@@ -178,7 +176,7 @@ console.log(text);
|
|
|
178
176
|
|
|
179
177
|
## Working With The AST
|
|
180
178
|
|
|
181
|
-
`CBOR.fromCBOR()`, `CBOR.
|
|
179
|
+
`CBOR.fromCBOR()`, `CBOR.fromCDN()`, and `CBOR.fromJS()` return a CBOR item.
|
|
182
180
|
Concrete node classes such as `CborTextString`, `CborByteString`, `CborArray`,
|
|
183
181
|
and `CborTag` are exported from `@cbortech/cbor/ast` for extensions. Every item
|
|
184
182
|
supports these methods:
|
|
@@ -187,11 +185,11 @@ supports these methods:
|
|
|
187
185
|
import { CBOR } from '@cbortech/cbor';
|
|
188
186
|
import { CborItem } from '@cbortech/cbor/ast';
|
|
189
187
|
|
|
190
|
-
const item = CBOR.
|
|
188
|
+
const item = CBOR.fromCDN('{ "x": 1 }');
|
|
191
189
|
item satisfies CborItem;
|
|
192
190
|
|
|
193
191
|
const bytes = item.toCBOR();
|
|
194
|
-
const text = item.
|
|
192
|
+
const text = item.toCDN();
|
|
195
193
|
const value = item.toJS();
|
|
196
194
|
```
|
|
197
195
|
|
|
@@ -200,23 +198,23 @@ const value = item.toJS();
|
|
|
200
198
|
```ts
|
|
201
199
|
import { CBOR } from '@cbortech/cbor';
|
|
202
200
|
|
|
203
|
-
const item = CBOR.
|
|
201
|
+
const item = CBOR.fromCDN('[_ 1, 2, 3]');
|
|
204
202
|
|
|
205
|
-
console.log(item.
|
|
203
|
+
console.log(item.toCDN());
|
|
206
204
|
// [_ 1,2,3]
|
|
207
205
|
|
|
208
206
|
console.log(item.toCBOR());
|
|
209
207
|
// Uint8Array(...)
|
|
210
208
|
```
|
|
211
209
|
|
|
212
|
-
### Decode to AST, then inspect as
|
|
210
|
+
### Decode to AST, then inspect as CDN
|
|
213
211
|
|
|
214
212
|
```ts
|
|
215
213
|
import { CBOR } from '@cbortech/cbor';
|
|
216
214
|
|
|
217
215
|
const item = CBOR.fromCBOR(new Uint8Array([0x83, 0x01, 0x02, 0x03]));
|
|
218
216
|
|
|
219
|
-
console.log(item.
|
|
217
|
+
console.log(item.toCDN());
|
|
220
218
|
// [1,2,3]
|
|
221
219
|
|
|
222
220
|
console.log(item.toJS());
|
|
@@ -305,7 +303,7 @@ console.log(cbor.stringify({ value }));
|
|
|
305
303
|
|
|
306
304
|
## Dates
|
|
307
305
|
|
|
308
|
-
|
|
306
|
+
CDN `dt'...'` and `DT'...'` literals are parsed by default. Add `CBOR.dt_as_Date`
|
|
309
307
|
when you want JavaScript `Date` objects.
|
|
310
308
|
|
|
311
309
|
```ts
|
|
@@ -332,37 +330,118 @@ console.log(text);
|
|
|
332
330
|
|
|
333
331
|
## Optional Extensions
|
|
334
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
|
+
|
|
335
399
|
Additional application extensions are published as separate packages. Install
|
|
336
400
|
the ones you need and pass them through the `extensions` option.
|
|
337
401
|
|
|
338
|
-
|
|
339
|
-
it requires an external package. For that reason, it is not bundled with this
|
|
340
|
-
package and is provided separately as `@cbortech/hash-extension`.
|
|
402
|
+
### hash
|
|
341
403
|
|
|
342
|
-
`
|
|
343
|
-
|
|
344
|
-
|
|
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).
|
|
345
409
|
|
|
346
410
|
```bash
|
|
347
|
-
npm install @cbortech/hash-extension
|
|
411
|
+
npm install @cbortech/hash-extension
|
|
348
412
|
```
|
|
349
413
|
|
|
350
414
|
```ts
|
|
351
415
|
import { CBOR } from '@cbortech/cbor';
|
|
352
|
-
import
|
|
353
|
-
import uuidExtension from '@cbortech/uuid-extension';
|
|
416
|
+
import { hash } from '@cbortech/hash-extension';
|
|
354
417
|
|
|
355
|
-
const cbor = new CBOR({
|
|
356
|
-
|
|
357
|
-
|
|
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';
|
|
358
440
|
|
|
359
|
-
const
|
|
360
|
-
console.log(digest.toEDN());
|
|
361
|
-
// hash'foo'
|
|
441
|
+
const cbor = new CBOR({ extensions: [uuid] });
|
|
362
442
|
|
|
363
|
-
const id = cbor.
|
|
364
|
-
|
|
365
|
-
// 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]
|
|
366
445
|
```
|
|
367
446
|
|
|
368
447
|
## Tags
|
|
@@ -477,7 +556,7 @@ CBOR items can produce and parse annotated hex dumps.
|
|
|
477
556
|
```ts
|
|
478
557
|
import { CBOR } from '@cbortech/cbor';
|
|
479
558
|
|
|
480
|
-
const item = CBOR.
|
|
559
|
+
const item = CBOR.fromCDN('[_ 1, [2, 3]]');
|
|
481
560
|
const dump = item.toHexDump();
|
|
482
561
|
|
|
483
562
|
console.log(dump);
|
|
@@ -495,7 +574,7 @@ const item = CBOR.fromHexDump(`
|
|
|
495
574
|
03 -- 3
|
|
496
575
|
`);
|
|
497
576
|
|
|
498
|
-
console.log(item.
|
|
577
|
+
console.log(item.toCDN());
|
|
499
578
|
// [1,2,3]
|
|
500
579
|
```
|
|
501
580
|
|
|
@@ -518,9 +597,9 @@ The `CBOR` facade also exposes:
|
|
|
518
597
|
This library targets:
|
|
519
598
|
|
|
520
599
|
- [CBOR, RFC 8949](https://www.rfc-editor.org/rfc/rfc8949)
|
|
521
|
-
- [
|
|
600
|
+
- [Concise Diagnostic Notation (CDN), draft-ietf-cbor-edn-literals-25](https://datatracker.ietf.org/doc/draft-ietf-cbor-edn-literals/25/)
|
|
522
601
|
|
|
523
|
-
|
|
602
|
+
CDN is a human-readable text notation for CBOR data. It is useful for
|
|
524
603
|
examples, test vectors, debugging, fixtures, and configuration-like files where
|
|
525
604
|
raw CBOR bytes would be hard to read.
|
|
526
605
|
|
|
@@ -529,11 +608,11 @@ and null values, but it can also represent CBOR-specific features such as byte
|
|
|
529
608
|
strings, tags, simple values, indefinite-length items, non-string map keys, and
|
|
530
609
|
application literals like `dt'2026-05-06T00:00:00Z'`.
|
|
531
610
|
|
|
532
|
-
|
|
533
|
-
commented JSON-style data can be parsed and formatted as
|
|
611
|
+
CDN is a superset of JSON and JSONC, so ordinary JSON data and
|
|
612
|
+
commented JSON-style data can be parsed and formatted as CDN without
|
|
534
613
|
special handling.
|
|
535
614
|
|
|
536
|
-
|
|
615
|
+
CDN is still an Internet-Draft rather than a widely deployed RFC.
|
|
537
616
|
|
|
538
617
|
## License
|
|
539
618
|
|