cborg 2.0.5 → 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,279 +0,0 @@
1
- import { assert } from 'chai';
2
- import {
3
- decode,
4
- encode
5
- } from '../lib/json/json.js';
6
- const toBytes = str => new TextEncoder().encode(str);
7
- function verifyRoundTrip(obj, sorting) {
8
- const encoded = new TextDecoder().decode(encode(obj, sorting === false ? { mapSorter: null } : undefined));
9
- const json = JSON.stringify(obj);
10
- assert.strictEqual(encoded, json);
11
- const decoded = decode(toBytes(JSON.stringify(obj)));
12
- assert.deepStrictEqual(decoded, obj);
13
- }
14
- function verifyEncodedForm(testCase) {
15
- const obj = JSON.parse(testCase);
16
- const encoded = encode(obj);
17
- assert.strictEqual(new TextDecoder().decode(encoded), JSON.stringify(obj));
18
- const decoded = decode(encoded);
19
- assert.deepStrictEqual(decoded, obj);
20
- const decoded2 = decode(toBytes(testCase));
21
- assert.deepStrictEqual(decoded2, obj);
22
- }
23
- describe('json basics', () => {
24
- it('can round-trip basic literals', () => {
25
- const testCases = [
26
- 'null',
27
- 'true',
28
- 'false',
29
- '0',
30
- '9007199254740991',
31
- '-9007199254740991',
32
- JSON.stringify(Number.MAX_VALUE),
33
- JSON.stringify(Number.MIN_VALUE)
34
- ];
35
- for (const testCase of testCases) {
36
- verifyEncodedForm(testCase);
37
- }
38
- assert.strictEqual(decode(toBytes('1E1')), 10);
39
- assert.strictEqual(decode(toBytes('0.1e1')), 1);
40
- assert.strictEqual(decode(toBytes('1e-1')), 0.1);
41
- assert.strictEqual(decode(toBytes('1e+00')), 1);
42
- assert.strictEqual(decode(toBytes('10.0')), 10);
43
- assert.deepStrictEqual(decode(toBytes('[-10.0,1.0,0.0,100.0]')), [
44
- -10,
45
- 1,
46
- 0,
47
- 100
48
- ]);
49
- verifyRoundTrip(true);
50
- verifyRoundTrip(false);
51
- verifyRoundTrip(null);
52
- verifyRoundTrip(100);
53
- verifyRoundTrip(-100);
54
- verifyRoundTrip(1.11);
55
- verifyRoundTrip(-100.11111);
56
- verifyRoundTrip(11100000000);
57
- verifyRoundTrip(1.0011111e-18);
58
- });
59
- it('handles large integers as BigInt', () => {
60
- const verify = (inp, str) => {
61
- if (str === undefined) {
62
- str = String(inp);
63
- }
64
- assert.strictEqual(decode(toBytes(str), { allowBigInt: true }), inp);
65
- assert.strictEqual(decode(toBytes(str)), parseFloat(str));
66
- };
67
- verify(Number.MAX_SAFE_INTEGER);
68
- verify(-Number.MAX_SAFE_INTEGER);
69
- verify(BigInt('9007199254740992'));
70
- verify(BigInt('9007199254740993'));
71
- verify(BigInt('11959030306112471731'));
72
- verify(BigInt('18446744073709551615'));
73
- verify(BigInt('9223372036854775807'));
74
- verify(BigInt('-9007199254740992'));
75
- verify(BigInt('-9007199254740993'));
76
- verify(BigInt('-9223372036854776000'));
77
- verify(BigInt('-11959030306112471732'));
78
- verify(BigInt('-18446744073709551616'));
79
- verify(-9007199254740992, '-9007199254740992.0');
80
- verify(-9223372036854776000, '-9223372036854776000.0');
81
- verify(-18446744073709552000, '-18446744073709551616.0');
82
- });
83
- it('can round-trip string literals', () => {
84
- const testCases = [
85
- JSON.stringify(''),
86
- JSON.stringify(' '),
87
- JSON.stringify('"'),
88
- JSON.stringify('\\'),
89
- JSON.stringify('\b\f\n\r\t'),
90
- JSON.stringify('"'),
91
- JSON.stringify('" %22 0x22 034 "'),
92
- '"\uD83D\uDE00"'
93
- ];
94
- for (const testCase of testCases) {
95
- verifyEncodedForm(testCase);
96
- }
97
- assert.strictEqual(decode(toBytes('"/ & \\/"')), '/ & /');
98
- verifyRoundTrip('this is a string');
99
- verifyRoundTrip('this \uD834\uDD1E is a \u263A\u263A \u2663 string ̐ ̀\n\r');
100
- verifyRoundTrip('');
101
- verifyRoundTrip('foo\\bar\nbaz\tbop\rbing"bip\'bang');
102
- });
103
- it('can round-trip array literals', () => {
104
- const testCases = [
105
- '[]',
106
- '[null]',
107
- '[true, false]',
108
- '[ \n 0,1, 2\n , 3,\n4] \n ',
109
- '[-10.0, 1.0, 0.0, 100.0]',
110
- '[["2 deep"]]'
111
- ];
112
- for (const testCase of testCases) {
113
- verifyEncodedForm(testCase);
114
- }
115
- verifyRoundTrip([
116
- 1,
117
- 2,
118
- 3,
119
- 'string',
120
- true,
121
- 4
122
- ]);
123
- verifyRoundTrip([
124
- 1,
125
- 2,
126
- 3,
127
- 'string',
128
- true,
129
- [
130
- 'and',
131
- 'a',
132
- 'nested',
133
- 'array',
134
- true
135
- ],
136
- 4
137
- ]);
138
- });
139
- it('can round-trip object literals', () => {
140
- const testCases = [
141
- '{}',
142
- '\n {\n "\\b"\n :\n""\n }\n ',
143
- '{"":""}',
144
- '{"1":{"2":0,"3":"deep"}}'
145
- ];
146
- for (const testCase of testCases) {
147
- verifyEncodedForm(testCase);
148
- }
149
- });
150
- it('will sort map keys', () => {
151
- const unsorted = {
152
- one: 1,
153
- two: 2,
154
- three: 3.1,
155
- str: 'string',
156
- bool: true,
157
- four: 4
158
- };
159
- verifyRoundTrip(unsorted, false);
160
- assert.strictEqual(new TextDecoder().decode(encode(unsorted)), '{"bool":true,"four":4,"one":1,"str":"string","three":3.1,"two":2}');
161
- });
162
- it('can handle novel cases', () => {
163
- assert.strictEqual(decode(toBytes('"this \\uD834\\uDD1E is a \\u263a\\u263a string"')), 'this \uD834\uDD1E is a \u263A\u263A string');
164
- verifyRoundTrip({
165
- one: 1,
166
- two: 2,
167
- three: 3.1,
168
- str: 'string',
169
- arr: [
170
- 'and',
171
- 'a',
172
- 'nested',
173
- [],
174
- 'array',
175
- [
176
- true,
177
- 1
178
- ],
179
- false
180
- ],
181
- bool: true,
182
- obj: {
183
- nested: 'object',
184
- a: [],
185
- o: {}
186
- },
187
- four: 4
188
- }, false);
189
- verifyRoundTrip([
190
- false,
191
- [
192
- {
193
- '#nFzU': {},
194
- '\\w>': -0.9441451951197325,
195
- '\t\'': '\'JB+2Wg\tw"IrM*#e^L/d&4rrzUuwq(1mH6aVRredB&Bfs]S"KqK(Tz1Q"URBAfw',
196
- '\n@FrfM': 'M[D]q&'
197
- },
198
- 'J4>\'Xdc+u2$%',
199
- 4227406737130333
200
- ]
201
- ], false);
202
- verifyRoundTrip([
203
- 0.12995619865708727,
204
- -4973404279772543,
205
- {
206
- drG2: [true],
207
- ';#K^Qf>V': null,
208
- '`2=': 'ecc<e/$+-.;U>Gr5RdZDJ\n5+:{=QHNN.tVVN~dX$FWFwu`6>"&=tW!*1*^\u263A)JFM1p|}&X.B|${*\\f@!w2\u263A+'
209
- }
210
- ], false);
211
- assert.strictEqual(`${ decode(encode(9007199254740991)) }`, '9007199254740991');
212
- assert.strictEqual(`${ decode(encode(9007199254740992)) }`, '9007199254740992');
213
- assert.strictEqual(`${ decode(encode(900719925474099100n)) }`, '900719925474099100');
214
- });
215
- it('should throw on bad types', () => {
216
- assert.throws(() => encode(new Uint8Array([
217
- 1,
218
- 2
219
- ])), /CBOR encode error: unsupported type: Uint8Array/);
220
- assert.throws(() => encode({
221
- boop: new Uint8Array([
222
- 1,
223
- 2
224
- ])
225
- }), /CBOR encode error: unsupported type: Uint8Array/);
226
- assert.throws(() => encode(undefined), /CBOR encode error: unsupported type: undefined/);
227
- assert.throws(() => encode(new Map([
228
- [
229
- 1,
230
- 2
231
- ],
232
- [
233
- 2,
234
- 3
235
- ]
236
- ])), /CBOR encode error: non-string map keys are not supported/);
237
- assert.throws(() => encode(new Map([
238
- [
239
- [
240
- 'foo',
241
- 'bar'
242
- ],
243
- 2
244
- ],
245
- [
246
- [
247
- 'bar',
248
- 'foo'
249
- ],
250
- 3
251
- ]
252
- ])), /CBOR encode error: complex map keys are not supported/);
253
- });
254
- it('should throw on bad decode failure modes', () => {
255
- assert.throws(() => decode(toBytes('{"a":1 & "b":2}')), 'CBOR decode error: unexpected character at position 7, was expecting object delimiter but found \'&\'');
256
- assert.throws(() => decode(toBytes('{"a":1,"b"!2}')), 'CBOR decode error: unexpected character at position 10, was expecting key/value delimiter \':\' but found \'!\'');
257
- assert.throws(() => decode(toBytes('[1,2&3]')), 'CBOR decode error: unexpected character at position 4, was expecting array delimiter but found \'&\'');
258
- assert.throws(() => decode(toBytes('{"a":!}')), 'CBOR decode error: unexpected character at position 5');
259
- assert.throws(() => decode(toBytes('"abc')), 'CBOR decode error: unexpected end of string at position 4');
260
- assert.throws(() => decode(toBytes('"ab\\xc"')), 'CBOR decode error: unexpected string escape character at position 5');
261
- assert.throws(() => decode(toBytes('"ab\x1Ec"')), 'CBOR decode error: invalid control character at position 3');
262
- assert.throws(() => decode(toBytes('"ab\\')), 'CBOR decode error: unexpected string termination at position 4');
263
- assert.throws(() => decode(toBytes('"\u263A').subarray(0, 3)), 'CBOR decode error: unexpected unicode sequence at position 1');
264
- assert.throws(() => decode(toBytes('"\\uxyza"')), 'CBOR decode error: unexpected unicode escape character at position 3');
265
- assert.throws(() => decode(toBytes('"\\u11"')), 'CBOR decode error: unexpected end of unicode escape sequence at position 3');
266
- assert.throws(() => decode(toBytes('-boop')), 'CBOR decode error: unexpected token at position 1');
267
- assert.throws(() => decode(toBytes('{"v":nope}')), 'CBOR decode error: unexpected token at position 7, expected to find \'null\'');
268
- assert.throws(() => decode(toBytes('[n]')), 'CBOR decode error: unexpected end of input at position 1');
269
- assert.throws(() => decode(toBytes('{"v":truu}')), 'CBOR decode error: unexpected token at position 9, expected to find \'true\'');
270
- assert.throws(() => decode(toBytes('[tr]')), 'CBOR decode error: unexpected end of input at position 1');
271
- assert.throws(() => decode(toBytes('{"v":flase}')), 'CBOR decode error: unexpected token at position 7, expected to find \'false\'');
272
- assert.throws(() => decode(toBytes('[fa]')), 'CBOR decode error: unexpected end of input at position 1');
273
- assert.throws(() => decode(toBytes('-0..1')), 'CBOR decode error: unexpected token at position 3');
274
- });
275
- it('should throw when rejectDuplicateMapKeys enabled on duplicate keys', () => {
276
- assert.deepStrictEqual(decode(toBytes('{"foo":1,"foo":2}')), { foo: 2 });
277
- assert.throws(() => decode(toBytes('{"foo":1,"foo":2}'), { rejectDuplicateMapKeys: true }), /CBOR decode error: found repeat map key "foo"/);
278
- });
279
- });
@@ -1,55 +0,0 @@
1
- import chai from 'chai';
2
- import { garbage } from 'ipld-garbage';
3
- import { uintBoundaries } from '../lib/0uint.js';
4
- import { encode } from '../cborg.js';
5
- import { encodedLength } from '../lib/length.js';
6
- import { dateEncoder } from './common.js';
7
- const {assert} = chai;
8
- function verifyLength(object, options) {
9
- const len = encodedLength(object, options);
10
- const encoded = encode(object, options);
11
- const actual = encoded.length;
12
- assert.strictEqual(actual, len, JSON.stringify(object));
13
- }
14
- describe('encodedLength', () => {
15
- it('int boundaries', () => {
16
- for (let ii = 0; ii < 4; ii++) {
17
- verifyLength(uintBoundaries[ii]);
18
- verifyLength(uintBoundaries[ii] - 1);
19
- verifyLength(uintBoundaries[ii] + 1);
20
- verifyLength(-1 * uintBoundaries[ii]);
21
- verifyLength(-1 * uintBoundaries[ii] - 1);
22
- verifyLength(-1 * uintBoundaries[ii] + 1);
23
- }
24
- });
25
- it('tags', () => {
26
- verifyLength({ date: new Date('2013-03-21T20:04:00Z') }, { typeEncoders: { Date: dateEncoder } });
27
- });
28
- it('floats', () => {
29
- verifyLength(0.5);
30
- verifyLength(0.5, { float64: true });
31
- verifyLength(8.940696716308594e-8);
32
- verifyLength(8.940696716308594e-8, { float64: true });
33
- });
34
- it('small garbage', function () {
35
- this.timeout(10000);
36
- for (let ii = 0; ii < 1000; ii++) {
37
- const gbg = garbage(1 << 6, { weights: { CID: 0 } });
38
- verifyLength(gbg);
39
- }
40
- });
41
- it('medium garbage', function () {
42
- this.timeout(10000);
43
- for (let ii = 0; ii < 100; ii++) {
44
- const gbg = garbage(1 << 16, { weights: { CID: 0 } });
45
- verifyLength(gbg);
46
- }
47
- });
48
- it('large garbage', function () {
49
- this.timeout(10000);
50
- for (let ii = 0; ii < 10; ii++) {
51
- const gbg = garbage(1 << 20, { weights: { CID: 0 } });
52
- verifyLength(gbg);
53
- }
54
- });
55
- });
package/esm/package.json DELETED
@@ -1,4 +0,0 @@
1
- {
2
- "type": "module",
3
- "browser": {}
4
- }
package/esm/taglib.js DELETED
@@ -1,37 +0,0 @@
1
- import {
2
- Token,
3
- Type
4
- } from './cborg.js';
5
- const neg1b = BigInt(-1);
6
- const pos1b = BigInt(1);
7
- const zerob = BigInt(0);
8
- const eightb = BigInt(8);
9
- export function bigIntDecoder(bytes) {
10
- let bi = zerob;
11
- for (let ii = 0; ii < bytes.length; ii++) {
12
- bi = (bi << eightb) + BigInt(bytes[ii]);
13
- }
14
- return bi;
15
- }
16
- function fromBigInt(bi) {
17
- const buf = [];
18
- while (bi > 0) {
19
- buf.unshift(Number(bi) & 255);
20
- bi >>= eightb;
21
- }
22
- return Uint8Array.from(buf);
23
- }
24
- const maxSafeBigInt = BigInt('18446744073709551615');
25
- const minSafeBigInt = BigInt('-18446744073709551616');
26
- export function bigIntEncoder(obj) {
27
- if (obj >= minSafeBigInt && obj <= maxSafeBigInt) {
28
- return null;
29
- }
30
- return [
31
- new Token(Type.tag, obj >= zerob ? 2 : 3),
32
- new Token(Type.bytes, fromBigInt(obj >= zerob ? obj : obj * neg1b - pos1b))
33
- ];
34
- }
35
- export function bigNegIntDecoder(bytes) {
36
- return neg1b - bigIntDecoder(bytes);
37
- }
package/index.js DELETED
@@ -1 +0,0 @@
1
- module.exports = require('./cjs/cborg.js')
package/json DELETED
@@ -1 +0,0 @@
1
- module.exports = require('./cjs/lib/json/json.js')
package/length DELETED
@@ -1 +0,0 @@
1
- module.exports = require('./cjs/lib/length.js')
package/taglib DELETED
@@ -1 +0,0 @@
1
- module.exports = require('./cjs/taglib.js')