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/node.cjs CHANGED
@@ -100,7 +100,7 @@ class Unpackr {
100
100
  currentUnpackr = this;
101
101
  if (this.structures) {
102
102
  currentStructures = this.structures;
103
- return checkedRead()
103
+ return checkedRead(options)
104
104
  } else if (!currentStructures || currentStructures.length > 0) {
105
105
  currentStructures = [];
106
106
  }
@@ -109,7 +109,7 @@ class Unpackr {
109
109
  if (!currentStructures || currentStructures.length > 0)
110
110
  currentStructures = [];
111
111
  }
112
- return checkedRead()
112
+ return checkedRead(options)
113
113
  }
114
114
  unpackMultiple(source, forEach) {
115
115
  let values, lastPosition = 0;
@@ -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) {
@@ -173,7 +175,7 @@ class Unpackr {
173
175
  return this.unpack(source, end)
174
176
  }
175
177
  }
176
- function checkedRead() {
178
+ function checkedRead(options) {
177
179
  try {
178
180
  if (!currentUnpackr.trusted && !sequentialMode) {
179
181
  let sharedLength = currentStructures.sharedLength || 0;
@@ -183,6 +185,8 @@ function checkedRead() {
183
185
  let result;
184
186
  if (currentUnpackr.randomAccessStructure && src[position] < 0x40 && src[position] >= 0x20 && readStruct) {
185
187
  result = readStruct(src, position, srcEnd, currentUnpackr);
188
+ if (!(options && options.lazy) && result)
189
+ result = result.toJSON();
186
190
  position = srcEnd;
187
191
  } else
188
192
  result = read();
@@ -1119,7 +1123,7 @@ let targetView;
1119
1123
  let position$1 = 0;
1120
1124
  let safeEnd;
1121
1125
  let bundledStrings$1 = null;
1122
- let writeStructSlots, prepareStructures;
1126
+ let writeStructSlots;
1123
1127
  const MAX_BUNDLE_SIZE = 0xf000;
1124
1128
  const hasNonLatin = /[\u0080-\uFFFF]/;
1125
1129
  const RECORD_SYMBOL = Symbol('record-id');
@@ -1252,7 +1256,7 @@ class Packr extends Unpackr {
1252
1256
  if (structures) {
1253
1257
  if (serializationsSinceTransitionRebuild < 10)
1254
1258
  serializationsSinceTransitionRebuild++;
1255
- let sharedLength = structures.sharedLength || maxSharedStructures;
1259
+ let sharedLength = structures.sharedLength || 0;
1256
1260
  if (structures.length > sharedLength)
1257
1261
  structures.length = sharedLength;
1258
1262
  if (transitionsCount > 10000) {
@@ -1271,10 +1275,9 @@ class Packr extends Unpackr {
1271
1275
  if (hasSharedUpdate && packr.saveStructures) {
1272
1276
  // we can't rely on start/end with REUSE_BUFFER_MODE since they will (probably) change when we save
1273
1277
  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) {
1278
+ let newSharedData = prepareStructures(structures, packr);
1279
+ if (packr.saveStructures(newSharedData, newSharedData.isCompatible) === false) {
1276
1280
  // get updated structures and try again if the update failed
1277
- packr._mergeStructures(packr.getStructures());
1278
1281
  return packr.pack(value)
1279
1282
  }
1280
1283
  packr.lastNamedStructuresLength = sharedLength;
@@ -1797,7 +1800,7 @@ class Packr extends Unpackr {
1797
1800
  const writeStruct = (object, safePrototype) => {
1798
1801
  let newPosition = writeStructSlots(object, target, position$1, structures, makeRoom, (value, newPosition, notifySharedUpdate) => {
1799
1802
  if (notifySharedUpdate)
1800
- hasSharedUpdate = true;
1803
+ return hasSharedUpdate = true;
1801
1804
  position$1 = newPosition;
1802
1805
  if (start > 0) {
1803
1806
  pack(value);
@@ -2042,6 +2045,15 @@ function addExtension$1(extension) {
2042
2045
  }
2043
2046
  addExtension(extension);
2044
2047
  }
2048
+ function prepareStructures(structures, packr) {
2049
+ structures.isCompatible = (existingStructures) => {
2050
+ let compatible = !existingStructures || ((packr.lastNamedStructuresLength || 0) === existingStructures.length);
2051
+ if (!compatible) // we want to merge these existing structures immediately since we already have it and we are in the right transaction
2052
+ packr._mergeStructures(existingStructures);
2053
+ return compatible;
2054
+ };
2055
+ return structures
2056
+ }
2045
2057
  function setWriteStructSlots(writeSlots, makeStructures) {
2046
2058
  writeStructSlots = writeSlots;
2047
2059
  prepareStructures = makeStructures;
@@ -2077,7 +2089,6 @@ setWriteStructSlots(writeStruct, prepareStructures$1);
2077
2089
  function writeStruct(object, target, position, structures, makeRoom, pack, packr) {
2078
2090
  let typedStructs = packr.typedStructs || (packr.typedStructs = []);
2079
2091
  // note that we rely on pack.js to load stored structures before we get to this point
2080
- packr.lastTypedStructuresLength = typedStructs.length;
2081
2092
  let targetView = target.dataView;
2082
2093
  let refsStartPosition = (typedStructs.lastStringStart || 100) + position;
2083
2094
  let safeEnd = target.length - 10;
@@ -2213,7 +2224,7 @@ function writeStruct(object, target, position, structures, makeRoom, pack, packr
2213
2224
  refPosition += encodeUtf8(target, value, refPosition);
2214
2225
  isNotAscii = refPosition - strStart > strLength;
2215
2226
  }
2216
- if (refOffset < 0x100) {
2227
+ if (refOffset < 0xf6) {
2217
2228
  if (isNotAscii)
2218
2229
  transition = nextTransition.string8 || createTypeTransition(nextTransition, UTF8, 1);
2219
2230
  else
@@ -2321,7 +2332,7 @@ function writeStruct(object, target, position, structures, makeRoom, pack, packr
2321
2332
  targetView.setUint32(position, refOffset, true);
2322
2333
  position += 4;
2323
2334
  }
2324
- } else {
2335
+ } else { // null or undefined
2325
2336
  transition = nextTransition.object16 || createTypeTransition(nextTransition, OBJECT_DATA, 2);
2326
2337
  targetView.setInt16(position, value === null ? -10 : -9, true);
2327
2338
  position += 2;
@@ -2429,6 +2440,8 @@ function onLoadedStructures$1(sharedData) {
2429
2440
  if (!(sharedData instanceof Map))
2430
2441
  return sharedData;
2431
2442
  let typed = sharedData.get('typed') || [];
2443
+ if (Object.isFrozen(typed))
2444
+ typed = typed.map(structure => structure.slice(0));
2432
2445
  let named = sharedData.get('named');
2433
2446
  let transitions = Object.create(null);
2434
2447
  for (let i = 0, l = typed.length; i < l; i++) {
@@ -2456,6 +2469,7 @@ function onLoadedStructures$1(sharedData) {
2456
2469
  }
2457
2470
  typed.transitions = transitions;
2458
2471
  this.typedStructs = typed;
2472
+ this.lastTypedStructuresLength = typed.length;
2459
2473
  return named;
2460
2474
  }
2461
2475
  var sourceSymbol = Symbol('source');
@@ -2487,15 +2501,16 @@ function readStruct$1(src, position, srcEnd, unpackr) {
2487
2501
  }
2488
2502
  var construct = structure.construct;
2489
2503
  if (!construct) {
2490
- construct = structure.construct = function() {
2504
+ construct = structure.construct = function LazyObject() {
2491
2505
  };
2492
2506
  var prototype = construct.prototype;
2507
+ let properties = [];
2493
2508
  Object.defineProperty(prototype, 'toJSON', {
2494
- get() {
2509
+ value() {
2495
2510
  // return an enumerable object with own properties to JSON stringify
2496
2511
  let resolved = {};
2497
- for (let i = 0, l = structure.length; i < l; i++) {
2498
- let key = structure[i];
2512
+ for (let i = 0, l = properties.length; i < l; i++) {
2513
+ let key = properties[i].key;
2499
2514
  resolved[key] = this[key];
2500
2515
  }
2501
2516
  return resolved;
@@ -2504,7 +2519,6 @@ function readStruct$1(src, position, srcEnd, unpackr) {
2504
2519
  });
2505
2520
  let currentOffset = 0;
2506
2521
  let lastRefProperty;
2507
- let properties = [];
2508
2522
  for (let i = 0, l = structure.length; i < l; i++) {
2509
2523
  let definition = structure[i];
2510
2524
  let [ type, size, key, enumerationOffset ] = definition;
@@ -2569,7 +2583,7 @@ function readStruct$1(src, position, srcEnd, unpackr) {
2569
2583
  next = next.next;
2570
2584
  }
2571
2585
  if (end == null)
2572
- end = srcEnd - refStart;
2586
+ end = source.srcEnd - refStart;
2573
2587
  if (source.srcString) {
2574
2588
  return source.srcString.slice(ref, end);
2575
2589
  }
@@ -2585,7 +2599,7 @@ function readStruct$1(src, position, srcEnd, unpackr) {
2585
2599
  asciiEnd = null;
2586
2600
  } while((next = next.next));
2587
2601
  if (asciiEnd == null)
2588
- asciiEnd = srcEnd - refStart
2602
+ asciiEnd = source.srcEnd - refStart
2589
2603
  source.srcString = src.toString('latin1', refStart, refStart + asciiEnd);
2590
2604
  return source.srcString.slice(ref, end);
2591
2605
  }
@@ -2620,7 +2634,7 @@ function readStruct$1(src, position, srcEnd, unpackr) {
2620
2634
  next = next.next;
2621
2635
  }
2622
2636
  if (end == null)
2623
- end = srcEnd - refStart;
2637
+ end = source.srcEnd - refStart;
2624
2638
  if (type === UTF8) {
2625
2639
  return src.toString('utf8', ref + refStart, end + refStart);
2626
2640
  } else {
@@ -2697,28 +2711,32 @@ function toConstant(code) {
2697
2711
  throw new Error('Unknown constant');
2698
2712
  }
2699
2713
  function prepareStructures$1(structures, packr) {
2700
- if (!packr.typedStructs)
2701
- return structures;
2702
- let structMap = new Map();
2703
- structMap.set('named', structures);
2704
- structMap.set('typed', packr.typedStructs);
2714
+ if (packr.typedStructs) {
2715
+ let structMap = new Map();
2716
+ structMap.set('named', structures);
2717
+ structMap.set('typed', packr.typedStructs);
2718
+ structures = structMap;
2719
+ }
2705
2720
  let lastTypedStructuresLength = packr.lastTypedStructuresLength || 0;
2706
- structMap.isCompatible = existing => {
2721
+ structures.isCompatible = existing => {
2722
+ let compatible = true;
2707
2723
  if (existing instanceof Map) {
2708
2724
  let named = existing.get('named') || [];
2709
2725
  if (named.length !== (packr.lastNamedStructuresLength || 0))
2710
- return false;
2726
+ compatible = false;
2711
2727
  let typed = existing.get('typed') || [];
2712
2728
  if (typed.length !== lastTypedStructuresLength)
2713
- return false;
2729
+ compatible = false;
2714
2730
  } else if (existing instanceof Array) {
2715
2731
  if (existing.length !== (packr.lastNamedStructuresLength || 0))
2716
- return false;
2732
+ compatible = false;
2717
2733
  }
2718
- return true;
2734
+ if (!compatible)
2735
+ packr._mergeStructures(existing);
2736
+ return compatible;
2719
2737
  };
2720
2738
  packr.lastTypedStructuresLength = packr.typedStructs?.length;
2721
- return structMap;
2739
+ return structures;
2722
2740
  }
2723
2741
 
2724
2742
  setReadStruct(readStruct$1, onLoadedStructures$1);