cbor-x 1.5.9 → 1.6.3

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.
@@ -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;
@@ -262,6 +265,7 @@
262
265
  }
263
266
  token = dataView.getUint32(position$1);
264
267
  position$1 += 4;
268
+ if (majorType === 1) return -1 - token; // can't safely use negation operator here
265
269
  break
266
270
  case 0x1b:
267
271
  if (majorType == 7) {
@@ -276,8 +280,7 @@
276
280
  } else if (currentDecoder.int64AsNumber) {
277
281
  token = dataView.getUint32(position$1) * 0x100000000;
278
282
  token += dataView.getUint32(position$1 + 4);
279
- } else
280
- token = dataView.getBigUint64(position$1);
283
+ } else token = dataView.getBigUint64(position$1);
281
284
  position$1 += 8;
282
285
  break
283
286
  case 0x1f:
@@ -290,6 +293,7 @@
290
293
  let array = [];
291
294
  let value, i = 0;
292
295
  while ((value = read()) != STOP_CODE) {
296
+ if (i >= maxArraySize) throw new Error(`Array length exceeds ${maxArraySize}`)
293
297
  array[i++] = value;
294
298
  }
295
299
  return majorType == 4 ? array : majorType == 3 ? array.join('') : Buffer.concat(array)
@@ -297,8 +301,19 @@
297
301
  let key;
298
302
  if (currentDecoder.mapsAsObjects) {
299
303
  let object = {};
300
- if (currentDecoder.keyMap) while((key = read()) != STOP_CODE) object[safeKey(currentDecoder.decodeKey(key))] = read();
301
- else while ((key = read()) != STOP_CODE) object[safeKey(key)] = read();
304
+ let i = 0;
305
+ if (currentDecoder.keyMap) {
306
+ while((key = read()) != STOP_CODE) {
307
+ if (i++ >= maxMapSize) throw new Error(`Property count exceeds ${maxMapSize}`)
308
+ object[safeKey(currentDecoder.decodeKey(key))] = read();
309
+ }
310
+ }
311
+ else {
312
+ while ((key = read()) != STOP_CODE) {
313
+ if (i++ >= maxMapSize) throw new Error(`Property count exceeds ${maxMapSize}`)
314
+ object[safeKey(key)] = read();
315
+ }
316
+ }
302
317
  return object
303
318
  } else {
304
319
  if (restoreMapsAsObject) {
@@ -306,8 +321,24 @@
306
321
  restoreMapsAsObject = false;
307
322
  }
308
323
  let map = new Map();
309
- if (currentDecoder.keyMap) while((key = read()) != STOP_CODE) map.set(currentDecoder.decodeKey(key), read());
310
- else while ((key = read()) != STOP_CODE) map.set(key, read());
324
+ if (currentDecoder.keyMap) {
325
+ let i = 0;
326
+ while((key = read()) != STOP_CODE) {
327
+ if (i++ >= maxMapSize) {
328
+ throw new Error(`Map size exceeds ${maxMapSize}`);
329
+ }
330
+ map.set(currentDecoder.decodeKey(key), read());
331
+ }
332
+ }
333
+ else {
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(key, read());
340
+ }
341
+ }
311
342
  return map
312
343
  }
313
344
  case 7:
@@ -338,12 +369,14 @@
338
369
  }
339
370
  return readFixedString(token)
340
371
  case 4: // array
372
+ if (token >= maxArraySize) throw new Error(`Array length exceeds ${maxArraySize}`)
341
373
  let array = new Array(token);
342
374
  //if (currentDecoder.keyMap) for (let i = 0; i < token; i++) array[i] = currentDecoder.decodeKey(read())
343
375
  //else
344
376
  for (let i = 0; i < token; i++) array[i] = read();
345
377
  return array
346
378
  case 5: // map
379
+ if (token >= maxMapSize) throw new Error(`Map size exceeds ${maxArraySize}`)
347
380
  if (currentDecoder.mapsAsObjects) {
348
381
  let object = {};
349
382
  if (currentDecoder.keyMap) for (let i = 0; i < token; i++) object[safeKey(currentDecoder.decodeKey(read()))] = read();
@@ -445,6 +478,7 @@
445
478
  }
446
479
  const validName = /^[a-zA-Z_$][a-zA-Z\d_$]*$/;
447
480
  function createStructureReader(structure) {
481
+ if (!structure) throw new Error('Structure is required in record definition');
448
482
  function readObject() {
449
483
  // get the array size from the header
450
484
  let length = src[position$1++];
@@ -771,7 +805,7 @@
771
805
  // bigint extension
772
806
  let value = BigInt(0);
773
807
  for (let i = 0, l = buffer.byteLength; i < l; i++) {
774
- value = BigInt(buffer[i]) + value << BigInt(8);
808
+ value = BigInt(buffer[i]) + (value << BigInt(8));
775
809
  }
776
810
  return value
777
811
  };
@@ -1117,6 +1151,12 @@
1117
1151
  currentExtensions[extension.tag] = extension.decode;
1118
1152
  }
1119
1153
 
