msgpackr 1.11.4 → 1.11.5

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) => {
@@ -1727,22 +1753,47 @@
1727
1753
  targetView.setFloat64(position, Number(value));
1728
1754
  } else if (this.largeBigIntToString) {
1729
1755
  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]);
1756
+ } else if (this.useBigIntExtension || this.moreTypes) {
1757
+ let empty = value < 0 ? BigInt(-1) : BigInt(0);
1758
+
1759
+ let array;
1760
+ if (value >> BigInt(0x10000) === empty) {
1761
+ let mask = BigInt(0x10000000000000000) - BigInt(1); // literal would overflow
1762
+ let chunks = [];
1763
+ while (true) {
1764
+ chunks.push(value & mask);
1765
+ if ((value >> BigInt(63)) === empty) break
1766
+ value >>= BigInt(64);
1767
+ }
1768
+
1769
+ array = new Uint8Array(new BigUint64Array(chunks).buffer);
1770
+ array.reverse();
1771
+ } else {
1772
+ let invert = value < 0;
1773
+ let string = (invert ? ~value : value).toString(16);
1774
+ if (string.length % 2) {
1775
+ string = '0' + string;
1776
+ } else if (parseInt(string.charAt(0), 16) >= 8) {
1777
+ string = '00' + string;
1778
+ }
1779
+
1780
+ if (hasNodeBuffer$1) {
1781
+ array = Buffer.from(string, 'hex');
1782
+ } else {
1783
+ array = new Uint8Array(string.length / 2);
1784
+ for (let i = 0; i < array.length; i++) {
1785
+ array[i] = parseInt(string.slice(i * 2, i * 2 + 2), 16);
1786
+ }
1787
+ }
1788
+
1789
+ if (invert) {
1790
+ for (let i = 0; i < array.length; i++) array[i] = ~array[i];
1791
+ }
1745
1792
  }
1793
+
1794
+ if (array.length + position > safeEnd)
1795
+ makeRoom(array.length + position);
1796
+ position = writeExtensionData(array, target, position, 0x42);
1746
1797
  return
1747
1798
  } else {
1748
1799
  throw new RangeError(value + ' was too large to fit in MessagePack 64-bit integer format, use' +
@@ -2030,6 +2081,7 @@
2030
2081
  // this means we are finished using our own buffer and we can write over it safely
2031
2082
  target = buffer;
2032
2083
  target.dataView || (target.dataView = new DataView(target.buffer, target.byteOffset, target.byteLength));
2084
+ targetView = target.dataView;
2033
2085
  position = 0;
2034
2086
  }
2035
2087
  set position (value) {
@@ -2776,7 +2828,7 @@
2776
2828
  };
2777
2829
  fullConstruct = structure.fullConstruct = function LoadedObject() {
2778
2830
  };
2779
- fullConstruct.prototype = unpackr.structPrototype ?? {};
2831
+ fullConstruct.prototype = unpackr.structPrototype || {};
2780
2832
  var prototype = construct.prototype = unpackr.structPrototype ? Object.create(unpackr.structPrototype) : {};
2781
2833
  let properties = [];
2782
2834
  let currentOffset = 0;
@@ -3317,7 +3369,7 @@
3317
3369
  {id: 2, type: 1, labels: {b: 1, c: 2}},
3318
3370
  {id: 3, type: 1, labels: {d: 1, e: 2}}
3319
3371
  ];
3320
-
3372
+
3321
3373
  var alternatives = [
3322
3374
  {useRecords: false}, // 88 bytes
3323
3375
  {useRecords: true}, // 58 bytes
@@ -3329,7 +3381,7 @@
3329
3381
  let packr = new Packr(o);
3330
3382
  var serialized = packr.pack(data);
3331
3383
  var deserialized = packr.unpack(serialized);
3332
- assert.deepEqual(deserialized, data);
3384
+ assert.deepEqual(deserialized, data);
3333
3385
  }
3334
3386
  });
3335
3387
 
@@ -3451,7 +3503,7 @@
3451
3503
  });
3452
3504
 
