msgpackr 1.11.4 → 1.11.6

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
@@ -995,21 +995,47 @@
995
995
  currentExtensions[0] = () => {}; // notepack defines extension 0 to mean undefined, so use that as the default here
996
996
  currentExtensions[0].noBuffer = true;
997
997
 
998
- currentExtensions[0x42] = (data) => {
999
- // decode bigint
1000
- let length = data.length;
1001
- let value = BigInt(data[0] & 0x80 ? data[0] - 0x100 : data[0]);
1002
- for (let i = 1; i < length; i++) {
1003
- value <<= BigInt(8);
1004
- value += BigInt(data[i]);
998
+ currentExtensions[0x42] = data => {
999
+ let headLength = (data.byteLength % 8) || 8;
1000
+ let head = BigInt(data[0] & 0x80 ? data[0] - 0x100 : data[0]);
1001
+ for (let i = 1; i < headLength; i++) {
1002
+ head <<= BigInt(8);
1003
+ head += BigInt(data[i]);
1005
1004
  }
1006
- return value;
1005
+ if (data.byteLength !== headLength) {
1006
+ let view = new DataView(data.buffer, data.byteOffset, data.byteLength);
1007
+ let decode = (start, end) => {
1008
+ let length = end - start;
1009
+ if (length <= 40) {
1010
+ let out = view.getBigUint64(start);
1011
+ for (let i = start + 8; i < end; i += 8) {
1012
+ out <<= BigInt(64n);
1013
+ out |= view.getBigUint64(i);
1014
+ }
1015
+ return out
1016
+ }
1017
+ // if (length === 8) return view.getBigUint64(start)
1018
+ let middle = start + (length >> 4 << 3);
1019
+ let left = decode(start, middle);
1020
+ let right = decode(middle, end);
1021
+ return (left << BigInt((end - middle) * 8)) | right
1022
+ };
1023
+ head = (head << BigInt((view.byteLength - headLength) * 8)) | decode(headLength, view.byteLength);
1024
+ }
1025
+ return head
1007
1026
  };
1008
1027
 
1009
- let errors = { Error, TypeError, ReferenceError };
1028
+ let errors = {
1029
+ Error, EvalError, RangeError, ReferenceError, SyntaxError, TypeError, URIError, AggregateError: typeof AggregateError === 'function' ? AggregateError : null,
1030
+ };
1010
1031
  currentExtensions[0x65] = () => {
1011
1032
  let data = read();
1012
- return (errors[data[0]] || Error)(data[1], { cause: data[2] })
1033
+ if (!errors[data[0]]) {
1034
+ let error = Error(data[1], { cause: data[2] });
1035
+ error.name = data[0];
1036
+ return error
1037
+ }
1038
+ return errors[data[0]](data[1], { cause: data[2] })
1013
1039
  };
1014
1040
 
1015
1041
  currentExtensions[0x69] = (data) => {
@@ -1312,7 +1338,8 @@
1312
1338
  hasSharedUpdate = false;
1313
1339
  let encodingError;
1314
1340
  try {
1315
- if (packr.randomAccessStructure && value && value.constructor && value.constructor === Object)
1341
+ if (packr.randomAccessStructure && value && value.constructor && (value.constructor === Object ||
1342
+ (value.constructor !== Map && !Array.isArray(value) && !extensionClasses.some(extClass => value instanceof extClass))))
1316
1343
  writeStruct(value);
1317
1344
  else
1318
1345
  pack(value);
@@ -1727,22 +1754,47 @@
1727
1754
  targetView.setFloat64(position, Number(value));
1728
1755
  } else if (this.largeBigIntToString) {
1729
1756
  return pack(value.toString());
1730
- } else if ((this.useBigIntExtension || this.moreTypes) && value < BigInt(2)**BigInt(1023) && value > -(BigInt(2)**BigInt(1023))) {
1731
- target[position++] = 0xc7;
1732
- position++;
1733
- target[position++] = 0x42; // "B" for BigInt
1734
- let bytes = [];
1735
- let alignedSign;
1736
- do {
1737
- let byte = value & BigInt(0xff);
1738
- alignedSign = (byte & BigInt(0x80)) === (value < BigInt(0) ? BigInt(0x80) : BigInt(0));
1739
- bytes.push(byte);
1740
- value >>= BigInt(8);
1741
- } while (!((value === BigInt(0) || value === BigInt(-1)) && alignedSign));
1742
- target[position-2] = bytes.length;
1743
- for (let i = bytes.length; i > 0;) {
1744
- target[position++] = Number(bytes[--i]);
1757
+ } else if (this.useBigIntExtension || this.moreTypes) {
1758
+ let empty = value < 0 ? BigInt(-1) : BigInt(0);
1759
+
1760
+ let array;
1761
+ if (value >> BigInt(0x10000) === empty) {
1762
+ let mask = BigInt(0x10000000000000000) - BigInt(1); // literal would overflow
1763
+ let chunks = [];
1764
+ while (true) {
1765
+ chunks.push(value & mask);
1766
+ if ((value >> BigInt(63)) === empty) break
1767
+ value >>= BigInt(64);
1768
+ }
1769
+
1770
+ array = new Uint8Array(new BigUint64Array(chunks).buffer);
1771
+ array.reverse();
1772
+ } else {
1773
+ let invert = value < 0;
1774
+ let string = (invert ? ~value : value).toString(16);
1775
+ if (string.length % 2) {
1776
+ string = '0' + string;
1777
+ } else if (parseInt(string.charAt(0), 16) >= 8) {
1778
+ string = '00' + string;
1779
+ }
1780
+
1781
+ if (hasNodeBuffer$1) {
1782
+ array = Buffer.from(string, 'hex');
1783
+ } else {
1784
+ array = new Uint8Array(string.length / 2);
1785
+ for (let i = 0; i < array.length; i++) {
1786
+ array[i] = parseInt(string.slice(i * 2, i * 2 + 2), 16);
1787
+ }
1788
+ }
1789
+
1790
+ if (invert) {
1791
+ for (let i = 0; i < array.length; i++) array[i] = ~array[i];
1792
+ }
1745
1793
  }
1794
+
1795
+ if (array.length + position > safeEnd)
1796
+ makeRoom(array.length + position);
1797
+ position = writeExtensionData(array, target, position, 0x42);
1746
1798
  return
1747
1799
  } else {
1748
1800
  throw new RangeError(value + ' was too large to fit in MessagePack 64-bit integer format, use' +
@@ -2030,6 +2082,7 @@
2030
2082
  // this means we are finished using our own buffer and we can write over it safely
2031
2083
  target = buffer;
2032
2084
  target.dataView || (target.dataView = new DataView(target.buffer, target.byteOffset, target.byteLength));
2085
+ targetView = target.dataView;
2033
2086
  position = 0;
2034
2087
  }
2035
2088
  set position (value) {
@@ -2776,7 +2829,7 @@
2776
2829
  };
2777
2830
  fullConstruct = structure.fullConstruct = function LoadedObject() {
2778
2831
  };
2779
- fullConstruct.prototype = unpackr.structPrototype ?? {};
2832
+ fullConstruct.prototype = unpackr.structPrototype || {};
2780
2833
  var prototype = construct.prototype = unpackr.structPrototype ? Object.create(unpackr.structPrototype) : {};
2781
2834
  let properties = [];
2782
2835
  let currentOffset = 0;
@@ -3317,7 +3370,7 @@
3317
3370
  {id: 2, type: 1, labels: {b: 1, c: 2}},
3318
3371
  {id: 3, type: 1, labels: {d: 1, e: 2}}
3319
3372
  ];
3320
-
3373
+
3321
3374
  var alternatives = [
3322
3375
  {useRecords: false}, // 88 bytes
3323
3376
  {useRecords: true}, // 58 bytes
@@ -3329,7 +3382,7 @@
3329
3382
  let packr = new Packr(o);
3330
3383
  var serialized = packr.pack(data);
3331
3384
  var deserialized = packr.unpack(serialized);
3332
- assert.deepEqual(deserialized, data);
3385
+ assert.deepEqual(deserialized, data);
3333
3386
  }
3334
3387
  });
3335
3388
 
@@ -3451,7 +3504,7 @@
3451
3504
  });
