cbor-x 1.5.9 → 1.6.0
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/README.md +12 -0
- package/decode.d.ts +1 -1
- package/decode.js +49 -5
- package/dist/decode-no-eval.cjs +47 -5
- package/dist/decode-no-eval.cjs.map +1 -1
- package/dist/index-no-eval.cjs +61 -8
- package/dist/index-no-eval.cjs.map +1 -1
- package/dist/index-no-eval.min.js +1 -1
- package/dist/index-no-eval.min.js.map +1 -1
- package/dist/index.js +61 -8
- package/dist/index.js.map +1 -1
- package/dist/index.min.js +1 -1
- package/dist/index.min.js.map +1 -1
- package/dist/node.cjs +62 -8
- package/dist/node.cjs.map +1 -1
- package/dist/test.js +65 -14
- package/dist/test.js.map +1 -1
- package/encode.js +15 -3
- package/index.d.ts +6 -1
- package/index.js +1 -1
- package/node-index.js +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -228,6 +228,18 @@ Note, that the performance is decreased with decimal rounding by about 20-25%, a
|
|
|
228
228
|
|
|
229
229
|
In addition, msgpackr exports a `roundFloat32(number)` function that can be used to round floating point numbers to the maximum significant decimal digits that can be stored in 32-bit float, just as DECIMAL_ROUND does when decoding. This can be useful for determining how a number will be decoded prior to encoding it.
|
|
230
230
|
|
|
231
|
+
### Setting Size Limits
|
|
232
|
+
You can set size limits on objects, arrays, and maps to prevent resource exhaustion when decoding. This can be done by calling the setMaxLimits export. Each of the properties are optional (only provide
|
|
233
|
+
the properties you want to change), for example (with the defaults):
|
|
234
|
+
```JavaScript
|
|
235
|
+
import { setMaxLimits } from 'cbor-x';
|
|
236
|
+
setMaxLimits({
|
|
237
|
+
maxArraySize: 112810000,
|
|
238
|
+
maxMapSize: 16810000,
|
|
239
|
+
maxObjectSize : 16710000
|
|
240
|
+
});
|
|
241
|
+
```
|
|
242
|
+
|
|
231
243
|
## Performance
|
|
232
244
|
Cbor-x is fast. Really fast. Here is comparison with the next fastest JS projects using the benchmark tool from `msgpack-lite` (and the sample data is from some clinical research data we use that has a good mix of different value types and structures). It also includes comparison to V8 native JSON functionality, and JavaScript Avro (`avsc`, a very optimized Avro implementation):
|
|
233
245
|
|
package/decode.d.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
export { decode, decodeMultiple, Decoder, addExtension, clearSource,roundFloat32, isNativeAccelerationEnabled,
|
|
2
|
-
Extension, Options, FLOAT32_OPTIONS} from '.'
|
|
2
|
+
Extension, Options, FLOAT32_OPTIONS, setMaxLimits, MAX_LIMITS_OPTIONS } from '.'
|
package/decode.js
CHANGED
|
@@ -14,6 +14,13 @@ const BUNDLED_STRINGS_ID = 0xdff9
|
|
|
14
14
|
const PACKED_TABLE_TAG_ID = 51
|
|
15
15
|
const PACKED_REFERENCE_TAG_ID = 6
|
|
16
16
|
const STOP_CODE = {}
|
|
17
|
+
let maxArraySize = 112810000 // This is the maximum array size in V8. We would potentially detect and set it higher
|
|
18
|
+
// for JSC, but this is pretty large and should be sufficient for most use cases
|
|
19
|
+
let maxMapSize = 16810000 // JavaScript has a fixed maximum map size of about 16710000, but JS itself enforces this,
|
|
20
|
+
// so we don't need to
|
|
21
|
+
|
|
22
|
+
let maxObjectSize = 16710000; // This is the maximum number of keys in a Map. It takes over a minute to create this
|
|
23
|
+
// many keys in an object, so also probably a reasonable choice there.
|
|
17
24
|
let strings = EMPTY_ARRAY
|
|
18
25
|
let stringPosition = 0
|
|
19
26
|
let currentDecoder = {}
|
|
@@ -294,6 +301,7 @@ export function read() {
|
|
|
294
301
|
let array = []
|
|
295
302
|
let value, i = 0
|
|
296
303
|
while ((value = read()) != STOP_CODE) {
|
|
304
|
+
if (i >= maxArraySize) throw new Error(`Array length exceeds ${maxArraySize}`)
|
|
297
305
|
array[i++] = value
|
|
298
306
|
}
|
|
299
307
|
return majorType == 4 ? array : majorType == 3 ? array.join('') : Buffer.concat(array)
|
|
@@ -301,8 +309,19 @@ export function read() {
|
|
|
301
309
|
let key
|
|
302
310
|
if (currentDecoder.mapsAsObjects) {
|
|
303
311
|
let object = {}
|
|
304
|
-
|
|
305
|
-
|
|
312
|
+
let i = 0;
|
|
313
|
+
if (currentDecoder.keyMap) {
|
|
314
|
+
while((key = read()) != STOP_CODE) {
|
|
315
|
+
if (i++ >= maxMapSize) throw new Error(`Property count exceeds ${maxMapSize}`)
|
|
316
|
+
object[safeKey(currentDecoder.decodeKey(key))] = read()
|
|
317
|
+
}
|
|
318
|
+
}
|
|
319
|
+
else {
|
|
320
|
+
while ((key = read()) != STOP_CODE) {
|
|
321
|
+
if (i++ >= maxMapSize) throw new Error(`Property count exceeds ${maxMapSize}`)
|
|
322
|
+
object[safeKey(key)] = read()
|
|
323
|
+
}
|
|
324
|
+
}
|
|
306
325
|
return object
|
|
307
326
|
} else {
|
|
308
327
|
if (restoreMapsAsObject) {
|
|
@@ -310,8 +329,24 @@ export function read() {
|
|
|
310
329
|
restoreMapsAsObject = false
|
|
311
330
|
}
|
|
312
331
|
let map = new Map()
|
|
313
|
-
if (currentDecoder.keyMap)
|
|
314
|
-
|
|
332
|
+
if (currentDecoder.keyMap) {
|
|
333
|
+
let i = 0;
|
|
334
|
+
while((key = read()) != STOP_CODE) {
|
|
335
|
+
if (i++ >= maxMapSize) {
|
|
336
|
+
throw new Error(`Map size exceeds ${maxMapSize}`);
|
|
337
|
+
}
|
|
338
|
+
map.set(currentDecoder.decodeKey(key), read())
|
|
339
|
+
}
|
|
340
|
+
}
|
|
341
|
+
else {
|
|
342
|
+
let i = 0;
|
|
343
|
+
while ((key = read()) != STOP_CODE) {
|
|
344
|
+
if (i++ >= maxMapSize) {
|
|
345
|
+
throw new Error(`Map size exceeds ${maxMapSize}`);
|
|
346
|
+
}
|
|
347
|
+
map.set(key, read())
|
|
348
|
+
}
|
|
349
|
+
}
|
|
315
350
|
return map
|
|
316
351
|
}
|
|
317
352
|
case 7:
|
|
@@ -342,12 +377,14 @@ export function read() {
|
|
|
342
377
|
}
|
|
343
378
|
return readFixedString(token)
|
|
344
379
|
case 4: // array
|
|
380
|
+
if (token >= maxArraySize) throw new Error(`Array length exceeds ${maxArraySize}`)
|
|
345
381
|
let array = new Array(token)
|
|
346
382
|
//if (currentDecoder.keyMap) for (let i = 0; i < token; i++) array[i] = currentDecoder.decodeKey(read())
|
|
347
383
|
//else
|
|
348
384
|
for (let i = 0; i < token; i++) array[i] = read()
|
|
349
385
|
return array
|
|
350
386
|
case 5: // map
|
|
387
|
+
if (token >= maxMapSize) throw new Error(`Map size exceeds ${maxArraySize}`)
|
|
351
388
|
if (currentDecoder.mapsAsObjects) {
|
|
352
389
|
let object = {}
|
|
353
390
|
if (currentDecoder.keyMap) for (let i = 0; i < token; i++) object[safeKey(currentDecoder.decodeKey(read()))] = read()
|
|
@@ -449,6 +486,7 @@ export function read() {
|
|
|
449
486
|
}
|
|
450
487
|
const validName = /^[a-zA-Z_$][a-zA-Z\d_$]*$/
|
|
451
488
|
function createStructureReader(structure) {
|
|
489
|
+
if (!structure) throw new Error('Structure is required in record definition');
|
|
452
490
|
function readObject() {
|
|
453
491
|
// get the array size from the header
|
|
454
492
|
let length = src[position++]
|
|
@@ -886,7 +924,7 @@ currentExtensions[2] = (buffer) => {
|
|
|
886
924
|
// bigint extension
|
|
887
925
|
let value = BigInt(0)
|
|
888
926
|
for (let i = 0, l = buffer.byteLength; i < l; i++) {
|
|
889
|
-
value = BigInt(buffer[i]) + value << BigInt(8)
|
|
927
|
+
value = BigInt(buffer[i]) + (value << BigInt(8))
|
|
890
928
|
}
|
|
891
929
|
return value
|
|
892
930
|
}
|
|
@@ -1236,6 +1274,12 @@ export function addExtension(extension) {
|
|
|
1236
1274
|
currentExtensions[extension.tag] = extension.decode
|
|
1237
1275
|
}
|
|
1238
1276
|
|
|
1277
|
+
export function setSizeLimits(limits) {
|
|
1278
|
+
if (limits.maxMapSize) maxMapSize = limits.maxMapSize;
|
|
1279
|
+
if (limits.maxArraySize) maxArraySize = limits.maxArraySize;
|
|
1280
|
+
if (limits.maxObjectSize) maxObjectSize = limits.maxObjectSize;
|
|
1281
|
+
}
|
|
1282
|
+
|
|
1239
1283
|
export const mult10 = new Array(147) // this is a table matching binary exponents to the multiplier to determine significant digit rounding
|
|
1240
1284
|
for (let i = 0; i < 256; i++) {
|
|
1241
1285
|
mult10[i] = +('1e' + Math.floor(45.15 - i * 0.30103))
|
package/dist/decode-no-eval.cjs
CHANGED
|
@@ -18,6 +18,10 @@
|
|
|
18
18
|
const BUNDLED_STRINGS_ID = 0xdff9;
|
|
19
19
|
const PACKED_REFERENCE_TAG_ID = 6;
|
|
20
20
|
const STOP_CODE = {};
|
|
21
|
+
let maxArraySize = 112810000; // This is the maximum array size in V8. We would potentially detect and set it higher
|
|
22
|
+
// for JSC, but this is pretty large and should be sufficient for most use cases
|
|
23
|
+
let maxMapSize = 16810000; // JavaScript has a fixed maximum map size of about 16710000, but JS itself enforces this,
|
|
24
|
+
// many keys in an object, so also probably a reasonable choice there.
|
|
21
25
|
let strings = EMPTY_ARRAY;
|
|
22
26
|
let stringPosition = 0;
|
|
23
27
|
let currentDecoder = {};
|
|
@@ -298,6 +302,7 @@
|
|
|
298
302
|
let array = [];
|
|
299
303
|
let value, i = 0;
|
|
300
304
|
while ((value = read()) != STOP_CODE) {
|
|
305
|
+
if (i >= maxArraySize) throw new Error(`Array length exceeds ${maxArraySize}`)
|
|
301
306
|
array[i++] = value;
|
|
302
307
|
}
|
|
303
308
|
return majorType == 4 ? array : majorType == 3 ? array.join('') : Buffer.concat(array)
|
|
@@ -305,8 +310,19 @@
|
|
|
305
310
|
let key;
|
|
306
311
|
if (currentDecoder.mapsAsObjects) {
|
|
307
312
|
let object = {};
|
|
308
|
-
|
|
309
|
-
|
|
313
|
+
let i = 0;
|
|
314
|
+
if (currentDecoder.keyMap) {
|
|
315
|
+
while((key = read()) != STOP_CODE) {
|
|
316
|
+
if (i++ >= maxMapSize) throw new Error(`Property count exceeds ${maxMapSize}`)
|
|
317
|
+
object[safeKey(currentDecoder.decodeKey(key))] = read();
|
|
318
|
+
}
|
|
319
|
+
}
|
|
320
|
+
else {
|
|
321
|
+
while ((key = read()) != STOP_CODE) {
|
|
322
|
+
if (i++ >= maxMapSize) throw new Error(`Property count exceeds ${maxMapSize}`)
|
|
323
|
+
object[safeKey(key)] = read();
|
|
324
|
+
}
|
|
325
|
+
}
|
|
310
326
|
return object
|
|
311
327
|
} else {
|
|
312
328
|
if (restoreMapsAsObject) {
|
|
@@ -314,8 +330,24 @@
|
|
|
314
330
|
restoreMapsAsObject = false;
|
|
315
331
|
}
|
|
316
332
|
let map = new Map();
|
|
317
|
-
if (currentDecoder.keyMap)
|
|
318
|
-
|
|
333
|
+
if (currentDecoder.keyMap) {
|
|
334
|
+
let i = 0;
|
|
335
|
+
while((key = read()) != STOP_CODE) {
|
|
336
|
+
if (i++ >= maxMapSize) {
|
|
337
|
+
throw new Error(`Map size exceeds ${maxMapSize}`);
|
|
338
|
+
}
|
|
339
|
+
map.set(currentDecoder.decodeKey(key), read());
|
|
340
|
+
}
|
|
341
|
+
}
|
|
342
|
+
else {
|
|
343
|
+
let i = 0;
|
|
344
|
+
while ((key = read()) != STOP_CODE) {
|
|
345
|
+
if (i++ >= maxMapSize) {
|
|
346
|
+
throw new Error(`Map size exceeds ${maxMapSize}`);
|
|
347
|
+
}
|
|
348
|
+
map.set(key, read());
|
|
349
|
+
}
|
|
350
|
+
}
|
|
319
351
|
return map
|
|
320
352
|
}
|
|
321
353
|
case 7:
|
|
@@ -346,12 +378,14 @@
|
|
|
346
378
|
}
|
|
347
379
|
return readFixedString(token)
|
|
348
380
|
case 4: // array
|
|
381
|
+
if (token >= maxArraySize) throw new Error(`Array length exceeds ${maxArraySize}`)
|
|
349
382
|
let array = new Array(token);
|
|
350
383
|
//if (currentDecoder.keyMap) for (let i = 0; i < token; i++) array[i] = currentDecoder.decodeKey(read())
|
|
351
384
|
//else
|
|
352
385
|
for (let i = 0; i < token; i++) array[i] = read();
|
|
353
386
|
return array
|
|
354
387
|
case 5: // map
|
|
388
|
+
if (token >= maxMapSize) throw new Error(`Map size exceeds ${maxArraySize}`)
|
|
355
389
|
if (currentDecoder.mapsAsObjects) {
|
|
356
390
|
let object = {};
|
|
357
391
|
if (currentDecoder.keyMap) for (let i = 0; i < token; i++) object[safeKey(currentDecoder.decodeKey(read()))] = read();
|
|
@@ -453,6 +487,7 @@
|
|
|
453
487
|
}
|
|
454
488
|
const validName = /^[a-zA-Z_$][a-zA-Z\d_$]*$/;
|
|
455
489
|
function createStructureReader(structure) {
|
|
490
|
+
if (!structure) throw new Error('Structure is required in record definition');
|
|
456
491
|
function readObject() {
|
|
457
492
|
// get the array size from the header
|
|
458
493
|
let length = src[position++];
|
|
@@ -814,7 +849,7 @@
|
|
|
814
849
|
// bigint extension
|
|
815
850
|
let value = BigInt(0);
|
|
816
851
|
for (let i = 0, l = buffer.byteLength; i < l; i++) {
|
|
817
|
-
value = BigInt(buffer[i]) + value << BigInt(8);
|
|
852
|
+
value = BigInt(buffer[i]) + (value << BigInt(8));
|
|
818
853
|
}
|
|
819
854
|
return value
|
|
820
855
|
};
|
|
@@ -1164,6 +1199,12 @@
|
|
|
1164
1199
|
currentExtensions[extension.tag] = extension.decode;
|
|
1165
1200
|
}
|
|
1166
1201
|
|
|
1202
|
+
function setSizeLimits(limits) {
|
|
1203
|
+
if (limits.maxMapSize) maxMapSize = limits.maxMapSize;
|
|
1204
|
+
if (limits.maxArraySize) maxArraySize = limits.maxArraySize;
|
|
1205
|
+
if (limits.maxObjectSize) limits.maxObjectSize;
|
|
1206
|
+
}
|
|
1207
|
+
|
|
1167
1208
|
const mult10 = new Array(147); // this is a table matching binary exponents to the multiplier to determine significant digit rounding
|
|
1168
1209
|
for (let i = 0; i < 256; i++) {
|
|
1169
1210
|
mult10[i] = +('1e' + Math.floor(45.15 - i * 0.30103));
|
|
@@ -1196,6 +1237,7 @@
|
|
|
1196
1237
|
exports.read = read;
|
|
1197
1238
|
exports.roundFloat32 = roundFloat32;
|
|
1198
1239
|
exports.setExtractor = setExtractor;
|
|
1240
|
+
exports.setSizeLimits = setSizeLimits;
|
|
1199
1241
|
exports.typedArrays = typedArrays;
|
|
1200
1242
|
|
|
1201
1243
|
}));
|