msgpackr 1.7.0 → 1.7.1

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/test.js CHANGED
@@ -190,7 +190,7 @@
190
190
 
191
191
  if (position == srcEnd) {
192
192
  // finished reading this source, cleanup references
193
- if (currentStructures?.restoreStructures)
193
+ if (currentStructures && currentStructures.restoreStructures)
194
194
  restoreStructures();
195
195
  currentStructures = null;
196
196
  src = null;
@@ -205,7 +205,7 @@
205
205
  // else more to read, but we are reading sequentially, so don't clear source yet
206
206
  return result
207
207
  } catch(error) {
208
- if (currentStructures?.restoreStructures)
208
+ if (currentStructures && currentStructures.restoreStructures)
209
209
  restoreStructures();
210
210
  clearSource();
211
211
  if (error instanceof RangeError || error.message.startsWith('Unexpected end of buffer') || position > srcEnd) {
@@ -1180,12 +1180,11 @@
1180
1180
  writeStruct(value);
1181
1181
  else
1182
1182
  pack(value);
1183
- if (bundledStrings$1) {
1184
- writeBundles(start, pack);
1185
- }
1186
- packr.offset = position$1; // update the offset so next serialization doesn't write over our buffer, but can continue writing to same buffer sequentially
1187
1183
  if (referenceMap && referenceMap.idsToInsert) {
1188
- position$1 += referenceMap.idsToInsert.length * 6;
1184
+ let incrementPosition = referenceMap.idsToInsert.length * 6;
1185
+ if (bundledStrings$1)
1186
+ writeBundles(start, pack, incrementPosition);
1187
+ position$1 += incrementPosition;
1189
1188
  if (position$1 > safeEnd)
1190
1189
  makeRoom(position$1);
1191
1190
  packr.offset = position$1;
@@ -1193,6 +1192,9 @@
1193
1192
  referenceMap = null;
1194
1193
  return serialized
1195
1194
  }
1195
+ if (bundledStrings$1)
1196
+ writeBundles(start, pack, 0);
1197
+ packr.offset = position$1; // update the offset so next serialization doesn't write over our buffer, but can continue writing to same buffer sequentially
1196
1198
  if (encodeOptions & REUSE_BUFFER_MODE) {
1197
1199
  target.start = start;
1198
1200
  target.end = position$1;
@@ -1971,9 +1973,9 @@
1971
1973
  return serialized
1972
1974
  }
1973
1975
 
1974
- function writeBundles(start, pack) {
1976
+ function writeBundles(start, pack, incrementPosition) {
1975
1977
  if (bundledStrings$1.length > 0) {
1976
- targetView.setUint32(bundledStrings$1.position + start, position$1 - bundledStrings$1.position - start);
1978
+ targetView.setUint32(bundledStrings$1.position + start, position$1 + incrementPosition - bundledStrings$1.position - start);
1977
1979
  let writeStrings = bundledStrings$1;
1978
1980
  bundledStrings$1 = null;
1979
1981
  pack(writeStrings[0]);
@@ -2450,7 +2452,7 @@
2450
2452
  case 27: recordId = src[position++] + (src[position++] << 8) + (src[position++] << 16) + (src[position++] << 24); break;
2451
2453
  }
2452
2454
  }
2453
- let structure = unpackr.typedStructs?.[recordId];
2455
+ let structure = unpackr.typedStructs && unpackr.typedStructs[recordId];
2454
2456
  if (!structure) {
2455
2457
  // copy src buffer because getStructures will override it
2456
2458
  src = Uint8Array.prototype.slice.call(src, position, srcEnd);
@@ -2742,7 +2744,7 @@
2742
2744
  packr._mergeStructures(existing);
2743
2745
  return compatible;
2744
2746
  };
2745
- packr.lastTypedStructuresLength = packr.typedStructs?.length;
2747
+ packr.lastTypedStructuresLength = packr.typedStructs && packr.typedStructs.length;
2746
2748
  return structures;
2747
2749
  }
2748
2750
 
@@ -3160,6 +3162,23 @@
3160
3162
  assert.equal(deserialized.uint16Array[1], 4);
3161
3163
  });
3162
3164
 
3165
+ test('structured clone with bundled strings', function() {
3166
+ const packer = new Packr$1({
3167
+ structuredClone: true, // both options must be enabled
3168
+ bundleStrings: true,
3169
+ });
3170
+
3171
+ const v = {};
3172
+
3173
+ const shared = {
3174
+ name1: v,
3175
+ name2: v, // one key has to be named `data`
3176
+ };
3177
+
3178
+ let deserialized = packer.unpack(packer.pack(shared));
3179
+ assert.equal(deserialized.name1, deserialized.name2);
3180
+ });
3181
+
3163
3182
  test('object without prototype', function(){
3164
3183
  var data = Object.create(null);
3165
3184
  data.test = 3;