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/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;
@@ -261,6 +264,7 @@
261
264
  }
262
265
  token = dataView.getUint32(position$1);
263
266
  position$1 += 4;
267
+ if (majorType === 1) return -1 - token; // can't safely use negation operator here
264
268
  break
265
269
  case 0x1b:
266
270
  if (majorType == 7) {
@@ -275,8 +279,7 @@
275
279
  } else if (currentDecoder.int64AsNumber) {
276
280
  token = dataView.getUint32(position$1) * 0x100000000;
277
281
  token += dataView.getUint32(position$1 + 4);
278
- } else
279
- token = dataView.getBigUint64(position$1);
282
+ } else token = dataView.getBigUint64(position$1);
280
283
  position$1 += 8;
281
284
  break
282
285
  case 0x1f:
@@ -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));
@@ -1165,8 +1205,8 @@
1165
1205
  let structures;
1166
1206
  let referenceMap;
1167
1207
  options = options || {};
1168
- let encodeUtf8 = ByteArray.prototype.utf8Write ? function(string, position, maxBytes) {
1169
- return target.utf8Write(string, position, maxBytes)
1208
+ let encodeUtf8 = ByteArray.prototype.utf8Write ? function(string, position) {
1209
+ return target.utf8Write(string, position, target.byteLength - position)
1170
1210
  } : (textEncoder && textEncoder.encodeInto) ?
1171
1211
  function(string, position) {
1172
1212
  return textEncoder.encodeInto(string, target.subarray(position)).written
@@ -1547,7 +1587,7 @@
1547
1587
  targetView.setUint32(position, value);
1548
1588
  position += 4;
1549
1589
  }
1550
- } else if (!this.alwaysUseFloat && value >> 0 === value) { // negative integer
1590
+ } else if (!this.alwaysUseFloat && value >> 0 === value) { // negative integer, 31-bit or less
1551
1591
  if (value >= -0x18) {
1552
1592
  target[position++] = 0x1f - value;
1553
1593
  } else if (value >= -0x100) {
@@ -1562,6 +1602,11 @@
1562
1602
  targetView.setUint32(position, ~value);
1563
1603
  position += 4;
1564
1604
  }
1605
+ } else if (!this.alwaysUseFloat && value < 0 && value >= -0x100000000 && Math.floor(value) === value) {
1606
+ // negative integer, 32-bit or less
1607
+ target[position++] = 0x3a;
1608
+ targetView.setUint32(position, -1 - value);
1609
+ position += 4;
1565
1610
  } else {
1566
1611
  let useFloat32;
1567
1612
  if ((useFloat32 = this.useFloat32) > 0 && value < 0x100000000 && value >= -0x80000000) {
@@ -1603,6 +1648,9 @@
1603
1648
  }
1604
1649
  let constructor = value.constructor;
1605
1650
  if (constructor === Object) {
1651
+ if (this.skipFunction === true) {
1652
+ value = Object.fromEntries([...Object.keys(value).filter(x => typeof value[x] !== "function").map(x => [x, value[x]])]);
1653
+ }
1606
1654
  writeObject(value);
1607
1655
  } else if (constructor === Array) {
1608
1656
  length = value.length;
@@ -1719,7 +1767,19 @@
1719
1767
  target[position++] = 0xfb;
1720
1768
  targetView.setFloat64(position, Number(value));
1721
1769
  } else {
1722
- throw new RangeError(value + ' was too large to fit in CBOR 64-bit integer format, set largeBigIntToFloat to convert to float-64')
1770
+ if (value >= BigInt(0))
1771
+ target[position++] = 0xc2; // tag 2
1772
+ else {
1773
+ target[position++] = 0xc3; // tag 2
1774
+ value = BigInt(-1) - value;
1775
+ }
1776
+ let bytes = [];
1777
+ while (value) {
1778
+ bytes.push(Number(value & BigInt(0xff)));
1779
+ value >>= BigInt(8);
1780
+ }
1781
+ writeBuffer(new Uint8Array(bytes.reverse()), makeRoom);
1782
+ return;
1723
1783
  }
1724
1784
  }
1725
1785
  position += 8;
@@ -1766,7 +1826,7 @@
1766
1826
  let objectOffset = position - start;
1767
1827
  position += 2;
1768
1828
  let size = 0;
1769
- if (encoder.keyMap) {
1829
+ if (encoder.keyMap) {
1770
1830
  for (let key in object) if (typeof object.hasOwnProperty !== 'function' || object.hasOwnProperty(key)) {
1771
1831
  encode(encoder.encodeKey(key));
1772
1832
  encode(object[key]);
@@ -1938,7 +1998,7 @@
1938
1998
  yield* tryEncode(value, iterateProperties, 'element');
1939
1999
  } else encode(value);
1940
2000
  }
1941
- } else if (object[Symbol.iterator]) {
2001
+ } else if (object[Symbol.iterator] && !object.buffer) { // iterator, but exclude typed arrays
1942
2002
  target[position++] = 0x9f; // start indefinite array
1943
2003
  for (let value of object) {
1944
2004
  if (value && (typeof value === 'object' || position - start > chunkThreshold)) {
@@ -2450,6 +2510,7 @@
2450
2510
  exports.encodeIter = encodeIter;
2451
2511
  exports.isNativeAccelerationEnabled = isNativeAccelerationEnabled;
2452
2512
  exports.roundFloat32 = roundFloat32;
2513
+ exports.setSizeLimits = setSizeLimits;
2453
2514
 
2454
2515
  }));
2455
2516
  //# sourceMappingURL=index.js.map