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/index.js CHANGED
@@ -188,7 +188,7 @@
188
188
 
189
189
  if (position == srcEnd) {
190
190
  // finished reading this source, cleanup references
191
- if (currentStructures?.restoreStructures)
191
+ if (currentStructures && currentStructures.restoreStructures)
192
192
  restoreStructures();
193
193
  currentStructures = null;
194
194
  src = null;
@@ -203,7 +203,7 @@
203
203
  // else more to read, but we are reading sequentially, so don't clear source yet
204
204
  return result
205
205
  } catch(error) {
206
- if (currentStructures?.restoreStructures)
206
+ if (currentStructures && currentStructures.restoreStructures)
207
207
  restoreStructures();
208
208
  clearSource();
209
209
  if (error instanceof RangeError || error.message.startsWith('Unexpected end of buffer') || position > srcEnd) {
@@ -815,7 +815,7 @@
815
815
  })
816
816
  }
817
817
  else
818
- throw new Error('Unknown extension type ' + type)
818
+ throw new Error('Unknown extension type ' + type)``
819
819
  }
820
820
 
821
821
  var keyCache = new Array(4096);
@@ -830,7 +830,7 @@
830
830
  return readFixedString(length)
831
831
  } else { // not cacheable, go back and do a standard read
832
832
  position--;
833
- return read()
833
+ return read().toString()
834
834
  }
835
835
  let key = ((length << 5) ^ (length > 1 ? dataView.getUint16(position) : length > 0 ? src[position] : 0)) & 0xfff;
836
836
  let entry = keyCache[key];
@@ -884,16 +884,7 @@
884
884
 
885
885
  // the registration of the record definition extension (as "r")
