cbor-x 1.5.8 → 1.5.9

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
@@ -499,7 +499,8 @@
499
499
  function safeKey(key) {
500
500
  // protect against prototype pollution
501
501
  if (typeof key === 'string') return key === '__proto__' ? '__proto_' : key
502
- if (typeof key !== 'object') return key.toString()
502
+ if (typeof key === 'number' || typeof key === 'boolean' || typeof key === 'bigint') return key.toString();
503
+ if (key == null) return key + '';
503
504
  // protect against expensive (DoS) string conversions
504
505
  throw new Error('Invalid property name type ' + typeof key);
505
506
  }
@@ -884,6 +885,7 @@
884
885
  referenceMap.id = 0;
885
886
  }
886
887
  let id = referenceMap.id++;
888
+ let startingPosition = position$1;
887
889
  let token = src[position$1];
888
890
  let target;
889
891
  // TODO: handle Maps, Sets, and other types that can cycle; this is complicated, because you potentially need to read
@@ -896,8 +898,20 @@
896
898
  let refEntry = { target }; // a placeholder object
897
899
  referenceMap.set(id, refEntry);
898
900
  let targetProperties = read(); // read the next value as the target object to id
899
- if (refEntry.used) // there is a cycle, so we have to assign properties to original target
901
+ if (refEntry.used) {// there is a cycle, so we have to assign properties to original target
902
+ if (Object.getPrototypeOf(target) !== Object.getPrototypeOf(targetProperties)) {
903
+ // this means that the returned target does not match the targetProperties, so we need rerun the read to
904
+ // have the correctly create instance be assigned as a reference, then we do the copy the properties back to the
905
+ // target
906
+ // reset the position so that the read can be repeated
907
+ position$1 = startingPosition;
908
+ // the returned instance is our new target for references
909
+ target = targetProperties;
910
+ referenceMap.set(id, { target });
911
+ targetProperties = read();
912
+ }
900
913
  return Object.assign(target, targetProperties)
914
+ }
901
915
  refEntry.target = targetProperties; // the placeholder wasn't used, replace with the deserialized one
902
916
  return targetProperties // no cycle, can just use the returned read object
903
917
  };
@@ -979,7 +993,7 @@
979
993
  for (let littleEndian = 0; littleEndian < 2; littleEndian++) {
980
994
  if (!littleEndian && bytesPerElement == 1)
981
995
  continue
982
- let sizeShift = bytesPerElement == 2 ? 1 : bytesPerElement == 4 ? 2 : 3;
996
+ let sizeShift = bytesPerElement == 2 ? 1 : bytesPerElement == 4 ? 2 : bytesPerElement == 8 ? 3 : 0;
983
997
  currentExtensions[littleEndian ? tag : (tag - 4)] = (bytesPerElement == 1 || littleEndian == isLittleEndianMachine$1) ? (buffer) => {
984
998
  if (!TypedArray)
985
999
  throw new Error('Could not find typed array for code ' + tag)
@@ -989,7 +1003,7 @@
989
1003
  bytesPerElement === 2 && !(buffer.byteOffset & 1) ||
990
1004
  bytesPerElement === 4 && !(buffer.byteOffset & 3) ||
991
1005
  bytesPerElement === 8 && !(buffer.byteOffset & 7))
992
- return new TypedArray(buffer.buffer, buffer.byteOffset, buffer.byteLength);
1006
+ return new TypedArray(buffer.buffer, buffer.byteOffset, buffer.byteLength >> sizeShift);
993
1007
  }
994
1008
  // we have to slice/copy here to get a new ArrayBuffer, if we are not word/byte aligned
995
1009
  return new TypedArray(Uint8Array.prototype.slice.call(buffer, 0).buffer)
@@ -1589,7 +1603,7 @@
1589
1603
  }
1590
1604
  let constructor = value.constructor;
1591
1605
  if (constructor === Object) {
1592
- writeObject(value, true);
1606
+ writeObject(value);
1593
1607
  } else if (constructor === Array) {
1594
1608
  length = value.length;
1595
1609
  if (length < 0x18) {
@@ -1684,8 +1698,8 @@
1684
1698
  return encode(json)
1685
1699
  }
1686
1700
 
1687
- // no extension found, write as object
1688
- writeObject(value, !value.hasOwnProperty); // if it doesn't have hasOwnProperty, don't do hasOwnProperty checks
1701
+ // no extension found, write as a plain object
1702
+ writeObject(value);
1689
1703
  }
