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/dist/index.js CHANGED
@@ -17,6 +17,9 @@
17
17
  const BUNDLED_STRINGS_ID = 0xdff9;
18
18
  const PACKED_REFERENCE_TAG_ID = 6;
19
19
  const STOP_CODE = {};
20
+ let maxArraySize = 112810000; // This is the maximum array size in V8. We would potentially detect and set it higher
21
+ // for JSC, but this is pretty large and should be sufficient for most use cases
22
+ let maxMapSize = 16810000; // JavaScript has a fixed maximum map size of about 16710000, but JS itself enforces this,
20
23
  let currentDecoder = {};
21
24
  let currentStructures;
22
25
  let srcString;
@@ -289,6 +292,7 @@
289
292
  let array = [];
290
293
  let value, i = 0;
291
294
  while ((value = read()) != STOP_CODE) {
295
+ if (i >= maxArraySize) throw new Error(`Array length exceeds ${maxArraySize}`)
292
296
  array[i++] = value;
293
297
  }
294
298
  return majorType == 4 ? array : majorType == 3 ? array.join('') : Buffer.concat(array)
@@ -296,8 +300,19 @@
296
300
  let key;
297
301
  if (currentDecoder.mapsAsObjects) {
298
302
  let object = {};
299
- if (currentDecoder.keyMap) while((key = read()) != STOP_CODE) object[safeKey(currentDecoder.decodeKey(key))] = read();
300
- else while ((key = read()) != STOP_CODE) object[safeKey(key)] = read();
303
+ let i = 0;
304
+ if (currentDecoder.keyMap) {
305
+ while((key = read()) != STOP_CODE) {
306
+ if (i++ >= maxMapSize) throw new Error(`Property count exceeds ${maxMapSize}`)
307
+ object[safeKey(currentDecoder.decodeKey(key))] = read();
308
+ }
309
+ }
310
+ else {
311
+ while ((key = read()) != STOP_CODE) {
312
+ if (i++ >= maxMapSize) throw new Error(`Property count exceeds ${maxMapSize}`)
313
+ object[safeKey(key)] = read();
314
+ }
315
+ }
301
316
  return object
302
317
  } else {
303
318
  if (restoreMapsAsObject) {
@@ -305,8 +320,24 @@
305
320
  restoreMapsAsObject = false;
306
321
  }
307
322
  let map = new Map();
308
- if (currentDecoder.keyMap) while((key = read()) != STOP_CODE) map.set(currentDecoder.decodeKey(key), read());
309
- else while ((key = read()) != STOP_CODE) map.set(key, read());
323
+ if (currentDecoder.keyMap) {
324
+ let i = 0;
325
+ while((key = read()) != STOP_CODE) {
326
+ if (i++ >= maxMapSize) {
327
+ throw new Error(`Map size exceeds ${maxMapSize}`);
328
+ }
329
+ map.set(currentDecoder.decodeKey(key), read());
330
+ }
331
+ }
332
+ else {
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(key, read());
339
+ }
340
+ }
310
341
  return map
311
342
  }
312
343
  case 7:
@@ -337,12 +368,14 @@
337
368
  }
338
369
  return readFixedString(token)
339
370
  case 4: // array
371
+ if (token >= maxArraySize) throw new Error(`Array length exceeds ${maxArraySize}`)
340
372
  let array = new Array(token);
341
373
  //if (currentDecoder.keyMap) for (let i = 0; i < token; i++) array[i] = currentDecoder.decodeKey(read())
342
374
  //else
343
375
  for (let i = 0; i < token; i++) array[i] = read();
344
376
  return array
345
377
  case 5: // map
378
+ if (token >= maxMapSize) throw new Error(`Map size exceeds ${maxArraySize}`)
346
379
  if (currentDecoder.mapsAsObjects) {
347
380
  let object = {};
348
381
  if (currentDecoder.keyMap) for (let i = 0; i < token; i++) object[safeKey(currentDecoder.decodeKey(read()))] = read();
@@ -444,6 +477,7 @@
444
477
  }
