msgpackr 1.10.2 → 1.11.1

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
@@ -997,7 +997,7 @@
997
997
  let length = data.length;
998
998
  let value = BigInt(data[0] & 0x80 ? data[0] - 0x100 : data[0]);
999
999
  for (let i = 1; i < length; i++) {
1000
- value <<= 8n;
1000
+ value <<= BigInt(8);
1001
1001
  value += BigInt(data[i]);
1002
1002
  }
1003
1003
  return value;
@@ -1203,7 +1203,7 @@
1203
1203
  let structures;
1204
1204
  let referenceMap;
1205
1205
  let encodeUtf8 = ByteArray.prototype.utf8Write ? function(string, position) {
1206
- return target.utf8Write(string, position, 0xffffffff)
1206
+ return target.utf8Write(string, position, target.byteLength - position)
1207
1207
  } : (textEncoder$1 && textEncoder$1.encodeInto) ?
1208
1208
  function(string, position) {
1209
1209
  return textEncoder$1.encodeInto(string, target.subarray(position)).written
@@ -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
  }
@@ -1711,18 +1711,20 @@
1711
1711
  if (this.largeBigIntToFloat) {
1712
1712
  target[position++] = 0xcb;
1713
1713
  targetView.setFloat64(position, Number(value));
1714
- } else if (this.useBigIntExtension && value < 2n**(1023n) && value > -(2n**(1023n))) {
1714
+ } else if (this.largeBigIntToString) {
1715
+ return pack(value.toString());
1716
+ } else if (this.useBigIntExtension && value < BigInt(2)**BigInt(1023) && value > -(BigInt(2)**BigInt(1023))) {
1715
1717
  target[position++] = 0xc7;
1716
1718
  position++;
1717
1719
  target[position++] = 0x42; // "B" for BigInt
1718
1720
  let bytes = [];
1719
1721
  let alignedSign;
1720
1722
  do {
1721
- let byte = value & 0xffn;
1722
- alignedSign = (byte & 0x80n) === (value < 0n ? 0x80n : 0n);
1723
+ let byte = value & BigInt(0xff);
1724
+ alignedSign = (byte & BigInt(0x80)) === (value < BigInt(0) ? BigInt(0x80) : BigInt(0));
1723
1725
  bytes.push(byte);
1724
- value >>= 8n;
1725
- } while (!((value === 0n || value === -1n) && alignedSign));
1726
+ value >>= BigInt(8);
1727
+ } while (!((value === BigInt(0) || value === BigInt(-1)) && alignedSign));
1726
1728
  target[position-2] = bytes.length;
1727
1729
  for (let i = bytes.length; i > 0;) {
1728
1730
  target[position++] = Number(bytes[--i]);
@@ -1730,7 +1732,8 @@
1730
1732
  return
1731
1733
  } else {
1732
1734
  throw new RangeError(value + ' was too large to fit in MessagePack 64-bit integer format, use' +
1733
- ' useBigIntExtension or set largeBigIntToFloat to convert to float-64')
1735
+ ' useBigIntExtension, or set largeBigIntToFloat to convert to float-64, or set' +
1736
+ ' largeBigIntToString to convert to string')
1734
1737
  }
1735
1738
  }
1736
1739
  position += 8;
@@ -1747,9 +1750,19 @@
1747
1750
  }
1748
1751
  };
1749
1752
 
1750
- const writePlainObject = (this.variableMapSize || this.coercibleKeyAsNumber) ? (object) => {
1753
+ const writePlainObject = (this.variableMapSize || this.coercibleKeyAsNumber || this.skipValues) ? (object) => {
1751
1754
  // this method is slightly slower, but generates "preferred serialization" (optimally small for smaller objects)
1752
- let keys = Object.keys(object);
1755
+ let keys;
1756
+ if (this.skipValues) {
1757
+ keys = [];
1758
+ for (let key in object) {
1759
+ if ((typeof object.hasOwnProperty !== 'function' || object.hasOwnProperty(key)) &&
1760
+ !this.skipValues.includes(object[key]))
1761
+ keys.push(key);
1762
+ }
1763
+ } else {
1764
+ keys = Object.keys(object);
1765
+ }
1753
1766
  let length = keys.length;
1754
1767
  if (length < 0x10) {
1755
1768
  target[position++] = 0x80 | length;
@@ -1790,6 +1803,10 @@
1790
1803
  size++;
1791
1804
  }
1792
1805
  }
1806
+ if (size > 0xffff) {
1807
+ throw new Error('Object is too large to serialize with fast 16-bit map size,' +
1808
+ ' use the "variableMapSize" option to serialize this object');
1809
+ }
1793
1810
  target[objectOffset++ + start] = size >> 8;
1794
1811
  target[objectOffset + start] = size & 0xff;
1795
1812
  };
@@ -1868,9 +1885,9 @@
1868
1885
  }
1869
1886
  };
1870
1887
 
1871
- // craete reference to useRecords if useRecords is a function
1888
+ // create reference to useRecords if useRecords is a function
1872
1889
  const checkUseRecords = typeof this.useRecords == 'function' && this.useRecords;
1873
-
1890
+
1874
1891
  const writeObject = checkUseRecords ? (object) => {
1875
1892
  checkUseRecords(object) ? writeRecord(object) : writePlainObject(object);
1876
1893
  } : writeRecord;
