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/node.cjs CHANGED
@@ -17,6 +17,10 @@ const RECORD_INLINE_ID = 0xdfff; // temporary first-come first-serve tag // prop
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,
23
+ // many keys in an object, so also probably a reasonable choice there.
20
24
  let strings = EMPTY_ARRAY;
21
25
  let stringPosition = 0;
22
26
  let currentDecoder = {};
@@ -293,6 +297,7 @@ function read() {
293
297
  let array = [];
294
298
  let value, i = 0;
295
299
  while ((value = read()) != STOP_CODE) {
300
+ if (i >= maxArraySize) throw new Error(`Array length exceeds ${maxArraySize}`)
296
301
  array[i++] = value;
297
302
  }
298
303
  return majorType == 4 ? array : majorType == 3 ? array.join('') : Buffer.concat(array)
@@ -300,8 +305,19 @@ function read() {
300
305
  let key;
301
306
  if (currentDecoder.mapsAsObjects) {
302
307
  let object = {};
303
- if (currentDecoder.keyMap) while((key = read()) != STOP_CODE) object[safeKey(currentDecoder.decodeKey(key))] = read();
304
- else while ((key = read()) != STOP_CODE) object[safeKey(key)] = read();
308
+ let i = 0;
309
+ if (currentDecoder.keyMap) {
310
+ while((key = read()) != STOP_CODE) {
311
+ if (i++ >= maxMapSize) throw new Error(`Property count exceeds ${maxMapSize}`)
312
+ object[safeKey(currentDecoder.decodeKey(key))] = read();
313
+ }
314
+ }
315
+ else {
316
+ while ((key = read()) != STOP_CODE) {
317
+ if (i++ >= maxMapSize) throw new Error(`Property count exceeds ${maxMapSize}`)
318
+ object[safeKey(key)] = read();
319
+ }
320
+ }
305
321
  return object
306
322
  } else {
307
323
  if (restoreMapsAsObject) {
@@ -309,8 +325,24 @@ function read() {
309
325
  restoreMapsAsObject = false;
310
326
  }
311
327
  let map = new Map();
312
- if (currentDecoder.keyMap) while((key = read()) != STOP_CODE) map.set(currentDecoder.decodeKey(key), read());
313
- else while ((key = read()) != STOP_CODE) map.set(key, read());
328
+ if (currentDecoder.keyMap) {
329
+ let i = 0;
330
+ while((key = read()) != STOP_CODE) {
331
+ if (i++ >= maxMapSize) {
332
+ throw new Error(`Map size exceeds ${maxMapSize}`);
333
+ }
334
+ map.set(currentDecoder.decodeKey(key), read());
335
+ }
336
+ }
337
+ else {
338
+ let i = 0;
339
+ while ((key = read()) != STOP_CODE) {
340
+ if (i++ >= maxMapSize) {
341
+ throw new Error(`Map size exceeds ${maxMapSize}`);
342
+ }
343
+ map.set(key, read());
344
+ }
345
+ }
314
346
  return map
315
347
  }
316
348
  case 7:
@@ -341,12 +373,14 @@ function read() {
341
373
  }
342
374
  return readFixedString(token)
343
375
  case 4: // array
376
+ if (token >= maxArraySize) throw new Error(`Array length exceeds ${maxArraySize}`)
344
377
  let array = new Array(token);
345
378
  //if (currentDecoder.keyMap) for (let i = 0; i < token; i++) array[i] = currentDecoder.decodeKey(read())
346
379
  //else
347
380
  for (let i = 0; i < token; i++) array[i] = read();
348
381
  return array
349
382
  case 5: // map
383
+ if (token >= maxMapSize) throw new Error(`Map size exceeds ${maxArraySize}`)
350
384
  if (currentDecoder.mapsAsObjects) {
351
385
  let object = {};
352
386
  if (currentDecoder.keyMap) for (let i = 0; i < token; i++) object[safeKey(currentDecoder.decodeKey(read()))] = read();
@@ -448,6 +482,7 @@ function read() {
448
482
  }
449
483
  const validName = /^[a-zA-Z_$][a-zA-Z\d_$]*$/;
450
484
  function createStructureReader(structure) {
485
+ if (!structure) throw new Error('Structure is required in record definition');
451
486
  function readObject() {
452
487
  // get the array size from the header
453
488
  let length = src[position$1++];
@@ -809,7 +844,7 @@ currentExtensions[2] = (buffer) => {
809
844
  // bigint extension
810
845
  let value = BigInt(0);
811
846
  for (let i = 0, l = buffer.byteLength; i < l; i++) {
812
- value = BigInt(buffer[i]) + value << BigInt(8);
847
+ value = BigInt(buffer[i]) + (value << BigInt(8));
813
848
  }
814
849
  return value
815
850
  };
@@ -1159,6 +1194,12 @@ function addExtension$1(extension) {
1159
1194
  currentExtensions[extension.tag] = extension.decode;
1160
1195
  }
1161
1196
 
1197
+ function setSizeLimits(limits) {
1198
+ if (limits.maxMapSize) maxMapSize = limits.maxMapSize;
1199
+ if (limits.maxArraySize) maxArraySize = limits.maxArraySize;
1200
+ if (limits.maxObjectSize) limits.maxObjectSize;
1201
+ }
1202
+
1162
1203
  const mult10 = new Array(147); // this is a table matching binary exponents to the multiplier to determine significant digit rounding
1163
1204
  for (let i = 0; i < 256; i++) {
1164
1205
  mult10[i] = +('1e' + Math.floor(45.15 - i * 0.30103));
@@ -1762,7 +1803,19 @@ class Encoder extends Decoder {
1762
1803
  target[position++] = 0xfb;
1763
1804
  targetView.setFloat64(position, Number(value));
1764
1805
  } else {
1765
- throw new RangeError(value + ' was too large to fit in CBOR 64-bit integer format, set largeBigIntToFloat to convert to float-64')
1806
+ if (value >= BigInt(0))
1807
+ target[position++] = 0xc2; // tag 2
1808
+ else {
1809
+ target[position++] = 0xc3; // tag 2
1810
+ value = BigInt(-1) - value;
1811
+ }
1812
+ let bytes = [];
1813
+ while (value) {
1814
+ bytes.push(Number(value & BigInt(0xff)));
1815
+ value >>= BigInt(8);
1816
+ }
1817
+ writeBuffer(new Uint8Array(bytes.reverse()), makeRoom);
1818
+ return;
1766
1819
  }
1767
1820
  }
1768
1821
  position += 8;
@@ -1809,7 +1862,7 @@ class Encoder extends Decoder {
1809
1862
  let objectOffset = position - start;
1810
1863
  position += 2;
1811
1864
  let size = 0;
1812
- if (encoder.keyMap) {
1865
+ if (encoder.keyMap) {
1813
1866
  for (let key in object) if (typeof object.hasOwnProperty !== 'function' || object.hasOwnProperty(key)) {
1814
1867
  encode(encoder.encodeKey(key));
1815
1868
  encode(object[key]);
@@ -1981,7 +2034,7 @@ class Encoder extends Decoder {
1981
2034
  yield* tryEncode(value, iterateProperties, 'element');
1982
2035
  } else encode(value);
1983
2036
  }
1984
- } else if (object[Symbol.iterator]) {
2037
+ } else if (object[Symbol.iterator] && !object.buffer) { // iterator, but exclude typed arrays
1985
2038
  target[position++] = 0x9f; // start indefinite array
1986
2039
  for (let value of object) {
1987
2040
  if (value && (typeof value === 'object' || position - start > chunkThreshold)) {
@@ -2571,5 +2624,6 @@ exports.encodeAsIterable = encodeAsIterable;
2571
2624
  exports.encodeIter = encodeIter;
2572
2625
  exports.mapsAsObjects = mapsAsObjects;
2573
2626
  exports.roundFloat32 = roundFloat32;
2627
+ exports.setSizeLimits = setSizeLimits;
2574
2628
  exports.useRecords = useRecords;
2575
2629
  //# sourceMappingURL=node.cjs.map