445
478
  const validName = /^[a-zA-Z_$][a-zA-Z\d_$]*$/;
446
479
  function createStructureReader(structure) {
480
+ if (!structure) throw new Error('Structure is required in record definition');
447
481
  function readObject() {
448
482
  // get the array size from the header
449
483
  let length = src[position$1++];
@@ -770,7 +804,7 @@
770
804
  // bigint extension
771
805
  let value = BigInt(0);
772
806
  for (let i = 0, l = buffer.byteLength; i < l; i++) {
773
- value = BigInt(buffer[i]) + value << BigInt(8);
807
+ value = BigInt(buffer[i]) + (value << BigInt(8));
774
808
  }
775
809
  return value
776
810
  };
@@ -1116,6 +1150,12 @@
1116
1150
  currentExtensions[extension.tag] = extension.decode;
1117
1151
  }
1118
1152
 
1153
+ function setSizeLimits(limits) {
1154
+ if (limits.maxMapSize) maxMapSize = limits.maxMapSize;
1155
+ if (limits.maxArraySize) maxArraySize = limits.maxArraySize;
1156
+ if (limits.maxObjectSize) limits.maxObjectSize;
1157
+ }
1158
+
1119
1159
  const mult10 = new Array(147); // this is a table matching binary exponents to the multiplier to determine significant digit rounding
1120
1160
  for (let i = 0; i < 256; i++) {
1121
1161
  mult10[i] = +('1e' + Math.floor(45.15 - i * 0.30103));
@@ -1719,7 +1759,19 @@
1719
1759
  target[position++] = 0xfb;
1720
1760
  targetView.setFloat64(position, Number(value));
1721
1761
  } else {
1722
- throw new RangeError(value + ' was too large to fit in CBOR 64-bit integer format, set largeBigIntToFloat to convert to float-64')
1762
+ if (value >= BigInt(0))
1763
+ target[position++] = 0xc2; // tag 2
1764
+ else {
1765
+ target[position++] = 0xc3; // tag 2
1766
+ value = BigInt(-1) - value;
1767
+ }
1768
+ let bytes = [];
1769
+ while (value) {
1770
+ bytes.push(Number(value & BigInt(0xff)));
1771
+ value >>= BigInt(8);
1772
+ }
1773
+ writeBuffer(new Uint8Array(bytes.reverse()), makeRoom);
1774
+ return;
1723
1775
  }
1724
1776
  }
1725
1777
  position += 8;
@@ -1766,7 +1818,7 @@
1766
1818
  let objectOffset = position - start;
1767
1819
  position += 2;
1768
1820
  let size = 0;
1769
- if (encoder.keyMap) {
1821
+ if (encoder.keyMap) {
1770
1822
  for (let key in object) if (typeof object.hasOwnProperty !== 'function' || object.hasOwnProperty(key)) {
1771
1823
  encode(encoder.encodeKey(key));
1772
1824
  encode(object[key]);
@@ -1938,7 +1990,7 @@
1938
1990
  yield* tryEncode(value, iterateProperties, 'element');
1939
1991
  } else encode(value);
1940
1992
  }
1941
- } else if (object[Symbol.iterator]) {
1993
+ } else if (object[Symbol.iterator] && !object.buffer) { // iterator, but exclude typed arrays
1942
1994
  target[position++] = 0x9f; // start indefinite array
1943
1995
  for (let value of object) {
1944
1996
  if (value && (typeof value === 'object' || position - start > chunkThreshold)) {
@@ -2450,6 +2502,7 @@
2450
2502
  exports.encodeIter = encodeIter;
2451
2503
  exports.isNativeAccelerationEnabled = isNativeAccelerationEnabled;
2452
2504
  exports.roundFloat32 = roundFloat32;
2505
+ exports.setSizeLimits = setSizeLimits;
2453
2506
 
2454
2507
  }));
2455
2508
  //# sourceMappingURL=index.js.map