@rotorsoft/act 0.33.1 → 0.33.3
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/README.md +1 -1
- package/dist/.tsbuildinfo +1 -1
- package/dist/@types/act.d.ts +25 -31
- package/dist/@types/act.d.ts.map +1 -1
- package/dist/@types/adapters/console-logger.d.ts +2 -0
- package/dist/@types/adapters/console-logger.d.ts.map +1 -1
- package/dist/@types/adapters/in-memory-cache.d.ts +7 -1
- package/dist/@types/adapters/in-memory-cache.d.ts.map +1 -1
- package/dist/@types/builders/state-builder.d.ts +37 -21
- package/dist/@types/builders/state-builder.d.ts.map +1 -1
- package/dist/@types/config.d.ts +0 -1
- package/dist/@types/config.d.ts.map +1 -1
- package/dist/@types/internal/correlate-cycle.d.ts +2 -2
- package/dist/@types/internal/drain-cycle.d.ts.map +1 -1
- package/dist/@types/internal/event-sourcing.d.ts.map +1 -1
- package/dist/@types/internal/index.d.ts +2 -2
- package/dist/@types/internal/index.d.ts.map +1 -1
- package/dist/@types/internal/tracing.d.ts +2 -2
- package/dist/@types/internal/tracing.d.ts.map +1 -1
- package/dist/@types/ports.d.ts.map +1 -1
- package/dist/@types/types/action.d.ts +7 -7
- package/dist/@types/types/action.d.ts.map +1 -1
- package/dist/@types/types/errors.d.ts +19 -19
- package/dist/@types/types/errors.d.ts.map +1 -1
- package/dist/@types/types/ports.d.ts +11 -10
- package/dist/@types/types/ports.d.ts.map +1 -1
- package/dist/@types/types/registry.d.ts +13 -4
- package/dist/@types/types/registry.d.ts.map +1 -1
- package/dist/@types/types/schemas.d.ts +19 -19
- package/dist/@types/types/schemas.d.ts.map +1 -1
- package/dist/@types/utils.d.ts.map +1 -1
- package/dist/{chunk-IDEYGKT4.js → chunk-AGWZY6YT.js} +9 -9
- package/dist/chunk-AGWZY6YT.js.map +1 -0
- package/dist/index.cjs +63 -59
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +51 -47
- package/dist/index.js.map +1 -1
- package/dist/types/index.cjs +7 -7
- package/dist/types/index.cjs.map +1 -1
- package/dist/types/index.js +1 -1
- package/package.json +4 -2
- package/dist/chunk-IDEYGKT4.js.map +0 -1
package/dist/index.js
CHANGED
|
@@ -13,7 +13,7 @@ import {
|
|
|
13
13
|
TargetSchema,
|
|
14
14
|
ValidationError,
|
|
15
15
|
ZodEmpty
|
|
16
|
-
} from "./chunk-
|
|
16
|
+
} from "./chunk-AGWZY6YT.js";
|
|
17
17
|
|
|
18
18
|
// src/adapters/console-logger.ts
|
|
19
19
|
var LEVEL_VALUES = {
|
|
@@ -67,8 +67,10 @@ var ConsoleLogger = class _ConsoleLogger {
|
|
|
67
67
|
this.debug = threshold <= 20 ? write.bind(this, "debug", 20) : noop;
|
|
68
68
|
this.trace = threshold <= 10 ? write.bind(this, "trace", 10) : noop;
|
|
69
69
|
}
|
|
70
|
+
/** No-op — `console.log` has no resources to release. */
|
|
70
71
|
async dispose() {
|
|
71
72
|
}
|
|
73
|
+
/** @inheritDoc */
|
|
72
74
|
child(bindings) {
|
|
73
75
|
return new _ConsoleLogger({
|
|
74
76
|
level: this.level,
|
|
@@ -193,28 +195,34 @@ var InMemoryCache = class {
|
|
|
193
195
|
constructor(options) {
|
|
194
196
|
this._entries = new LruMap(options?.maxSize ?? 1e3);
|
|
195
197
|
}
|
|
198
|
+
/** @inheritDoc */
|
|
196
199
|
async get(stream) {
|
|
197
200
|
return this._entries.get(stream);
|
|
198
201
|
}
|
|
202
|
+
/** @inheritDoc */
|
|
199
203
|
async set(stream, entry) {
|
|
200
204
|
this._entries.set(stream, entry);
|
|
201
205
|
}
|
|
206
|
+
/** @inheritDoc */
|
|
202
207
|
async invalidate(stream) {
|
|
203
208
|
this._entries.delete(stream);
|
|
204
209
|
}
|
|
210
|
+
/** @inheritDoc */
|
|
205
211
|
async clear() {
|
|
206
212
|
this._entries.clear();
|
|
207
213
|
}
|
|
214
|
+
/** @inheritDoc */
|
|
208
215
|
async dispose() {
|
|
209
216
|
this._entries.clear();
|
|
210
217
|
}
|
|
218
|
+
/** Current number of entries held by the LRU. */
|
|
211
219
|
get size() {
|
|
212
220
|
return this._entries.size;
|
|
213
221
|
}
|
|
214
222
|
};
|
|
215
223
|
|
|
216
224
|
// src/utils.ts
|
|
217
|
-
import {
|
|
225
|
+
import { prettifyError, ZodError } from "zod";
|
|
218
226
|
|
|
219
227
|
// src/config.ts
|
|
220
228
|
import * as fs from "fs";
|
|
@@ -738,7 +746,7 @@ var InMemoryStore = class {
|
|
|
738
746
|
var ExitCodes = ["ERROR", "EXIT"];
|
|
739
747
|
var adapters = /* @__PURE__ */ new Map();
|
|
740
748
|
function port(injector) {
|
|
741
|
-
return
|
|
749
|
+
return (adapter) => {
|
|
742
750
|
if (!adapters.has(injector.name)) {
|
|
743
751
|
const injected = injector(adapter);
|
|
744
752
|
adapters.set(injector.name, injected);
|
|
@@ -1083,8 +1091,8 @@ var CorrelateCycle = class {
|
|
|
1083
1091
|
}
|
|
1084
1092
|
/**
|
|
1085
1093
|
* Start a periodic correlation worker. Returns false if one is already
|
|
1086
|
-
* running. Errors from `correlate()` are
|
|
1087
|
-
*
|
|
1094
|
+
* running. Errors from `correlate()` are routed through `log()` so they
|
|
1095
|
+
* land in the configured logger (the timer keeps running on failure).
|
|
1088
1096
|
*/
|
|
1089
1097
|
startPolling(query = {}, frequency = 1e4, callback) {
|
|
1090
1098
|
if (this._timer) return false;
|
|
@@ -1092,7 +1100,7 @@ var CorrelateCycle = class {
|
|
|
1092
1100
|
this._timer = setInterval(
|
|
1093
1101
|
() => this.correlate({ ...query, after: this._checkpoint, limit }).then((result) => {
|
|
1094
1102
|
if (callback && result.subscribed) callback(result.subscribed);
|
|
1095
|
-
}).catch(
|
|
1103
|
+
}).catch((err) => log().error(err)),
|
|
1096
1104
|
frequency
|
|
1097
1105
|
);
|
|
1098
1106
|
return true;
|
|
@@ -1530,8 +1538,8 @@ var block = (leases) => store().block(leases);
|
|
|
1530
1538
|
var subscribe = (streams) => store().subscribe(streams);
|
|
1531
1539
|
|
|
1532
1540
|
// src/internal/event-sourcing.ts
|
|
1533
|
-
import { patch } from "@rotorsoft/act-patch";
|
|
1534
1541
|
import { randomUUID as randomUUID3 } from "crypto";
|
|
1542
|
+
import { patch } from "@rotorsoft/act-patch";
|
|
1535
1543
|
async function snap(snapshot) {
|
|
1536
1544
|
try {
|
|
1537
1545
|
const { id, stream, name, meta, version } = snapshot.event;
|
|
@@ -1591,7 +1599,7 @@ async function load(me, stream, callback, asOf) {
|
|
|
1591
1599
|
`Skipping unknown event "${String(e.name)}" on stream "${stream}" (id=${e.id}) \u2014 no reducer in state "${me.name}"`
|
|
1592
1600
|
);
|
|
1593
1601
|
}
|
|
1594
|
-
callback
|
|
1602
|
+
callback?.({
|
|
1595
1603
|
event,
|
|
1596
1604
|
state: state2,
|
|
1597
1605
|
version,
|
|
@@ -1621,7 +1629,7 @@ async function load(me, stream, callback, asOf) {
|
|
|
1621
1629
|
async function action(me, action2, target, payload, reactingTo, skipValidation = false) {
|
|
1622
1630
|
const { stream, expectedVersion, actor } = target;
|
|
1623
1631
|
if (!stream) throw new Error("Missing target stream");
|
|
1624
|
-
|
|
1632
|
+
const validated = skipValidation ? payload : validate(action2, payload, me.actions[action2]);
|
|
1625
1633
|
const snapshot = await load(me, stream);
|
|
1626
1634
|
if (snapshot.event?.name === TOMBSTONE_EVENT)
|
|
1627
1635
|
throw new StreamClosedError(stream);
|
|
@@ -1632,14 +1640,14 @@ async function action(me, action2, target, payload, reactingTo, skipValidation =
|
|
|
1632
1640
|
if (!valid(snapshot.state, actor))
|
|
1633
1641
|
throw new InvariantError(
|
|
1634
1642
|
action2,
|
|
1635
|
-
|
|
1643
|
+
validated,
|
|
1636
1644
|
target,
|
|
1637
1645
|
snapshot,
|
|
1638
1646
|
description
|
|
1639
1647
|
);
|
|
1640
1648
|
});
|
|
1641
1649
|
}
|
|
1642
|
-
const result = me.on[action2](
|
|
1650
|
+
const result = me.on[action2](validated, snapshot, target);
|
|
1643
1651
|
if (!result) return [snapshot];
|
|
1644
1652
|
if (Array.isArray(result) && result.length === 0) {
|
|
1645
1653
|
return [snapshot];
|
|
@@ -1700,7 +1708,7 @@ async function action(me, action2, target, payload, reactingTo, skipValidation =
|
|
|
1700
1708
|
};
|
|
1701
1709
|
});
|
|
1702
1710
|
const last = snapshots.at(-1);
|
|
1703
|
-
const snapped = me.snap
|
|
1711
|
+
const snapped = me.snap?.(last);
|
|
1704
1712
|
cache().set(stream, {
|
|
1705
1713
|
state: last.state,
|
|
1706
1714
|
version: last.event.version,
|
|
@@ -1881,6 +1889,17 @@ function buildDrain(logger) {
|
|
|
1881
1889
|
var DEFAULT_MAX_SUBSCRIBED_STREAMS = 1e3;
|
|
1882
1890
|
var DEFAULT_SETTLE_DEBOUNCE_MS = 10;
|
|
1883
1891
|
var Act = class {
|
|
1892
|
+
/**
|
|
1893
|
+
* Create a new Act orchestrator. Prefer the {@link act} builder over
|
|
1894
|
+
* direct construction — `act()...build()` wires the registry, merges
|
|
1895
|
+
* partial states, and collects batch handlers from registered slices
|
|
1896
|
+
* and projections in one pass.
|
|
1897
|
+
*
|
|
1898
|
+
* @param registry Schemas for every event and action across registered states
|
|
1899
|
+
* @param _states Merged map of state name → state definition
|
|
1900
|
+
* @param batchHandlers Static-target projection batch handlers (target → handler)
|
|
1901
|
+
* @param options Tuning knobs — see {@link ActOptions}
|
|
1902
|
+
*/
|
|
1884
1903
|
constructor(registry, _states = /* @__PURE__ */ new Map(), batchHandlers = /* @__PURE__ */ new Map(), options = {}) {
|
|
1885
1904
|
this.registry = registry;
|
|
1886
1905
|
this._states = _states;
|
|
@@ -1968,12 +1987,6 @@ var Act = class {
|
|
|
1968
1987
|
this._emitter.off(event, listener);
|
|
1969
1988
|
return this;
|
|
1970
1989
|
}
|
|
1971
|
-
/**
|
|
1972
|
-
* Create a new Act orchestrator.
|
|
1973
|
-
*
|
|
1974
|
-
* @param registry The registry of state, event, and action schemas
|
|
1975
|
-
* @param states Map of state names to their (potentially merged) state definitions
|
|
1976
|
-
*/
|
|
1977
1990
|
/** Batch handlers for static-target projections (target → handler) */
|
|
1978
1991
|
_batch_handlers;
|
|
1979
1992
|
/** Event-sourcing handlers, optionally wrapped with trace decorators */
|
|
@@ -2119,14 +2132,9 @@ var Act = class {
|
|
|
2119
2132
|
*
|
|
2120
2133
|
* For small result sets, consider using {@link query_array} instead.
|
|
2121
2134
|
*
|
|
2122
|
-
* @param query -
|
|
2123
|
-
*
|
|
2124
|
-
*
|
|
2125
|
-
* @param query.after - Filter events after this event ID
|
|
2126
|
-
* @param query.before - Filter events before this event ID
|
|
2127
|
-
* @param query.created_after - Filter events after this timestamp
|
|
2128
|
-
* @param query.created_before - Filter events before this timestamp
|
|
2129
|
-
* @param query.limit - Maximum number of events to return
|
|
2135
|
+
* @param query - Filter criteria — see {@link Query} for available fields
|
|
2136
|
+
* (`stream`, `name`, `after`, `before`, `created_after`, `created_before`,
|
|
2137
|
+
* `limit`, `with_snaps`, `stream_exact`)
|
|
2130
2138
|
* @param callback - Optional callback invoked for each matching event
|
|
2131
2139
|
* @returns Object with first event, last event, and total count
|
|
2132
2140
|
*
|
|
@@ -2162,11 +2170,12 @@ var Act = class {
|
|
|
2162
2170
|
* @see {@link query_array} for loading events into memory
|
|
2163
2171
|
*/
|
|
2164
2172
|
async query(query, callback) {
|
|
2165
|
-
let first
|
|
2173
|
+
let first;
|
|
2174
|
+
let last;
|
|
2166
2175
|
const count = await store().query((e) => {
|
|
2167
|
-
!first
|
|
2176
|
+
if (!first) first = e;
|
|
2168
2177
|
last = e;
|
|
2169
|
-
callback
|
|
2178
|
+
callback?.(e);
|
|
2170
2179
|
}, query);
|
|
2171
2180
|
return { first, last, count };
|
|
2172
2181
|
}
|
|
@@ -2217,10 +2226,8 @@ var Act = class {
|
|
|
2217
2226
|
* Call `correlate()` before `drain()` to discover target streams. For a higher-level
|
|
2218
2227
|
* API that handles debouncing, correlation, and signaling automatically, use {@link settle}.
|
|
2219
2228
|
*
|
|
2220
|
-
* @param options - Drain configuration
|
|
2221
|
-
*
|
|
2222
|
-
* @param options.eventLimit - Maximum events to fetch per stream (default: 10)
|
|
2223
|
-
* @param options.leaseMillis - Lease duration in milliseconds (default: 10000)
|
|
2229
|
+
* @param options - Drain configuration — see {@link DrainOptions} for fields
|
|
2230
|
+
* (`streamLimit`, `eventLimit`, `leaseMillis`).
|
|
2224
2231
|
* @returns Drain statistics with fetched, leased, acked, and blocked counts
|
|
2225
2232
|
*
|
|
2226
2233
|
* @example In tests and scripts
|
|
@@ -2255,8 +2262,8 @@ var Act = class {
|
|
|
2255
2262
|
* the next drain cycle.
|
|
2256
2263
|
*
|
|
2257
2264
|
* @param query - Query filter to scan for new correlations
|
|
2258
|
-
* @param query
|
|
2259
|
-
*
|
|
2265
|
+
* @param query - Scan filter — see {@link Query} for fields (typically
|
|
2266
|
+
* `{ after: <event-id>, limit: <count> }`)
|
|
2260
2267
|
* @returns Object with newly leased streams and last scanned event ID
|
|
2261
2268
|
*
|
|
2262
2269
|
* @example Manual correlation
|
|
@@ -2303,9 +2310,8 @@ var Act = class {
|
|
|
2303
2310
|
*
|
|
2304
2311
|
* **Note:** Only one correlation worker can run at a time per Act instance.
|
|
2305
2312
|
*
|
|
2306
|
-
* @param query - Query filter for correlation scans
|
|
2307
|
-
*
|
|
2308
|
-
* @param query.limit - Events to scan per cycle (default: 100)
|
|
2313
|
+
* @param query - Query filter for correlation scans — see {@link Query}
|
|
2314
|
+
* (typically `{ after: -1, limit: 100 }`)
|
|
2309
2315
|
* @param frequency - Correlation frequency in milliseconds (default: 10000)
|
|
2310
2316
|
* @param callback - Optional callback invoked with newly discovered streams
|
|
2311
2317
|
* @returns `true` if worker started, `false` if already running
|
|
@@ -2472,14 +2478,11 @@ var Act = class {
|
|
|
2472
2478
|
* fully catches up paginated streams (e.g. after `reset()` on a long
|
|
2473
2479
|
* projection) without forcing callers to loop.
|
|
2474
2480
|
*
|
|
2475
|
-
* @param options - Settle configuration
|
|
2476
|
-
*
|
|
2477
|
-
*
|
|
2478
|
-
*
|
|
2479
|
-
*
|
|
2480
|
-
* @param options.streamLimit - Maximum streams per drain cycle (default: 10)
|
|
2481
|
-
* @param options.eventLimit - Maximum events per stream (default: 10)
|
|
2482
|
-
* @param options.leaseMillis - Lease duration in milliseconds (default: 10000)
|
|
2481
|
+
* @param options - Settle configuration — see {@link SettleOptions} for fields:
|
|
2482
|
+
* `debounceMs` (default 10), `correlate` (default `{ after: -1, limit: 100 }`),
|
|
2483
|
+
* `maxPasses` (default `Infinity` — kill-switch for runaway loops),
|
|
2484
|
+
* `streamLimit` (default 10), `eventLimit` (default 10),
|
|
2485
|
+
* `leaseMillis` (default 10000).
|
|
2483
2486
|
*
|
|
2484
2487
|
* @example API mutations
|
|
2485
2488
|
* ```typescript
|
|
@@ -2742,7 +2745,8 @@ function action_builder(state2) {
|
|
|
2742
2745
|
throw new Error(`Duplicate action "${action2}"`);
|
|
2743
2746
|
internal.actions[action2] = schema;
|
|
2744
2747
|
function given(rules) {
|
|
2745
|
-
|
|
2748
|
+
internal.given ??= {};
|
|
2749
|
+
internal.given[action2] = rules;
|
|
2746
2750
|
return { emit };
|
|
2747
2751
|
}
|
|
2748
2752
|
function emit(handler) {
|