msgpackr 2.0.0 → 2.0.1

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/unpack.js CHANGED
@@ -137,6 +137,8 @@ export class Unpackr {
137
137
  }
138
138
  }
139
139
  _mergeStructures(loadedStructures, existingStructures) {
140
+ if (this._onLoadedStructures)
141
+ loadedStructures = this._onLoadedStructures(loadedStructures);
140
142
  loadedStructures = loadedStructures || [];
141
143
  if (Object.isFrozen(loadedStructures))
142
144
  loadedStructures = loadedStructures.map(structure => structure.slice(0));
@@ -176,7 +178,15 @@ export function checkedRead(options) {
176
178
  if (sharedLength < currentStructures.length)
177
179
  currentStructures.length = sharedLength;
178
180
  }
179
- let result = read();
181
+ let result;
182
+ if (currentUnpackr._readStruct && src[position] < 0x40 && src[position] >= 0x20) {
183
+ result = currentUnpackr._readStruct(src, position, srcEnd);
184
+ src = null; // dispose of this so that recursive unpack calls don't save state
185
+ if (!(options && options.lazy) && result)
186
+ result = result.toJSON();
187
+ position = srcEnd;
188
+ } else
189
+ result = read();
180
190
  if (bundledStrings) { // bundled strings to skip past
181
191
  position = bundledStrings.postBundlePosition;
182
192
  bundledStrings = null;
@@ -1151,6 +1161,8 @@ currentExtensions[0xff] = (data) => {
1151
1161
  // currentExtensions[0x52] = () =>
1152
1162
 
1153
1163
  function saveState(callback) {
1164
+ if (currentUnpackr && currentUnpackr._onSaveState)
1165
+ currentUnpackr._onSaveState();
1154
1166
  let savedSrcEnd = srcEnd;
1155
1167
  let savedPosition = position;
1156
1168
  let savedStringPosition = stringPosition;
@@ -1220,3 +1232,7 @@ export function roundFloat32(float32Number) {
1220
1232
  let multiplier = mult10[((u8Array[3] & 0x7f) << 1) | (u8Array[2] >> 7)];
1221
1233
  return ((multiplier * float32Number + (float32Number > 0 ? 0.5 : -0.5)) >> 0) / multiplier;
1222
1234
  }
1235
+ // Marker for downstream libraries (e.g. structon) to detect per-instance
1236
+ // struct-decoding hooks (this._readStruct, this._onLoadedStructures,
1237
+ // this._onSaveState). See `checkedRead` for the dispatch.
1238
+ Unpackr.SUPPORTS_STRUCT_HOOKS = true;