cbor-x 1.5.1 → 1.5.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
@@ -36,6 +36,16 @@ let defaultOptions = {
36
36
  mapsAsObjects: true
37
37
  };
38
38
  let sequentialMode = false;
39
+ let inlineObjectReadThreshold = 2;
40
+ // no-eval build
41
+ try {
42
+ new Function('');
43
+ } catch(error) {
44
+ // if eval variants are not supported, do not create inline object readers ever
45
+ inlineObjectReadThreshold = Infinity;
46
+ }
47
+
48
+
39
49
 
40
50
  class Decoder {
41
51
  constructor(options) {
@@ -468,7 +478,7 @@ function createStructureReader(structure) {
468
478
  return compiledReader(read) // with the right length, so we use it
469
479
  compiledReader = compiledReader.next; // see if there is another reader with the right length
470
480
  }
471
- if (this.slowReads++ >= 3) { // create a fast compiled reader
481
+ if (this.slowReads++ >= inlineObjectReadThreshold) { // create a fast compiled reader
472
482
  let array = this.length == length ? this : this.slice(0, length);
473
483
  compiledReader = currentDecoder.keyMap
474
484
  ? new Function('r', 'return {' + array.map(k => currentDecoder.decodeKey(k)).map(k => validName.test(k) ? safeKey(k) + ':r()' : ('[' + JSON.stringify(k) + ']:r()')).join(',') + '}')
@@ -984,9 +994,11 @@ for (let i = 0; i < typedArrays.length; i++) {
984
994
  }
985
995
  function registerTypedArray(TypedArray, tag) {
986
996
  let dvMethod = 'get' + TypedArray.name.slice(0, -5);
987
- if (typeof TypedArray !== 'function')
997
+ let bytesPerElement;
998
+ if (typeof TypedArray === 'function')
999
+ bytesPerElement = TypedArray.BYTES_PER_ELEMENT;
1000
+ else
988
1001
  TypedArray = null;
989
- let bytesPerElement = TypedArray.BYTES_PER_ELEMENT;
990
1002
  for (let littleEndian = 0; littleEndian < 2; littleEndian++) {
991
1003
  if (!littleEndian && bytesPerElement == 1)
992
1004
  continue
@@ -1133,7 +1145,7 @@ try {
1133
1145
  textEncoder = new TextEncoder();
1134
1146
  } catch (error) {}
1135
1147
  let extensions, extensionClasses;
1136
- const Buffer$1 = globalThis.Buffer;
1148
+ const Buffer$1 = typeof globalThis === 'object' && globalThis.Buffer;
1137
1149
  const hasNodeBuffer = typeof Buffer$1 !== 'undefined';
1138
1150
  const ByteArrayAllocate = hasNodeBuffer ? Buffer$1.allocUnsafeSlow : Uint8Array;
1139
1151
  const ByteArray = hasNodeBuffer ? Buffer$1 : Uint8Array;
@@ -1524,7 +1536,7 @@ class Encoder extends Decoder {
1524
1536
  }
1525
1537
  position += length;
1526
1538
  } else if (type === 'number') {
1527
- if (value >>> 0 === value) {// positive integer, 32-bit or less
1539
+ if (!this.alwaysUseFloat && value >>> 0 === value) {// positive integer, 32-bit or less
1528
1540
  // positive uint
1529
1541
  if (value < 0x18) {
1530
1542
  target[position++] = value;
@@ -1540,7 +1552,7 @@ class Encoder extends Decoder {
1540
1552
  targetView.setUint32(position, value);
1541
1553
  position += 4;
1542
1554
  }
1543
- } else if (value >> 0 === value) { // negative integer
1555
+ } else if (!this.alwaysUseFloat && value >> 0 === value) { // negative integer
1544
1556
  if (value >= -0x18) {
1545
1557
  target[position++] = 0x1f - value;
1546
1558
  } else if (value >= -0x100) {