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