1690
1704
  }
1691
1705
  } else if (type === 'boolean') {
@@ -1747,19 +1761,19 @@
1747
1761
  }
1748
1762
  }
1749
1763
  } :
1750
- (object, safePrototype) => {
1764
+ (object) => {
1751
1765
  target[position++] = 0xb9; // always use map 16, so we can preallocate and set the length afterwards
1752
1766
  let objectOffset = position - start;
1753
1767
  position += 2;
1754
1768
  let size = 0;
1755
1769
  if (encoder.keyMap) {
1756
- for (let key in object) if (safePrototype || object.hasOwnProperty(key)) {
1770
+ for (let key in object) if (typeof object.hasOwnProperty !== 'function' || object.hasOwnProperty(key)) {
1757
1771
  encode(encoder.encodeKey(key));
1758
1772
  encode(object[key]);
1759
1773
  size++;
1760
1774
  }
1761
1775
  } else {
1762
- for (let key in object) if (safePrototype || object.hasOwnProperty(key)) {
1776
+ for (let key in object) if (typeof object.hasOwnProperty !== 'function' || object.hasOwnProperty(key)) {
1763
1777
  encode(key);
1764
1778
  encode(object[key]);
1765
1779
  size++;
@@ -1768,7 +1782,7 @@
1768
1782
  target[objectOffset++ + start] = size >> 8;
1769
1783
  target[objectOffset + start] = size & 0xff;
1770
1784
  } :
1771
- (object, safePrototype) => {
1785
+ (object, skipValues) => {
1772
1786
  let nextTransition, transition = structures.transitions || (structures.transitions = Object.create(null));
1773
1787
  let newTransitions = 0;
1774
1788
  let length = 0;
@@ -1787,7 +1801,7 @@
1787
1801
  transition = nextTransition;
1788
1802
  }
1789
1803
  } else {
1790
- for (let key in object) if (safePrototype || object.hasOwnProperty(key)) {
1804
+ for (let key in object) if (typeof object.hasOwnProperty !== 'function' || object.hasOwnProperty(key)) {
1791
1805
  nextTransition = transition[key];
1792
1806
  if (!nextTransition) {
1793
1807
  if (transition[RECORD_SYMBOL] & 0x100000) {// this indicates it is a brancheable/extendable terminal node, so we will use this record id and extend it
@@ -1847,9 +1861,9 @@
1847
1861
  writeArrayHeader(length + 2);
1848
1862
  encode(0xe000 + recordId);
1849
1863
  encode(keys);
1850
- if (safePrototype === null) return; // special exit for iterator
1864
+ if (skipValues) return; // special exit for iterator
1851
1865
  for (let key in object)
1852
- if (safePrototype || object.hasOwnProperty(key))
1866
+ if (typeof object.hasOwnProperty !== 'function' || object.hasOwnProperty(key))
1853
1867
  encode(object[key]);
1854
1868
  return
1855
1869
  }
@@ -1859,9 +1873,9 @@
1859
1873
  } else {
1860
1874
  writeArrayHeader(length);
1861
1875
  }
1862
- if (safePrototype === null) return; // special exit for iterator
1876
+ if (skipValues) return; // special exit for iterator
1863
1877
  for (let key in object)
1864
- if (safePrototype || object.hasOwnProperty(key))
1878
+ if (typeof object.hasOwnProperty !== 'function' || object.hasOwnProperty(key))
1865
1879
  encode(object[key]);
1866
1880
  };
1867
1881
  const makeRoom = (end) => {
@@ -1899,7 +1913,7 @@
1899
1913
  if (constructor === Object) {
1900
1914
  let useRecords = encoder.useRecords !== false;
1901
1915
  if (useRecords)
1902
- writeObject(object, null); // write the record identifier
1916
+ writeObject(object, true); // write the record identifier
1903
1917
  else
1904
1918
  writeEntityLength(Object.keys(object).length, 0xa0);
1905
1919
  for (let key in object) {