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/test.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;
@@ -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) {
@@ -167,7 +169,7 @@
167
169
  return this.unpack(source, end)
168
170
  }
169
171
  }
170
- function checkedRead() {
172
+ function checkedRead(options) {
171
173
  try {
172
174
  if (!currentUnpackr.trusted && !sequentialMode) {
173
175
  let sharedLength = currentStructures.sharedLength || 0;
@@ -177,6 +179,8 @@
177
179
  let result;
178
180
  if (currentUnpackr.randomAccessStructure && src[position] < 0x40 && src[position] >= 0x20 && readStruct) {
179
181
  result = readStruct(src, position, srcEnd, currentUnpackr);
182
+ if (!(options && options.lazy) && result)
183
+ result = result.toJSON();
180
184
  position = srcEnd;
181
185
  } else
182
186
  result = read();
@@ -1045,7 +1049,7 @@
1045
1049
  let position$1 = 0;
1046
1050
  let safeEnd;
1047
1051
  let bundledStrings$1 = null;
1048
- let writeStructSlots, prepareStructures;
1052
+ let writeStructSlots;
1049
1053
  const MAX_BUNDLE_SIZE = 0xf000;
1050
1054
  const hasNonLatin = /[\u0080-\uFFFF]/;
1051
1055
  const RECORD_SYMBOL = Symbol('record-id');
@@ -1178,7 +1182,7 @@
1178
1182
  if (structures) {
1179
1183
  if (serializationsSinceTransitionRebuild < 10)
1180
1184
  serializationsSinceTransitionRebuild++;
1181
- let sharedLength = structures.sharedLength || maxSharedStructures;
1185
+ let sharedLength = structures.sharedLength || 0;
1182
1186
  if (structures.length > sharedLength)
1183
1187
  structures.length = sharedLength;
1184
1188
  if (transitionsCount > 10000) {
@@ -1197,10 +1201,9 @@
1197
1201
  if (hasSharedUpdate && packr.saveStructures) {
1198
1202
  // we can't rely on start/end with REUSE_BUFFER_MODE since they will (probably) change when we save
1199
1203
  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) {
1204
+ let newSharedData = prepareStructures(structures, packr);
1205
+ if (packr.saveStructures(newSharedData, newSharedData.isCompatible) === false) {
1202
1206
  // get updated structures and try again if the update failed
1203
- packr._mergeStructures(packr.getStructures());
1204
1207
  return packr.pack(value)
1205
1208
  }
1206
1209
  packr.lastNamedStructuresLength = sharedLength;
@@ -1723,7 +1726,7 @@
1723
1726
  const writeStruct = (object, safePrototype) => {
1724
1727
  let newPosition = writeStructSlots(object, target, position$1, structures, makeRoom, (value, newPosition, notifySharedUpdate) => {
1725
1728
  if (notifySharedUpdate)
1726
- hasSharedUpdate = true;
1729
+ return hasSharedUpdate = true;
1727
1730
  position$1 = newPosition;
1728
1731
  if (start > 0) {
1729
1732
  pack(value);
@@ -1956,6 +1959,15 @@
1956
1959
  pack(writeStrings[1]);
1957
1960
  }
1958
1961
  }
1962
+ function prepareStructures(structures, packr) {
1963
+ structures.isCompatible = (existingStructures) => {
1964
+ let compatible = !existingStructures || ((packr.lastNamedStructuresLength || 0) === existingStructures.length);
1965
+ if (!compatible) // we want to merge these existing structures immediately since we already have it and we are in the right transaction
1966
+ packr._mergeStructures(existingStructures);
1967
+ return compatible;
1968
+ };
1969
+ return structures
1970
+ }
1959
1971
  function setWriteStructSlots(writeSlots, makeStructures) {
1960
1972
  writeStructSlots = writeSlots;
1961
1973
  prepareStructures = makeStructures;
@@ -1987,7 +1999,6 @@
1987
1999
  function writeStruct(object, target, position, structures, makeRoom, pack, packr) {
1988
2000
  let typedStructs = packr.typedStructs || (packr.typedStructs = []);
1989
2001
  // note that we rely on pack.js to load stored structures before we get to this point
1990
- packr.lastTypedStructuresLength = typedStructs.length;
1991
2002
  let targetView = target.dataView;
1992
2003
  let refsStartPosition = (typedStructs.lastStringStart || 100) + position;
1993
2004
  let safeEnd = target.length - 10;
@@ -2123,7 +2134,7 @@
2123
2134
  refPosition += encodeUtf8(target, value, refPosition);
2124
2135
  isNotAscii = refPosition - strStart > strLength;
2125
2136
  }
2126
- if (refOffset < 0x100) {
2137
+ if (refOffset < 0xf6) {
2127
2138
  if (isNotAscii)
2128
2139
  transition = nextTransition.string8 || createTypeTransition(nextTransition, UTF8, 1);
2129
2140
  else
@@ -2231,7 +2242,7 @@
2231
2242
  targetView.setUint32(position, refOffset, true);
2232
2243
  position += 4;
2233
2244
  }
2234
- } else {
2245
+ } else { // null or undefined
2235
2246
  transition = nextTransition.object16 || createTypeTransition(nextTransition, OBJECT_DATA, 2);
2236
2247
  targetView.setInt16(position, value === null ? -10 : -9, true);
2237
2248
  position += 2;
@@ -2339,6 +2350,8 @@
2339
2350
  if (!(sharedData instanceof Map))
2340
2351
  return sharedData;
2341
2352
  let typed = sharedData.get('typed') || [];
2353
+ if (Object.isFrozen(typed))
2354
+ typed = typed.map(structure => structure.slice(0));
2342
2355
  let named = sharedData.get('named');
2343
2356
  let transitions = Object.create(null);
2344
2357
  for (let i = 0, l = typed.length; i < l; i++) {
@@ -2366,6 +2379,7 @@
2366
2379
  }
2367
2380
  typed.transitions = transitions;
2368
2381
  this.typedStructs = typed;
2382
+ this.lastTypedStructuresLength = typed.length;
2369
2383
  return named;
2370
2384
  }
2371
2385
  var sourceSymbol = Symbol('source');
@@ -2397,15 +2411,16 @@
2397
2411
  }
2398
2412
  var construct = structure.construct;
2399
2413
  if (!construct) {
2400
- construct = structure.construct = function() {
2414
+ construct = structure.construct = function LazyObject() {
2401
2415
  };
2402
2416
  var prototype = construct.prototype;
2417
+ let properties = [];
2403
2418
  Object.defineProperty(prototype, 'toJSON', {
2404
- get() {
2419
+ value() {
2405
2420
  // return an enumerable object with own properties to JSON stringify
2406
2421
  let resolved = {};
2407
- for (let i = 0, l = structure.length; i < l; i++) {
2408
- let key = structure[i];
2422
+ for (let i = 0, l = properties.length; i < l; i++) {
2423
+ let key = properties[i].key;
2409
2424
  resolved[key] = this[key];
2410
2425
  }
2411
2426
  return resolved;
@@ -2414,7 +2429,6 @@
2414
2429
  });
2415
2430
  let currentOffset = 0;
2416
2431
  let lastRefProperty;
2417
- let properties = [];
2418
2432
  for (let i = 0, l = structure.length; i < l; i++) {
2419
2433
  let definition = structure[i];
2420
2434
  let [ type, size, key, enumerationOffset ] = definition;
@@ -2479,7 +2493,7 @@
2479
2493
  next = next.next;
2480
2494
  }
2481
2495
  if (end == null)
2482
- end = srcEnd - refStart;
2496
+ end = source.srcEnd - refStart;
2483
2497
  if (source.srcString) {
2484
2498
  return source.srcString.slice(ref, end);
2485
2499
  }
@@ -2495,7 +2509,7 @@
2495
2509
  asciiEnd = null;
2496
2510
  } while((next = next.next));
2497
2511
  if (asciiEnd == null)
2498
- asciiEnd = srcEnd - refStart
2512
+ asciiEnd = source.srcEnd - refStart
2499
2513
  source.srcString = src.toString('latin1', refStart, refStart + asciiEnd);
2500
2514
  return source.srcString.slice(ref, end);
2501
2515
  }
@@ -2530,7 +2544,7 @@
2530
2544
  next = next.next;
2531
2545
  }
2532
2546
  if (end == null)
2533
- end = srcEnd - refStart;
2547
+ end = source.srcEnd - refStart;
2534
2548
  if (type === UTF8) {
2535
2549
  return src.toString('utf8', ref + refStart, end + refStart);
2536
2550
  } else {
@@ -2607,28 +2621,32 @@
2607
2621
  throw new Error('Unknown constant');
2608
2622
  }
2609
2623
  function prepareStructures$1(structures, packr) {
2610
- if (!packr.typedStructs)
2611
- return structures;
2612
- let structMap = new Map();
2613
- structMap.set('named', structures);
2614
- structMap.set('typed', packr.typedStructs);
2624
+ if (packr.typedStructs) {
2625
+ let structMap = new Map();
2626
+ structMap.set('named', structures);
2627
+ structMap.set('typed', packr.typedStructs);
2628
+ structures = structMap;
2629
+ }
2615
2630
  let lastTypedStructuresLength = packr.lastTypedStructuresLength || 0;
2616
- structMap.isCompatible = existing => {
2631
+ structures.isCompatible = existing => {
2632
+ let compatible = true;
2617
2633
  if (existing instanceof Map) {
2618
2634
  let named = existing.get('named') || [];
2619
2635
  if (named.length !== (packr.lastNamedStructuresLength || 0))
2620
- return false;
2636
+ compatible = false;
2621
2637
  let typed = existing.get('typed') || [];
2622
2638
  if (typed.length !== lastTypedStructuresLength)
2623
- return false;
2639
+ compatible = false;
2624
2640
  } else if (existing instanceof Array) {
2625
2641
  if (existing.length !== (packr.lastNamedStructuresLength || 0))
2626
- return false;
2642
+ compatible = false;
2627
2643
  }
2628
- return true;
2644
+ if (!compatible)
2645
+ packr._mergeStructures(existing);
2646
+ return compatible;
2629
2647
  };
2630
2648
  packr.lastTypedStructuresLength = packr.typedStructs?.length;
2631
- return structMap;
2649
+ return structures;
2632
2650
  }
2633
2651
 
2634
2652
  setReadStruct(readStruct$1, onLoadedStructures$1);
@@ -2800,7 +2818,7 @@
2800
2818
  } });
2801
2819
  for (let i = 0; i < 20; i++) {
2802
2820
  var serialized = packr.pack(data);
2803
- var deserialized = packr.unpack(serialized);
2821
+ var deserialized = packr.unpack(serialized, { lazy: true });
2804
2822
  var copied = {};
2805
2823
  for (let key in deserialized) {
2806
2824
  copied[key] = deserialized[key];