msgpackr 1.9.2 → 1.9.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/index.js CHANGED
@@ -932,10 +932,10 @@
932
932
  currentExtensions[0] = () => {}; // notepack defines extension 0 to mean undefined, so use that as the default here
933
933
  currentExtensions[0].noBuffer = true;
934
934
 
935
- let glbl = typeof globalThis === 'object' ? globalThis : window;
935
+ let errors = { Error, TypeError, ReferenceError };
936
936
  currentExtensions[0x65] = () => {
937
937
  let data = read();
938
- return (glbl[data[0]] || Error)(data[1])
938
+ return (errors[data[0]] || Error)(data[1])
939
939
  };
940
940
 
941
941
  currentExtensions[0x69] = (data) => {
@@ -973,6 +973,7 @@
973
973
 
974
974
  const typedArrays = ['Int8','Uint8','Uint8Clamped','Int16','Uint16','Int32','Uint32','Float32','Float64','BigInt64','BigUint64'].map(type => type + 'Array');
975
975
 
976
+ let glbl = typeof globalThis === 'object' ? globalThis : window;
976
977
  currentExtensions[0x74] = (data) => {
977
978
  let typeCode = data[0];
978
979
  let typedArrayName = typedArrays[typeCode];
@@ -1467,7 +1468,7 @@
1467
1468
  targetView.setFloat64(position, value);
1468
1469
  position += 8;
1469
1470
  }
1470
- } else if (type === 'object') {
1471
+ } else if (type === 'object' || type === 'function') {
1471
1472
  if (!value)
1472
1473
  target[position++] = 0xc0;
1473
1474
  else {
@@ -1574,6 +1575,11 @@
1574
1575
  } else {
1575
1576
  if (value.toJSON) // use this as an alternate mechanism for expressing how to serialize
1576
1577
  return pack(value.toJSON());
1578
+
1579
+ // if there is a writeFunction, use it, otherwise just encode as undefined
1580
+ if (type === 'function')
1581
+ return pack(this.writeFunction && this.writeFunction(value));
1582
+
1577
1583
  // no extension found, write as object
1578
1584
  writeObject(value, !value.hasOwnProperty); // if it doesn't have hasOwnProperty, don't do hasOwnProperty checks
1579
1585
  }
@@ -1608,14 +1614,12 @@
1608
1614
  target[position++] = 0;
1609
1615
  target[position++] = 0;
1610
1616
  }
1611
- } else if (type === 'function') {
1612
- pack(this.writeFunction && this.writeFunction()); // if there is a writeFunction, use it, otherwise just encode as undefined
1613
1617
  } else {
1614
1618
  throw new Error('Unknown type: ' + type)
1615
1619
  }
1616
1620
  };
1617
1621
 
1618
- const writeObject = this.useRecords === false ? this.variableMapSize ? (object) => {
1622
+ const writePlainObject = this.variableMapSize ? (object) => {
1619
1623
  // this method is slightly slower, but generates "preferred serialization" (optimally small for smaller objects)
1620
1624
  let keys = Object.keys(object);
1621
1625
  let length = keys.length;
@@ -1650,7 +1654,9 @@
1650
1654
  }
1651
1655
  target[objectOffset++ + start] = size >> 8;
1652
1656
  target[objectOffset + start] = size & 0xff;
1653
- } :
1657
+ };
1658
+
1659
+ const writeRecord = this.useRecords === false ? writePlainObject :
1654
1660
  (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)
1655
1661
  (object, safePrototype) => {
1656
1662
  let nextTransition, transition = structures.transitions || (structures.transitions = Object.create(null));
@@ -1722,6 +1728,14 @@
1722
1728
  if (safePrototype || object.hasOwnProperty(key))
1723
1729
  pack(object[key]);
1724
1730
  };
1731
+
1732
+ // craete reference to useRecords if useRecords is a function
1733
+ const checkUseRecords = typeof this.useRecords == 'function' && this.useRecords;
1734
+
1735
+ const writeObject = checkUseRecords ? (object, safePrototype) => {
1736
+ checkUseRecords(object) ? writeRecord(object,safePrototype) : writePlainObject(object,safePrototype);
1737
+ } : writeRecord;
1738
+
1725
1739
  const makeRoom = (end) => {
1726
1740
  let newSize;
1727
1741
  if (end > 0x1000000) {