1154
+ function setSizeLimits(limits) {
1155
+ if (limits.maxMapSize) maxMapSize = limits.maxMapSize;
1156
+ if (limits.maxArraySize) maxArraySize = limits.maxArraySize;
1157
+ if (limits.maxObjectSize) limits.maxObjectSize;
1158
+ }
1159
+
1120
1160
  const mult10 = new Array(147); // this is a table matching binary exponents to the multiplier to determine significant digit rounding
1121
1161
  for (let i = 0; i < 256; i++) {
1122
1162
  mult10[i] = +('1e' + Math.floor(45.15 - i * 0.30103));
@@ -1166,8 +1206,8 @@
1166
1206
  let structures;
1167
1207
  let referenceMap;
1168
1208
  options = options || {};
1169
- let encodeUtf8 = ByteArray.prototype.utf8Write ? function(string, position, maxBytes) {
1170
- return target.utf8Write(string, position, maxBytes)
1209
+ let encodeUtf8 = ByteArray.prototype.utf8Write ? function(string, position) {
1210
+ return target.utf8Write(string, position, target.byteLength - position)
1171
1211
  } : (textEncoder && textEncoder.encodeInto) ?
1172
1212
  function(string, position) {
1173
1213
  return textEncoder.encodeInto(string, target.subarray(position)).written
@@ -1548,7 +1588,7 @@
1548
1588
  targetView.setUint32(position, value);
1549
1589
  position += 4;
1550
1590
  }
1551
- } else if (!this.alwaysUseFloat && value >> 0 === value) { // negative integer
1591
+ } else if (!this.alwaysUseFloat && value >> 0 === value) { // negative integer, 31-bit or less
1552
1592
  if (value >= -0x18) {
1553
1593
  target[position++] = 0x1f - value;
1554
1594
  } else if (value >= -0x100) {
@@ -1563,6 +1603,11 @@
1563
1603
  targetView.setUint32(position, ~value);
1564
1604
  position += 4;
1565
1605
  }
1606
+ } else if (!this.alwaysUseFloat && value < 0 && value >= -0x100000000 && Math.floor(value) === value) {
1607
+ // negative integer, 32-bit or less
1608
+ target[position++] = 0x3a;
1609
+ targetView.setUint32(position, -1 - value);
1610
+ position += 4;
1566
1611
  } else {
1567
1612
  let useFloat32;
1568
1613
  if ((useFloat32 = this.useFloat32) > 0 && value < 0x100000000 && value >= -0x80000000) {
@@ -1604,6 +1649,9 @@
1604
1649
  }
1605
1650
  let constructor = value.constructor;
1606
1651
  if (constructor === Object) {
1652
+ if (this.skipFunction === true) {
1653
+ value = Object.fromEntries([...Object.keys(value).filter(x => typeof value[x] !== "function").map(x => [x, value[x]])]);
1654
+ }
1607
1655
  writeObject(value);
1608
1656
  } else if (constructor === Array) {
1609
1657
  length = value.length;
@@ -1720,7 +1768,19 @@
1720
1768
  target[position++] = 0xfb;
1721
1769
  targetView.setFloat64(position, Number(value));
1722
1770
  } else {
1723
- throw new RangeError(value + ' was too large to fit in CBOR 64-bit integer format, set largeBigIntToFloat to convert to float-64')
1771
+ if (value >= BigInt(0))
1772
+ target[position++] = 0xc2; // tag 2
1773
+ else {
1774
+ target[position++] = 0xc3; // tag 2
1775
+ value = BigInt(-1) - value;
1776
+ }
1777
+ let bytes = [];
1778
+ while (value) {
1779
+ bytes.push(Number(value & BigInt(0xff)));
1780
+ value >>= BigInt(8);
1781
+ }
1782
+ writeBuffer(new Uint8Array(bytes.reverse()), makeRoom);
1783
+ return;
1724
1784
  }
1725
1785
  }
1726
1786
  position += 8;
@@ -1767,7 +1827,7 @@
1767
1827
  let objectOffset = position - start;
1768
1828
  position += 2;
1769
1829
  let size = 0;
1770
- if (encoder.keyMap) {
1830
+ if (encoder.keyMap) {
1771
1831
  for (let key in object) if (typeof object.hasOwnProperty !== 'function' || object.hasOwnProperty(key)) {
1772
1832
  encode(encoder.encodeKey(key));
1773
1833
  encode(object[key]);
@@ -1939,7 +1999,7 @@
1939
1999
  yield* tryEncode(value, iterateProperties, 'element');
1940
2000
  } else encode(value);
1941
2001
  }
1942
- } else if (object[Symbol.iterator]) {
2002
+ } else if (object[Symbol.iterator] && !object.buffer) { // iterator, but exclude typed arrays
1943
2003
  target[position++] = 0x9f; // start indefinite array
1944
2004
  for (let value of object) {
1945
2005
  if (value && (typeof value === 'object' || position - start > chunkThreshold)) {
@@ -2451,6 +2511,7 @@
2451
2511
  exports.encodeIter = encodeIter;
2452
2512
  exports.isNativeAccelerationEnabled = isNativeAccelerationEnabled;
2453
2513
  exports.roundFloat32 = roundFloat32;
2514
+ exports.setSizeLimits = setSizeLimits;
2454
2515
 
2455
2516
  }));
2456
2517
  //# sourceMappingURL=index-no-eval.cjs.map