msgpackr 1.9.2 → 1.9.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
@@ -988,10 +988,10 @@ const recordDefinition = (id, highByte) => {
988
988
  currentExtensions[0] = () => {}; // notepack defines extension 0 to mean undefined, so use that as the default here
989
989
  currentExtensions[0].noBuffer = true;
990
990
 
991
- let glbl = typeof globalThis === 'object' ? globalThis : window;
991
+ let errors = { Error, TypeError, ReferenceError };
992
992
  currentExtensions[0x65] = () => {
993
993
  let data = read();
994
- return (glbl[data[0]] || Error)(data[1])
994
+ return (errors[data[0]] || Error)(data[1])
995
995
  };
996
996
 
997
997
  currentExtensions[0x69] = (data) => {
@@ -1029,6 +1029,7 @@ currentExtensions[0x73] = () => new Set(read());
1029
1029
 
1030
1030
  const typedArrays = ['Int8','Uint8','Uint8Clamped','Int16','Uint16','Int32','Uint32','Float32','Float64','BigInt64','BigUint64'].map(type => type + 'Array');
1031
1031
 
1032
+ let glbl = typeof globalThis === 'object' ? globalThis : window;
1032
1033
  currentExtensions[0x74] = (data) => {
1033
1034
  let typeCode = data[0];
1034
1035
  let typedArrayName = typedArrays[typeCode];
@@ -1534,7 +1535,7 @@ class Packr extends Unpackr {
1534
1535
  targetView.setFloat64(position, value);
1535
1536
  position += 8;
1536
1537
  }
1537
- } else if (type === 'object') {
1538
+ } else if (type === 'object' || type === 'function') {
1538
1539
  if (!value)
1539
1540
  target[position++] = 0xc0;
1540
1541
  else {
@@ -1641,6 +1642,11 @@ class Packr extends Unpackr {
1641
1642
  } else {
1642
1643
  if (value.toJSON) // use this as an alternate mechanism for expressing how to serialize
1643
1644
  return pack(value.toJSON());
1645
+
1646
+ // if there is a writeFunction, use it, otherwise just encode as undefined
1647
+ if (type === 'function')
1648
+ return pack(this.writeFunction && this.writeFunction(value));
1649
+
1644
1650
  // no extension found, write as object
1645
1651
  writeObject(value, !value.hasOwnProperty); // if it doesn't have hasOwnProperty, don't do hasOwnProperty checks
1646
1652
  }
@@ -1675,14 +1681,12 @@ class Packr extends Unpackr {
1675
1681
  target[position++] = 0;
1676
1682
  target[position++] = 0;
1677
1683
  }
1678
- } else if (type === 'function') {
1679
- pack(this.writeFunction && this.writeFunction()); // if there is a writeFunction, use it, otherwise just encode as undefined
1680
1684
  } else {
1681
1685
  throw new Error('Unknown type: ' + type)
1682
1686
  }
1683
1687
  };
1684
1688
 
1685
- const writeObject = this.useRecords === false ? this.variableMapSize ? (object) => {
1689
+ const writePlainObject = (this.variableMapSize || this.coercibleKeyAsNumber) ? (object) => {
1686
1690
  // this method is slightly slower, but generates "preferred serialization" (optimally small for smaller objects)
1687
1691
  let keys = Object.keys(object);
1688
1692
  let length = keys.length;
@@ -1698,9 +1702,19 @@ class Packr extends Unpackr {
1698
1702
  position += 4;
1699
1703
  }
1700
1704
  let key;
1701
- for (let i = 0; i < length; i++) {
1702
- pack(key = keys[i]);
1703
- pack(object[key]);
1705
+ if (this.coercibleKeyAsNumber) {
1706
+ for (let i = 0; i < length; i++) {
1707
+ key = keys[i];
1708
+ let num = Number(key);
1709
+ pack(isNaN(num) ? key : num);
1710
+ pack(object[key]);
1711
+ }
1712
+
1713
+ } else {
1714
+ for (let i = 0; i < length; i++) {
1715
+ pack(key = keys[i]);
1716
+ pack(object[key]);
1717
+ }
1704
1718
  }
1705
1719
  } :
1706
1720
  (object, safePrototype) => {
@@ -1717,7 +1731,9 @@ class Packr extends Unpackr {
1717
1731
  }
1718
1732
  target[objectOffset++ + start] = size >> 8;
1719
1733
  target[objectOffset + start] = size & 0xff;
1720
- } :
1734
+ };
1735
+
1736
+ const writeRecord = this.useRecords === false ? writePlainObject :
1721
1737
  (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)
1722
1738
  (object, safePrototype) => {
1723
1739
  let nextTransition, transition = structures.transitions || (structures.transitions = Object.create(null));
@@ -1789,6 +1805,14 @@ class Packr extends Unpackr {
1789
1805
  if (safePrototype || object.hasOwnProperty(key))
1790
1806
  pack(object[key]);
1791
1807
  };
1808
+
1809
+ // craete reference to useRecords if useRecords is a function
1810
+ const checkUseRecords = typeof this.useRecords == 'function' && this.useRecords;
1811
+
1812
+ const writeObject = checkUseRecords ? (object, safePrototype) => {
1813
+ checkUseRecords(object) ? writeRecord(object,safePrototype) : writePlainObject(object,safePrototype);
1814
+ } : writeRecord;
1815
+
1792
1816
  const makeRoom = (end) => {
1793
1817
  let newSize;
1794
1818
  if (end > 0x1000000) {