cborg 1.10.1 → 2.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.
Files changed (140) hide show
  1. package/.github/dependabot.yml +16 -0
  2. package/.github/workflows/test-and-release.yml +63 -0
  3. package/CHANGELOG.md +332 -0
  4. package/bench/bench.js +117 -0
  5. package/bench/json.js +124 -0
  6. package/bench/package.json +8 -0
  7. package/lib/bin.js +1 -1
  8. package/package.json +29 -47
  9. package/types/lib/4array.d.ts +1 -1
  10. package/types/lib/5map.d.ts +1 -1
  11. package/types/lib/6tag.d.ts +1 -1
  12. package/types/lib/7float.d.ts +1 -1
  13. package/types/lib/bin.d.ts +1 -0
  14. package/types/lib/token.d.ts +13 -13
  15. package/cjs/browser-test/appendix_a.js +0 -643
  16. package/cjs/browser-test/common.js +0 -24
  17. package/cjs/browser-test/node-test-bin.js +0 -348
  18. package/cjs/browser-test/noop-bin-test.js +0 -4
  19. package/cjs/browser-test/test-0uint.js +0 -158
  20. package/cjs/browser-test/test-1negint.js +0 -152
  21. package/cjs/browser-test/test-2bytes.js +0 -254
  22. package/cjs/browser-test/test-3string.js +0 -144
  23. package/cjs/browser-test/test-4array.js +0 -200
  24. package/cjs/browser-test/test-5map.js +0 -667
  25. package/cjs/browser-test/test-6tag.js +0 -75
  26. package/cjs/browser-test/test-7float.js +0 -253
  27. package/cjs/browser-test/test-bl.js +0 -91
  28. package/cjs/browser-test/test-cbor-vectors.js +0 -98
  29. package/cjs/browser-test/test-decode-errors.js +0 -77
  30. package/cjs/browser-test/test-fuzz.js +0 -56
  31. package/cjs/browser-test/test-json.js +0 -281
  32. package/cjs/browser-test/test-length.js +0 -63
  33. package/cjs/cborg.js +0 -14
  34. package/cjs/lib/0uint.js +0 -163
  35. package/cjs/lib/1negint.js +0 -63
  36. package/cjs/lib/2bytes.js +0 -62
  37. package/cjs/lib/3string.js +0 -46
  38. package/cjs/lib/4array.js +0 -51
  39. package/cjs/lib/5map.js +0 -51
  40. package/cjs/lib/6tag.js +0 -36
  41. package/cjs/lib/7float.js +0 -188
  42. package/cjs/lib/bin.js +0 -139
  43. package/cjs/lib/bl.js +0 -77
  44. package/cjs/lib/byte-utils.js +0 -245
  45. package/cjs/lib/common.js +0 -22
  46. package/cjs/lib/decode.js +0 -140
  47. package/cjs/lib/diagnostic.js +0 -124
  48. package/cjs/lib/encode.js +0 -248
  49. package/cjs/lib/is.js +0 -87
  50. package/cjs/lib/json/decode.js +0 -414
  51. package/cjs/lib/json/encode.js +0 -161
  52. package/cjs/lib/json/json.js +0 -12
  53. package/cjs/lib/jump.js +0 -174
  54. package/cjs/lib/length.js +0 -36
  55. package/cjs/lib/token.js +0 -46
  56. package/cjs/node-test/appendix_a.js +0 -643
  57. package/cjs/node-test/common.js +0 -24
  58. package/cjs/node-test/node-test-bin.js +0 -348
  59. package/cjs/node-test/noop-bin-test.js +0 -4
  60. package/cjs/node-test/test-0uint.js +0 -158
  61. package/cjs/node-test/test-1negint.js +0 -152
  62. package/cjs/node-test/test-2bytes.js +0 -254
  63. package/cjs/node-test/test-3string.js +0 -144
  64. package/cjs/node-test/test-4array.js +0 -200
  65. package/cjs/node-test/test-5map.js +0 -667
  66. package/cjs/node-test/test-6tag.js +0 -75
  67. package/cjs/node-test/test-7float.js +0 -253
  68. package/cjs/node-test/test-bl.js +0 -91
  69. package/cjs/node-test/test-cbor-vectors.js +0 -98
  70. package/cjs/node-test/test-decode-errors.js +0 -77
  71. package/cjs/node-test/test-fuzz.js +0 -56
  72. package/cjs/node-test/test-json.js +0 -281
  73. package/cjs/node-test/test-length.js +0 -63
  74. package/cjs/taglib.js +0 -44
  75. package/cli.js +0 -7
  76. package/esm/browser-test/appendix_a.js +0 -640
  77. package/esm/browser-test/common.js +0 -19
  78. package/esm/browser-test/node-test-bin.js +0 -339
  79. package/esm/browser-test/noop-bin-test.js +0 -1
  80. package/esm/browser-test/test-0uint.js +0 -155
  81. package/esm/browser-test/test-1negint.js +0 -149
  82. package/esm/browser-test/test-2bytes.js +0 -252
  83. package/esm/browser-test/test-3string.js +0 -141
  84. package/esm/browser-test/test-4array.js +0 -197
  85. package/esm/browser-test/test-5map.js +0 -664
  86. package/esm/browser-test/test-6tag.js +0 -78
  87. package/esm/browser-test/test-7float.js +0 -250
  88. package/esm/browser-test/test-bl.js +0 -84
  89. package/esm/browser-test/test-cbor-vectors.js +0 -95
  90. package/esm/browser-test/test-decode-errors.js +0 -69
  91. package/esm/browser-test/test-fuzz.js +0 -50
  92. package/esm/browser-test/test-json.js +0 -279
  93. package/esm/browser-test/test-length.js +0 -55
  94. package/esm/cborg.js +0 -12
  95. package/esm/lib/0uint.js +0 -152
  96. package/esm/lib/1negint.js +0 -55
  97. package/esm/lib/2bytes.js +0 -59
  98. package/esm/lib/3string.js +0 -43
  99. package/esm/lib/4array.js +0 -41
  100. package/esm/lib/5map.js +0 -41
  101. package/esm/lib/6tag.js +0 -27
  102. package/esm/lib/7float.js +0 -179
  103. package/esm/lib/bin.js +0 -137
  104. package/esm/lib/bl.js +0 -74
  105. package/esm/lib/byte-utils.js +0 -228
  106. package/esm/lib/common.js +0 -19
  107. package/esm/lib/decode.js +0 -139
  108. package/esm/lib/diagnostic.js +0 -123
  109. package/esm/lib/encode.js +0 -246
  110. package/esm/lib/is.js +0 -81
  111. package/esm/lib/json/decode.js +0 -413
  112. package/esm/lib/json/encode.js +0 -160
  113. package/esm/lib/json/json.js +0 -10
  114. package/esm/lib/jump.js +0 -168
  115. package/esm/lib/length.js +0 -31
  116. package/esm/lib/token.js +0 -43
  117. package/esm/node-test/appendix_a.js +0 -640
  118. package/esm/node-test/common.js +0 -19
  119. package/esm/node-test/node-test-bin.js +0 -339
  120. package/esm/node-test/noop-bin-test.js +0 -1
  121. package/esm/node-test/test-0uint.js +0 -155
  122. package/esm/node-test/test-1negint.js +0 -149
  123. package/esm/node-test/test-2bytes.js +0 -252
  124. package/esm/node-test/test-3string.js +0 -141
  125. package/esm/node-test/test-4array.js +0 -197
  126. package/esm/node-test/test-5map.js +0 -664
  127. package/esm/node-test/test-6tag.js +0 -78
  128. package/esm/node-test/test-7float.js +0 -250
  129. package/esm/node-test/test-bl.js +0 -84
  130. package/esm/node-test/test-cbor-vectors.js +0 -95
  131. package/esm/node-test/test-decode-errors.js +0 -69
  132. package/esm/node-test/test-fuzz.js +0 -50
  133. package/esm/node-test/test-json.js +0 -279
  134. package/esm/node-test/test-length.js +0 -55
  135. package/esm/package.json +0 -4
  136. package/esm/taglib.js +0 -37
  137. package/index.js +0 -1
  138. package/json +0 -1
  139. package/length +0 -1
  140. package/taglib +0 -1
