msgpackr 1.10.2 → 1.11.0

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
@@ -1233,7 +1233,7 @@ class Packr extends Unpackr {
1233
1233
  if (!this.structures && options.useRecords != false)
1234
1234
  this.structures = [];
1235
1235
  // two byte record ids for shared structures
1236
- let useTwoByteRecords = maxSharedStructures > 32 || (maxOwnStructures + maxSharedStructures > 64);
1236
+ let useTwoByteRecords = maxSharedStructures > 32 || (maxOwnStructures + maxSharedStructures > 64);
1237
1237
  let sharedLimitId = maxSharedStructures + 0x40;
1238
1238
  let maxStructureId = maxSharedStructures + maxOwnStructures + 0x40;
1239
1239
  if (maxStructureId > 8256) {
@@ -1251,7 +1251,7 @@ class Packr extends Unpackr {
1251
1251
  }
1252
1252
  safeEnd = target.length - 10;
1253
1253
  if (safeEnd - position < 0x800) {
1254
- // don't start too close to the end,
1254
+ // don't start too close to the end,
1255
1255
  target = new ByteArrayAllocate(target.length);
1256
1256
  targetView = target.dataView || (target.dataView = new DataView(target.buffer, 0, target.length));
1257
1257
  safeEnd = target.length - 10;
@@ -1594,7 +1594,7 @@ class Packr extends Unpackr {
1594
1594
  targetView.setUint32(position, referee.id);
1595
1595
  position += 4;
1596
1596
  return
1597
- } else
1597
+ } else
1598
1598
  referenceMap.set(value, { offset: position - start });
1599
1599
  }
1600
1600
  let constructor = value.constructor;
@@ -1622,7 +1622,7 @@ class Packr extends Unpackr {
1622
1622
  pack(entryValue);
1623
1623
  }
1624
1624
  }
1625
- } else {
1625
+ } else {
1626
1626
  for (let i = 0, l = extensions.length; i < l; i++) {
1627
1627
  let extensionClass = extensionClasses[i];
1628
1628
  if (value instanceof extensionClass) {
@@ -1690,11 +1690,11 @@ class Packr extends Unpackr {
1690
1690
  if (json !== value)
1691
1691
  return pack(json)
1692
1692
  }
1693
-
1693
+
1694
1694
  // if there is a writeFunction, use it, otherwise just encode as undefined
1695
1695
  if (type === 'function')
1696
1696
  return pack(this.writeFunction && this.writeFunction(value));
1697
-
1697
+
1698
1698
  // no extension found, write as plain object
1699
1699
  writeObject(value);
1700
1700
  }
@@ -1752,9 +1752,19 @@ class Packr extends Unpackr {
1752
1752
  }
1753
1753
  };
1754
1754
 
1755
- const writePlainObject = (this.variableMapSize || this.coercibleKeyAsNumber) ? (object) => {
1755
+ const writePlainObject = (this.variableMapSize || this.coercibleKeyAsNumber || this.skipValues) ? (object) => {
1756
1756
  // this method is slightly slower, but generates "preferred serialization" (optimally small for smaller objects)
1757
- let keys = Object.keys(object);
1757
+ let keys;
1758
+ if (this.skipValues) {
1759
+ keys = [];
1760
+ for (let key in object) {
1761
+ if ((typeof object.hasOwnProperty !== 'function' || object.hasOwnProperty(key)) &&
1762
+ !this.skipValues.includes(object[key]))
1763
+ keys.push(key);
1764
+ }
1765
+ } else {
1766
+ keys = Object.keys(object);
1767
+ }
1758
1768
  let length = keys.length;
1759
1769
  if (length < 0x10) {
1760
1770
  target[position++] = 0x80 | length;
@@ -1873,9 +1883,9 @@ class Packr extends Unpackr {
1873
1883
  }
1874
1884
  };
1875
1885
 
1876
- // craete reference to useRecords if useRecords is a function
1886
+ // create reference to useRecords if useRecords is a function
1877
1887
  const checkUseRecords = typeof this.useRecords == 'function' && this.useRecords;
1878
-
1888
+
1879
1889
  const writeObject = checkUseRecords ? (object) => {
1880
1890
  checkUseRecords(object) ? writeRecord(object) : writePlainObject(object);
1881
1891
  } : writeRecord;
@@ -2003,9 +2013,15 @@ class Packr extends Unpackr {
2003
2013
  useBuffer(buffer) {
2004
2014
  // this means we are finished using our own buffer and we can write over it safely
2005
2015
  target = buffer;
2006
- targetView = new DataView(target.buffer, target.byteOffset, target.byteLength);
2016
+ target.dataView || (target.dataView = new DataView(target.buffer, target.byteOffset, target.byteLength));
2007
2017
  position = 0;
2008
2018
  }
2019
+ set position (value) {
2020
+ position = value;
2021
+ }
2022
+ get position() {
2023
+ return position;
2024
+ }
2009
2025
  clearSharedData() {
2010
2026
  if (this.structures)
2011
2027
  this.structures = [];
@@ -2722,6 +2738,8 @@ function readStruct(src, position, srcEnd, unpackr) {
2722
2738
  src = Uint8Array.prototype.slice.call(src, position, srcEnd);
2723
2739
  srcEnd -= position;
2724
2740
  position = 0;
2741
+ if (!unpackr.getStructures)
2742
+ throw new Error(`Reference to shared structure ${recordId} without getStructures method`);
2725
2743
  unpackr._mergeStructures(unpackr.getStructures());
2726
2744
  if (!unpackr.typedStructs)
2727
2745
  throw new Error('Could not find any shared typed structures');