@crvouga/mockingbird-runtime 0.2.0 → 0.2.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.
@@ -2,7 +2,7 @@ import {
2
2
  normalizeIdBase,
3
3
  ownedTransport,
4
4
  signV4
5
- } from "./chunk-OISPHKE2.js";
5
+ } from "./chunk-4BRVNPWR.js";
6
6
  import "./chunk-2KDYLZXC.js";
7
7
  import {
8
8
  createClock,
@@ -3750,7 +3750,7 @@ async function preflightFleet(config, options) {
3750
3750
  break;
3751
3751
  }
3752
3752
  }
3753
- targets.set(name, { ...target, packageVersion: packageVersion ?? "0.2.0" });
3753
+ targets.set(name, { ...target, packageVersion: packageVersion ?? "0.2.1" });
3754
3754
  } catch (error) {
3755
3755
  throw new Error(`service ${name}: ${error instanceof Error ? error.message : String(error)}`, { cause: error });
3756
3756
  }
@@ -4268,6 +4268,59 @@ var awsXml = (value, listTag = "member") => {
4268
4268
  return Object.entries(value).filter(([, item]) => item !== void 0).map(([key, item]) => `<${key}>${awsXml(item, listTag)}</${key}>`).join("");
4269
4269
  };
4270
4270
 
4271
+ // ../service/core/dist/write-journal.js
4272
+ var WriteJournal = class {
4273
+ records = /* @__PURE__ */ new Map();
4274
+ sequences = /* @__PURE__ */ new Map();
4275
+ restore(sqlite, namespace) {
4276
+ sqlite.transaction(() => {
4277
+ const remove = sqlite.prepare("DELETE FROM mockingbird_records WHERE namespace = ? AND collection = ? AND id = ?");
4278
+ const put = sqlite.prepare(`INSERT INTO mockingbird_records (namespace, collection, id, seq, value) VALUES (?, ?, ?, ?, ?)
4279
+ ON CONFLICT(namespace, collection, id) DO UPDATE SET seq = excluded.seq, value = excluded.value`);
4280
+ for (const { collection, id, before } of this.records.values()) {
4281
+ if (before)
4282
+ put.run(namespace, collection, id, before.seq, before.value);
4283
+ else
4284
+ remove.run(namespace, collection, id);
4285
+ }
4286
+ const removeSequence = sqlite.prepare("DELETE FROM mockingbird_sequences WHERE namespace = ? AND name = ? AND kind = 'collection'");
4287
+ const putSequence = sqlite.prepare(`INSERT INTO mockingbird_sequences (namespace, name, kind, value) VALUES (?, ?, 'collection', ?)
4288
+ ON CONFLICT(namespace, name, kind) DO UPDATE SET value = excluded.value`);
4289
+ for (const [name, before] of this.sequences) {
4290
+ if (before === null)
4291
+ removeSequence.run(namespace, name);
4292
+ else
4293
+ putSequence.run(namespace, name, before);
4294
+ }
4295
+ });
4296
+ }
4297
+ };
4298
+ var open = /* @__PURE__ */ new WeakMap();
4299
+ var openJournals = (sqlite, namespace) => open.get(sqlite)?.get(namespace);
4300
+ var openJournal = (sqlite, namespace) => {
4301
+ let namespaces = open.get(sqlite);
4302
+ if (!namespaces) {
4303
+ namespaces = /* @__PURE__ */ new Map();
4304
+ open.set(sqlite, namespaces);
4305
+ }
4306
+ let journals = namespaces.get(namespace);
4307
+ if (!journals) {
4308
+ journals = /* @__PURE__ */ new Set();
4309
+ namespaces.set(namespace, journals);
4310
+ }
4311
+ const journal = new WriteJournal();
4312
+ journals.add(journal);
4313
+ return journal;
4314
+ };
4315
+ var closeJournal = (sqlite, namespace, journal) => {
4316
+ const journals = open.get(sqlite)?.get(namespace);
4317
+ if (!journals)
4318
+ return;
4319
+ journals.delete(journal);
4320
+ if (journals.size === 0)
4321
+ open.get(sqlite)?.delete(namespace);
4322
+ };
4323
+
4271
4324
  // ../service/core/dist/collection.js
