@rotorsoft/act 1.31.13 → 1.31.15

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.
Files changed (33) hide show
  1. package/dist/.tsbuildinfo +1 -1
  2. package/dist/@types/act.d.ts +2 -0
  3. package/dist/@types/act.d.ts.map +1 -1
  4. package/dist/@types/builders/event-builder.d.ts +11 -4
  5. package/dist/@types/builders/event-builder.d.ts.map +1 -1
  6. package/dist/@types/internal/circuit-breaker.d.ts +7 -0
  7. package/dist/@types/internal/circuit-breaker.d.ts.map +1 -1
  8. package/dist/@types/internal/date-reviver.d.ts +50 -0
  9. package/dist/@types/internal/date-reviver.d.ts.map +1 -0
  10. package/dist/@types/internal/drain-cycle.d.ts +3 -1
  11. package/dist/@types/internal/drain-cycle.d.ts.map +1 -1
  12. package/dist/@types/internal/index.d.ts +1 -0
  13. package/dist/@types/internal/index.d.ts.map +1 -1
  14. package/dist/@types/internal/sensitive.d.ts +22 -4
  15. package/dist/@types/internal/sensitive.d.ts.map +1 -1
  16. package/dist/@types/ports.d.ts +19 -0
  17. package/dist/@types/ports.d.ts.map +1 -1
  18. package/dist/@types/scoped.d.ts +17 -5
  19. package/dist/@types/scoped.d.ts.map +1 -1
  20. package/dist/{chunk-D5MBFIKL.js → chunk-7IBF4FTQ.js} +16 -11
  21. package/dist/{chunk-D5MBFIKL.js.map → chunk-7IBF4FTQ.js.map} +1 -1
  22. package/dist/{chunk-IO2OOCOR.js → chunk-T2AT4N6B.js} +13 -3
  23. package/dist/{chunk-IO2OOCOR.js.map → chunk-T2AT4N6B.js.map} +1 -1
  24. package/dist/index.cjs +156 -85
  25. package/dist/index.cjs.map +1 -1
  26. package/dist/index.js +133 -76
  27. package/dist/index.js.map +1 -1
  28. package/dist/test/index.cjs.map +1 -1
  29. package/dist/test/index.js +2 -2
  30. package/dist/types/index.cjs +14 -9
  31. package/dist/types/index.cjs.map +1 -1
  32. package/dist/types/index.js +1 -1
  33. package/package.json +3 -3