3452
3505
 
3453
3506
  test('BigInt', function() {
3454
- let packr = new Packr({useBigIntExtension: true});
3507
+ let packr = new Packr({ useBigIntExtension: true });
3455
3508
  let data = {
3456
3509
  a: 3333333333333333333333333333n,
3457
3510
  b: 1234567890123456789012345678901234567890n,
@@ -3459,10 +3512,28 @@
3459
3512
  d: -352523523642364364364264264264264264262642642n,
3460
3513
  e: 0xffffffffffffffffffffffffffn,
3461
3514
  f: -0xffffffffffffffffffffffffffn,
3462
- o: -12345678901234567890n,
3463
- array: [],
3515
+ g: (1234n << 123n) ^ (5678n << 56n) ^ 890n,
3516
+ h: (-1234n << 123n) ^ (5678n << 56n) ^ 890n,
3517
+ i: (1234n << 1234n) ^ (5678n << 567n) ^ 890n,
3518
+ j: (-1234n << 1234n) ^ (5678n << 567n) ^ 890n,
3519
+ k: 0xdeadn << 0xbeefn,
3520
+ l: -0xdeadn << 0xbeefn,
3521
+ m: 11n << 0x11111n ^ 111n,
3522
+ n: -11n << 0x11111n ^ 111n,
3523
+ o: 12345678901234567890n,
3524
+ p: -12345678901234567890n,
3525
+ exp: [],
3526
+ expexp: [],
3464
3527
  };
3465
-
3528
+
3529
+ for (let n = 1n; n.toString(16).length * 4 < 1500; n <<= 1n, n |= BigInt(Math.floor(Math.random() * 2))) {
3530
+ data.exp.push(n, -n);
3531
+ }
3532
+
3533
+ for (let n = 7n; n.toString(16).length * 4 < 150000; n *= n) {
3534
+ data.expexp.push(n, -n);
3535
+ }
3536
+
3466
3537
  let serialized = packr.pack(data);
3467
3538
  let deserialized = packr.unpack(serialized);
3468
3539
  assert.deepEqual(data, deserialized);
@@ -3689,7 +3760,7 @@
3689
3760
 
3690
3761
  test('extended class pack/unpack proxied', function(){
3691
3762
  function Extended() {
3692
-
3763
+
3693
3764
  }
3694
3765
  Extended.prototype.__call__ = function(){
3695
3766
  return this.value * 4
@@ -3700,7 +3771,7 @@
3700
3771
 
3701
3772
  var instance = function() { instance.__call__();/* callable stuff */ };
3702
3773
  Object.setPrototypeOf(instance,Extended.prototype);
3703
-
3774
+
3704
3775
  instance.value = 4;
3705
3776
  var data = instance;
3706
3777
 
@@ -3781,7 +3852,9 @@
3781
3852
  test('moreTypes: Error with causes', function() {
3782
3853
  const object = {
3783
3854
  error: new Error('test'),
3784
- errorWithCause: new Error('test-1', { cause: new Error('test-2')}),
3855
+ errorWithCause: new Error('test-1', { cause: new Error('test-2') }),
3856
+ type: new TypeError(),
3857
+ range: new RangeError('test', { cause: [1, 2] }),
3785
3858
  };
3786
3859
  const packr = new Packr({
3787
3860
  moreTypes: true,
@@ -3794,6 +3867,12 @@
3794
3867
  assert.equal(deserialized.errorWithCause.message, object.errorWithCause.message);
3795
3868
  assert.equal(deserialized.errorWithCause.cause.message, object.errorWithCause.cause.message);
3796
3869
  assert.equal(deserialized.errorWithCause.cause.cause, object.errorWithCause.cause.cause);
3870
+ assert.equal(deserialized.type.message, object.type.message);
3871
+ assert.equal(deserialized.range.message, object.range.message);
3872
+ assert.deepEqual(deserialized.range.cause, object.range.cause);
3873
+ assert(deserialized.error instanceof Error);
3874
+ assert(deserialized.type instanceof TypeError);
3875
+ assert(deserialized.range instanceof RangeError);
3797
3876
  });
3798
3877
 
3799
3878
  test('structured cloning: self reference', function() {
@@ -4022,7 +4101,7 @@
4022
4101
  getStructures() {
4023
4102
  return structures
4024
4103
  },
4025
- saveStructures(structures) {
4104
+ saveStructures(structures) {
4026
4105
  },
4027
4106
  maxSharedStructures: 100
4028
4107
  });
@@ -4030,7 +4109,7 @@
4030
4109
  getStructures() {
4031
4110
  return structures2
4032
4111
  },
4033
- saveStructures(structures) {
4112
+ saveStructures(structures) {
4034
4113
  },
4035
4114
  maxSharedStructures: 100
4036
4115
  });