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/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) => {
@@ -1317,7 +1343,8 @@ class Packr extends Unpackr {
1317
1343
  hasSharedUpdate = false;
1318
1344
  let encodingError;
1319
1345
  try {
1320
- if (packr.randomAccessStructure && value && value.constructor && value.constructor === Object)
1346
+ if (packr.randomAccessStructure && value && value.constructor && (value.constructor === Object ||
1347
+ (value.constructor !== Map && !Array.isArray(value) && !extensionClasses.some(extClass => value instanceof extClass))))
1321
1348
  writeStruct(value);
1322
1349
  else
1323
1350
  pack(value);
@@ -1732,22 +1759,47 @@ class Packr extends Unpackr {
1732
1759
  targetView.setFloat64(position, Number(value));
1733
1760
  } else if (this.largeBigIntToString) {
1734
1761
  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]);
1762
+ } else if (this.useBigIntExtension || this.moreTypes) {
1763
+ let empty = value < 0 ? BigInt(-1) : BigInt(0);
1764
+
1765
+ let array;
1766
+ if (value >> BigInt(0x10000) === empty) {
1767
+ let mask = BigInt(0x10000000000000000) - BigInt(1); // literal would overflow
1768
+ let chunks = [];
1769
+ while (true) {
1770
+ chunks.push(value & mask);
1771
+ if ((value >> BigInt(63)) === empty) break
1772
+ value >>= BigInt(64);
1773
+ }
1774
+
1775
+ array = new Uint8Array(new BigUint64Array(chunks).buffer);
1776
+ array.reverse();
1777
+ } else {
1778
+ let invert = value < 0;
1779
+ let string = (invert ? ~value : value).toString(16);
1780
+ if (string.length % 2) {
1781
+ string = '0' + string;
1782
+ } else if (parseInt(string.charAt(0), 16) >= 8) {
1783
+ string = '00' + string;
1784
+ }
1785
+
1786
+ if (hasNodeBuffer$1) {
1787
+ array = Buffer.from(string, 'hex');
1788
+ } else {
1789
+ array = new Uint8Array(string.length / 2);
1790
+ for (let i = 0; i < array.length; i++) {
1791
+ array[i] = parseInt(string.slice(i * 2, i * 2 + 2), 16);
1792
+ }
1793
+ }
1794
+
1795
+ if (invert) {
1796
+ for (let i = 0; i < array.length; i++) array[i] = ~array[i];
1797
+ }
1750
1798
  }
1799
+
1800
+ if (array.length + position > safeEnd)
1801
+ makeRoom(array.length + position);
1802
+ position = writeExtensionData(array, target, position, 0x42);
1751
1803
  return
1752
1804
  } else {
1753
1805
  throw new RangeError(value + ' was too large to fit in MessagePack 64-bit integer format, use' +
@@ -2035,6 +2087,7 @@ class Packr extends Unpackr {
2035
2087
  // this means we are finished using our own buffer and we can write over it safely
2036
2088
  target = buffer;
2037
2089
  target.dataView || (target.dataView = new DataView(target.buffer, target.byteOffset, target.byteLength));
2090
+ targetView = target.dataView;
2038
2091
  position = 0;
2039
2092
  }
2040
2093
  set position (value) {
@@ -2783,7 +2836,7 @@ function readStruct(src, position, srcEnd, unpackr) {
2783
2836
  };
2784
2837
  fullConstruct = structure.fullConstruct = function LoadedObject() {
2785
2838
  };
2786
- fullConstruct.prototype = unpackr.structPrototype ?? {};
2839
+ fullConstruct.prototype = unpackr.structPrototype || {};
2787
2840
  var prototype = construct.prototype = unpackr.structPrototype ? Object.create(unpackr.structPrototype) : {};
2788
2841
  let properties = [];
2789
2842
  let currentOffset = 0;
@@ -3244,6 +3297,9 @@ exports.FLOAT32_OPTIONS = FLOAT32_OPTIONS;
3244
3297
  exports.NEVER = NEVER;
3245
3298
  exports.Packr = Packr;
3246
3299
  exports.PackrStream = PackrStream;
3300
+ exports.RESERVE_START_SPACE = RESERVE_START_SPACE;
3301
+ exports.RESET_BUFFER_MODE = RESET_BUFFER_MODE;
3302
+ exports.REUSE_BUFFER_MODE = REUSE_BUFFER_MODE;
3247
3303
  exports.Unpackr = Unpackr;
3248
3304
  exports.UnpackrStream = UnpackrStream;
3249
3305
  exports.addExtension = addExtension;