@@ -1998,9 +2015,15 @@
1998
2015
  useBuffer(buffer) {
1999
2016
  // this means we are finished using our own buffer and we can write over it safely
2000
2017
  target = buffer;
2001
- targetView = new DataView(target.buffer, target.byteOffset, target.byteLength);
2018
+ target.dataView || (target.dataView = new DataView(target.buffer, target.byteOffset, target.byteLength));
2002
2019
  position = 0;
2003
2020
  }
2021
+ set position (value) {
2022
+ position = value;
2023
+ }
2024
+ get position() {
2025
+ return position;
2026
+ }
2004
2027
  clearSharedData() {
2005
2028
  if (this.structures)
2006
2029
  this.structures = [];
@@ -2138,7 +2161,7 @@
2138
2161
  target[position++] = length >> 8;
2139
2162
  target[position++] = length & 0xff;
2140
2163
  } else {
2141
- var { target, position, targetView } = allocateForWrite(length + 5);
2164
+ let { target, position, targetView } = allocateForWrite(length + 5);
2142
2165
  target[position++] = 0xc6;
2143
2166
  targetView.setUint32(position, length);
2144
2167
  position += 4;
@@ -2274,7 +2297,7 @@
2274
2297
  textEncoder = new TextEncoder();
2275
2298
  } catch (error) {}
2276
2299
  const encodeUtf8 = hasNodeBuffer ? function(target, string, position) {
2277
- return target.utf8Write(string, position, 0xffffffff)
2300
+ return target.utf8Write(string, position, target.byteLength - position)
2278
2301
  } : (textEncoder && textEncoder.encodeInto) ?
2279
2302
  function(target, string, position) {
2280
2303
  return textEncoder.encodeInto(string, target.subarray(position)).written
@@ -2715,6 +2738,8 @@
2715
2738
  src = Uint8Array.prototype.slice.call(src, position, srcEnd);
2716
2739
  srcEnd -= position;
2717
2740
  position = 0;
2741
+ if (!unpackr.getStructures)
2742
+ throw new Error(`Reference to shared structure ${recordId} without getStructures method`);
2718
2743
  unpackr._mergeStructures(unpackr.getStructures());
2719
2744
  if (!unpackr.typedStructs)
2720
2745
  throw new Error('Could not find any shared typed structures');
@@ -4175,6 +4200,13 @@
4175
4200
  serialized = packr.pack(tooBigInt);
4176
4201
  deserialized = unpack(serialized);
4177
4202
  assert.isTrue(deserialized.tooBig > 2n**65n);
4203
+
4204
+ packr = new Packr({
4205
+ largeBigIntToString: true
4206
+ });
4207
+ serialized = packr.pack(tooBigInt);
4208
+ deserialized = unpack(serialized);
4209
+ assert.equal(deserialized.tooBig, (2n**66n).toString());
4178
4210
  });
4179
4211
 
4180
4212
  test('roundFloat32', function() {
@@ -4247,6 +4279,54 @@
4247
4279
  const deserialized = unpack(serialized);
4248
4280
  assert.deepStrictEqual(deserialized, { someData: [1, 2, 3, 4] });
4249
4281
  });
4282
+ test('skip values', function () {
4283
+ var data = {
4284
+ data: [
4285
+ { a: 1, name: 'one', type: 'odd', isOdd: true },
4286
+ { a: 2, name: 'two', type: 'even', isOdd: undefined },
4287
+ { a: 3, name: 'three', type: 'odd', isOdd: true },
4288
+ { a: 4, name: 'four', type: 'even', isOdd: null},
4289
+ { a: 5, name: 'five', type: 'odd', isOdd: true },
4290
+ { a: 6, name: 'six', type: 'even', isOdd: null }
4291
+ ],
4292
+ description: 'some names',
4293
+ types: ['odd', 'even'],
4294
+ convertEnumToNum: [
4295
+ { prop: 'test' },
4296
+ { prop: 'test' },
4297
+ { prop: 'test' },
4298
+ { prop: 1 },
4299
+ { prop: 2 },
4300
+ { prop: [undefined, null] },
4301
+ { prop: null }
4302
+ ]
4303
+ };
4304
+ var expected = {
4305
+ data: [
4306
+ { a: 1, name: 'one', type: 'odd', isOdd: true },
4307
+ { a: 2, name: 'two', type: 'even' },
4308
+ { a: 3, name: 'three', type: 'odd', isOdd: true },
4309
+ { a: 4, name: 'four', type: 'even', },
4310
+ { a: 5, name: 'five', type: 'odd', isOdd: true },
4311
+ { a: 6, name: 'six', type: 'even' }
4312
+ ],
4313
+ description: 'some names',
4314
+ types: ['odd', 'even'],
4315
+ convertEnumToNum: [
4316
+ { prop: 'test' },
4317
+ { prop: 'test' },
4318
+ { prop: 'test' },
4319
+ { prop: 1 },
4320
+ { prop: 2 },
4321
+ { prop: [undefined, null] },
4322
+ {}
4323
+ ]
4324
+ };
4325
+ let packr = new Packr({ useRecords: false, skipValues: [undefined, null] });
4326
+ var serialized = packr.pack(data);
4327
+ var deserialized = packr.unpack(serialized);
4328
+ assert.deepEqual(deserialized, expected);
4329
+ });
4250
4330
  });
4251
4331
  suite('msgpackr performance tests', function(){
4252
4332
  test('performance JSON.parse', function() {