awaitly 1.31.1 → 1.32.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/adapters.cjs +2 -2
- package/dist/adapters.cjs.map +1 -1
- package/dist/adapters.d.cts +3 -1
- package/dist/adapters.d.ts +3 -1
- package/dist/adapters.js +2 -2
- package/dist/adapters.js.map +1 -1
- package/dist/batch.cjs +2 -2
- package/dist/batch.cjs.map +1 -1
- package/dist/batch.d.cts +3 -1
- package/dist/batch.d.ts +3 -1
- package/dist/batch.js +2 -2
- package/dist/batch.js.map +1 -1
- package/dist/circuit-breaker.cjs +2 -2
- package/dist/circuit-breaker.cjs.map +1 -1
- package/dist/circuit-breaker.d.cts +3 -1
- package/dist/circuit-breaker.d.ts +3 -1
- package/dist/circuit-breaker.js +2 -2
- package/dist/circuit-breaker.js.map +1 -1
- package/dist/conditional.d.cts +3 -1
- package/dist/conditional.d.ts +3 -1
- package/dist/core.cjs +2 -2
- package/dist/core.cjs.map +1 -1
- package/dist/core.d.cts +2 -1
- package/dist/core.d.ts +2 -1
- package/dist/core.js +2 -2
- package/dist/core.js.map +1 -1
- package/dist/diagnostics.cjs +3 -3
- package/dist/diagnostics.cjs.map +1 -1
- package/dist/diagnostics.d.cts +3 -1
- package/dist/diagnostics.d.ts +3 -1
- package/dist/diagnostics.js +3 -3
- package/dist/diagnostics.js.map +1 -1
- package/dist/durable.cjs +3 -3
- package/dist/durable.cjs.map +1 -1
- package/dist/durable.d.cts +8 -361
- package/dist/durable.d.ts +8 -361
- package/dist/durable.js +3 -3
- package/dist/durable.js.map +1 -1
- package/dist/engine.cjs +11 -0
- package/dist/engine.cjs.map +1 -0
- package/dist/engine.d.cts +114 -0
- package/dist/engine.d.ts +114 -0
- package/dist/engine.js +11 -0
- package/dist/engine.js.map +1 -0
- package/dist/errors-entry-CMH73Eym.d.cts +339 -0
- package/dist/errors-entry-DOt5UUl4.d.ts +339 -0
- package/dist/errors.cjs +1 -1
- package/dist/errors.cjs.map +1 -1
- package/dist/errors.d.cts +2 -318
- package/dist/errors.d.ts +2 -318
- package/dist/errors.js +1 -1
- package/dist/errors.js.map +1 -1
- package/dist/fetch.cjs +2 -2
- package/dist/fetch.cjs.map +1 -1
- package/dist/fetch.d.cts +3 -1
- package/dist/fetch.d.ts +3 -1
- package/dist/fetch.js +2 -2
- package/dist/fetch.js.map +1 -1
- package/dist/functional.cjs +1 -1
- package/dist/functional.cjs.map +1 -1
- package/dist/functional.d.cts +2 -0
- package/dist/functional.d.ts +2 -0
- package/dist/functional.js +1 -1
- package/dist/functional.js.map +1 -1
- package/dist/{guards-PU64_GKv.d.cts → guards-B79mP5Q8.d.cts} +3 -3
- package/dist/{guards-B5lgMJq0.d.ts → guards-BUq6NJCM.d.ts} +3 -3
- package/dist/{hitl-Dyiy0R1v.d.cts → hitl-BCqkMAHw.d.cts} +2 -2
- package/dist/{hitl-BjeSm1sJ.d.ts → hitl-BZtPx0aU.d.ts} +2 -2
- package/dist/hitl.cjs +2 -2
- package/dist/hitl.cjs.map +1 -1
- package/dist/hitl.d.cts +9 -6
- package/dist/hitl.d.ts +9 -6
- package/dist/hitl.js +2 -2
- package/dist/hitl.js.map +1 -1
- package/dist/index-BVUAOWGG.d.ts +417 -0
- package/dist/index-CQnpmUcC.d.cts +417 -0
- package/dist/index.cjs +1 -1
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +4 -3
- package/dist/index.d.ts +4 -3
- package/dist/index.js +1 -1
- package/dist/index.js.map +1 -1
- package/dist/otel.d.cts +3 -1
- package/dist/otel.d.ts +3 -1
- package/dist/{persistence-entry-B3ukwbb5.d.ts → persistence-entry-ClFhxd2Q.d.ts} +11 -5
- package/dist/{persistence-entry-CgVzswbg.d.cts → persistence-entry-jflY61az.d.cts} +11 -5
- package/dist/persistence.d.cts +6 -3
- package/dist/persistence.d.ts +6 -3
- package/dist/policies.d.cts +3 -1
- package/dist/policies.d.ts +3 -1
- package/dist/ratelimit.cjs +2 -2
- package/dist/ratelimit.cjs.map +1 -1
- package/dist/ratelimit.d.cts +3 -1
- package/dist/ratelimit.d.ts +3 -1
- package/dist/ratelimit.js +2 -2
- package/dist/ratelimit.js.map +1 -1
- package/dist/reliability.cjs +2 -2
- package/dist/reliability.cjs.map +1 -1
- package/dist/reliability.d.cts +3 -1
- package/dist/reliability.d.ts +3 -1
- package/dist/reliability.js +2 -2
- package/dist/reliability.js.map +1 -1
- package/dist/resolver.cjs +2 -2
- package/dist/resolver.cjs.map +1 -1
- package/dist/resolver.d.cts +5 -3
- package/dist/resolver.d.ts +5 -3
- package/dist/resolver.js +2 -2
- package/dist/resolver.js.map +1 -1
- package/dist/resource.cjs +2 -2
- package/dist/resource.cjs.map +1 -1
- package/dist/resource.d.cts +3 -1
- package/dist/resource.d.ts +3 -1
- package/dist/resource.js +2 -2
- package/dist/resource.js.map +1 -1
- package/dist/result/retry.cjs +1 -1
- package/dist/result/retry.cjs.map +1 -1
- package/dist/result/retry.d.cts +2 -0
- package/dist/result/retry.d.ts +2 -0
- package/dist/result/retry.js +1 -1
- package/dist/result/retry.js.map +1 -1
- package/dist/result.cjs +1 -1
- package/dist/result.cjs.map +1 -1
- package/dist/result.d.cts +8 -23
- package/dist/result.d.ts +8 -23
- package/dist/result.js +1 -1
- package/dist/result.js.map +1 -1
- package/dist/{run-entry-C1uFytM6.d.cts → run-entry-rw7zll_G.d.ts} +16 -45
- package/dist/{run-entry-C1uFytM6.d.ts → run-entry-yQu63Yj9.d.cts} +16 -45
- package/dist/run.cjs +2 -2
- package/dist/run.cjs.map +1 -1
- package/dist/run.d.cts +3 -1
- package/dist/run.d.ts +3 -1
- package/dist/run.js +2 -2
- package/dist/run.js.map +1 -1
- package/dist/saga.cjs +2 -2
- package/dist/saga.cjs.map +1 -1
- package/dist/saga.d.cts +3 -1
- package/dist/saga.d.ts +3 -1
- package/dist/saga.js +2 -2
- package/dist/saga.js.map +1 -1
- package/dist/singleflight.d.cts +3 -1
- package/dist/singleflight.d.ts +3 -1
- package/dist/streaming.cjs +4 -4
- package/dist/streaming.cjs.map +1 -1
- package/dist/streaming.d.cts +5 -3
- package/dist/streaming.d.ts +5 -3
- package/dist/streaming.js +4 -4
- package/dist/streaming.js.map +1 -1
- package/dist/testing.cjs +8 -4
- package/dist/testing.cjs.map +1 -1
- package/dist/testing.d.cts +87 -4
- package/dist/testing.d.ts +87 -4
- package/dist/testing.js +8 -4
- package/dist/testing.js.map +1 -1
- package/dist/{types-wa_wTOn3.d.ts → types-CuWK5AlK.d.ts} +1 -1
- package/dist/{types-DUWrNIJu.d.cts → types-uR3JpwvF.d.cts} +1 -1
- package/dist/webhook.cjs +2 -2
- package/dist/webhook.cjs.map +1 -1
- package/dist/webhook.d.cts +7 -4
- package/dist/webhook.d.ts +7 -4
- package/dist/webhook.js +2 -2
- package/dist/webhook.js.map +1 -1
- package/dist/workflow.cjs +3 -3
- package/dist/workflow.cjs.map +1 -1
- package/dist/workflow.d.cts +47 -11
- package/dist/workflow.d.ts +47 -11
- package/dist/workflow.js +3 -3
- package/dist/workflow.js.map +1 -1
- package/package.json +15 -1
|
@@ -0,0 +1,417 @@
|
|
|
1
|
+
import { ad as WorkflowEvent, ae as RunStep, R as Result } from './run-entry-yQu63Yj9.cjs';
|
|
2
|
+
import { S as SnapshotStore, d as WorkflowSnapshot, e as AnyResultFn, E as ErrorsOfDeps, f as WorkflowContext, W as WorkflowCancelledError } from './persistence-entry-jflY61az.cjs';
|
|
3
|
+
import { U as UnexpectedError } from './errors-entry-CMH73Eym.cjs';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* awaitly/durable
|
|
7
|
+
*
|
|
8
|
+
* Durable execution with automatic state persistence.
|
|
9
|
+
* Workflows automatically checkpoint after each keyed step and can resume from any point.
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Error returned when workflow cannot resume due to version mismatch.
|
|
14
|
+
* Indicates the stored state was created with a different workflow version.
|
|
15
|
+
* Fail-fast contract: bump version when you change step keys, order, or outputs.
|
|
16
|
+
*/
|
|
17
|
+
type VersionMismatchError = {
|
|
18
|
+
type: "VERSION_MISMATCH";
|
|
19
|
+
/** Workflow execution ID */
|
|
20
|
+
workflowId: string;
|
|
21
|
+
/** Version stored in persisted state */
|
|
22
|
+
storedVersion: number;
|
|
23
|
+
/** Version requested by this run */
|
|
24
|
+
requestedVersion: number;
|
|
25
|
+
/** Guidance message with suggested actions */
|
|
26
|
+
message: string;
|
|
27
|
+
/** Use requestedVersion. */
|
|
28
|
+
currentVersion?: number;
|
|
29
|
+
};
|
|
30
|
+
/**
|
|
31
|
+
* Error returned when workflow execution is rejected due to concurrent run.
|
|
32
|
+
*/
|
|
33
|
+
type ConcurrentExecutionError = {
|
|
34
|
+
type: "CONCURRENT_EXECUTION";
|
|
35
|
+
/** The workflow ID that is already running */
|
|
36
|
+
workflowId: string;
|
|
37
|
+
/** Guidance message */
|
|
38
|
+
message: string;
|
|
39
|
+
/**
|
|
40
|
+
* Distinguishes in-process (activeWorkflows) from cross-process (lock held).
|
|
41
|
+
* Enables debuggability and branching without parsing the message.
|
|
42
|
+
*/
|
|
43
|
+
reason?: "in-process" | "cross-process";
|
|
44
|
+
};
|
|
45
|
+
/**
|
|
46
|
+
* Error returned when a persistence store operation fails.
|
|
47
|
+
*/
|
|
48
|
+
type PersistenceError = {
|
|
49
|
+
type: "PERSISTENCE_ERROR";
|
|
50
|
+
/** The operation that failed */
|
|
51
|
+
operation: "load" | "save" | "delete";
|
|
52
|
+
/** The workflow ID */
|
|
53
|
+
workflowId: string;
|
|
54
|
+
/** The underlying error */
|
|
55
|
+
cause: unknown;
|
|
56
|
+
/** Guidance message */
|
|
57
|
+
message: string;
|
|
58
|
+
};
|
|
59
|
+
/**
|
|
60
|
+
* Type guard to check if an error is a VersionMismatchError.
|
|
61
|
+
*/
|
|
62
|
+
declare function isVersionMismatch(error: unknown): error is VersionMismatchError;
|
|
63
|
+
/**
|
|
64
|
+
* Type guard to check if an error is a ConcurrentExecutionError.
|
|
65
|
+
*/
|
|
66
|
+
declare function isConcurrentExecution(error: unknown): error is ConcurrentExecutionError;
|
|
67
|
+
/**
|
|
68
|
+
* Type guard to check if an error is a PersistenceError.
|
|
69
|
+
*/
|
|
70
|
+
declare function isPersistenceError(error: unknown): error is PersistenceError;
|
|
71
|
+
/**
|
|
72
|
+
* Error returned when a workflow's lease expires mid-execution.
|
|
73
|
+
* Indicates the lock was lost and another process may have reclaimed the workflow.
|
|
74
|
+
*/
|
|
75
|
+
type LeaseExpiredError = {
|
|
76
|
+
type: "LEASE_EXPIRED";
|
|
77
|
+
/** The workflow ID whose lease expired */
|
|
78
|
+
workflowId: string;
|
|
79
|
+
/** Guidance message */
|
|
80
|
+
message: string;
|
|
81
|
+
};
|
|
82
|
+
/**
|
|
83
|
+
* Type guard to check if an error is a LeaseExpiredError.
|
|
84
|
+
*/
|
|
85
|
+
declare function isLeaseExpired(error: unknown): error is LeaseExpiredError;
|
|
86
|
+
/**
|
|
87
|
+
* Error returned when an idempotency key is reused with different input.
|
|
88
|
+
*/
|
|
89
|
+
type IdempotencyConflictError = {
|
|
90
|
+
type: "IDEMPOTENCY_CONFLICT";
|
|
91
|
+
idempotencyKey: string;
|
|
92
|
+
workflowId: string;
|
|
93
|
+
message: string;
|
|
94
|
+
};
|
|
95
|
+
declare function isIdempotencyConflict(error: unknown): error is IdempotencyConflictError;
|
|
96
|
+
/**
|
|
97
|
+
* Optional cross-process lock interface.
|
|
98
|
+
* When a store implements this, durable.run uses it to ensure only one process
|
|
99
|
+
* runs a given workflow ID at a time (when allowConcurrent is false).
|
|
100
|
+
*
|
|
101
|
+
* Uses a lease (TTL) + owner token so a crashed worker does not wedge a workflow
|
|
102
|
+
* indefinitely. Release must verify the owner token so one process never
|
|
103
|
+
* unlocks another's lease.
|
|
104
|
+
*
|
|
105
|
+
* Mid-run lock loss (e.g. lease expires during a long workflow) is handled
|
|
106
|
+
* adapter-side; core assumes the lock is held until release in finally.
|
|
107
|
+
* Adapters may implement heartbeats/renewal.
|
|
108
|
+
*/
|
|
109
|
+
interface WorkflowLock {
|
|
110
|
+
/**
|
|
111
|
+
* Try to acquire a lease for the workflow ID.
|
|
112
|
+
* @param id - Workflow execution ID
|
|
113
|
+
* @param options - Optional TTL for the lease (adapter default if omitted)
|
|
114
|
+
* @returns Owner token if acquired, null if already held by another
|
|
115
|
+
*/
|
|
116
|
+
tryAcquire(id: string, options?: {
|
|
117
|
+
ttlMs?: number;
|
|
118
|
+
}): Promise<{
|
|
119
|
+
ownerToken: string;
|
|
120
|
+
} | null>;
|
|
121
|
+
/**
|
|
122
|
+
* Release the lease. Must verify owner token; no-op or ignore if token
|
|
123
|
+
* does not match (e.g. lease already expired or taken by another).
|
|
124
|
+
*/
|
|
125
|
+
release(id: string, ownerToken: string): Promise<void>;
|
|
126
|
+
/**
|
|
127
|
+
* Extend the lease for an already-held lock.
|
|
128
|
+
* Returns true if renewed, false if lost.
|
|
129
|
+
* Optional — when not implemented, no heartbeat runs.
|
|
130
|
+
*/
|
|
131
|
+
renew?(id: string, ownerToken: string, options?: {
|
|
132
|
+
ttlMs?: number;
|
|
133
|
+
}): Promise<boolean>;
|
|
134
|
+
}
|
|
135
|
+
/**
|
|
136
|
+
* Options for durable workflow execution.
|
|
137
|
+
*/
|
|
138
|
+
interface DurableOptions<C = void> {
|
|
139
|
+
/**
|
|
140
|
+
* Unique workflow execution ID.
|
|
141
|
+
* Used as the key for state persistence.
|
|
142
|
+
*
|
|
143
|
+
* @example 'order-checkout-123', 'user-onboarding-abc'
|
|
144
|
+
*/
|
|
145
|
+
id: string;
|
|
146
|
+
/**
|
|
147
|
+
* Snapshot store for persistence. Optional. When omitted, an in-memory store is used (per process).
|
|
148
|
+
* Same-process resume/retry works; state is lost on restart. Override with postgres/mongo/libsql for persistence.
|
|
149
|
+
*
|
|
150
|
+
* @example
|
|
151
|
+
* ```typescript
|
|
152
|
+
* // Zero-config: uses in-memory store (per process)
|
|
153
|
+
* await durable.run(deps, fn, { id: 'my-id' });
|
|
154
|
+
*
|
|
155
|
+
* // Override: pass a store for persistence across restarts
|
|
156
|
+
* import { postgres } from 'awaitly-postgres';
|
|
157
|
+
* const store = postgres('postgresql://localhost/mydb');
|
|
158
|
+
* await durable.run(deps, fn, { id: 'my-id', store });
|
|
159
|
+
* ```
|
|
160
|
+
*/
|
|
161
|
+
store?: SnapshotStore;
|
|
162
|
+
/**
|
|
163
|
+
* Workflow logic version.
|
|
164
|
+
* If stored state has a different version, workflow will reject resume with VersionMismatchError
|
|
165
|
+
* unless onVersionMismatch is used to clear or migrate.
|
|
166
|
+
*
|
|
167
|
+
* Bump when you change step keys, reorder steps, or change step outputs in a way old checkpoints can't satisfy.
|
|
168
|
+
*
|
|
169
|
+
* @default 1
|
|
170
|
+
*/
|
|
171
|
+
version?: number;
|
|
172
|
+
/**
|
|
173
|
+
* When stored state version differs from requested version, either throw (default), clear state and run from scratch, or supply migrated snapshot.
|
|
174
|
+
* Use for migration or one-off clear without wrapping durable.run.
|
|
175
|
+
*
|
|
176
|
+
* @default 'throw'
|
|
177
|
+
*/
|
|
178
|
+
onVersionMismatch?: (ctx: {
|
|
179
|
+
id: string;
|
|
180
|
+
storedVersion: number;
|
|
181
|
+
requestedVersion: number;
|
|
182
|
+
}) => "throw" | "clear" | {
|
|
183
|
+
migratedSnapshot: WorkflowSnapshot;
|
|
184
|
+
} | Promise<"throw" | "clear" | {
|
|
185
|
+
migratedSnapshot: WorkflowSnapshot;
|
|
186
|
+
}>;
|
|
187
|
+
/**
|
|
188
|
+
* Allow concurrent executions with the same workflow ID.
|
|
189
|
+
* When `false` (default), a second run with the same ID will be rejected while one is active.
|
|
190
|
+
*
|
|
191
|
+
* @default false
|
|
192
|
+
*/
|
|
193
|
+
allowConcurrent?: boolean;
|
|
194
|
+
/**
|
|
195
|
+
* Lease TTL in milliseconds for cross-process locking.
|
|
196
|
+
* Only used when the store implements WorkflowLock and allowConcurrent is false.
|
|
197
|
+
* A crashed worker's lease expires after this duration so the workflow can be picked up again.
|
|
198
|
+
*
|
|
199
|
+
* @default 60000 (1 minute)
|
|
200
|
+
*/
|
|
201
|
+
lockTtlMs?: number;
|
|
202
|
+
/**
|
|
203
|
+
* Heartbeat interval for lease renewal (ms).
|
|
204
|
+
* Only active when store implements WorkflowLock with renew().
|
|
205
|
+
* @default lockTtlMs / 3
|
|
206
|
+
*/
|
|
207
|
+
heartbeatIntervalMs?: number;
|
|
208
|
+
/**
|
|
209
|
+
* Whether to abort the workflow when lease is lost mid-execution.
|
|
210
|
+
* @default true
|
|
211
|
+
*/
|
|
212
|
+
abortOnLeaseLoss?: boolean;
|
|
213
|
+
/**
|
|
214
|
+
* Metadata to store alongside workflow state.
|
|
215
|
+
* Useful for debugging, auditing, or filtering workflows.
|
|
216
|
+
*
|
|
217
|
+
* @example { userId: 'user-123', source: 'api' }
|
|
218
|
+
*/
|
|
219
|
+
metadata?: Record<string, unknown>;
|
|
220
|
+
/**
|
|
221
|
+
* External AbortSignal for workflow-level cancellation.
|
|
222
|
+
* Cancellation persists state up to the last completed step.
|
|
223
|
+
*/
|
|
224
|
+
signal?: AbortSignal;
|
|
225
|
+
/**
|
|
226
|
+
* Create per-run context for event correlation.
|
|
227
|
+
*/
|
|
228
|
+
createContext?: () => C;
|
|
229
|
+
/**
|
|
230
|
+
* Unified event stream for workflow and step lifecycle.
|
|
231
|
+
* Includes durable-specific events: `persist_success` and `persist_error`.
|
|
232
|
+
*/
|
|
233
|
+
onEvent?: (event: DurableWorkflowEvent<unknown, C>, ctx: C) => void;
|
|
234
|
+
/**
|
|
235
|
+
* Handler for expected and unexpected errors.
|
|
236
|
+
*/
|
|
237
|
+
onError?: (error: unknown, stepName?: string, ctx?: C) => void;
|
|
238
|
+
/**
|
|
239
|
+
* Idempotency key for deduplication.
|
|
240
|
+
* If provided and a completed workflow with this key exists in the store,
|
|
241
|
+
* the stored result is returned without re-execution.
|
|
242
|
+
* If the stored input differs, an IdempotencyConflictError is returned.
|
|
243
|
+
*/
|
|
244
|
+
idempotencyKey?: string;
|
|
245
|
+
/**
|
|
246
|
+
* Workflow input for idempotency conflict detection.
|
|
247
|
+
* When idempotencyKey is set, this is compared against stored input.
|
|
248
|
+
* Must be JSON-serializable.
|
|
249
|
+
*/
|
|
250
|
+
input?: unknown;
|
|
251
|
+
}
|
|
252
|
+
/**
|
|
253
|
+
* Extended workflow event type that includes durable-specific events.
|
|
254
|
+
* E is the deps error type - the full error type includes E | UnexpectedError.
|
|
255
|
+
*/
|
|
256
|
+
type DurableWorkflowEvent<E, C = void> = WorkflowEvent<E | UnexpectedError, C> | {
|
|
257
|
+
type: "persist_success";
|
|
258
|
+
workflowId: string;
|
|
259
|
+
stepKey: string;
|
|
260
|
+
ts: number;
|
|
261
|
+
context?: C;
|
|
262
|
+
} | {
|
|
263
|
+
type: "persist_error";
|
|
264
|
+
workflowId: string;
|
|
265
|
+
stepKey: string;
|
|
266
|
+
error: unknown;
|
|
267
|
+
ts: number;
|
|
268
|
+
context?: C;
|
|
269
|
+
};
|
|
270
|
+
/**
|
|
271
|
+
* Options for bulk delete of workflow state.
|
|
272
|
+
*/
|
|
273
|
+
interface DeleteStatesOptions {
|
|
274
|
+
/**
|
|
275
|
+
* Max number of concurrent delete calls when store has no deleteMany.
|
|
276
|
+
* @default 10
|
|
277
|
+
*/
|
|
278
|
+
concurrency?: number;
|
|
279
|
+
/**
|
|
280
|
+
* When true, collect errors and return them; when false, throw on first error.
|
|
281
|
+
* @default true
|
|
282
|
+
*/
|
|
283
|
+
continueOnError?: boolean;
|
|
284
|
+
}
|
|
285
|
+
/**
|
|
286
|
+
* Result of bulk delete of workflow state.
|
|
287
|
+
*/
|
|
288
|
+
interface DeleteStatesResult {
|
|
289
|
+
/** Number of entries successfully deleted. */
|
|
290
|
+
deleted: number;
|
|
291
|
+
/** Per-id errors when continueOnError was true and some deletes failed. */
|
|
292
|
+
errors?: Array<{
|
|
293
|
+
id: string;
|
|
294
|
+
error: unknown;
|
|
295
|
+
}>;
|
|
296
|
+
}
|
|
297
|
+
/**
|
|
298
|
+
* Durable workflow execution namespace.
|
|
299
|
+
*/
|
|
300
|
+
declare const durable: {
|
|
301
|
+
/**
|
|
302
|
+
* Execute a workflow with automatic state persistence.
|
|
303
|
+
*
|
|
304
|
+
* Features:
|
|
305
|
+
* - **Automatic checkpointing**: State is saved after each keyed step
|
|
306
|
+
* - **Crash recovery**: Resume from the last completed step on restart
|
|
307
|
+
* - **Version checking**: Reject resume if workflow logic version changed
|
|
308
|
+
* - **Concurrency control**: Prevent duplicate executions of the same workflow ID
|
|
309
|
+
* - **Cancellation support**: Integrates with AbortSignal for graceful shutdown
|
|
310
|
+
*
|
|
311
|
+
* ## How It Works
|
|
312
|
+
*
|
|
313
|
+
* 1. On start: Load existing state from store (if any)
|
|
314
|
+
* 2. Check version compatibility (reject if mismatch)
|
|
315
|
+
* 3. Pre-populate cache from loaded state (skip completed steps)
|
|
316
|
+
* 4. Execute workflow, persisting state after each keyed step
|
|
317
|
+
* 5. On completion: Delete stored state (clean up)
|
|
318
|
+
* 6. On error/cancellation: State remains for future resume
|
|
319
|
+
*
|
|
320
|
+
* ## Important Notes
|
|
321
|
+
*
|
|
322
|
+
* - **Only keyed steps are durable**: Use `{ key: 'step-name' }` option
|
|
323
|
+
* - **Steps should be idempotent**: They may be retried on resume
|
|
324
|
+
* - **Serialization**: State is JSON-serialized; complex objects may lose fidelity
|
|
325
|
+
*
|
|
326
|
+
* @param deps - Workflow dependencies (Result-returning functions)
|
|
327
|
+
* @param fn - Workflow function receiving ({ step, deps, ctx })
|
|
328
|
+
* @param options - Durable execution options
|
|
329
|
+
* @returns AsyncResult with workflow result or error
|
|
330
|
+
*
|
|
331
|
+
* @example
|
|
332
|
+
* ```typescript
|
|
333
|
+
* import { durable } from 'awaitly/durable';
|
|
334
|
+
*
|
|
335
|
+
* // Zero-config: uses in-memory store (per process)
|
|
336
|
+
* const result = await durable.run(
|
|
337
|
+
* { fetchUser, createOrder, sendEmail },
|
|
338
|
+
* async ({ step, deps: { fetchUser, createOrder, sendEmail } }) => {
|
|
339
|
+
* const user = await step(() => fetchUser('123'), { key: 'fetch-user' });
|
|
340
|
+
* const order = await step(() => createOrder(user), { key: 'create-order' });
|
|
341
|
+
* await step(() => sendEmail(order), { key: 'send-email' });
|
|
342
|
+
* return order;
|
|
343
|
+
* },
|
|
344
|
+
* { id: 'checkout-123' }
|
|
345
|
+
* );
|
|
346
|
+
*
|
|
347
|
+
* // Override: pass a store for persistence across restarts
|
|
348
|
+
* import { createMemoryStatePersistence } from 'awaitly/persistence';
|
|
349
|
+
* const store = createMemoryStatePersistence();
|
|
350
|
+
* await durable.run(deps, fn, { id: 'checkout-123', store });
|
|
351
|
+
*
|
|
352
|
+
* if (result.ok) {
|
|
353
|
+
* console.log('Order completed:', result.value);
|
|
354
|
+
* } else if (isWorkflowCancelled(result.error)) {
|
|
355
|
+
* console.log('Workflow cancelled at:', result.error.lastStepKey);
|
|
356
|
+
* }
|
|
357
|
+
* ```
|
|
358
|
+
*/
|
|
359
|
+
run<const Deps extends Readonly<Record<string, AnyResultFn>>, T, C = void>(deps: Deps, fn: (context: {
|
|
360
|
+
step: RunStep<ErrorsOfDeps<Deps>>;
|
|
361
|
+
deps: Deps;
|
|
362
|
+
ctx: WorkflowContext<C>;
|
|
363
|
+
}) => T | Promise<T>, options: DurableOptions<C>): Promise<Result<T, ErrorsOfDeps<Deps> | UnexpectedError | WorkflowCancelledError | VersionMismatchError | ConcurrentExecutionError | PersistenceError | LeaseExpiredError | IdempotencyConflictError, unknown>>;
|
|
364
|
+
/**
|
|
365
|
+
* Clear all persisted workflow state from the store.
|
|
366
|
+
* Use for admin/testing. If the store implements `clear()`, that is used;
|
|
367
|
+
* otherwise clears by listing and deleting in pages.
|
|
368
|
+
*
|
|
369
|
+
* @param store - Snapshot store
|
|
370
|
+
*/
|
|
371
|
+
clearState(store: SnapshotStore): Promise<void>;
|
|
372
|
+
/**
|
|
373
|
+
* Check if a workflow ID has persisted state (can be resumed).
|
|
374
|
+
*
|
|
375
|
+
* @param store - Snapshot store
|
|
376
|
+
* @param id - Workflow execution ID
|
|
377
|
+
* @returns `true` if state exists, `false` otherwise (including on store errors)
|
|
378
|
+
*/
|
|
379
|
+
hasState(store: SnapshotStore, id: string): Promise<boolean>;
|
|
380
|
+
/**
|
|
381
|
+
* Delete persisted state for a workflow (cancel resume capability).
|
|
382
|
+
* Deleting is effectively an ack/reset: the workflow can no longer resume from that state.
|
|
383
|
+
* If you delete while a run is in flight, the run continues; on success it may delete again (no-op) or save (recreating state).
|
|
384
|
+
* For multi-worker safety, prefer deleting only when the workflow is not running or when you hold the lock.
|
|
385
|
+
*
|
|
386
|
+
* @param store - Snapshot store
|
|
387
|
+
* @param id - Workflow execution ID
|
|
388
|
+
* @returns `true` on success, `false` on store errors
|
|
389
|
+
*/
|
|
390
|
+
deleteState(store: SnapshotStore, id: string): Promise<boolean>;
|
|
391
|
+
/**
|
|
392
|
+
* Bulk delete persisted state for multiple workflow IDs (best-effort).
|
|
393
|
+
* Use for admin/cleanup. Deletes in a loop with optional concurrency.
|
|
394
|
+
*
|
|
395
|
+
* @param store - Snapshot store
|
|
396
|
+
* @param ids - Workflow execution IDs to delete
|
|
397
|
+
* @param options - Optional concurrency and error handling
|
|
398
|
+
* @returns Count of deleted entries and any errors when continueOnError is true
|
|
399
|
+
*/
|
|
400
|
+
deleteStates(store: SnapshotStore, ids: string[], options?: DeleteStatesOptions): Promise<DeleteStatesResult>;
|
|
401
|
+
/**
|
|
402
|
+
* List workflow IDs with persisted state.
|
|
403
|
+
*
|
|
404
|
+
* @param store - Snapshot store
|
|
405
|
+
* @param options - Optional prefix and limit
|
|
406
|
+
* @returns Array of { id, updatedAt } entries
|
|
407
|
+
*/
|
|
408
|
+
listPending(store: SnapshotStore, options?: {
|
|
409
|
+
prefix?: string;
|
|
410
|
+
limit?: number;
|
|
411
|
+
}): Promise<Array<{
|
|
412
|
+
id: string;
|
|
413
|
+
updatedAt: string;
|
|
414
|
+
}>>;
|
|
415
|
+
};
|
|
416
|
+
|
|
417
|
+
export { type ConcurrentExecutionError as C, type DeleteStatesOptions as D, type IdempotencyConflictError as I, type LeaseExpiredError as L, type PersistenceError as P, type VersionMismatchError as V, type WorkflowLock as W, type DeleteStatesResult as a, type DurableOptions as b, type DurableWorkflowEvent as c, durable as d, isIdempotencyConflict as e, isLeaseExpired as f, isPersistenceError as g, isVersionMismatch as h, isConcurrentExecution as i };
|
package/dist/index.cjs
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
"use strict";var N=Object.defineProperty;var je=Object.getOwnPropertyDescriptor;var Ie=Object.getOwnPropertyNames;var _e=Object.prototype.hasOwnProperty;var De=(e,r)=>{for(var n in r)N(e,n,{get:r[n],enumerable:!0})},He=(e,r,n,t)=>{if(r&&typeof r=="object"||typeof r=="function")for(let o of Ie(r))!_e.call(e,o)&&o!==n&&N(e,o,{get:()=>r[o],enumerable:!(t=je(r,o))||t.enumerable});return e};var Ke=e=>He(N({},"__esModule",{value:!0}),e);var Ye={};De(Ye,{AWAITLY_CANCELLED:()=>M,AWAITLY_TIMEOUT:()=>V,AWAITLY_UNEXPECTED:()=>G,Awaitly:()=>We,DESERIALIZATION_ERROR:()=>f,PROMISE_REJECTED:()=>l,R:()=>S,TaggedError:()=>C,UNEXPECTED_ERROR:()=>d,UnwrapError:()=>R,all:()=>ye,allAsync:()=>ke,allSettled:()=>xe,allSettledAsync:()=>we,andThen:()=>ue,any:()=>me,anyAsync:()=>ge,bimap:()=>le,compose:()=>O,deserialize:()=>he,err:()=>s,flatMapAsync:()=>D,flatten:()=>ve,flow:()=>U,from:()=>Q,fromNullable:()=>ne,fromPromise:()=>ee,getOrElse:()=>A,getOrElseLazy:()=>m,hydrate:()=>Re,identity:()=>b,isErr:()=>T,isOk:()=>c,isPromiseRejectedError:()=>z,isSerializedResult:()=>de,isUnexpectedError:()=>y,map:()=>te,mapAsync:()=>B,mapError:()=>oe,mapErrorTry:()=>ie,mapTry:()=>ce,match:()=>se,matchError:()=>X,matchErrorPartial:()=>Oe,ok:()=>u,orElse:()=>Te,orElseAsync:()=>pe,partition:()=>Ae,pipe:()=>h,race:()=>I,recover:()=>Ce,recoverAsync:()=>fe,recoverWith:()=>w,runOrNull:()=>q,runOrThrow:()=>Y,runOrThrowAsync:()=>Z,runOrUndefined:()=>$,serialize:()=>Ue,tags:()=>L,tap:()=>ae,tapAsync:()=>F,tapError:()=>Ee,tapErrorAsync:()=>j,traverse:()=>_,traverseAsync:()=>H,traverseParallel:()=>K,tryAsync:()=>re,unwrap:()=>k,unwrapOr:()=>J,unwrapOrElse:()=>W,zip:()=>v,zipAsync:()=>Pe});module.exports=Ke(Ye);var be={};De(be,{AWAITLY_CANCELLED:()=>M,AWAITLY_TIMEOUT:()=>V,AWAITLY_UNEXPECTED:()=>G,DESERIALIZATION_ERROR:()=>f,PROMISE_REJECTED:()=>l,UNEXPECTED_ERROR:()=>d,UnwrapError:()=>R,all:()=>ye,allAsync:()=>ke,allSettled:()=>xe,allSettledAsync:()=>we,andThen:()=>ue,any:()=>me,anyAsync:()=>ge,bimap:()=>le,deserialize:()=>he,err:()=>s,flatten:()=>ve,from:()=>Q,fromNullable:()=>ne,fromPromise:()=>ee,hydrate:()=>Re,isErr:()=>T,isOk:()=>c,isPromiseRejectedError:()=>z,isSerializedResult:()=>de,isUnexpectedError:()=>y,map:()=>te,mapError:()=>oe,mapErrorTry:()=>ie,mapTry:()=>ce,match:()=>se,matchError:()=>X,matchErrorPartial:()=>Oe,ok:()=>u,orElse:()=>Te,orElseAsync:()=>pe,partition:()=>Ae,recover:()=>Ce,recoverAsync:()=>fe,runOrNull:()=>q,runOrThrow:()=>Y,runOrThrowAsync:()=>Z,runOrUndefined:()=>$,serialize:()=>Ue,tags:()=>L,tap:()=>ae,tapError:()=>Ee,tryAsync:()=>re,unwrap:()=>k,unwrapOr:()=>J,unwrapOrElse:()=>W,zip:()=>v,zipAsync:()=>Pe});var d="UNEXPECTED_ERROR",l="PROMISE_REJECTED",G="AWAITLY_UNEXPECTED",M="AWAITLY_CANCELLED",V="AWAITLY_TIMEOUT",L=(...e)=>e;function u(e){return{ok:!0,value:e}}function s(e,r){let n=r?.cause;return{ok:!1,error:e,...n!==void 0?{cause:n}:{}}}var c=e=>e.ok,T=e=>!e.ok,y=e=>typeof e=="object"&&e!==null&&"type"in e&&e.type===d,z=e=>typeof e=="object"&&e!==null&&"type"in e&&e.type===l;function X(e,r){if(y(e))return r.UNEXPECTED_ERROR(e);if(e==="UNEXPECTED_ERROR"){let n={type:d,cause:{type:"UNCAUGHT_EXCEPTION",thrown:e}};return r.UNEXPECTED_ERROR(n)}return r[e](e)}var R=class extends Error{error;cause;constructor(r){let n=typeof r.error=="string"?r.error:JSON.stringify(r.error);super(`Attempted to unwrap an Err: ${n}`),this.name="UnwrapError",this.error=r.error,this.cause=r.cause}},k=e=>{if(e.ok)return e.value;throw new R(e)},J=(e,r)=>e.ok?e.value:r,W=(e,r)=>e.ok?e.value:r(e.error,e.cause),Y=e=>k(e),Z=e=>Promise.resolve(e).then(k),q=e=>e.ok?e.value:null,$=e=>e.ok?e.value:void 0;function Q(e,r){try{return u(e())}catch(n){return r?s(r(n),{cause:n}):s(n)}}async function ee(e,r){try{return u(await e)}catch(n){return r?s(r(n),{cause:n}):s(n)}}async function re(e,r){try{return u(await e())}catch(n){return r?s(r(n),{cause:n}):s(n)}}function ne(e,r){return e!=null?u(e):s(r())}function te(e,r){return e.ok?u(r(e.value)):e}function oe(e,r){return e.ok?e:s(r(e.error,e.cause),{cause:e.cause})}function se(e,r){return e.ok?r.ok(e.value):r.err(e.error,e.cause)}function ue(e,r){return e.ok?r(e.value):e}function ae(e,r){return e.ok&&r(e.value),e}function Ee(e,r){return e.ok||r(e.error,e.cause),e}function ce(e,r,n){if(!e.ok)return e;try{return u(r(e.value))}catch(t){return s(n(t),{cause:t})}}function ie(e,r,n){if(e.ok)return e;try{return s(r(e.error),{cause:e.cause})}catch(t){return s(n(t),{cause:t})}}function le(e,r,n){return e.ok?u(r(e.value)):s(n(e.error,e.cause),{cause:e.cause})}function Te(e,r){return e.ok?e:r(e.error,e.cause)}async function pe(e,r){return e.ok?e:r(e.error,e.cause)}function Ce(e,r){return e.ok?u(e.value):u(r(e.error,e.cause))}async function fe(e,r){let n=await e;return n.ok?u(n.value):u(await r(n.error,n.cause))}function Re(e){if(typeof e!="object"||e===null||!("ok"in e))return null;let r=e;return r.ok===!0&&"value"in r?u(r.value):r.ok===!1&&"error"in r?s(r.error,{cause:r.cause}):null}function de(e){if(typeof e!="object"||e===null||!("ok"in e))return!1;let r=e;return r.ok===!0&&"value"in r||r.ok===!1&&"error"in r}function ye(e){let r=[];for(let n of e){if(!n.ok)return n;r.push(n.value)}return u(r)}async function ke(e){let r=[];for(let n of e)try{let t=await n;if(!t.ok)return t;r.push(t.value)}catch(t){return s({type:l,cause:t},{cause:{type:"PROMISE_REJECTION",reason:t}})}return u(r)}function xe(e){let r=[],n=[];for(let t of e)t.ok?r.push(t.value):n.push({error:t.error,cause:t.cause});return n.length>0?s(n):u(r)}async function we(e){let r=await Promise.all(e.map(o=>Promise.resolve(o).then(a=>({status:"result",result:a})).catch(a=>({status:"rejected",error:{type:l,cause:a},cause:{type:"PROMISE_REJECTION",reason:a}})))),n=[],t=[];for(let o of r)o.status==="rejected"?t.push({error:o.error,cause:o.cause}):o.result.ok?n.push(o.result.value):t.push({error:o.result.error,cause:o.result.cause});return t.length>0?s(t):u(n)}function Ae(e){let r=[],n=[];for(let t of e)t.ok?r.push(t.value):n.push(t.error);return{values:r,errors:n}}function me(e){if(e.length===0)return s({type:"EMPTY_INPUT",message:"any() requires at least one Result"});let r;for(let n of e){if(n.ok)return n;r||(r=n)}return r}async function ge(e){return e.length===0?s({type:"EMPTY_INPUT",message:"anyAsync() requires at least one Result"}):new Promise(r=>{let n=!1,t=e.length,o=null;for(let a of e)Promise.resolve(a).catch(E=>s({type:l,cause:E},{cause:{type:"PROMISE_REJECTION",reason:E}})).then(E=>{if(!n){if(E.ok){n=!0,r(E);return}o||(o=E),t--,t===0&&r(o)}})})}function v(e,r){return e.ok?r.ok?u([e.value,r.value]):r:e}async function Pe(e,r){let n=a=>Promise.resolve(a).catch(E=>s({type:l,cause:E},{cause:{type:"PROMISE_REJECTION",reason:E}})),[t,o]=await Promise.all([n(e),n(r)]);return v(t,o)}function ve(e){return e.ok?e.value:e}var f="DESERIALIZATION_ERROR";function he(e){if(typeof e!="object"||e===null)return s({type:f,value:e});if(!("ok"in e))return s({type:f,value:e});let r=e;return r.ok===!0&&"value"in r?u(r.value):r.ok===!1&&"error"in r?s(r.error,{cause:r.cause}):s({type:f,value:e})}function Ue(e){return e.ok?{ok:!0,value:e.value}:{ok:!1,error:e.error}}function Oe(e,r,n){if(y(e)){let o=r.UNEXPECTED_ERROR;return o?o(e):n(e)}let t=r[e];return t?t(e):n(e)}var x=class extends Error{_tag};function C(e,r){return class extends x{_tag=e;constructor(n,t){let o=r?.message?r.message(n??{}):e;if(super(o),this.name=e,Object.setPrototypeOf(this,new.target.prototype),n&&typeof n=="object"){let{_tag:a,name:E,message:i,stack:g,...p}=n,P=Object.prototype.hasOwnProperty.call(p,"cause"),Fe=P?p.cause:void 0;P&&delete p.cause;let Be=t?.cause!==void 0;if(P&&Be)throw new TypeError("TaggedError: cannot provide 'cause' in props when also setting ErrorOptions.cause");Object.assign(this,p),P&&(this.cause=Fe),Be&&(this.cause=t?.cause)}else t?.cause!==void 0&&(this.cause=t.cause)}}}Object.defineProperty(C,Symbol.hasInstance,{value:e=>e instanceof x});(o=>{function e(a){return a instanceof Error}o.isError=e;function r(a){return a instanceof x}o.isTaggedError=r;function n(a,E){let i=a._tag,g=E[i];return g(a)}o.match=n;function t(a,E,i){let g=a._tag,p=E[g];return p?p(a):i(a)}o.matchPartial=t})(C||={});function h(e,...r){return r.reduce((n,t)=>t(n),e)}function U(...e){return r=>e.reduce((n,t)=>t(n),r)}function O(...e){return r=>e.reduceRight((n,t)=>t(n),r)}var b=e=>e;function Se(e,r){return c(e)?u(r(e.value)):e}function Ne(e,r){return c(e)?r(e.value):e}function Ge(e,r,n){return c(e)?u(r(e.value)):s(n(e.error),{cause:e.cause})}function Me(e,r){return T(e)?s(r(e.error),{cause:e.cause}):e}function Ve(e,r){return c(e)&&r(e.value),e}function Le(e,r){return T(e)&&r(e.error),e}function ze(e,r){return c(e)?r.ok(e.value):r.err(e.error,e.cause)}function Xe(e,r){return c(e)?e.value:r(e.error)}function w(e,r){return c(e)?e:r(e.error)}function A(e,r){return c(e)?e.value:r}function m(e,r){return c(e)?e.value:r()}async function B(e,r){let n=await e;return c(n)?u(await r(n.value)):n}async function D(e,r){let n=await e;return c(n)?r(n.value):n}async function F(e,r){let n=await e;return c(n)&&await r(n.value),n}async function j(e,r){let n=await e;return T(n)&&await r(n.error),n}async function Je(e){if(e.length===0)return u([]);let r=new Array(e.length),n=0,t=!1;return new Promise(o=>{e.forEach((a,E)=>{a.then(i=>{t||(T(i)?(t=!0,o(i)):(r[E]=i.value,n++,n===e.length&&(t=!0,o(u(r)))))},i=>{t||(t=!0,o(s({type:l,cause:i},{cause:{type:"PROMISE_REJECTION",reason:i}})))})})})}async function I(e){return Promise.race(e.map(r=>r.catch(n=>s({type:l,cause:n},{cause:{type:"PROMISE_REJECTION",reason:n}}))))}function _(e,r){let n=[];for(let t=0;t<e.length;t++){let o=r(e[t],t);if(T(o))return o;n.push(o.value)}return u(n)}async function H(e,r){let n=[];for(let t=0;t<e.length;t++){let o=await r(e[t],t);if(T(o))return o;n.push(o.value)}return u(n)}async function K(e,r){return Je(e.map((n,t)=>r(n,t)))}var S={map:e=>r=>Se(r,e),flatMap:e=>r=>Ne(r,e),bimap:(e,r)=>n=>Ge(n,e,r),mapError:e=>r=>Me(r,e),tap:e=>r=>Ve(r,e),tapError:e=>r=>Le(r,e),match:e=>r=>ze(r,e),recover:e=>r=>Xe(r,e),recoverWith:e=>r=>w(r,e),getOrElse:e=>r=>A(r,e),getOrElseLazy:e=>r=>m(r,e)};var We={...be,TaggedError:C,pipe:h,flow:U,compose:O,identity:b,R:S,recoverWith:w,getOrElse:A,getOrElseLazy:m,mapAsync:B,flatMapAsync:D,tapAsync:F,tapErrorAsync:j,race:I,traverse:_,traverseAsync:H,traverseParallel:K};0&&(module.exports={AWAITLY_CANCELLED,AWAITLY_TIMEOUT,AWAITLY_UNEXPECTED,Awaitly,DESERIALIZATION_ERROR,PROMISE_REJECTED,R,TaggedError,UNEXPECTED_ERROR,UnwrapError,all,allAsync,allSettled,allSettledAsync,andThen,any,anyAsync,bimap,compose,deserialize,err,flatMapAsync,flatten,flow,from,fromNullable,fromPromise,getOrElse,getOrElseLazy,hydrate,identity,isErr,isOk,isPromiseRejectedError,isSerializedResult,isUnexpectedError,map,mapAsync,mapError,mapErrorTry,mapTry,match,matchError,matchErrorPartial,ok,orElse,orElseAsync,partition,pipe,race,recover,recoverAsync,recoverWith,runOrNull,runOrThrow,runOrThrowAsync,runOrUndefined,serialize,tags,tap,tapAsync,tapError,tapErrorAsync,traverse,traverseAsync,traverseParallel,tryAsync,unwrap,unwrapOr,unwrapOrElse,zip,zipAsync});
|
|
1
|
+
"use strict";var S=Object.defineProperty;var ze=Object.getOwnPropertyDescriptor;var Le=Object.getOwnPropertyNames;var Ge=Object.prototype.hasOwnProperty;var De=(e,r)=>{for(var n in r)S(e,n,{get:r[n],enumerable:!0})},$e=(e,r,n,t)=>{if(r&&typeof r=="object"||typeof r=="function")for(let o of Le(r))!Ge.call(e,o)&&o!==n&&S(e,o,{get:()=>r[o],enumerable:!(t=ze(r,o))||t.enumerable});return e};var Je=e=>$e(S({},"__esModule",{value:!0}),e);var or={};De(or,{AWAITLY_CANCELLED:()=>V,AWAITLY_TIMEOUT:()=>z,AWAITLY_UNEXPECTED:()=>M,Awaitly:()=>tr,DESERIALIZATION_ERROR:()=>C,PROMISE_REJECTED:()=>T,R:()=>N,TaggedError:()=>i,UnexpectedError:()=>d,UnwrapError:()=>R,all:()=>ke,allAsync:()=>ye,allSettled:()=>xe,allSettledAsync:()=>me,andThen:()=>ue,any:()=>we,anyAsync:()=>Ae,bimap:()=>le,compose:()=>U,deserialize:()=>he,err:()=>s,flatMapAsync:()=>D,flatten:()=>ve,flow:()=>O,from:()=>Q,fromNullable:()=>ne,fromPromise:()=>ee,getOrElse:()=>g,getOrElseLazy:()=>w,hydrate:()=>Ce,identity:()=>b,isErr:()=>p,isOk:()=>c,isPromiseRejectedError:()=>G,isSerializedResult:()=>Re,isUnexpectedError:()=>y,map:()=>te,mapAsync:()=>B,mapError:()=>oe,mapErrorTry:()=>ce,mapTry:()=>Ee,match:()=>se,matchError:()=>$,matchErrorPartial:()=>Ue,ok:()=>u,orElse:()=>Te,orElseAsync:()=>pe,partition:()=>ge,pipe:()=>h,race:()=>_,recover:()=>fe,recoverAsync:()=>de,recoverWith:()=>m,runOrNull:()=>X,runOrThrow:()=>Y,runOrThrowAsync:()=>Z,runOrUndefined:()=>q,serialize:()=>Oe,tags:()=>L,tap:()=>ae,tapAsync:()=>F,tapError:()=>ie,tapErrorAsync:()=>j,traverse:()=>I,traverseAsync:()=>K,traverseParallel:()=>H,tryAsync:()=>re,unwrap:()=>x,unwrapOr:()=>J,unwrapOrElse:()=>W,zip:()=>v,zipAsync:()=>Pe});module.exports=Je(or);var be={};De(be,{AWAITLY_CANCELLED:()=>V,AWAITLY_TIMEOUT:()=>z,AWAITLY_UNEXPECTED:()=>M,DESERIALIZATION_ERROR:()=>C,PROMISE_REJECTED:()=>T,UnexpectedError:()=>d,UnwrapError:()=>R,all:()=>ke,allAsync:()=>ye,allSettled:()=>xe,allSettledAsync:()=>me,andThen:()=>ue,any:()=>we,anyAsync:()=>Ae,bimap:()=>le,deserialize:()=>he,err:()=>s,flatten:()=>ve,from:()=>Q,fromNullable:()=>ne,fromPromise:()=>ee,hydrate:()=>Ce,isErr:()=>p,isOk:()=>c,isPromiseRejectedError:()=>G,isSerializedResult:()=>Re,isUnexpectedError:()=>y,map:()=>te,mapError:()=>oe,mapErrorTry:()=>ce,mapTry:()=>Ee,match:()=>se,matchError:()=>$,matchErrorPartial:()=>Ue,ok:()=>u,orElse:()=>Te,orElseAsync:()=>pe,partition:()=>ge,recover:()=>fe,recoverAsync:()=>de,runOrNull:()=>X,runOrThrow:()=>Y,runOrThrowAsync:()=>Z,runOrUndefined:()=>q,serialize:()=>Oe,tags:()=>L,tap:()=>ae,tapError:()=>ie,tryAsync:()=>re,unwrap:()=>x,unwrapOr:()=>J,unwrapOrElse:()=>W,zip:()=>v,zipAsync:()=>Pe});var k=class extends Error{_tag};function i(e,r){return class extends k{_tag=e;constructor(n,t){let o=r?.message?r.message(n??{}):e;if(super(o),this.name=e,Object.setPrototypeOf(this,new.target.prototype),n&&typeof n=="object"){let{_tag:a,name:E,message:l,stack:A,...f}=n,P=Object.prototype.hasOwnProperty.call(f,"cause"),Ve=P?f.cause:void 0;P&&delete f.cause;let Be=t?.cause!==void 0;if(P&&Be)throw new TypeError("TaggedError: cannot provide 'cause' in props when also setting ErrorOptions.cause");Object.assign(this,f),P&&(this.cause=Ve),Be&&(this.cause=t?.cause)}else t?.cause!==void 0&&(this.cause=t.cause)}}}Object.defineProperty(i,Symbol.hasInstance,{value:e=>e instanceof k});(o=>{function e(a){return a instanceof Error}o.isError=e;function r(a){return a instanceof k}o.isTaggedError=r;function n(a,E){let l=a._tag,A=E[l];return A(a)}o.match=n;function t(a,E,l){let A=a._tag,f=E[A];return f?f(a):l(a)}o.matchPartial=t})(i||={});var Fe=class extends i("TimeoutError",{message:r=>r.operation?`TimeoutError: ${r.operation} timed out after ${r.ms}ms`:`TimeoutError: Operation timed out after ${r.ms}ms`}){},je=class extends i("RetryExhaustedError",{message:r=>r.operation?`RetryExhaustedError: ${r.operation} failed after ${r.attempts} attempts`:`RetryExhaustedError: Operation failed after ${r.attempts} attempts`}){},_e=class extends i("RateLimitError",{message:r=>r.limiterName?`RateLimitError: Rate limit exceeded for ${r.limiterName}${r.retryAfterMs?`, retry after ${r.retryAfterMs}ms`:""}`:`RateLimitError: Rate limit exceeded${r.retryAfterMs?`, retry after ${r.retryAfterMs}ms`:""}`}){},Ie=class extends i("CircuitBreakerOpenError",{message:r=>`CircuitBreakerOpenError: Circuit ${r.circuitName} is ${r.state??"OPEN"}${r.retryAfterMs?`, retry after ${Math.ceil(r.retryAfterMs/1e3)}s`:""}`}){},Ke=class extends i("ValidationError",{message:r=>`ValidationError: Invalid ${r.field} - ${r.reason}`}){},He=class extends i("NotFoundError",{message:r=>r.id?`NotFoundError: ${r.resource} with id ${r.id} not found`:`NotFoundError: ${r.resource} not found`}){},Ne=class extends i("UnauthorizedError",{message:r=>r.reason?`UnauthorizedError: ${r.reason}`:r.action&&r.resource?`UnauthorizedError: Not authorized to ${r.action} ${r.resource}`:"UnauthorizedError: Access denied"}){},Se=class extends i("NetworkError",{message:r=>r.url?`NetworkError: ${r.reason} (${r.url})`:`NetworkError: ${r.reason}`}){},Me=class extends i("CompensationError",{message:r=>`CompensationError: Failed to compensate step ${r.step}`}){},d=class extends i("UnexpectedError",{message:r=>`UnexpectedError: ${r.cause instanceof Error?r.cause.message:String(r.cause??"unknown")}`}){};var T="PROMISE_REJECTED",M="AWAITLY_UNEXPECTED",V="AWAITLY_CANCELLED",z="AWAITLY_TIMEOUT",L=(...e)=>e;function u(e){return{ok:!0,value:e}}function s(e,r){let n=r?.cause;return{ok:!1,error:e,...n!==void 0?{cause:n}:{}}}var c=e=>e.ok,p=e=>!e.ok,y=e=>e instanceof d||typeof e=="object"&&e!==null&&"_tag"in e&&e._tag==="UnexpectedError",G=e=>typeof e=="object"&&e!==null&&"type"in e&&e.type===T;function $(e,r){return y(e)?r.UnexpectedError(e):r[e](e)}var R=class extends Error{error;cause;constructor(r){let n=typeof r.error=="string"?r.error:JSON.stringify(r.error);super(`Attempted to unwrap an Err: ${n}`),this.name="UnwrapError",this.error=r.error,this.cause=r.cause}},x=e=>{if(e.ok)return e.value;throw new R(e)},J=(e,r)=>e.ok?e.value:r,W=(e,r)=>e.ok?e.value:r(e.error,e.cause),Y=e=>x(e),Z=e=>Promise.resolve(e).then(x),X=e=>e.ok?e.value:null,q=e=>e.ok?e.value:void 0;function Q(e,r){try{return u(e())}catch(n){return r?s(r(n),{cause:n}):s(n)}}async function ee(e,r){try{return u(await e)}catch(n){return r?s(r(n),{cause:n}):s(n)}}async function re(e,r){try{return u(await e())}catch(n){return r?s(r(n),{cause:n}):s(n)}}function ne(e,r){return e!=null?u(e):s(r())}function te(e,r){return e.ok?u(r(e.value)):e}function oe(e,r){return e.ok?e:s(r(e.error,e.cause),{cause:e.cause})}function se(e,r){return e.ok?r.ok(e.value):r.err(e.error,e.cause)}function ue(e,r){return e.ok?r(e.value):e}function ae(e,r){return e.ok&&r(e.value),e}function ie(e,r){return e.ok||r(e.error,e.cause),e}function Ee(e,r,n){if(!e.ok)return e;try{return u(r(e.value))}catch(t){return s(n(t),{cause:t})}}function ce(e,r,n){if(e.ok)return e;try{return s(r(e.error),{cause:e.cause})}catch(t){return s(n(t),{cause:t})}}function le(e,r,n){return e.ok?u(r(e.value)):s(n(e.error,e.cause),{cause:e.cause})}function Te(e,r){return e.ok?e:r(e.error,e.cause)}async function pe(e,r){return e.ok?e:r(e.error,e.cause)}function fe(e,r){return e.ok?u(e.value):u(r(e.error,e.cause))}async function de(e,r){let n=await e;return n.ok?u(n.value):u(await r(n.error,n.cause))}function Ce(e){if(typeof e!="object"||e===null||!("ok"in e))return null;let r=e;return r.ok===!0&&"value"in r?u(r.value):r.ok===!1&&"error"in r?s(r.error,{cause:r.cause}):null}function Re(e){if(typeof e!="object"||e===null||!("ok"in e))return!1;let r=e;return r.ok===!0&&"value"in r||r.ok===!1&&"error"in r}function ke(e){let r=[];for(let n of e){if(!n.ok)return n;r.push(n.value)}return u(r)}async function ye(e){let r=[];for(let n of e)try{let t=await n;if(!t.ok)return t;r.push(t.value)}catch(t){return s({type:T,cause:t},{cause:{type:"PROMISE_REJECTION",reason:t}})}return u(r)}function xe(e){let r=[],n=[];for(let t of e)t.ok?r.push(t.value):n.push({error:t.error,cause:t.cause});return n.length>0?s(n):u(r)}async function me(e){let r=await Promise.all(e.map(o=>Promise.resolve(o).then(a=>({status:"result",result:a})).catch(a=>({status:"rejected",error:{type:T,cause:a},cause:{type:"PROMISE_REJECTION",reason:a}})))),n=[],t=[];for(let o of r)o.status==="rejected"?t.push({error:o.error,cause:o.cause}):o.result.ok?n.push(o.result.value):t.push({error:o.result.error,cause:o.result.cause});return t.length>0?s(t):u(n)}function ge(e){let r=[],n=[];for(let t of e)t.ok?r.push(t.value):n.push(t.error);return{values:r,errors:n}}function we(e){if(e.length===0)return s({type:"EMPTY_INPUT",message:"any() requires at least one Result"});let r;for(let n of e){if(n.ok)return n;r||(r=n)}return r}async function Ae(e){return e.length===0?s({type:"EMPTY_INPUT",message:"anyAsync() requires at least one Result"}):new Promise(r=>{let n=!1,t=e.length,o=null;for(let a of e)Promise.resolve(a).catch(E=>s({type:T,cause:E},{cause:{type:"PROMISE_REJECTION",reason:E}})).then(E=>{if(!n){if(E.ok){n=!0,r(E);return}o||(o=E),t--,t===0&&r(o)}})})}function v(e,r){return e.ok?r.ok?u([e.value,r.value]):r:e}async function Pe(e,r){let n=a=>Promise.resolve(a).catch(E=>s({type:T,cause:E},{cause:{type:"PROMISE_REJECTION",reason:E}})),[t,o]=await Promise.all([n(e),n(r)]);return v(t,o)}function ve(e){return e.ok?e.value:e}var C="DESERIALIZATION_ERROR";function he(e){if(typeof e!="object"||e===null)return s({type:C,value:e});if(!("ok"in e))return s({type:C,value:e});let r=e;return r.ok===!0&&"value"in r?u(r.value):r.ok===!1&&"error"in r?s(r.error,{cause:r.cause}):s({type:C,value:e})}function Oe(e){return e.ok?{ok:!0,value:e.value}:{ok:!1,error:e.error}}function Ue(e,r,n){if(y(e)){let o=r.UnexpectedError;return o?o(e):n(e)}let t=r[e];return t?t(e):n(e)}function h(e,...r){return r.reduce((n,t)=>t(n),e)}function O(...e){return r=>e.reduce((n,t)=>t(n),r)}function U(...e){return r=>e.reduceRight((n,t)=>t(n),r)}var b=e=>e;function We(e,r){return c(e)?u(r(e.value)):e}function Ye(e,r){return c(e)?r(e.value):e}function Ze(e,r,n){return c(e)?u(r(e.value)):s(n(e.error),{cause:e.cause})}function Xe(e,r){return p(e)?s(r(e.error),{cause:e.cause}):e}function qe(e,r){return c(e)&&r(e.value),e}function Qe(e,r){return p(e)&&r(e.error),e}function er(e,r){return c(e)?r.ok(e.value):r.err(e.error,e.cause)}function rr(e,r){return c(e)?e.value:r(e.error)}function m(e,r){return c(e)?e:r(e.error)}function g(e,r){return c(e)?e.value:r}function w(e,r){return c(e)?e.value:r()}async function B(e,r){let n=await e;return c(n)?u(await r(n.value)):n}async function D(e,r){let n=await e;return c(n)?r(n.value):n}async function F(e,r){let n=await e;return c(n)&&await r(n.value),n}async function j(e,r){let n=await e;return p(n)&&await r(n.error),n}async function nr(e){if(e.length===0)return u([]);let r=new Array(e.length),n=0,t=!1;return new Promise(o=>{e.forEach((a,E)=>{a.then(l=>{t||(p(l)?(t=!0,o(l)):(r[E]=l.value,n++,n===e.length&&(t=!0,o(u(r)))))},l=>{t||(t=!0,o(s({type:T,cause:l},{cause:{type:"PROMISE_REJECTION",reason:l}})))})})})}async function _(e){return Promise.race(e.map(r=>r.catch(n=>s({type:T,cause:n},{cause:{type:"PROMISE_REJECTION",reason:n}}))))}function I(e,r){let n=[];for(let t=0;t<e.length;t++){let o=r(e[t],t);if(p(o))return o;n.push(o.value)}return u(n)}async function K(e,r){let n=[];for(let t=0;t<e.length;t++){let o=await r(e[t],t);if(p(o))return o;n.push(o.value)}return u(n)}async function H(e,r){return nr(e.map((n,t)=>r(n,t)))}var N={map:e=>r=>We(r,e),flatMap:e=>r=>Ye(r,e),bimap:(e,r)=>n=>Ze(n,e,r),mapError:e=>r=>Xe(r,e),tap:e=>r=>qe(r,e),tapError:e=>r=>Qe(r,e),match:e=>r=>er(r,e),recover:e=>r=>rr(r,e),recoverWith:e=>r=>m(r,e),getOrElse:e=>r=>g(r,e),getOrElseLazy:e=>r=>w(r,e)};var tr={...be,TaggedError:i,pipe:h,flow:O,compose:U,identity:b,R:N,recoverWith:m,getOrElse:g,getOrElseLazy:w,mapAsync:B,flatMapAsync:D,tapAsync:F,tapErrorAsync:j,race:_,traverse:I,traverseAsync:K,traverseParallel:H};0&&(module.exports={AWAITLY_CANCELLED,AWAITLY_TIMEOUT,AWAITLY_UNEXPECTED,Awaitly,DESERIALIZATION_ERROR,PROMISE_REJECTED,R,TaggedError,UnexpectedError,UnwrapError,all,allAsync,allSettled,allSettledAsync,andThen,any,anyAsync,bimap,compose,deserialize,err,flatMapAsync,flatten,flow,from,fromNullable,fromPromise,getOrElse,getOrElseLazy,hydrate,identity,isErr,isOk,isPromiseRejectedError,isSerializedResult,isUnexpectedError,map,mapAsync,mapError,mapErrorTry,mapTry,match,matchError,matchErrorPartial,ok,orElse,orElseAsync,partition,pipe,race,recover,recoverAsync,recoverWith,runOrNull,runOrThrow,runOrThrowAsync,runOrUndefined,serialize,tags,tap,tapAsync,tapError,tapErrorAsync,traverse,traverseAsync,traverseParallel,tryAsync,unwrap,unwrapOr,unwrapOrElse,zip,zipAsync});
|
|
2
2
|
//# sourceMappingURL=index.cjs.map
|