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.
@@ -944,21 +944,47 @@
944
944
  currentExtensions[0] = () => {}; // notepack defines extension 0 to mean undefined, so use that as the default here
945
945
  currentExtensions[0].noBuffer = true;
946
946
 
947
- currentExtensions[0x42] = (data) => {
948
- // decode bigint
949
- let length = data.length;
950
- let value = BigInt(data[0] & 0x80 ? data[0] - 0x100 : data[0]);
951
- for (let i = 1; i < length; i++) {
952
- value <<= BigInt(8);
953
- value += BigInt(data[i]);
947
+ currentExtensions[0x42] = data => {
948
+ let headLength = (data.byteLength % 8) || 8;
949
+ let head = BigInt(data[0] & 0x80 ? data[0] - 0x100 : data[0]);
950
+ for (let i = 1; i < headLength; i++) {
951
+ head <<= BigInt(8);
952
+ head += BigInt(data[i]);
954
953
  }
955
- return value;
954
+ if (data.byteLength !== headLength) {
955
+ let view = new DataView(data.buffer, data.byteOffset, data.byteLength);
956
+ let decode = (start, end) => {
957
+ let length = end - start;
958
+ if (length <= 40) {
959
+ let out = view.getBigUint64(start);
960
+ for (let i = start + 8; i < end; i += 8) {
961
+ out <<= BigInt(64n);
962
+ out |= view.getBigUint64(i);
963
+ }
964
+ return out
965
+ }
966
+ // if (length === 8) return view.getBigUint64(start)
967
+ let middle = start + (length >> 4 << 3);
968
+ let left = decode(start, middle);
969
+ let right = decode(middle, end);
970
+ return (left << BigInt((end - middle) * 8)) | right
971
+ };
972
+ head = (head << BigInt((view.byteLength - headLength) * 8)) | decode(headLength, view.byteLength);
973
+ }
974
+ return head
956
975
  };
957
976
 
958
- let errors = { Error, TypeError, ReferenceError };
977
+ let errors = {
978
+ Error, EvalError, RangeError, ReferenceError, SyntaxError, TypeError, URIError, AggregateError: typeof AggregateError === 'function' ? AggregateError : null,
979
+ };
959
980
  currentExtensions[0x65] = () => {
960
981
  let data = read();
961
- return (errors[data[0]] || Error)(data[1], { cause: data[2] })
982
+ if (!errors[data[0]]) {
983
+ let error = Error(data[1], { cause: data[2] });
984
+ error.name = data[0];
985
+ return error
986
+ }
987
+ return errors[data[0]](data[1], { cause: data[2] })
962
988
  };
963
989
 
964
990
  currentExtensions[0x69] = (data) => {
@@ -1251,7 +1277,8 @@
1251
1277
  hasSharedUpdate = false;
1252
1278
  let encodingError;
1253
1279
  try {
1254
- if (packr.randomAccessStructure && value && value.constructor && value.constructor === Object)
1280
+ if (packr.randomAccessStructure && value && value.constructor && (value.constructor === Object ||
1281
+ (value.constructor !== Map && !Array.isArray(value) && !extensionClasses.some(extClass => value instanceof extClass))))
1255
1282
  writeStruct(value);
1256
1283
  else
1257
1284
  pack(value);
@@ -1666,22 +1693,47 @@
1666
1693
  targetView.setFloat64(position, Number(value));
1667
1694
  } else if (this.largeBigIntToString) {
1668
1695
  return pack(value.toString());
1669
- } else if ((this.useBigIntExtension || this.moreTypes) && value < BigInt(2)**BigInt(1023) && value > -(BigInt(2)**BigInt(1023))) {
1670
- target[position++] = 0xc7;
1671
- position++;
1672
- target[position++] = 0x42; // "B" for BigInt
1673
- let bytes = [];
1674
- let alignedSign;
1675
- do {
1676
- let byte = value & BigInt(0xff);
1677
- alignedSign = (byte & BigInt(0x80)) === (value < BigInt(0) ? BigInt(0x80) : BigInt(0));
1678
- bytes.push(byte);
1679
- value >>= BigInt(8);
1680
- } while (!((value === BigInt(0) || value === BigInt(-1)) && alignedSign));
1681
- target[position-2] = bytes.length;
1682
- for (let i = bytes.length; i > 0;) {
1683
- target[position++] = Number(bytes[--i]);
1696
+ } else if (this.useBigIntExtension || this.moreTypes) {
1697
+ let empty = value < 0 ? BigInt(-1) : BigInt(0);
1698
+
1699
+ let array;
1700
+ if (value >> BigInt(0x10000) === empty) {
1701
+ let mask = BigInt(0x10000000000000000) - BigInt(1); // literal would overflow
1702
+ let chunks = [];
1703
+ while (true) {
1704
+ chunks.push(value & mask);
1705
+ if ((value >> BigInt(63)) === empty) break
1706
+ value >>= BigInt(64);
1707
+ }
1708
+
1709
+ array = new Uint8Array(new BigUint64Array(chunks).buffer);
1710
+ array.reverse();
1711
+ } else {
1712
+ let invert = value < 0;
1713
+ let string = (invert ? ~value : value).toString(16);
1714
+ if (string.length % 2) {
1715
+ string = '0' + string;
1716
+ } else if (parseInt(string.charAt(0), 16) >= 8) {
1717
+ string = '00' + string;
1718
+ }
1719
+
1720
+ if (hasNodeBuffer) {
1721
+ array = Buffer.from(string, 'hex');
1722
+ } else {
1723
+ array = new Uint8Array(string.length / 2);
1724
+ for (let i = 0; i < array.length; i++) {
1725
+ array[i] = parseInt(string.slice(i * 2, i * 2 + 2), 16);
1726
+ }
1727
+ }
1728
+
1729
+ if (invert) {
1730
+ for (let i = 0; i < array.length; i++) array[i] = ~array[i];
1731
+ }
1684
1732
  }
1733
+
1734
+ if (array.length + position > safeEnd)
1735
+ makeRoom(array.length + position);
1736
+ position = writeExtensionData(array, target, position, 0x42);
1685
1737
  return
1686
1738
  } else {
1687
1739
  throw new RangeError(value + ' was too large to fit in MessagePack 64-bit integer format, use' +
@@ -1969,6 +2021,7 @@
1969
2021
  // this means we are finished using our own buffer and we can write over it safely
1970
2022
  target = buffer;
1971
2023
  target.dataView || (target.dataView = new DataView(target.buffer, target.byteOffset, target.byteLength));
2024
+ targetView = target.dataView;
1972
2025
  position = 0;
1973
2026
  }
1974
2027
  set position (value) {