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/index.js CHANGED
@@ -932,6 +932,17 @@
932
932
  currentExtensions[0] = () => {}; // notepack defines extension 0 to mean undefined, so use that as the default here
933
933
  currentExtensions[0].noBuffer = true;
934
934
 
935
+ currentExtensions[0x42] = (data) => {
936
+ // decode bigint
937
+ let length = data.length;
938
+ let value = BigInt(data[0] & 0x80 ? data[0] - 0x100 : data[0]);
939
+ for (let i = 1; i < length; i++) {
940
+ value <<= 8n;
941
+ value += BigInt(data[i]);
942
+ }
943
+ return value;
944
+ };
945
+
935
946
  let errors = { Error, TypeError, ReferenceError };
936
947
  currentExtensions[0x65] = () => {
937
948
  let data = read();
@@ -1206,6 +1217,7 @@
1206
1217
  }
1207
1218
  if (hasSharedUpdate)
1208
1219
  hasSharedUpdate = false;
1220
+ let encodingError;
1209
1221
  try {
1210
1222
  if (packr.randomAccessStructure && value && value.constructor && value.constructor === Object)
1211
1223
  writeStruct(value);
@@ -1256,6 +1268,9 @@
1256
1268
  return target
1257
1269
  }
1258
1270
  return target.subarray(start, position) // position can change if we call pack again in saveStructures, so we get the buffer now
1271
+ } catch(error) {
1272
+ encodingError = error;
1273
+ throw error;
1259
1274
  } finally {
1260
1275
  if (structures) {
1261
1276
  resetStructures();
@@ -1264,12 +1279,14 @@
1264
1279
  // we can't rely on start/end with REUSE_BUFFER_MODE since they will (probably) change when we save
1265
1280
  let returnBuffer = target.subarray(start, position);
1266
1281
  let newSharedData = prepareStructures(structures, packr);
1267
- if (packr.saveStructures(newSharedData, newSharedData.isCompatible) === false) {
1268
- // get updated structures and try again if the update failed
1269
- return packr.pack(value, encodeOptions)
1282
+ if (!encodingError) { // TODO: If there is an encoding error, should make the structures as uninitialized so they get rebuilt next time
1283
+ if (packr.saveStructures(newSharedData, newSharedData.isCompatible) === false) {
1284
+ // get updated structures and try again if the update failed
1285
+ return packr.pack(value, encodeOptions)
1286
+ }
1287
+ packr.lastNamedStructuresLength = sharedLength;
1288
+ return returnBuffer
1270
1289
  }
1271
- packr.lastNamedStructuresLength = sharedLength;
1272
- return returnBuffer
1273
1290
  }
1274
1291
  }
1275
1292
  if (encodeOptions & RESET_BUFFER_MODE)
@@ -1611,8 +1628,26 @@
1611
1628
  if (this.largeBigIntToFloat) {
1612
1629
  target[position++] = 0xcb;
1613
1630
  targetView.setFloat64(position, Number(value));
1631
+ } else if (this.useBigIntExtension && value < 2n**(1023n) && value > -(2n**(1023n))) {
1632
+ target[position++] = 0xc7;
1633
+ position++;
1634
+ target[position++] = 0x42; // "B" for BigInt
1635
+ let bytes = [];
1636
+ let alignedSign;
1637
+ do {
1638
+ let byte = value & 0xffn;
1639
+ alignedSign = (byte & 0x80n) === (value < 0n ? 0x80n : 0n);
1640
+ bytes.push(byte);
1641
+ value >>= 8n;
1642
+ } while (!((value === 0n || value === -1n) && alignedSign));
1643
+ target[position-2] = bytes.length;
1644
+ for (let i = bytes.length; i > 0;) {
1645
+ target[position++] = Number(bytes[--i]);
1646
+ }
1647
+ return
1614
1648
  } else {
1615
- throw new RangeError(value + ' was too large to fit in MessagePack 64-bit integer format, set largeBigIntToFloat to convert to float-64')
1649
+ throw new RangeError(value + ' was too large to fit in MessagePack 64-bit integer format, use' +
1650
+ ' useBigIntExtension or set largeBigIntToFloat to convert to float-64')
1616
1651
  }
1617
1652
  }
1618
1653
  position += 8;