@rotorsoft/act 2.1.4 → 2.1.6

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(
@@ -1306,7 +1404,10 @@ async function scan(source, opts = {}, callback) {
1306
1404
  let migrated = event;
1307
1405
  const migration = event_migrations?.[event.name];
1308
1406
  if (migration) {
1309
- 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
+ );
1310
1411
  const new_data = migration.migrate(old_data);
1311
1412
  migration.to_schema.parse(new_data);
1312
1413
  migrated = {
@@ -1343,6 +1444,7 @@ async function scan(source, opts = {}, callback) {
1343
1444
  // A cause missing from the source has no id in the new
1344
1445
  // log; its old id would name another event there.
1345
1446
  id: id_map.get(cause.id) ?? -1,
1447
+ name: event_migrations?.[cause.name]?.to ?? cause.name,
1346
1448
  stream: stream_rename ? stream_rename(cause.stream) : cause.stream
1347
1449
  }
1348
1450
  }
@@ -3505,7 +3607,7 @@ var Act = class {
3505
3607
  if (error instanceof StoreError) throw error;
3506
3608
  this._logger.error(
3507
3609
  error,
3508
- `Reaction-requested close of ${targets.map((t) => `"${t.stream}"`).join(", ")} failed and was not retried; close it with app.close(...) once the cause is fixed.`
3610
+ `Reaction-requested close of ${targets.map((t) => `"${t.stream}"`).join(", ")} failed; the policy retries it (a full close the next day, a prune on the stream's next commit), or close it now with app.close(...) once the cause is fixed.`
3509
3611
  );
3510
3612
  }
3511
3613
  },
@@ -4579,98 +4681,6 @@ function reaction_on(event, events, builder) {
4579
4681
 
4580
4682
  // src/builders/event-builder.ts
4581
4683
  import { z as z4 } from "zod";
4582
-
4583
- // src/internal/date-reviver.ts
4584
- import { z as z3 } from "zod";
4585
- function variant_schema(schema, walk) {
4586
- const shape = def_of(schema)?.shape;
4587
- if (!shape) {
4588
- const dates = walk(schema);
4589
- return { schema: dates ?? schema, dated: !!dates };
4590
- }
4591
- const next = {};
4592
- let dated = false;
4593
- for (const [key, field] of Object.entries(shape)) {
4594
- const dates = walk(field);
4595
- if (dates) dated = true;
4596
- next[key] = (dates ?? field).optional();
4597
- }
4598
- return { schema: z3.looseObject(next), dated };
4599
- }
4600
- function date_reviver_schema(schema) {
4601
- let recursive = false;
4602
- const pass = (tie) => {
4603
- const done = /* @__PURE__ */ new Map();
4604
- const active = /* @__PURE__ */ new Set();
4605
- const walk = (node) => {
4606
- if (done.has(node)) return done.get(node);
4607
- if (active.has(node)) {
4608
- recursive = true;
4609
- return tie ? z3.lazy(() => done.get(node)) : void 0;
4610
- }
4611
- active.add(node);
4612
- const result = build(node, walk);
4613
- active.delete(node);
4614
- done.set(node, result);
4615
- return result;
4616
- };
4617
- return walk(schema);
4618
- };
4619
- const first = pass(false);
4620
- return first && recursive ? pass(true) : first;
4621
- }
4622
- function build(schema, walk) {
4623
- const def = def_of(schema);
4624
- if (!def) return void 0;
4625
- const inner = () => walk(def.innerType);
4626
- switch (def.type) {
4627
- case "date":
4628
- return z3.coerce.date();
4629
- case "object": {
4630
- const shape = def.shape;
4631
- if (!shape) return void 0;
4632
- const dated = {};
4633
- for (const [key, field] of Object.entries(shape)) {
4634
- const dates = walk(field);
4635
- if (dates) dated[key] = dates.optional();
4636
- }
4637
- return Object.keys(dated).length ? z3.looseObject(dated) : void 0;
4638
- }
4639
- case "array": {
4640
- const element = walk(def.element);
4641
- return element && z3.array(element);
4642
- }
4643
- case "tuple": {
4644
- const items = def.items.map((i) => walk(i));
4645
- return items.some(Boolean) ? z3.tuple(items.map((i) => i ?? z3.unknown())) : void 0;
4646
- }
4647
- case "record": {
4648
- const value = walk(def.valueType);
4649
- return value && z3.record(z3.string(), value);
4650
- }
4651
- case "union": {
4652
- const variants = def.options.map(
4653
- (o) => variant_schema(o, walk)
4654
- );
4655
- return variants.some((v) => v.dated) ? z3.union(variants.map((v) => v.schema)) : void 0;
4656
- }
4657
- case "nullable":
4658
- return inner()?.nullable();
4659
- case "pipe":
4660
- return walk(def.out);
4661
- case "lazy":
4662
- return walk(def.getter());
4663
- case "intersection": {
4664
- const left = walk(def.left);
4665
- const right = walk(def.right);
4666
- return left && right ? left.pipe(right) : left ?? right;
4667
- }
4668
- default:
4669
- return inner_of(def) ? inner() : void 0;
4670
- }
4671
- }
4672
-
4673
- // src/builders/event-builder.ts
4674
4684
  function event_tags(schema) {
4675
4685
  const pii = pii_schemas(schema);
4676
4686
  const pii_dates = {};