msgpackr 1.10.1 → 1.10.2

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
@@ -967,8 +967,10 @@ function readKey() {
967
967
  }
968
968
 
969
969
  function asSafeString(property) {
970
+ // protect against expensive (DoS) string conversions
970
971
  if (typeof property === 'string') return property;
971
- if (typeof property === 'number') return property.toString();
972
+ if (typeof property === 'number' || typeof property === 'boolean' || typeof property === 'bigint') return property.toString();
973
+ if (property == null) return property + '';
972
974
  throw new Error('Invalid property type for record', typeof property);
973
975
  }
974
976
  // the registration of the record definition extension (as "r")
@@ -1008,7 +1010,7 @@ currentExtensions[0x42] = (data) => {
1008
1010
  let errors = { Error, TypeError, ReferenceError };
1009
1011
  currentExtensions[0x65] = () => {
1010
1012
  let data = read();
1011
- return (errors[data[0]] || Error)(data[1])
1013
+ return (errors[data[0]] || Error)(data[1], { cause: data[2] })
1012
1014
  };
1013
1015
 
1014
1016
  currentExtensions[0x69] = (data) => {
@@ -1052,8 +1054,15 @@ let glbl = typeof globalThis === 'object' ? globalThis : window;
1052
1054
  currentExtensions[0x74] = (data) => {
1053
1055
  let typeCode = data[0];
1054
1056
  let typedArrayName = typedArrays[typeCode];
1055
- if (!typedArrayName)
1057
+ if (!typedArrayName) {
1058
+ if (typeCode === 16) {
1059
+ let ab = new ArrayBuffer(data.length - 1);
1060
+ let u8 = new Uint8Array(ab);
1061
+ u8.set(data.subarray(1));
1062
+ return ab;
1063
+ }
1056
1064
  throw new Error('Could not find typed array for code ' + typeCode)
1065
+ }
1057
1066
  // we have to always slice/copy here to get a new ArrayBuffer that is word/byte aligned
1058
1067
  return new glbl[typedArrayName](Uint8Array.prototype.slice.call(data, 1).buffer)
1059
1068
  };
@@ -1360,10 +1369,14 @@ class Packr extends Unpackr {
1360
1369
  return packr.pack(value, encodeOptions)
1361
1370
  }
1362
1371
  packr.lastNamedStructuresLength = sharedLength;
1372
+ // don't keep large buffers around
1373
+ if (target.length > 0x40000000) target = null;
1363
1374
  return returnBuffer
1364
1375
  }
1365
1376
  }
1366
1377
  }
1378
+ // don't keep large buffers around, they take too much memory and cause problems (limit at 1GB)
1379
+ if (target.length > 0x40000000) target = null;
1367
1380
  if (encodeOptions & RESET_BUFFER_MODE)
1368
1381
  position = start;
1369
1382
  }
@@ -1586,7 +1599,7 @@ class Packr extends Unpackr {
1586
1599
  }
1587
1600
  let constructor = value.constructor;
1588
1601
  if (constructor === Object) {
1589
- writeObject(value, true);
1602
+ writeObject(value);
1590
1603
  } else if (constructor === Array) {
1591
1604
  packArray(value);
1592
1605
  } else if (constructor === Map) {
@@ -1682,8 +1695,8 @@ class Packr extends Unpackr {
1682
1695
  if (type === 'function')
1683
1696
  return pack(this.writeFunction && this.writeFunction(value));
1684
1697
 
1685
- // no extension found, write as object
1686
- writeObject(value, !value.hasOwnProperty); // if it doesn't have hasOwnProperty, don't do hasOwnProperty checks
1698
+ // no extension found, write as plain object
1699
+ writeObject(value);
1687
1700
  }
1688
1701
  }
1689
1702
  }
@@ -1770,13 +1783,13 @@ class Packr extends Unpackr {
1770
1783
  }
1771
1784
  }
1772
1785
  } :
