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/test.js CHANGED
@@ -1228,7 +1228,7 @@
1228
1228
  if (!this.structures && options.useRecords != false)
1229
1229
  this.structures = [];
1230
1230
  // two byte record ids for shared structures
1231
- let useTwoByteRecords = maxSharedStructures > 32 || (maxOwnStructures + maxSharedStructures > 64);
1231
+ let useTwoByteRecords = maxSharedStructures > 32 || (maxOwnStructures + maxSharedStructures > 64);
1232
1232
  let sharedLimitId = maxSharedStructures + 0x40;
1233
1233
  let maxStructureId = maxSharedStructures + maxOwnStructures + 0x40;
1234
1234
  if (maxStructureId > 8256) {
@@ -1246,7 +1246,7 @@
1246
1246
  }
1247
1247
  safeEnd = target.length - 10;
1248
1248
  if (safeEnd - position < 0x800) {
1249
- // don't start too close to the end,
1249
+ // don't start too close to the end,
1250
1250
  target = new ByteArrayAllocate(target.length);
1251
1251
  targetView = target.dataView || (target.dataView = new DataView(target.buffer, 0, target.length));
1252
1252
  safeEnd = target.length - 10;
@@ -1589,7 +1589,7 @@
1589
1589
  targetView.setUint32(position, referee.id);
1590
1590
  position += 4;
1591
1591
  return
1592
- } else
1592
+ } else
1593
1593
  referenceMap.set(value, { offset: position - start });
1594
1594
  }
1595
1595
  let constructor = value.constructor;
@@ -1617,7 +1617,7 @@
1617
1617
  pack(entryValue);
1618
1618
  }
1619
1619
  }
1620
- } else {
1620
+ } else {
1621
1621
  for (let i = 0, l = extensions.length; i < l; i++) {
1622
1622
  let extensionClass = extensionClasses[i];
1623
1623
  if (value instanceof extensionClass) {
@@ -1685,11 +1685,11 @@
1685
1685
  if (json !== value)
1686
1686
  return pack(json)
1687
1687
  }
1688
-
1688
+
1689
1689
  // if there is a writeFunction, use it, otherwise just encode as undefined
1690
1690
  if (type === 'function')
1691
1691
  return pack(this.writeFunction && this.writeFunction(value));
1692
-
1692
+
1693
1693
  // no extension found, write as plain object
1694
1694
  writeObject(value);
1695
1695
  }
@@ -1747,9 +1747,19 @@
1747
1747
  }
1748
1748
  };
1749
1749
 