package/dist/index.cjs CHANGED
@@ -68,6 +68,7 @@ __export(index_exports, {
68
68
  cache: () => cache2,
69
69
  config: () => config,
70
70
  dateReviver: () => dateReviver,
71
+ default_scope: () => default_scope,
71
72
  dispose: () => dispose,
72
73
  disposeAndExit: () => disposeAndExit,
73
74
  extend: () => extend,
@@ -407,23 +408,28 @@ function is_pii(schema) {
407
408
  cur = inner;
408
409
  }
409
410
  }
410
- function pii_fields(schema) {
411
+ function pii_schemas(schema) {
411
412
  const shape = schema.shape;
412
413
  if (shape && typeof shape === "object") {
413
- const fields = [];
414
- for (const key of Object.keys(shape)) {
415
- if (is_pii(shape[key])) fields.push(key);
416
- }
414
+ const fields = {};
415
+ for (const key of Object.keys(shape))
416
+ if (is_pii(shape[key])) fields[key] = shape[key];
417
417
  return fields;
418
418
  }
419
419
  const options = schema.options;
420
420
  if (Array.isArray(options)) {
421
- const fields = /* @__PURE__ */ new Set();
421
+ const fields = {};
422
422
  for (const option of options)
423
- for (const key of pii_fields(option)) fields.add(key);
424
- return [...fields];
423
+ for (const [key, field] of Object.entries(
424
+ pii_schemas(option)
425
+ ))
426
+ fields[key] ??= field;
427
+ return fields;
425
428
  }
426
- return [];
429
+ return {};
430
+ }
431
+ function pii_fields(schema) {
432
+ return Object.keys(pii_schemas(schema));
427
433
  }
428
434
  function pii_split(emitted, fields) {
429
435
  const data = { ...emitted.data };
@@ -1607,7 +1613,7 @@ var import_node_async_hooks = require("async_hooks");
1607
1613
  var scoped = new import_node_async_hooks.AsyncLocalStorage();
1608
1614
  var reacting = new import_node_async_hooks.AsyncLocalStorage();
1609
1615
  function make_run_scoped(bag) {
1610
- return bag ? (fn) => scoped.run(bag, fn) : (fn) => fn();
1616
+ return (fn) => scoped.run(bag, fn);
1611
1617
  }
1612
1618
  function current_ports() {
1613
1619
  return scoped.getStore();
@@ -1661,6 +1667,15 @@ var _cache = port(function cache(adapter) {
1661
1667
  var cache2 = ((adapter) => {
1662
1668
  return current_ports()?.cache ?? _cache(adapter);
1663
1669
  });
1670
+ var default_scope = () => DEFAULT_SCOPE;
1671
+ var DEFAULT_SCOPE = {
1672
+ get store() {
1673
+ return _store();
1674
+ },
1675
+ get cache() {
1676
+ return _cache();
1677
+ }
1678
+ };
1664
1679
  async function disposeAndExit(code = "EXIT") {
1665
1680
  if (code === "ERROR" && config().env === "production") {
1666
1681
  log().warn(
@@ -2547,6 +2562,15 @@ var CircuitBreaker = class {
2547
2562
  this._cooldown_ms = config2.cooldownMs;
2548
2563
  this._hooks = hooks;
2549
2564
  }
2565
+ /**
2566
+ * True when the store has failed at least once since the last pass that
2567
+ * went all the way through. The breaker can still read "closed" here: it
2568
+ * only opens after several failures in a row, so this catches the first
2569
+ * one, which `state()` cannot.
2570
+ */
2571
+ get failing() {
2572
+ return this._failures > 0;
2573
+ }
2550
2574
  /** Current state given the wall-clock `now`. */
2551
2575
  state(now) {
2552
2576
  if (this._opened_at === void 0) return "closed";
@@ -3500,18 +3524,83 @@ function close_correlation(correlator, actor) {
3500
3524
  });
3501
3525
  }
3502
3526
 
3503
- // src/internal/defer-config.ts
3527
+ // src/internal/date-reviver.ts
3504
3528
  var import_zod7 = require("zod");
3505
- var DeferDurationSchema = import_zod7.z.object({
3506
- days: import_zod7.z.number().positive().optional(),
3507
- hours: import_zod7.z.number().positive().optional(),
3508
- minutes: import_zod7.z.number().positive().optional()
3529
+ var def_of = (schema) => schema._zod?.def ?? schema.def;
3530
+ function variant_schema(schema) {
3531
+ const shape = def_of(schema)?.shape;
3532
+ if (!shape) {
3533
+ const dates = date_reviver_schema(schema);
3534
+ return { schema: dates ?? schema, dated: !!dates };
3535
+ }
3536
+ const next = {};
3537
+ let dated = false;
3538
+ for (const [key, field] of Object.entries(shape)) {
3539
+ const dates = date_reviver_schema(field);
3540
+ if (dates) dated = true;
3541
+ next[key] = (dates ?? field).optional();
3542
+ }
3543
+ return { schema: import_zod7.z.looseObject(next), dated };
3544
+ }
3545
+ function date_reviver_schema(schema) {
3546
+ const def = def_of(schema);
3547
+ if (!def) return void 0;
3548
+ const inner = () => date_reviver_schema(def.innerType);
3549
+ switch (def.type) {
3550
+ case "date":
3551
+ return import_zod7.z.coerce.date();
3552
+ case "object": {
3553
+ const shape = def.shape;
3554
+ if (!shape) return void 0;
3555
+ const dated = {};
3556
+ for (const [key, field] of Object.entries(shape)) {
3557
+ const dates = date_reviver_schema(field);
3558
+ if (dates) dated[key] = dates.optional();
3559
+ }
3560
+ return Object.keys(dated).length ? import_zod7.z.looseObject(dated) : void 0;
3561
+ }
3562
+ case "array": {
3563
+ const element = date_reviver_schema(def.element);
3564
+ return element && import_zod7.z.array(element);
3565
+ }
3566
+ case "tuple": {
3567
+ const items = def.items.map((i) => date_reviver_schema(i));
3568
+ return items.some(Boolean) ? import_zod7.z.tuple(items.map((i) => i ?? import_zod7.z.unknown())) : void 0;
3569
+ }
3570
+ case "record": {
3571
+ const value = date_reviver_schema(def.valueType);
3572
+ return value && import_zod7.z.record(import_zod7.z.string(), value);
3573
+ }
3574
+ case "union": {
3575
+ const variants = def.options.map(variant_schema);
3576
+ return variants.some((v) => v.dated) ? import_zod7.z.union(variants.map((v) => v.schema)) : void 0;
3577
+ }
3578
+ case "nullable":
3579
+ return inner()?.nullable();
3580
+ case "optional":
3581
+ case "nonoptional":
3582
+ case "readonly":
3583
+ case "default":
3584
+ case "prefault":
3585
+ case "catch":
3586
+ return inner();
3587
+ default:
3588
+ return void 0;
3589
+ }
3590
+ }
3591
+
3592
+ // src/internal/defer-config.ts
3593
+ var import_zod8 = require("zod");
3594
+ var DeferDurationSchema = import_zod8.z.object({
3595
+ days: import_zod8.z.number().positive().optional(),
3596
+ hours: import_zod8.z.number().positive().optional(),
3597
+ minutes: import_zod8.z.number().positive().optional()
3509
3598
  }).strict().refine((d) => d.days != null || d.hours != null || d.minutes != null, {
3510
3599
  message: "defer: a duration needs at least one of days, hours, or minutes"
3511
3600
  });
3512
- var DeferWhenSchema = import_zod7.z.object({
3601
+ var DeferWhenSchema = import_zod8.z.object({
3513
3602
  after: DeferDurationSchema.optional(),
3514
- at: import_zod7.z.date().optional()
3603
+ at: import_zod8.z.date().optional()
3515
3604
  }).strict().refine((w) => w.after === void 0 !== (w.at === void 0), {
3516
3605
  message: "defer: specify exactly one of `after` or `at`"
3517
3606
  });
@@ -4254,10 +4343,10 @@ function trace_cycle(logger, leased, fetched, handled, acked, blocked) {
4254
4343
  }
4255
4344
 
4256
4345
  // src/internal/drain-cycle.ts
4257
- function budget_exhausted(lease, options) {
4258
- if (!options?.blockOnError || lease.retry <= options.maxRetries)
4346
+ function budget_exhausted(lease, options, store_failing) {
4347
+ if (store_failing || !options?.blockOnError || lease.retry <= options.maxRetries)
4259
4348
  return void 0;
4260
- const error = `Blocking ${lease.stream} after ${lease.retry} claims with no acknowledged progress \u2014 the lease was lost on every attempt, so the retry budget (${options.maxRetries}) was spent without the handler ever reporting an error. Raise leaseMillis for this handler, then unblock the stream.`;
4349
+ const error = `Blocking ${lease.stream} after ${lease.retry} claims with no acknowledged progress \u2014 the retry budget (${options.maxRetries}) was spent without the handler ever reporting an error. That means every attempt lost its lease before it could ack: raise leaseMillis for this handler, then unblock the stream.`;
4261
4350
  log().error(error);
4262
4351
  return { lease, handled: 0, acked_at: lease.at, error, block: true };
4263
4352
  }
@@ -4268,7 +4357,7 @@ function warn_misrouted(seen, stream, handler, event) {
4268
4357
  `Reaction "${handler}" on "${event}" resolved to target "${stream}", which a projection already serves. A target is served by one batch handler or one state projection, so this reaction can never run \u2014 and delivering to it would hand the projection an aggregate from another stream. Skipping it. The equivalent static \`.to({ target })\` is rejected at build; a dynamic resolver's target is only knowable here.`
4269
4358
  );
4270
4359
  }
4271
- async function run_drain_cycle(ops, registry, batch_handlers, misrouted, handle, handle_batch, lagging, leading, eventLimit, leaseMillis, on_blocked, lane) {
4360
+ async function run_drain_cycle(ops, registry, batch_handlers, misrouted, store_failing, handle, handle_batch, lagging, leading, eventLimit, leaseMillis, on_blocked, lane) {
4272
4361
  const leased = await ops.claim(
4273
4362
  lagging,
4274
4363
  leading,
@@ -4310,7 +4399,11 @@ async function run_drain_cycle(ops, registry, batch_handlers, misrouted, handle,
4310
4399
  const entry = fetch_map.get(lease.stream);
4311
4400
  const at = entry.fetch.events.at(-1)?.id || fetch_window_at;
4312
4401
  const { payloads } = entry;
4313
- const exhausted = budget_exhausted(lease, payloads[0]?.options);
4402
+ const exhausted = budget_exhausted(
4403
+ lease,
4404
+ payloads[0]?.options,
4405
+ store_failing
4406
+ );
4314
4407
  if (exhausted) return Promise.resolve(exhausted);
4315
4408
  const batchHandler = batch_handlers.get(lease.stream);
4316
4409
  if (batchHandler && payloads.length > 0) {
@@ -4482,6 +4575,7 @@ var DrainController = class {
4482
4575
  this._deps.registry,
4483
4576
  this._deps.batch_handlers,
4484
4577
  this._misrouted,
4578
+ this._deps.breaker.failing,
4485
4579
  this._deps.handle,
4486
4580
  this._deps.handle_batch,
4487
4581
  lagging,
@@ -4988,6 +5082,8 @@ var Act = class {
4988
5082
  * per-Act ports (ACT-501). No-op when the Act is unscoped — so the singleton
4989
5083
  * path keeps reading fresh `store()`/`cache()` per call, which matters for
4990
5084
  * tests that dispose and re-seed mid-suite. */
5085
+ /** This Act's ports: its own bag, or the singleton adapters. */
5086
+ _ports;
4991
5087
  _scoped;
4992
5088
  /**
4993
5089
  * Correlation-id generator for originating actions. Bound at
@@ -5066,7 +5162,8 @@ var Act = class {
5066
5162
  this._batch_handlers = batch_handlers;
5067
5163
  this._lanes = lanes;
5068
5164
  validate_only_lanes(options, lanes);
5069
- this._scoped = make_run_scoped(options.scoped);
5165
+ this._ports = options.scoped ?? default_scope();
5166
+ this._scoped = make_run_scoped(this._ports);
5070
5167
  this._correlator = options.correlator ?? default_correlator;
5071
5168
  this._es = build_es(this._logger, this._correlator, patch_fn);
5072
5169
  this._cd = build_drain(this._logger);
@@ -5095,7 +5192,7 @@ var Act = class {
5095
5192
  this._audit_deps = this._build_audit_deps();
5096
5193
  this._correlate = this._build_correlate(options, classification);
5097
5194
  this._settle = this._build_settle(options);
5098
- this._notify_disposer = this._wire_notify(options.scoped?.store ?? store2());
5195
+ this._notify_disposer = this._wire_notify(this._ports.store);
5099
5196
  register_weak_disposer(new WeakRef(this), (self) => self.shutdown());
5100
5197
  }
5101
5198
  /**
@@ -5344,13 +5441,13 @@ var Act = class {
5344
5441
  this._shutdown_promise = (async () => {
5345
5442
  this.stop_correlations();
5346
5443
  await this._scoped(() => this._correlate.release_correlation());
5444
+ const disposer = await this._notify_disposer;
5445
+ if (disposer) await disposer();
5347
5446
  this.stop_settling();
5348
5447
  this._breaker.stop();
5349
5448
  for (const c of this._drain_controllers.values()) c.stop();
5350
5449
  await this._await_inflight(options?.graceMs);
5351
5450
  this._emitter.removeAllListeners();
5352
- const disposer = await this._notify_disposer;
5353
- if (disposer) await disposer();
5354
5451
  })();
5355
5452
  }
5356
5453
  return this._shutdown_promise;
@@ -6348,7 +6445,7 @@ var Act = class {
6348
6445
  };
6349
6446
 
6350
6447
  // src/builders/merge.ts
6351
- var import_zod8 = require("zod");
6448
+ var import_zod9 = require("zod");
6352
6449
  function base_type_name(zodType) {
6353
6450
  let t = zodType;
6354
6451
  while (typeof t.unwrap === "function") {
@@ -6357,7 +6454,7 @@ function base_type_name(zodType) {
6357
6454
  return t.constructor.name;
6358
6455
  }
6359
6456
  function merge_schemas(existing, incoming, state_name) {
6360
- if (existing instanceof import_zod8.ZodObject && incoming instanceof import_zod8.ZodObject) {
6457
+ if (existing instanceof import_zod9.ZodObject && incoming instanceof import_zod9.ZodObject) {
6361
6458
  const existing_shape = existing.shape;
6362
6459
  const incoming_shape = incoming.shape;
6363
6460
  for (const key of Object.keys(incoming_shape)) {
@@ -6544,70 +6641,43 @@ function reaction_on(event, events, builder) {
6544
6641
  }
6545
6642
 
6546
6643
  // src/builders/event-builder.ts
6547
- var import_zod9 = require("zod");
6548
- var def_of = (schema) => schema._zod?.def ?? schema.def;
6549
- function to_read_schema(schema, found) {
6550
- const def = def_of(schema);
6551
- if (!def) return schema;
6552
- switch (def.type) {
6553
- case "date":
6554
- found.date = true;
6555
- return import_zod9.z.coerce.date();
6556
- case "object": {
6557
- const shape = def.shape;
6558
- if (!shape) return schema;
6559
- const next = {};
6560
- for (const [key, inner] of Object.entries(shape))
6561
- next[key] = to_read_schema(inner, found);
6562
- return import_zod9.z.looseObject(next);
6563
- }
6564
- case "array":
6565
- return import_zod9.z.array(to_read_schema(def.element, found));
6566
- case "record":
6567
- return import_zod9.z.record(
6568
- import_zod9.z.string(),
6569
- to_read_schema(def.valueType, found)
6570
- );
6571
- case "union":
6572
- return import_zod9.z.union(
6573
- def.options.map(
6574
- (o) => to_read_schema(o, found)
6575
- )
6576
- );
6577
- case "optional":
6578
- return to_read_schema(def.innerType, found).optional();
6579
- case "nullable":
6580
- return to_read_schema(def.innerType, found).nullable();
6581
- default:
6582
- return schema;
6583
- }
6584
- }
6644
+ var import_zod10 = require("zod");
6585
6645
  function event_tags(schema) {
6586
- const sensitive2 = [];
6587
- const collect = (node) => {
6588
- const shape = def_of(node)?.shape;
6589
- if (shape) {
6590
- for (const key of Object.keys(shape))
6591
- if (is_pii(shape[key])) sensitive2.push(key);
6592
- return;
6593
- }
6594
- const options = node.options;
6595
- if (Array.isArray(options)) for (const option of options) collect(option);
6646
+ const pii = pii_schemas(schema);
6647
+ const pii_dates = {};
6648
+ for (const [key, field] of Object.entries(pii)) {
6649
+ const dates = date_reviver_schema(field);
6650
+ if (dates) pii_dates[key] = dates.optional();
6651
+ }
6652
+ const data_reviver_schema = date_reviver_schema(schema);
6653
+ const revive = (schema2) => (data) => {
6654
+ const revived = schema2.safeParse(data);
6655
+ return revived.success ? revived.data : data;
6596
6656
  };
6597
- collect(schema);
6598
- const found = { date: false };
6599
- const read_schema = to_read_schema(schema, found);
6657
+ const dated_pii = Object.keys(pii_dates).length > 0;
6600
6658
  return {
6601
- sensitive: [...new Set(sensitive2)],
6602
- parse: found.date ? (data) => read_schema.parse(data) : void 0
6659
+ sensitive: Object.keys(pii),
6660
+ date_reviver: data_reviver_schema && revive(data_reviver_schema),
6661
+ pii_date_reviver: dated_pii ? revive(import_zod10.z.looseObject(pii_dates)) : void 0
6603
6662
  };
6604
6663
  }
6605
6664
  function make_event_reader(tags, disclosure, predicate = null) {
6606
- const { sensitive: sensitive2, parse: parse2 } = tags;
6607
- if (!parse2 && sensitive2.length === 0) return void 0;
6665
+ const { sensitive: sensitive2, date_reviver, pii_date_reviver } = tags;
6666
+ if (!date_reviver && sensitive2.length === 0) return void 0;
6608
6667
  const gate = sensitive2.length === 0 ? IDENTITY_GATE : disclosure === "strip" ? ((event) => pii_strip(event, sensitive2)) : make_gate(sensitive2, predicate);
6609
- if (!parse2) return gate;
6610
- return ((event, actor) => gate({ ...event, data: parse2(event.data) }, actor));
6668
+ if (!date_reviver) return gate;
6669
+ const revive_pii = disclosure === "redact" && predicate ? pii_date_reviver : void 0;
6670
+ return ((event, actor) => {
6671
+ const pii = event.pii;
6672
+ return gate(
6673
+ {
6674
+ ...event,
6675
+ data: date_reviver(event.data),
6676
+ ...revive_pii && actor && pii != null ? { pii: revive_pii(pii) } : {}
6677
+ },
6678
+ actor
6679
+ );
6680
+ });
6611
6681
  }
6612
6682
  function build_events(registry, states, lanes, owners) {
6613
6683
  const declared = /* @__PURE__ */ new Set([DEFAULT_LANE, ...lanes.map((l) => l.name)]);
@@ -7394,6 +7464,7 @@ function write_line(writer, line) {
7394
7464
  cache,
7395
7465
  config,
7396
7466
  dateReviver,
7467
+ default_scope,
7397
7468
  dispose,
7398
7469
  disposeAndExit,
7399
7470
  extend,