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/node.cjs CHANGED
@@ -999,21 +999,47 @@ const recordDefinition = (id, highByte) => {
999
999
  currentExtensions[0] = () => {}; // notepack defines extension 0 to mean undefined, so use that as the default here
1000
1000
  currentExtensions[0].noBuffer = true;
1001
1001
 
1002
- currentExtensions[0x42] = (data) => {
1003
- // decode bigint
1004
- let length = data.length;
1005
- let value = BigInt(data[0] & 0x80 ? data[0] - 0x100 : data[0]);
1006
- for (let i = 1; i < length; i++) {
1007
- value <<= BigInt(8);
1008
- value += BigInt(data[i]);
1009
- }
1010
- return value;
1002
+ currentExtensions[0x42] = data => {
1003
+ let headLength = (data.byteLength % 8) || 8;
1004
+ let head = BigInt(data[0] & 0x80 ? data[0] - 0x100 : data[0]);
1005
+ for (let i = 1; i < headLength; i++) {
1006
+ head <<= BigInt(8);
1007
+ head += BigInt(data[i]);
1008
+ }
1009
+ if (data.byteLength !== headLength) {
1010
+ let view = new DataView(data.buffer, data.byteOffset, data.byteLength);
1011
+ let decode = (start, end) => {
1012
+ let length = end - start;
1013
+ if (length <= 40) {
1014
+ let out = view.getBigUint64(start);
1015
+ for (let i = start + 8; i < end; i += 8) {
1016
+ out <<= BigInt(64n);
1017
+ out |= view.getBigUint64(i);
1018
+ }
1019
+ return out
1020
+ }
1021
+ // if (length === 8) return view.getBigUint64(start)
1022
+ let middle = start + (length >> 4 << 3);
1023
+ let left = decode(start, middle);
1024
+ let right = decode(middle, end);
1025
+ return (left << BigInt((end - middle) * 8)) | right
1026
+ };
1027
+ head = (head << BigInt((view.byteLength - headLength) * 8)) | decode(headLength, view.byteLength);
1028
+ }
1029
+ return head
1011
1030
  };
1012
1031
 
1013
- let errors = { Error, TypeError, ReferenceError };
1032
+ let errors = {
1033
+ Error, EvalError, RangeError, ReferenceError, SyntaxError, TypeError, URIError, AggregateError: typeof AggregateError === 'function' ? AggregateError : null,
1034
+ };
1014
1035
  currentExtensions[0x65] = () => {
1015
1036
  let data = read();
1016
- return (errors[data[0]] || Error)(data[1], { cause: data[2] })
1037
+ if (!errors[data[0]]) {
1038
+ let error = Error(data[1], { cause: data[2] });
1039
+ error.name = data[0];
1040
+ return error
1041
+ }
1042
+ return errors[data[0]](data[1], { cause: data[2] })
1017
1043
  };
1018
1044
 
1019
1045
  currentExtensions[0x69] = (data) => {
@@ -1732,22 +1758,47 @@ class Packr extends Unpackr {
1732
1758
  targetView.setFloat64(position, Number(value));
1733
1759
  } else if (this.largeBigIntToString) {
1734
1760
  return pack(value.toString());
1735
- } else if ((this.useBigIntExtension || this.moreTypes) && value < BigInt(2)**BigInt(1023) && value > -(BigInt(2)**BigInt(1023))) {
1736
- target[position++] = 0xc7;
1737
- position++;
1738
- target[position++] = 0x42; // "B" for BigInt
1739
- let bytes = [];
1740
- let alignedSign;
1741
- do {
1742
- let byte = value & BigInt(0xff);
1743
- alignedSign = (byte & BigInt(0x80)) === (value < BigInt(0) ? BigInt(0x80) : BigInt(0));
1744
- bytes.push(byte);
1745
- value >>= BigInt(8);
1746
- } while (!((value === BigInt(0) || value === BigInt(-1)) && alignedSign));
1747
- target[position-2] = bytes.length;
1748
- for (let i = bytes.length; i > 0;) {
1749
- target[position++] = Number(bytes[--i]);
1761
+ } else if (this.useBigIntExtension || this.moreTypes) {
1762
+ let empty = value < 0 ? BigInt(-1) : BigInt(0);
1763
+
1764
+ let array;
1765
+ if (value >> BigInt(0x10000) === empty) {
1766
+ let mask = BigInt(0x10000000000000000) - BigInt(1); // literal would overflow
1767
+ let chunks = [];
1768
+ while (true) {
1769
+ chunks.push(value & mask);
1770
+ if ((value >> BigInt(63)) === empty) break
1771
+ value >>= BigInt(64);
1772
+ }
1773
+
1774
+ array = new Uint8Array(new BigUint64Array(chunks).buffer);
1775
+ array.reverse();
1776
+ } else {
1777
+ let invert = value < 0;
1778
+ let string = (invert ? ~value : value).toString(16);
1779
+ if (string.length % 2) {
1780
+ string = '0' + string;
1781
+ } else if (parseInt(string.charAt(0), 16) >= 8) {
1782
+ string = '00' + string;
1783
+ }
1784
+
1785
+ if (hasNodeBuffer$1) {
1786
+ array = Buffer.from(string, 'hex');
1787
+ } else {
1788
+ array = new Uint8Array(string.length / 2);
1789
+ for (let i = 0; i < array.length; i++) {
1790
+ array[i] = parseInt(string.slice(i * 2, i * 2 + 2), 16);
1791
+ }
1792
+ }
1793
+
1794
+ if (invert) {
1795
+ for (let i = 0; i < array.length; i++) array[i] = ~array[i];
1796
+ }
1750
1797
  }
1798
+
1799
+ if (array.length + position > safeEnd)
1800
+ makeRoom(array.length + position);
1801
+ position = writeExtensionData(array, target, position, 0x42);
1751
1802
  return
1752
1803
  } else {
1753
1804
  throw new RangeError(value + ' was too large to fit in MessagePack 64-bit integer format, use' +
@@ -2035,6 +2086,7 @@ class Packr extends Unpackr {
2035
2086
  // this means we are finished using our own buffer and we can write over it safely
2036
2087
  target = buffer;
2037
2088
  target.dataView || (target.dataView = new DataView(target.buffer, target.byteOffset, target.byteLength));
2089
+ targetView = target.dataView;
2038
2090
  position = 0;
2039
2091
  }
2040
2092
  set position (value) {
@@ -2783,7 +2835,7 @@ function readStruct(src, position, srcEnd, unpackr) {
2783
2835
  };
2784
2836
  fullConstruct = structure.fullConstruct = function LoadedObject() {
2785
2837
  };
2786
- fullConstruct.prototype = unpackr.structPrototype ?? {};
2838
+ fullConstruct.prototype = unpackr.structPrototype || {};
2787
2839
  var prototype = construct.prototype = unpackr.structPrototype ? Object.create(unpackr.structPrototype) : {};
2788
2840
  let properties = [];
2789
2841
  let currentOffset = 0;