1750
- const writePlainObject = (this.variableMapSize || this.coercibleKeyAsNumber) ? (object) => {
1750
+ const writePlainObject = (this.variableMapSize || this.coercibleKeyAsNumber || this.skipValues) ? (object) => {
1751
1751
  // this method is slightly slower, but generates "preferred serialization" (optimally small for smaller objects)
1752
- let keys = Object.keys(object);
1752
+ let keys;
1753
+ if (this.skipValues) {
1754
+ keys = [];
1755
+ for (let key in object) {
1756
+ if ((typeof object.hasOwnProperty !== 'function' || object.hasOwnProperty(key)) &&
1757
+ !this.skipValues.includes(object[key]))
1758
+ keys.push(key);
1759
+ }
1760
+ } else {
1761
+ keys = Object.keys(object);
1762
+ }
1753
1763
  let length = keys.length;
1754
1764
  if (length < 0x10) {
1755
1765
  target[position++] = 0x80 | length;
@@ -1868,9 +1878,9 @@
1868
1878
  }
1869
1879
  };
1870
1880
 
1871
- // craete reference to useRecords if useRecords is a function
1881
+ // create reference to useRecords if useRecords is a function
1872
1882
  const checkUseRecords = typeof this.useRecords == 'function' && this.useRecords;
1873
-
1883
+
1874
1884
  const writeObject = checkUseRecords ? (object) => {
1875
1885
  checkUseRecords(object) ? writeRecord(object) : writePlainObject(object);
1876
1886
  } : writeRecord;
@@ -1998,9 +2008,15 @@
1998
2008
  useBuffer(buffer) {
1999
2009
  // this means we are finished using our own buffer and we can write over it safely
2000
2010
  target = buffer;
2001
- targetView = new DataView(target.buffer, target.byteOffset, target.byteLength);
2011
+ target.dataView || (target.dataView = new DataView(target.buffer, target.byteOffset, target.byteLength));
2002
2012
  position = 0;
2003
2013
  }
2014
+ set position (value) {
2015
+ position = value;
2016
+ }
2017
+ get position() {
2018
+ return position;
2019
+ }
2004
2020
  clearSharedData() {
2005
2021
  if (this.structures)
2006
2022
  this.structures = [];
@@ -2715,6 +2731,8 @@
2715
2731
  src = Uint8Array.prototype.slice.call(src, position, srcEnd);
2716
2732
  srcEnd -= position;
2717
2733
  position = 0;
2734
+ if (!unpackr.getStructures)
2735
+ throw new Error(`Reference to shared structure ${recordId} without getStructures method`);
2718
2736
  unpackr._mergeStructures(unpackr.getStructures());
2719
2737
  if (!unpackr.typedStructs)
2720
2738
  throw new Error('Could not find any shared typed structures');
@@ -4247,6 +4265,54 @@
4247
4265
  const deserialized = unpack(serialized);
4248
4266
  assert.deepStrictEqual(deserialized, { someData: [1, 2, 3, 4] });
4249
4267
  });
4268
+ test('skip values', function () {
4269
+ var data = {
4270
+ data: [
4271
+ { a: 1, name: 'one', type: 'odd', isOdd: true },
4272
+ { a: 2, name: 'two', type: 'even', isOdd: undefined },
4273
+ { a: 3, name: 'three', type: 'odd', isOdd: true },
4274
+ { a: 4, name: 'four', type: 'even', isOdd: null},
4275
+ { a: 5, name: 'five', type: 'odd', isOdd: true },
4276
+ { a: 6, name: 'six', type: 'even', isOdd: null }
4277
+ ],
4278
+ description: 'some names',
4279
+ types: ['odd', 'even'],
4280
+ convertEnumToNum: [
4281
+ { prop: 'test' },
4282
+ { prop: 'test' },
4283
+ { prop: 'test' },
4284
+ { prop: 1 },
4285
+ { prop: 2 },
4286
+ { prop: [undefined, null] },
4287
+ { prop: null }
4288
+ ]
4289
+ };
4290
+ var expected = {
4291
+ data: [
4292
+ { a: 1, name: 'one', type: 'odd', isOdd: true },
4293
+ { a: 2, name: 'two', type: 'even' },
4294
+ { a: 3, name: 'three', type: 'odd', isOdd: true },
4295
+ { a: 4, name: 'four', type: 'even', },
4296
+ { a: 5, name: 'five', type: 'odd', isOdd: true },
4297
+ { a: 6, name: 'six', type: 'even' }
4298
+ ],
4299
+ description: 'some names',
4300
+ types: ['odd', 'even'],
4301
+ convertEnumToNum: [
4302
+ { prop: 'test' },
4303
+ { prop: 'test' },
4304
+ { prop: 'test' },
4305
+ { prop: 1 },
4306
+ { prop: 2 },
4307
+ { prop: [undefined, null] },
4308
+ {}
4309
+ ]
4310
+ };
4311
+ let packr = new Packr({ useRecords: false, skipValues: [undefined, null] });
4312
+ var serialized = packr.pack(data);
4313
+ var deserialized = packr.unpack(serialized);
4314
+ assert.deepEqual(deserialized, expected);
4315
+ });
4250
4316
  });
4251
4317
  suite('msgpackr performance tests', function(){
4252
4318
  test('performance JSON.parse', function() {