1773
- (object, safePrototype) => {
1786
+ (object) => {
1774
1787
  target[position++] = 0xde; // always using map 16, so we can preallocate and set the length afterwards
1775
1788
  let objectOffset = position - start;
1776
1789
  position += 2;
1777
1790
  let size = 0;
1778
1791
  for (let key in object) {
1779
- if (safePrototype || object.hasOwnProperty(key)) {
1792
+ if (typeof object.hasOwnProperty !== 'function' || object.hasOwnProperty(key)) {
1780
1793
  pack(key);
1781
1794
  pack(object[key]);
1782
1795
  size++;
@@ -1788,12 +1801,12 @@ class Packr extends Unpackr {
1788
1801
 
1789
1802
  const writeRecord = this.useRecords === false ? writePlainObject :
1790
1803
  (options.progressiveRecords && !useTwoByteRecords) ? // this is about 2% faster for highly stable structures, since it only requires one for-in loop (but much more expensive when new structure needs to be written)
1791
- (object, safePrototype) => {
1804
+ (object) => {
1792
1805
  let nextTransition, transition = structures.transitions || (structures.transitions = Object.create(null));
1793
1806
  let objectOffset = position++ - start;
1794
1807
  let wroteKeys;
1795
1808
  for (let key in object) {
1796
- if (safePrototype || object.hasOwnProperty(key)) {
1809
+ if (typeof object.hasOwnProperty !== 'function' || object.hasOwnProperty(key)) {
1797
1810
  nextTransition = transition[key];
1798
1811
  if (nextTransition)
1799
1812
  transition = nextTransition;
@@ -1832,10 +1845,10 @@ class Packr extends Unpackr {
1832
1845
  insertNewRecord(transition, Object.keys(object), objectOffset, 0);
1833
1846
  }
1834
1847
  } :
1835
- (object, safePrototype) => {
1848
+ (object) => {
1836
1849
  let nextTransition, transition = structures.transitions || (structures.transitions = Object.create(null));
1837
1850
  let newTransitions = 0;
1838
- for (let key in object) if (safePrototype || object.hasOwnProperty(key)) {
1851
+ for (let key in object) if (typeof object.hasOwnProperty !== 'function' || object.hasOwnProperty(key)) {
1839
1852
  nextTransition = transition[key];
1840
1853
  if (!nextTransition) {
1841
1854
  nextTransition = transition[key] = Object.create(null);
@@ -1855,7 +1868,7 @@ class Packr extends Unpackr {
1855
1868
  }
1856
1869
  // now write the values
1857
1870
  for (let key in object)
1858
- if (safePrototype || object.hasOwnProperty(key)) {
1871
+ if (typeof object.hasOwnProperty !== 'function' || object.hasOwnProperty(key)) {
1859
1872
  pack(object[key]);
1860
1873
  }
1861
1874
  };
@@ -1863,8 +1876,8 @@ class Packr extends Unpackr {
1863
1876
  // craete reference to useRecords if useRecords is a function
1864
1877
  const checkUseRecords = typeof this.useRecords == 'function' && this.useRecords;
1865
1878
 
1866
- const writeObject = checkUseRecords ? (object, safePrototype) => {
1867
- checkUseRecords(object) ? writeRecord(object,safePrototype) : writePlainObject(object,safePrototype);
1879
+ const writeObject = checkUseRecords ? (object) => {
1880
+ checkUseRecords(object) ? writeRecord(object) : writePlainObject(object);
1868
1881
  } : writeRecord;
1869
1882
 
1870
1883
  const makeRoom = (end) => {
@@ -1969,7 +1982,7 @@ class Packr extends Unpackr {
1969
1982
  target[insertionOffset + start] = keysTarget[0];
1970
1983
  }
1971
1984
  };
1972
- const writeStruct = (object, safePrototype) => {
1985
+ const writeStruct = (object) => {
1973
1986
  let newPosition = writeStructSlots(object, target, start, position, structures, makeRoom, (value, newPosition, notifySharedUpdate) => {
1974
1987
  if (notifySharedUpdate)
1975
1988
  return hasSharedUpdate = true;
@@ -1983,7 +1996,7 @@ class Packr extends Unpackr {
1983
1996
  return position;
1984
1997
  }, this);
1985
1998
  if (newPosition === 0) // bail and go to a msgpack object
1986
- return writeObject(object, true);
1999
+ return writeObject(object);
1987
2000
  position = newPosition;
1988
2001
  };
1989
2002
  }
@@ -2061,7 +2074,7 @@ extensions = [{
2061
2074
  target[position++] = 0x65; // 'e' for error
2062
2075
  target[position++] = 0;
2063
2076
  }
2064
- pack([ error.name, error.message ]);
2077
+ pack([ error.name, error.message, error.cause ]);
2065
2078
  }
2066
2079
  }, {
2067
2080
  pack(regex, allocateForWrite, pack) {
@@ -2114,6 +2127,7 @@ function writeExtBuffer(typedArray, type, allocateForWrite, encode) {
2114
2127
  }
2115
2128
  target[position++] = 0x74; // "t" for typed array
2116
2129
  target[position++] = type;
2130
+ if (!typedArray.buffer) typedArray = new Uint8Array(typedArray);
2117
2131
  target.set(new Uint8Array(typedArray.buffer, typedArray.byteOffset, typedArray.byteLength), position);
2118
2132
  }
2119
2133
  function writeBuffer(buffer, allocateForWrite) {