msgpackr 1.8.0 → 1.8.2

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
@@ -120,7 +120,7 @@ class Unpackr {
120
120
  let size = source.length;
121
121
  let value = this ? this.unpack(source, size) : defaultUnpackr.unpack(source, size);
122
122
  if (forEach) {
123
- forEach(value);
123
+ if (forEach(value) === false) return;
124
124
  while(position$1 < size) {
125
125
  lastPosition = position$1;
126
126
  if (forEach(checkedRead()) === false) {
@@ -1173,7 +1173,7 @@ class Packr extends Unpackr {
1173
1173
  if (maxSharedStructures > 8160)
1174
1174
  throw new Error('Maximum maxSharedStructure is 8160')
1175
1175
  if (options.structuredClone && options.moreTypes == undefined) {
1176
- options.moreTypes = true;
1176
+ this.moreTypes = true;
1177
1177
  }
1178
1178
  let maxOwnStructures = options.maxOwnStructures;
1179
1179
  if (maxOwnStructures == null)
@@ -1334,6 +1334,23 @@ class Packr extends Unpackr {
1334
1334
  position = start;
1335
1335
  }
1336
1336
  };
1337
+ const packArray = (value) => {
1338
+ var length = value.length;
1339
+ if (length < 0x10) {
1340
+ target[position++] = 0x90 | length;
1341
+ } else if (length < 0x10000) {
1342
+ target[position++] = 0xdc;
1343
+ target[position++] = length >> 8;
1344
+ target[position++] = length & 0xff;
1345
+ } else {
1346
+ target[position++] = 0xdd;
1347
+ targetView.setUint32(position, length);
1348
+ position += 4;
1349
+ }
1350
+ for (let i = 0; i < length; i++) {
1351
+ pack(value[i]);
1352
+ }
1353
+ };
1337
1354
  const pack = (value) => {
1338
1355
  if (position > safeEnd)
1339
1356
  target = makeRoom(position);
@@ -1516,22 +1533,8 @@ class Packr extends Unpackr {
1516
1533
  let constructor = value.constructor;
1517
1534
  if (constructor === Object) {
1518
1535
  writeObject(value, true);
1519
- } else if (constructor === Array || Array.isArray(value)) {
1520
- length = value.length;
1521
- if (length < 0x10) {
1522
- target[position++] = 0x90 | length;
1523
- } else if (length < 0x10000) {
1524
- target[position++] = 0xdc;
1525
- target[position++] = length >> 8;
1526
- target[position++] = length & 0xff;
1527
- } else {
1528
- target[position++] = 0xdd;
1529
- targetView.setUint32(position, length);
1530
- position += 4;
1531
- }
1532
- for (let i = 0; i < length; i++) {
1533
- pack(value[i]);
1534
- }
1536
+ } else if (constructor === Array) {
1537
+ packArray(value);
1535
1538
  } else if (constructor === Map) {
1536
1539
  length = value.size;
1537
1540
  if (length < 0x10) {
@@ -1560,7 +1563,16 @@ class Packr extends Unpackr {
1560
1563
  target[position++] = extension.type;
1561
1564
  target[position++] = 0;
1562
1565
  }
1563
- pack(extension.write.call(this, value));
1566
+ let writeResult = extension.write.call(this, value);
1567
+ if (writeResult === value) { // avoid infinite recursion
1568
+ if (Array.isArray(value)) {
1569
+ packArray(value);
1570
+ } else {
1571
+ writeObject(value);
1572
+ }
1573
+ } else {
1574
+ pack(writeResult);
1575
+ }
1564
1576
  return
1565
1577
  }
1566
1578
  let currentTarget = target;
@@ -1597,8 +1609,13 @@ class Packr extends Unpackr {
1597
1609
  return
1598
1610
  }
1599
1611
  }
1600
- // no extension found, write as object
1601
- writeObject(value, !value.hasOwnProperty); // if it doesn't have hasOwnProperty, don't do hasOwnProperty checks
1612
+ // check isArray after extensions, because extensions can extend Array
1613
+ if (Array.isArray(value)) {
1614
+ packArray(value);
1615
+ } else {
1616
+ // no extension found, write as object
1617
+ writeObject(value, !value.hasOwnProperty); // if it doesn't have hasOwnProperty, don't do hasOwnProperty checks
1618
+ }
1602
1619
  }
1603
1620
  }
1604
1621
  } else if (type === 'boolean') {
@@ -1851,12 +1868,11 @@ class Packr extends Unpackr {
1851
1868
  if (notifySharedUpdate)
1852
1869
  return hasSharedUpdate = true;
1853
1870
  position = newPosition;
1854
- if (start > 0) {
1855
- pack(value);
1856
- if (start == 0)
1857
- return { position, targetView, target }; // indicate the buffer was re-allocated
1858
- } else
1859
- pack(value);
1871
+ let startTarget = target;
1872
+ pack(value);
1873
+ if (startTarget !== target) {
1874
+ return { position, targetView, target }; // indicate the buffer was re-allocated
1875
+ }
1860
1876
  return position;
1861
1877
  }, this);
1862
1878
  if (newPosition === 0) // bail and go to a msgpack object
@@ -2776,24 +2792,35 @@ function readStruct(src, position, srcEnd, unpackr) {
2776
2792
  let objectLiteralProperties = [];
2777
2793
  let args = [];
2778
2794
  let i = 0;
2795
+ let hasInheritedProperties;
2779
2796
  for (let property of properties) { // assign in enumeration order
2797
+ if (unpackr.alwaysLazyProperty && unpackr.alwaysLazyProperty(property.key)) {
2798
+ // these properties are not eagerly evaluated and this can be used for creating properties
2799
+ // that are not serialized as JSON
2800
+ hasInheritedProperties = true;
2801
+ continue;
2802
+ }
2780
2803
  Object.defineProperty(prototype, property.key, { get: withSource(property.get), enumerable: true });
2781
2804
  let valueFunction = 'v' + i++;
2782
2805
  args.push(valueFunction);
2783
2806
  objectLiteralProperties.push('[' + JSON.stringify(property.key) + ']:' + valueFunction + '(s)');
2784
2807
  }
2808
+ if (hasInheritedProperties) {
2809
+ objectLiteralProperties.push('__proto__:this');
2810
+ }
2785
2811
  let toObject = (new Function(...args, 'return function(s){return{' + objectLiteralProperties.join(',') + '}}')).apply(null, properties.map(prop => prop.get));
2786
2812
  Object.defineProperty(prototype, 'toJSON', {
2787
- value() {
2788
- return toObject(this[sourceSymbol]);
2813
+ value(omitUnderscoredProperties) {
2814
+ return toObject.call(this, this[sourceSymbol]);
2789
2815
  }
2790
2816
  });
2791
2817
  } else {
2792
2818
  Object.defineProperty(prototype, 'toJSON', {
2793
- value() {
2819
+ value(omitUnderscoredProperties) {
2794
2820
  // return an enumerable object with own properties to JSON stringify
2795
2821
  let resolved = {};
2796
2822
  for (let i = 0, l = properties.length; i < l; i++) {
2823
+ // TODO: check alwaysLazyProperty
2797
2824
  let key = properties[i].key;
2798
2825
 
2799
2826
  resolved[key] = this[key];