msgpackr 1.9.9 → 1.10.0

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
@@ -988,6 +988,17 @@ const recordDefinition = (id, highByte) => {
988
988
  currentExtensions[0] = () => {}; // notepack defines extension 0 to mean undefined, so use that as the default here
989
989
  currentExtensions[0].noBuffer = true;
990
990
 
991
+ currentExtensions[0x42] = (data) => {
992
+ // decode bigint
993
+ let length = data.length;
994
+ let value = BigInt(data[0] & 0x80 ? data[0] - 0x100 : data[0]);
995
+ for (let i = 1; i < length; i++) {
996
+ value <<= 8n;
997
+ value += BigInt(data[i]);
998
+ }
999
+ return value;
1000
+ };
1001
+
991
1002
  let errors = { Error, TypeError, ReferenceError };
992
1003
  currentExtensions[0x65] = () => {
993
1004
  let data = read();
@@ -1273,6 +1284,7 @@ class Packr extends Unpackr {
1273
1284
  }
1274
1285
  if (hasSharedUpdate)
1275
1286
  hasSharedUpdate = false;
1287
+ let encodingError;
1276
1288
  try {
1277
1289
  if (packr.randomAccessStructure && value && value.constructor && value.constructor === Object)
1278
1290
  writeStruct(value);
@@ -1323,6 +1335,9 @@ class Packr extends Unpackr {
1323
1335
  return target
1324
1336
  }
1325
1337
  return target.subarray(start, position) // position can change if we call pack again in saveStructures, so we get the buffer now
1338
+ } catch(error) {
1339
+ encodingError = error;
1340
+ throw error;
1326
1341
  } finally {
1327
1342
  if (structures) {
1328
1343
  resetStructures();
@@ -1331,12 +1346,14 @@ class Packr extends Unpackr {
1331
1346
  // we can't rely on start/end with REUSE_BUFFER_MODE since they will (probably) change when we save
1332
1347
  let returnBuffer = target.subarray(start, position);
1333
1348
  let newSharedData = prepareStructures$1(structures, packr);
1334
- if (packr.saveStructures(newSharedData, newSharedData.isCompatible) === false) {
1335
- // get updated structures and try again if the update failed
1336
- return packr.pack(value, encodeOptions)
1349
+ if (!encodingError) { // TODO: If there is an encoding error, should make the structures as uninitialized so they get rebuilt next time
1350
+ if (packr.saveStructures(newSharedData, newSharedData.isCompatible) === false) {
1351
+ // get updated structures and try again if the update failed
1352
+ return packr.pack(value, encodeOptions)
1353
+ }
1354
+ packr.lastNamedStructuresLength = sharedLength;
1355
+ return returnBuffer
1337
1356
  }
1338
- packr.lastNamedStructuresLength = sharedLength;
1339
- return returnBuffer
1340
1357
  }
1341
1358
  }
1342
1359
  if (encodeOptions & RESET_BUFFER_MODE)
@@ -1678,8 +1695,26 @@ class Packr extends Unpackr {
1678
1695
  if (this.largeBigIntToFloat) {
1679
1696
  target[position++] = 0xcb;
1680
1697
  targetView.setFloat64(position, Number(value));
1698
+ } else if (this.useBigIntExtension && value < 2n**(1023n) && value > -(2n**(1023n))) {
1699
+ target[position++] = 0xc7;
1700
+ position++;
1701
+ target[position++] = 0x42; // "B" for BigInt
1702
+ let bytes = [];
1703
+ let alignedSign;
1704
+ do {
1705
+ let byte = value & 0xffn;
1706
+ alignedSign = (byte & 0x80n) === (value < 0n ? 0x80n : 0n);
1707
+ bytes.push(byte);
1708
+ value >>= 8n;
1709
+ } while (!((value === 0n || value === -1n) && alignedSign));
1710
+ target[position-2] = bytes.length;
1711
+ for (let i = bytes.length; i > 0;) {
1712
+ target[position++] = Number(bytes[--i]);
1713
+ }
1714
+ return
1681
1715
  } else {
1682
- throw new RangeError(value + ' was too large to fit in MessagePack 64-bit integer format, set largeBigIntToFloat to convert to float-64')
1716
+ throw new RangeError(value + ' was too large to fit in MessagePack 64-bit integer format, use' +
1717
+ ' useBigIntExtension or set largeBigIntToFloat to convert to float-64')
1683
1718
  }
1684
1719
  }
1685
1720
  position += 8;
@@ -2434,6 +2469,8 @@ function writeStruct(object, target, encodingStart, position, structures, makeRo
2434
2469
  position = updatedPosition;
2435
2470
  } else queuedReferences.push(key, value, keyIndex);
2436
2471
  break;
2472
+ default:
2473
+ queuedReferences.push(key, value, keyIndex);
2437
2474
  }
2438
2475
  keyIndex++;
2439
2476
  }