@rotorsoft/act 0.40.0 → 0.41.0
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/.tsbuildinfo +1 -1
- package/dist/@types/builders/act-builder.d.ts.map +1 -1
- package/dist/@types/builders/slice-builder.d.ts.map +1 -1
- package/dist/@types/internal/backoff.d.ts +22 -0
- package/dist/@types/internal/backoff.d.ts.map +1 -0
- package/dist/@types/internal/correlate-cycle.d.ts.map +1 -1
- package/dist/@types/internal/drain-cycle.d.ts +34 -1
- package/dist/@types/internal/drain-cycle.d.ts.map +1 -1
- package/dist/@types/internal/lru-map.d.ts.map +1 -0
- package/dist/@types/internal/reactions.d.ts.map +1 -1
- package/dist/@types/types/reaction.d.ts +44 -0
- package/dist/@types/types/reaction.d.ts.map +1 -1
- package/dist/{chunk-TP2OZWHP.js → chunk-M5YFKVRV.js} +2 -2
- package/dist/chunk-M5YFKVRV.js.map +1 -0
- package/dist/index.cjs +87 -8
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +87 -8
- package/dist/index.js.map +1 -1
- package/dist/test/index.cjs +1 -1
- package/dist/test/index.cjs.map +1 -1
- package/dist/test/index.js +1 -1
- package/package.json +2 -2
- package/dist/@types/lru-map.d.ts.map +0 -1
- package/dist/chunk-TP2OZWHP.js.map +0 -1
- /package/dist/@types/{lru-map.d.ts → internal/lru-map.d.ts} +0 -0
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"backoff.d.ts","sourceRoot":"","sources":["../../../src/internal/backoff.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAEH,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,sBAAsB,CAAC;AAE3D;;;;;;;;;GASG;AACH,wBAAgB,mBAAmB,CACjC,KAAK,EAAE,MAAM,EACb,IAAI,EAAE,cAAc,GAAG,SAAS,GAC/B,MAAM,CAkBR"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"correlate-cycle.d.ts","sourceRoot":"","sources":["../../../src/internal/correlate-cycle.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;
|
|
1
|
+
{"version":3,"file":"correlate-cycle.d.ts","sourceRoot":"","sources":["../../../src/internal/correlate-cycle.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;AAGH,OAAO,KAAK,EACV,KAAK,EAEL,QAAQ,EACR,cAAc,EACd,OAAO,EACR,MAAM,mBAAmB,CAAC;AAC3B,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,YAAY,CAAC;AAG3C;;;;;;;;;GASG;AACH,MAAM,MAAM,YAAY,GAAG;IACzB,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAC;IACxB,QAAQ,CAAC,MAAM,CAAC,EAAE,MAAM,CAAC;IACzB,QAAQ,CAAC,QAAQ,CAAC,EAAE,MAAM,CAAC;CAC5B,CAAC;AAEF;;;;;GAKG;AACH,qBAAa,cAAc,CACzB,UAAU,SAAS,cAAc,CAAC,QAAQ,CAAC,EAC3C,OAAO,SAAS,OAAO,EACvB,QAAQ,SAAS,OAAO;IAQtB,OAAO,CAAC,QAAQ,CAAC,QAAQ;IACzB,OAAO,CAAC,QAAQ,CAAC,aAAa;IAC9B,OAAO,CAAC,QAAQ,CAAC,mBAAmB;IACpC,OAAO,CAAC,QAAQ,CAAC,EAAE;IAEnB,OAAO,CAAC,QAAQ,CAAC,MAAM,CAAC;IAX1B,OAAO,CAAC,WAAW,CAAM;IACzB,OAAO,CAAC,YAAY,CAAS;IAC7B,OAAO,CAAC,MAAM,CAAyD;IACvE,OAAO,CAAC,QAAQ,CAAC,WAAW,CAAiB;gBAG1B,QAAQ,EAAE,QAAQ,CAAC,UAAU,EAAE,OAAO,EAAE,QAAQ,CAAC,EACjD,aAAa,EAAE,aAAa,CAAC,YAAY,CAAC,EAC1C,mBAAmB,EAAE,OAAO,EAC5B,EAAE,EAAE,QAAQ,CAAC,OAAO,CAAC,EACtC,oBAAoB,EAAE,MAAM,EACX,MAAM,CAAC,GAAE,MAAM,IAAI,aAAA;IAKtC,gCAAgC;IAChC,IAAI,UAAU,IAAI,MAAM,CAEvB;IAED;;;;;;OAMG;IACG,IAAI,IAAI,OAAO,CAAC,IAAI,CAAC;IAY3B;;;;OAIG;IACG,SAAS,CACb,KAAK,GAAE,KAAgC,GACtC,OAAO,CAAC;QAAE,UAAU,EAAE,MAAM,CAAC;QAAC,OAAO,EAAE,MAAM,CAAA;KAAE,CAAC;IA8EnD;;;;OAIG;IACH,YAAY,CACV,KAAK,GAAE,KAAU,EACjB,SAAS,SAAS,EAClB,QAAQ,CAAC,EAAE,CAAC,UAAU,EAAE,MAAM,KAAK,IAAI,GACtC,OAAO;IAgBV,wDAAwD;IACxD,WAAW,IAAI,IAAI;CAMpB"}
|
|
@@ -28,6 +28,13 @@ export type HandleResult = Readonly<{
|
|
|
28
28
|
at: number;
|
|
29
29
|
error?: string;
|
|
30
30
|
block?: boolean;
|
|
31
|
+
/**
|
|
32
|
+
* Wall-clock timestamp (ms since epoch) at which the next attempt on
|
|
33
|
+
* this stream may run. Populated by `_finalize` only on retry paths
|
|
34
|
+
* where the reaction defined `options.backoff`. Undefined means "no
|
|
35
|
+
* backoff configured" — drain re-attempts as soon as the lease expires.
|
|
36
|
+
*/
|
|
37
|
+
nextAttemptAt?: number;
|
|
31
38
|
}>;
|
|
32
39
|
/**
|
|
33
40
|
* Per-event reaction dispatcher signature (matches `Act.handle`).
|
|
@@ -60,9 +67,17 @@ export type DrainCycle<TEvents extends Schemas> = {
|
|
|
60
67
|
* Returns `undefined` when nothing was claimed — caller can short-circuit
|
|
61
68
|
* the rest of the drain pass.
|
|
62
69
|
*
|
|
70
|
+
* **Deferred streams.** When `isDeferred(stream)` returns `true`, the
|
|
71
|
+
* cycle skips dispatch for that lease — no handle, no ack, no block. The
|
|
72
|
+
* lease holds for `leaseMillis` via the existing claim mechanism, which
|
|
73
|
+
* blocks competing workers from re-attempting during the backoff window
|
|
74
|
+
* and serves as the per-worker pacing timer. Subsequent claims after
|
|
75
|
+
* `leased_until` expires will re-acquire the lease and re-skip until the
|
|
76
|
+
* controller clears the entry.
|
|
77
|
+
*
|
|
63
78
|
* @internal
|
|
64
79
|
*/
|
|
65
|
-
export declare function runDrainCycle<TEvents extends Schemas, TActions extends Schemas, TSchemaReg extends SchemaRegister<TActions>>(ops: DrainOps<TEvents>, registry: Registry<TSchemaReg, TEvents, TActions>, batchHandlers: Map<string, BatchHandler<TEvents>>, handle: Handle<TEvents>, handleBatch: HandleBatch<TEvents>, lagging: number, leading: number, eventLimit: number, leaseMillis: number): Promise<DrainCycle<TEvents> | undefined>;
|
|
80
|
+
export declare function runDrainCycle<TEvents extends Schemas, TActions extends Schemas, TSchemaReg extends SchemaRegister<TActions>>(ops: DrainOps<TEvents>, registry: Registry<TSchemaReg, TEvents, TActions>, batchHandlers: Map<string, BatchHandler<TEvents>>, handle: Handle<TEvents>, handleBatch: HandleBatch<TEvents>, lagging: number, leading: number, eventLimit: number, leaseMillis: number, isDeferred?: (stream: string) => boolean): Promise<DrainCycle<TEvents> | undefined>;
|
|
66
81
|
/**
|
|
67
82
|
* Dependencies the {@link DrainController} needs from the orchestrator.
|
|
68
83
|
* The lifecycle event sinks (`onAcked` / `onBlocked`) are callbacks so
|
|
@@ -98,6 +113,15 @@ export declare class DrainController<TEvents extends Schemas, TActions extends S
|
|
|
98
113
|
private _armed;
|
|
99
114
|
private _locked;
|
|
100
115
|
private _ratio;
|
|
116
|
+
/**
|
|
117
|
+
* Per-stream backoff: `stream → nextAttemptAt` (ms since epoch). Set by
|
|
118
|
+
* `_finalize` via `HandleResult.nextAttemptAt`; cleared on successful
|
|
119
|
+
* ack or terminal block. Lives in process memory — per-worker pacing
|
|
120
|
+
* by design (see {@link BackoffOptions} for the multi-worker trade-off).
|
|
121
|
+
*/
|
|
122
|
+
private _backoff;
|
|
123
|
+
/** Timer re-arming drain at the earliest pending `nextAttemptAt`. */
|
|
124
|
+
private _backoffTimer;
|
|
101
125
|
constructor(deps: DrainControllerDeps<TEvents, TActions, TSchemaReg>);
|
|
102
126
|
/**
|
|
103
127
|
* Signal that a commit (or reset / cold-start) may have produced work.
|
|
@@ -107,6 +131,15 @@ export declare class DrainController<TEvents extends Schemas, TActions extends S
|
|
|
107
131
|
arm(): void;
|
|
108
132
|
/** Read-only flag — true while a commit / reset is unprocessed. */
|
|
109
133
|
get armed(): boolean;
|
|
134
|
+
/** Returns true when `stream` is currently within a backoff window. */
|
|
135
|
+
private isDeferred;
|
|
136
|
+
/**
|
|
137
|
+
* Schedule the next drain re-arm at the earliest pending backoff
|
|
138
|
+
* expiry. Called only when the backoff map is non-empty (caller guard).
|
|
139
|
+
* Idempotent — collapses many simultaneously deferred streams into a
|
|
140
|
+
* single timer.
|
|
141
|
+
*/
|
|
142
|
+
private scheduleBackoffWake;
|
|
110
143
|
/** Run one drain pass. Short-circuits when not armed or already running. */
|
|
111
144
|
drain({ streamLimit, eventLimit, leaseMillis, }?: DrainOptions): Promise<Drain<TEvents>>;
|
|
112
145
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"drain-cycle.d.ts","sourceRoot":"","sources":["../../../src/internal/drain-cycle.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;GAeG;AAGH,OAAO,KAAK,EACV,YAAY,EACZ,YAAY,EACZ,KAAK,EACL,YAAY,EACZ,KAAK,EACL,KAAK,EACL,MAAM,EACN,eAAe,EACf,QAAQ,EACR,cAAc,EACd,OAAO,EACR,MAAM,mBAAmB,CAAC;AAC3B,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,YAAY,CAAC;AAG3C;;;;;GAKG;AACH,MAAM,MAAM,YAAY,GAAG,QAAQ,CAAC;IAClC,KAAK,EAAE,KAAK,CAAC;IACb,OAAO,EAAE,MAAM,CAAC;IAChB,EAAE,EAAE,MAAM,CAAC;IACX,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,KAAK,CAAC,EAA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AAC,EAAE,CAAC;IAGV,OAAO,CAAC,QAAQ,CAAC,QAAQ;IAFrC,OAAO,CAAC,QAAQ,CAAC,QAAQ,CAAmB;gBAEf,QAAQ,EAAE,MAAM;IAE7C,GAAG,CAAC,GAAG,EAAE,CAAC,GAAG,CAAC,GAAG,SAAS;IAS1B,GAAG,CAAC,GAAG,EAAE,CAAC,GAAG,OAAO;IAIpB,GAAG,CAAC,GAAG,EAAE,CAAC,EAAE,KAAK,EAAE,CAAC,GAAG,IAAI;IAW3B,MAAM,CAAC,GAAG,EAAE,CAAC,GAAG,OAAO;IAIvB,KAAK,IAAI,IAAI;IAIb,IAAI,IAAI,IAAI,MAAM,CAEjB;CACF;AAED;;;;;;GAMG;AACH,qBAAa,MAAM,CAAC,CAAC;IACnB,OAAO,CAAC,QAAQ,CAAC,IAAI,CAAkB;gBAE3B,OAAO,EAAE,MAAM;IAI3B,GAAG,CAAC,KAAK,EAAE,CAAC,GAAG,OAAO;IAItB,GAAG,CAAC,KAAK,EAAE,CAAC,GAAG,IAAI;IAInB,MAAM,CAAC,KAAK,EAAE,CAAC,GAAG,OAAO;IAIzB,KAAK,IAAI,IAAI;IAIb,IAAI,IAAI,IAAI,MAAM,CAEjB;CACF"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"reactions.d.ts","sourceRoot":"","sources":["../../../src/internal/reactions.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;GAiBG;AAEH,OAAO,KAAK,EACV,KAAK,EAGL,IAAI,EAEJ,MAAM,EAGN,OAAO,EAER,MAAM,mBAAmB,CAAC;
|
|
1
|
+
{"version":3,"file":"reactions.d.ts","sourceRoot":"","sources":["../../../src/internal/reactions.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;GAiBG;AAEH,OAAO,KAAK,EACV,KAAK,EAGL,IAAI,EAEJ,MAAM,EAGN,OAAO,EAER,MAAM,mBAAmB,CAAC;AAE3B,OAAO,KAAK,EAAE,MAAM,EAAE,WAAW,EAAgB,MAAM,kBAAkB,CAAC;AAE1E;;;;;;GAMG;AACH,MAAM,MAAM,YAAY,CACtB,OAAO,SAAS,OAAO,EACvB,QAAQ,SAAS,OAAO,EACxB,MAAM,SAAS,KAAK,GAAG,KAAK,IAC1B;IACF,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAC;IACxB,QAAQ,CAAC,OAAO,EAAE,IAAI,CAAC,OAAO,EAAE,QAAQ,EAAE,MAAM,CAAC,CAAC,IAAI,CAAC,CAAC;IACxD,QAAQ,CAAC,SAAS,EAAE,IAAI,CAAC,OAAO,EAAE,QAAQ,EAAE,MAAM,CAAC,CAAC,MAAM,CAAC,CAAC;IAC5D,QAAQ,CAAC,UAAU,EAAE,IAAI,CAAC,OAAO,EAAE,QAAQ,EAAE,MAAM,CAAC,CAAC,OAAO,CAAC,CAAC;IAC9D,QAAQ,CAAC,eAAe,EAAE,IAAI,CAAC,OAAO,EAAE,QAAQ,EAAE,MAAM,CAAC,CAAC,aAAa,CAAC,CAAC;CAC1E,CAAC;AAqCF;;;;;;;;;GASG;AACH,wBAAgB,WAAW,CACzB,OAAO,SAAS,OAAO,EACvB,QAAQ,SAAS,OAAO,EACxB,MAAM,SAAS,KAAK,GAAG,KAAK,EAC5B,IAAI,EAAE,YAAY,CAAC,OAAO,EAAE,QAAQ,EAAE,MAAM,CAAC,GAAG,MAAM,CAAC,OAAO,CAAC,CAoDhE;AAED;;;;;;GAMG;AACH,wBAAgB,gBAAgB,CAAC,OAAO,SAAS,OAAO,EACtD,MAAM,EAAE,MAAM,GACb,WAAW,CAAC,OAAO,CAAC,CA2BtB"}
|
|
@@ -114,14 +114,58 @@ export type Resolved = {
|
|
|
114
114
|
readonly source?: string;
|
|
115
115
|
readonly priority?: number;
|
|
116
116
|
};
|
|
117
|
+
/**
|
|
118
|
+
* Backoff strategy for delaying the next retry attempt after a reaction
|
|
119
|
+
* handler throws.
|
|
120
|
+
*
|
|
121
|
+
* - `fixed` — wait `baseMs` between attempts
|
|
122
|
+
* - `linear` — wait `baseMs * (retry + 1)`
|
|
123
|
+
* - `exponential` — wait `baseMs * 2^retry`, capped at `maxMs` if provided
|
|
124
|
+
*
|
|
125
|
+
* `retry` is the lease's retry counter at finalize time, where `0` is the
|
|
126
|
+
* first attempt that just failed. So the delay applies *before* the next
|
|
127
|
+
* attempt.
|
|
128
|
+
*/
|
|
129
|
+
export type BackoffStrategy = "fixed" | "linear" | "exponential";
|
|
130
|
+
/**
|
|
131
|
+
* Per-reaction retry backoff configuration. When set, the drain controller
|
|
132
|
+
* defers the next attempt at this stream until the computed delay has
|
|
133
|
+
* elapsed.
|
|
134
|
+
*
|
|
135
|
+
* Backoff state lives in process memory on the {@link DrainController}.
|
|
136
|
+
* With N competing workers (each running its own controller), retries
|
|
137
|
+
* escalate at most N× faster than configured — the shared `retry` counter
|
|
138
|
+
* on the stream watermark climbs across workers, reaching the
|
|
139
|
+
* `blockOnError` threshold sooner. This is intentional: per-worker pacing
|
|
140
|
+
* speeds up recovery on transient per-worker faults, and poison messages
|
|
141
|
+
* still get quarantined.
|
|
142
|
+
*
|
|
143
|
+
* @property strategy - {@link BackoffStrategy}
|
|
144
|
+
* @property baseMs - Base delay (must be ≥ 0)
|
|
145
|
+
* @property maxMs - Optional cap; only used by `exponential`
|
|
146
|
+
* @property jitter - Multiply final delay by `0.5 + random()` (range
|
|
147
|
+
* `[0.5, 1.5)`) to avoid thundering herds when many streams retry in
|
|
148
|
+
* lockstep
|
|
149
|
+
*/
|
|
150
|
+
export type BackoffOptions = {
|
|
151
|
+
readonly strategy: BackoffStrategy;
|
|
152
|
+
readonly baseMs: number;
|
|
153
|
+
readonly maxMs?: number;
|
|
154
|
+
readonly jitter?: boolean;
|
|
155
|
+
};
|
|
117
156
|
/**
|
|
118
157
|
* Options for reaction processing.
|
|
119
158
|
* @property blockOnError - Whether to block on error.
|
|
120
159
|
* @property maxRetries - Maximum number of retries.
|
|
160
|
+
* @property backoff - Optional retry pacing. When omitted, retries run as
|
|
161
|
+
* soon as the lease expires (current behavior — implicit backoff bounded
|
|
162
|
+
* by `leaseMillis`). When set, the drain controller waits at least the
|
|
163
|
+
* computed delay before re-attempting on this worker.
|
|
121
164
|
*/
|
|
122
165
|
export type ReactionOptions = {
|
|
123
166
|
readonly blockOnError: boolean;
|
|
124
167
|
readonly maxRetries: number;
|
|
168
|
+
readonly backoff?: BackoffOptions;
|
|
125
169
|
};
|
|
126
170
|
/**
|
|
127
171
|
* Distributive mapped type that produces a proper discriminated union of
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"reaction.d.ts","sourceRoot":"","sources":["../../../src/types/reaction.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AACH,OAAO,KAAK,EACV,KAAK,EACL,SAAS,EACT,IAAI,EACJ,KAAK,EACL,MAAM,EACN,OAAO,EACP,QAAQ,EACT,MAAM,aAAa,CAAC;AAErB;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAkCG;AACH,MAAM,MAAM,eAAe,CACzB,OAAO,SAAS,OAAO,EACvB,IAAI,SAAS,MAAM,OAAO,EAC1B,QAAQ,SAAS,OAAO,GAAG,OAAO,EAClC,MAAM,SAAS,KAAK,GAAG,KAAK,IAC1B,CACF,KAAK,EAAE,SAAS,CAAC,OAAO,EAAE,IAAI,CAAC,EAC/B,MAAM,EAAE,MAAM,EACd,GAAG,EAAE,IAAI,CAAC,OAAO,EAAE,QAAQ,EAAE,MAAM,CAAC,KACjC,OAAO,CAAC,QAAQ,CAAC,MAAM,EAAE,OAAO,CAAC,GAAG,IAAI,CAAC,CAAC;AAE/C;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAmDG;AACH,MAAM,MAAM,gBAAgB,CAC1B,OAAO,SAAS,OAAO,EACvB,IAAI,SAAS,MAAM,OAAO,IAExB,QAAQ,GACR,CAAC,CAAC,KAAK,EAAE,SAAS,CAAC,OAAO,EAAE,IAAI,CAAC,KAAK,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MAAM,CAAC;IACvB,QAAQ,CAAC,OAAO,EAAE,OAAO,CAAC;CAC3B,CAAC;AAEF;;;;GAIG;AACH,MAAM,MAAM,YAAY,GAAG,KAAK,GAAG;IAAE,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAA;CAAE,CAAC;AAE9D;;;;;GAKG;AACH,MAAM,MAAM,YAAY,GAAG;IACzB,QAAQ,CAAC,WAAW,CAAC,EAAE,MAAM,CAAC;IAC9B,QAAQ,CAAC,UAAU,CAAC,EAAE,MAAM,CAAC;IAC7B,QAAQ,CAAC,WAAW,CAAC,EAAE,MAAM,CAAC;CAC/B,CAAC;AAEF;;;;;;GAMG;AACH,MAAM,MAAM,KAAK,CAAC,OAAO,SAAS,OAAO,IAAI;IAC3C,QAAQ,CAAC,OAAO,EAAE,KAAK,CAAC,OAAO,CAAC,CAAC;IACjC,QAAQ,CAAC,MAAM,EAAE,KAAK,EAAE,CAAC;IACzB,QAAQ,CAAC,KAAK,EAAE,KAAK,EAAE,CAAC;IACxB,QAAQ,CAAC,OAAO,EAAE,YAAY,EAAE,CAAC;CAClC,CAAC;AAEF;;;;;;;;;;;;;GAaG;AACH,MAAM,MAAM,aAAa,GAAG,YAAY,GAAG;IACzC,QAAQ,CAAC,UAAU,CAAC,EAAE,MAAM,CAAC;IAC7B,QAAQ,CAAC,SAAS,CAAC,EAAE,KAAK,CAAC;IAC3B,QAAQ,CAAC,SAAS,CAAC,EAAE,MAAM,CAAC;CAC7B,CAAC"}
|
|
@@ -120,7 +120,7 @@ var ConsoleLogger = class _ConsoleLogger {
|
|
|
120
120
|
}
|
|
121
121
|
};
|
|
122
122
|
|
|
123
|
-
// src/lru-map.ts
|
|
123
|
+
// src/internal/lru-map.ts
|
|
124
124
|
var LruMap = class {
|
|
125
125
|
constructor(_maxSize) {
|
|
126
126
|
this._maxSize = _maxSize;
|
|
@@ -874,4 +874,4 @@ export {
|
|
|
874
874
|
SNAP_EVENT,
|
|
875
875
|
TOMBSTONE_EVENT
|
|
876
876
|
};
|
|
877
|
-
//# sourceMappingURL=chunk-
|
|
877
|
+
//# sourceMappingURL=chunk-M5YFKVRV.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/adapters/console-logger.ts","../src/internal/lru-map.ts","../src/adapters/in-memory-cache.ts","../src/config.ts","../src/ports.ts","../src/utils.ts","../src/adapters/in-memory-store.ts"],"sourcesContent":["/**\n * @module adapters/console-logger\n *\n * High-performance console logger inspired by pino's design:\n * - Numeric level comparison for O(1) gating\n * - stdout.write() in production for raw JSON lines (no console overhead)\n * - Colorized single-line output in development\n * - No-op method replacement when level is above threshold\n * - Child logger support with merged bindings\n */\nimport type { Logger } from \"../types/index.js\";\n\nconst LEVEL_VALUES: Record<string, number> = {\n fatal: 60,\n error: 50,\n warn: 40,\n info: 30,\n debug: 20,\n trace: 10,\n};\n\nconst LEVEL_COLORS: Record<string, string> = {\n fatal: \"\\x1b[41m\\x1b[37m\", // white on red bg\n error: \"\\x1b[31m\", // red\n warn: \"\\x1b[33m\", // yellow\n info: \"\\x1b[32m\", // green\n debug: \"\\x1b[36m\", // cyan\n trace: \"\\x1b[90m\", // gray\n};\n\nconst RESET = \"\\x1b[0m\";\n\nconst noop = () => {};\n\n/**\n * Default console logger for the Act framework.\n *\n * Production mode emits newline-delimited JSON (compatible with GCP, AWS\n * CloudWatch, Datadog, and other structured log ingestion systems).\n *\n * Development mode emits colorized, human-readable output.\n */\nexport class ConsoleLogger implements Logger {\n level: string;\n private readonly _pretty: boolean;\n\n readonly fatal: Logger[\"fatal\"];\n readonly error: Logger[\"error\"];\n readonly warn: Logger[\"warn\"];\n readonly info: Logger[\"info\"];\n readonly debug: Logger[\"debug\"];\n readonly trace: Logger[\"trace\"];\n\n constructor(\n options: {\n level?: string;\n pretty?: boolean;\n bindings?: Record<string, unknown>;\n } = {}\n ) {\n const {\n level = \"info\",\n pretty = process.env.NODE_ENV !== \"production\",\n bindings,\n } = options;\n this._pretty = pretty;\n this.level = level;\n\n const threshold = LEVEL_VALUES[level] ?? 30;\n const write = pretty\n ? this._prettyWrite.bind(this, bindings)\n : this._jsonWrite.bind(this, bindings);\n\n // Assign methods — noop when level is gated (like pino's level-based replacement)\n this.fatal = write.bind(this, \"fatal\", 60); // fatal is always enabled\n this.error = threshold <= 50 ? write.bind(this, \"error\", 50) : noop;\n this.warn = threshold <= 40 ? write.bind(this, \"warn\", 40) : noop;\n this.info = threshold <= 30 ? write.bind(this, \"info\", 30) : noop;\n this.debug = threshold <= 20 ? write.bind(this, \"debug\", 20) : noop;\n this.trace = threshold <= 10 ? write.bind(this, \"trace\", 10) : noop;\n }\n\n /** No-op — `console.log` has no resources to release. */\n async dispose(): Promise<void> {}\n\n /** @inheritDoc */\n child(bindings: Record<string, unknown>): Logger {\n return new ConsoleLogger({\n level: this.level,\n pretty: this._pretty,\n bindings,\n });\n }\n\n private _jsonWrite(\n bindings: Record<string, unknown> | undefined,\n level: string,\n _num: number,\n objOrMsg: unknown,\n msg?: string\n ): void {\n let obj: Record<string, unknown>;\n let message: string | undefined;\n\n if (typeof objOrMsg === \"string\") {\n message = objOrMsg;\n obj = {};\n } else if (objOrMsg !== null && typeof objOrMsg === \"object\") {\n message = msg;\n obj = { ...(objOrMsg as Record<string, unknown>) };\n } else {\n message = msg;\n obj = { value: objOrMsg };\n }\n\n const entry = Object.assign({ level, time: Date.now() }, bindings, obj);\n if (message) entry.msg = message;\n\n let line: string;\n try {\n line = JSON.stringify(entry);\n } catch {\n // Cyclic or unserializable payload — emit a minimal line rather\n // than crash the log call site.\n line = JSON.stringify({\n level,\n time: entry.time,\n msg: message ?? \"[unserializable]\",\n unserializable: true,\n });\n }\n process.stdout.write(line + \"\\n\");\n }\n\n private _prettyWrite(\n bindings: Record<string, unknown> | undefined,\n level: string,\n _num: number,\n objOrMsg: unknown,\n msg?: string\n ): void {\n const color = LEVEL_COLORS[level];\n const tag = `${color}${level.toUpperCase().padEnd(5)}${RESET}`;\n const ts = new Date().toISOString().slice(11, 23); // HH:mm:ss.SSS\n\n let message: string;\n let data: string | undefined;\n\n if (typeof objOrMsg === \"string\") {\n message = objOrMsg;\n } else {\n message = msg ?? \"\";\n if (objOrMsg !== undefined && objOrMsg !== null) {\n try {\n data = JSON.stringify(objOrMsg);\n } catch {\n data = \"[unserializable]\";\n }\n }\n }\n\n const bindStr =\n bindings && Object.keys(bindings).length\n ? ` ${JSON.stringify(bindings)}`\n : \"\";\n\n const parts = [ts, tag, message, data, bindStr].filter(Boolean);\n process.stdout.write(parts.join(\" \") + \"\\n\");\n }\n}\n","/**\n * @module lru-map\n * @category Internal\n *\n * Tiny bounded LRU map / set built on insertion-ordered `Map`. Used to cap\n * memory in long-running orchestrators that mint large numbers of keys —\n * notably:\n *\n * - {@link InMemoryCache}: stream → state checkpoint\n * - `Act._subscribed_streams`: stream → presence (LruSet)\n *\n * Apps with millions of dynamic streams (one target per aggregate) can't\n * afford an unbounded `Set<string>` — eviction is required.\n *\n * @internal\n */\n\n/**\n * Bounded LRU map. `get()` promotes; `has()` does not. `set()` always\n * promotes and evicts the oldest entry when at capacity.\n *\n * @internal\n */\nexport class LruMap<K, V> {\n private readonly _entries = new Map<K, V>();\n\n constructor(private readonly _maxSize: number) {}\n\n get(key: K): V | undefined {\n const v = this._entries.get(key);\n if (v === undefined) return undefined;\n // promote: delete + re-insert moves to most-recent position\n this._entries.delete(key);\n this._entries.set(key, v);\n return v;\n }\n\n has(key: K): boolean {\n return this._entries.has(key);\n }\n\n set(key: K, value: V): void {\n this._entries.delete(key);\n if (this._entries.size >= this._maxSize) {\n // size >= maxSize ≥ 1 → at least one entry exists → next().value\n // is the oldest key (asserted with `!`).\n const oldest = this._entries.keys().next().value!;\n this._entries.delete(oldest);\n }\n this._entries.set(key, value);\n }\n\n delete(key: K): boolean {\n return this._entries.delete(key);\n }\n\n clear(): void {\n this._entries.clear();\n }\n\n get size(): number {\n return this._entries.size;\n }\n}\n\n/**\n * Bounded LRU set built on top of {@link LruMap}. `has()` does not promote;\n * `add()` does (re-inserting if already present, evicting the oldest at\n * capacity).\n *\n * @internal\n */\nexport class LruSet<T> {\n private readonly _map: LruMap<T, true>;\n\n constructor(maxSize: number) {\n this._map = new LruMap(maxSize);\n }\n\n has(value: T): boolean {\n return this._map.has(value);\n }\n\n add(value: T): void {\n this._map.set(value, true);\n }\n\n delete(value: T): boolean {\n return this._map.delete(value);\n }\n\n clear(): void {\n this._map.clear();\n }\n\n get size(): number {\n return this._map.size;\n }\n}\n","import { LruMap } from \"../internal/lru-map.js\";\nimport type { Cache, CacheEntry, Schema } from \"../types/index.js\";\n\n/**\n * In-memory LRU cache for stream snapshots.\n *\n * Backed by an internal `LruMap` for O(1) get/set with LRU eviction.\n * Configurable `maxSize` bounds memory usage.\n *\n * @example\n * ```typescript\n * import { cache } from \"@rotorsoft/act\";\n * import { InMemoryCache } from \"@rotorsoft/act\";\n *\n * cache(new InMemoryCache({ maxSize: 500 }));\n * ```\n */\n/* eslint-disable @typescript-eslint/require-await -- async interface for Redis-compatibility */\nexport class InMemoryCache implements Cache {\n // CacheEntry<any> lets `get<TState>` and `set<TState>` flow without casts:\n // any is bidirectionally compatible with the per-call TState binding, while\n // the public Cache interface still presents a typed surface to callers.\n private readonly _entries: LruMap<string, CacheEntry<any>>;\n\n constructor(options?: { maxSize?: number }) {\n this._entries = new LruMap(options?.maxSize ?? 1000);\n }\n\n /** @inheritDoc */\n async get<TState extends Schema>(\n stream: string\n ): Promise<CacheEntry<TState> | undefined> {\n return this._entries.get(stream);\n }\n\n /** @inheritDoc */\n async set<TState extends Schema>(\n stream: string,\n entry: CacheEntry<TState>\n ): Promise<void> {\n this._entries.set(stream, entry);\n }\n\n /** @inheritDoc */\n async invalidate(stream: string): Promise<void> {\n this._entries.delete(stream);\n }\n\n /** @inheritDoc */\n async clear(): Promise<void> {\n this._entries.clear();\n }\n\n /** @inheritDoc */\n async dispose(): Promise<void> {\n this._entries.clear();\n }\n\n /** Current number of entries held by the LRU. */\n get size(): number {\n return this._entries.size;\n }\n}\n","/**\n * @packageDocumentation\n * Configuration utilities for Act Framework environment, logging, and package metadata.\n *\n * Provides type-safe configuration loading and validation using Zod schemas.\n *\n * @module config\n */\nimport * as fs from \"node:fs\";\nimport { z } from \"zod\";\nimport { log } from \"./ports.js\";\nimport {\n type Environment,\n Environments,\n type LogLevel,\n LogLevels,\n} from \"./types/index.js\";\nimport { extend } from \"./utils.js\";\n\n/**\n * Zod schema for validating package.json metadata.\n * @internal\n */\nexport const PackageSchema = z.object({\n name: z.string().min(1),\n version: z.string().min(1),\n description: z.string().min(1).optional(),\n author: z\n .object({ name: z.string().min(1), email: z.string().optional() })\n .optional()\n .or(z.string().min(1))\n .optional(),\n license: z.string().min(1).optional(),\n dependencies: z.record(z.string(), z.string()).optional(),\n});\n\n/**\n * Type representing the validated package.json metadata.\n *\n * @internal\n */\nexport type Package = z.infer<typeof PackageSchema>;\n\n/**\n * Fallback package metadata when `package.json` can't be read at module\n * load — happens when the framework is consumed from a CWD that doesn't\n * have one (bundled CLIs, Lambda layers, embedded scripts) or when the\n * file exists but is malformed.\n *\n * The values are deliberately synthetic so callers spot them immediately:\n * `config().name === \"act-fallback\"` is a runtime signal that the framework\n * couldn't load the host project's package.json.\n *\n * @internal\n */\nconst FALLBACK_PACKAGE: Package = {\n name: \"act-fallback\",\n version: \"0.0.0-fallback\",\n description: \"Synthetic fallback — package.json could not be loaded\",\n};\n\n/**\n * Loads and parses the local package.json file as a Package object. On\n * any read or parse failure, falls back to {@link FALLBACK_PACKAGE} and\n * stashes the error so {@link config} can surface it on first access —\n * we can't call `log()` here because the logger port memoizes on first\n * call and locking it at module load defeats user injection.\n *\n * @internal\n */\nconst getPackage = (): Package => {\n try {\n const raw = fs.readFileSync(\"package.json\");\n return JSON.parse(raw.toString()) as Package;\n } catch (err) {\n pkgLoadError = err;\n return FALLBACK_PACKAGE;\n }\n};\n\n/** Stashed read/parse error from {@link getPackage}, surfaced by config(). */\nlet pkgLoadError: unknown;\n\n/**\n * Zod schema for the full Act Framework configuration object.\n * Includes package metadata, environment, logging, and timing options.\n * @internal\n */\nconst BaseSchema = PackageSchema.extend({\n env: z.enum(Environments),\n logLevel: z.enum(LogLevels),\n logSingleLine: z.boolean(),\n sleepMs: z.number().int().min(0).max(5000),\n});\n\n/**\n * Type representing the validated Act Framework configuration object.\n */\nexport type Config = z.infer<typeof BaseSchema>;\n\nconst { NODE_ENV, LOG_LEVEL, LOG_SINGLE_LINE, SLEEP_MS } = process.env;\n\nconst env = (NODE_ENV || \"development\") as Environment;\nconst logLevel = (LOG_LEVEL ||\n (NODE_ENV === \"test\"\n ? \"fatal\"\n : NODE_ENV === \"production\"\n ? \"info\"\n : \"trace\")) as LogLevel;\nconst logSingleLine = (LOG_SINGLE_LINE || \"true\") === \"true\";\nconst sleepMs = parseInt(NODE_ENV === \"test\" ? \"0\" : (SLEEP_MS ?? \"100\"), 10);\n\nconst pkg = getPackage();\n\n// Lazily validated on first call. Cannot run extend() at module load\n// because of a utils.ts <-> config.ts cycle (utils imports config for\n// sleep()'s default). Inputs are frozen after import, so the cached\n// result is stable for the life of the process.\nlet _validated: Config | undefined;\n\n/**\n * Gets the current Act Framework configuration.\n *\n * Configuration is loaded from package.json and environment variables, providing\n * type-safe access to application metadata and runtime settings.\n *\n * **Environment Variables:**\n * - `NODE_ENV`: \"development\" | \"test\" | \"staging\" | \"production\" (default: \"development\")\n * - `LOG_LEVEL`: \"fatal\" | \"error\" | \"warn\" | \"info\" | \"debug\" | \"trace\"\n * - `LOG_SINGLE_LINE`: \"true\" | \"false\" (default: \"true\")\n * - `SLEEP_MS`: Milliseconds for sleep utility (default: 100, 0 for tests)\n *\n * **Defaults by environment:**\n * - test: logLevel=\"error\", sleepMs=0\n * - production: logLevel=\"info\"\n * - development: logLevel=\"trace\"\n *\n * @returns The validated configuration object\n *\n * @example Basic usage\n * ```typescript\n * import { config } from \"@rotorsoft/act\";\n *\n * const cfg = config();\n * console.log(`App: ${cfg.name} v${cfg.version}`);\n * console.log(`Environment: ${cfg.env}`);\n * console.log(`Log level: ${cfg.logLevel}`);\n * ```\n *\n * @example Environment-specific behavior\n * ```typescript\n * import { config } from \"@rotorsoft/act\";\n *\n * const cfg = config();\n *\n * if (cfg.env === \"production\") {\n * // Use PostgreSQL in production\n * store(new PostgresStore(prodConfig));\n * } else {\n * // Use in-memory store for dev/test\n * store(new InMemoryStore());\n * }\n * ```\n *\n * @example Adjusting log levels\n * ```typescript\n * // Set via environment variable:\n * // LOG_LEVEL=debug npm start\n *\n * // Or check in code:\n * const cfg = config();\n * if (cfg.logLevel === \"trace\") {\n * logger.trace(\"Detailed debugging enabled\");\n * }\n * ```\n *\n * @see {@link Config} for configuration type\n */\nexport const config = (): Config => {\n if (!_validated) {\n _validated = extend(\n { ...pkg, env, logLevel, logSingleLine, sleepMs },\n BaseSchema\n );\n if (pkgLoadError) {\n // Surface the fallback once, after _validated is set so the\n // recursive log() → config() call short-circuits. log() resolves\n // through the port singleton — respects user injection and level.\n const msg =\n pkgLoadError instanceof Error\n ? pkgLoadError.message\n : typeof pkgLoadError === \"string\"\n ? pkgLoadError\n : \"unknown error\";\n log().warn(\n `[act] Could not read package.json (${msg}); using synthetic ` +\n `name=\"${FALLBACK_PACKAGE.name}\" version=\"${FALLBACK_PACKAGE.version}\".`\n );\n pkgLoadError = undefined;\n }\n }\n return _validated;\n};\n","import { AsyncLocalStorage } from \"node:async_hooks\";\nimport { ConsoleLogger } from \"./adapters/console-logger.js\";\nimport { InMemoryCache } from \"./adapters/in-memory-cache.js\";\nimport { InMemoryStore } from \"./adapters/in-memory-store.js\";\nimport { config } from \"./config.js\";\nimport type {\n Cache,\n Disposable,\n Disposer,\n Logger,\n Store,\n} from \"./types/index.js\";\n\n/** Per-Act ports bag (ACT-501). Both required together — a shared cache across stores would collide on stream keys. */\nexport type Scoped = {\n readonly store: Store;\n readonly cache: Cache;\n};\n\n/** AsyncLocalStorage carrying the active Act's ports. Internal — not re-exported. */\nexport const scoped = new AsyncLocalStorage<Scoped>();\n\n/**\n * Port/adapter infrastructure for the Act framework.\n *\n * All infrastructure concerns (logging, storage, caching) are managed as\n * singleton adapters injected via port functions. Each port follows the same\n * pattern: first call wins with a sensible default, optional adapter injection.\n *\n * - `log()` — structured logging (default: ConsoleLogger)\n * - `store()` — event persistence (default: InMemoryStore)\n * - `cache()` — state checkpoints (default: InMemoryCache)\n * - `dispose()` — register cleanup functions for graceful shutdown\n *\n * @module ports\n */\n\n/**\n * List of exit codes for process termination. Consumed by signal handlers\n * and {@link disposeAndExit}; not part of the user-facing surface.\n *\n * @internal\n */\nexport const ExitCodes = [\"ERROR\", \"EXIT\"] as const;\n\n/**\n * Type for allowed exit codes.\n *\n * - `\"ERROR\"` — abnormal termination (uncaught exception, unhandled rejection)\n * - `\"EXIT\"` — clean shutdown (SIGINT, SIGTERM, or manual trigger)\n *\n * @internal\n */\nexport type ExitCode = (typeof ExitCodes)[number];\n\n// ---------------------------------------------------------------------------\n// Port factory\n// ---------------------------------------------------------------------------\n\n/**\n * Factory function that creates or returns the injected adapter.\n * @internal\n */\ntype Injector<Port extends Disposable> = (adapter?: Port) => Port;\n\n/** Singleton adapter registry, keyed by injector function name. */\nconst adapters = new Map<string, Disposable>();\n\n/**\n * Creates a singleton port with optional adapter injection.\n *\n * The first call initializes the adapter (using the provided adapter or the\n * injector's default). Subsequent calls return the cached singleton. Adapters\n * are disposed in reverse registration order during {@link disposeAndExit}.\n *\n * @param injector - Named function that creates the default adapter\n * @returns Port function: call with no args to get the singleton, or pass an\n * adapter on the first call to override the default\n *\n * @example\n * ```typescript\n * const store = port(function store(adapter?: Store) {\n * return adapter || new InMemoryStore();\n * });\n * const s = store(); // InMemoryStore\n * ```\n */\nexport function port<Port extends Disposable>(injector: Injector<Port>) {\n return (adapter?: Port): Port => {\n if (!adapters.has(injector.name)) {\n const injected = injector(adapter);\n adapters.set(injector.name, injected);\n // log() is now in adapters (or this IS the log port we just registered),\n // so the recursive call resolves immediately. Routing through the logger\n // means level gating (e.g. silenced in tests at fatal) takes effect.\n log().info(`[act] + ${injector.name}:${injected.constructor.name}`);\n }\n return adapters.get(injector.name) as Port;\n };\n}\n\n// ---------------------------------------------------------------------------\n// Ports: log, store, cache\n// ---------------------------------------------------------------------------\n\n/**\n * Gets or injects the singleton logger.\n *\n * By default, Act uses a built-in {@link ConsoleLogger} that emits JSON lines\n * in production (compatible with GCP, AWS CloudWatch, Datadog) and colorized\n * output in development — zero external dependencies.\n *\n * For pino, inject a `PinoLogger` from `@rotorsoft/act-pino` before building\n * your application.\n *\n * @param adapter - Optional logger implementation to inject\n * @returns The singleton logger instance\n *\n * @example Default console logger\n * ```typescript\n * import { log } from \"@rotorsoft/act\";\n * const logger = log();\n * logger.info(\"Application started\");\n * ```\n *\n * @example Injecting pino\n * ```typescript\n * import { log } from \"@rotorsoft/act\";\n * import { PinoLogger } from \"@rotorsoft/act-pino\";\n * log(new PinoLogger({ level: \"debug\", pretty: true }));\n * ```\n *\n * @see {@link Logger} for the interface contract\n * @see {@link ConsoleLogger} for the default implementation\n */\nexport const log = port(function log(adapter?: Logger) {\n const cfg = config();\n return (\n adapter ||\n new ConsoleLogger({\n level: cfg.logLevel,\n pretty: cfg.env !== \"production\",\n })\n );\n});\n\n/**\n * Gets or injects the singleton event store.\n *\n * By default, Act uses an {@link InMemoryStore} suitable for development and\n * testing. For production, inject a persistent store like `PostgresStore` from\n * `@rotorsoft/act-pg` before building your application.\n *\n * **Important:** Store injection must happen before creating any Act instances.\n * Once set, the store cannot be changed without restarting the application.\n *\n * @param adapter - Optional store implementation to inject\n * @returns The singleton store instance\n *\n * @example Default in-memory store\n * ```typescript\n * import { store } from \"@rotorsoft/act\";\n * const s = store();\n * ```\n *\n * @example Injecting PostgreSQL\n * ```typescript\n * import { store } from \"@rotorsoft/act\";\n * import { PostgresStore } from \"@rotorsoft/act-pg\";\n *\n * store(new PostgresStore({\n * host: \"localhost\",\n * port: 5432,\n * database: \"myapp\",\n * user: \"postgres\",\n * password: \"secret\",\n * }));\n * ```\n *\n * @see {@link Store} for the interface contract\n * @see {@link InMemoryStore} for the default implementation\n */\n// ALS check lives outside `port()` — its cache fires only once, so the\n// per-call branch on a scoped Act has to be in the public wrapper.\nconst _store = port(function store(adapter?: Store): Store {\n return adapter ?? new InMemoryStore();\n});\n\nexport const store = ((adapter?: Store): Store => {\n return scoped.getStore()?.store ?? _store(adapter);\n}) as (adapter?: Store) => Store;\n\n/**\n * Gets or injects the singleton cache.\n *\n * By default, Act uses an {@link InMemoryCache} (LRU, maxSize 1000). For\n * distributed deployments, inject a Redis-backed cache before building your\n * application.\n *\n * @param adapter - Optional cache implementation to inject\n * @returns The singleton cache instance\n *\n * @see {@link Cache} for the interface contract\n * @see {@link InMemoryCache} for the default implementation\n */\nconst _cache = port(function cache(adapter?: Cache) {\n return adapter ?? new InMemoryCache();\n});\n\nexport const cache = ((adapter?: Cache): Cache => {\n return scoped.getStore()?.cache ?? _cache(adapter);\n}) as (adapter?: Cache) => Cache;\n\n// ---------------------------------------------------------------------------\n// Disposal\n// ---------------------------------------------------------------------------\n\n/** Registered cleanup functions, executed in reverse order during shutdown. */\nconst disposers: Disposer[] = [];\n\n/**\n * Disposes all registered adapters and disposers, then exits the process.\n *\n * Execution order:\n * 1. Custom disposers (registered via {@link dispose}) — in reverse order\n * 2. Port adapters (log, store, cache) — in reverse registration order\n * 3. Adapter registry is cleared\n * 4. Process exits (skipped in test environment)\n *\n * In production, `\"ERROR\"` exits are silently ignored to avoid crashing on\n * transient failures (e.g. an uncaught promise in a non-critical path).\n *\n * @param code - Exit code: `\"EXIT\"` for clean shutdown (exit 0),\n * `\"ERROR\"` for abnormal termination (exit 1)\n */\nexport async function disposeAndExit(code: ExitCode = \"EXIT\"): Promise<void> {\n if (code === \"ERROR\" && config().env === \"production\") {\n // Surface the swallow so incident triage can see it. Without this\n // log the framework looks unresponsive after an uncaught exception\n // in prod — exactly when operators most need a breadcrumb.\n log().warn(\n \"disposeAndExit('ERROR') ignored in production — process kept alive\"\n );\n return;\n }\n\n // Run sequentially in reverse registration order so a disposer can rely on\n // later-registered disposers (and adapters on later-registered adapters)\n // having already finished — Promise.all would race them.\n for (const disposer of [...disposers].reverse()) {\n await disposer();\n }\n for (const adapter of [...adapters.values()].reverse()) {\n await adapter.dispose();\n log().info(`[act] - ${adapter.constructor.name}`);\n }\n adapters.clear();\n config().env !== \"test\" && process.exit(code === \"ERROR\" ? 1 : 0);\n}\n\n/**\n * Registers a cleanup function for graceful shutdown.\n *\n * Disposers are called automatically on SIGINT, SIGTERM, uncaught exceptions,\n * and unhandled rejections. They execute in reverse registration order before\n * port adapters are disposed.\n *\n * @param disposer - Async function to call during cleanup. Omit to get a\n * reference to {@link disposeAndExit} without registering.\n * @returns Function to manually trigger disposal and exit\n *\n * @example\n * ```typescript\n * import { dispose } from \"@rotorsoft/act\";\n *\n * const db = connectDatabase();\n * dispose(async () => await db.close());\n *\n * // In tests\n * afterAll(async () => await dispose()());\n * ```\n *\n * @see {@link disposeAndExit} for the full shutdown sequence\n */\nexport function dispose(\n disposer?: Disposer\n): (code?: ExitCode) => Promise<void> {\n disposer && disposers.push(disposer);\n return disposeAndExit;\n}\n\n// ---------------------------------------------------------------------------\n// Constants\n// ---------------------------------------------------------------------------\n\n/**\n * Event name used internally for snapshot events in the event store.\n * Snapshot events store a full state checkpoint, enabling efficient cold-start\n * recovery without replaying the entire event stream.\n */\nexport const SNAP_EVENT = \"__snapshot__\";\n\n/**\n * Event name used internally for tombstone events in the event store.\n * A tombstone marks a stream as permanently closed — no further writes\n * are permitted until the stream is explicitly restarted via `close()`.\n *\n * @see {@link Act.close} for the close-the-books API\n */\nexport const TOMBSTONE_EVENT = \"__tombstone__\";\n","import { prettifyError, ZodError, type ZodType } from \"zod\";\nimport { config } from \"./config.js\";\nimport { ValidationError } from \"./types/index.js\";\n\n/**\n * @module utils\n * @category Utilities\n *\n * Small utilities used across the framework:\n * - {@link validate} — parse a payload against a Zod schema, throwing\n * {@link ValidationError} on failure.\n * - {@link extend} — validate a source object and merge into defaults.\n * - {@link sleep} — async delay (default duration from `config().sleepMs`).\n */\n\n/**\n * Parse `payload` against `schema`, returning the validated value or throwing\n * a {@link ValidationError} with prettified Zod details. When `schema` is\n * omitted, returns `payload` unchanged. The framework calls this for every\n * `app.do()` action, every emitted event, and every state init.\n *\n * @example\n * ```typescript\n * const UserSchema = z.object({ email: z.string().email() });\n * const user = validate(\"User\", { email: \"alice@example.com\" }, UserSchema);\n * ```\n *\n * @see {@link ValidationError}\n */\nexport const validate = <S>(\n target: string,\n payload: Readonly<S>,\n schema?: ZodType<S>\n): Readonly<S> => {\n try {\n return schema ? schema.parse(payload) : payload;\n } catch (error) {\n if (error instanceof ZodError) {\n throw new ValidationError(target, payload, prettifyError(error));\n }\n throw new ValidationError(target, payload, error);\n }\n};\n\n/**\n * Validate `source` against `schema` and return a new object that merges\n * `source` over the optional `target` defaults. Used by {@link config} for\n * env-var-overrides-defaults patterns; safe to call elsewhere — it never\n * mutates `target`.\n *\n * @example\n * ```typescript\n * const schema = z.object({ host: z.string(), port: z.number() });\n * const cfg = extend({ port: 8080 }, schema, { host: \"localhost\", port: 80 });\n * // → { host: \"localhost\", port: 8080 }\n * ```\n *\n * @throws {@link ValidationError} if `source` fails the schema.\n */\nexport const extend = <\n S extends Record<string, unknown>,\n T extends Record<string, unknown>,\n>(\n source: Readonly<S>,\n schema: ZodType<S>,\n target?: Readonly<T>\n): Readonly<S & T> => {\n const value = validate(\"config\", source, schema);\n return { ...target, ...value } as Readonly<S & T>;\n};\n\n/**\n * Pause for `ms` milliseconds (or `config().sleepMs` when omitted — `100ms`\n * in dev, `0ms` in tests). Used by adapters to simulate async I/O.\n *\n * @example\n * ```typescript\n * await sleep(); // default delay from config\n * await sleep(500); // explicit 500ms\n * ```\n */\nexport async function sleep(ms?: number) {\n return new Promise((resolve) => setTimeout(resolve, ms ?? config().sleepMs));\n}\n","/**\n * @packageDocumentation\n * @module act/adapters\n * In-memory event store adapter for the Act Framework.\n *\n * This adapter implements the Store interface and is suitable for development, testing, and demonstration purposes.\n * All data is stored in memory and lost on process exit.\n *\n * @category Adapters\n */\nimport { SNAP_EVENT, TOMBSTONE_EVENT } from \"../ports.js\";\nimport { ConcurrencyError } from \"../types/errors.js\";\nimport type {\n BlockedLease,\n Committed,\n EventMeta,\n Lease,\n Message,\n PrioritizeFilter,\n Query,\n QueryStreams,\n QueryStreamsResult,\n Schema,\n Schemas,\n Store,\n StreamPosition,\n} from \"../types/index.js\";\nimport { sleep } from \"../utils.js\";\n\n/**\n * @internal\n * Represents an in-memory stream for event processing and leasing.\n */\nclass InMemoryStream {\n private _at = -1;\n private _retry = -1;\n private _blocked = false;\n private _error = \"\";\n private _leased_by: string | undefined = undefined;\n private _leased_until: Date | undefined = undefined;\n private _priority = 0;\n\n constructor(\n readonly stream: string,\n readonly source: string | undefined,\n priority = 0\n ) {\n this._priority = priority;\n }\n\n get priority() {\n return this._priority;\n }\n\n /**\n * Bump the priority via {@link subscribe}: keeps the maximum across\n * reactions so the highest-priority registrant wins.\n */\n bumpPriority(priority: number) {\n if (priority > this._priority) this._priority = priority;\n }\n\n /**\n * Set the priority outright via {@link prioritize}: operator\n * runtime override that ignores the build-time `max()` invariant.\n */\n setPriority(priority: number) {\n this._priority = priority;\n }\n\n get is_available() {\n return (\n !this._blocked &&\n (!this._leased_until || this._leased_until <= new Date())\n );\n }\n\n get at() {\n return this._at;\n }\n\n get retry() {\n return this._retry;\n }\n\n get blocked() {\n return this._blocked;\n }\n\n get error() {\n return this._error;\n }\n\n get leased_by() {\n return this._leased_by;\n }\n\n get leased_until() {\n return this._leased_until;\n }\n\n /**\n * Attempt to lease this stream for processing.\n * @param lease - The lease request.\n * @param millis - Lease duration in milliseconds.\n * @returns The granted lease or undefined if blocked.\n */\n lease(lease: Lease, millis: number): Lease {\n if (millis > 0) {\n this._leased_by = lease.by;\n this._leased_until = new Date(Date.now() + millis);\n }\n this._retry = this._retry + 1;\n return {\n stream: this.stream,\n source: this.source,\n at: lease.at,\n by: lease.by,\n retry: this._retry,\n lagging: lease.lagging,\n };\n }\n\n /**\n * Acknowledge completion of processing for this stream.\n * @param lease - The lease request.\n */\n ack(lease: Lease) {\n if (this._leased_by === lease.by) {\n this._leased_by = undefined;\n this._leased_until = undefined;\n this._at = lease.at;\n this._retry = -1;\n return {\n stream: this.stream,\n source: this.source,\n at: this._at,\n by: lease.by,\n retry: this._retry,\n lagging: lease.lagging,\n };\n }\n }\n\n /**\n * Block a stream for processing after failing to process and reaching max retries with blocking enabled.\n * @param lease - The lease request.\n * @param error Blocked error message.\n */\n block(lease: Lease, error: string) {\n if (this._leased_by === lease.by) {\n this._blocked = true;\n this._error = error;\n return {\n stream: this.stream,\n source: this.source,\n at: this._at,\n by: this._leased_by,\n retry: this._retry,\n error: this._error,\n lagging: lease.lagging,\n };\n }\n }\n\n /**\n * Reset this stream's watermark and state for replay. The retry counter\n * resets to -1 to match the constructor + ack() invariant (\"released\n * stream\"); the next claim() bumps it to 0 (first attempt).\n */\n reset() {\n this._at = -1;\n this._retry = -1;\n this._blocked = false;\n this._error = \"\";\n this._leased_by = undefined;\n this._leased_until = undefined;\n }\n}\n\n/**\n * In-memory event store implementation.\n *\n * This is the default store used by Act when no other store is injected.\n * It stores all events in memory and is suitable for:\n * - Development and prototyping\n * - Unit and integration testing\n * - Demonstrations and examples\n *\n * **Not suitable for production** - all data is lost when the process exits.\n * Use {@link PostgresStore} for production deployments.\n *\n * The in-memory store provides:\n * - Full {@link Store} interface implementation\n * - Optimistic concurrency control\n * - Stream leasing for distributed processing simulation\n * - Snapshot support\n * - Fast performance (no I/O overhead)\n *\n * **`Store.notify` is intentionally not implemented.** The notify hook is a\n * cross-process wake-up signal — local commits already arm the drain via\n * `do()`. An in-memory store is single-process by definition, so there is\n * no remote writer to be notified of. The {@link Act} orchestrator\n * detects the absence and falls back to the existing debounce/poll path.\n *\n * @example Using in tests\n * ```typescript\n * import { store } from \"@rotorsoft/act\";\n *\n * describe(\"Counter\", () => {\n * beforeEach(async () => {\n * // Reset store between tests\n * await store().seed();\n * });\n *\n * it(\"increments\", async () => {\n * await app.do(\"increment\", target, { by: 5 });\n * const snapshot = await app.load(Counter, \"counter-1\");\n * expect(snapshot.state.count).toBe(5);\n * });\n * });\n * ```\n *\n * @example Explicit instantiation\n * ```typescript\n * import { InMemoryStore } from \"@rotorsoft/act\";\n *\n * const testStore = new InMemoryStore();\n * await testStore.seed();\n *\n * // Use for specific test scenarios\n * await testStore.commit(\"test-stream\", events, meta);\n * ```\n *\n * @example Querying events\n * ```typescript\n * const events: any[] = [];\n * await store().query(\n * (event) => events.push(event),\n * { stream: \"test-stream\" }\n * );\n * console.log(`Found ${events.length} events`);\n * ```\n *\n * @see {@link Store} for the interface definition\n * @see {@link PostgresStore} for production use\n * @see {@link store} for injecting stores\n *\n * @category Adapters\n */\nexport class InMemoryStore implements Store {\n // stored events\n private _events: Committed<Schemas, keyof Schemas>[] = [];\n // stored stream positions and other metadata\n private _streams: Map<string, InMemoryStream> = new Map();\n // last committed version per stream — O(1) replacement for filter-on-commit\n private _streamVersions: Map<string, number> = new Map();\n // max non-snapshot event id per stream — drives the source-pattern probe in claim()\n // without scanning the full event log.\n private _maxEventIdByStream: Map<string, number> = new Map();\n // global max non-snapshot event id — fast pre-check for source-less streams in claim()\n private _maxNonSnapEventId = -1;\n\n private _resetIndexes() {\n this._events.length = 0;\n this._streamVersions.clear();\n this._maxEventIdByStream.clear();\n this._maxNonSnapEventId = -1;\n }\n\n /**\n * Dispose of the store and clear all events.\n * @returns Promise that resolves when disposal is complete.\n */\n async dispose() {\n await sleep();\n this._resetIndexes();\n }\n\n /**\n * Seed the store with initial data (no-op for in-memory).\n * @returns Promise that resolves when seeding is complete.\n */\n async seed() {\n await sleep();\n }\n\n /**\n * Drop all data from the store.\n * @returns Promise that resolves when the store is cleared.\n */\n async drop() {\n await sleep();\n this._resetIndexes();\n this._streams = new Map();\n }\n\n private in_query<E extends Schemas>(query: Query, e: Committed<E, keyof E>) {\n if (query.stream) {\n if (query.stream_exact) {\n if (e.stream !== query.stream) return false;\n } else if (!RegExp(query.stream).test(e.stream)) return false;\n }\n if (query.names && !query.names.includes(e.name as string)) return false;\n if (query.correlation && e.meta?.correlation !== query.correlation)\n return false;\n if (e.name === SNAP_EVENT && !query.with_snaps) return false;\n return true;\n }\n\n /**\n * Query events in the store, optionally filtered by query options.\n * @param callback - Function to call for each event.\n * @param query - Optional query options.\n * @returns The number of events processed.\n */\n async query<E extends Schemas>(\n callback: (event: Committed<E, keyof E>) => void,\n query?: Query\n ) {\n await sleep();\n let count = 0;\n if (query?.backward) {\n let i = (query?.before || this._events.length) - 1;\n while (i >= 0) {\n const e = this._events[i--];\n if (query && !this.in_query(query, e)) continue;\n if (query?.created_before && e.created >= query.created_before)\n continue;\n if (query.after && e.id <= query.after) break;\n if (query.created_after && e.created <= query.created_after) break;\n callback(e as Committed<E, keyof E>);\n count++;\n if (query?.limit && count >= query.limit) break;\n }\n } else {\n let i = (query?.after ?? -1) + 1;\n while (i < this._events.length) {\n const e = this._events[i++];\n if (query && !this.in_query(query, e)) continue;\n if (query?.created_after && e.created <= query.created_after) continue;\n if (query?.before && e.id >= query.before) break;\n if (query?.created_before && e.created >= query.created_before) break;\n callback(e as Committed<E, keyof E>);\n count++;\n if (query?.limit && count >= query.limit) break;\n }\n }\n return count;\n }\n\n /**\n * Commit one or more events to a stream.\n * @param stream - The stream name.\n * @param msgs - The events/messages to commit.\n * @param meta - Event metadata.\n * @param expectedVersion - Optional optimistic concurrency check.\n * @returns The committed events with metadata.\n * @throws ConcurrencyError if expectedVersion does not match.\n */\n async commit<E extends Schemas>(\n stream: string,\n msgs: Message<E, keyof E>[],\n meta: EventMeta,\n expectedVersion?: number\n ) {\n await sleep();\n const currentVersion = this._streamVersions.get(stream) ?? -1;\n if (\n typeof expectedVersion === \"number\" &&\n currentVersion !== expectedVersion\n ) {\n throw new ConcurrencyError(\n stream,\n currentVersion,\n msgs as Message<Schemas, keyof Schemas>[],\n expectedVersion\n );\n }\n\n let version = currentVersion + 1;\n let lastNonSnapId = -1;\n const committed = msgs.map(({ name, data }) => {\n const c: Committed<E, keyof E> = {\n id: this._events.length,\n stream,\n version,\n created: new Date(),\n name,\n data,\n meta,\n };\n this._events.push(c as Committed<Schemas, keyof Schemas>);\n if (name !== SNAP_EVENT) lastNonSnapId = c.id;\n version++;\n return c;\n });\n this._streamVersions.set(stream, version - 1);\n if (lastNonSnapId >= 0) {\n this._maxEventIdByStream.set(stream, lastNonSnapId);\n // commit always assigns a fresh id from this._events.length, so any\n // non-snap commit strictly raises the global max.\n this._maxNonSnapEventId = lastNonSnapId;\n }\n return committed;\n }\n\n /**\n * Atomically discovers and leases streams for processing.\n * Fuses poll + lease into a single operation.\n * @param lagging - Max streams from lagging frontier.\n * @param leading - Max streams from leading frontier.\n * @param by - Lease holder identifier.\n * @param millis - Lease duration in milliseconds.\n * @returns Granted leases.\n */\n async claim(lagging: number, leading: number, by: string, millis: number) {\n await sleep();\n // Cache compiled regexes — multiple subscribed streams typically share the\n // same source pattern, and the inner loop can run thousands of times per claim.\n const sourceRegex = new Map<string, RegExp>();\n const getRegex = (source: string) => {\n let re = sourceRegex.get(source);\n if (!re) {\n re = new RegExp(source);\n sourceRegex.set(source, re);\n }\n return re;\n };\n const hasWork = (s: InMemoryStream): boolean => {\n if (s.at < 0) return true;\n if (!s.source) return s.at < this._maxNonSnapEventId;\n const re = getRegex(s.source);\n for (const [streamName, maxId] of this._maxEventIdByStream) {\n if (maxId > s.at && re.test(streamName)) return true;\n }\n return false;\n };\n const available = [...this._streams.values()].filter(\n (s) => s.is_available && hasWork(s)\n );\n // Lagging frontier orders by priority DESC (higher first), then by\n // watermark ASC (most-behind first). Mirrors the PG `claim()` SQL\n // — see `libs/act-pg/PERFORMANCE.md` for the benchmark that\n // motivated the priority dimension.\n const lag = available\n .sort((a, b) => b.priority - a.priority || a.at - b.at)\n .slice(0, lagging)\n .map((s) => ({\n stream: s.stream,\n source: s.source,\n at: s.at,\n lagging: true,\n }));\n const lead = available\n .sort((a, b) => b.at - a.at)\n .slice(0, leading)\n .map((s) => ({\n stream: s.stream,\n source: s.source,\n at: s.at,\n lagging: false,\n }));\n // deduplicate (a stream can appear in both frontiers)\n const seen = new Set<string>();\n const combined = [...lag, ...lead].filter((p) => {\n if (seen.has(p.stream)) return false;\n seen.add(p.stream);\n return true;\n });\n // lease each atomically\n return combined\n .map((p) =>\n this._streams.get(p.stream)?.lease({ ...p, by, retry: 0 }, millis)\n )\n .filter((l) => !!l);\n }\n\n /**\n * Registers streams for event processing. When the same stream is\n * resubscribed with a different priority, the **maximum** wins — so\n * the highest-priority registered reaction sets the scheduling lane.\n * Use {@link prioritize} for operator runtime overrides.\n *\n * @param streams - Streams to register with optional source + priority.\n * @returns subscribed count and current max watermark.\n */\n async subscribe(\n streams: Array<{ stream: string; source?: string; priority?: number }>\n ) {\n await sleep();\n let subscribed = 0;\n for (const { stream, source, priority = 0 } of streams) {\n const existing = this._streams.get(stream);\n if (existing) {\n existing.bumpPriority(priority);\n } else {\n this._streams.set(stream, new InMemoryStream(stream, source, priority));\n subscribed++;\n }\n }\n let watermark = -1;\n for (const s of this._streams.values()) {\n if (s.at > watermark) watermark = s.at;\n }\n return { subscribed, watermark };\n }\n\n /**\n * Acknowledge completion of processing for leased streams.\n * @param leases - Leases to acknowledge, including last processed watermark and lease holder.\n */\n async ack(leases: Lease[]) {\n await sleep();\n return leases\n .map((l) => this._streams.get(l.stream)?.ack(l))\n .filter((l) => !!l);\n }\n\n /**\n * Block a stream for processing after failing to process and reaching max retries with blocking enabled.\n * @param leases - Leases to block, including lease holder and last error message.\n * @returns Blocked leases.\n */\n async block(leases: BlockedLease[]) {\n await sleep();\n return leases\n .map((l) => this._streams.get(l.stream)?.block(l, l.error))\n .filter((l) => !!l);\n }\n\n /**\n * Reset watermarks for the given streams to -1, clearing retry, blocked,\n * error, and lease state so they can be replayed from the beginning.\n * @param streams - Stream names to reset.\n * @returns Count of streams that were actually reset.\n */\n async reset(streams: string[]) {\n await sleep();\n let count = 0;\n for (const name of streams) {\n const s = this._streams.get(name);\n if (s) {\n s.reset();\n count++;\n }\n }\n return count;\n }\n\n /**\n * Bulk-update priority of streams matching `filter`. Mirrors\n * {@link query_streams}'s filter semantics — see {@link Store.prioritize}.\n * Unlike {@link subscribe} (which keeps `max()` of registered\n * priorities), this sets the priority outright — operator override\n * for the build-time scheduling policy.\n *\n * @returns Count of streams whose priority changed.\n */\n async prioritize(filter: PrioritizeFilter, priority: number) {\n await sleep();\n const streamRe =\n filter.stream && !filter.stream_exact\n ? new RegExp(filter.stream)\n : undefined;\n const sourceRe =\n filter.source && !filter.source_exact\n ? new RegExp(filter.source)\n : undefined;\n let count = 0;\n for (const s of this._streams.values()) {\n if (filter.stream !== undefined) {\n if (\n filter.stream_exact\n ? s.stream !== filter.stream\n : !streamRe!.test(s.stream)\n )\n continue;\n }\n if (filter.source !== undefined) {\n if (s.source === undefined) continue;\n if (\n filter.source_exact\n ? s.source !== filter.source\n : !sourceRe!.test(s.source)\n )\n continue;\n }\n if (filter.blocked !== undefined && s.blocked !== filter.blocked)\n continue;\n if (s.priority !== priority) {\n s.setPriority(priority);\n count++;\n }\n }\n return count;\n }\n\n /**\n * Streams registered subscription positions to the callback, ordered by\n * stream name. Returns the highest event id in the store and the count\n * of positions emitted.\n */\n async query_streams(\n callback: (position: StreamPosition) => void,\n query?: QueryStreams\n ): Promise<QueryStreamsResult> {\n await sleep();\n const limit = query?.limit ?? 100;\n const after = query?.after;\n const blocked = query?.blocked;\n const streamRe =\n query?.stream && !query.stream_exact\n ? new RegExp(query.stream)\n : undefined;\n const sourceRe =\n query?.source && !query.source_exact\n ? new RegExp(query.source)\n : undefined;\n\n const sorted = [...this._streams.values()].sort((a, b) =>\n a.stream.localeCompare(b.stream)\n );\n\n let count = 0;\n for (const s of sorted) {\n if (after !== undefined && s.stream <= after) continue;\n if (query?.stream !== undefined) {\n if (\n query.stream_exact\n ? s.stream !== query.stream\n : !streamRe!.test(s.stream)\n )\n continue;\n }\n if (query?.source !== undefined) {\n if (s.source === undefined) continue;\n if (\n query.source_exact\n ? s.source !== query.source\n : !sourceRe!.test(s.source)\n )\n continue;\n }\n if (blocked !== undefined && s.blocked !== blocked) continue;\n callback({\n stream: s.stream,\n source: s.source,\n at: s.at,\n retry: s.retry,\n blocked: s.blocked,\n error: s.error,\n priority: s.priority,\n leased_by: s.leased_by,\n leased_until: s.leased_until,\n });\n count++;\n if (count >= limit) break;\n }\n return { maxEventId: this._events.length - 1, count };\n }\n\n /**\n * Atomically truncates streams and seeds each with a snapshot or tombstone.\n * @param targets - Streams to truncate with optional snapshot state and meta.\n * @returns Map keyed by stream name, each entry with `deleted` count and `committed` event.\n */\n async truncate(\n targets: Array<{\n stream: string;\n snapshot?: Schema;\n meta?: EventMeta;\n }>\n ) {\n await sleep();\n // Count per-stream deletions\n const deletedCounts = new Map<string, number>();\n const streamSet = new Set(targets.map((t) => t.stream));\n for (const e of this._events) {\n if (streamSet.has(e.stream)) {\n deletedCounts.set(e.stream, (deletedCounts.get(e.stream) ?? 0) + 1);\n }\n }\n this._events = this._events.filter((e) => !streamSet.has(e.stream));\n for (const stream of streamSet) {\n this._streams.delete(stream);\n this._streamVersions.delete(stream);\n this._maxEventIdByStream.delete(stream);\n }\n const result = new Map<\n string,\n { deleted: number; committed: Committed<Schemas, keyof Schemas> }\n >();\n for (const { stream, snapshot, meta } of targets) {\n const event: Committed<Schemas, keyof Schemas> = {\n id: this._events.length,\n stream,\n version: 0,\n created: new Date(),\n name: snapshot !== undefined ? SNAP_EVENT : TOMBSTONE_EVENT,\n data: snapshot ?? {},\n meta: meta ?? { correlation: \"\", causation: {} },\n };\n this._events.push(event);\n this._streamVersions.set(stream, 0);\n if (event.name !== SNAP_EVENT) {\n this._maxEventIdByStream.set(stream, event.id);\n }\n result.set(stream, {\n deleted: deletedCounts.get(stream) ?? 0,\n committed: event,\n });\n }\n // Recompute global max from the per-stream index — deletions may have\n // dropped the previous max, while new tombstones may have raised it.\n let max = -1;\n for (const id of this._maxEventIdByStream.values()) if (id > max) max = id;\n this._maxNonSnapEventId = max;\n return result;\n }\n}\n"],"mappings":";;;;;;;;AAYA,IAAM,eAAuC;AAAA,EAC3C,OAAO;AAAA,EACP,OAAO;AAAA,EACP,MAAM;AAAA,EACN,MAAM;AAAA,EACN,OAAO;AAAA,EACP,OAAO;AACT;AAEA,IAAM,eAAuC;AAAA,EAC3C,OAAO;AAAA;AAAA,EACP,OAAO;AAAA;AAAA,EACP,MAAM;AAAA;AAAA,EACN,MAAM;AAAA;AAAA,EACN,OAAO;AAAA;AAAA,EACP,OAAO;AAAA;AACT;AAEA,IAAM,QAAQ;AAEd,IAAM,OAAO,MAAM;AAAC;AAUb,IAAM,gBAAN,MAAM,eAAgC;AAAA,EAC3C;AAAA,EACiB;AAAA,EAER;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EAET,YACE,UAII,CAAC,GACL;AACA,UAAM;AAAA,MACJ,QAAQ;AAAA,MACR,SAAS,QAAQ,IAAI,aAAa;AAAA,MAClC;AAAA,IACF,IAAI;AACJ,SAAK,UAAU;AACf,SAAK,QAAQ;AAEb,UAAM,YAAY,aAAa,KAAK,KAAK;AACzC,UAAM,QAAQ,SACV,KAAK,aAAa,KAAK,MAAM,QAAQ,IACrC,KAAK,WAAW,KAAK,MAAM,QAAQ;AAGvC,SAAK,QAAQ,MAAM,KAAK,MAAM,SAAS,EAAE;AACzC,SAAK,QAAQ,aAAa,KAAK,MAAM,KAAK,MAAM,SAAS,EAAE,IAAI;AAC/D,SAAK,OAAO,aAAa,KAAK,MAAM,KAAK,MAAM,QAAQ,EAAE,IAAI;AAC7D,SAAK,OAAO,aAAa,KAAK,MAAM,KAAK,MAAM,QAAQ,EAAE,IAAI;AAC7D,SAAK,QAAQ,aAAa,KAAK,MAAM,KAAK,MAAM,SAAS,EAAE,IAAI;AAC/D,SAAK,QAAQ,aAAa,KAAK,MAAM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,GAAG,4BAC9B,iBAAiB,IAAI,cAAc,iBAAiB,OAAO;AAAA,MACxE;AACA,qBAAe;AAAA,IACjB;AAAA,EACF;AACA,SAAO;AACT;","names":["log","store","cache"]}
|
package/dist/index.cjs
CHANGED
|
@@ -190,7 +190,7 @@ var ConsoleLogger = class _ConsoleLogger {
|
|
|
190
190
|
}
|
|
191
191
|
};
|
|
192
192
|
|
|
193
|
-
// src/lru-map.ts
|
|
193
|
+
// src/internal/lru-map.ts
|
|
194
194
|
var LruMap = class {
|
|
195
195
|
constructor(_maxSize) {
|
|
196
196
|
this._maxSize = _maxSize;
|
|
@@ -1387,10 +1387,20 @@ function computeLagLeadRatio(handled, lagging, leading) {
|
|
|
1387
1387
|
}
|
|
1388
1388
|
|
|
1389
1389
|
// src/internal/drain-cycle.ts
|
|
1390
|
-
async function runDrainCycle(ops, registry, batchHandlers, handle, handleBatch, lagging, leading, eventLimit, leaseMillis) {
|
|
1390
|
+
async function runDrainCycle(ops, registry, batchHandlers, handle, handleBatch, lagging, leading, eventLimit, leaseMillis, isDeferred) {
|
|
1391
1391
|
const leased = await ops.claim(lagging, leading, (0, import_node_crypto2.randomUUID)(), leaseMillis);
|
|
1392
1392
|
if (!leased.length) return void 0;
|
|
1393
|
-
const
|
|
1393
|
+
const active = isDeferred ? leased.filter((l) => !isDeferred(l.stream)) : leased;
|
|
1394
|
+
if (!active.length) {
|
|
1395
|
+
return {
|
|
1396
|
+
leased,
|
|
1397
|
+
fetched: [],
|
|
1398
|
+
handled: [],
|
|
1399
|
+
acked: [],
|
|
1400
|
+
blocked: []
|
|
1401
|
+
};
|
|
1402
|
+
}
|
|
1403
|
+
const fetched = await ops.fetch(active, eventLimit);
|
|
1394
1404
|
const fetchMap = /* @__PURE__ */ new Map();
|
|
1395
1405
|
const fetch_window_at = fetched.reduce(
|
|
1396
1406
|
(max, { at, events }) => Math.max(max, events.at(-1)?.id || at),
|
|
@@ -1409,7 +1419,7 @@ async function runDrainCycle(ops, registry, batchHandlers, handle, handleBatch,
|
|
|
1409
1419
|
fetchMap.set(stream, { fetch: f, payloads });
|
|
1410
1420
|
}
|
|
1411
1421
|
const handled = await Promise.all(
|
|
1412
|
-
|
|
1422
|
+
active.map((lease) => {
|
|
1413
1423
|
const entry = fetchMap.get(lease.stream);
|
|
1414
1424
|
const at = entry.fetch.events.at(-1)?.id || fetch_window_at;
|
|
1415
1425
|
const { payloads } = entry;
|
|
@@ -1441,6 +1451,15 @@ var DrainController = class {
|
|
|
1441
1451
|
_armed = false;
|
|
1442
1452
|
_locked = false;
|
|
1443
1453
|
_ratio = 0.5;
|
|
1454
|
+
/**
|
|
1455
|
+
* Per-stream backoff: `stream → nextAttemptAt` (ms since epoch). Set by
|
|
1456
|
+
* `_finalize` via `HandleResult.nextAttemptAt`; cleared on successful
|
|
1457
|
+
* ack or terminal block. Lives in process memory — per-worker pacing
|
|
1458
|
+
* by design (see {@link BackoffOptions} for the multi-worker trade-off).
|
|
1459
|
+
*/
|
|
1460
|
+
_backoff = /* @__PURE__ */ new Map();
|
|
1461
|
+
/** Timer re-arming drain at the earliest pending `nextAttemptAt`. */
|
|
1462
|
+
_backoffTimer;
|
|
1444
1463
|
/**
|
|
1445
1464
|
* Signal that a commit (or reset / cold-start) may have produced work.
|
|
1446
1465
|
* Subsequent `drain()` calls will run the pipeline; once the pipeline
|
|
@@ -1453,6 +1472,32 @@ var DrainController = class {
|
|
|
1453
1472
|
get armed() {
|
|
1454
1473
|
return this._armed;
|
|
1455
1474
|
}
|
|
1475
|
+
/** Returns true when `stream` is currently within a backoff window. */
|
|
1476
|
+
isDeferred = (stream) => {
|
|
1477
|
+
const next = this._backoff.get(stream);
|
|
1478
|
+
return next !== void 0 && next > Date.now();
|
|
1479
|
+
};
|
|
1480
|
+
/**
|
|
1481
|
+
* Schedule the next drain re-arm at the earliest pending backoff
|
|
1482
|
+
* expiry. Called only when the backoff map is non-empty (caller guard).
|
|
1483
|
+
* Idempotent — collapses many simultaneously deferred streams into a
|
|
1484
|
+
* single timer.
|
|
1485
|
+
*/
|
|
1486
|
+
scheduleBackoffWake() {
|
|
1487
|
+
if (this._backoffTimer) clearTimeout(this._backoffTimer);
|
|
1488
|
+
let earliest = Number.POSITIVE_INFINITY;
|
|
1489
|
+
for (const t of this._backoff.values()) if (t < earliest) earliest = t;
|
|
1490
|
+
const delay = Math.max(0, earliest - Date.now());
|
|
1491
|
+
this._backoffTimer = setTimeout(() => {
|
|
1492
|
+
this._backoffTimer = void 0;
|
|
1493
|
+
const now = Date.now();
|
|
1494
|
+
for (const [stream, at] of this._backoff) {
|
|
1495
|
+
if (at <= now) this._backoff.delete(stream);
|
|
1496
|
+
}
|
|
1497
|
+
this._armed = true;
|
|
1498
|
+
}, delay);
|
|
1499
|
+
this._backoffTimer.unref();
|
|
1500
|
+
}
|
|
1456
1501
|
/** Run one drain pass. Short-circuits when not armed or already running. */
|
|
1457
1502
|
async drain({
|
|
1458
1503
|
streamLimit = 10,
|
|
@@ -1474,7 +1519,8 @@ var DrainController = class {
|
|
|
1474
1519
|
lagging,
|
|
1475
1520
|
leading,
|
|
1476
1521
|
eventLimit,
|
|
1477
|
-
leaseMillis
|
|
1522
|
+
leaseMillis,
|
|
1523
|
+
this._backoff.size > 0 ? this.isDeferred : void 0
|
|
1478
1524
|
);
|
|
1479
1525
|
if (!cycle) {
|
|
1480
1526
|
this._armed = false;
|
|
@@ -1482,6 +1528,14 @@ var DrainController = class {
|
|
|
1482
1528
|
}
|
|
1483
1529
|
const { leased, fetched, handled, acked, blocked } = cycle;
|
|
1484
1530
|
this._ratio = computeLagLeadRatio(handled, lagging, leading);
|
|
1531
|
+
for (const lease of acked) this._backoff.delete(lease.stream);
|
|
1532
|
+
for (const lease of blocked) this._backoff.delete(lease.stream);
|
|
1533
|
+
for (const h of handled) {
|
|
1534
|
+
if (h.nextAttemptAt !== void 0 && !h.block) {
|
|
1535
|
+
this._backoff.set(h.lease.stream, h.nextAttemptAt);
|
|
1536
|
+
}
|
|
1537
|
+
}
|
|
1538
|
+
if (this._backoff.size > 0) this.scheduleBackoffWake();
|
|
1485
1539
|
if (acked.length) this.deps.onAcked(acked);
|
|
1486
1540
|
if (blocked.length) this.deps.onBlocked(blocked);
|
|
1487
1541
|
const hasErrors = handled.some(({ error }) => error);
|
|
@@ -1676,6 +1730,27 @@ var _this_ = ({ stream }) => ({
|
|
|
1676
1730
|
target: stream
|
|
1677
1731
|
});
|
|
1678
1732
|
|
|
1733
|
+
// src/internal/backoff.ts
|
|
1734
|
+
function computeBackoffDelay(retry, opts) {
|
|
1735
|
+
if (!opts || opts.baseMs <= 0) return 0;
|
|
1736
|
+
const r = Math.max(0, retry);
|
|
1737
|
+
let delay;
|
|
1738
|
+
switch (opts.strategy) {
|
|
1739
|
+
case "fixed":
|
|
1740
|
+
delay = opts.baseMs;
|
|
1741
|
+
break;
|
|
1742
|
+
case "linear":
|
|
1743
|
+
delay = opts.baseMs * (r + 1);
|
|
1744
|
+
break;
|
|
1745
|
+
case "exponential":
|
|
1746
|
+
delay = opts.baseMs * 2 ** r;
|
|
1747
|
+
if (opts.maxMs !== void 0) delay = Math.min(delay, opts.maxMs);
|
|
1748
|
+
break;
|
|
1749
|
+
}
|
|
1750
|
+
if (opts.jitter) delay = delay * (0.5 + Math.random());
|
|
1751
|
+
return Math.max(0, Math.floor(delay));
|
|
1752
|
+
}
|
|
1753
|
+
|
|
1679
1754
|
// src/internal/reactions.ts
|
|
1680
1755
|
function finalize(lease, handled, at, error, options, logger) {
|
|
1681
1756
|
if (!error) return { lease, handled, at };
|
|
@@ -1683,12 +1758,14 @@ function finalize(lease, handled, at, error, options, logger) {
|
|
|
1683
1758
|
const block2 = lease.retry >= options.maxRetries && options.blockOnError;
|
|
1684
1759
|
if (block2)
|
|
1685
1760
|
logger.error(`Blocking ${lease.stream} after ${lease.retry} retries.`);
|
|
1761
|
+
const nextAttemptAt = !block2 && options.backoff ? Date.now() + computeBackoffDelay(lease.retry, options.backoff) : void 0;
|
|
1686
1762
|
return {
|
|
1687
1763
|
lease,
|
|
1688
1764
|
handled,
|
|
1689
1765
|
at,
|
|
1690
1766
|
error: handled === 0 ? error.message : void 0,
|
|
1691
|
-
block: block2
|
|
1767
|
+
block: block2,
|
|
1768
|
+
nextAttemptAt
|
|
1692
1769
|
};
|
|
1693
1770
|
}
|
|
1694
1771
|
function buildHandle(deps) {
|
|
@@ -3008,7 +3085,8 @@ function act() {
|
|
|
3008
3085
|
resolver: _this_,
|
|
3009
3086
|
options: {
|
|
3010
3087
|
blockOnError: options?.blockOnError ?? true,
|
|
3011
|
-
maxRetries: options?.maxRetries ?? 3
|
|
3088
|
+
maxRetries: options?.maxRetries ?? 3,
|
|
3089
|
+
backoff: options?.backoff
|
|
3012
3090
|
}
|
|
3013
3091
|
};
|
|
3014
3092
|
if (!handler.name)
|
|
@@ -3134,7 +3212,8 @@ function slice() {
|
|
|
3134
3212
|
resolver: _this_,
|
|
3135
3213
|
options: {
|
|
3136
3214
|
blockOnError: options?.blockOnError ?? true,
|
|
3137
|
-
maxRetries: options?.maxRetries ?? 3
|
|
3215
|
+
maxRetries: options?.maxRetries ?? 3,
|
|
3216
|
+
backoff: options?.backoff
|
|
3138
3217
|
}
|
|
3139
3218
|
};
|
|
3140
3219
|
if (!handler.name)
|