886
886
  const recordDefinition = (id, highByte) => {
887
- let structure;
888
- if (currentUnpackr.freezeData) {
889
- currentUnpackr.freezeData = false;
890
- try {
891
- structure = read();
892
- } finally {
893
- currentUnpackr.freezeData = true;
894
- }
895
- } else
896
- structure = read();
887
+ let structure = read().map(property => property.toString()); // ensure that all keys are strings and that the array is mutable
897
888
  let firstByte = id;
898
889
  if (highByte !== undefined) {
899
890
  id = id < 32 ? -((highByte << 5) + id) : ((highByte << 5) + id);
@@ -1186,19 +1177,45 @@
1186
1177
  writeStruct(value);
1187
1178
  else
1188
1179
  pack(value);
1189
- if (bundledStrings$1) {
1190
- writeBundles(start, pack);
1191
- }
1192
- packr.offset = position$1; // update the offset so next serialization doesn't write over our buffer, but can continue writing to same buffer sequentially
1180
+ let lastBundle = bundledStrings$1;
1181
+ if (bundledStrings$1)
1182
+ writeBundles(start, pack, 0);
1193
1183
  if (referenceMap && referenceMap.idsToInsert) {
1194
- position$1 += referenceMap.idsToInsert.length * 6;
1184
+ let idsToInsert = referenceMap.idsToInsert.sort((a, b) => a.offset > b.offset ? 1 : -1);
1185
+ let i = idsToInsert.length;
1186
+ let incrementPosition = -1;
1187
+ while (lastBundle && i > 0) {
1188
+ let insertionPoint = idsToInsert[--i].offset + start;
1189
+ if (insertionPoint < (lastBundle.stringsPosition + start) && incrementPosition === -1)
1190
+ incrementPosition = 0;
1191
+ if (insertionPoint > (lastBundle.position + start)) {
1192
+ if (incrementPosition >= 0)
1193
+ incrementPosition += 6;
1194
+ } else {
1195
+ if (incrementPosition >= 0) {
1196
+ // update the bundle reference now
1197
+ targetView.setUint32(lastBundle.position + start,
1198
+ targetView.getUint32(lastBundle.position + start) + incrementPosition);
1199
+ incrementPosition = -1; // reset
1200
+ }
1201
+ lastBundle = lastBundle.previous;
1202
+ i++;
1203
+ }
1204
+ }
1205
+ if (incrementPosition >= 0 && lastBundle) {
1206
+ // update the bundle reference now
1207
+ targetView.setUint32(lastBundle.position + start,
1208
+ targetView.getUint32(lastBundle.position + start) + incrementPosition);
1209
+ }
1210
+ position$1 += idsToInsert.length * 6;
1195
1211
  if (position$1 > safeEnd)
1196
1212
  makeRoom(position$1);
1197
1213
  packr.offset = position$1;
1198
- let serialized = insertIds(target.subarray(start, position$1), referenceMap.idsToInsert);
1214
+ let serialized = insertIds(target.subarray(start, position$1), idsToInsert);
1199
1215
  referenceMap = null;
1200
1216
  return serialized
1201
1217
  }
1218
+ packr.offset = position$1; // update the offset so next serialization doesn't write over our buffer, but can continue writing to same buffer sequentially
1202
1219
  if (encodeOptions & REUSE_BUFFER_MODE) {
1203
1220
  target.start = start;
1204
1221
  target.end = position$1;
@@ -1255,13 +1272,15 @@
1255
1272
  let maxBytes = (bundledStrings$1[0] ? bundledStrings$1[0].length * 3 + bundledStrings$1[1].length : 0) + 10;
1256
1273
  if (position$1 + maxBytes > safeEnd)
1257
1274
  target = makeRoom(position$1 + maxBytes);
1258
- 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
1275
+ let lastBundle;
1276
+ 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
1277
+ lastBundle = bundledStrings$1;
1259
1278
  target[position$1] = 0xc8; // ext 16
1260
1279
  position$1 += 3; // reserve for the writing bundle size
1261
1280
  target[position$1++] = 0x62; // 'b'
1262
1281
  extStart = position$1 - start;
1263
1282
  position$1 += 4; // reserve for writing bundle reference
1264
- writeBundles(start, pack); // write the last bundles
1283
+ writeBundles(start, pack, 0); // write the last bundles
1265
1284
  targetView.setUint16(extStart + start - 3, position$1 - start - extStart);
1266
1285
  } 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)
1267
1286
  target[position$1++] = 0xd6; // fixext 4
@@ -1270,6 +1289,7 @@
1270
1289
  position$1 += 4; // reserve for writing bundle reference
1271
1290
  }
1272
1291
  bundledStrings$1 = ['', '']; // create new ones
1292
+ bundledStrings$1.previous = lastBundle;
1273
1293
  bundledStrings$1.size = 0;
1274
1294
  bundledStrings$1.position = extStart;
1275
1295
  }
@@ -1948,7 +1968,6 @@
1948
1968
  let nextId;
1949
1969
  let distanceToMove = idsToInsert.length * 6;
1950
1970
  let lastEnd = serialized.length - distanceToMove;
1951
- idsToInsert.sort((a, b) => a.offset > b.offset ? 1 : -1);
1952
1971
  while (nextId = idsToInsert.pop()) {
1953
1972
  let offset = nextId.offset;
1954
1973
  let id = nextId.id;
@@ -1966,9 +1985,10 @@
1966
1985
  return serialized
1967
1986
  }
1968
1987
 
1969
- function writeBundles(start, pack) {
1988
+ function writeBundles(start, pack, incrementPosition) {
1970
1989
  if (bundledStrings$1.length > 0) {
1971
- targetView.setUint32(bundledStrings$1.position + start, position$1 - bundledStrings$1.position - start);
1990
+ targetView.setUint32(bundledStrings$1.position + start, position$1 + incrementPosition - bundledStrings$1.position - start);
1991
+ bundledStrings$1.stringsPosition = position$1 - start;
1972
1992
  let writeStrings = bundledStrings$1;
1973
1993
  bundledStrings$1 = null;
1974
1994
  pack(writeStrings[0]);