brass-runtime 1.14.0 → 1.16.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/README.md +410 -135
- package/dist/agent/cli/main.cjs +49 -43
- package/dist/agent/cli/main.js +11 -5
- package/dist/agent/cli/main.mjs +11 -5
- package/dist/agent/index.cjs +8 -3
- package/dist/agent/index.d.ts +1 -1
- package/dist/agent/index.js +7 -2
- package/dist/agent/index.mjs +7 -2
- package/dist/{chunk-BMRF4FN6.js → chunk-2WC63LJK.mjs} +68 -242
- package/dist/chunk-3RG5ZIWI.js +10 -0
- package/dist/chunk-45F7OKGT.cjs +104 -0
- package/dist/chunk-5YOQOXEQ.cjs +2491 -0
- package/dist/chunk-7HUOJA4W.cjs +493 -0
- package/dist/{chunk-4N2JEK4H.mjs → chunk-7LVI2GIN.js} +252 -495
- package/dist/chunk-7TL2LHQJ.js +2491 -0
- package/dist/chunk-7V4KY4RL.mjs +104 -0
- package/dist/chunk-7XOPAB5Q.js +2143 -0
- package/dist/chunk-CCKHV5BT.mjs +193 -0
- package/dist/chunk-CY33PGEX.mjs +1110 -0
- package/dist/chunk-DJQ7OMMB.cjs +144 -0
- package/dist/chunk-F5EUMJL7.mjs +2143 -0
- package/dist/chunk-FM4W4QPL.js +193 -0
- package/dist/chunk-G3XGCZDQ.js +131 -0
- package/dist/{chunk-JT7D6M5H.js → chunk-G6IQOE4P.mjs} +252 -495
- package/dist/chunk-GOV47PPB.mjs +552 -0
- package/dist/chunk-H55LI6WY.js +93 -0
- package/dist/chunk-IJT6RRQ5.cjs +93 -0
- package/dist/chunk-J3H54ZRV.mjs +131 -0
- package/dist/chunk-JF4XXPZ5.cjs +552 -0
- package/dist/chunk-JNFRRJYH.cjs +2143 -0
- package/dist/chunk-JX3LZQJH.cjs +354 -0
- package/dist/chunk-K2T3DV26.mjs +93 -0
- package/dist/chunk-KCPT2D6G.js +552 -0
- package/dist/chunk-MWXMNYJS.cjs +1110 -0
- package/dist/{chunk-XTMZTVIT.cjs → chunk-N6VHMOWB.cjs} +140 -134
- package/dist/{chunk-WJESVBWN.js → chunk-NC5SDRYE.js} +16 -10
- package/dist/chunk-NOYZIMUJ.mjs +144 -0
- package/dist/chunk-NYL4D7SK.cjs +131 -0
- package/dist/chunk-OBGZSXTJ.cjs +10 -0
- package/dist/{chunk-UWMMYKVK.mjs → chunk-OOGJ73B6.js} +68 -242
- package/dist/chunk-PNVFW245.js +144 -0
- package/dist/chunk-PRWCB3QL.mjs +2491 -0
- package/dist/chunk-QY5FKYEQ.js +1110 -0
- package/dist/chunk-ROJC3NBJ.js +104 -0
- package/dist/chunk-SPUEME2B.cjs +343 -0
- package/dist/chunk-TDVMADDN.js +343 -0
- package/dist/chunk-TVN5I4U6.cjs +193 -0
- package/dist/chunk-U5KWK3PX.mjs +343 -0
- package/dist/chunk-VFIUZG7J.mjs +354 -0
- package/dist/{chunk-BKBFSOGT.cjs → chunk-WQ5QNU5R.cjs} +460 -703
- package/dist/chunk-XDZOO4L5.js +354 -0
- package/dist/chunk-Y6FXYEAI.mjs +10 -0
- package/dist/{chunk-MQF7HZ7Y.mjs → chunk-ZGLD4TVZ.mjs} +16 -10
- package/dist/client-CtFmoDvM.d.ts +645 -0
- package/dist/core/index.cjs +284 -0
- package/dist/core/index.d.ts +567 -0
- package/dist/core/index.js +284 -0
- package/dist/core/index.mjs +284 -0
- package/dist/{effect-DM56H743.d.ts → effect-CGNl5Rqp.d.ts} +118 -11
- package/dist/effectRunner-3ZHAD3LE.cjs +8 -0
- package/dist/effectRunner-A4CHJXJI.js +8 -0
- package/dist/effectRunner-OPUF6QRN.mjs +8 -0
- package/dist/http/index.cjs +4130 -890
- package/dist/http/index.d.ts +2289 -219
- package/dist/http/index.js +4116 -876
- package/dist/http/index.mjs +4116 -876
- package/dist/http/testing.cjs +159 -0
- package/dist/http/testing.d.ts +42 -0
- package/dist/http/testing.js +159 -0
- package/dist/http/testing.mjs +159 -0
- package/dist/index.cjs +305 -1168
- package/dist/index.d.ts +9 -701
- package/dist/index.js +176 -1039
- package/dist/index.mjs +176 -1039
- package/dist/observability/index.cjs +677 -0
- package/dist/observability/index.d.ts +79 -0
- package/dist/observability/index.js +677 -0
- package/dist/observability/index.mjs +677 -0
- package/dist/schedule-Fque9Abz.d.ts +70 -0
- package/dist/schema/index.cjs +25 -0
- package/dist/schema/index.d.ts +177 -0
- package/dist/schema/index.js +25 -0
- package/dist/schema/index.mjs +25 -0
- package/dist/server-C8hDXA74.d.ts +674 -0
- package/dist/stream-dvSs0QS5.d.ts +74 -0
- package/dist/tracer-B5tRH9H7.d.ts +230 -0
- package/dist/tracing-Dt9S_6V8.d.ts +148 -0
- package/package.json +37 -3
- package/dist/chunk-SKVY72E5.cjs +0 -667
- package/dist/stream-Oqe6WeLE.d.ts +0 -173
|
@@ -0,0 +1,567 @@
|
|
|
1
|
+
import { A as Async, f as Runtime, E as Exit, R as RuntimeHooks, g as RuntimeRegistry, k as Fiber } from '../effect-CGNl5Rqp.js';
|
|
2
|
+
export { u as AbortablePromiseFinish, v as AbortablePromiseLabelStats, w as AbortablePromiseOptions, x as AbortablePromiseOutcome, y as AbortablePromiseStats, C as AsyncWithPromise, G as CancelToken, H as Canceler, I as Cause, K as ContextNode, Y as FiberContext, l as FiberId, $ as FiberInfo, a3 as FiberRunState, m as FiberStatus, af as Interrupted, J as JSONValue, ai as None, aj as NoopHooks, O as Option, c as RuntimeEmitContext, b as RuntimeEvent, d as RuntimeEventRecord, i as RuntimeFiber, h as RuntimeOptions, e as RuntimeSpanLink, S as Scope, aB as ScopeId, aC as ScopeInfo, aD as Some, T as TraceContext, Z as ZIO, aI as abortablePromiseStats, aJ as acquireRelease, aK as async, aL as asyncCatchAll, aM as asyncFail, aN as asyncFlatMap, aO as asyncFold, aP as asyncInterruptible, aQ as asyncMap, aR as asyncMapError, aS as asyncSucceed, aT as asyncSync, aU as asyncTotal, aV as catchAll, aW as ctxExtend, aX as ctxToObject, aY as emptyContext, aZ as end, a$ as fail, b0 as flatMap, b1 as fork, b2 as fromPromiseAbortable, b3 as getBenchmarkBudget, b4 as getCurrentFiber, b8 as linkAbortController, ba as makeCancelToken, bc as makeRuntimeEventRecord, bd as map, be as mapAsync, bf as mapError, bg as mapTryAsync, bh as none, bi as orElseOptional, bj as resetAbortablePromiseStats, bl as runtimeEventRecordContext, bm as runtimeForCaller, bp as setBenchmarkBudget, bq as some, br as succeed, bs as sync, bt as toPromise, bu as toPromiseByCaller, bv as unit, bw as unsafeGetCurrentRuntime, bx as unsafeRunAsync, by as unsafeRunFoldWithEnv, bz as withAsyncPromise, bA as withCurrentFiber, bB as withScope, bC as withScopeAsync } from '../effect-CGNl5Rqp.js';
|
|
3
|
+
export { M as ManagedResource, R as Resource, R as ResourceDescriptor, S as Span, a as SpanContext, b as SpanEvent, c as SpanStatus, T as Tracer, d as TracerConfig, e as bracket, f as ensuring, m as makeResource, g as makeTracer, h as managed, i as managedAll, r as resource, j as resourceAll, k as resourceFromManaged, l as resourceSucceed, u as useManaged, n as useResource } from '../tracing-Dt9S_6V8.js';
|
|
4
|
+
import { S as Schedule } from '../schedule-Fque9Abz.js';
|
|
5
|
+
export { a as ScheduleDecision, b as andThenSchedule, c as contramapSchedule, e as elapsed, d as exponential, f as fibonacci, g as fixed, i as intersect, j as jitter, h as jittered, k as jitteredSchedule, m as mapSchedule, r as recurs, l as repeatWithSchedule, n as retryWithSchedule, t as takeSchedule, u as union, w as whileInput, o as windowed } from '../schedule-Fque9Abz.js';
|
|
6
|
+
export { B as BrassEnv, f as CircuitBreaker, g as CircuitBreakerConfig, h as CircuitBreakerError, i as CircuitBreakerState, C as CircuitBreakerStats, j as Counter, E as EventBus, k as EventBusOptions, l as EventHandler, G as Gauge, H as Histogram, m as HistogramBuckets, I as InMemoryTracer, e as InMemoryTracerOptions, n as InMemoryTracerStats, a as MetricExemplar, b as MetricSnapshot, o as MetricType, p as MetricValue, M as MetricsRegistry, R as RuntimeSpan, q as RuntimeSpanEvent, d as RuntimeTraceIdGenerator, r as defaultTracer, s as makeCircuitBreaker, t as makeMetrics, u as runtimeHooksToEventHandler } from '../tracer-B5tRH9H7.js';
|
|
7
|
+
|
|
8
|
+
type TimeoutError = {
|
|
9
|
+
readonly _tag: "TimeoutError";
|
|
10
|
+
readonly ms: number;
|
|
11
|
+
};
|
|
12
|
+
/**
|
|
13
|
+
* Suspends the fiber for `ms` milliseconds. Cancellable via fiber interruption.
|
|
14
|
+
*/
|
|
15
|
+
declare function sleep(ms: number): Async<unknown, never, void>;
|
|
16
|
+
/**
|
|
17
|
+
* Runs `effect` with a timeout of `ms` milliseconds.
|
|
18
|
+
* - If the effect completes before the timeout, returns its result.
|
|
19
|
+
* - If the timeout fires first, the effect is cancelled and a TimeoutError is returned.
|
|
20
|
+
*
|
|
21
|
+
* Works with ANY effect type (Async, FlatMap, Fold, etc.) by forking
|
|
22
|
+
* the effect into a child fiber and interrupting it on timeout.
|
|
23
|
+
*/
|
|
24
|
+
declare function timeout<R, E, A>(effect: Async<R, E, A>, ms: number): Async<R, E | TimeoutError, A>;
|
|
25
|
+
type RetryPolicy = {
|
|
26
|
+
/** Maximum number of retry attempts (0 = no retries, just the initial attempt) */
|
|
27
|
+
readonly maxRetries: number;
|
|
28
|
+
/** Base delay in ms for exponential backoff */
|
|
29
|
+
readonly baseDelayMs: number;
|
|
30
|
+
/** Maximum delay cap in ms */
|
|
31
|
+
readonly maxDelayMs: number;
|
|
32
|
+
/** Total time budget in ms (optional). Retries stop if elapsed time exceeds this. */
|
|
33
|
+
readonly maxElapsedMs?: number;
|
|
34
|
+
/** Custom predicate: should this error trigger a retry? Default: always retry. */
|
|
35
|
+
readonly shouldRetry?: (error: unknown, attempt: number) => boolean;
|
|
36
|
+
/** Jitter strategy. Default: "full" (random 0..delay). "none" = no jitter. */
|
|
37
|
+
readonly jitter?: "full" | "none";
|
|
38
|
+
};
|
|
39
|
+
type RetryState = {
|
|
40
|
+
readonly attempt: number;
|
|
41
|
+
readonly elapsedMs: number;
|
|
42
|
+
readonly lastError: unknown;
|
|
43
|
+
};
|
|
44
|
+
/**
|
|
45
|
+
* Retries an effect according to the given policy.
|
|
46
|
+
* Uses exponential backoff with full jitter by default.
|
|
47
|
+
*
|
|
48
|
+
* ```ts
|
|
49
|
+
* const result = retry(
|
|
50
|
+
* fetchData(),
|
|
51
|
+
* { maxRetries: 3, baseDelayMs: 100, maxDelayMs: 5000 }
|
|
52
|
+
* );
|
|
53
|
+
* ```
|
|
54
|
+
*/
|
|
55
|
+
declare function retry<R, E, A>(effect: Async<R, E, A>, policy: RetryPolicy): Async<R, E, A>;
|
|
56
|
+
/**
|
|
57
|
+
* Retry with a simple count (no backoff, immediate retry).
|
|
58
|
+
* Useful for flaky operations that just need a few attempts.
|
|
59
|
+
*/
|
|
60
|
+
declare function retryN<R, E, A>(effect: Async<R, E, A>, n: number): Async<R, E, A>;
|
|
61
|
+
/**
|
|
62
|
+
* Retry with exponential backoff and full jitter.
|
|
63
|
+
* Convenience wrapper with sensible defaults.
|
|
64
|
+
*/
|
|
65
|
+
declare function retryWithBackoff<R, E, A>(effect: Async<R, E, A>, opts?: {
|
|
66
|
+
maxRetries?: number;
|
|
67
|
+
baseDelayMs?: number;
|
|
68
|
+
maxDelayMs?: number;
|
|
69
|
+
maxElapsedMs?: number;
|
|
70
|
+
shouldRetry?: (error: unknown, attempt: number) => boolean;
|
|
71
|
+
}): Async<R, E, A>;
|
|
72
|
+
|
|
73
|
+
type Semaphore = {
|
|
74
|
+
/** Current number of available permits. */
|
|
75
|
+
readonly available: () => number;
|
|
76
|
+
/** Total number of permits (capacity). */
|
|
77
|
+
readonly capacity: number;
|
|
78
|
+
/** Number of effects waiting for a permit. */
|
|
79
|
+
readonly waiting: () => number;
|
|
80
|
+
/** Acquire a permit, run the effect, and release the permit. */
|
|
81
|
+
readonly withPermit: <R, E, A>(effect: Async<R, E, A>) => Async<R, E, A>;
|
|
82
|
+
/** Acquire a permit (manual). Must call release() when done. */
|
|
83
|
+
readonly acquire: () => Async<unknown, never, void>;
|
|
84
|
+
/** Release a permit (manual). */
|
|
85
|
+
readonly release: () => void;
|
|
86
|
+
};
|
|
87
|
+
type SemaphoreStats = {
|
|
88
|
+
readonly capacity: number;
|
|
89
|
+
readonly available: number;
|
|
90
|
+
readonly waiting: number;
|
|
91
|
+
readonly acquired: number;
|
|
92
|
+
readonly released: number;
|
|
93
|
+
};
|
|
94
|
+
/**
|
|
95
|
+
* Creates a counting semaphore with `n` permits.
|
|
96
|
+
*
|
|
97
|
+
* ```ts
|
|
98
|
+
* const sem = makeSemaphore(5); // max 5 concurrent
|
|
99
|
+
*
|
|
100
|
+
* // Automatic acquire/release:
|
|
101
|
+
* const result = await run(sem.withPermit(fetchData()));
|
|
102
|
+
*
|
|
103
|
+
* // Manual acquire/release:
|
|
104
|
+
* await run(sem.acquire());
|
|
105
|
+
* try { ... } finally { sem.release(); }
|
|
106
|
+
* ```
|
|
107
|
+
*/
|
|
108
|
+
declare function makeSemaphore(n: number): Semaphore;
|
|
109
|
+
|
|
110
|
+
type Ref<A> = {
|
|
111
|
+
/** Get the current value. */
|
|
112
|
+
readonly get: () => Async<unknown, never, A>;
|
|
113
|
+
/** Set a new value. */
|
|
114
|
+
readonly set: (value: A) => Async<unknown, never, void>;
|
|
115
|
+
/** Modify the value with a function and return the new value. */
|
|
116
|
+
readonly update: (f: (current: A) => A) => Async<unknown, never, A>;
|
|
117
|
+
/** Modify the value and return a derived result. */
|
|
118
|
+
readonly modify: <B>(f: (current: A) => [B, A]) => Async<unknown, never, B>;
|
|
119
|
+
/** Get the current value synchronously (for non-effect contexts). */
|
|
120
|
+
readonly unsafeGet: () => A;
|
|
121
|
+
};
|
|
122
|
+
/**
|
|
123
|
+
* Creates a mutable reference with an initial value.
|
|
124
|
+
*
|
|
125
|
+
* ```ts
|
|
126
|
+
* const counter = makeRef(0);
|
|
127
|
+
*
|
|
128
|
+
* // In effects:
|
|
129
|
+
* await run(counter.update(n => n + 1));
|
|
130
|
+
* const value = await run(counter.get());
|
|
131
|
+
*
|
|
132
|
+
* // Synchronous access (outside effects):
|
|
133
|
+
* counter.unsafeGet();
|
|
134
|
+
* ```
|
|
135
|
+
*/
|
|
136
|
+
declare function makeRef<A>(initial: A): Ref<A>;
|
|
137
|
+
/**
|
|
138
|
+
* Creates a derived Ref that applies a lens to a parent Ref.
|
|
139
|
+
*
|
|
140
|
+
* ```ts
|
|
141
|
+
* const state = makeRef({ count: 0, name: "test" });
|
|
142
|
+
* const countRef = derivedRef(state, s => s.count, (s, c) => ({ ...s, count: c }));
|
|
143
|
+
* ```
|
|
144
|
+
*/
|
|
145
|
+
declare function derivedRef<A, B>(parent: Ref<A>, get: (a: A) => B, set: (a: A, b: B) => A): Ref<B>;
|
|
146
|
+
|
|
147
|
+
type ShutdownConfig = {
|
|
148
|
+
/** Max time to wait for in-flight work to complete. Default: 30000ms */
|
|
149
|
+
readonly timeoutMs?: number;
|
|
150
|
+
/** Called when shutdown starts. */
|
|
151
|
+
readonly onStart?: () => void;
|
|
152
|
+
/** Called when shutdown completes. */
|
|
153
|
+
readonly onComplete?: (stats: ShutdownStats) => void;
|
|
154
|
+
/** Called if shutdown times out. */
|
|
155
|
+
readonly onTimeout?: (stats: ShutdownStats) => void;
|
|
156
|
+
};
|
|
157
|
+
type ShutdownStats = {
|
|
158
|
+
readonly startedAt: number;
|
|
159
|
+
readonly completedAt: number;
|
|
160
|
+
readonly elapsedMs: number;
|
|
161
|
+
readonly timedOut: boolean;
|
|
162
|
+
};
|
|
163
|
+
/**
|
|
164
|
+
* Performs a graceful shutdown of the runtime.
|
|
165
|
+
*
|
|
166
|
+
* 1. Signals the scheduler to stop accepting new work
|
|
167
|
+
* 2. Waits for in-flight fibers to complete (up to timeoutMs)
|
|
168
|
+
* 3. Calls the runtime's shutdown hook
|
|
169
|
+
* 4. Reports stats
|
|
170
|
+
*
|
|
171
|
+
* ```ts
|
|
172
|
+
* await gracefulShutdown(runtime, {
|
|
173
|
+
* timeoutMs: 5000,
|
|
174
|
+
* onStart: () => console.log("Shutting down..."),
|
|
175
|
+
* onComplete: (stats) => console.log(`Done in ${stats.elapsedMs}ms`),
|
|
176
|
+
* });
|
|
177
|
+
* ```
|
|
178
|
+
*/
|
|
179
|
+
declare function gracefulShutdown<R>(runtime: Runtime<R>, config?: ShutdownConfig): Promise<ShutdownStats>;
|
|
180
|
+
/**
|
|
181
|
+
* Registers process signal handlers for graceful shutdown.
|
|
182
|
+
* Handles SIGTERM and SIGINT (Ctrl+C).
|
|
183
|
+
*
|
|
184
|
+
* ```ts
|
|
185
|
+
* registerShutdownHooks(runtime, {
|
|
186
|
+
* timeoutMs: 10000,
|
|
187
|
+
* onComplete: () => process.exit(0),
|
|
188
|
+
* onTimeout: () => process.exit(1),
|
|
189
|
+
* });
|
|
190
|
+
* ```
|
|
191
|
+
*/
|
|
192
|
+
declare function registerShutdownHooks<R>(runtime: Runtime<R>, config?: ShutdownConfig): () => void;
|
|
193
|
+
|
|
194
|
+
type TestRuntimeOptions = {
|
|
195
|
+
/** If true, effects run synchronously where possible. Default: true */
|
|
196
|
+
readonly synchronous?: boolean;
|
|
197
|
+
};
|
|
198
|
+
/**
|
|
199
|
+
* Creates a test runtime that provides controlled execution.
|
|
200
|
+
*
|
|
201
|
+
* ```ts
|
|
202
|
+
* const { runtime, run, runExit } = makeTestRuntime();
|
|
203
|
+
*
|
|
204
|
+
* const result = await run(myEffect);
|
|
205
|
+
* const exit = await runExit(myEffect); // get full Exit (success or failure)
|
|
206
|
+
* ```
|
|
207
|
+
*/
|
|
208
|
+
declare function makeTestRuntime<R = {}>(env?: R, options?: TestRuntimeOptions): {
|
|
209
|
+
runtime: Runtime<R>;
|
|
210
|
+
run: <E, A>(effect: Async<R, E, A>) => Promise<A>;
|
|
211
|
+
runExit: <E, A>(effect: Async<R, E, A>) => Promise<Exit<E, A>>;
|
|
212
|
+
};
|
|
213
|
+
/**
|
|
214
|
+
* Asserts that an effect succeeds with a specific value.
|
|
215
|
+
*
|
|
216
|
+
* ```ts
|
|
217
|
+
* await assertSucceeds(myEffect, 42);
|
|
218
|
+
* ```
|
|
219
|
+
*/
|
|
220
|
+
declare function assertSucceeds<R, E, A>(effect: Async<R, E, A>, expected: A, runtime?: Runtime<R>): Promise<void>;
|
|
221
|
+
/**
|
|
222
|
+
* Asserts that an effect fails with a specific error.
|
|
223
|
+
*
|
|
224
|
+
* ```ts
|
|
225
|
+
* await assertFails(myEffect, "not found");
|
|
226
|
+
* ```
|
|
227
|
+
*/
|
|
228
|
+
declare function assertFails<R, E, A>(effect: Async<R, E, A>, expectedError: E, runtime?: Runtime<R>): Promise<void>;
|
|
229
|
+
/**
|
|
230
|
+
* Asserts that an effect fails with an error matching a predicate.
|
|
231
|
+
*
|
|
232
|
+
* ```ts
|
|
233
|
+
* await assertFailsWith(myEffect, (e) => e._tag === "NotFound");
|
|
234
|
+
* ```
|
|
235
|
+
*/
|
|
236
|
+
declare function assertFailsWith<R, E, A>(effect: Async<R, E, A>, predicate: (error: E) => boolean, runtime?: Runtime<R>): Promise<void>;
|
|
237
|
+
/**
|
|
238
|
+
* Asserts that an effect completes within a time limit.
|
|
239
|
+
*
|
|
240
|
+
* ```ts
|
|
241
|
+
* await assertCompletesWithin(myEffect, 100); // must finish in 100ms
|
|
242
|
+
* ```
|
|
243
|
+
*/
|
|
244
|
+
declare function assertCompletesWithin<R, E, A>(effect: Async<R, E, A>, maxMs: number, runtime?: Runtime<R>): Promise<A>;
|
|
245
|
+
/**
|
|
246
|
+
* Creates an effect that fails on the first N calls, then succeeds.
|
|
247
|
+
* Useful for testing retry logic.
|
|
248
|
+
*
|
|
249
|
+
* ```ts
|
|
250
|
+
* const flaky = flakyEffect(3, "success!", "temporary error");
|
|
251
|
+
* // Fails 3 times, then returns "success!"
|
|
252
|
+
* ```
|
|
253
|
+
*/
|
|
254
|
+
declare function flakyEffect<E, A>(failCount: number, successValue: A, errorValue: E): Async<unknown, E, A>;
|
|
255
|
+
/**
|
|
256
|
+
* Creates an effect that takes a specific amount of time to complete.
|
|
257
|
+
* Useful for testing timeouts and concurrency.
|
|
258
|
+
*
|
|
259
|
+
* ```ts
|
|
260
|
+
* const slow = delayedEffect(100, "done"); // completes after 100ms
|
|
261
|
+
* ```
|
|
262
|
+
*/
|
|
263
|
+
declare function delayedEffect<A>(ms: number, value: A): Async<unknown, never, A>;
|
|
264
|
+
/**
|
|
265
|
+
* Creates an effect that never completes (hangs forever).
|
|
266
|
+
* Useful for testing timeouts and interruption.
|
|
267
|
+
*/
|
|
268
|
+
declare function neverEffect<A = never>(): Async<unknown, never, A>;
|
|
269
|
+
|
|
270
|
+
/**
|
|
271
|
+
* A Layer describes how to build a service.
|
|
272
|
+
*
|
|
273
|
+
* - RIn: dependencies required to build this service
|
|
274
|
+
* - E: possible failure during construction
|
|
275
|
+
* - ROut: the service produced
|
|
276
|
+
*/
|
|
277
|
+
type Layer<RIn, E, ROut> = {
|
|
278
|
+
readonly _tag: "Layer";
|
|
279
|
+
readonly build: (deps: RIn) => Async<unknown, E, {
|
|
280
|
+
service: ROut;
|
|
281
|
+
release: () => Async<unknown, never, void>;
|
|
282
|
+
}>;
|
|
283
|
+
};
|
|
284
|
+
/**
|
|
285
|
+
* Creates a Layer from an acquire/release pair.
|
|
286
|
+
*
|
|
287
|
+
* ```ts
|
|
288
|
+
* const DbLayer = layer(
|
|
289
|
+
* () => createPool({ max: 10 }),
|
|
290
|
+
* (pool) => pool.close()
|
|
291
|
+
* );
|
|
292
|
+
* ```
|
|
293
|
+
*/
|
|
294
|
+
declare function layer<ROut, E = never>(acquire: () => Async<unknown, E, ROut>, release?: (service: ROut) => Async<unknown, never, void>): Layer<unknown, E, ROut>;
|
|
295
|
+
/**
|
|
296
|
+
* Creates a Layer that depends on another service.
|
|
297
|
+
*
|
|
298
|
+
* ```ts
|
|
299
|
+
* const RepoLayer = layerFrom<DbPool>()(
|
|
300
|
+
* (pool) => createRepo(pool),
|
|
301
|
+
* (repo) => repo.close()
|
|
302
|
+
* );
|
|
303
|
+
* ```
|
|
304
|
+
*/
|
|
305
|
+
declare function layerFrom<RIn>(): <ROut, E = never>(acquire: (deps: RIn) => Async<unknown, E, ROut>, release?: (service: ROut) => Async<unknown, never, void>) => Layer<RIn, E, ROut>;
|
|
306
|
+
/**
|
|
307
|
+
* Creates a Layer from a pure value (no lifecycle).
|
|
308
|
+
*
|
|
309
|
+
* ```ts
|
|
310
|
+
* const ConfigLayer = layerSucceed({ port: 3000, host: "localhost" });
|
|
311
|
+
* ```
|
|
312
|
+
*/
|
|
313
|
+
declare function layerSucceed<ROut>(value: ROut): Layer<unknown, never, ROut>;
|
|
314
|
+
/**
|
|
315
|
+
* Creates a Layer that always fails.
|
|
316
|
+
*/
|
|
317
|
+
declare function layerFail<E>(error: E): Layer<unknown, E, never>;
|
|
318
|
+
/**
|
|
319
|
+
* Compose two layers: the output of `from` feeds into `to`.
|
|
320
|
+
*
|
|
321
|
+
* ```ts
|
|
322
|
+
* const AppLayer = compose(DbLayer, RepoLayer);
|
|
323
|
+
* // DbLayer produces DbPool → RepoLayer consumes DbPool → produces Repo
|
|
324
|
+
* ```
|
|
325
|
+
*/
|
|
326
|
+
declare function compose<R1, E1, Mid, E2, ROut>(from: Layer<R1, E1, Mid>, to: Layer<Mid, E2, ROut>): Layer<R1, E1 | E2, ROut>;
|
|
327
|
+
/**
|
|
328
|
+
* Merge two independent layers into one that produces both services.
|
|
329
|
+
*
|
|
330
|
+
* ```ts
|
|
331
|
+
* const AppLayer = merge(DbLayer, CacheLayer);
|
|
332
|
+
* // Produces { db: DbPool, cache: CacheClient }
|
|
333
|
+
* ```
|
|
334
|
+
*/
|
|
335
|
+
declare function merge<R1, E1, A, R2, E2, B>(left: Layer<R1, E1, A>, right: Layer<R2, E2, B>): Layer<R1 & R2, E1 | E2, A & B>;
|
|
336
|
+
/**
|
|
337
|
+
* Map the output of a layer.
|
|
338
|
+
*/
|
|
339
|
+
declare function mapLayer<RIn, E, A, B>(l: Layer<RIn, E, A>, f: (a: A) => B): Layer<RIn, E, B>;
|
|
340
|
+
/**
|
|
341
|
+
* Builds a layer, runs an effect with the produced service, and releases.
|
|
342
|
+
*
|
|
343
|
+
* ```ts
|
|
344
|
+
* const result = await run(
|
|
345
|
+
* provideLayer(AppLayer, (services) => services.db.query("SELECT 1"))
|
|
346
|
+
* );
|
|
347
|
+
* ```
|
|
348
|
+
*/
|
|
349
|
+
declare function provideLayer<RIn, E, ROut, E2, A>(l: Layer<RIn, E, ROut>, use: (service: ROut) => Async<unknown, E2, A>, deps?: RIn): Async<unknown, E | E2, A>;
|
|
350
|
+
|
|
351
|
+
type WorkerPoolConfig = {
|
|
352
|
+
/** Number of worker threads. Default: number of CPUs - 1 */
|
|
353
|
+
readonly size?: number;
|
|
354
|
+
/** Max queued tasks. Tasks beyond this are rejected. Default: 1000 */
|
|
355
|
+
readonly maxQueue?: number;
|
|
356
|
+
/** Task timeout in ms. Default: 30000 */
|
|
357
|
+
readonly taskTimeoutMs?: number;
|
|
358
|
+
};
|
|
359
|
+
type WorkerPoolError = {
|
|
360
|
+
readonly _tag: "WorkerPoolFull";
|
|
361
|
+
readonly queued: number;
|
|
362
|
+
} | {
|
|
363
|
+
readonly _tag: "WorkerTaskTimeout";
|
|
364
|
+
readonly ms: number;
|
|
365
|
+
} | {
|
|
366
|
+
readonly _tag: "WorkerTaskError";
|
|
367
|
+
readonly message: string;
|
|
368
|
+
} | {
|
|
369
|
+
readonly _tag: "WorkerPoolClosed";
|
|
370
|
+
};
|
|
371
|
+
type WorkerPool = {
|
|
372
|
+
/** Execute a function in a worker thread. */
|
|
373
|
+
readonly execute: <A>(fn: () => A) => Async<unknown, WorkerPoolError, A>;
|
|
374
|
+
/** Execute a serializable task (function source + args). */
|
|
375
|
+
readonly run: <A>(taskSource: string, args?: any[]) => Async<unknown, WorkerPoolError, A>;
|
|
376
|
+
/** Current pool stats. */
|
|
377
|
+
readonly stats: () => WorkerPoolStats;
|
|
378
|
+
/** Shutdown the pool (terminate all workers). */
|
|
379
|
+
readonly shutdown: () => Promise<void>;
|
|
380
|
+
};
|
|
381
|
+
type WorkerPoolStats = {
|
|
382
|
+
readonly size: number;
|
|
383
|
+
readonly busy: number;
|
|
384
|
+
readonly idle: number;
|
|
385
|
+
readonly queued: number;
|
|
386
|
+
readonly completed: number;
|
|
387
|
+
readonly failed: number;
|
|
388
|
+
readonly timedOut: number;
|
|
389
|
+
};
|
|
390
|
+
/**
|
|
391
|
+
* Creates a worker pool for CPU-intensive tasks.
|
|
392
|
+
*
|
|
393
|
+
* NOTE: This is a simplified implementation that uses setTimeout to simulate
|
|
394
|
+
* async execution. For real worker_threads support, the pool would need to
|
|
395
|
+
* spawn actual Worker instances. This provides the API contract and can be
|
|
396
|
+
* upgraded to real threads when needed.
|
|
397
|
+
*
|
|
398
|
+
* ```ts
|
|
399
|
+
* const pool = makeWorkerPool({ size: 4 });
|
|
400
|
+
*
|
|
401
|
+
* const result = await run(pool.execute(() => heavyComputation()));
|
|
402
|
+
*
|
|
403
|
+
* await pool.shutdown();
|
|
404
|
+
* ```
|
|
405
|
+
*/
|
|
406
|
+
declare function makeWorkerPool(config?: WorkerPoolConfig): WorkerPool;
|
|
407
|
+
|
|
408
|
+
type LogLevel = "debug" | "info" | "warn" | "error";
|
|
409
|
+
declare function consoleJsonLogger(): RuntimeHooks;
|
|
410
|
+
|
|
411
|
+
declare function dumpAllFibers(reg: RuntimeRegistry): string;
|
|
412
|
+
|
|
413
|
+
/**
|
|
414
|
+
* Base type for tagged errors. All errors should extend this pattern:
|
|
415
|
+
*
|
|
416
|
+
* ```ts
|
|
417
|
+
* type NetworkError = { _tag: "NetworkError"; url: string; status: number };
|
|
418
|
+
* type TimeoutError = { _tag: "TimeoutError"; ms: number };
|
|
419
|
+
* type AppError = NetworkError | TimeoutError;
|
|
420
|
+
* ```
|
|
421
|
+
*/
|
|
422
|
+
type TaggedError = {
|
|
423
|
+
readonly _tag: string;
|
|
424
|
+
};
|
|
425
|
+
/**
|
|
426
|
+
* Catches a specific error by its `_tag` field and handles it.
|
|
427
|
+
* Other errors pass through unchanged.
|
|
428
|
+
*
|
|
429
|
+
* ```ts
|
|
430
|
+
* const result = catchTag(effect, "NetworkError", (e) => fallbackValue);
|
|
431
|
+
* // e is narrowed to NetworkError
|
|
432
|
+
* ```
|
|
433
|
+
*/
|
|
434
|
+
declare function catchTag<R, E extends TaggedError, A, Tag extends E["_tag"], B>(effect: Async<R, E, A>, tag: Tag, handler: (error: Extract<E, {
|
|
435
|
+
_tag: Tag;
|
|
436
|
+
}>) => Async<R, Exclude<E, {
|
|
437
|
+
_tag: Tag;
|
|
438
|
+
}>, A | B>): Async<R, Exclude<E, {
|
|
439
|
+
_tag: Tag;
|
|
440
|
+
}>, A | B>;
|
|
441
|
+
/**
|
|
442
|
+
* Catches multiple error tags with a single handler map.
|
|
443
|
+
*
|
|
444
|
+
* ```ts
|
|
445
|
+
* const result = catchTags(effect, {
|
|
446
|
+
* NetworkError: (e) => asyncSucceed(defaultValue),
|
|
447
|
+
* TimeoutError: (e) => retry(effect),
|
|
448
|
+
* });
|
|
449
|
+
* ```
|
|
450
|
+
*/
|
|
451
|
+
declare function catchTags<R, E extends TaggedError, A, Handlers extends Partial<{
|
|
452
|
+
[K in E["_tag"]]: (error: Extract<E, {
|
|
453
|
+
_tag: K;
|
|
454
|
+
}>) => Async<R, any, any>;
|
|
455
|
+
}>>(effect: Async<R, E, A>, handlers: Handlers): Async<R, Exclude<E, {
|
|
456
|
+
_tag: keyof Handlers & string;
|
|
457
|
+
}>, A>;
|
|
458
|
+
/**
|
|
459
|
+
* Maps the error channel of an effect.
|
|
460
|
+
*
|
|
461
|
+
* ```ts
|
|
462
|
+
* const result = mapError(effect, (e) => ({ _tag: "Wrapped", cause: e }));
|
|
463
|
+
* ```
|
|
464
|
+
*/
|
|
465
|
+
declare function mapError<R, E, E2, A>(effect: Async<R, E, A>, f: (error: E) => E2): Async<R, E2, A>;
|
|
466
|
+
/**
|
|
467
|
+
* Wraps any error with a tag, making it part of a discriminated union.
|
|
468
|
+
*
|
|
469
|
+
* ```ts
|
|
470
|
+
* const typed = tagError(fetchData(), "NetworkError", (e) => ({ url, cause: e }));
|
|
471
|
+
* // Error type becomes { _tag: "NetworkError"; url: string; cause: unknown }
|
|
472
|
+
* ```
|
|
473
|
+
*/
|
|
474
|
+
declare function tagError<R, E, A, Tag extends string, Fields extends Record<string, unknown>>(effect: Async<R, E, A>, tag: Tag, enrich?: (error: E) => Fields): Async<R, {
|
|
475
|
+
_tag: Tag;
|
|
476
|
+
} & Fields, A>;
|
|
477
|
+
/**
|
|
478
|
+
* If the effect fails, run the fallback instead.
|
|
479
|
+
*
|
|
480
|
+
* ```ts
|
|
481
|
+
* const result = orElse(primary(), () => fallback());
|
|
482
|
+
* ```
|
|
483
|
+
*/
|
|
484
|
+
declare function orElse<R, E, A, R2, E2, B>(effect: Async<R, E, A>, fallback: (error: E) => Async<R2, E2, B>): Async<R & R2, E2, A | B>;
|
|
485
|
+
|
|
486
|
+
type SupervisorStrategy = "one-for-one" | "all-for-one";
|
|
487
|
+
type SupervisorEscalation = "ignore" | "shutdown";
|
|
488
|
+
type SupervisorRestartMode = "never" | "always" | "on-failure";
|
|
489
|
+
type SupervisedChildStatus = "running" | "restarting" | "succeeded" | "failed" | "interrupted";
|
|
490
|
+
type SupervisorRestartContext = {
|
|
491
|
+
readonly supervisorId: number;
|
|
492
|
+
readonly childId: number;
|
|
493
|
+
readonly name?: string;
|
|
494
|
+
readonly restartCount: number;
|
|
495
|
+
readonly exit: Exit<any, any>;
|
|
496
|
+
};
|
|
497
|
+
type SupervisorRestartPolicy = SupervisorRestartMode | {
|
|
498
|
+
readonly mode?: SupervisorRestartMode;
|
|
499
|
+
readonly maxRestarts?: number;
|
|
500
|
+
readonly withinMs?: number;
|
|
501
|
+
readonly delayMs?: number | ((context: SupervisorRestartContext) => number);
|
|
502
|
+
readonly schedule?: Schedule<SupervisorRestartContext, unknown>;
|
|
503
|
+
};
|
|
504
|
+
type SupervisorEvent = {
|
|
505
|
+
readonly type: "child-start" | "child-end" | "child-restart" | "child-escalate" | "shutdown";
|
|
506
|
+
readonly supervisorId: number;
|
|
507
|
+
readonly childId?: number;
|
|
508
|
+
readonly name?: string;
|
|
509
|
+
readonly status?: SupervisedChildStatus;
|
|
510
|
+
readonly restartCount?: number;
|
|
511
|
+
readonly delayMs?: number;
|
|
512
|
+
readonly exit?: Exit<any, any>;
|
|
513
|
+
readonly reason?: string;
|
|
514
|
+
};
|
|
515
|
+
type SupervisorConfig = {
|
|
516
|
+
readonly strategy?: SupervisorStrategy;
|
|
517
|
+
readonly restart?: SupervisorRestartPolicy;
|
|
518
|
+
readonly escalation?: SupervisorEscalation;
|
|
519
|
+
readonly clock?: () => number;
|
|
520
|
+
readonly onEvent?: (event: SupervisorEvent) => void;
|
|
521
|
+
};
|
|
522
|
+
type SupervisedChildSpec<R, E, A> = {
|
|
523
|
+
readonly name?: string;
|
|
524
|
+
readonly effect: Async<R, E, A> | (() => Async<R, E, A>);
|
|
525
|
+
readonly restart?: SupervisorRestartPolicy;
|
|
526
|
+
};
|
|
527
|
+
type SupervisedFiber<E, A> = {
|
|
528
|
+
readonly id: number;
|
|
529
|
+
readonly name?: string;
|
|
530
|
+
readonly current: () => Fiber<E, A> | undefined;
|
|
531
|
+
readonly status: () => SupervisedChildStatus;
|
|
532
|
+
readonly restartCount: () => number;
|
|
533
|
+
readonly interrupt: () => void;
|
|
534
|
+
readonly join: (cb: (exit: Exit<E, A>) => void) => void;
|
|
535
|
+
};
|
|
536
|
+
declare class Supervisor<R> {
|
|
537
|
+
private readonly runtime;
|
|
538
|
+
readonly id: number;
|
|
539
|
+
private readonly records;
|
|
540
|
+
private readonly strategy;
|
|
541
|
+
private readonly restart;
|
|
542
|
+
private readonly escalation;
|
|
543
|
+
private readonly clock;
|
|
544
|
+
private readonly onEvent?;
|
|
545
|
+
private closed;
|
|
546
|
+
constructor(runtime: Runtime<R>, config?: SupervisorConfig);
|
|
547
|
+
start<E, A>(spec: SupervisedChildSpec<R, E, A>): SupervisedFiber<E, A>;
|
|
548
|
+
startAll<const Specs extends readonly SupervisedChildSpec<R, any, any>[]>(specs: Specs): {
|
|
549
|
+
[K in keyof Specs]: Specs[K] extends SupervisedChildSpec<R, infer E, infer A> ? SupervisedFiber<E, A> : never;
|
|
550
|
+
};
|
|
551
|
+
shutdown(): Async<R, never, void>;
|
|
552
|
+
private handle;
|
|
553
|
+
private launch;
|
|
554
|
+
private onChildExit;
|
|
555
|
+
private restartAll;
|
|
556
|
+
private scheduleRestart;
|
|
557
|
+
private nextDelay;
|
|
558
|
+
private escalate;
|
|
559
|
+
private finish;
|
|
560
|
+
private restartContext;
|
|
561
|
+
private emit;
|
|
562
|
+
}
|
|
563
|
+
declare function makeSupervisor<R>(runtime: Runtime<R>, config?: SupervisorConfig): Supervisor<R>;
|
|
564
|
+
declare function supervise<R, E, A>(runtime: Runtime<R>, spec: SupervisedChildSpec<R, E, A>, config?: SupervisorConfig): SupervisedFiber<E, A>;
|
|
565
|
+
declare function joinSupervised<E, A>(fiber: SupervisedFiber<E, A>): Async<unknown, E, A>;
|
|
566
|
+
|
|
567
|
+
export { Async, Exit, Fiber, type Layer, type LogLevel, type Ref, type RetryPolicy, type RetryState, Runtime, RuntimeHooks, RuntimeRegistry, Schedule, type Semaphore, type SemaphoreStats, type ShutdownConfig, type ShutdownStats, type SupervisedChildSpec, type SupervisedChildStatus, type SupervisedFiber, Supervisor, type SupervisorConfig, type SupervisorEscalation, type SupervisorEvent, type SupervisorRestartContext, type SupervisorRestartMode, type SupervisorRestartPolicy, type SupervisorStrategy, type TaggedError, type TestRuntimeOptions, type TimeoutError, type WorkerPool, type WorkerPoolConfig, type WorkerPoolError, type WorkerPoolStats, assertCompletesWithin, assertFails, assertFailsWith, assertSucceeds, catchTag, catchTags, compose as composeLayer, consoleJsonLogger, delayedEffect, derivedRef, dumpAllFibers, flakyEffect, gracefulShutdown, joinSupervised, layer, layerFail, layerFrom, layerSucceed, makeRef, makeSemaphore, makeSupervisor, makeTestRuntime, makeWorkerPool, mapError as mapErrorTyped, mapLayer, merge as mergeLayer, neverEffect, orElse, provideLayer, registerShutdownHooks, retry, retryN, retryWithBackoff, sleep, supervise, tagError, timeout };
|