@rotorsoft/act 2.1.3 → 2.1.5

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
@@ -1161,6 +1161,98 @@ var subscribe = (streams, correlated_at, correlator) => store().subscribe(stream
1161
1161
  // src/internal/event-sourcing.ts
1162
1162
  import { patch } from "@rotorsoft/act-patch";
1163
1163
  import { prettifyError, ZodError } from "zod";
1164
+
1165
+ // src/internal/date-reviver.ts
1166
+ import { z as z3 } from "zod";
1167
+ function variant_schema(schema, walk) {
1168
+ const shape = def_of(schema)?.shape;
1169
+ if (!shape) {
1170
+ const dates = walk(schema);
1171
+ return { schema: dates ?? schema, dated: !!dates };
1172
+ }
1173
+ const next = {};
1174
+ let dated = false;
1175
+ for (const [key, field] of Object.entries(shape)) {
1176
+ const dates = walk(field);
1177
+ if (dates) dated = true;
1178
+ next[key] = (dates ?? field).optional();
1179
+ }
1180
+ return { schema: z3.looseObject(next), dated };
1181
+ }
1182
+ function date_reviver_schema(schema) {
1183
+ let recursive = false;
1184
+ const pass = (tie) => {
1185
+ const done = /* @__PURE__ */ new Map();
1186
+ const active = /* @__PURE__ */ new Set();
1187
+ const walk = (node) => {
1188
+ if (done.has(node)) return done.get(node);
1189
+ if (active.has(node)) {
1190
+ recursive = true;
1191
+ return tie ? z3.lazy(() => done.get(node)) : void 0;
1192
+ }
1193
+ active.add(node);
1194
+ const result = build(node, walk);
1195
+ active.delete(node);
1196
+ done.set(node, result);
1197
+ return result;
1198
+ };
1199
+ return walk(schema);
1200
+ };
1201
+ const first = pass(false);
1202
+ return first && recursive ? pass(true) : first;
1203
+ }
1204
+ function build(schema, walk) {
1205
+ const def = def_of(schema);
1206
+ if (!def) return void 0;
1207
+ const inner = () => walk(def.innerType);
1208
+ switch (def.type) {
1209
+ case "date":
1210
+ return z3.coerce.date();
1211
+ case "object": {
1212
+ const shape = def.shape;
1213
+ if (!shape) return void 0;
1214
+ const dated = {};
1215
+ for (const [key, field] of Object.entries(shape)) {
1216
+ const dates = walk(field);
1217
+ if (dates) dated[key] = dates.optional();
1218
+ }
1219
+ return Object.keys(dated).length ? z3.looseObject(dated) : void 0;
1220
+ }
1221
+ case "array": {
1222
+ const element = walk(def.element);
1223
+ return element && z3.array(element);
1224
+ }
1225
+ case "tuple": {
1226
+ const items = def.items.map((i) => walk(i));
1227
+ return items.some(Boolean) ? z3.tuple(items.map((i) => i ?? z3.unknown())) : void 0;
1228
+ }
1229
+ case "record": {
1230
+ const value = walk(def.valueType);
1231
+ return value && z3.record(z3.string(), value);
1232
+ }
1233
+ case "union": {
1234
+ const variants = def.options.map(
1235
+ (o) => variant_schema(o, walk)
1236
+ );
1237
+ return variants.some((v) => v.dated) ? z3.union(variants.map((v) => v.schema)) : void 0;
1238
+ }
1239
+ case "nullable":
1240
+ return inner()?.nullable();
1241
+ case "pipe":
1242
+ return walk(def.out);
1243
+ case "lazy":
1244
+ return walk(def.getter());
1245
+ case "intersection": {
1246
+ const left = walk(def.left);
1247
+ const right = walk(def.right);
1248
+ return left && right ? left.pipe(right) : left ?? right;
1249
+ }
1250
+ default:
1251
+ return inner_of(def) ? inner() : void 0;
1252
+ }
1253
+ }
1254
+
1255
+ // src/internal/event-sourcing.ts
1164
1256
  var bare_patch = (_me, state2, partial) => patch(state2, partial);
1165
1257
  var value_at = (value, path) => path.reduce(
1166
1258
  (at, key) => at?.[key],
@@ -1264,6 +1356,12 @@ async function scan(source, opts = {}, callback) {
1264
1356
  let processed = 0;
1265
1357
  let at;
1266
1358
  const started = /* @__PURE__ */ new Set();
1359
+ const migration_dates = new Map(
1360
+ Object.entries(event_migrations ?? {}).map(([name, { from_schema }]) => [
1361
+ name,
1362
+ date_reviver_schema(from_schema)
1363
+ ])
1364
+ );
1267
1365
  const closed_streams = /* @__PURE__ */ new Set();
1268
1366
  if (drop_closed_streams) {
1269
1367
  await source.query(
@@ -1286,6 +1384,7 @@ async function scan(source, opts = {}, callback) {
1286
1384
  let id;
1287
1385
  await source.query(
1288
1386
  async (event) => {
1387
+ if (at !== void 0 && event.id <= at) return;
1289
1388
  got++;
1290
1389
  id = event.id;
1291
1390
  processed++;
@@ -1305,7 +1404,10 @@ async function scan(source, opts = {}, callback) {
1305
1404
  let migrated = event;
1306
1405
  const migration = event_migrations?.[event.name];
1307
1406
  if (migration) {
1308
- const old_data = migration.from_schema.parse(event.data);
1407
+ const revived = migration_dates.get(event.name)?.safeParse(event.data);
1408
+ const old_data = migration.from_schema.parse(
1409
+ revived?.success ? revived.data : event.data
1410
+ );
1309
1411
  const new_data = migration.migrate(old_data);
1310
1412
  migration.to_schema.parse(new_data);
1311
1413
  migrated = {
@@ -1342,6 +1444,7 @@ async function scan(source, opts = {}, callback) {
1342
1444
  // A cause missing from the source has no id in the new
1343
1445
  // log; its old id would name another event there.
1344
1446
  id: id_map.get(cause.id) ?? -1,
1447
+ name: event_migrations?.[cause.name]?.to ?? cause.name,
1345
1448
  stream: stream_rename ? stream_rename(cause.stream) : cause.stream
1346
1449
  }
1347
1450
  }
@@ -2341,10 +2444,12 @@ function synthesize_autoclose_reactions(registry, states, read_window) {
2341
2444
  throw new CloseSignal({
2342
2445
  stream: aggregate,
2343
2446
  archive: archiver ? () => archiver(aggregate, head) : void 0,
2344
- // Ack this reaction's own watermark to the live head so the
2345
- // close-cycle guard (which matches subscriptions by source =
2346
- // aggregate) sees it caught up instead of blocking its own close.
2347
- at: head.id
2447
+ // Hold the head and look again tomorrow: a close skipped for a
2448
+ // lagging reaction, or a failed archive, retries then, and a
2449
+ // closed stream has no head left to act on. The close guard
2450
+ // ignores this parked row, so it doesn't block its own close.
2451
+ at: head.id - 1,
2452
+ defer: days_after(/* @__PURE__ */ new Date(), 1)
2348
2453
  });
2349
2454
  if (keep_days !== void 0) {
2350
2455
  const cutoff = days_before_now(keep_days);
@@ -4576,98 +4681,6 @@ function reaction_on(event, events, builder) {
4576
4681
 
4577
4682
  // src/builders/event-builder.ts
4578
4683
  import { z as z4 } from "zod";
4579
-
4580
- // src/internal/date-reviver.ts
4581
- import { z as z3 } from "zod";
4582
- function variant_schema(schema, walk) {
4583
- const shape = def_of(schema)?.shape;
4584
- if (!shape) {
4585
- const dates = walk(schema);
4586
- return { schema: dates ?? schema, dated: !!dates };
4587
- }
4588
- const next = {};
4589
- let dated = false;
4590
- for (const [key, field] of Object.entries(shape)) {
4591
- const dates = walk(field);
4592
- if (dates) dated = true;
4593
- next[key] = (dates ?? field).optional();
4594
- }
4595
- return { schema: z3.looseObject(next), dated };
4596
- }
4597
- function date_reviver_schema(schema) {
4598
- let recursive = false;
4599
- const pass = (tie) => {
4600
- const done = /* @__PURE__ */ new Map();
4601
- const active = /* @__PURE__ */ new Set();
4602
- const walk = (node) => {
4603
- if (done.has(node)) return done.get(node);
4604
- if (active.has(node)) {
4605
- recursive = true;
4606
- return tie ? z3.lazy(() => done.get(node)) : void 0;
4607
- }
4608
- active.add(node);
4609
- const result = build(node, walk);
4610
- active.delete(node);
4611
- done.set(node, result);
4612
- return result;
4613
- };
4614
- return walk(schema);
4615
- };
4616
- const first = pass(false);
4617
- return first && recursive ? pass(true) : first;
4618
- }
4619
- function build(schema, walk) {
4620
- const def = def_of(schema);
4621
- if (!def) return void 0;
4622
- const inner = () => walk(def.innerType);
4623
- switch (def.type) {
4624
- case "date":
4625
- return z3.coerce.date();
4626
- case "object": {
4627
- const shape = def.shape;
4628
- if (!shape) return void 0;
4629
- const dated = {};
4630
- for (const [key, field] of Object.entries(shape)) {
4631
- const dates = walk(field);
4632
- if (dates) dated[key] = dates.optional();
4633
- }
4634
- return Object.keys(dated).length ? z3.looseObject(dated) : void 0;
4635
- }
4636
- case "array": {
4637
- const element = walk(def.element);
4638
- return element && z3.array(element);
4639
- }
4640
- case "tuple": {
4641
- const items = def.items.map((i) => walk(i));
4642
- return items.some(Boolean) ? z3.tuple(items.map((i) => i ?? z3.unknown())) : void 0;
4643
- }
4644
- case "record": {
4645
- const value = walk(def.valueType);
4646
- return value && z3.record(z3.string(), value);
4647
- }
4648
- case "union": {
4649
- const variants = def.options.map(
4650
- (o) => variant_schema(o, walk)
4651
- );
4652
- return variants.some((v) => v.dated) ? z3.union(variants.map((v) => v.schema)) : void 0;
4653
- }
4654
- case "nullable":
4655
- return inner()?.nullable();
4656
- case "pipe":
4657
- return walk(def.out);
4658
- case "lazy":
4659
- return walk(def.getter());
4660
- case "intersection": {
4661
- const left = walk(def.left);
4662
- const right = walk(def.right);
4663
- return left && right ? left.pipe(right) : left ?? right;
4664
- }
4665
- default:
4666
- return inner_of(def) ? inner() : void 0;
4667
- }
4668
- }
4669
-
4670
- // src/builders/event-builder.ts
4671
4684
  function event_tags(schema) {
4672
4685
  const pii = pii_schemas(schema);
4673
4686
  const pii_dates = {};