tryharder 0.0.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/LICENSE +21 -0
- package/README.md +468 -0
- package/dist/errors.d.ts +22 -0
- package/dist/errors.js +17 -0
- package/dist/index.d.ts +269 -0
- package/dist/index.js +1223 -0
- package/dist/shared/chunk-996eswzx.js +105 -0
- package/dist/types.d.ts +17 -0
- package/dist/types.js +3 -0
- package/package.json +71 -0
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,269 @@
|
|
|
1
|
+
/** @internal */
|
|
2
|
+
declare class ControlError extends Error {}
|
|
3
|
+
declare class CancellationError extends ControlError {
|
|
4
|
+
constructor(message?: string, options?: ErrorOptions);
|
|
5
|
+
}
|
|
6
|
+
declare class TimeoutError extends ControlError {
|
|
7
|
+
constructor(message?: string, options?: ErrorOptions);
|
|
8
|
+
}
|
|
9
|
+
declare class RetryExhaustedError extends Error {
|
|
10
|
+
constructor(message?: string, options?: ErrorOptions);
|
|
11
|
+
}
|
|
12
|
+
declare class UnhandledException extends Error {
|
|
13
|
+
constructor(message?: string, options?: ErrorOptions);
|
|
14
|
+
}
|
|
15
|
+
type TaskRecord = Record<string, any>;
|
|
16
|
+
type TaskValidation<T extends TaskRecord> = { [K in keyof T] : T[K] extends (...args: any[]) => any ? T[K] : never };
|
|
17
|
+
type TaskResult<T> = T extends (...args: any[]) => infer R ? Awaited<R> : never;
|
|
18
|
+
type InferredTaskContext<T extends TaskRecord> = {
|
|
19
|
+
$result: { readonly [K in keyof T] : ReturnType<T[K]> extends Promise<infer R> ? Promise<R> : Promise<ReturnType<T[K]>> };
|
|
20
|
+
$signal: AbortSignal;
|
|
21
|
+
$disposer: AsyncDisposableStack;
|
|
22
|
+
};
|
|
23
|
+
type AllValue<T extends TaskRecord> = { [K in keyof T] : TaskResult<T[K]> };
|
|
24
|
+
interface AllCatchContext<T extends TaskRecord> {
|
|
25
|
+
failedTask: (keyof T & string) | undefined;
|
|
26
|
+
partial: Partial<AllValue<T>>;
|
|
27
|
+
signal: AbortSignal;
|
|
28
|
+
}
|
|
29
|
+
type AllCatchFn<
|
|
30
|
+
T extends TaskRecord,
|
|
31
|
+
C
|
|
32
|
+
> = (error: unknown, ctx: AllCatchContext<T>) => C | Promise<C>;
|
|
33
|
+
interface AllOptions<
|
|
34
|
+
T extends TaskRecord,
|
|
35
|
+
C
|
|
36
|
+
> {
|
|
37
|
+
catch: AllCatchFn<T, C>;
|
|
38
|
+
}
|
|
39
|
+
interface SettledFulfilled<T> {
|
|
40
|
+
status: "fulfilled";
|
|
41
|
+
value: T;
|
|
42
|
+
}
|
|
43
|
+
interface SettledRejected {
|
|
44
|
+
status: "rejected";
|
|
45
|
+
reason: unknown;
|
|
46
|
+
}
|
|
47
|
+
type SettledResult<T> = SettledFulfilled<T> | SettledRejected;
|
|
48
|
+
type AllSettledResult<T extends TaskRecord> = { [K in keyof T] : SettledResult<TaskResult<T[K]>> };
|
|
49
|
+
interface RetryInfo {
|
|
50
|
+
attempt: number;
|
|
51
|
+
limit: number;
|
|
52
|
+
}
|
|
53
|
+
interface BaseTryCtx {
|
|
54
|
+
signal?: AbortSignal;
|
|
55
|
+
}
|
|
56
|
+
type TryCtxFor<HasRetry extends boolean> = BaseTryCtx & (HasRetry extends true ? {
|
|
57
|
+
retry: RetryInfo;
|
|
58
|
+
} : Record<never, never>);
|
|
59
|
+
type TryCtx = TryCtxFor<true>;
|
|
60
|
+
type NonPromise<T> = T extends PromiseLike<unknown> ? never : T;
|
|
61
|
+
interface BaseRetryPolicy {
|
|
62
|
+
/**
|
|
63
|
+
* Maximum number of attempts, including the first run.
|
|
64
|
+
*/
|
|
65
|
+
limit: number;
|
|
66
|
+
/**
|
|
67
|
+
* Delay in milliseconds between attempts. Defaults to 0.
|
|
68
|
+
*/
|
|
69
|
+
delayMs?: number;
|
|
70
|
+
/**
|
|
71
|
+
* Adds random jitter to delays when enabled.
|
|
72
|
+
*/
|
|
73
|
+
jitter?: boolean;
|
|
74
|
+
/**
|
|
75
|
+
* Return true to retry after an error, false to stop.
|
|
76
|
+
*/
|
|
77
|
+
shouldRetry?: (error: unknown, ctx: TryCtx) => boolean;
|
|
78
|
+
}
|
|
79
|
+
interface LinearBackoffRetryPolicy extends BaseRetryPolicy {
|
|
80
|
+
/**
|
|
81
|
+
* Use linearly increasing delay between attempts.
|
|
82
|
+
*/
|
|
83
|
+
backoff: "linear";
|
|
84
|
+
/**
|
|
85
|
+
* Not supported for linear backoff.
|
|
86
|
+
*/
|
|
87
|
+
maxDelayMs?: never;
|
|
88
|
+
}
|
|
89
|
+
interface ExponentialBackoffRetryPolicy extends BaseRetryPolicy {
|
|
90
|
+
/**
|
|
91
|
+
* Use exponential delay growth between attempts.
|
|
92
|
+
*/
|
|
93
|
+
backoff: "exponential";
|
|
94
|
+
/**
|
|
95
|
+
* Optional cap for exponential delay in milliseconds.
|
|
96
|
+
*/
|
|
97
|
+
maxDelayMs?: number;
|
|
98
|
+
}
|
|
99
|
+
/**
|
|
100
|
+
* Retry policy using a constant delay strategy.
|
|
101
|
+
*/
|
|
102
|
+
interface ConstantBackoffRetryPolicy extends BaseRetryPolicy {
|
|
103
|
+
/**
|
|
104
|
+
* Required discriminant: use a fixed delay between attempts.
|
|
105
|
+
*/
|
|
106
|
+
backoff: "constant";
|
|
107
|
+
/**
|
|
108
|
+
* Not supported for constant backoff.
|
|
109
|
+
*/
|
|
110
|
+
maxDelayMs?: never;
|
|
111
|
+
}
|
|
112
|
+
/**
|
|
113
|
+
* Retry configuration object.
|
|
114
|
+
*/
|
|
115
|
+
type RetryPolicy = LinearBackoffRetryPolicy | ExponentialBackoffRetryPolicy | ConstantBackoffRetryPolicy;
|
|
116
|
+
/**
|
|
117
|
+
* Retry shorthand or full retry configuration.
|
|
118
|
+
*
|
|
119
|
+
* - `number`: attempt limit
|
|
120
|
+
* - `RetryPolicy`: detailed retry settings
|
|
121
|
+
*/
|
|
122
|
+
type RetryOptions = number | RetryPolicy;
|
|
123
|
+
declare function retryOptions(policy: RetryOptions): RetryPolicy;
|
|
124
|
+
declare const FLOW_EXIT_BRAND: unique symbol;
|
|
125
|
+
type FlowExit<T> = {
|
|
126
|
+
readonly [FLOW_EXIT_BRAND]: T;
|
|
127
|
+
};
|
|
128
|
+
type ExitValue<T> = T extends FlowExit<infer V> ? V : never;
|
|
129
|
+
type FlowExitValue<T extends TaskRecord> = { [K in keyof T] : ExitValue<TaskResult<T[K]>> }[keyof T];
|
|
130
|
+
type InferredFlowTaskContext<T extends TaskRecord> = InferredTaskContext<T> & {
|
|
131
|
+
$exit<V>(value: V): FlowExit<V>;
|
|
132
|
+
};
|
|
133
|
+
type FlowResult<T extends TaskRecord> = FlowExitValue<T>;
|
|
134
|
+
type RunTryFn<
|
|
135
|
+
T,
|
|
136
|
+
Ctx extends BaseTryCtx = BaseTryCtx
|
|
137
|
+
> = (ctx: Ctx) => NonPromise<T> | Promise<T>;
|
|
138
|
+
type RunCatchFn<E> = (error: unknown) => NonPromise<E> | Promise<E>;
|
|
139
|
+
interface RunAsyncOptions<
|
|
140
|
+
T,
|
|
141
|
+
E,
|
|
142
|
+
Ctx extends BaseTryCtx = BaseTryCtx
|
|
143
|
+
> {
|
|
144
|
+
try: RunTryFn<T, Ctx>;
|
|
145
|
+
catch: RunCatchFn<E>;
|
|
146
|
+
}
|
|
147
|
+
type AsyncRunInput<
|
|
148
|
+
T,
|
|
149
|
+
E,
|
|
150
|
+
Ctx extends BaseTryCtx = BaseTryCtx
|
|
151
|
+
> = RunTryFn<T, Ctx> | RunAsyncOptions<T, E, Ctx>;
|
|
152
|
+
type SyncRunTryFn<
|
|
153
|
+
T,
|
|
154
|
+
Ctx extends BaseTryCtx = BaseTryCtx
|
|
155
|
+
> = (ctx: Ctx) => NonPromise<T>;
|
|
156
|
+
type SyncRunCatchFn<E> = (error: unknown) => NonPromise<E>;
|
|
157
|
+
interface SyncRunOptions<
|
|
158
|
+
T,
|
|
159
|
+
E,
|
|
160
|
+
Ctx extends BaseTryCtx = BaseTryCtx
|
|
161
|
+
> {
|
|
162
|
+
try: SyncRunTryFn<T, Ctx>;
|
|
163
|
+
catch: SyncRunCatchFn<E>;
|
|
164
|
+
}
|
|
165
|
+
type SyncRunInput<
|
|
166
|
+
T,
|
|
167
|
+
E,
|
|
168
|
+
Ctx extends BaseTryCtx = BaseTryCtx
|
|
169
|
+
> = SyncRunTryFn<T, Ctx> | SyncRunOptions<T, E, Ctx>;
|
|
170
|
+
/**
|
|
171
|
+
* Wraps are observational hooks: they can inspect execution context and
|
|
172
|
+
* surround execution, but they must not mutate context or replace it.
|
|
173
|
+
*/
|
|
174
|
+
type WrapCtx = Readonly<Omit<TryCtx, "retry">> & {
|
|
175
|
+
readonly retry: Readonly<TryCtx["retry"]>;
|
|
176
|
+
};
|
|
177
|
+
type WrapFn = (ctx: WrapCtx, next: () => unknown) => unknown;
|
|
178
|
+
interface BuilderConfig {
|
|
179
|
+
/**
|
|
180
|
+
* Retry configuration applied to the run.
|
|
181
|
+
*/
|
|
182
|
+
retry?: RetryPolicy;
|
|
183
|
+
/**
|
|
184
|
+
* Timeout configuration applied to the run.
|
|
185
|
+
*/
|
|
186
|
+
timeout?: number;
|
|
187
|
+
/**
|
|
188
|
+
* Abort signals used to cancel execution.
|
|
189
|
+
*/
|
|
190
|
+
signals?: AbortSignal[];
|
|
191
|
+
/**
|
|
192
|
+
* Wrapper middleware chain around execution.
|
|
193
|
+
*/
|
|
194
|
+
wraps?: WrapFn[];
|
|
195
|
+
}
|
|
196
|
+
type ConfigRunErrors = RetryExhaustedError | TimeoutError | CancellationError;
|
|
197
|
+
type OrchestrationMethods = "all" | "allSettled" | "flow";
|
|
198
|
+
type SignalBuilderHiddenMethods<SupportsOrchestration extends boolean> = "runSync" | "wrap" | (SupportsOrchestration extends true ? never : OrchestrationMethods);
|
|
199
|
+
type ExecutionBuilderSurface<
|
|
200
|
+
E extends ConfigRunErrors,
|
|
201
|
+
HasRetry extends boolean
|
|
202
|
+
> = Omit<RunBuilder<E, HasRetry, false>, OrchestrationMethods | "wrap">;
|
|
203
|
+
type AsyncExecutionBuilderSurface<
|
|
204
|
+
E extends ConfigRunErrors,
|
|
205
|
+
HasRetry extends boolean
|
|
206
|
+
> = Omit<RunBuilder<E, HasRetry, false>, OrchestrationMethods | "runSync" | "wrap">;
|
|
207
|
+
type SignalBuilderSurface<
|
|
208
|
+
E extends ConfigRunErrors,
|
|
209
|
+
HasRetry extends boolean,
|
|
210
|
+
SupportsOrchestration extends boolean
|
|
211
|
+
> = Omit<RunBuilder<E, HasRetry, SupportsOrchestration>, SignalBuilderHiddenMethods<SupportsOrchestration>>;
|
|
212
|
+
declare class RunBuilder<
|
|
213
|
+
E extends ConfigRunErrors = never,
|
|
214
|
+
HasRetry extends boolean = false,
|
|
215
|
+
SupportsOrchestration extends boolean = true
|
|
216
|
+
> {
|
|
217
|
+
protected readonly config: BuilderConfig;
|
|
218
|
+
constructor(config?: BuilderConfig);
|
|
219
|
+
protected buildRetryConfig(policy: RetryOptions): BuilderConfig;
|
|
220
|
+
protected buildTimeoutConfig(ms: number): BuilderConfig;
|
|
221
|
+
protected buildSignalConfig(signal: AbortSignal): BuilderConfig;
|
|
222
|
+
retry(policy: number): ExecutionBuilderSurface<E | RetryExhaustedError, true>;
|
|
223
|
+
retry(policy: RetryOptions): AsyncExecutionBuilderSurface<E | RetryExhaustedError, true>;
|
|
224
|
+
timeout(ms: number): AsyncExecutionBuilderSurface<E | TimeoutError, HasRetry>;
|
|
225
|
+
signal(signal: AbortSignal): SignalBuilderSurface<E | CancellationError, HasRetry, SupportsOrchestration>;
|
|
226
|
+
wrap(fn: WrapFn): RunBuilder<E, HasRetry, SupportsOrchestration>;
|
|
227
|
+
run<T>(tryFn: RunTryFn<T, TryCtxFor<HasRetry>>): Promise<T | UnhandledException | E>;
|
|
228
|
+
run<
|
|
229
|
+
T,
|
|
230
|
+
C
|
|
231
|
+
>(options: AsyncRunInput<T, C, TryCtxFor<HasRetry>>): Promise<T | C | E>;
|
|
232
|
+
runSync<T>(tryFn: SyncRunTryFn<T, TryCtxFor<HasRetry>>): T | UnhandledException | E;
|
|
233
|
+
runSync<
|
|
234
|
+
T,
|
|
235
|
+
C
|
|
236
|
+
>(input: SyncRunInput<T, C, TryCtxFor<HasRetry>>): T | C | E;
|
|
237
|
+
all<
|
|
238
|
+
T extends TaskRecord,
|
|
239
|
+
C = never
|
|
240
|
+
>(tasks: T & TaskValidation<NoInfer<T>> & ThisType<InferredTaskContext<T>>, options?: AllOptions<T, C>): Promise<{ [K in keyof T] : TaskResult<T[K]> } | C>;
|
|
241
|
+
allSettled<T extends TaskRecord>(tasks: T & TaskValidation<NoInfer<T>> & ThisType<InferredTaskContext<T>>): Promise<AllSettledResult<T>>;
|
|
242
|
+
flow<T extends TaskRecord>(tasks: T & ThisType<InferredFlowTaskContext<T>>): Promise<FlowResult<T>>;
|
|
243
|
+
}
|
|
244
|
+
declare function dispose(): AsyncDisposableStack;
|
|
245
|
+
type GenUnwrap<T> = Exclude<Awaited<T>, Error>;
|
|
246
|
+
type GenUse = <T>(value: T) => Generator<T, GenUnwrap<T>, GenUnwrap<T>>;
|
|
247
|
+
type GenErrors<TYield> = Extract<Awaited<TYield>, Error>;
|
|
248
|
+
type CheckIsAsync<
|
|
249
|
+
TYield,
|
|
250
|
+
TReturn
|
|
251
|
+
> = Extract<TYield | TReturn, PromiseLike<unknown>> extends never ? false : true;
|
|
252
|
+
type GenResult<
|
|
253
|
+
TYield,
|
|
254
|
+
TReturn
|
|
255
|
+
> = CheckIsAsync<TYield, TReturn> extends true ? Promise<Awaited<TReturn> | GenErrors<TYield>> : Awaited<TReturn> | GenErrors<TYield>;
|
|
256
|
+
declare function driveGen<
|
|
257
|
+
TYield,
|
|
258
|
+
TReturn
|
|
259
|
+
>(factory: (useFn: GenUse) => Generator<TYield, TReturn, unknown>): GenResult<TYield, TReturn>;
|
|
260
|
+
declare const all: RunBuilder["all"];
|
|
261
|
+
declare const allSettled2: RunBuilder["allSettled"];
|
|
262
|
+
declare const flow: RunBuilder["flow"];
|
|
263
|
+
declare const retry: RunBuilder["retry"];
|
|
264
|
+
declare const run: RunBuilder["run"];
|
|
265
|
+
declare const runSync2: RunBuilder["runSync"];
|
|
266
|
+
declare const signal: RunBuilder["signal"];
|
|
267
|
+
declare const timeout: RunBuilder["timeout"];
|
|
268
|
+
declare const wrap: RunBuilder["wrap"];
|
|
269
|
+
export { wrap, timeout, signal, runSync2 as runSync, run, retryOptions, retry, driveGen as gen, flow, dispose, allSettled2 as allSettled, all };
|