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/README.md CHANGED
@@ -157,6 +157,18 @@ unpackMultiple(data, (value) => {
157
157
  })
158
158
  ```
159
159
 
160
+ If you need to know the start and end offsets of the unpacked values, these are
161
+ provided as optional parameters in the callback:
162
+ ```js
163
+ let data = new Uint8Array([1, 2, 3]) // encodings of values 1, 2, and 3
164
+ unpackMultiple(data, (value,start,end) => {
165
+ // called for each value
166
+ // `start` is the data buffer offset where the value was read from
167
+ // `end` is `start` plus the byte length of the encoded value
168
+ // return false if you wish to end the parsing
169
+ })
170
+ ```
171
+
160
172
  ## Options
161
173
  The following options properties can be provided to the Packr or Unpackr constructor:
162
174
 
@@ -119,10 +119,10 @@
119
119
  let size = source.length;
120
120
  let value = this ? this.unpack(source, size) : defaultUnpackr.unpack(source, size);
121
121
  if (forEach) {
122
- if (forEach(value) === false) return;
122
+ if (forEach(value, lastPosition, position$1) === false) return;
123
123
  while(position$1 < size) {
124
124
  lastPosition = position$1;
125
- if (forEach(checkedRead()) === false) {
125
+ if (forEach(checkedRead(), lastPosition, position$1) === false) {
126
126
  return
127
127
  }
128
128
  }
@@ -194,6 +194,10 @@
194
194
  position$1 = bundledStrings$1.postBundlePosition;
195
195
  bundledStrings$1 = null;
196
196
  }
197
+ if (sequentialMode)
198
+ // we only need to restore the structures if there was an error, but if we completed a read,
199
+ // we can clear this out and keep the structures we read
200
+ currentStructures.restoreStructures = null;
197
201
 
198
202
  if (position$1 == srcEnd) {
199
203
  // finished reading this source, cleanup references
@@ -916,7 +920,10 @@
916
920
  structure.highByte = highByte;
917
921
  }
918
922
  let existingStructure = currentStructures[id];
919
- if (existingStructure && existingStructure.isShared) {
923
+ // If it is a shared structure, we need to restore any changes after reading.
924
+ // Also in sequential mode, we may get incomplete reads and thus errors, and we need to restore
925
+ // to the state prior to an incomplete read in order to properly resume.
926
+ if (existingStructure && (existingStructure.isShared || sequentialMode)) {
920
927
  (currentStructures.restoreStructures || (currentStructures.restoreStructures = []))[id] = existingStructure;
921
928
  }
922
929
  currentStructures[id] = structure;
@@ -926,10 +933,10 @@
926
933
  currentExtensions[0] = () => {}; // notepack defines extension 0 to mean undefined, so use that as the default here
927
934
  currentExtensions[0].noBuffer = true;
928
935
 
929
- let global = typeof globalThis === 'object' ? globalThis : window;
936
+ let glbl = typeof globalThis === 'object' ? globalThis : window;
930
937
  currentExtensions[0x65] = () => {
931
938
  let data = read();
932
- return (global[data[0]] || Error)(data[1])
939
+ return (glbl[data[0]] || Error)(data[1])
933
940
  };
934
941
 
935
942
  currentExtensions[0x69] = (data) => {
@@ -973,7 +980,7 @@
973
980
  if (!typedArrayName)
974
981
  throw new Error('Could not find typed array for code ' + typeCode)
975
982
  // we have to always slice/copy here to get a new ArrayBuffer that is word/byte aligned
976
- return new global[typedArrayName](Uint8Array.prototype.slice.call(data, 1).buffer)
983
+ return new glbl[typedArrayName](Uint8Array.prototype.slice.call(data, 1).buffer)
977
984
  };
978
985
  currentExtensions[0x78] = () => {
979
986
  let data = read();
@@ -1253,7 +1260,7 @@
1253
1260
  if (serializationsSinceTransitionRebuild < 10)
1254
1261
  serializationsSinceTransitionRebuild++;
1255
1262
  let sharedLength = structures.sharedLength || 0;
1256
- if (structures.length > sharedLength)
1263
+ if (structures.length > sharedLength && !isSequential)
1257
1264
  structures.length = sharedLength;
1258
1265
  if (transitionsCount > 10000) {
1259
1266
  // force a rebuild occasionally after a lot of transitions so it can get cleaned up
@@ -1888,7 +1895,10 @@
1888
1895
  }
1889
1896
  }, {
1890
1897
  pack(set, allocateForWrite, pack) {
1891
- if (this.setAsEmptyObject) return pack({})
1898
+ if (this.setAsEmptyObject) {
1899
+ allocateForWrite(0);
1900
+ return pack({})
1901
+ }
1892
1902
  let array = Array.from(set);
1893
1903
  let { target, position} = allocateForWrite(this.moreTypes ? 3 : 0);
1894
1904
  if (this.moreTypes) {