msgpackr 1.9.1 → 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/node.cjs CHANGED
@@ -122,10 +122,10 @@ class Unpackr {
122
122
  let size = source.length;
123
123
  let value = this ? this.unpack(source, size) : defaultUnpackr.unpack(source, size);
124
124
  if (forEach) {
125
- if (forEach(value) === false) return;
125
+ if (forEach(value, lastPosition, position$1) === false) return;
126
126
  while(position$1 < size) {
127
127
  lastPosition = position$1;
128
- if (forEach(checkedRead()) === false) {
128
+ if (forEach(checkedRead(), lastPosition, position$1) === false) {
129
129
  return
130
130
  }
131
131
  }
@@ -199,6 +199,10 @@ function checkedRead(options) {
199
199
  position$1 = bundledStrings$1.postBundlePosition;
200
200
  bundledStrings$1 = null;
201
201
  }
202
+ if (sequentialMode)
203
+ // we only need to restore the structures if there was an error, but if we completed a read,
204
+ // we can clear this out and keep the structures we read
205
+ currentStructures.restoreStructures = null;
202
206
 
203
207
  if (position$1 == srcEnd) {
204
208
  // finished reading this source, cleanup references
@@ -971,7 +975,10 @@ const recordDefinition = (id, highByte) => {
971
975
  structure.highByte = highByte;
972
976
  }
973
977
  let existingStructure = currentStructures[id];
974
- if (existingStructure && existingStructure.isShared) {
978
+ // If it is a shared structure, we need to restore any changes after reading.
979
+ // Also in sequential mode, we may get incomplete reads and thus errors, and we need to restore
980
+ // to the state prior to an incomplete read in order to properly resume.
981
+ if (existingStructure && (existingStructure.isShared || sequentialMode)) {
975
982
  (currentStructures.restoreStructures || (currentStructures.restoreStructures = []))[id] = existingStructure;
976
983
  }
977
984
  currentStructures[id] = structure;
@@ -981,10 +988,10 @@ const recordDefinition = (id, highByte) => {
981
988
  currentExtensions[0] = () => {}; // notepack defines extension 0 to mean undefined, so use that as the default here
982
989
  currentExtensions[0].noBuffer = true;
983
990
 
984
- let glbl = typeof globalThis === 'object' ? globalThis : window;
991
+ let errors = { Error, TypeError, ReferenceError };
985
992
  currentExtensions[0x65] = () => {
986
993
  let data = read();
987
- return (glbl[data[0]] || Error)(data[1])
994
+ return (errors[data[0]] || Error)(data[1])
988
995
  };
989
996
 
990
997
  currentExtensions[0x69] = (data) => {
@@ -1022,6 +1029,7 @@ currentExtensions[0x73] = () => new Set(read());
1022
1029
 
1023
1030
  const typedArrays = ['Int8','Uint8','Uint8Clamped','Int16','Uint16','Int32','Uint32','Float32','Float64','BigInt64','BigUint64'].map(type => type + 'Array');
1024
1031
 
1032
+ let glbl = typeof globalThis === 'object' ? globalThis : window;
1025
1033
  currentExtensions[0x74] = (data) => {
1026
1034
  let typeCode = data[0];
1027
1035
  let typedArrayName = typedArrays[typeCode];
@@ -1319,7 +1327,7 @@ class Packr extends Unpackr {
1319
1327
  if (serializationsSinceTransitionRebuild < 10)
1320
1328
  serializationsSinceTransitionRebuild++;
1321
1329
  let sharedLength = structures.sharedLength || 0;
1322
- if (structures.length > sharedLength)
1330
+ if (structures.length > sharedLength && !isSequential)
1323
1331
  structures.length = sharedLength;
1324
1332
  if (transitionsCount > 10000) {
1325
1333
  // force a rebuild occasionally after a lot of transitions so it can get cleaned up
@@ -1527,7 +1535,7 @@ class Packr extends Unpackr {
1527
1535
  targetView.setFloat64(position, value);
1528
1536
  position += 8;
1529
1537
  }
1530
- } else if (type === 'object') {
1538
+ } else if (type === 'object' || type === 'function') {
1531
1539
  if (!value)
1532
1540
  target[position++] = 0xc0;
1533
1541
  else {
@@ -1634,6 +1642,11 @@ class Packr extends Unpackr {
1634
1642
  } else {
1635
1643
  if (value.toJSON) // use this as an alternate mechanism for expressing how to serialize
1636
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
+
1637
1650
  // no extension found, write as object
1638
1651
  writeObject(value, !value.hasOwnProperty); // if it doesn't have hasOwnProperty, don't do hasOwnProperty checks
1639
1652
  }
@@ -1668,14 +1681,12 @@ class Packr extends Unpackr {
1668
1681
  target[position++] = 0;
1669
1682
  target[position++] = 0;
1670
1683
  }
1671
- } else if (type === 'function') {
1672
- pack(this.writeFunction && this.writeFunction()); // if there is a writeFunction, use it, otherwise just encode as undefined
1673
1684
  } else {
1674
1685
  throw new Error('Unknown type: ' + type)
1675
1686
  }
1676
1687
  };
1677
1688
 
1678
- const writeObject = this.useRecords === false ? this.variableMapSize ? (object) => {
1689
+ const writePlainObject = this.variableMapSize ? (object) => {
1679
1690
  // this method is slightly slower, but generates "preferred serialization" (optimally small for smaller objects)
1680
1691
  let keys = Object.keys(object);
1681
1692
  let length = keys.length;
@@ -1710,7 +1721,9 @@ class Packr extends Unpackr {
1710
1721
  }
1711
1722
  target[objectOffset++ + start] = size >> 8;
1712
1723
  target[objectOffset + start] = size & 0xff;
1713
- } :
1724
+ };
1725
+
1726
+ const writeRecord = this.useRecords === false ? writePlainObject :
1714
1727
  (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)
1715
1728
  (object, safePrototype) => {
1716
1729
  let nextTransition, transition = structures.transitions || (structures.transitions = Object.create(null));
@@ -1782,6 +1795,14 @@ class Packr extends Unpackr {
1782
1795
  if (safePrototype || object.hasOwnProperty(key))
1783
1796
  pack(object[key]);
1784
1797
  };
1798
+
1799
+ // craete reference to useRecords if useRecords is a function
1800
+ const checkUseRecords = typeof this.useRecords == 'function' && this.useRecords;
1801
+
1802
+ const writeObject = checkUseRecords ? (object, safePrototype) => {
1803
+ checkUseRecords(object) ? writeRecord(object,safePrototype) : writePlainObject(object,safePrototype);
1804
+ } : writeRecord;
1805
+
1785
1806
  const makeRoom = (end) => {
1786
1807
  let newSize;
1787
1808
  if (end > 0x1000000) {
@@ -1954,7 +1975,10 @@ extensions = [{
1954
1975
  }
1955
1976
  }, {
1956
1977
  pack(set, allocateForWrite, pack) {
1957
- if (this.setAsEmptyObject) return pack({})
1978
+ if (this.setAsEmptyObject) {
1979
+ allocateForWrite(0);
1980
+ return pack({})
1981
+ }
1958
1982
  let array = Array.from(set);
1959
1983
  let { target, position} = allocateForWrite(this.moreTypes ? 3 : 0);
1960
1984
  if (this.moreTypes) {