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/README.md CHANGED
@@ -186,7 +186,8 @@ The following options properties can be provided to the Packr or Unpackr constru
186
186
  * `largeBigIntToFloat` - If a bigint needs to be encoded that is larger than will fit in 64-bit integers, it will be encoded as a float-64 (otherwise will throw a RangeError).
187
187
  * `useBigIntExtension` - If a bigint needs to be encoded that is larger than will fit in 64-bit integers, it will be encoded using a custom extension that supports up to about 1000-bits of integer precision.
188
188
  * `encodeUndefinedAsNil` - Encodes a value of `undefined` as a MessagePack `nil`, the same as a `null`.
189
- * `int64AsType` - This will decode uint64 and int64 numbers as the specified type. The type can be `bigint` (default), `number`, `string`, or `auto` (where range [-2^53...2^53] is represented by number and everything else by a bigint).
189
+ * `int64AsType` - This will decode uint64 and int64 numbers as the specified type. The type can be `bigint` (default), `number`, `string`, or `auto` (where range [-2^53...2^53] is represented by number and everything else by a bigint).
190
+ * `skipValues` - This can be an array of property values that will indicate properties that should be skipped when serializing objects. For example, to mimic `JSON.stringify`'s behavior of skipping properties with a value of `undefined`, you can provide `skipValues: [undefined]`. Note, that this will only apply to serializing objects as standard MessagePack maps, not to records. Also, the array is checked by calling the `include` method, so you can provide an object with an `includes` if you want a custom function to skip values.
190
191
  * `onInvalidDate` - This can be provided as function that will be called when an invalid date is provided. The function can throw an error, or return a value that will be encoded in place of the invalid date. If not provided, an invalid date will be encoded as an invalid timestamp (which decodes with msgpackr back to an invalid date).
191
192
  * `writeFunction` - This can be provided as function that will be called when a function is encountered. The function can throw an error, or return a value that will be encoded in place of the function. If not provided, a function will be encoded as undefined (similar to `JSON.stringify`).
192
193
  * `mapAsEmptyObject` - Encodes JS `Map`s as empty objects (for back-compat with older libraries).
@@ -1167,7 +1167,7 @@
1167
1167
  if (!this.structures && options.useRecords != false)
1168
1168
  this.structures = [];
1169
1169
  // two byte record ids for shared structures
1170
- let useTwoByteRecords = maxSharedStructures > 32 || (maxOwnStructures + maxSharedStructures > 64);
1170
+ let useTwoByteRecords = maxSharedStructures > 32 || (maxOwnStructures + maxSharedStructures > 64);
1171
1171
  let sharedLimitId = maxSharedStructures + 0x40;
1172
1172
  let maxStructureId = maxSharedStructures + maxOwnStructures + 0x40;
1173
1173
  if (maxStructureId > 8256) {
@@ -1185,7 +1185,7 @@
1185
1185
  }
1186
1186
  safeEnd = target.length - 10;
1187
1187
  if (safeEnd - position < 0x800) {
1188
- // don't start too close to the end,
1188
+ // don't start too close to the end,
1189
1189
  target = new ByteArrayAllocate(target.length);
1190
1190
  targetView = target.dataView || (target.dataView = new DataView(target.buffer, 0, target.length));
1191
1191
  safeEnd = target.length - 10;
@@ -1528,7 +1528,7 @@
1528
1528
  targetView.setUint32(position, referee.id);
1529
1529
  position += 4;
1530
1530
  return
1531
- } else
1531
+ } else
1532
1532
  referenceMap.set(value, { offset: position - start });
1533
1533
  }
1534
1534
  let constructor = value.constructor;
@@ -1556,7 +1556,7 @@
1556
1556
  pack(entryValue);
1557
1557
  }
1558
1558
  }
1559
- } else {
1559
+ } else {
1560
1560
  for (let i = 0, l = extensions.length; i < l; i++) {
1561
1561
  let extensionClass = extensionClasses[i];
1562
1562
  if (value instanceof extensionClass) {
@@ -1624,11 +1624,11 @@
1624
1624
  if (json !== value)
1625
1625
  return pack(json)
1626
1626
  }
1627
-
1627
+
1628
1628
  // if there is a writeFunction, use it, otherwise just encode as undefined
1629
1629
  if (type === 'function')
1630
1630
  return pack(this.writeFunction && this.writeFunction(value));
1631
-
1631
+
1632
1632
  // no extension found, write as plain object
1633
1633
  writeObject(value);
1634
1634
  }
@@ -1686,9 +1686,19 @@
1686
1686
  }
1687
1687
  };
1688
1688
 
1689
- const writePlainObject = (this.variableMapSize || this.coercibleKeyAsNumber) ? (object) => {
1689
+ const writePlainObject = (this.variableMapSize || this.coercibleKeyAsNumber || this.skipValues) ? (object) => {
1690
1690
  // this method is slightly slower, but generates "preferred serialization" (optimally small for smaller objects)
1691
- let keys = Object.keys(object);
1691
+ let keys;
1692
+ if (this.skipValues) {
1693
+ keys = [];
1694
+ for (let key in object) {
1695
+ if ((typeof object.hasOwnProperty !== 'function' || object.hasOwnProperty(key)) &&
1696
+ !this.skipValues.includes(object[key]))
1697
+ keys.push(key);
1698
+ }
1699
+ } else {
1700
+ keys = Object.keys(object);
1701
+ }
1692
1702
  let length = keys.length;
1693
1703
  if (length < 0x10) {
1694
1704
  target[position++] = 0x80 | length;
@@ -1807,9 +1817,9 @@
1807
1817
  }
1808
1818
  };
1809
1819
 
1810
- // craete reference to useRecords if useRecords is a function
1820
+ // create reference to useRecords if useRecords is a function
1811
1821
  const checkUseRecords = typeof this.useRecords == 'function' && this.useRecords;
1812
-
1822
+
1813
1823
  const writeObject = checkUseRecords ? (object) => {
1814
1824
  checkUseRecords(object) ? writeRecord(object) : writePlainObject(object);
1815
1825
  } : writeRecord;
@@ -1937,9 +1947,15 @@
1937
1947
  useBuffer(buffer) {
1938
1948
  // this means we are finished using our own buffer and we can write over it safely
1939
1949
  target = buffer;
1940
- targetView = new DataView(target.buffer, target.byteOffset, target.byteLength);
1950
+ target.dataView || (target.dataView = new DataView(target.buffer, target.byteOffset, target.byteLength));
1941
1951
  position = 0;
1942
1952
  }
1953
+ set position (value) {
1954
+ position = value;
1955
+ }
1956
+ get position() {
1957
+ return position;
1958
+ }
1943
1959
  clearSharedData() {
1944
1960
  if (this.structures)
1945
1961
  this.structures = [];
@@ -2285,6 +2301,8 @@
2285
2301
  exports.FLOAT32_OPTIONS = FLOAT32_OPTIONS;
2286
2302
  exports.NEVER = NEVER;
2287
2303
  exports.Packr = Packr;
2304
+ exports.RESERVE_START_SPACE = RESERVE_START_SPACE;
2305
+ exports.RESET_BUFFER_MODE = RESET_BUFFER_MODE;
2288
2306
  exports.REUSE_BUFFER_MODE = REUSE_BUFFER_MODE;
2289
2307
  exports.Unpackr = Unpackr;
2290
2308
  exports.addExtension = addExtension;