@@ -1,78 +0,0 @@
1
- import chai from 'chai';
2
- import {
3
- Token,
4
- Type
5
- } from '../lib/token.js';
6
- import {
7
- decode,
8
- encode
9
- } from '../cborg.js';
10
- import {
11
- fromHex,
12
- toHex
13
- } from '../lib/byte-utils.js';
14
- import {
15
- dateDecoder,
16
- dateEncoder
17
- } from './common.js';
18
- const {assert} = chai;
19
- function Uint16ArrayDecoder(obj) {
20
- if (typeof obj !== 'string') {
21
- throw new Error('expected string for tag 23');
22
- }
23
- const u8a = fromHex(obj);
24
- return new Uint16Array(u8a.buffer, u8a.byteOffset, u8a.length / 2);
25
- }
26
- function Uint16ArrayEncoder(obj) {
27
- if (!(obj instanceof Uint16Array)) {
28
- throw new Error('expected Uint16Array for "Uint16Array" encoder');
29
- }
30
- return [
31
- new Token(Type.tag, 23),
32
- new Token(Type.string, toHex(obj))
33
- ];
34
- }
35
- describe('tag', () => {
36
- it('date', () => {
37
- assert.throws(() => encode({ d: new Date() }), /unsupported type: Date/);
38
- assert.equal(toHex(encode(new Date('2013-03-21T20:04:00Z'), { typeEncoders: { Date: dateEncoder } })), 'c074323031332d30332d32315432303a30343a30305a');
39
- const decodedDate = decode(fromHex('c074323031332d30332d32315432303a30343a30305a'), { tags: { 0: dateDecoder } });
40
- assert.instanceOf(decodedDate, Date);
41
- assert.equal(decodedDate.toISOString(), new Date('2013-03-21T20:04:00Z').toISOString());
42
- });
43
- it('Uint16Array as hex/23 (overide existing type)', () => {
44
- assert.equal(toHex(encode(Uint16Array.from([
45
- 1,
46
- 2,
47
- 3
48
- ]), { typeEncoders: { Uint16Array: Uint16ArrayEncoder } })), 'd76c303130303032303030333030');
49
- const decoded = decode(fromHex('d76c303130303032303030333030'), { tags: { 23: Uint16ArrayDecoder } });
50
- assert.instanceOf(decoded, Uint16Array);
51
- assert.equal(toHex(decoded), toHex(Uint16Array.from([
52
- 1,
53
- 2,
54
- 3
55
- ])));
56
- });
57
- it('tag int too large', () => {
58
- const verify = (hex, strict) => {
59
- if (!strict) {
60
- assert.throws(() => decode(fromHex(hex), {
61
- tags: { 8: dateDecoder },
62
- strict: true
63
- }), /integer encoded in more bytes than necessary/);
64
- }
65
- const decodedDate = decode(fromHex(hex), {
66
- tags: { 8: dateDecoder },
67
- strict
68
- });
69
- assert.instanceOf(decodedDate, Date);
70
- assert.equal(decodedDate.toISOString(), new Date('2013-03-21T20:04:00Z').toISOString());
71
- };
72
- verify('c874323031332d30332d32315432303a30343a30305a', true);
73
- verify('d80874323031332d30332d32315432303a30343a30305a', false);
74
- verify('d9000874323031332d30332d32315432303a30343a30305a', false);
75
- verify('da0000000874323031332d30332d32315432303a30343a30305a', false);
76
- verify('db000000000000000874323031332d30332d32315432303a30343a30305a', false);
77
- });
78
- });
@@ -1,250 +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: '8601f5f4f6f720',
14
- expected: [
15
- 1,
16
- true,
17
- false,
18
- null,
19
- undefined,
20
- -1
21
- ],
22
- type: 'array of float specials'
23
- },
24
- {
25
- data: 'f93800',
26
- expected: 0.5,
27
- type: 'float16'
28
- },
29
- {
30
- data: 'f9b800',
31
- expected: -0.5,
32
- type: 'float16'
33
- },
34
- {
35
- data: 'fa33c00000',
36
- expected: 8.940696716308594e-8,
37
- type: 'float32'
38
- },
39
- {
40
- data: 'fab3c00000',
41
- expected: -8.940696716308594e-8,
42
- type: 'float32'
43
- },
44
- {
45
- data: 'fb3ff199999999999a',
46
- expected: 1.1,
47
- type: 'float64'
48
- },
49
- {
50
- data: 'fbbff199999999999a',
51
- expected: -1.1,
52
- type: 'float64'
53
- },
54
- {
55
- data: 'fb3ff1c71c71c71c72',
56
- expected: 1.1111111111111112,
57
- type: 'float64'
58
- },
59
- {
60
- data: 'fb0000000000000002',
61
- expected: 1e-323,
62
- type: 'float64'
63
- },
64
- {
65
- data: 'fb8000000000000002',
66
- expected: -1e-323,
67
- type: 'float64'
68
- },
69
- {
70
- data: 'fb3fefffffffffffff',
71
- expected: 0.9999999999999999,
72
- type: 'float64'
73
- },
74
- {
75
- data: 'fbbfefffffffffffff',
76
- expected: -0.9999999999999999,
77
- type: 'float64'
78
- },
79
- {
80
- data: 'f97c00',
81
- expected: Infinity,
82
- type: 'Infinity'
83
- },
84
- {
85
- data: 'fb7ff0000000000000',
86
- expected: Infinity,
87
- type: 'Infinity',
88
- strict: false
89
- },
90
- {
91
- data: 'f9fc00',
92
- expected: -Infinity,
93
- type: '-Infinity'
94
- },
95
- {
96
- data: 'fbfff0000000000000',
97
- expected: -Infinity,
98
- type: '-Infinity',
99
- strict: false
100
- },
101
- {
102
- data: 'f97e00',
103
- expected: NaN,
104
- type: 'NaN'
105
- },
106
- {
107
- data: 'f97ff8',
108
- expected: NaN,
109
- type: 'NaN',
110
- strict: false
111
- },
112
- {
113
- data: 'fa7ff80000',
114
- expected: NaN,
115
- type: 'NaN',
116
- strict: false
117
- },
118
- {
119
- data: 'fb7ff8000000000000',
120
- expected: NaN,
121
- type: 'NaN',
122
- strict: false
123
- },
124
- {
125
- data: 'fb7ff8cafedeadbeef',
126
- expected: NaN,
127
- type: 'NaN',
128
- strict: false
129
- },
130
- {
131
- data: 'fb40f4241a31a5a515',
132
- expected: 82497.63712086187,
133
- type: 'float64'
134
- }
135
- ];
136
- describe('float', () => {
137
- describe('decode', () => {
138
- for (const fixture of fixtures) {
139
- const data = fromHex(fixture.data);
140
- it(`should decode ${ fixture.type }=${ fixture.expected }`, () => {
141
- assert.deepStrictEqual(decode(data), fixture.expected, `decode ${ fixture.type }`);
142
- assert.deepStrictEqual(decode(data, { strict: true }), fixture.expected, `decode ${ fixture.type }`);
143
- });
144
- }
145
- });
146
- it('error', () => {
147
- assert.throws(() => decode(fromHex('f80000')), Error, 'simple values are not supported');
148
- assert.throws(() => decode(fromHex('f900')), Error, 'not enough data for float16');
149
- assert.throws(() => decode(fromHex('fa0000')), Error, 'not enough data for float32');
150
- assert.throws(() => decode(fromHex('fb00000000')), Error, 'not enough data for float64');
151
- });
152
- describe('encode', () => {
153
- for (const fixture of fixtures) {
154
- if (fixture.strict !== false) {
155
- it(`should encode ${ fixture.type }=${ fixture.expected }`, () => {
156
- assert.strictEqual(toHex(encode(fixture.expected)), fixture.data, `encode ${ fixture.type }`);
157
- });
158
- }
159
- }
160
- });
161
- describe('encode float64', () => {
162
- for (const fixture of fixtures) {
163
- if (fixture.type.startsWith('float')) {
164
- it(`should encode ${ fixture.type }=${ fixture.expected }`, () => {
165
- const encoded = encode(fixture.expected, { float64: true });
166
- assert.strictEqual(encoded.length, 9);
167
- assert.strictEqual(encoded[0], 251);
168
- assert.strictEqual(decode(encoded), fixture.expected, `encode float64 ${ fixture.type }`);
169
- });
170
- }
171
- }
172
- });
173
- describe('roundtrip', () => {
174
- for (const fixture of fixtures) {
175
- if (!fixture.unsafe && fixture.strict !== false) {
176
- it(`should roundtrip ${ fixture.type }=${ fixture.expected }`, () => {
177
- assert.deepStrictEqual(decode(encode(fixture.expected)), fixture.expected, `roundtrip ${ fixture.type }`);
178
- });
179
- }
180
- }
181
- });
182
- describe('specials', () => {
183
- it('indefinite length switch fails on BREAK', () => {
184
- assert.throws(() => decode(Uint8Array.from([
185
- 131,
186
- 1,
187
- 2,
188
- 255
189
- ])), /unexpected break to lengthed array/);
190
- assert.throws(() => decode(Uint8Array.from([
191
- 131,
192
- 1,
193
- 2,
194
- 255
195
- ]), { allowIndefinite: false }), /indefinite/);
196
- });
197
- it('can switch off undefined support', () => {
198
- assert.deepStrictEqual(decode(fromHex('f7')), undefined);
199
- assert.throws(() => decode(fromHex('f7'), { allowUndefined: false }), /undefined/);
200
- assert.deepStrictEqual(decode(fromHex('830102f7')), [
201
- 1,
202
- 2,
203
- undefined
204
- ]);
205
- assert.throws(() => decode(fromHex('830102f7'), { allowUndefined: false }), /undefined/);
206
- });
207
- it('can coerce undefined to null', () => {
208
- assert.deepStrictEqual(decode(fromHex('f7'), { coerceUndefinedToNull: false }), undefined);
209
- assert.deepStrictEqual(decode(fromHex('f7'), { coerceUndefinedToNull: true }), null);
210
- assert.deepStrictEqual(decode(fromHex('830102f7'), { coerceUndefinedToNull: false }), [
211
- 1,
212
- 2,
213
- undefined
214
- ]);
215
- assert.deepStrictEqual(decode(fromHex('830102f7'), { coerceUndefinedToNull: true }), [
216
- 1,
217
- 2,
218
- null
219
- ]);
220
- });
221
- it('can switch off Infinity support', () => {
222
- assert.deepStrictEqual(decode(fromHex('830102f97c00')), [
223
- 1,
224
- 2,
225
- Infinity
226
- ]);
227
- assert.deepStrictEqual(decode(fromHex('830102f9fc00')), [
228
- 1,
229
- 2,
230
- -Infinity
231
- ]);
232
- assert.throws(() => decode(fromHex('830102f97c00'), { allowInfinity: false }), /Infinity/);
233
- assert.throws(() => decode(fromHex('830102f9fc00'), { allowInfinity: false }), /Infinity/);
234
- for (const fixture of fixtures.filter(f => f.type.endsWith('Infinity'))) {
235
- assert.throws(() => decode(fromHex(fixture.data), { allowInfinity: false }), /Infinity/);
236
- }
237
- });
238
- it('can switch off NaN support', () => {
239
- assert.deepStrictEqual(decode(fromHex('830102f97e00')), [
240
- 1,
241
- 2,
242
- NaN
243
- ]);
244
- assert.throws(() => decode(fromHex('830102f97e00'), { allowNaN: false }), /NaN/);
245
- for (const fixture of fixtures.filter(f => f.type === 'NaN')) {
246
- assert.throws(() => decode(fromHex(fixture.data), { allowNaN: false }), /NaN/);
247
- }
248
- });
249
- });
250
- });
@@ -1,84 +0,0 @@
1
- import chai from 'chai';
2
- import { Bl } from '../lib/bl.js';
3
- const {assert} = chai;
4
- describe('Internal bytes list', () => {
5
- describe('push', () => {
6
- it('push bits', () => {
7
- const bl = new Bl(10);
8
- const expected = [];
9
- for (let i = 0; i < 25; i++) {
10
- bl.push([i + 1]);
11
- expected.push(i + 1);
12
- }
13
- assert.deepEqual([...bl.toBytes()], expected);
14
- });
15
- for (let i = 4; i < 21; i++) {
16
- it(`push Bl(${ i })`, () => {
17
- const bl = new Bl(i);
18
- const expected = [
19
- 1,
20
- 2,
21
- 3,
22
- 4,
23
- 5,
24
- 6,
25
- 7,
26
- 8,
27
- 9,
28
- 10,
29
- 100,
30
- 110,
31
- 120,
32
- 11,
33
- 12,
34
- 130,
35
- 13,
36
- 14,
37
- 15,
38
- 16,
39
- 17,
40
- 18,
41
- 19,
42
- 20,
43
- 21,
44
- 22,
45
- 23
46
- ];
47
- for (let i = 0; i < 5; i++) {
48
- bl.push([i + 1]);
49
- }
50
- bl.push(Uint8Array.from([
51
- 6,
52
- 7,
53
- 8,
54
- 9,
55
- 10
56
- ]));
57
- bl.push([100]);
58
- bl.push(Uint8Array.from([
59
- 110,
60
- 120
61
- ]));
62
- bl.push(Uint8Array.from([
63
- 11,
64
- 12
65
- ]));
66
- bl.push([130]);
67
- bl.push(Uint8Array.from([
68
- 13,
69
- 14,
70
- 15,
71
- 16,
72
- 17,
73
- 18,
74
- 19,
75
- 20,
76
- 21,
77
- 22,
78
- 23
79
- ]));
80
- assert.deepEqual([...bl.toBytes()], expected);
81
- });
82
- }
83
- });
84
- });
@@ -1,95 +0,0 @@
1
- import chai from 'chai';
2
- import {
3
- decode,
4
- encode
5
- } from '../cborg.js';
6
- import * as taglib from '../taglib.js';
7
- import {
8
- fromHex,
9
- toHex
10
- } from '../lib/byte-utils.js';
11
- import { fixtures } from './appendix_a.js';
12
- const {assert} = chai;
13
- const tags = [];
14
- const typeEncoders = {};
15
- tags[0] = function (obj) {
16
- if (typeof obj !== 'string') {
17
- throw new Error('expected string for tag 1');
18
- }
19
- return `0("${ new Date(obj).toISOString().replace(/\.000Z$/, 'Z') }")`;
20
- };
21
- tags[1] = function (obj) {
22
- if (typeof obj !== 'number') {
23
- throw new Error('expected number for tag 1');
24
- }
25
- return `1(${ obj })`;
26
- };
27
- tags[2] = taglib.bigIntDecoder;
28
- typeEncoders.bigint = taglib.bigIntEncoder;
29
- tags[3] = taglib.bigNegIntDecoder;
30
- tags[23] = function (obj) {
31
- if (!(obj instanceof Uint8Array)) {
32
- throw new Error('expected byte array for tag 23');
33
- }
34
- return `23(h'${ toHex(obj) }')`;
35
- };
36
- tags[24] = function (obj) {
37
- return tags[23](obj).replace(/^23/, '24');
38
- };
39
- tags[32] = function (obj) {
40
- if (typeof obj !== 'string') {
41
- throw new Error('expected string for tag 32');
42
- }
43
- ;
44
- (() => new URL(obj))();
45
- return `32("${ obj }")`;
46
- };
47
- describe('cbor/test-vectors', () => {
48
- let i = 0;
49
- for (const fixture of fixtures) {
50
- const u8a = fromHex(fixture.hex);
51
- let expected = fixture.decoded !== undefined ? fixture.decoded : fixture.diagnostic;
52
- if (typeof expected === 'string' && expected.startsWith('h\'')) {
53
- expected = fromHex(expected.replace(/(^h)'|('$)/g, ''));
54
- }
55
- it(`test vector #${ i }: ${ inspect(expected).replace(/\n\s*/g, '') }`, () => {
56
- if (fixture.error) {
57
- assert.throws(() => decode(u8a, { tags }), fixture.error);
58
- } else {
59
- if (fixture.noTagDecodeError) {
60
- assert.throws(() => decode(u8a), fixture.noTagDecodeError);
61
- }
62
- let actual = decode(u8a, { tags });
63
- if (typeof actual === 'bigint') {
64
- actual = inspect(actual);
65
- }
66
- if (typeof expected === 'bigint') {
67
- expected = inspect(expected);
68
- }
69
- assert.deepEqual(actual, expected);
70
- if (fixture.roundtrip) {
71
- if (fixture.noTagEncodeError) {
72
- assert.throws(() => encode(decode(u8a, { tags })), fixture.noTagEncodeError);
73
- }
74
- const reencoded = encode(decode(u8a, { tags }), { typeEncoders });
75
- assert.equal(toHex(reencoded), fixture.hex);
76
- }
77
- }
78
- });
79
- i++;
80
- }
81
- it.skip('encode w/ tags', () => {
82
- });
83
- });
84
- function inspect(o) {
85
- if (typeof o === 'string') {
86
- return `'${ o }'`;
87
- }
88
- if (o instanceof Uint8Array) {
89
- return `Uint8Array<${ o.join(',') }>`;
90
- }
91
- if (o == null || typeof o !== 'object') {
92
- return String(o);
93
- }
94
- return JSON.stringify(o);
95
- }
@@ -1,69 +0,0 @@
1
- import chai from 'chai';
2
- import { decode } from '../cborg.js';
3
- import { fromHex } from '../lib/byte-utils.js';
4
- const {assert} = chai;
5
- describe('decode errors', () => {
6
- it('not Uint8Array', () => {
7
- for (const arg of [
8
- true,
9
- false,
10
- null,
11
- undefined,
12
- 'string',
13
- { obj: 'ect' },
14
- {},
15
- ['array'],
16
- [],
17
- [
18
- 1,
19
- 2,
20
- 3
21
- ],
22
- 0,
23
- 100,
24
- 1.1,
25
- -1,
26
- Symbol.for('nope')
27
- ]) {
28
- assert.throws(() => decode(arg), /CBOR decode error.*must be a Uint8Array/);
29
- }
30
- });
31
- it('no data', () => {
32
- assert.throws(() => decode(new Uint8Array('')), /CBOR decode error.*content/);
33
- });
34
- it('break only', () => {
35
- assert.throws(() => decode(new Uint8Array([255])), /CBOR decode error.*break/);
36
- });
37
- it('not enough map entries (value)', () => {
38
- assert.throws(() => decode(fromHex('a2616f016174')), /map.*not enough entries.*value/);
39
- });
40
- it('not enough map entries (key)', () => {
41
- assert.throws(() => decode(fromHex('a2616f01')), /map.*not enough entries.*key/);
42
- });
43
- it('break in lengthed map', () => {
44
- assert.throws(() => decode(fromHex('a2616f01ff740f')), /unexpected break to lengthed map/);
45
- });
46
- it('not enough array entries', () => {
47
- assert.throws(() => decode(fromHex('82616f')), /array.*not enough entries/);
48
- });
49
- it('break in lengthed array', () => {
50
- assert.throws(() => decode(fromHex('82ff')), /unexpected break to lengthed array/);
51
- });
52
- it('no such decoder', () => {
53
- assert.throws(() => decode(fromHex('82ff')), /unexpected break to lengthed array/);
54
- });
55
- it('too many terminals', () => {
56
- assert.throws(() => decode(fromHex('0101')), /too many terminals/);
57
- });
58
- it('rejectDuplicateMapKeys enabled on duplicate keys', () => {
59
- assert.deepStrictEqual(decode(fromHex('a3636261720363666f6f0163666f6f02')), {
60
- foo: 2,
61
- bar: 3
62
- });
63
- assert.throws(() => decode(fromHex('a3636261720363666f6f0163666f6f02'), { rejectDuplicateMapKeys: true }), /CBOR decode error: found repeat map key "foo"/);
64
- assert.throws(() => decode(fromHex('a3636261720363666f6f0163666f6f02'), {
65
- useMaps: true,
66
- rejectDuplicateMapKeys: true
67
- }), /CBOR decode error: found repeat map key "foo"/);
68
- });
69
- });
@@ -1,50 +0,0 @@
1
- import { garbage } from 'ipld-garbage';
2
- import {
3
- decode,
4
- encode
5
- } from '../cborg.js';
6
- import chai from 'chai';
7
- const {assert} = chai;
8
- describe('Fuzz round-trip', () => {
9
- it('random objects', function () {
10
- this.timeout(5000);
11
- for (let i = 0; i < 1000; i++) {
12
- const obj = garbage(300, { weights: { CID: 0 } });
13
- const byts = encode(obj);
14
- const decoded = decode(byts);
15
- assert.deepEqual(decoded, obj);
16
- }
17
- });
18
- it('circular references error', () => {
19
- let obj = {};
20
- obj.obj = obj;
21
- assert.throws(() => encode(obj), /circular references/);
22
- obj = {
23
- blip: [
24
- 1,
25
- 2,
26
- { blop: {} }
27
- ]
28
- };
29
- obj.blip[2].blop.boop = obj;
30
- assert.throws(() => encode(obj), /circular references/);
31
- obj = {
32
- blip: [
33
- 1,
34
- 2,
35
- { blop: {} }
36
- ]
37
- };
38
- obj.blip[2].blop.boop = obj.blip;
39
- assert.throws(() => encode(obj), /circular references/);
40
- obj = {
41
- blip: {},
42
- bloop: {}
43
- };
44
- obj.bloop = obj.blip;
45
- assert.doesNotThrow(() => encode(obj));
46
- const arr = [];
47
- arr[0] = arr;
48
- assert.throws(() => encode(arr), /circular references/);
49
- });
50
- });