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