msgpackr 1.7.0-alpha4 → 1.7.0-alpha5

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;
@@ -175,7 +175,7 @@ class Unpackr {
175
175
  return this.unpack(source, end)
176
176
  }
177
177
  }
178
- function checkedRead() {
178
+ function checkedRead(options) {
179
179
  try {
180
180
  if (!currentUnpackr.trusted && !sequentialMode) {
181
181
  let sharedLength = currentStructures.sharedLength || 0;
@@ -185,6 +185,8 @@ function checkedRead() {
185
185
  let result;
186
186
  if (currentUnpackr.randomAccessStructure && src[position] < 0x40 && src[position] >= 0x20 && readStruct) {
187
187
  result = readStruct(src, position, srcEnd, currentUnpackr);
188
+ if (!(options && options.lazy) && result)
189
+ result = result.toJSON();
188
190
  position = srcEnd;
189
191
  } else
190
192
  result = read();
@@ -2502,12 +2504,13 @@ function readStruct$1(src, position, srcEnd, unpackr) {
2502
2504
  construct = structure.construct = function LazyObject() {
2503
2505
  };
2504
2506
  var prototype = construct.prototype;
2507
+ let properties = [];
2505
2508
  Object.defineProperty(prototype, 'toJSON', {
2506
- get() {
2509
+ value() {
2507
2510
  // return an enumerable object with own properties to JSON stringify
2508
2511
  let resolved = {};
2509
- for (let i = 0, l = structure.length; i < l; i++) {
2510
- let key = structure[i];
2512
+ for (let i = 0, l = properties.length; i < l; i++) {
2513
+ let key = properties[i].key;
2511
2514
  resolved[key] = this[key];
2512
2515
  }
2513
2516
  return resolved;
@@ -2516,7 +2519,6 @@ function readStruct$1(src, position, srcEnd, unpackr) {
2516
2519
  });
2517
2520
  let currentOffset = 0;
2518
2521
  let lastRefProperty;
2519
- let properties = [];
2520
2522
  for (let i = 0, l = structure.length; i < l; i++) {
2521
2523
  let definition = structure[i];
2522
2524
  let [ type, size, key, enumerationOffset ] = definition;
@@ -2709,13 +2711,14 @@ function toConstant(code) {
2709
2711
  throw new Error('Unknown constant');
2710
2712
  }
2711
2713
  function prepareStructures$1(structures, packr) {
2712
- if (!packr.typedStructs)
2713
- return structures;
2714
- let structMap = new Map();
2715
- structMap.set('named', structures);
2716
- 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
+ }
2717
2720
  let lastTypedStructuresLength = packr.lastTypedStructuresLength || 0;
2718
- structMap.isCompatible = existing => {
2721
+ structures.isCompatible = existing => {
2719
2722
  let compatible = true;
2720
2723
  if (existing instanceof Map) {
2721
2724
  let named = existing.get('named') || [];
@@ -2733,7 +2736,7 @@ function prepareStructures$1(structures, packr) {
2733
2736
  return compatible;
2734
2737
  };
2735
2738
  packr.lastTypedStructuresLength = packr.typedStructs?.length;
2736
- return structMap;
2739
+ return structures;
2737
2740
  }
2738
2741
 
2739
2742
  setReadStruct(readStruct$1, onLoadedStructures$1);