msgpackr 1.9.0 → 1.9.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/index.js CHANGED
@@ -118,10 +118,10 @@
118
118
  let size = source.length;
119
119
  let value = this ? this.unpack(source, size) : defaultUnpackr.unpack(source, size);
120
120
  if (forEach) {
121
- if (forEach(value) === false) return;
121
+ if (forEach(value, lastPosition, position$1) === false) return;
122
122
  while(position$1 < size) {
123
123
  lastPosition = position$1;
124
- if (forEach(checkedRead()) === false) {
124
+ if (forEach(checkedRead(), lastPosition, position$1) === false) {
125
125
  return
126
126
  }
127
127
  }
@@ -193,6 +193,10 @@
193
193
  position$1 = bundledStrings$1.postBundlePosition;
194
194
  bundledStrings$1 = null;
195
195
  }
196
+ if (sequentialMode)
197
+ // we only need to restore the structures if there was an error, but if we completed a read,
198
+ // we can clear this out and keep the structures we read
199
+ currentStructures.restoreStructures = null;
196
200
 
197
201
  if (position$1 == srcEnd) {
198
202
  // finished reading this source, cleanup references
@@ -915,7 +919,10 @@
915
919
  structure.highByte = highByte;
916
920
  }
917
921
  let existingStructure = currentStructures[id];
918
- if (existingStructure && existingStructure.isShared) {
922
+ // If it is a shared structure, we need to restore any changes after reading.
923
+ // Also in sequential mode, we may get incomplete reads and thus errors, and we need to restore
924
+ // to the state prior to an incomplete read in order to properly resume.
925
+ if (existingStructure && (existingStructure.isShared || sequentialMode)) {
919
926
  (currentStructures.restoreStructures || (currentStructures.restoreStructures = []))[id] = existingStructure;
920
927
  }
921
928
  currentStructures[id] = structure;
@@ -925,10 +932,10 @@
925
932
  currentExtensions[0] = () => {}; // notepack defines extension 0 to mean undefined, so use that as the default here
926
933
  currentExtensions[0].noBuffer = true;
927
934
 
928
- let global = typeof globalThis === 'object' ? globalThis : window;
935
+ let glbl = typeof globalThis === 'object' ? globalThis : window;
929
936
  currentExtensions[0x65] = () => {
930
937
  let data = read();
931
- return (global[data[0]] || Error)(data[1])
938
+ return (glbl[data[0]] || Error)(data[1])
932
939
  };
933
940
 
934
941
  currentExtensions[0x69] = (data) => {
@@ -972,7 +979,7 @@
972
979
  if (!typedArrayName)
973
980
  throw new Error('Could not find typed array for code ' + typeCode)
974
981
  // we have to always slice/copy here to get a new ArrayBuffer that is word/byte aligned
975
- return new global[typedArrayName](Uint8Array.prototype.slice.call(data, 1).buffer)
982
+ return new glbl[typedArrayName](Uint8Array.prototype.slice.call(data, 1).buffer)
976
983
  };
977
984
  currentExtensions[0x78] = () => {
978
985
  let data = read();
@@ -1252,7 +1259,7 @@
1252
1259
  if (serializationsSinceTransitionRebuild < 10)
1253
1260
  serializationsSinceTransitionRebuild++;
1254
1261
  let sharedLength = structures.sharedLength || 0;
1255
- if (structures.length > sharedLength)
1262
+ if (structures.length > sharedLength && !isSequential)
1256
1263
  structures.length = sharedLength;
1257
1264
  if (transitionsCount > 10000) {
1258
1265
  // force a rebuild occasionally after a lot of transitions so it can get cleaned up
@@ -1887,7 +1894,10 @@
1887
1894
  }
1888
1895
  }, {
1889
1896
  pack(set, allocateForWrite, pack) {
1890
- if (this.setAsEmptyObject) return pack({})
1897
+ if (this.setAsEmptyObject) {
1898
+ allocateForWrite(0);
1899
+ return pack({})
1900
+ }
1891
1901
  let array = Array.from(set);
1892
1902
  let { target, position} = allocateForWrite(this.moreTypes ? 3 : 0);
1893
1903
  if (this.moreTypes) {