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.
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 = {};
@@ -265,6 +269,7 @@ function read() {
265
269
  }
266
270
  token = dataView.getUint32(position$1);
267
271
  position$1 += 4;
272
+ if (majorType === 1) return -1 - token; // can't safely use negation operator here
268
273
  break
269
274
  case 0x1b:
270
275
  if (majorType == 7) {
@@ -279,8 +284,7 @@ function read() {
279
284
  } else if (currentDecoder.int64AsNumber) {
280
285
  token = dataView.getUint32(position$1) * 0x100000000;
281
286
  token += dataView.getUint32(position$1 + 4);
282
- } else
283
- token = dataView.getBigUint64(position$1);
287
+ } else token = dataView.getBigUint64(position$1);
284
288
  position$1 += 8;
285
289
  break
286
290
  case 0x1f:
@@ -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));
@@ -1208,8 +1249,8 @@ class Encoder extends Decoder {
1208
1249
  let structures;
1209
1250
  let referenceMap;
1210
1251
  options = options || {};
1211
- let encodeUtf8 = ByteArray.prototype.utf8Write ? function(string, position, maxBytes) {
1212
- return target.utf8Write(string, position, maxBytes)
1252
+ let encodeUtf8 = ByteArray.prototype.utf8Write ? function(string, position) {
1253
+ return target.utf8Write(string, position, target.byteLength - position)
1213
1254
  } : (textEncoder && textEncoder.encodeInto) ?
1214
1255
  function(string, position) {
1215
1256
  return textEncoder.encodeInto(string, target.subarray(position)).written
@@ -1590,7 +1631,7 @@ class Encoder extends Decoder {
1590
1631
  targetView.setUint32(position, value);
1591
1632
  position += 4;
1592
1633
  }
1593
- } else if (!this.alwaysUseFloat && value >> 0 === value) { // negative integer
1634
+ } else if (!this.alwaysUseFloat && value >> 0 === value) { // negative integer, 31-bit or less
1594
1635
  if (value >= -0x18) {
1595
1636
  target[position++] = 0x1f - value;
1596
1637
  } else if (value >= -0x100) {
@@ -1605,6 +1646,11 @@ class Encoder extends Decoder {
1605
1646
  targetView.setUint32(position, ~value);
1606
1647
  position += 4;
1607
1648
  }
1649
+ } else if (!this.alwaysUseFloat && value < 0 && value >= -0x100000000 && Math.floor(value) === value) {
1650
+ // negative integer, 32-bit or less
1651
+ target[position++] = 0x3a;
1652
+ targetView.setUint32(position, -1 - value);
1653
+ position += 4;
1608
1654
  } else {
1609
1655
  let useFloat32;
1610
1656
  if ((useFloat32 = this.useFloat32) > 0 && value < 0x100000000 && value >= -0x80000000) {
@@ -1646,6 +1692,9 @@ class Encoder extends Decoder {
1646
1692
  }
1647
1693
  let constructor = value.constructor;
1648
1694
  if (constructor === Object) {
1695
+ if (this.skipFunction === true) {
1696
+ value = Object.fromEntries([...Object.keys(value).filter(x => typeof value[x] !== "function").map(x => [x, value[x]])]);
1697
+ }
1649
1698
  writeObject(value);
1650
1699
  } else if (constructor === Array) {
1651
1700
  length = value.length;
@@ -1762,7 +1811,19 @@ class Encoder extends Decoder {
1762
1811
  target[position++] = 0xfb;
1763
1812
  targetView.setFloat64(position, Number(value));
1764
1813
  } else {
1765
- throw new RangeError(value + ' was too large to fit in CBOR 64-bit integer format, set largeBigIntToFloat to convert to float-64')
1814
+ if (value >= BigInt(0))
1815
+ target[position++] = 0xc2; // tag 2
1816
+ else {
1817
+ target[position++] = 0xc3; // tag 2
1818
+ value = BigInt(-1) - value;
1819
+ }
1820
+ let bytes = [];
1821
+ while (value) {
1822
+ bytes.push(Number(value & BigInt(0xff)));
1823
+ value >>= BigInt(8);
1824
+ }
1825
+ writeBuffer(new Uint8Array(bytes.reverse()), makeRoom);
1826
+ return;
1766
1827
  }
1767
1828
  }
1768
1829
  position += 8;
@@ -1809,7 +1870,7 @@ class Encoder extends Decoder {
1809
1870
  let objectOffset = position - start;
1810
1871
  position += 2;
1811
1872
  let size = 0;
1812
- if (encoder.keyMap) {
1873
+ if (encoder.keyMap) {
1813
1874
  for (let key in object) if (typeof object.hasOwnProperty !== 'function' || object.hasOwnProperty(key)) {
1814
1875
  encode(encoder.encodeKey(key));
1815
1876
  encode(object[key]);
@@ -1981,7 +2042,7 @@ class Encoder extends Decoder {
1981
2042
  yield* tryEncode(value, iterateProperties, 'element');
1982
2043
  } else encode(value);
1983
2044
  }
1984
- } else if (object[Symbol.iterator]) {
2045
+ } else if (object[Symbol.iterator] && !object.buffer) { // iterator, but exclude typed arrays
1985
2046
  target[position++] = 0x9f; // start indefinite array
1986
2047
  for (let value of object) {
1987
2048
  if (value && (typeof value === 'object' || position - start > chunkThreshold)) {
@@ -2571,5 +2632,6 @@ exports.encodeAsIterable = encodeAsIterable;
2571
2632
  exports.encodeIter = encodeIter;
2572
2633
  exports.mapsAsObjects = mapsAsObjects;
2573
2634
  exports.roundFloat32 = roundFloat32;
2635
+ exports.setSizeLimits = setSizeLimits;
2574
2636
  exports.useRecords = useRecords;
2575
2637
  //# sourceMappingURL=node.cjs.map