msgpackr 1.11.13 → 1.11.14

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
@@ -1423,7 +1423,14 @@
1423
1423
  let newSharedData = prepareStructures$1(structures, packr);
1424
1424
  if (!encodingError) { // TODO: If there is an encoding error, should make the structures as uninitialized so they get rebuilt next time
1425
1425
  if (packr.saveStructures(newSharedData, newSharedData.isCompatible) === false) {
1426
- // get updated structures and try again if the update failed
1426
+ // The save was declined (a concurrent writer updated the shared structures,
1427
+ // or the store transaction did not durably commit). Our in-memory
1428
+ // structures + transition trie may now reference record ids that were
1429
+ // never persisted; re-packing as-is would re-emit the same record pointing
1430
+ // at an unpersisted structure (-> "Record id is not defined" on decode).
1431
+ // Mark structures uninitialized so the re-pack reloads durable structures
1432
+ // via getStructures, rebuilds the transition trie, and re-mints + re-saves.
1433
+ structures.uninitialized = true;
1427
1434
  return packr.pack(value, encodeOptions)
1428
1435
  }
1429
1436
  packr.lastNamedStructuresLength = sharedLength;
@@ -4219,6 +4226,31 @@
4219
4226
  assert.deepEqual(inputData, outputData);
4220
4227
  });
4221
4228
 
4229
+ test('declined structure save re-packs against durable structures (no dangling record)', function() {
4230
+ // Regression: when saveStructures declines a save (a concurrent writer updated the shared
4231
+ // structures, or the store txn did not durably commit), the in-memory structures/transition
4232
+ // trie reference a record id that was never persisted. The re-pack must reload the durable
4233
+ // structures and re-mint/re-save — otherwise it re-emits the same record pointing at an
4234
+ // unsaved structure, and reads throw "Record id is not defined".
4235
+ const meta = new Packr();
4236
+ let store = null;
4237
+ let declinedOnce = false;
4238
+ const packr = new Packr({
4239
+ useRecords: true,
4240
+ getStructures() { return store ? meta.unpack(store) : undefined },
4241
+ saveStructures(structures) {
4242
+ if (!declinedOnce) { declinedOnce = true; return false } // decline the first save
4243
+ store = meta.pack(structures); return true
4244
+ },
4245
+ });
4246
+ const a = packr.pack({ x: 9, y: 8 }); // first mint is declined -> must re-pack + re-save
4247
+ const b = packr.pack({ x: 7, y: 6 }); // same shape -> must still reference a saved structure
4248
+ // A fresh reader sees only the durably-saved structures (another thread / post-restart):
4249
+ const reader = new Packr({ getStructures() { return store ? meta.unpack(store) : undefined } });
4250
+ assert.deepEqual(reader.unpack(a), { x: 9, y: 8 });
4251
+ assert.deepEqual(reader.unpack(b), { x: 7, y: 6 });
4252
+ });
4253
+
4222
4254
  test('big buffer', function() {
4223
4255
  var size = 100000000;
4224
4256
  var data = new Uint8Array(size).fill(1);