@rotorsoft/act 1.35.0 → 1.35.2

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
@@ -40,7 +40,7 @@ import {
40
40
  sleep,
41
41
  store,
42
42
  validate
43
- } from "./chunk-MHPUVTMX.js";
43
+ } from "./chunk-SVVVS6GA.js";
44
44
  import {
45
45
  ActorSchema,
46
46
  CausationEventSchema,
@@ -71,7 +71,7 @@ import {
71
71
  pii_fields,
72
72
  pii_schemas,
73
73
  sensitive
74
- } from "./chunk-DH2HW5XJ.js";
74
+ } from "./chunk-H2QN755D.js";
75
75
 
76
76
  // src/signals.ts
77
77
  process.once("SIGINT", async (arg) => {
@@ -1648,12 +1648,30 @@ var subscribe = (streams, correlated_at, correlator) => store().subscribe(stream
1648
1648
 
1649
1649
  // src/internal/event-sourcing.ts
1650
1650
  import { patch } from "@rotorsoft/act-patch";
1651
- import { prettifyError } from "zod";
1651
+ import { prettifyError, ZodError } from "zod";
1652
1652
  var bare_patch = (_me, state2, partial) => patch(state2, partial);
1653
- var check_invariants = (target, state2, schema) => {
1654
- const result = schema.safeParse(state2);
1655
- if (!result.success)
1656
- throw new ValidationError(target, state2, prettifyError(result.error));
1653
+ var value_at = (value, path) => path.reduce(
1654
+ (at, key) => at?.[key],
1655
+ value
1656
+ );
1657
+ var check_invariants = (target, state2, schema, pii_aware) => {
1658
+ let result;
1659
+ try {
1660
+ result = schema.safeParse(state2);
1661
+ } catch (error) {
1662
+ throw new ValidationError(target, state2, error);
1663
+ }
1664
+ if (result.success) return;
1665
+ const issues = pii_aware ? result.error.issues.filter((issue) => {
1666
+ const value = value_at(state2, issue.path);
1667
+ return value !== REDACTED && value !== SHREDDED;
1668
+ }) : result.error.issues;
1669
+ if (issues.length)
1670
+ throw new ValidationError(
1671
+ target,
1672
+ state2,
1673
+ prettifyError(new ZodError(issues))
1674
+ );
1657
1675
  };
1658
1676
  var DEFAULT_BATCH = 500;
1659
1677
  var cache_put = (stream, entry) => cache().set(stream, entry).catch(
@@ -1909,7 +1927,7 @@ async function action(me, action2, target, payload, options, patch_fn = bare_pat
1909
1927
  );
1910
1928
  if (snapshot.event?.name === TOMBSTONE_EVENT)
1911
1929
  throw new StreamClosedError(stream);
1912
- const expected = expectedVersion ?? (snapshot.version >= 0 ? snapshot.version : void 0);
1930
+ const expected = expectedVersion ?? snapshot.version;
1913
1931
  for (const { valid, description } of me.given?.[action2] ?? [])
1914
1932
  if (!valid(snapshot.state, actor))
1915
1933
  throw new InvariantError(
@@ -1973,17 +1991,15 @@ async function action(me, action2, target, payload, options, patch_fn = bare_pat
1973
1991
  patches++;
1974
1992
  return { state: state2, patch: patch2, patches };
1975
1993
  });
1976
- check_invariants(`${me.name}.${action2}`, state2, me.state);
1994
+ check_invariants(
1995
+ `${me.name}.${action2}`,
1996
+ state2,
1997
+ me.state,
1998
+ me.pii_aware
1999
+ );
1977
2000
  let committed;
1978
2001
  try {
1979
- committed = await store().commit(
1980
- stream,
1981
- emitted,
1982
- meta,
1983
- // Reactions skip the inferred version guard (leases already
1984
- // serialize them); an explicit expectedVersion is still honored.
1985
- reactingTo ? expectedVersion : expected
1986
- );
2002
+ committed = await store().commit(stream, emitted, meta, expected);
1987
2003
  } catch (error) {
1988
2004
  if (error instanceof ConcurrencyError) void cache_drop(stream);
1989
2005
  throw error;
@@ -2000,21 +2016,16 @@ async function action(me, action2, target, payload, options, patch_fn = bare_pat
2000
2016
  replayed: snapshot.replayed
2001
2017
  }));
2002
2018
  const last = snapshots.at(-1);
2003
- const contiguous = committed[0].version === snapshot.version + 1;
2004
- const snapped = contiguous && me.snap?.(last);
2005
- const snap_event = snapped ? await snap(last) : void 0;
2006
- if (!me.pii_aware) {
2007
- if (contiguous)
2008
- void cache_put(stream, {
2009
- stream,
2010
- state: last.state,
2011
- version: snap_event?.version ?? last.event.version,
2012
- event_id: snap_event?.id ?? last.event.id,
2013
- patches: snap_event ? 0 : last.patches,
2014
- snaps: snap_event ? last.snaps + 1 : last.snaps
2015
- });
2016
- else void cache_drop(stream);
2017
- }
2019
+ const snap_event = me.snap?.(last) ? await snap(last) : void 0;
2020
+ if (!me.pii_aware)
2021
+ void cache_put(stream, {
2022
+ stream,
2023
+ state: last.state,
2024
+ version: snap_event?.version ?? last.event.version,
2025
+ event_id: snap_event?.id ?? last.event.id,
2026
+ patches: snap_event ? 0 : last.patches,
2027
+ snaps: snap_event ? last.snaps + 1 : last.snaps
2028
+ });
2018
2029
  return snapshots;
2019
2030
  } catch (error) {
2020
2031
  if (!(error instanceof ConcurrencyError)) throw error;