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