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/node.cjs CHANGED
@@ -147,6 +147,8 @@ class Unpackr {
147
147
  if (onLoadedStructures)
148
148
  loadedStructures = onLoadedStructures.call(this, loadedStructures);
149
149
  loadedStructures = loadedStructures || [];
150
+ if (Object.isFrozen(loadedStructures))
151
+ loadedStructures = loadedStructures.map(structure => structure.slice(0));
150
152
  for (let i = 0, l = loadedStructures.length; i < l; i++) {
151
153
  let structure = loadedStructures[i];
152
154
  if (structure) {
@@ -1119,7 +1121,7 @@ let targetView;
1119
1121
  let position$1 = 0;
1120
1122
  let safeEnd;
1121
1123
  let bundledStrings$1 = null;
1122
- let writeStructSlots, prepareStructures;
1124
+ let writeStructSlots;
1123
1125
  const MAX_BUNDLE_SIZE = 0xf000;
1124
1126
  const hasNonLatin = /[\u0080-\uFFFF]/;
1125
1127
  const RECORD_SYMBOL = Symbol('record-id');
@@ -1252,7 +1254,7 @@ class Packr extends Unpackr {
1252
1254
  if (structures) {
1253
1255
  if (serializationsSinceTransitionRebuild < 10)
1254
1256
  serializationsSinceTransitionRebuild++;
1255
- let sharedLength = structures.sharedLength || maxSharedStructures;
1257
+ let sharedLength = structures.sharedLength || 0;
1256
1258
  if (structures.length > sharedLength)
1257
1259
  structures.length = sharedLength;
1258
1260
  if (transitionsCount > 10000) {
@@ -1271,10 +1273,9 @@ class Packr extends Unpackr {
1271
1273
  if (hasSharedUpdate && packr.saveStructures) {
1272
1274
  // we can't rely on start/end with REUSE_BUFFER_MODE since they will (probably) change when we save
1273
1275
  let returnBuffer = target.subarray(start, position$1);
1274
- let newSharedData = prepareStructures ? prepareStructures(structures, packr) : structures;
1275
- if (packr.saveStructures(newSharedData, newSharedData.isCompatible || packr.lastNamedStructuresLength || 0) === false) {
1276
+ let newSharedData = prepareStructures(structures, packr);
1277
+ if (packr.saveStructures(newSharedData, newSharedData.isCompatible) === false) {
1276
1278
  // get updated structures and try again if the update failed
1277
- packr._mergeStructures(packr.getStructures());
1278
1279
  return packr.pack(value)
1279
1280
  }
1280
1281
  packr.lastNamedStructuresLength = sharedLength;
@@ -1797,7 +1798,7 @@ class Packr extends Unpackr {
1797
1798
  const writeStruct = (object, safePrototype) => {
1798
1799
  let newPosition = writeStructSlots(object, target, position$1, structures, makeRoom, (value, newPosition, notifySharedUpdate) => {
1799
1800
  if (notifySharedUpdate)
1800
- hasSharedUpdate = true;
1801
+ return hasSharedUpdate = true;
1801
1802
  position$1 = newPosition;
1802
1803
  if (start > 0) {
1803
1804
  pack(value);
@@ -2042,6 +2043,15 @@ function addExtension$1(extension) {
2042
2043
  }
2043
2044
  addExtension(extension);
2044
2045
  }
2046
+ function prepareStructures(structures, packr) {
2047
+ structures.isCompatible = (existingStructures) => {
2048
+ let compatible = !existingStructures || ((packr.lastNamedStructuresLength || 0) === existingStructures.length);
2049
+ if (!compatible) // we want to merge these existing structures immediately since we already have it and we are in the right transaction
2050
+ packr._mergeStructures(existingStructures);
2051
+ return compatible;
2052
+ };
2053
+ return structures
2054
+ }
2045
2055
  function setWriteStructSlots(writeSlots, makeStructures) {
2046
2056
  writeStructSlots = writeSlots;
2047
2057
  prepareStructures = makeStructures;
@@ -2077,7 +2087,6 @@ setWriteStructSlots(writeStruct, prepareStructures$1);
2077
2087
  function writeStruct(object, target, position, structures, makeRoom, pack, packr) {
2078
2088
  let typedStructs = packr.typedStructs || (packr.typedStructs = []);
2079
2089
  // note that we rely on pack.js to load stored structures before we get to this point
2080
- packr.lastTypedStructuresLength = typedStructs.length;
2081
2090
  let targetView = target.dataView;
2082
2091
  let refsStartPosition = (typedStructs.lastStringStart || 100) + position;
2083
2092
  let safeEnd = target.length - 10;
@@ -2429,6 +2438,8 @@ function onLoadedStructures$1(sharedData) {
2429
2438
  if (!(sharedData instanceof Map))
2430
2439
  return sharedData;
2431
2440
  let typed = sharedData.get('typed') || [];
2441
+ if (Object.isFrozen(typed))
2442
+ typed = typed.map(structure => structure.slice(0));
2432
2443
  let named = sharedData.get('named');
2433
2444
  let transitions = Object.create(null);
2434
2445
  for (let i = 0, l = typed.length; i < l; i++) {
@@ -2456,6 +2467,7 @@ function onLoadedStructures$1(sharedData) {
2456
2467
  }
2457
2468
  typed.transitions = transitions;
2458
2469
  this.typedStructs = typed;
2470
+ this.lastTypedStructuresLength = typed.length;
2459
2471
  return named;
2460
2472
  }
2461
2473
  var sourceSymbol = Symbol('source');
@@ -2487,7 +2499,7 @@ function readStruct$1(src, position, srcEnd, unpackr) {
2487
2499
  }
2488
2500
  var construct = structure.construct;
2489
2501
  if (!construct) {
2490
- construct = structure.construct = function() {
2502
+ construct = structure.construct = function LazyObject() {
2491
2503
  };
2492
2504
  var prototype = construct.prototype;
2493
2505
  Object.defineProperty(prototype, 'toJSON', {
@@ -2569,7 +2581,7 @@ function readStruct$1(src, position, srcEnd, unpackr) {
2569
2581
  next = next.next;
2570
2582
  }
2571
2583
  if (end == null)
2572
- end = srcEnd - refStart;
2584
+ end = source.srcEnd - refStart;
2573
2585
  if (source.srcString) {
2574
2586
  return source.srcString.slice(ref, end);
2575
2587
  }
@@ -2585,7 +2597,7 @@ function readStruct$1(src, position, srcEnd, unpackr) {
2585
2597
  asciiEnd = null;
2586
2598
  } while((next = next.next));
2587
2599
  if (asciiEnd == null)
2588
- asciiEnd = srcEnd - refStart
2600
+ asciiEnd = source.srcEnd - refStart
2589
2601
  source.srcString = src.toString('latin1', refStart, refStart + asciiEnd);
2590
2602
  return source.srcString.slice(ref, end);
2591
2603
  }
@@ -2620,7 +2632,7 @@ function readStruct$1(src, position, srcEnd, unpackr) {
2620
2632
  next = next.next;
2621
2633
  }
2622
2634
  if (end == null)
2623
- end = srcEnd - refStart;
2635
+ end = source.srcEnd - refStart;
2624
2636
  if (type === UTF8) {
2625
2637
  return src.toString('utf8', ref + refStart, end + refStart);
2626
2638
  } else {
@@ -2704,18 +2716,21 @@ function prepareStructures$1(structures, packr) {
2704
2716
  structMap.set('typed', packr.typedStructs);
2705
2717
  let lastTypedStructuresLength = packr.lastTypedStructuresLength || 0;
2706
2718
  structMap.isCompatible = existing => {
2719
+ let compatible = true;
2707
2720
  if (existing instanceof Map) {
2708
2721
  let named = existing.get('named') || [];
2709
2722
  if (named.length !== (packr.lastNamedStructuresLength || 0))
2710
- return false;
2723
+ compatible = false;
2711
2724
  let typed = existing.get('typed') || [];
2712
2725
  if (typed.length !== lastTypedStructuresLength)
2713
- return false;
2726
+ compatible = false;
2714
2727
  } else if (existing instanceof Array) {
2715
2728
  if (existing.length !== (packr.lastNamedStructuresLength || 0))
2716
- return false;
2729
+ compatible = false;
2717
2730
  }
2718
- return true;
2731
+ if (!compatible)
2732
+ packr._mergeStructures(existing);
2733
+ return compatible;
2719
2734
  };
2720
2735
  packr.lastTypedStructuresLength = packr.typedStructs?.length;
2721
2736
  return structMap;