msgpackr 1.7.1 → 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
@@ -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,20 +1177,44 @@
1186
1177
  writeStruct(value);
1187
1178
  else
1188
1179
  pack(value);
1180
+ let lastBundle = bundledStrings$1;
1181
+ if (bundledStrings$1)
1182
+ writeBundles(start, pack, 0);
1189
1183
  if (referenceMap && referenceMap.idsToInsert) {
1190
- let incrementPosition = referenceMap.idsToInsert.length * 6;
1191
- if (bundledStrings$1)
1192
- writeBundles(start, pack, incrementPosition);
1193
- position$1 += incrementPosition;
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;
1194
1211
  if (position$1 > safeEnd)
1195
1212
  makeRoom(position$1);
1196
1213
  packr.offset = position$1;
1197
- let serialized = insertIds(target.subarray(start, position$1), referenceMap.idsToInsert);
1214
+ let serialized = insertIds(target.subarray(start, position$1), idsToInsert);
1198
1215
  referenceMap = null;
1199
1216
  return serialized
1200
1217
  }
1201
- if (bundledStrings$1)
1202
- writeBundles(start, pack, 0);
1203
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
1204
1219
  if (encodeOptions & REUSE_BUFFER_MODE) {
1205
1220
  target.start = start;
@@ -1257,13 +1272,15 @@
1257
1272
  let maxBytes = (bundledStrings$1[0] ? bundledStrings$1[0].length * 3 + bundledStrings$1[1].length : 0) + 10;
1258
1273
  if (position$1 + maxBytes > safeEnd)
1259
1274
  target = makeRoom(position$1 + maxBytes);
1260
- 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;
1261
1278
  target[position$1] = 0xc8; // ext 16
1262
1279
  position$1 += 3; // reserve for the writing bundle size
1263
1280
  target[position$1++] = 0x62; // 'b'
1264
1281
  extStart = position$1 - start;
1265
1282
  position$1 += 4; // reserve for writing bundle reference
1266
- writeBundles(start, pack); // write the last bundles
1283
+ writeBundles(start, pack, 0); // write the last bundles
1267
1284
  targetView.setUint16(extStart + start - 3, position$1 - start - extStart);
1268
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)
1269
1286
  target[position$1++] = 0xd6; // fixext 4
@@ -1272,6 +1289,7 @@
1272
1289
  position$1 += 4; // reserve for writing bundle reference
1273
1290
  }
1274
1291
  bundledStrings$1 = ['', '']; // create new ones
1292
+ bundledStrings$1.previous = lastBundle;
1275
1293
  bundledStrings$1.size = 0;
1276
1294
  bundledStrings$1.position = extStart;
1277
1295
  }
@@ -1950,7 +1968,6 @@
1950
1968
  let nextId;
1951
1969
  let distanceToMove = idsToInsert.length * 6;
1952
1970
  let lastEnd = serialized.length - distanceToMove;
1953
- idsToInsert.sort((a, b) => a.offset > b.offset ? 1 : -1);
1954
1971
  while (nextId = idsToInsert.pop()) {
1955
1972
  let offset = nextId.offset;
1956
1973
  let id = nextId.id;
@@ -1971,6 +1988,7 @@
1971
1988
  function writeBundles(start, pack, incrementPosition) {
1972
1989
  if (bundledStrings$1.length > 0) {
1973
1990
  targetView.setUint32(bundledStrings$1.position + start, position$1 + incrementPosition - bundledStrings$1.position - start);
1991
+ bundledStrings$1.stringsPosition = position$1 - start;
1974
1992
  let writeStrings = bundledStrings$1;
1975
1993
  bundledStrings$1 = null;
1976
1994
  pack(writeStrings[0]);