msgpackr 1.7.0 → 1.7.2

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
@@ -196,7 +196,7 @@ function checkedRead(options) {
196
196
 
197
197
  if (position == srcEnd) {
198
198
  // finished reading this source, cleanup references
199
- if (currentStructures?.restoreStructures)
199
+ if (currentStructures && currentStructures.restoreStructures)
200
200
  restoreStructures();
201
201
  currentStructures = null;
202
202
  src = null;
@@ -211,7 +211,7 @@ function checkedRead(options) {
211
211
  // else more to read, but we are reading sequentially, so don't clear source yet
212
212
  return result
213
213
  } catch(error) {
214
- if (currentStructures?.restoreStructures)
214
+ if (currentStructures && currentStructures.restoreStructures)
215
215
  restoreStructures();
216
216
  clearSource();
217
217
  if (error instanceof RangeError || error.message.startsWith('Unexpected end of buffer') || position > srcEnd) {
@@ -872,7 +872,7 @@ function readExt(length) {
872
872
  })
873
873
  }
874
874
  else
875
- throw new Error('Unknown extension type ' + type)
875
+ throw new Error('Unknown extension type ' + type)``
876
876
  }
877
877
 
878
878
  var keyCache = new Array(4096);
@@ -887,7 +887,7 @@ function readKey() {
887
887
  return readFixedString(length)
888
888
  } else { // not cacheable, go back and do a standard read
889
889
  position--;
890
- return read()
890
+ return read().toString()
891
891
  }
892
892
  let key = ((length << 5) ^ (length > 1 ? dataView.getUint16(position) : length > 0 ? src[position] : 0)) & 0xfff;
893
893
  let entry = keyCache[key];
@@ -941,16 +941,7 @@ function readKey() {
941
941
 
942
942
  // the registration of the record definition extension (as "r")
943
943
  const recordDefinition = (id, highByte) => {
944
- let structure;
945
- if (currentUnpackr.freezeData) {
946
- currentUnpackr.freezeData = false;
947
- try {
948
- structure = read();
949
- } finally {
950
- currentUnpackr.freezeData = true;
951
- }
952
- } else
953
- structure = read();
944
+ let structure = read().map(property => property.toString()); // ensure that all keys are strings and that the array is mutable
954
945
  let firstByte = id;
955
946
  if (highByte !== undefined) {
956
947
  id = id < 32 ? -((highByte << 5) + id) : ((highByte << 5) + id);
@@ -1254,19 +1245,45 @@ class Packr extends Unpackr {
1254
1245
  writeStruct(value);
1255
1246
  else
1256
1247
  pack(value);
1257
- if (bundledStrings$1) {
1258
- writeBundles(start, pack);
1259
- }
1260
- packr.offset = position$1; // update the offset so next serialization doesn't write over our buffer, but can continue writing to same buffer sequentially
1248
+ let lastBundle = bundledStrings$1;
1249
+ if (bundledStrings$1)
1250
+ writeBundles(start, pack, 0);
1261
1251
  if (referenceMap && referenceMap.idsToInsert) {
1262
- position$1 += referenceMap.idsToInsert.length * 6;
1252
+ let idsToInsert = referenceMap.idsToInsert.sort((a, b) => a.offset > b.offset ? 1 : -1);
1253
+ let i = idsToInsert.length;
1254
+ let incrementPosition = -1;
1255
+ while (lastBundle && i > 0) {
1256
+ let insertionPoint = idsToInsert[--i].offset + start;
1257
+ if (insertionPoint < (lastBundle.stringsPosition + start) && incrementPosition === -1)
1258
+ incrementPosition = 0;
1259
+ if (insertionPoint > (lastBundle.position + start)) {
1260
+ if (incrementPosition >= 0)
1261
+ incrementPosition += 6;
1262
+ } else {
1263
+ if (incrementPosition >= 0) {
1264
+ // update the bundle reference now
1265
+ targetView.setUint32(lastBundle.position + start,
1266
+ targetView.getUint32(lastBundle.position + start) + incrementPosition);
1267
+ incrementPosition = -1; // reset
1268
+ }
1269
+ lastBundle = lastBundle.previous;
1270
+ i++;
1271
+ }
1272
+ }
1273
+ if (incrementPosition >= 0 && lastBundle) {
1274
+ // update the bundle reference now
1275
+ targetView.setUint32(lastBundle.position + start,
1276
+ targetView.getUint32(lastBundle.position + start) + incrementPosition);
1277
+ }
1278
+ position$1 += idsToInsert.length * 6;
1263
1279
  if (position$1 > safeEnd)
1264
1280
  makeRoom(position$1);
1265
1281
  packr.offset = position$1;
1266
- let serialized = insertIds(target.subarray(start, position$1), referenceMap.idsToInsert);
1282
+ let serialized = insertIds(target.subarray(start, position$1), idsToInsert);
1267
1283
  referenceMap = null;
1268
1284
  return serialized
1269
1285
  }
1286
+ packr.offset = position$1; // update the offset so next serialization doesn't write over our buffer, but can continue writing to same buffer sequentially
1270
1287
  if (encodeOptions & REUSE_BUFFER_MODE) {
1271
1288
  target.start = start;
1272
1289
  target.end = position$1;
@@ -1323,13 +1340,15 @@ class Packr extends Unpackr {
1323
1340
  let maxBytes = (bundledStrings$1[0] ? bundledStrings$1[0].length * 3 + bundledStrings$1[1].length : 0) + 10;
1324
1341
  if (position$1 + maxBytes > safeEnd)
1325
1342
  target = makeRoom(position$1 + maxBytes);
1326
- if (bundledStrings$1.position) { // here we use the 0x62 extension to write the last bundle and reserve sapce for the reference pointer to the next/current bundle
1343
+ let lastBundle;
1344
+ if (bundledStrings$1.position) { // here we use the 0x62 extension to write the last bundle and reserve space for the reference pointer to the next/current bundle
1345
+ lastBundle = bundledStrings$1;
1327
1346
  target[position$1] = 0xc8; // ext 16
1328
1347
  position$1 += 3; // reserve for the writing bundle size
1329
1348
  target[position$1++] = 0x62; // 'b'
1330
1349
  extStart = position$1 - start;
1331
1350
  position$1 += 4; // reserve for writing bundle reference
1332
- writeBundles(start, pack); // write the last bundles
1351
+ writeBundles(start, pack, 0); // write the last bundles
1333
1352
  targetView.setUint16(extStart + start - 3, position$1 - start - extStart);
1334
1353
  } else { // here we use the 0x62 extension just to reserve the space for the reference pointer to the bundle (will be updated once the bundle is written)
1335
1354
  target[position$1++] = 0xd6; // fixext 4
@@ -1338,6 +1357,7 @@ class Packr extends Unpackr {
1338
1357
  position$1 += 4; // reserve for writing bundle reference
1339
1358
  }
1340
1359
  bundledStrings$1 = ['', '']; // create new ones
1360
+ bundledStrings$1.previous = lastBundle;
1341
1361
  bundledStrings$1.size = 0;
1342
1362
  bundledStrings$1.position = extStart;
1343
1363
  }
@@ -2027,7 +2047,6 @@ function insertIds(serialized, idsToInsert) {
2027
2047
  let nextId;
2028
2048
  let distanceToMove = idsToInsert.length * 6;
2029
2049
  let lastEnd = serialized.length - distanceToMove;
2030
- idsToInsert.sort((a, b) => a.offset > b.offset ? 1 : -1);
2031
2050
  while (nextId = idsToInsert.pop()) {
2032
2051
  let offset = nextId.offset;
2033
2052
  let id = nextId.id;
@@ -2045,9 +2064,10 @@ function insertIds(serialized, idsToInsert) {
2045
2064
  return serialized
2046
2065
  }
2047
2066
 
2048
- function writeBundles(start, pack) {
2067
+ function writeBundles(start, pack, incrementPosition) {
2049
2068
  if (bundledStrings$1.length > 0) {
2050
- targetView.setUint32(bundledStrings$1.position + start, position$1 - bundledStrings$1.position - start);
2069
+ targetView.setUint32(bundledStrings$1.position + start, position$1 + incrementPosition - bundledStrings$1.position - start);
2070
+ bundledStrings$1.stringsPosition = position$1 - start;
2051
2071
  let writeStrings = bundledStrings$1;
2052
2072
  bundledStrings$1 = null;
2053
2073
  pack(writeStrings[0]);
@@ -2540,7 +2560,7 @@ function readStruct$1(src, position, srcEnd, unpackr) {
2540
2560
  case 27: recordId = src[position++] + (src[position++] << 8) + (src[position++] << 16) + (src[position++] << 24); break;
2541
2561
  }
2542
2562
  }
2543
- let structure = unpackr.typedStructs?.[recordId];
2563
+ let structure = unpackr.typedStructs && unpackr.typedStructs[recordId];
2544
2564
  if (!structure) {
2545
2565
  // copy src buffer because getStructures will override it
2546
2566
  src = Uint8Array.prototype.slice.call(src, position, srcEnd);
@@ -2832,7 +2852,7 @@ function prepareStructures$1(structures, packr) {
2832
2852
  packr._mergeStructures(existing);
2833
2853
  return compatible;
2834
2854
  };
2835
- packr.lastTypedStructuresLength = packr.typedStructs?.length;
2855
+ packr.lastTypedStructuresLength = packr.typedStructs && packr.typedStructs.length;
2836
2856
  return structures;
2837
2857
  }
2838
2858