4272
4325
  var Collection = class {
4273
4326
  sqlite;
@@ -4287,8 +4340,32 @@ var Collection = class {
4287
4340
  this.namespace = namespace;
4288
4341
  this.collectionName = collectionName;
4289
4342
  }
4343
+ /** Note the record's current row in every open rollback journal that has not seen it yet. */
4344
+ journalRecord(id) {
4345
+ const journals = openJournals(this.sqlite, this.namespace);
4346
+ if (!journals)
4347
+ return;
4348
+ const key = `${this.collectionName}\0${id}`;
4349
+ let before;
4350
+ for (const journal of journals) {
4351
+ if (journal.records.has(key))
4352
+ continue;
4353
+ if (before === void 0) {
4354
+ before = this.prepare("SELECT seq, value FROM mockingbird_records WHERE namespace = ? AND collection = ? AND id = ?").get(this.namespace, this.collectionName, id) ?? null;
4355
+ }
4356
+ journal.records.set(key, { collection: this.collectionName, id, before });
4357
+ }
4358
+ }
4290
4359
  bumpCollectionSeq() {
4291
4360
  const row = this.prepare("SELECT value FROM mockingbird_sequences WHERE namespace = ? AND name = ? AND kind = 'collection'").get(this.namespace, this.collectionName);
4361
+ const journals = openJournals(this.sqlite, this.namespace);
4362
+ if (journals) {
4363
+ for (const journal of journals) {
4364
+ if (!journal.sequences.has(this.collectionName)) {
4365
+ journal.sequences.set(this.collectionName, row?.value ?? null);
4366
+ }
4367
+ }
4368
+ }
4292
4369
  const next = (row?.value ?? 0) + 1;
4293
4370
  this.prepare(`INSERT INTO mockingbird_sequences (namespace, name, kind, value) VALUES (?, ?, 'collection', ?)
4294
4371
  ON CONFLICT(namespace, name, kind) DO UPDATE SET value = excluded.value`).run(this.namespace, this.collectionName, next);
@@ -4310,6 +4387,7 @@ var Collection = class {
4310
4387
  /** Insert a new record, assigning it the next sequence number. */
4311
4388
  insert(id, value) {
4312
4389
  return this.sqlite.transaction(() => {
4390
+ this.journalRecord(id);
4313
4391
  const seq = this.bumpCollectionSeq();
4314
4392
  const stored = { seq, value };
4315
4393
  this.prepare(`INSERT INTO mockingbird_records (namespace, collection, id, seq, value)
@@ -4324,12 +4402,14 @@ var Collection = class {
4324
4402
  const row = this.prepare("SELECT seq, value FROM mockingbird_records WHERE namespace = ? AND collection = ? AND id = ?").get(this.namespace, this.collectionName, id);
4325
4403
  if (!row)
4326
4404
  return void 0;
4405
+ this.journalRecord(id);
4327
4406
  const stored = { seq: row.seq, value };
4328
4407
  this.prepare("UPDATE mockingbird_records SET value = ? WHERE namespace = ? AND collection = ? AND id = ?").run(JSON.stringify(stored), this.namespace, this.collectionName, id);
4329
4408
  return stored;
4330
4409
  });
4331
4410
  }
4332
4411
  delete(id) {
4412
+ this.journalRecord(id);
4333
4413
  const result = this.prepare("DELETE FROM mockingbird_records WHERE namespace = ? AND collection = ? AND id = ?").run(this.namespace, this.collectionName, id);
4334
4414
  return result.changes > 0;
4335
4415
  }
@@ -5314,13 +5394,14 @@ var restoreNamespace = (sqlite, namespace, snapshot) => {
5314
5394
  });
5315
5395
  };
5316
5396
  var withNamespaceRollback = async (sqlite, namespace, run) => {
5317
- const rollback = new Timeline({ maxCheckpoints: 1 });
5318
- const before = rollback.commit(snapshotNamespace(sqlite, namespace));
5397
+ const journal = openJournal(sqlite, namespace);
5319
5398
  try {
5320
5399
  return await run();
5321
5400
  } catch (error) {
5322
- restoreNamespace(sqlite, namespace, before.value);
5401
+ journal.restore(sqlite, namespace);
5323
5402
  throw error;
5403
+ } finally {
5404
+ closeJournal(sqlite, namespace, journal);
5324
5405
  }
5325
5406
  };
5326
5407
 
@@ -5677,7 +5758,7 @@ var integerParam = (value) => {
5677
5758
  return Number(value);
5678
5759
  };
5679
5760
  var stateAdminRoutes = (routes) => {
5680
- const open = (namespace) => {
5761
+ const open2 = (namespace) => {
5681
5762
  try {
5682
5763
  return { ok: true, value: routes.open(namespace) };
5683
5764
  } catch (error) {
@@ -5689,11 +5770,11 @@ var stateAdminRoutes = (routes) => {
5689
5770
  };
5690
5771
  return {
5691
5772
  "GET /state": ({ namespace }) => {
5692
- const scope = open(namespace);
5773
+ const scope = open2(namespace);
5693
5774
  return fromResult(scope.ok ? { ok: true, value: inspectState(scope.value) } : scope, 200);
5694
5775
  },
5695
5776
  "GET /state/:collection": ({ namespace, params, url }) => {
5696
- const scope = open(namespace);
5777
+ const scope = open2(namespace);
5697
5778
  if (!scope.ok)
5698
5779
  return fromResult(scope, 200);
5699
5780
  const limit = integerParam(url.searchParams.get("limit"));
@@ -5710,13 +5791,13 @@ var stateAdminRoutes = (routes) => {
5710
5791
  }), 200);
5711
5792
  },
5712
5793
  "GET /state/:collection/:id": ({ namespace, params }) => {
5713
- const scope = open(namespace);
5794
+ const scope = open2(namespace);
5714
5795
  if (!scope.ok)
5715
5796
  return fromResult(scope, 200);
5716
5797
  return fromResult(readStateRecord(scope.value, params.collection ?? "", params.id ?? ""), 200);
5717
5798
  },
5718
5799
  "POST /state/:collection": ({ namespace, params, body }) => {
5719
- const scope = open(namespace);
5800
+ const scope = open2(namespace);
5720
5801
  if (!scope.ok)
5721
5802
  return fromResult(scope, 201);
5722
5803
  if (!isRecord2(body) || !("value" in body)) {
@@ -5734,7 +5815,7 @@ var stateAdminRoutes = (routes) => {
5734
5815
  return fromResult(created, 201);
5735
5816
  },
5736
5817
  "PUT /state/:collection/:id": ({ namespace, params, body }) => {
5737
- const scope = open(namespace);
5818
+ const scope = open2(namespace);
5738
5819
  if (!scope.ok)
5739
5820
  return fromResult(scope, 200);
5740
5821
  if (!isRecord2(body) || !("value" in body))
@@ -5745,7 +5826,7 @@ var stateAdminRoutes = (routes) => {
5745
5826
  return fromResult(replaced, 200);
5746
5827
  },
5747
5828
  "PATCH /state/:collection/:id": ({ namespace, params, body }) => {
5748
- const scope = open(namespace);
5829
+ const scope = open2(namespace);
5749
5830
  if (!scope.ok)
5750
5831
  return fromResult(scope, 200);
5751
5832
  if (!isRecord2(body) || !("value" in body))
@@ -5756,7 +5837,7 @@ var stateAdminRoutes = (routes) => {
5756
5837
  return fromResult(patched, 200);
5757
5838
  },
5758
5839
  "DELETE /state/:collection/:id": ({ namespace, params }) => {
5759
- const scope = open(namespace);
5840
+ const scope = open2(namespace);
5760
5841
  if (!scope.ok)
5761
5842
  return fromResult(scope, 200);
5762
5843
  const removed = deleteStateRecord(scope.value, params.collection ?? "", params.id ?? "");
@@ -5768,8 +5849,8 @@ var stateAdminRoutes = (routes) => {
5768
5849
  };
5769
5850
 
5770
5851
  // ../service/core/dist/version.js
5771
- var UNRELEASED_VERSION = "0.2.0";
5772
- var PACKAGE_VERSION = true ? "0.2.0" : UNRELEASED_VERSION;
5852
+ var UNRELEASED_VERSION = "0.2.1";
5853
+ var PACKAGE_VERSION = true ? "0.2.1" : UNRELEASED_VERSION;
5773
5854
 
5774
5855
  // ../service/core/dist/webhooks.js
5775
5856
  var signers = {
@@ -7662,4 +7743,4 @@ export {
7662
7743
  signV4,
7663
7744
  putObject
7664
7745
  };
7665
- //# sourceMappingURL=chunk-OISPHKE2.js.map
7746
+ //# sourceMappingURL=chunk-4BRVNPWR.js.map