cborg 2.0.4 → 3.0.0-alpha.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/.github/dependabot.yml +16 -0
- package/.github/workflows/test-and-release.yml +63 -0
- package/CHANGELOG.md +332 -0
- package/bench/bench.js +117 -0
- package/bench/json.js +124 -0
- package/bench/package.json +8 -0
- package/lib/bin.js +1 -1
- package/package.json +29 -47
- package/types/lib/bin.d.ts +1 -0
- package/cjs/browser-test/appendix_a.js +0 -643
- package/cjs/browser-test/common.js +0 -24
- package/cjs/browser-test/node-test-bin.js +0 -348
- package/cjs/browser-test/noop-bin-test.js +0 -4
- package/cjs/browser-test/test-0uint.js +0 -158
- package/cjs/browser-test/test-1negint.js +0 -152
- package/cjs/browser-test/test-2bytes.js +0 -254
- package/cjs/browser-test/test-3string.js +0 -144
- package/cjs/browser-test/test-4array.js +0 -200
- package/cjs/browser-test/test-5map.js +0 -667
- package/cjs/browser-test/test-6tag.js +0 -75
- package/cjs/browser-test/test-7float.js +0 -253
- package/cjs/browser-test/test-bl.js +0 -91
- package/cjs/browser-test/test-cbor-vectors.js +0 -98
- package/cjs/browser-test/test-decode-errors.js +0 -77
- package/cjs/browser-test/test-fuzz.js +0 -56
- package/cjs/browser-test/test-json.js +0 -281
- package/cjs/browser-test/test-length.js +0 -63
- package/cjs/cborg.js +0 -14
- package/cjs/lib/0uint.js +0 -163
- package/cjs/lib/1negint.js +0 -63
- package/cjs/lib/2bytes.js +0 -62
- package/cjs/lib/3string.js +0 -46
- package/cjs/lib/4array.js +0 -51
- package/cjs/lib/5map.js +0 -51
- package/cjs/lib/6tag.js +0 -36
- package/cjs/lib/7float.js +0 -188
- package/cjs/lib/bin.js +0 -139
- package/cjs/lib/bl.js +0 -77
- package/cjs/lib/byte-utils.js +0 -245
- package/cjs/lib/common.js +0 -22
- package/cjs/lib/decode.js +0 -140
- package/cjs/lib/diagnostic.js +0 -124
- package/cjs/lib/encode.js +0 -248
- package/cjs/lib/is.js +0 -87
- package/cjs/lib/json/decode.js +0 -414
- package/cjs/lib/json/encode.js +0 -161
- package/cjs/lib/json/json.js +0 -12
- package/cjs/lib/jump.js +0 -174
- package/cjs/lib/length.js +0 -36
- package/cjs/lib/token.js +0 -46
- package/cjs/node-test/appendix_a.js +0 -643
- package/cjs/node-test/common.js +0 -24
- package/cjs/node-test/node-test-bin.js +0 -348
- package/cjs/node-test/noop-bin-test.js +0 -4
- package/cjs/node-test/test-0uint.js +0 -158
- package/cjs/node-test/test-1negint.js +0 -152
- package/cjs/node-test/test-2bytes.js +0 -254
- package/cjs/node-test/test-3string.js +0 -144
- package/cjs/node-test/test-4array.js +0 -200
- package/cjs/node-test/test-5map.js +0 -667
- package/cjs/node-test/test-6tag.js +0 -75
- package/cjs/node-test/test-7float.js +0 -253
- package/cjs/node-test/test-bl.js +0 -91
- package/cjs/node-test/test-cbor-vectors.js +0 -98
- package/cjs/node-test/test-decode-errors.js +0 -77
- package/cjs/node-test/test-fuzz.js +0 -56
- package/cjs/node-test/test-json.js +0 -281
- package/cjs/node-test/test-length.js +0 -63
- package/cjs/taglib.js +0 -44
- package/cli.js +0 -7
- package/esm/browser-test/appendix_a.js +0 -640
- package/esm/browser-test/common.js +0 -19
- package/esm/browser-test/node-test-bin.js +0 -339
- package/esm/browser-test/noop-bin-test.js +0 -1
- package/esm/browser-test/test-0uint.js +0 -155
- package/esm/browser-test/test-1negint.js +0 -149
- package/esm/browser-test/test-2bytes.js +0 -252
- package/esm/browser-test/test-3string.js +0 -141
- package/esm/browser-test/test-4array.js +0 -197
- package/esm/browser-test/test-5map.js +0 -664
- package/esm/browser-test/test-6tag.js +0 -78
- package/esm/browser-test/test-7float.js +0 -250
- package/esm/browser-test/test-bl.js +0 -84
- package/esm/browser-test/test-cbor-vectors.js +0 -95
- package/esm/browser-test/test-decode-errors.js +0 -69
- package/esm/browser-test/test-fuzz.js +0 -50
- package/esm/browser-test/test-json.js +0 -279
- package/esm/browser-test/test-length.js +0 -55
- package/esm/cborg.js +0 -12
- package/esm/lib/0uint.js +0 -152
- package/esm/lib/1negint.js +0 -55
- package/esm/lib/2bytes.js +0 -59
- package/esm/lib/3string.js +0 -43
- package/esm/lib/4array.js +0 -41
- package/esm/lib/5map.js +0 -41
- package/esm/lib/6tag.js +0 -27
- package/esm/lib/7float.js +0 -179
- package/esm/lib/bin.js +0 -137
- package/esm/lib/bl.js +0 -74
- package/esm/lib/byte-utils.js +0 -228
- package/esm/lib/common.js +0 -19
- package/esm/lib/decode.js +0 -139
- package/esm/lib/diagnostic.js +0 -123
- package/esm/lib/encode.js +0 -246
- package/esm/lib/is.js +0 -81
- package/esm/lib/json/decode.js +0 -413
- package/esm/lib/json/encode.js +0 -160
- package/esm/lib/json/json.js +0 -10
- package/esm/lib/jump.js +0 -168
- package/esm/lib/length.js +0 -31
- package/esm/lib/token.js +0 -43
- package/esm/node-test/appendix_a.js +0 -640
- package/esm/node-test/common.js +0 -19
- package/esm/node-test/node-test-bin.js +0 -339
- package/esm/node-test/noop-bin-test.js +0 -1
- package/esm/node-test/test-0uint.js +0 -155
- package/esm/node-test/test-1negint.js +0 -149
- package/esm/node-test/test-2bytes.js +0 -252
- package/esm/node-test/test-3string.js +0 -141
- package/esm/node-test/test-4array.js +0 -197
- package/esm/node-test/test-5map.js +0 -664
- package/esm/node-test/test-6tag.js +0 -78
- package/esm/node-test/test-7float.js +0 -250
- package/esm/node-test/test-bl.js +0 -84
- package/esm/node-test/test-cbor-vectors.js +0 -95
- package/esm/node-test/test-decode-errors.js +0 -69
- package/esm/node-test/test-fuzz.js +0 -50
- package/esm/node-test/test-json.js +0 -279
- package/esm/node-test/test-length.js +0 -55
- package/esm/package.json +0 -4
- package/esm/taglib.js +0 -37
- package/index.js +0 -1
- package/json +0 -1
- package/length +0 -1
- package/taglib +0 -1
|
@@ -1,252 +0,0 @@
|
|
|
1
|
-
import chai from 'chai';
|
|
2
|
-
import {
|
|
3
|
-
decode,
|
|
4
|
-
encode
|
|
5
|
-
} from '../cborg.js';
|
|
6
|
-
import {
|
|
7
|
-
useBuffer,
|
|
8
|
-
fromHex,
|
|
9
|
-
toHex
|
|
10
|
-
} from '../lib/byte-utils.js';
|
|
11
|
-
const {assert} = chai;
|
|
12
|
-
const fixtures = [
|
|
13
|
-
{
|
|
14
|
-
data: '40',
|
|
15
|
-
expected: '',
|
|
16
|
-
type: 'bytes'
|
|
17
|
-
},
|
|
18
|
-
{
|
|
19
|
-
data: '41a1',
|
|
20
|
-
expected: 'a1',
|
|
21
|
-
type: 'bytes'
|
|
22
|
-
},
|
|
23
|
-
{
|
|
24
|
-
data: '5801a1',
|
|
25
|
-
expected: 'a1',
|
|
26
|
-
type: 'bytes',
|
|
27
|
-
strict: false
|
|
28
|
-
},
|
|
29
|
-
{
|
|
30
|
-
data: '58ff000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f202122232425262728292a2b2c2d2e2f303132333435363738393a3b3c3d3e3f404142434445464748494a4b4c4d4e4f505152535455565758595a5b5c5d5e5f606162636465666768696a6b6c6d6e6f707172737475767778797a7b7c7d7e7f808182838485868788898a8b8c8d8e8f909192939495969798999a9b9c9d9e9fa0a1a2a3a4a5a6a7a8a9aaabacadaeafb0b1b2b3b4b5b6b7b8b9babbbcbdbebfc0c1c2c3c4c5c6c7c8c9cacbcccdcecfd0d1d2d3d4d5d6d7d8d9dadbdcdddedfe0e1e2e3e4e5e6e7e8e9eaebecedeeeff0f1f2f3f4f5f6f7f8f9fafbfcfdfe',
|
|
31
|
-
expected: '000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f202122232425262728292a2b2c2d2e2f303132333435363738393a3b3c3d3e3f404142434445464748494a4b4c4d4e4f505152535455565758595a5b5c5d5e5f606162636465666768696a6b6c6d6e6f707172737475767778797a7b7c7d7e7f808182838485868788898a8b8c8d8e8f909192939495969798999a9b9c9d9e9fa0a1a2a3a4a5a6a7a8a9aaabacadaeafb0b1b2b3b4b5b6b7b8b9babbbcbdbebfc0c1c2c3c4c5c6c7c8c9cacbcccdcecfd0d1d2d3d4d5d6d7d8d9dadbdcdddedfe0e1e2e3e4e5e6e7e8e9eaebecedeeeff0f1f2f3f4f5f6f7f8f9fafbfcfdfe',
|
|
32
|
-
type: 'bytes',
|
|
33
|
-
label: 'long bytes, 8-bit length'
|
|
34
|
-
},
|
|
35
|
-
{
|
|
36
|
-
data: '5900ff000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f202122232425262728292a2b2c2d2e2f303132333435363738393a3b3c3d3e3f404142434445464748494a4b4c4d4e4f505152535455565758595a5b5c5d5e5f606162636465666768696a6b6c6d6e6f707172737475767778797a7b7c7d7e7f808182838485868788898a8b8c8d8e8f909192939495969798999a9b9c9d9e9fa0a1a2a3a4a5a6a7a8a9aaabacadaeafb0b1b2b3b4b5b6b7b8b9babbbcbdbebfc0c1c2c3c4c5c6c7c8c9cacbcccdcecfd0d1d2d3d4d5d6d7d8d9dadbdcdddedfe0e1e2e3e4e5e6e7e8e9eaebecedeeeff0f1f2f3f4f5f6f7f8f9fafbfcfdfe',
|
|
37
|
-
expected: '000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f202122232425262728292a2b2c2d2e2f303132333435363738393a3b3c3d3e3f404142434445464748494a4b4c4d4e4f505152535455565758595a5b5c5d5e5f606162636465666768696a6b6c6d6e6f707172737475767778797a7b7c7d7e7f808182838485868788898a8b8c8d8e8f909192939495969798999a9b9c9d9e9fa0a1a2a3a4a5a6a7a8a9aaabacadaeafb0b1b2b3b4b5b6b7b8b9babbbcbdbebfc0c1c2c3c4c5c6c7c8c9cacbcccdcecfd0d1d2d3d4d5d6d7d8d9dadbdcdddedfe0e1e2e3e4e5e6e7e8e9eaebecedeeeff0f1f2f3f4f5f6f7f8f9fafbfcfdfe',
|
|
38
|
-
type: 'bytes',
|
|
39
|
-
label: 'long bytes, 16-bit length',
|
|
40
|
-
strict: false
|
|
41
|
-
},
|
|
42
|
-
{
|
|
43
|
-
data: '5a000000ff000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f202122232425262728292a2b2c2d2e2f303132333435363738393a3b3c3d3e3f404142434445464748494a4b4c4d4e4f505152535455565758595a5b5c5d5e5f606162636465666768696a6b6c6d6e6f707172737475767778797a7b7c7d7e7f808182838485868788898a8b8c8d8e8f909192939495969798999a9b9c9d9e9fa0a1a2a3a4a5a6a7a8a9aaabacadaeafb0b1b2b3b4b5b6b7b8b9babbbcbdbebfc0c1c2c3c4c5c6c7c8c9cacbcccdcecfd0d1d2d3d4d5d6d7d8d9dadbdcdddedfe0e1e2e3e4e5e6e7e8e9eaebecedeeeff0f1f2f3f4f5f6f7f8f9fafbfcfdfe',
|
|
44
|
-
expected: '000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f202122232425262728292a2b2c2d2e2f303132333435363738393a3b3c3d3e3f404142434445464748494a4b4c4d4e4f505152535455565758595a5b5c5d5e5f606162636465666768696a6b6c6d6e6f707172737475767778797a7b7c7d7e7f808182838485868788898a8b8c8d8e8f909192939495969798999a9b9c9d9e9fa0a1a2a3a4a5a6a7a8a9aaabacadaeafb0b1b2b3b4b5b6b7b8b9babbbcbdbebfc0c1c2c3c4c5c6c7c8c9cacbcccdcecfd0d1d2d3d4d5d6d7d8d9dadbdcdddedfe0e1e2e3e4e5e6e7e8e9eaebecedeeeff0f1f2f3f4f5f6f7f8f9fafbfcfdfe',
|
|
45
|
-
type: 'bytes',
|
|
46
|
-
label: 'long bytes, 32-bit length',
|
|
47
|
-
strict: false
|
|
48
|
-
},
|
|
49
|
-
{
|
|
50
|
-
data: '5b00000000000000ff000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f202122232425262728292a2b2c2d2e2f303132333435363738393a3b3c3d3e3f404142434445464748494a4b4c4d4e4f505152535455565758595a5b5c5d5e5f606162636465666768696a6b6c6d6e6f707172737475767778797a7b7c7d7e7f808182838485868788898a8b8c8d8e8f909192939495969798999a9b9c9d9e9fa0a1a2a3a4a5a6a7a8a9aaabacadaeafb0b1b2b3b4b5b6b7b8b9babbbcbdbebfc0c1c2c3c4c5c6c7c8c9cacbcccdcecfd0d1d2d3d4d5d6d7d8d9dadbdcdddedfe0e1e2e3e4e5e6e7e8e9eaebecedeeeff0f1f2f3f4f5f6f7f8f9fafbfcfdfe',
|
|
51
|
-
expected: '000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f202122232425262728292a2b2c2d2e2f303132333435363738393a3b3c3d3e3f404142434445464748494a4b4c4d4e4f505152535455565758595a5b5c5d5e5f606162636465666768696a6b6c6d6e6f707172737475767778797a7b7c7d7e7f808182838485868788898a8b8c8d8e8f909192939495969798999a9b9c9d9e9fa0a1a2a3a4a5a6a7a8a9aaabacadaeafb0b1b2b3b4b5b6b7b8b9babbbcbdbebfc0c1c2c3c4c5c6c7c8c9cacbcccdcecfd0d1d2d3d4d5d6d7d8d9dadbdcdddedfe0e1e2e3e4e5e6e7e8e9eaebecedeeeff0f1f2f3f4f5f6f7f8f9fafbfcfdfe',
|
|
52
|
-
type: 'bytes',
|
|
53
|
-
label: 'long bytes, 64-bit length',
|
|
54
|
-
strict: false
|
|
55
|
-
}
|
|
56
|
-
];
|
|
57
|
-
(() => {
|
|
58
|
-
function rnd(length) {
|
|
59
|
-
return new Uint8Array(Array.from({ length }, () => Math.floor(Math.random() * 255)));
|
|
60
|
-
}
|
|
61
|
-
const expected16 = rnd(256);
|
|
62
|
-
fixtures.push({
|
|
63
|
-
data: new Uint8Array([
|
|
64
|
-
...fromHex('590100'),
|
|
65
|
-
...expected16
|
|
66
|
-
]),
|
|
67
|
-
expected: expected16,
|
|
68
|
-
type: 'bytes',
|
|
69
|
-
label: 'long bytes, 16-bit length strict-compat'
|
|
70
|
-
});
|
|
71
|
-
const expected32 = rnd(65536);
|
|
72
|
-
fixtures.push({
|
|
73
|
-
data: new Uint8Array([
|
|
74
|
-
...fromHex('5a00010000'),
|
|
75
|
-
...expected32
|
|
76
|
-
]),
|
|
77
|
-
expected: expected32,
|
|
78
|
-
type: 'bytes',
|
|
79
|
-
label: 'long bytes, 32-bit length strict-compat'
|
|
80
|
-
});
|
|
81
|
-
})();
|
|
82
|
-
describe('bytes', () => {
|
|
83
|
-
describe('decode', () => {
|
|
84
|
-
for (const fixture of fixtures) {
|
|
85
|
-
const data = fromHex(fixture.data);
|
|
86
|
-
it(`should decode ${ fixture.type }=${ fixture.label || fixture.expected }`, () => {
|
|
87
|
-
let actual = decode(data);
|
|
88
|
-
assert.strictEqual(toHex(actual), toHex(fromHex(fixture.expected)), `decode ${ fixture.type }`);
|
|
89
|
-
if (fixture.strict === false) {
|
|
90
|
-
assert.throws(() => decode(data, { strict: true }), Error, 'CBOR decode error: integer encoded in more bytes than necessary (strict decode)');
|
|
91
|
-
} else {
|
|
92
|
-
actual = decode(data, { strict: true });
|
|
93
|
-
assert.strictEqual(toHex(actual), toHex(fromHex(fixture.expected)), `decode ${ fixture.type } strict`);
|
|
94
|
-
}
|
|
95
|
-
});
|
|
96
|
-
it('should fail to decode very large length', () => {
|
|
97
|
-
assert.throws(() => decode(fromHex('5ba5f702b3a5f702b3000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f202122232425262728292a2b2c2d2e2f303132333435363738393a3b3c3d3e3f404142434445464748494a4b4c4d4e4f505152535455565758595a5b5c5d5e5f606162636465666768696a6b6c6d6e6f707172737475767778797a7b7c7d7e7f808182838485868788898a8b8c8d8e8f909192939495969798999a9b9c9d9e9fa0a1a2a3a4a5a6a7a8a9aaabacadaeafb0b1b2b3b4b5b6b7b8b9babbbcbdbebfc0c1c2c3c4c5c6c7c8c9cacbcccdcecfd0d1d2d3d4d5d6d7d8d9dadbdcdddedfe0e1e2e3e4e5e6e7e8e9eaebecedeeeff0f1f2f3f4f5f6f7f8f9fafbfcfdfe')), /CBOR decode error: 64-bit integer bytes lengths not supported/);
|
|
98
|
-
});
|
|
99
|
-
}
|
|
100
|
-
});
|
|
101
|
-
describe('encode', () => {
|
|
102
|
-
for (const fixture of fixtures) {
|
|
103
|
-
if (fixture.data.length >= 100000000) {
|
|
104
|
-
it.skip(`(TODO) skipping encode of very large bytes ${ fixture.type }=${ fixture.label || fixture.expected }`, () => {
|
|
105
|
-
});
|
|
106
|
-
continue;
|
|
107
|
-
}
|
|
108
|
-
const data = fromHex(fixture.expected);
|
|
109
|
-
const expectedHex = toHex(fixture.data);
|
|
110
|
-
it(`should encode ${ fixture.type }=${ fixture.label || fixture.expected }`, () => {
|
|
111
|
-
if (fixture.unsafe) {
|
|
112
|
-
assert.throws(() => encode(data), Error, /^CBOR encode error: number too large to encode \(-\d+\)$/);
|
|
113
|
-
} else if (fixture.strict === false) {
|
|
114
|
-
assert.notStrictEqual(toHex(encode(data)), expectedHex, `encode ${ fixture.type } !strict`);
|
|
115
|
-
} else {
|
|
116
|
-
assert.strictEqual(toHex(encode(data)), expectedHex, `encode ${ fixture.type }`);
|
|
117
|
-
}
|
|
118
|
-
});
|
|
119
|
-
}
|
|
120
|
-
});
|
|
121
|
-
describe('typedarrays', () => {
|
|
122
|
-
const cases = [
|
|
123
|
-
{
|
|
124
|
-
obj: Uint8Array.from([
|
|
125
|
-
1,
|
|
126
|
-
2,
|
|
127
|
-
3
|
|
128
|
-
]),
|
|
129
|
-
hex: '43010203'
|
|
130
|
-
},
|
|
131
|
-
{
|
|
132
|
-
obj: Uint8ClampedArray.from([
|
|
133
|
-
1,
|
|
134
|
-
2,
|
|
135
|
-
3
|
|
136
|
-
]),
|
|
137
|
-
hex: '43010203'
|
|
138
|
-
},
|
|
139
|
-
{
|
|
140
|
-
obj: Uint16Array.from([
|
|
141
|
-
1,
|
|
142
|
-
2,
|
|
143
|
-
3
|
|
144
|
-
]),
|
|
145
|
-
hex: '46010002000300'
|
|
146
|
-
},
|
|
147
|
-
{
|
|
148
|
-
obj: Uint32Array.from([
|
|
149
|
-
1,
|
|
150
|
-
2,
|
|
151
|
-
3
|
|
152
|
-
]),
|
|
153
|
-
hex: '4c010000000200000003000000'
|
|
154
|
-
},
|
|
155
|
-
{
|
|
156
|
-
obj: Int8Array.from([
|
|
157
|
-
1,
|
|
158
|
-
2,
|
|
159
|
-
-3
|
|
160
|
-
]),
|
|
161
|
-
hex: '430102fd'
|
|
162
|
-
},
|
|
163
|
-
{
|
|
164
|
-
obj: Int16Array.from([
|
|
165
|
-
1,
|
|
166
|
-
2,
|
|
167
|
-
-3
|
|
168
|
-
]),
|
|
169
|
-
hex: '4601000200fdff'
|
|
170
|
-
},
|
|
171
|
-
{
|
|
172
|
-
obj: Int32Array.from([
|
|
173
|
-
1,
|
|
174
|
-
2,
|
|
175
|
-
-3
|
|
176
|
-
]),
|
|
177
|
-
hex: '4c0100000002000000fdffffff'
|
|
178
|
-
},
|
|
179
|
-
{
|
|
180
|
-
obj: Float32Array.from([
|
|
181
|
-
1,
|
|
182
|
-
2,
|
|
183
|
-
-3
|
|
184
|
-
]),
|
|
185
|
-
hex: '4c0000803f00000040000040c0'
|
|
186
|
-
},
|
|
187
|
-
{
|
|
188
|
-
obj: Float64Array.from([
|
|
189
|
-
1,
|
|
190
|
-
2,
|
|
191
|
-
-3
|
|
192
|
-
]),
|
|
193
|
-
hex: '5818000000000000f03f000000000000004000000000000008c0'
|
|
194
|
-
},
|
|
195
|
-
{
|
|
196
|
-
obj: BigUint64Array.from([
|
|
197
|
-
BigInt(1),
|
|
198
|
-
BigInt(2),
|
|
199
|
-
BigInt(3)
|
|
200
|
-
]),
|
|
201
|
-
hex: '5818010000000000000002000000000000000300000000000000'
|
|
202
|
-
},
|
|
203
|
-
{
|
|
204
|
-
obj: BigInt64Array.from([
|
|
205
|
-
BigInt(1),
|
|
206
|
-
BigInt(2),
|
|
207
|
-
BigInt(-3)
|
|
208
|
-
]),
|
|
209
|
-
hex: '581801000000000000000200000000000000fdffffffffffffff'
|
|
210
|
-
},
|
|
211
|
-
{
|
|
212
|
-
obj: new DataView(Uint8Array.from([
|
|
213
|
-
1,
|
|
214
|
-
2,
|
|
215
|
-
3
|
|
216
|
-
]).buffer),
|
|
217
|
-
hex: '43010203'
|
|
218
|
-
},
|
|
219
|
-
{
|
|
220
|
-
obj: Uint8Array.from([
|
|
221
|
-
1,
|
|
222
|
-
2,
|
|
223
|
-
3
|
|
224
|
-
]).buffer,
|
|
225
|
-
hex: '43010203'
|
|
226
|
-
}
|
|
227
|
-
];
|
|
228
|
-
for (const testCase of cases) {
|
|
229
|
-
it(testCase.obj.constructor.name, () => {
|
|
230
|
-
assert.equal(toHex(encode(testCase.obj)), testCase.hex);
|
|
231
|
-
const decoded = decode(fromHex(testCase.hex));
|
|
232
|
-
assert.instanceOf(decoded, Uint8Array);
|
|
233
|
-
assert.equal(toHex(decoded), toHex(testCase.obj));
|
|
234
|
-
});
|
|
235
|
-
}
|
|
236
|
-
});
|
|
237
|
-
if (useBuffer) {
|
|
238
|
-
describe('buffer', () => {
|
|
239
|
-
it('can encode Node.js Buffers', () => {
|
|
240
|
-
const obj = global.Buffer.from([
|
|
241
|
-
1,
|
|
242
|
-
2,
|
|
243
|
-
3
|
|
244
|
-
]);
|
|
245
|
-
assert.equal(toHex(encode(obj)), '43010203');
|
|
246
|
-
const decoded = decode(fromHex('43010203'));
|
|
247
|
-
assert.instanceOf(decoded, Uint8Array);
|
|
248
|
-
assert.equal(toHex(decoded), toHex(obj));
|
|
249
|
-
});
|
|
250
|
-
});
|
|
251
|
-
}
|
|
252
|
-
});
|
|
@@ -1,141 +0,0 @@
|
|
|
1
|
-
import chai from 'chai';
|
|
2
|
-
import {
|
|
3
|
-
decode,
|
|
4
|
-
encode
|
|
5
|
-
} from '../cborg.js';
|
|
6
|
-
import {
|
|
7
|
-
fromHex,
|
|
8
|
-
toHex
|
|
9
|
-
} from '../lib/byte-utils.js';
|
|
10
|
-
const {assert} = chai;
|
|
11
|
-
const fixtures = [
|
|
12
|
-
{
|
|
13
|
-
data: '60',
|
|
14
|
-
expected: '',
|
|
15
|
-
type: 'string'
|
|
16
|
-
},
|
|
17
|
-
{
|
|
18
|
-
data: '6161',
|
|
19
|
-
expected: 'a',
|
|
20
|
-
type: 'string'
|
|
21
|
-
},
|
|
22
|
-
{
|
|
23
|
-
data: '780161',
|
|
24
|
-
expected: 'a',
|
|
25
|
-
type: 'string',
|
|
26
|
-
strict: false
|
|
27
|
-
},
|
|
28
|
-
{
|
|
29
|
-
data: '6c48656c6c6f20776f726c6421',
|
|
30
|
-
expected: 'Hello world!',
|
|
31
|
-
type: 'string'
|
|
32
|
-
},
|
|
33
|
-
{
|
|
34
|
-
data: '6fc48c6175657320c39f76c49b746521',
|
|
35
|
-
expected: 'Čaues ßvěte!',
|
|
36
|
-
type: 'string'
|
|
37
|
-
},
|
|
38
|
-
{
|
|
39
|
-
data: '78964c6f72656d20697073756d20646f6c6f722073697420616d65742c20636f6e73656374657475722061646970697363696e6720656c69742e20446f6e6563206d692074656c6c75732c20696163756c6973206e656320766573746962756c756d20717569732c206665726d656e74756d206e6f6e2066656c69732e204d616563656e6173207574206a7573746f20706f73756572652e',
|
|
40
|
-
expected: 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec mi tellus, iaculis nec vestibulum quis, fermentum non felis. Maecenas ut justo posuere.',
|
|
41
|
-
type: 'string',
|
|
42
|
-
label: 'long string, 8-bit length'
|
|
43
|
-
},
|
|
44
|
-
{
|
|
45
|
-
data: '7900964c6f72656d20697073756d20646f6c6f722073697420616d65742c20636f6e73656374657475722061646970697363696e6720656c69742e20446f6e6563206d692074656c6c75732c20696163756c6973206e656320766573746962756c756d20717569732c206665726d656e74756d206e6f6e2066656c69732e204d616563656e6173207574206a7573746f20706f73756572652e',
|
|
46
|
-
expected: 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec mi tellus, iaculis nec vestibulum quis, fermentum non felis. Maecenas ut justo posuere.',
|
|
47
|
-
type: 'string',
|
|
48
|
-
label: 'long string, 16-bit length',
|
|
49
|
-
strict: false
|
|
50
|
-
},
|
|
51
|
-
{
|
|
52
|
-
data: '7a000000964c6f72656d20697073756d20646f6c6f722073697420616d65742c20636f6e73656374657475722061646970697363696e6720656c69742e20446f6e6563206d692074656c6c75732c20696163756c6973206e656320766573746962756c756d20717569732c206665726d656e74756d206e6f6e2066656c69732e204d616563656e6173207574206a7573746f20706f73756572652e',
|
|
53
|
-
expected: 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec mi tellus, iaculis nec vestibulum quis, fermentum non felis. Maecenas ut justo posuere.',
|
|
54
|
-
type: 'string',
|
|
55
|
-
label: 'long string, 32-bit length',
|
|
56
|
-
strict: false
|
|
57
|
-
},
|
|
58
|
-
{
|
|
59
|
-
data: '7b00000000000000964c6f72656d20697073756d20646f6c6f722073697420616d65742c20636f6e73656374657475722061646970697363696e6720656c69742e20446f6e6563206d692074656c6c75732c20696163756c6973206e656320766573746962756c756d20717569732c206665726d656e74756d206e6f6e2066656c69732e204d616563656e6173207574206a7573746f20706f73756572652e',
|
|
60
|
-
expected: 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec mi tellus, iaculis nec vestibulum quis, fermentum non felis. Maecenas ut justo posuere.',
|
|
61
|
-
type: 'string',
|
|
62
|
-
label: 'long string, 64-bit length',
|
|
63
|
-
strict: false
|
|
64
|
-
}
|
|
65
|
-
];
|
|
66
|
-
(() => {
|
|
67
|
-
function rnd(length) {
|
|
68
|
-
const sa = [];
|
|
69
|
-
let l = 0;
|
|
70
|
-
while (l < length) {
|
|
71
|
-
const ascii = length - l < 3;
|
|
72
|
-
const base = ascii ? 32 : 126976;
|
|
73
|
-
const max = ascii ? 126 : 130816;
|
|
74
|
-
const cc = Math.floor(Math.random() * (max - base)) + base;
|
|
75
|
-
const s = String.fromCharCode(cc);
|
|
76
|
-
l += new TextEncoder().encode(s).length;
|
|
77
|
-
sa.push(s);
|
|
78
|
-
}
|
|
79
|
-
return sa.join('');
|
|
80
|
-
}
|
|
81
|
-
const expected16 = rnd(256);
|
|
82
|
-
fixtures.push({
|
|
83
|
-
data: new Uint8Array([
|
|
84
|
-
...fromHex('790100'),
|
|
85
|
-
...new TextEncoder().encode(expected16)
|
|
86
|
-
]),
|
|
87
|
-
expected: expected16,
|
|
88
|
-
type: 'string',
|
|
89
|
-
label: 'long string, 16-bit length strict-compat'
|
|
90
|
-
});
|
|
91
|
-
const expected32 = rnd(65536);
|
|
92
|
-
fixtures.push({
|
|
93
|
-
data: new Uint8Array([
|
|
94
|
-
...fromHex('7a00010000'),
|
|
95
|
-
...new TextEncoder().encode(expected32)
|
|
96
|
-
]),
|
|
97
|
-
expected: expected32,
|
|
98
|
-
type: 'string',
|
|
99
|
-
label: 'long string, 32-bit length strict-compat'
|
|
100
|
-
});
|
|
101
|
-
})();
|
|
102
|
-
describe('string', () => {
|
|
103
|
-
describe('decode', () => {
|
|
104
|
-
for (const fixture of fixtures) {
|
|
105
|
-
const data = fromHex(fixture.data);
|
|
106
|
-
it(`should decode ${ fixture.type }=${ fixture.label || fixture.expected }`, () => {
|
|
107
|
-
let actual = decode(data);
|
|
108
|
-
assert.strictEqual(actual, fixture.expected, `decode ${ fixture.type }`);
|
|
109
|
-
if (fixture.strict === false) {
|
|
110
|
-
assert.throws(() => decode(data, { strict: true }), Error, 'CBOR decode error: integer encoded in more bytes than necessary (strict decode)');
|
|
111
|
-
} else {
|
|
112
|
-
actual = decode(data, { strict: true });
|
|
113
|
-
assert.strictEqual(actual, fixture.expected, `decode ${ fixture.type } strict`);
|
|
114
|
-
}
|
|
115
|
-
});
|
|
116
|
-
it('should fail to decode very large length', () => {
|
|
117
|
-
assert.throws(() => decode(fromHex('7ba5f702b3a5f702b34c6f72656d20697073756d20646f6c6f722073697420616d65742c20636f6e73656374657475722061646970697363696e6720656c69742e20446f6e6563206d692074656c6c75732c20696163756c6973206e656320766573746962756c756d20717569732c206665726d656e74756d206e6f6e2066656c69732e204d616563656e6173207574206a7573746f20706f73756572652e')), /CBOR decode error: 64-bit integer string lengths not supported/);
|
|
118
|
-
});
|
|
119
|
-
}
|
|
120
|
-
});
|
|
121
|
-
describe('encode', () => {
|
|
122
|
-
for (const fixture of fixtures) {
|
|
123
|
-
if (fixture.data.length >= 100000000) {
|
|
124
|
-
it.skip(`(TODO) skipping encode of very large string ${ fixture.type }=${ fixture.label || fixture.expected }`, () => {
|
|
125
|
-
});
|
|
126
|
-
continue;
|
|
127
|
-
}
|
|
128
|
-
const data = fixture.expected;
|
|
129
|
-
const expectedHex = toHex(fixture.data);
|
|
130
|
-
it(`should encode ${ fixture.type }=${ fixture.label || fixture.expected }`, () => {
|
|
131
|
-
if (fixture.unsafe) {
|
|
132
|
-
assert.throws(() => encode(data), Error, /^CBOR encode error: number too large to encode \(-\d+\)$/);
|
|
133
|
-
} else if (fixture.strict === false) {
|
|
134
|
-
assert.notStrictEqual(toHex(encode(data)), expectedHex, `encode ${ fixture.type } !strict`);
|
|
135
|
-
} else {
|
|
136
|
-
assert.strictEqual(toHex(encode(data)), expectedHex, `encode ${ fixture.type }`);
|
|
137
|
-
}
|
|
138
|
-
});
|
|
139
|
-
}
|
|
140
|
-
});
|
|
141
|
-
});
|
|
@@ -1,197 +0,0 @@
|
|
|
1
|
-
import chai from 'chai';
|
|
2
|
-
import {
|
|
3
|
-
decode,
|
|
4
|
-
encode
|
|
5
|
-
} from '../cborg.js';
|
|
6
|
-
import {
|
|
7
|
-
fromHex,
|
|
8
|
-
toHex
|
|
9
|
-
} from '../lib/byte-utils.js';
|
|
10
|
-
const {assert} = chai;
|
|
11
|
-
const fixtures = [
|
|
12
|
-
{
|
|
13
|
-
data: '80',
|
|
14
|
-
expected: [],
|
|
15
|
-
type: 'array empty'
|
|
16
|
-
},
|
|
17
|
-
{
|
|
18
|
-
data: '8102',
|
|
19
|
-
expected: [2],
|
|
20
|
-
type: 'array 1 compact uint'
|
|
21
|
-
},
|
|
22
|
-
{
|
|
23
|
-
data: '8118ff',
|
|
24
|
-
expected: [255],
|
|
25
|
-
type: 'array 1 uint8'
|
|
26
|
-
},
|
|
27
|
-
{
|
|
28
|
-
data: '811901f4',
|
|
29
|
-
expected: [500],
|
|
30
|
-
type: 'array 1 uint16'
|
|
31
|
-
},
|
|
32
|
-
{
|
|
33
|
-
data: '811a00010000',
|
|
34
|
-
expected: [65536],
|
|
35
|
-
type: 'array 1 uint32'
|
|
36
|
-
},
|
|
37
|
-
{
|
|
38
|
-
data: '811b00000000000000ff',
|
|
39
|
-
expected: [255],
|
|
40
|
-
type: 'array 1 uint64',
|
|
41
|
-
strict: false
|
|
42
|
-
},
|
|
43
|
-
{
|
|
44
|
-
data: '811b0016db6db6db6db7',
|
|
45
|
-
expected: [Number.MAX_SAFE_INTEGER / 1.4],
|
|
46
|
-
type: 'array 1 uint64'
|
|
47
|
-
},
|
|
48
|
-
{
|
|
49
|
-
data: '811b001fffffffffffff',
|
|
50
|
-
expected: [Number.MAX_SAFE_INTEGER],
|
|
51
|
-
type: 'array 1 uint64'
|
|
52
|
-
},
|
|
53
|
-
{
|
|
54
|
-
data: '8403040506',
|
|
55
|
-
expected: [
|
|
56
|
-
3,
|
|
57
|
-
4,
|
|
58
|
-
5,
|
|
59
|
-
6
|
|
60
|
-
],
|
|
61
|
-
type: 'array 4 ints'
|
|
62
|
-
},
|
|
63
|
-
{
|
|
64
|
-
data: '8c1b0016db6db6db6db71a000100001901f40200202238ff3aa5f702b33b0016db6db6db6db74261316fc48c6175657320c39f76c49b746521',
|
|
65
|
-
expected: [
|
|
66
|
-
Number.MAX_SAFE_INTEGER / 1.4,
|
|
67
|
-
65536,
|
|
68
|
-
500,
|
|
69
|
-
2,
|
|
70
|
-
0,
|
|
71
|
-
-1,
|
|
72
|
-
-3,
|
|
73
|
-
-256,
|
|
74
|
-
-2784428724,
|
|
75
|
-
Number.MIN_SAFE_INTEGER / 1.4 - 1,
|
|
76
|
-
new TextEncoder().encode('a1'),
|
|
77
|
-
'Čaues ßvěte!'
|
|
78
|
-
],
|
|
79
|
-
type: 'array mixed terminals',
|
|
80
|
-
label: '[]'
|
|
81
|
-
},
|
|
82
|
-
{
|
|
83
|
-
data: '8265617272617982626f66820582666e657374656482666172726179736121',
|
|
84
|
-
expected: [
|
|
85
|
-
'array',
|
|
86
|
-
[
|
|
87
|
-
'of',
|
|
88
|
-
[
|
|
89
|
-
5,
|
|
90
|
-
[
|
|
91
|
-
'nested',
|
|
92
|
-
[
|
|
93
|
-
'arrays',
|
|
94
|
-
'!'
|
|
95
|
-
]
|
|
96
|
-
]
|
|
97
|
-
]
|
|
98
|
-
]
|
|
99
|
-
],
|
|
100
|
-
type: 'array nested'
|
|
101
|
-
},
|
|
102
|
-
{
|
|
103
|
-
data: '980403040506',
|
|
104
|
-
expected: [
|
|
105
|
-
3,
|
|
106
|
-
4,
|
|
107
|
-
5,
|
|
108
|
-
6
|
|
109
|
-
],
|
|
110
|
-
type: 'array 4 ints, length8',
|
|
111
|
-
strict: false
|
|
112
|
-
},
|
|
113
|
-
{
|
|
114
|
-
data: '99000403040506',
|
|
115
|
-
expected: [
|
|
116
|
-
3,
|
|
117
|
-
4,
|
|
118
|
-
5,
|
|
119
|
-
6
|
|
120
|
-
],
|
|
121
|
-
type: 'array 4 ints, length16',
|
|
122
|
-
strict: false
|
|
123
|
-
},
|
|
124
|
-
{
|
|
125
|
-
data: '9a0000000403040506',
|
|
126
|
-
expected: [
|
|
127
|
-
3,
|
|
128
|
-
4,
|
|
129
|
-
5,
|
|
130
|
-
6
|
|
131
|
-
],
|
|
132
|
-
type: 'array 4 ints, length32',
|
|
133
|
-
strict: false
|
|
134
|
-
},
|
|
135
|
-
{
|
|
136
|
-
data: '9b000000000000000403040506',
|
|
137
|
-
expected: [
|
|
138
|
-
3,
|
|
139
|
-
4,
|
|
140
|
-
5,
|
|
141
|
-
6
|
|
142
|
-
],
|
|
143
|
-
type: 'array 4 ints, length64',
|
|
144
|
-
strict: false
|
|
145
|
-
}
|
|
146
|
-
];
|
|
147
|
-
describe('array', () => {
|
|
148
|
-
describe('decode', () => {
|
|
149
|
-
for (const fixture of fixtures) {
|
|
150
|
-
const data = fromHex(fixture.data);
|
|
151
|
-
it(`should decode ${ fixture.type }=${ fixture.label || fixture.expected }`, () => {
|
|
152
|
-
assert.deepStrictEqual(decode(data), fixture.expected, `decode ${ fixture.type }`);
|
|
153
|
-
if (fixture.strict === false) {
|
|
154
|
-
assert.throws(() => decode(data, { strict: true }), Error, 'CBOR decode error: integer encoded in more bytes than necessary (strict decode)');
|
|
155
|
-
} else {
|
|
156
|
-
assert.deepStrictEqual(decode(data, { strict: true }), fixture.expected, `decode ${ fixture.type }`);
|
|
157
|
-
}
|
|
158
|
-
});
|
|
159
|
-
it('should fail to decode very large length', () => {
|
|
160
|
-
assert.throws(() => decode(fromHex('9ba5f702b3a5f7020403040506')), /CBOR decode error: 64-bit integer array lengths not supported/);
|
|
161
|
-
});
|
|
162
|
-
}
|
|
163
|
-
});
|
|
164
|
-
describe('encode', () => {
|
|
165
|
-
for (const fixture of fixtures) {
|
|
166
|
-
it(`should encode ${ fixture.type }=${ fixture.label || fixture.expected }`, () => {
|
|
167
|
-
if (fixture.unsafe) {
|
|
168
|
-
assert.throws(encode.bind(null, fixture.expected), Error, /^CBOR encode error: number too large to encode \(\d+\)$/);
|
|
169
|
-
} else if (fixture.strict === false) {
|
|
170
|
-
assert.notDeepEqual(toHex(encode(fixture.expected)), fixture.data, `encode ${ fixture.type } !strict`);
|
|
171
|
-
} else {
|
|
172
|
-
assert.strictEqual(toHex(encode(fixture.expected)), fixture.data, `encode ${ fixture.type }`);
|
|
173
|
-
}
|
|
174
|
-
});
|
|
175
|
-
}
|
|
176
|
-
});
|
|
177
|
-
describe('roundtrip', () => {
|
|
178
|
-
for (const fixture of fixtures) {
|
|
179
|
-
if (!fixture.unsafe && fixture.strict !== false) {
|
|
180
|
-
it(`should roundtrip ${ fixture.type }=${ fixture.label || fixture.expected }`, () => {
|
|
181
|
-
assert.deepStrictEqual(decode(encode(fixture.expected)), fixture.expected, `roundtrip ${ fixture.type }`);
|
|
182
|
-
});
|
|
183
|
-
}
|
|
184
|
-
}
|
|
185
|
-
});
|
|
186
|
-
describe('specials', () => {
|
|
187
|
-
it('can decode indefinite length items', () => {
|
|
188
|
-
assert.deepStrictEqual(decode(fromHex('9f616f6174ff')), [
|
|
189
|
-
'o',
|
|
190
|
-
't'
|
|
191
|
-
]);
|
|
192
|
-
});
|
|
193
|
-
it('can switch off indefinite length support', () => {
|
|
194
|
-
assert.throws(() => decode(fromHex('9f616f6174ff'), { allowIndefinite: false }), /indefinite/);
|
|
195
|
-
});
|
|
196
|
-
});
|
|
197
|
-
});
|