msgpackr 1.7.0-alpha3 → 1.7.0-alpha4

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/test.js CHANGED
@@ -141,6 +141,8 @@
141
141
  if (onLoadedStructures)
142
142
  loadedStructures = onLoadedStructures.call(this, loadedStructures);
143
143
  loadedStructures = loadedStructures || [];
144
+ if (Object.isFrozen(loadedStructures))
145
+ loadedStructures = loadedStructures.map(structure => structure.slice(0));
144
146
  for (let i = 0, l = loadedStructures.length; i < l; i++) {
145
147
  let structure = loadedStructures[i];
146
148
  if (structure) {
@@ -1045,7 +1047,7 @@
1045
1047
  let position$1 = 0;
1046
1048
  let safeEnd;
1047
1049
  let bundledStrings$1 = null;
1048
- let writeStructSlots, prepareStructures;
1050
+ let writeStructSlots;
1049
1051
  const MAX_BUNDLE_SIZE = 0xf000;
1050
1052
  const hasNonLatin = /[\u0080-\uFFFF]/;
1051
1053
  const RECORD_SYMBOL = Symbol('record-id');
@@ -1178,7 +1180,7 @@
1178
1180
  if (structures) {
1179
1181
  if (serializationsSinceTransitionRebuild < 10)
1180
1182
  serializationsSinceTransitionRebuild++;
1181
- let sharedLength = structures.sharedLength || maxSharedStructures;
1183
+ let sharedLength = structures.sharedLength || 0;
1182
1184
  if (structures.length > sharedLength)
1183
1185
  structures.length = sharedLength;
1184
1186
  if (transitionsCount > 10000) {
@@ -1197,10 +1199,9 @@
1197
1199
  if (hasSharedUpdate && packr.saveStructures) {
1198
1200
  // we can't rely on start/end with REUSE_BUFFER_MODE since they will (probably) change when we save
1199
1201
  let returnBuffer = target.subarray(start, position$1);
1200
- let newSharedData = prepareStructures ? prepareStructures(structures, packr) : structures;
1201
- if (packr.saveStructures(newSharedData, newSharedData.isCompatible || packr.lastNamedStructuresLength || 0) === false) {
1202
+ let newSharedData = prepareStructures(structures, packr);
1203
+ if (packr.saveStructures(newSharedData, newSharedData.isCompatible) === false) {
1202
1204
  // get updated structures and try again if the update failed
1203
- packr._mergeStructures(packr.getStructures());
1204
1205
  return packr.pack(value)
1205
1206
  }
1206
1207
  packr.lastNamedStructuresLength = sharedLength;
@@ -1723,7 +1724,7 @@
1723
1724
  const writeStruct = (object, safePrototype) => {
1724
1725
  let newPosition = writeStructSlots(object, target, position$1, structures, makeRoom, (value, newPosition, notifySharedUpdate) => {
1725
1726
  if (notifySharedUpdate)
1726
- hasSharedUpdate = true;
1727
+ return hasSharedUpdate = true;
1727
1728
  position$1 = newPosition;
1728
1729
  if (start > 0) {
1729
1730
  pack(value);
@@ -1956,6 +1957,15 @@
1956
1957
  pack(writeStrings[1]);
1957
1958
  }
1958
1959
  }
1960
+ function prepareStructures(structures, packr) {
1961
+ structures.isCompatible = (existingStructures) => {
1962
+ let compatible = !existingStructures || ((packr.lastNamedStructuresLength || 0) === existingStructures.length);
1963
+ if (!compatible) // we want to merge these existing structures immediately since we already have it and we are in the right transaction
1964
+ packr._mergeStructures(existingStructures);
1965
+ return compatible;
1966
+ };
1967
+ return structures
1968
+ }
1959
1969
  function setWriteStructSlots(writeSlots, makeStructures) {
1960
1970
  writeStructSlots = writeSlots;
1961
1971
  prepareStructures = makeStructures;
@@ -1987,7 +1997,6 @@
1987
1997
  function writeStruct(object, target, position, structures, makeRoom, pack, packr) {
1988
1998
  let typedStructs = packr.typedStructs || (packr.typedStructs = []);
1989
1999
  // note that we rely on pack.js to load stored structures before we get to this point
1990
- packr.lastTypedStructuresLength = typedStructs.length;
1991
2000
  let targetView = target.dataView;
1992
2001
  let refsStartPosition = (typedStructs.lastStringStart || 100) + position;
1993
2002
  let safeEnd = target.length - 10;
@@ -2339,6 +2348,8 @@
2339
2348
  if (!(sharedData instanceof Map))
2340
2349
  return sharedData;
2341
2350
  let typed = sharedData.get('typed') || [];
2351
+ if (Object.isFrozen(typed))
2352
+ typed = typed.map(structure => structure.slice(0));
2342
2353
  let named = sharedData.get('named');
2343
2354
  let transitions = Object.create(null);
2344
2355
  for (let i = 0, l = typed.length; i < l; i++) {
@@ -2366,6 +2377,7 @@
2366
2377
  }
2367
2378
  typed.transitions = transitions;
2368
2379
  this.typedStructs = typed;
2380
+ this.lastTypedStructuresLength = typed.length;
2369
2381
  return named;
2370
2382
  }
2371
2383
  var sourceSymbol = Symbol('source');
@@ -2397,7 +2409,7 @@
2397
2409
  }
2398
2410
  var construct = structure.construct;
2399
2411
  if (!construct) {
2400
- construct = structure.construct = function() {
2412
+ construct = structure.construct = function LazyObject() {
2401
2413
  };
2402
2414
  var prototype = construct.prototype;
2403
2415
  Object.defineProperty(prototype, 'toJSON', {
@@ -2479,7 +2491,7 @@
2479
2491
  next = next.next;
2480
2492
  }
2481
2493
  if (end == null)
2482
- end = srcEnd - refStart;
2494
+ end = source.srcEnd - refStart;
2483
2495
  if (source.srcString) {
2484
2496
  return source.srcString.slice(ref, end);
2485
2497
  }
@@ -2495,7 +2507,7 @@
2495
2507
  asciiEnd = null;
2496
2508
  } while((next = next.next));
2497
2509
  if (asciiEnd == null)
2498
- asciiEnd = srcEnd - refStart
2510
+ asciiEnd = source.srcEnd - refStart
2499
2511
  source.srcString = src.toString('latin1', refStart, refStart + asciiEnd);
2500
2512
  return source.srcString.slice(ref, end);
2501
2513
  }
@@ -2530,7 +2542,7 @@
2530
2542
  next = next.next;
2531
2543
  }
2532
2544
  if (end == null)
2533
- end = srcEnd - refStart;
2545
+ end = source.srcEnd - refStart;
2534
2546
  if (type === UTF8) {
2535
2547
  return src.toString('utf8', ref + refStart, end + refStart);
2536
2548
  } else {
@@ -2614,18 +2626,21 @@
2614
2626
  structMap.set('typed', packr.typedStructs);
2615
2627
  let lastTypedStructuresLength = packr.lastTypedStructuresLength || 0;
2616
2628
  structMap.isCompatible = existing => {
2629
+ let compatible = true;
2617
2630
  if (existing instanceof Map) {
2618
2631
  let named = existing.get('named') || [];
2619
2632
  if (named.length !== (packr.lastNamedStructuresLength || 0))
2620
- return false;
2633
+ compatible = false;
2621
2634
  let typed = existing.get('typed') || [];
2622
2635
  if (typed.length !== lastTypedStructuresLength)
2623
- return false;
2636
+ compatible = false;
2624
2637
  } else if (existing instanceof Array) {
2625
2638
  if (existing.length !== (packr.lastNamedStructuresLength || 0))
2626
- return false;
2639
+ compatible = false;
2627
2640
  }
2628
- return true;
2641
+ if (!compatible)
2642
+ packr._mergeStructures(existing);
2643
+ return compatible;
2629
2644
  };
2630
2645
  packr.lastTypedStructuresLength = packr.typedStructs?.length;
2631
2646
  return structMap;