msgpackr 1.9.8 → 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)
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;
@@ -1927,7 +1962,7 @@ class Packr extends Unpackr {
1927
1962
  }
1928
1963
  };
1929
1964
  const writeStruct = (object, safePrototype) => {
1930
- let newPosition = writeStructSlots(object, target, position, structures, makeRoom, (value, newPosition, notifySharedUpdate) => {
1965
+ let newPosition = writeStructSlots(object, target, start, position, structures, makeRoom, (value, newPosition, notifySharedUpdate) => {
1931
1966
  if (notifySharedUpdate)
1932
1967
  return hasSharedUpdate = true;
1933
1968
  position = newPosition;
@@ -2230,7 +2265,7 @@ const encodeUtf8 = hasNodeBuffer ? function(target, string, position) {
2230
2265
  return textEncoder.encodeInto(string, target.subarray(position)).written
2231
2266
  } : false;
2232
2267
  setWriteStructSlots(writeStruct, prepareStructures);
2233
- function writeStruct(object, target, position, structures, makeRoom, pack, packr) {
2268
+ function writeStruct(object, target, encodingStart, position, structures, makeRoom, pack, packr) {
2234
2269
  let typedStructs = packr.typedStructs || (packr.typedStructs = []);
2235
2270
  // note that we rely on pack.js to load stored structures before we get to this point
2236
2271
  let targetView = target.dataView;
@@ -2238,12 +2273,12 @@ function writeStruct(object, target, position, structures, makeRoom, pack, packr
2238
2273
  let safeEnd = target.length - 10;
2239
2274
  let start = position;
2240
2275
  if (position > safeEnd) {
2241
- let lastStart = start;
2242
2276
  target = makeRoom(position);
2243
2277
  targetView = target.dataView;
2244
- position -= lastStart;
2245
- refsStartPosition -= lastStart;
2246
- start = 0;
2278
+ position -= encodingStart;
2279
+ start -= encodingStart;
2280
+ refsStartPosition -= encodingStart;
2281
+ encodingStart = 0;
2247
2282
  safeEnd = target.length - 10;
2248
2283
  }
2249
2284
 
@@ -2281,13 +2316,13 @@ function writeStruct(object, target, position, structures, makeRoom, pack, packr
2281
2316
  };
2282
2317
  }
2283
2318
  if (position > safeEnd) {
2284
- let lastStart = start;
2285
2319
  target = makeRoom(position);
2286
2320
  targetView = target.dataView;
2287
- position -= lastStart;
2288
- refsStartPosition -= lastStart;
2289
- refPosition -= lastStart;
2290
- start = 0;
2321
+ position -= encodingStart;
2322
+ start -= encodingStart;
2323
+ refsStartPosition -= encodingStart;
2324
+ refPosition -= encodingStart;
2325
+ encodingStart = 0;
2291
2326
  safeEnd = target.length - 10;
2292
2327
  }
2293
2328
  switch (typeof value) {
@@ -2326,13 +2361,13 @@ function writeStruct(object, target, position, structures, makeRoom, pack, packr
2326
2361
  let strLength = value.length;
2327
2362
  refOffset = refPosition - refsStartPosition;
2328
2363
  if ((strLength << 2) + refPosition > safeEnd) {
2329
- let lastStart = start;
2330
2364
  target = makeRoom((strLength << 2) + refPosition);
2331
2365
  targetView = target.dataView;
2332
- position -= lastStart;
2333
- refsStartPosition -= lastStart;
2334
- refPosition -= lastStart;
2335
- start = 0;
2366
+ position -= encodingStart;
2367
+ start -= encodingStart;
2368
+ refsStartPosition -= encodingStart;
2369
+ refPosition -= encodingStart;
2370
+ encodingStart = 0;
2336
2371
  safeEnd = target.length - 10;
2337
2372
  }
2338
2373
  if (strLength > ((0xff00 + refOffset) >> 2)) {
@@ -2434,6 +2469,8 @@ function writeStruct(object, target, position, structures, makeRoom, pack, packr
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
  }
@@ -2491,9 +2528,10 @@ function writeStruct(object, target, position, structures, makeRoom, pack, packr
2491
2528
  refPosition = newPosition.position;
2492
2529
  targetView = newPosition.targetView;
2493
2530
  target = newPosition.target;
2494
- refsStartPosition -= start;
2495
- position -= start;
2496
- start = 0;
2531
+ refsStartPosition -= encodingStart;
2532
+ position -= encodingStart;
2533
+ start -= encodingStart;
2534
+ encodingStart = 0;
2497
2535
  } else
2498
2536
  refPosition = newPosition;
2499
2537
  if (size === 2) {
@@ -2567,7 +2605,7 @@ function writeStruct(object, target, position, structures, makeRoom, pack, packr
2567
2605
  if (refsStartPosition === refPosition)
2568
2606
  return position; // no refs
2569
2607
  typedStructs.lastStringStart = position - start;
2570
- return writeStruct(object, target, start, structures, makeRoom, pack, packr);
2608
+ return writeStruct(object, target, encodingStart, start, structures, makeRoom, pack, packr);
2571
2609
  }
2572
2610
  return refPosition;
2573
2611
  }