msgpackr 1.7.0-alpha3 → 1.7.0-alpha6

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
@@ -94,7 +94,7 @@
94
94
  currentUnpackr = this;
95
95
  if (this.structures) {
96
96
  currentStructures = this.structures;
97
- return checkedRead()
97
+ return checkedRead(options)
98
98
  } else if (!currentStructures || currentStructures.length > 0) {
99
99
  currentStructures = [];
100
100
  }
@@ -103,7 +103,7 @@
103
103
  if (!currentStructures || currentStructures.length > 0)
104
104
  currentStructures = [];
105
105
  }
106
- return checkedRead()
106
+ return checkedRead(options)
107
107
  }
108
108
  unpackMultiple(source, forEach) {
109
109
  let values, lastPosition = 0;
@@ -139,6 +139,8 @@
139
139
  }
140
140
  _mergeStructures(loadedStructures, existingStructures) {
141
141
  loadedStructures = loadedStructures || [];
142
+ if (Object.isFrozen(loadedStructures))
143
+ loadedStructures = loadedStructures.map(structure => structure.slice(0));
142
144
  for (let i = 0, l = loadedStructures.length; i < l; i++) {
143
145
  let structure = loadedStructures[i];
144
146
  if (structure) {
@@ -165,7 +167,7 @@
165
167
  return this.unpack(source, end)
166
168
  }
167
169
  }
168
- function checkedRead() {
170
+ function checkedRead(options) {
169
171
  try {
170
172
  if (!currentUnpackr.trusted && !sequentialMode) {
171
173
  let sharedLength = currentStructures.sharedLength || 0;
@@ -175,6 +177,8 @@
175
177
  let result;
176
178
  if (currentUnpackr.randomAccessStructure && src[position] < 0x40 && src[position] >= 0x20 && readStruct) {
177
179
  result = readStruct(src, position, srcEnd, currentUnpackr);
180
+ if (!(options && options.lazy) && result)
181
+ result = result.toJSON();
178
182
  position = srcEnd;
179
183
  } else
180
184
  result = read();
@@ -1187,7 +1191,7 @@
1187
1191
  if (structures) {
1188
1192
  if (serializationsSinceTransitionRebuild < 10)
1189
1193
  serializationsSinceTransitionRebuild++;
1190
- let sharedLength = structures.sharedLength || maxSharedStructures;
1194
+ let sharedLength = structures.sharedLength || 0;
1191
1195
  if (structures.length > sharedLength)
1192
1196
  structures.length = sharedLength;
1193
1197
  if (transitionsCount > 10000) {
@@ -1206,10 +1210,9 @@
1206
1210
  if (hasSharedUpdate && packr.saveStructures) {
1207
1211
  // we can't rely on start/end with REUSE_BUFFER_MODE since they will (probably) change when we save
1208
1212
  let returnBuffer = target.subarray(start, position$1);
1209
- let newSharedData = structures;
1210
- if (packr.saveStructures(newSharedData, newSharedData.isCompatible || packr.lastNamedStructuresLength || 0) === false) {
1213
+ let newSharedData = prepareStructures(structures, packr);
1214
+ if (packr.saveStructures(newSharedData, newSharedData.isCompatible) === false) {
1211
1215
  // get updated structures and try again if the update failed
1212
- packr._mergeStructures(packr.getStructures());
1213
1216
  return packr.pack(value)
1214
1217
  }
1215
1218
  packr.lastNamedStructuresLength = sharedLength;
@@ -1966,6 +1969,15 @@
1966
1969
  }
1967
1970
  addExtension(extension);
1968
1971
  }
1972
+ function prepareStructures(structures, packr) {
1973
+ structures.isCompatible = (existingStructures) => {
1974
+ let compatible = !existingStructures || ((packr.lastNamedStructuresLength || 0) === existingStructures.length);
1975
+ if (!compatible) // we want to merge these existing structures immediately since we already have it and we are in the right transaction
1976
+ packr._mergeStructures(existingStructures);
1977
+ return compatible;
1978
+ };
1979
+ return structures
1980
+ }
1969
1981
 
1970
1982
  let defaultPackr = new Packr({ useRecords: false });
1971
1983
  const pack = defaultPackr.pack;