cbor-x 1.5.2 → 1.5.4

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(',') + '}')
@@ -1135,7 +1145,7 @@ try {
1135
1145
  textEncoder = new TextEncoder();
1136
1146
  } catch (error) {}
1137
1147
  let extensions, extensionClasses;
1138
- const Buffer$1 = globalThis.Buffer;
1148
+ const Buffer$1 = typeof globalThis === 'object' && globalThis.Buffer;
1139
1149
  const hasNodeBuffer = typeof Buffer$1 !== 'undefined';
1140
1150
  const ByteArrayAllocate = hasNodeBuffer ? Buffer$1.allocUnsafeSlow : Uint8Array;
1141
1151
  const ByteArray = hasNodeBuffer ? Buffer$1 : Uint8Array;
@@ -1686,6 +1696,13 @@ class Encoder extends Decoder {
1686
1696
  error.iteratorNotHandled = true;
1687
1697
  throw error;
1688
1698
  }
1699
+ if (this.useToJSON && value.toJSON) {
1700
+ const json = value.toJSON();
1701
+ // if for some reason value.toJSON returns itself it'll loop forever
1702
+ if (json !== value)
1703
+ return encode(json)
1704
+ }
1705
+
1689
1706
  // no extension found, write as object
1690
1707
  writeObject(value, !value.hasOwnProperty); // if it doesn't have hasOwnProperty, don't do hasOwnProperty checks
1691
1708
  }
@@ -1739,7 +1756,7 @@ class Encoder extends Decoder {
1739
1756
  }
1740
1757
  if (encoder.keyMap) {
1741
1758
  for (let i = 0; i < length; i++) {
1742
- encode(encodeKey(keys[i]));
1759
+ encode(encoder.encodeKey(keys[i]));
1743
1760
  encode(vals[i]);
1744
1761
  }
1745
1762
  } else {