3453
3505
  test('BigInt', function() {
3454
- let packr = new Packr({useBigIntExtension: true});
3506
+ let packr = new Packr({ useBigIntExtension: true });
3455
3507
  let data = {
3456
3508
  a: 3333333333333333333333333333n,
3457
3509
  b: 1234567890123456789012345678901234567890n,
@@ -3459,10 +3511,28 @@
3459
3511
  d: -352523523642364364364264264264264264262642642n,
3460
3512
  e: 0xffffffffffffffffffffffffffn,
3461
3513
  f: -0xffffffffffffffffffffffffffn,
3462
- o: -12345678901234567890n,
3463
- array: [],
3514
+ g: (1234n << 123n) ^ (5678n << 56n) ^ 890n,
3515
+ h: (-1234n << 123n) ^ (5678n << 56n) ^ 890n,
3516
+ i: (1234n << 1234n) ^ (5678n << 567n) ^ 890n,
3517
+ j: (-1234n << 1234n) ^ (5678n << 567n) ^ 890n,
3518
+ k: 0xdeadn << 0xbeefn,
3519
+ l: -0xdeadn << 0xbeefn,
3520
+ m: 11n << 0x11111n ^ 111n,
3521
+ n: -11n << 0x11111n ^ 111n,
3522
+ o: 12345678901234567890n,
3523
+ p: -12345678901234567890n,
3524
+ exp: [],
3525
+ expexp: [],
3464
3526
  };
3465
-
3527
+
3528
+ for (let n = 1n; n.toString(16).length * 4 < 1500; n <<= 1n, n |= BigInt(Math.floor(Math.random() * 2))) {
3529
+ data.exp.push(n, -n);
3530
+ }
3531
+
3532
+ for (let n = 7n; n.toString(16).length * 4 < 150000; n *= n) {
3533
+ data.expexp.push(n, -n);
3534
+ }
3535
+
3466
3536
  let serialized = packr.pack(data);
3467
3537
  let deserialized = packr.unpack(serialized);
3468
3538
  assert.deepEqual(data, deserialized);
@@ -3689,7 +3759,7 @@
3689
3759
 
3690
3760
  test('extended class pack/unpack proxied', function(){
3691
3761
  function Extended() {
3692
-
3762
+
3693
3763
  }
3694
3764
  Extended.prototype.__call__ = function(){
3695
3765
  return this.value * 4
@@ -3700,7 +3770,7 @@
3700
3770
 
3701
3771
  var instance = function() { instance.__call__();/* callable stuff */ };
3702
3772
  Object.setPrototypeOf(instance,Extended.prototype);
3703
-
3773
+
3704
3774
  instance.value = 4;
3705
3775
  var data = instance;
3706
3776
 
@@ -3781,7 +3851,9 @@
3781
3851
  test('moreTypes: Error with causes', function() {
3782
3852
  const object = {
3783
3853
  error: new Error('test'),
3784
- errorWithCause: new Error('test-1', { cause: new Error('test-2')}),
3854
+ errorWithCause: new Error('test-1', { cause: new Error('test-2') }),
3855
+ type: new TypeError(),
3856
+ range: new RangeError('test', { cause: [1, 2] }),
3785
3857
  };
3786
3858
  const packr = new Packr({
3787
3859
  moreTypes: true,
@@ -3794,6 +3866,12 @@
3794
3866
  assert.equal(deserialized.errorWithCause.message, object.errorWithCause.message);
3795
3867
  assert.equal(deserialized.errorWithCause.cause.message, object.errorWithCause.cause.message);
3796
3868
  assert.equal(deserialized.errorWithCause.cause.cause, object.errorWithCause.cause.cause);
3869
+ assert.equal(deserialized.type.message, object.type.message);
3870
+ assert.equal(deserialized.range.message, object.range.message);
3871
+ assert.deepEqual(deserialized.range.cause, object.range.cause);
3872
+ assert(deserialized.error instanceof Error);
3873
+ assert(deserialized.type instanceof TypeError);
3874
+ assert(deserialized.range instanceof RangeError);
3797
3875
  });
3798
3876
 
3799
3877
  test('structured cloning: self reference', function() {
@@ -4022,7 +4100,7 @@
4022
4100
  getStructures() {
4023
4101
  return structures
4024
4102
  },
4025
- saveStructures(structures) {
4103
+ saveStructures(structures) {
4026
4104
  },
4027
4105
  maxSharedStructures: 100
4028
4106
  });
@@ -4030,7 +4108,7 @@
4030
4108
  getStructures() {
4031
4109
  return structures2
4032
4110
  },
4033
- saveStructures(structures) {
4111
+ saveStructures(structures) {
4034
4112
  },
4035
4113
  maxSharedStructures: 100
4036
4114
  });