@ontrails/core 1.0.0-beta.14 → 1.0.0-beta.16
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/CHANGELOG.md +74 -0
- package/README.md +73 -24
- package/package.json +11 -4
- package/src/activation-provenance.ts +111 -0
- package/src/activation-source-compatibility.ts +430 -0
- package/src/activation-source-projection.ts +221 -0
- package/src/activation-source.ts +91 -0
- package/src/blob-ref.ts +51 -0
- package/src/branded.ts +1 -1
- package/src/context.ts +28 -4
- package/src/contour.ts +344 -0
- package/src/cross-batch.ts +69 -0
- package/src/cross-schema.ts +36 -0
- package/src/detours.ts +8 -0
- package/src/draft.ts +60 -52
- package/src/error-projection.ts +51 -0
- package/src/errors.ts +245 -48
- package/src/execute.ts +1250 -40
- package/src/fire.ts +1169 -0
- package/src/index.ts +383 -49
- package/src/internal/fork-ctx.ts +69 -0
- package/src/layer-projection.ts +192 -0
- package/src/layer.ts +81 -0
- package/src/observe.ts +361 -0
- package/src/path-security.ts +1 -1
- package/src/permits.ts +12 -0
- package/src/redaction/patterns.ts +6 -3
- package/src/resilience.ts +11 -11
- package/src/resource-config.ts +792 -0
- package/src/resource.ts +175 -0
- package/src/result.ts +18 -0
- package/src/run.ts +7 -6
- package/src/schedule-runtime.ts +689 -0
- package/src/schedule.ts +324 -0
- package/src/serialization.ts +90 -55
- package/src/signal-diagnostics.ts +633 -0
- package/src/signal-ref.ts +87 -0
- package/src/signal.ts +37 -0
- package/src/store/accessor-protocol.ts +56 -0
- package/src/store/index.ts +4 -0
- package/src/structured-examples.ts +245 -0
- package/src/surface-derivation.ts +91 -0
- package/src/surface-filter.ts +176 -0
- package/src/topo.ts +710 -59
- package/src/tracing.ts +345 -0
- package/src/trail.ts +347 -33
- package/src/trails/derive-trail.ts +842 -0
- package/src/trails/index.ts +9 -0
- package/src/trails/ingest.ts +138 -0
- package/src/{internal/trails-db.ts → trails-db.ts} +50 -31
- package/src/transport-error-map.ts +174 -0
- package/src/type-utils.ts +17 -2
- package/src/types.ts +208 -17
- package/src/validate-established-topo.ts +8 -2
- package/src/validate-topo.ts +244 -13
- package/src/validation.ts +58 -1
- package/src/webhook.ts +276 -0
- package/src/workspace.ts +1 -1
- package/src/zod-wrappers.ts +72 -0
- package/.turbo/turbo-build.log +0 -1
- package/.turbo/turbo-lint.log +0 -3
- package/.turbo/turbo-typecheck.log +0 -1
- package/dist/adapters.d.ts +0 -39
- package/dist/adapters.d.ts.map +0 -1
- package/dist/adapters.js +0 -2
- package/dist/adapters.js.map +0 -1
- package/dist/blob-ref.d.ts +0 -20
- package/dist/blob-ref.d.ts.map +0 -1
- package/dist/blob-ref.js +0 -22
- package/dist/blob-ref.js.map +0 -1
- package/dist/branded.d.ts +0 -36
- package/dist/branded.d.ts.map +0 -1
- package/dist/branded.js +0 -89
- package/dist/branded.js.map +0 -1
- package/dist/collections.d.ts +0 -31
- package/dist/collections.d.ts.map +0 -1
- package/dist/collections.js +0 -60
- package/dist/collections.js.map +0 -1
- package/dist/context.d.ts +0 -10
- package/dist/context.d.ts.map +0 -1
- package/dist/context.js +0 -21
- package/dist/context.js.map +0 -1
- package/dist/derive.d.ts +0 -44
- package/dist/derive.d.ts.map +0 -1
- package/dist/derive.js +0 -149
- package/dist/derive.js.map +0 -1
- package/dist/dispatch.d.ts +0 -27
- package/dist/dispatch.d.ts.map +0 -1
- package/dist/dispatch.js +0 -34
- package/dist/dispatch.js.map +0 -1
- package/dist/draft.d.ts +0 -28
- package/dist/draft.d.ts.map +0 -1
- package/dist/draft.js +0 -156
- package/dist/draft.js.map +0 -1
- package/dist/errors.d.ts +0 -83
- package/dist/errors.d.ts.map +0 -1
- package/dist/errors.js +0 -142
- package/dist/errors.js.map +0 -1
- package/dist/event.d.ts +0 -8
- package/dist/event.d.ts.map +0 -1
- package/dist/event.js +0 -7
- package/dist/event.js.map +0 -1
- package/dist/execute.d.ts +0 -35
- package/dist/execute.d.ts.map +0 -1
- package/dist/execute.js +0 -90
- package/dist/execute.js.map +0 -1
- package/dist/fetch.d.ts +0 -15
- package/dist/fetch.d.ts.map +0 -1
- package/dist/fetch.js +0 -102
- package/dist/fetch.js.map +0 -1
- package/dist/gate.d.ts +0 -17
- package/dist/gate.d.ts.map +0 -1
- package/dist/gate.js +0 -21
- package/dist/gate.js.map +0 -1
- package/dist/guards.d.ts +0 -17
- package/dist/guards.d.ts.map +0 -1
- package/dist/guards.js +0 -25
- package/dist/guards.js.map +0 -1
- package/dist/health.d.ts +0 -18
- package/dist/health.d.ts.map +0 -1
- package/dist/health.js +0 -5
- package/dist/health.js.map +0 -1
- package/dist/index.d.ts +0 -48
- package/dist/index.d.ts.map +0 -1
- package/dist/index.js +0 -51
- package/dist/index.js.map +0 -1
- package/dist/internal/topo-saves.d.ts +0 -47
- package/dist/internal/topo-saves.d.ts.map +0 -1
- package/dist/internal/topo-saves.js +0 -310
- package/dist/internal/topo-saves.js.map +0 -1
- package/dist/internal/topo-store-read.d.ts +0 -67
- package/dist/internal/topo-store-read.d.ts.map +0 -1
- package/dist/internal/topo-store-read.js +0 -222
- package/dist/internal/topo-store-read.js.map +0 -1
- package/dist/internal/topo-store.d.ts +0 -12
- package/dist/internal/topo-store.d.ts.map +0 -1
- package/dist/internal/topo-store.js +0 -571
- package/dist/internal/topo-store.js.map +0 -1
- package/dist/internal/trails-db.d.ts +0 -16
- package/dist/internal/trails-db.d.ts.map +0 -1
- package/dist/internal/trails-db.js +0 -118
- package/dist/internal/trails-db.js.map +0 -1
- package/dist/job.d.ts +0 -24
- package/dist/job.d.ts.map +0 -1
- package/dist/job.js +0 -17
- package/dist/job.js.map +0 -1
- package/dist/layer.d.ts +0 -6
- package/dist/layer.d.ts.map +0 -1
- package/dist/layer.js +0 -4
- package/dist/layer.js.map +0 -1
- package/dist/path-security.d.ts +0 -28
- package/dist/path-security.d.ts.map +0 -1
- package/dist/path-security.js +0 -63
- package/dist/path-security.js.map +0 -1
- package/dist/patterns/bulk.d.ts +0 -15
- package/dist/patterns/bulk.d.ts.map +0 -1
- package/dist/patterns/bulk.js +0 -14
- package/dist/patterns/bulk.js.map +0 -1
- package/dist/patterns/change.d.ts +0 -10
- package/dist/patterns/change.d.ts.map +0 -1
- package/dist/patterns/change.js +0 -10
- package/dist/patterns/change.js.map +0 -1
- package/dist/patterns/date-range.d.ts +0 -10
- package/dist/patterns/date-range.d.ts.map +0 -1
- package/dist/patterns/date-range.js +0 -10
- package/dist/patterns/date-range.js.map +0 -1
- package/dist/patterns/index.d.ts +0 -9
- package/dist/patterns/index.d.ts.map +0 -1
- package/dist/patterns/index.js +0 -9
- package/dist/patterns/index.js.map +0 -1
- package/dist/patterns/pagination.d.ts +0 -18
- package/dist/patterns/pagination.d.ts.map +0 -1
- package/dist/patterns/pagination.js +0 -18
- package/dist/patterns/pagination.js.map +0 -1
- package/dist/patterns/progress.d.ts +0 -11
- package/dist/patterns/progress.d.ts.map +0 -1
- package/dist/patterns/progress.js +0 -11
- package/dist/patterns/progress.js.map +0 -1
- package/dist/patterns/sorting.d.ts +0 -13
- package/dist/patterns/sorting.d.ts.map +0 -1
- package/dist/patterns/sorting.js +0 -10
- package/dist/patterns/sorting.js.map +0 -1
- package/dist/patterns/status.d.ts +0 -15
- package/dist/patterns/status.d.ts.map +0 -1
- package/dist/patterns/status.js +0 -9
- package/dist/patterns/status.js.map +0 -1
- package/dist/patterns/timestamps.d.ts +0 -10
- package/dist/patterns/timestamps.d.ts.map +0 -1
- package/dist/patterns/timestamps.js +0 -10
- package/dist/patterns/timestamps.js.map +0 -1
- package/dist/provision-config.d.ts +0 -22
- package/dist/provision-config.d.ts.map +0 -1
- package/dist/provision-config.js +0 -210
- package/dist/provision-config.js.map +0 -1
- package/dist/provision.d.ts +0 -71
- package/dist/provision.d.ts.map +0 -1
- package/dist/provision.js +0 -56
- package/dist/provision.js.map +0 -1
- package/dist/redaction/index.d.ts +0 -4
- package/dist/redaction/index.d.ts.map +0 -1
- package/dist/redaction/index.js +0 -3
- package/dist/redaction/index.js.map +0 -1
- package/dist/redaction/patterns.d.ts +0 -9
- package/dist/redaction/patterns.d.ts.map +0 -1
- package/dist/redaction/patterns.js +0 -39
- package/dist/redaction/patterns.js.map +0 -1
- package/dist/redaction/redactor.d.ts +0 -27
- package/dist/redaction/redactor.d.ts.map +0 -1
- package/dist/redaction/redactor.js +0 -89
- package/dist/redaction/redactor.js.map +0 -1
- package/dist/resilience.d.ts +0 -34
- package/dist/resilience.d.ts.map +0 -1
- package/dist/resilience.js +0 -164
- package/dist/resilience.js.map +0 -1
- package/dist/result.d.ts +0 -57
- package/dist/result.d.ts.map +0 -1
- package/dist/result.js +0 -156
- package/dist/result.js.map +0 -1
- package/dist/run.d.ts +0 -27
- package/dist/run.d.ts.map +0 -1
- package/dist/run.js +0 -34
- package/dist/run.js.map +0 -1
- package/dist/serialization.d.ts +0 -27
- package/dist/serialization.d.ts.map +0 -1
- package/dist/serialization.js +0 -153
- package/dist/serialization.js.map +0 -1
- package/dist/service-config.d.ts +0 -22
- package/dist/service-config.d.ts.map +0 -1
- package/dist/service-config.js +0 -210
- package/dist/service-config.js.map +0 -1
- package/dist/service.d.ts +0 -71
- package/dist/service.d.ts.map +0 -1
- package/dist/service.js +0 -56
- package/dist/service.js.map +0 -1
- package/dist/signal.d.ts +0 -33
- package/dist/signal.d.ts.map +0 -1
- package/dist/signal.js +0 -17
- package/dist/signal.js.map +0 -1
- package/dist/topo-store.d.ts +0 -48
- package/dist/topo-store.d.ts.map +0 -1
- package/dist/topo-store.js +0 -175
- package/dist/topo-store.js.map +0 -1
- package/dist/topo.d.ts +0 -25
- package/dist/topo.d.ts.map +0 -1
- package/dist/topo.js +0 -112
- package/dist/topo.js.map +0 -1
- package/dist/trail.d.ts +0 -96
- package/dist/trail.d.ts.map +0 -1
- package/dist/trail.js +0 -20
- package/dist/trail.js.map +0 -1
- package/dist/type-utils.d.ts +0 -24
- package/dist/type-utils.d.ts.map +0 -1
- package/dist/type-utils.js +0 -12
- package/dist/type-utils.js.map +0 -1
- package/dist/types.d.ts +0 -71
- package/dist/types.d.ts.map +0 -1
- package/dist/types.js +0 -3
- package/dist/types.js.map +0 -1
- package/dist/validate-established-topo.d.ts +0 -76
- package/dist/validate-established-topo.d.ts.map +0 -1
- package/dist/validate-established-topo.js +0 -43
- package/dist/validate-established-topo.js.map +0 -1
- package/dist/validate-topo.d.ts +0 -24
- package/dist/validate-topo.d.ts.map +0 -1
- package/dist/validate-topo.js +0 -176
- package/dist/validate-topo.js.map +0 -1
- package/dist/validation.d.ts +0 -27
- package/dist/validation.d.ts.map +0 -1
- package/dist/validation.js +0 -165
- package/dist/validation.js.map +0 -1
- package/dist/workspace.d.ts +0 -25
- package/dist/workspace.d.ts.map +0 -1
- package/dist/workspace.js +0 -57
- package/dist/workspace.js.map +0 -1
- package/src/__tests__/blob-ref.test.ts +0 -103
- package/src/__tests__/branded.test.ts +0 -148
- package/src/__tests__/collections.test.ts +0 -126
- package/src/__tests__/context.test.ts +0 -77
- package/src/__tests__/derive.test.ts +0 -260
- package/src/__tests__/errors.test.ts +0 -309
- package/src/__tests__/execute.test.ts +0 -525
- package/src/__tests__/fetch.test.ts +0 -217
- package/src/__tests__/gate.test.ts +0 -125
- package/src/__tests__/guards.test.ts +0 -102
- package/src/__tests__/path-security.test.ts +0 -114
- package/src/__tests__/patterns.test.ts +0 -273
- package/src/__tests__/redaction.test.ts +0 -244
- package/src/__tests__/resilience.test.ts +0 -246
- package/src/__tests__/result.test.ts +0 -155
- package/src/__tests__/run.test.ts +0 -181
- package/src/__tests__/serialization.test.ts +0 -401
- package/src/__tests__/service-config.test.ts +0 -228
- package/src/__tests__/service.test.ts +0 -197
- package/src/__tests__/signal.test.ts +0 -82
- package/src/__tests__/topo-store-read.test.ts +0 -251
- package/src/__tests__/topo-store.test.ts +0 -469
- package/src/__tests__/topo.test.ts +0 -277
- package/src/__tests__/trail-permit.test.ts +0 -60
- package/src/__tests__/trail.test.ts +0 -261
- package/src/__tests__/trails-db.test.ts +0 -191
- package/src/__tests__/type-utils.test.ts +0 -90
- package/src/__tests__/validate-topo.test.ts +0 -489
- package/src/__tests__/validation.test.ts +0 -336
- package/src/__tests__/workspace.test.ts +0 -183
- package/src/event.ts +0 -15
- package/src/gate.ts +0 -44
- package/src/internal/topo-saves.ts +0 -429
- package/src/internal/topo-store-read.ts +0 -473
- package/src/internal/topo-store.ts +0 -1087
- package/src/provision-config.ts +0 -373
- package/src/provision.ts +0 -148
- package/src/topo-store.ts +0 -301
- package/tsconfig.json +0 -9
- package/tsconfig.tsbuildinfo +0 -1
package/dist/resilience.js
DELETED
|
@@ -1,164 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Resilience utilities for @ontrails/core
|
|
3
|
-
*
|
|
4
|
-
* Retry with exponential backoff and timeout wrappers,
|
|
5
|
-
* all returning Result types.
|
|
6
|
-
*/
|
|
7
|
-
import { CancelledError, TimeoutError, isRetryable } from './errors.js';
|
|
8
|
-
import { Result } from './result.js';
|
|
9
|
-
// ---------------------------------------------------------------------------
|
|
10
|
-
// Helpers (defined before usage)
|
|
11
|
-
// ---------------------------------------------------------------------------
|
|
12
|
-
const sleep = (ms, signal) =>
|
|
13
|
-
// oxlint-disable-next-line avoid-new -- Promise constructor needed for setTimeout-based sleep
|
|
14
|
-
new Promise((resolve) => {
|
|
15
|
-
if (signal?.aborted) {
|
|
16
|
-
resolve();
|
|
17
|
-
return;
|
|
18
|
-
}
|
|
19
|
-
let settled = false;
|
|
20
|
-
const done = () => {
|
|
21
|
-
if (!settled) {
|
|
22
|
-
settled = true;
|
|
23
|
-
resolve();
|
|
24
|
-
}
|
|
25
|
-
};
|
|
26
|
-
const timer = setTimeout(done, ms);
|
|
27
|
-
const onAbort = () => {
|
|
28
|
-
clearTimeout(timer);
|
|
29
|
-
done();
|
|
30
|
-
};
|
|
31
|
-
signal?.addEventListener('abort', onAbort, { once: true });
|
|
32
|
-
});
|
|
33
|
-
// ---------------------------------------------------------------------------
|
|
34
|
-
// shouldRetry (default)
|
|
35
|
-
// ---------------------------------------------------------------------------
|
|
36
|
-
/** Default retry predicate using the error taxonomy. */
|
|
37
|
-
export const shouldRetry = (error) => isRetryable(error);
|
|
38
|
-
// ---------------------------------------------------------------------------
|
|
39
|
-
// getBackoffDelay
|
|
40
|
-
// ---------------------------------------------------------------------------
|
|
41
|
-
/** Compute exponential backoff delay with full jitter. */
|
|
42
|
-
export const getBackoffDelay = (attempt, options) => {
|
|
43
|
-
const base = options?.baseDelay ?? 1000;
|
|
44
|
-
const max = options?.maxDelay ?? 30_000;
|
|
45
|
-
const factor = options?.backoffFactor ?? 2;
|
|
46
|
-
const exponential = base * factor ** attempt;
|
|
47
|
-
const capped = Math.min(exponential, max);
|
|
48
|
-
// Full jitter: random value in [0, capped]
|
|
49
|
-
return Math.floor(Math.random() * capped);
|
|
50
|
-
};
|
|
51
|
-
// ---------------------------------------------------------------------------
|
|
52
|
-
// retry
|
|
53
|
-
// ---------------------------------------------------------------------------
|
|
54
|
-
/**
|
|
55
|
-
* Retry an async function that returns a Result.
|
|
56
|
-
*
|
|
57
|
-
* On each failure, checks whether the error is retryable and whether the
|
|
58
|
-
* attempt budget remains. Applies exponential backoff with jitter between
|
|
59
|
-
* retries.
|
|
60
|
-
*/
|
|
61
|
-
/** Attempt a single retry iteration. Returns the result to stop, or undefined to continue. */
|
|
62
|
-
const tryAttempt = async (fn, attempt, maxAttempts, retryPredicate, options) => {
|
|
63
|
-
const result = await fn();
|
|
64
|
-
if (result.isOk()) {
|
|
65
|
-
return { done: true, result };
|
|
66
|
-
}
|
|
67
|
-
const isLast = attempt === maxAttempts - 1;
|
|
68
|
-
if (isLast || !retryPredicate(result.error)) {
|
|
69
|
-
return { done: true, result };
|
|
70
|
-
}
|
|
71
|
-
const delay = getBackoffDelay(attempt, options);
|
|
72
|
-
if (delay > 0) {
|
|
73
|
-
await sleep(delay, options?.signal);
|
|
74
|
-
}
|
|
75
|
-
return { done: false, result };
|
|
76
|
-
};
|
|
77
|
-
/** Resolve retry options to concrete values. */
|
|
78
|
-
const resolveRetryOptions = (options) => ({
|
|
79
|
-
maxAttempts: options?.maxAttempts ?? 3,
|
|
80
|
-
retryPredicate: options?.shouldRetry ?? shouldRetry,
|
|
81
|
-
signal: options?.signal,
|
|
82
|
-
});
|
|
83
|
-
export const retry = async (fn, options) => {
|
|
84
|
-
const { maxAttempts, retryPredicate, signal } = resolveRetryOptions(options);
|
|
85
|
-
let lastResult;
|
|
86
|
-
for (let attempt = 0; attempt < maxAttempts; attempt += 1) {
|
|
87
|
-
if (signal?.aborted) {
|
|
88
|
-
return Result.err(new CancelledError('Retry cancelled'));
|
|
89
|
-
}
|
|
90
|
-
const outcome = await tryAttempt(fn, attempt, maxAttempts, retryPredicate, options);
|
|
91
|
-
lastResult = outcome.result;
|
|
92
|
-
if (outcome.done) {
|
|
93
|
-
return outcome.result;
|
|
94
|
-
}
|
|
95
|
-
}
|
|
96
|
-
return lastResult ?? Result.err(new CancelledError('Retry exhausted'));
|
|
97
|
-
};
|
|
98
|
-
// ---------------------------------------------------------------------------
|
|
99
|
-
// withTimeout
|
|
100
|
-
// ---------------------------------------------------------------------------
|
|
101
|
-
/**
|
|
102
|
-
* Run an async Result-returning function with a timeout.
|
|
103
|
-
*
|
|
104
|
-
* If the timeout fires first, returns a TimeoutError result.
|
|
105
|
-
* Also respects an external AbortSignal.
|
|
106
|
-
*/
|
|
107
|
-
export const withTimeout = (fn, ms, signal) => {
|
|
108
|
-
if (signal?.aborted) {
|
|
109
|
-
return Promise.resolve(Result.err(new CancelledError('Already cancelled')));
|
|
110
|
-
}
|
|
111
|
-
// oxlint-disable-next-line avoid-new, promise/no-multiple-resolved -- Promise constructor needed for timeout race; settled guard ensures single resolution
|
|
112
|
-
return new Promise((resolve) => {
|
|
113
|
-
let settled = false;
|
|
114
|
-
// oxlint-disable-next-line prefer-const -- assigned after declaration
|
|
115
|
-
let timer;
|
|
116
|
-
const onAbort = () => {
|
|
117
|
-
if (settled) {
|
|
118
|
-
return;
|
|
119
|
-
}
|
|
120
|
-
settled = true;
|
|
121
|
-
clearTimeout(timer);
|
|
122
|
-
signal?.removeEventListener('abort', onAbort);
|
|
123
|
-
// oxlint-disable-next-line promise/no-multiple-resolved -- settled guard ensures single resolution
|
|
124
|
-
resolve(Result.err(new CancelledError('Operation cancelled')));
|
|
125
|
-
};
|
|
126
|
-
signal?.addEventListener('abort', onAbort, { once: true });
|
|
127
|
-
timer = setTimeout(() => {
|
|
128
|
-
if (settled) {
|
|
129
|
-
return;
|
|
130
|
-
}
|
|
131
|
-
settled = true;
|
|
132
|
-
signal?.removeEventListener('abort', onAbort);
|
|
133
|
-
// oxlint-disable-next-line promise/no-multiple-resolved -- settled guard ensures single resolution
|
|
134
|
-
resolve(Result.err(new TimeoutError(`Operation timed out after ${ms}ms`, {
|
|
135
|
-
context: { timeoutMs: ms },
|
|
136
|
-
})));
|
|
137
|
-
}, ms);
|
|
138
|
-
// oxlint-disable-next-line prefer-await-to-then, no-void -- .then() needed inside Promise constructor; void discards unhandled rejection
|
|
139
|
-
void fn().then(
|
|
140
|
-
// oxlint-disable-next-line prefer-await-to-callbacks -- callback required inside .then()
|
|
141
|
-
(result) => {
|
|
142
|
-
if (settled) {
|
|
143
|
-
return;
|
|
144
|
-
}
|
|
145
|
-
settled = true;
|
|
146
|
-
clearTimeout(timer);
|
|
147
|
-
signal?.removeEventListener('abort', onAbort);
|
|
148
|
-
// oxlint-disable-next-line promise/no-multiple-resolved -- settled guard ensures single resolution
|
|
149
|
-
resolve(result);
|
|
150
|
-
},
|
|
151
|
-
// oxlint-disable-next-line prefer-await-to-callbacks -- rejection handler required inside .then()
|
|
152
|
-
(error) => {
|
|
153
|
-
if (settled) {
|
|
154
|
-
return;
|
|
155
|
-
}
|
|
156
|
-
settled = true;
|
|
157
|
-
clearTimeout(timer);
|
|
158
|
-
signal?.removeEventListener('abort', onAbort);
|
|
159
|
-
// oxlint-disable-next-line promise/no-multiple-resolved -- settled guard ensures single resolution
|
|
160
|
-
resolve(Result.err(error instanceof Error ? error : new Error(String(error))));
|
|
161
|
-
});
|
|
162
|
-
});
|
|
163
|
-
};
|
|
164
|
-
//# sourceMappingURL=resilience.js.map
|
package/dist/resilience.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"resilience.js","sourceRoot":"","sources":["../src/resilience.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,EAAE,cAAc,EAAE,YAAY,EAAE,WAAW,EAAE,MAAM,aAAa,CAAC;AACxE,OAAO,EAAE,MAAM,EAAE,MAAM,aAAa,CAAC;AAqBrC,8EAA8E;AAC9E,iCAAiC;AACjC,8EAA8E;AAE9E,MAAM,KAAK,GAAG,CAAC,EAAU,EAAE,MAAoB,EAAiB,EAAE;AAChE,8FAA8F;AAC9F,IAAI,OAAO,CAAC,CAAC,OAAO,EAAE,EAAE;IACtB,IAAI,MAAM,EAAE,OAAO,EAAE,CAAC;QACpB,OAAO,EAAE,CAAC;QACV,OAAO;IACT,CAAC;IACD,IAAI,OAAO,GAAG,KAAK,CAAC;IACpB,MAAM,IAAI,GAAG,GAAG,EAAE;QAChB,IAAI,CAAC,OAAO,EAAE,CAAC;YACb,OAAO,GAAG,IAAI,CAAC;YACf,OAAO,EAAE,CAAC;QACZ,CAAC;IACH,CAAC,CAAC;IACF,MAAM,KAAK,GAAG,UAAU,CAAC,IAAI,EAAE,EAAE,CAAC,CAAC;IACnC,MAAM,OAAO,GAAG,GAAG,EAAE;QACnB,YAAY,CAAC,KAAK,CAAC,CAAC;QACpB,IAAI,EAAE,CAAC;IACT,CAAC,CAAC;IACF,MAAM,EAAE,gBAAgB,CAAC,OAAO,EAAE,OAAO,EAAE,EAAE,IAAI,EAAE,IAAI,EAAE,CAAC,CAAC;AAC7D,CAAC,CAAC,CAAC;AAEL,8EAA8E;AAC9E,wBAAwB;AACxB,8EAA8E;AAE9E,wDAAwD;AACxD,MAAM,CAAC,MAAM,WAAW,GAAG,CAAC,KAAY,EAAW,EAAE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B,CAAC,CACH,CACF,CAAC;QACJ,CAAC,EAAE,EAAE,CAAC,CAAC;QAEP,yIAAyI;QACzI,KAAK,EAAE,EAAE,CAAC,IAAI;QACZ,yFAAyF;QACzF,CAAC,MAAM,EAAE,EAAE;YACT,IAAI,OAAO,EAAE,CAAC;gBACZ,OAAO;YACT,CAAC;YACD,OAAO,GAAG,IAAI,CAAC;YACf,YAAY,CAAC,KAAK,CAAC,CAAC;YACpB,MAAM,EAAE,mBAAmB,CAAC,OAAO,EAAE,OAAO,CAAC,CAAC;YAC9C,mGAAmG;YACnG,OAAO,CAAC,MAAM,CAAC,CAAC;QAClB,CAAC;QACD,kGAAkG;QAClG,CAAC,KAAc,EAAE,EAAE;YACjB,IAAI,OAAO,EAAE,CAAC;gBACZ,OAAO;YACT,CAAC;YACD,OAAO,GAAG,IAAI,CAAC;YACf,YAAY,CAAC,KAAK,CAAC,CAAC;YACpB,MAAM,EAAE,mBAAmB,CAAC,OAAO,EAAE,OAAO,CAAC,CAAC;YAC9C,mGAAmG;YACnG,OAAO,CACL,MAAM,CAAC,GAAG,CAAC,KAAK,YAAY,KAAK,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,IAAI,KAAK,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC,CAAC,CACtE,CAAC;QACJ,CAAC,CACF,CAAC;IACJ,CAAC,CAAC,CAAC;AACL,CAAC,CAAC"}
|
package/dist/result.d.ts
DELETED
|
@@ -1,57 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* A type-safe Result monad for representing success/failure without exceptions.
|
|
3
|
-
*/
|
|
4
|
-
import { InternalError, ValidationError } from './errors.js';
|
|
5
|
-
declare class Ok<T, E> {
|
|
6
|
-
readonly value: T;
|
|
7
|
-
constructor(value: T);
|
|
8
|
-
isOk(): this is Ok<T, E>;
|
|
9
|
-
isErr(): this is Err<E>;
|
|
10
|
-
map<U>(fn: (value: T) => U): Result<U, E>;
|
|
11
|
-
flatMap<U, F = E>(fn: (value: T) => Result<U, F>): Result<U, E | F>;
|
|
12
|
-
mapErr<F>(_fn: (error: E) => F): Result<T, F>;
|
|
13
|
-
match<U>(handlers: {
|
|
14
|
-
ok: (value: T) => U;
|
|
15
|
-
err: (error: E) => U;
|
|
16
|
-
}): U;
|
|
17
|
-
unwrap(): T;
|
|
18
|
-
unwrapOr(_fallback: T): T;
|
|
19
|
-
}
|
|
20
|
-
declare class Err<E> {
|
|
21
|
-
readonly error: E;
|
|
22
|
-
constructor(error: E);
|
|
23
|
-
isOk(): this is Ok<never, E>;
|
|
24
|
-
isErr(): this is Err<E>;
|
|
25
|
-
map<U>(_fn: (value: never) => U): Result<U, E>;
|
|
26
|
-
flatMap<U, F = E>(_fn: (value: never) => Result<U, F>): Result<U, E | F>;
|
|
27
|
-
mapErr<F>(fn: (error: E) => F): Result<never, F>;
|
|
28
|
-
match<U>(handlers: {
|
|
29
|
-
ok: (value: never) => U;
|
|
30
|
-
err: (error: E) => U;
|
|
31
|
-
}): U;
|
|
32
|
-
unwrap(): never;
|
|
33
|
-
unwrapOr<T>(fallback: T): T;
|
|
34
|
-
}
|
|
35
|
-
export type Result<T, E = Error> = Ok<T, E> | Err<E>;
|
|
36
|
-
export declare const Result: {
|
|
37
|
-
readonly combine: <T, E>(results: readonly Result<T, E>[]) => Result<T[], E>;
|
|
38
|
-
readonly err: <E>(error: E) => Err<E>;
|
|
39
|
-
/**
|
|
40
|
-
* Wrap a fetch call in a Result, mapping failures to TrailsError subclasses.
|
|
41
|
-
*
|
|
42
|
-
* Network errors become NetworkError. Abort signals become CancelledError.
|
|
43
|
-
* HTTP error status codes map to the appropriate error category.
|
|
44
|
-
*/
|
|
45
|
-
readonly fromFetch: (input: string | URL | Request, init?: RequestInit) => Promise<Result<Response, Error>>;
|
|
46
|
-
/**
|
|
47
|
-
* Parse a JSON string, returning a Result instead of throwing.
|
|
48
|
-
*/
|
|
49
|
-
readonly fromJson: (json: string) => Result<unknown, ValidationError>;
|
|
50
|
-
readonly ok: <T = void>(value?: T) => Result<T, never>;
|
|
51
|
-
/**
|
|
52
|
-
* Stringify a value to JSON, returning a Result. Handles circular references.
|
|
53
|
-
*/
|
|
54
|
-
readonly toJson: (value: unknown) => Result<string, InternalError>;
|
|
55
|
-
};
|
|
56
|
-
export {};
|
|
57
|
-
//# sourceMappingURL=result.d.ts.map
|
package/dist/result.d.ts.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"result.d.ts","sourceRoot":"","sources":["../src/result.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,OAAO,EAAE,aAAa,EAAE,eAAe,EAAE,MAAM,aAAa,CAAC;AAE7D,cAAM,EAAE,CAAC,CAAC,EAAE,CAAC;IACX,QAAQ,CAAC,KAAK,EAAE,CAAC,CAAC;gBAEN,KAAK,EAAE,CAAC;IAKpB,IAAI,IAAI,IAAI,IAAI,EAAE,CAAC,CAAC,EAAE,CAAC,CAAC;IAKxB,KAAK,IAAI,IAAI,IAAI,GAAG,CAAC,CAAC,CAAC;IAIvB,GAAG,CAAC,CAAC,EAAE,EAAE,EAAE,CAAC,KAAK,EAAE,CAAC,KAAK,CAAC,GAAG,MAAM,CAAC,CAAC,EAAE,CAAC,CAAC;IAIzC,OAAO,CAAC,CAAC,EAAE,CAAC,GAAG,CAAC,EAAE,EAAE,EAAE,CAAC,KAAK,EAAE,CAAC,KAAK,MAAM,CAAC,CAAC,EAAE,CAAC,CAAC,GAAG,MAAM,CAAC,CAAC,EAAE,CAAC,GAAG,CAAC,CAAC;IAInE,MAAM,CAAC,CAAC,EAAE,GAAG,EAAE,CAAC,KAAK,EAAE,CAAC,KAAK,CAAC,GAAG,MAAM,CAAC,CAAC,EAAE,CAAC,CAAC;IAI7C,KAAK,CAAC,CAAC,EAAE,QAAQ,EAAE;QAAE,EAAE,EAAE,CAAC,KAAK,EAAE,CAAC,KAAK,CAAC,CAAC;QAAC,GAAG,EAAE,CAAC,KAAK,EAAE,CAAC,KAAK,CAAC,CAA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AM,CAAC,CAAC,EAAE,CAAC,CAAC,EAAE,KAAG,MAAM,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC;mBAW3D,CAAC,SAAS,CAAC,KAAG,GAAG,CAAC,CAAC,CAAC;IAIxB;;;;;OAKG;gCAEM,MAAM,GAAG,GAAG,GAAG,OAAO,SACtB,WAAW,KACjB,OAAO,CAAC,MAAM,CAAC,QAAQ,EAAE,KAAK,CAAC,CAAC;IAMnC;;OAEG;8BACY,MAAM,KAAG,MAAM,CAAC,OAAO,EAAE,eAAe,CAAC;kBAarD,CAAC,iBAAiB,CAAC,KAAG,MAAM,CAAC,CAAC,EAAE,KAAK,CAAC;IAIzC;;OAEG;6BACW,OAAO,KAAG,MAAM,CAAC,MAAM,EAAE,aAAa,CAAC;CA0C7C,CAAC"}
|
package/dist/result.js
DELETED
|
@@ -1,156 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* A type-safe Result monad for representing success/failure without exceptions.
|
|
3
|
-
*/
|
|
4
|
-
import { InternalError, ValidationError } from './errors.js';
|
|
5
|
-
class Ok {
|
|
6
|
-
value;
|
|
7
|
-
constructor(value) {
|
|
8
|
-
this.value = value;
|
|
9
|
-
}
|
|
10
|
-
// oxlint-disable-next-line class-methods-use-this -- type guard for Result discriminated union
|
|
11
|
-
isOk() {
|
|
12
|
-
return true;
|
|
13
|
-
}
|
|
14
|
-
// oxlint-disable-next-line class-methods-use-this -- type guard for Result discriminated union
|
|
15
|
-
isErr() {
|
|
16
|
-
return false;
|
|
17
|
-
}
|
|
18
|
-
map(fn) {
|
|
19
|
-
return new Ok(fn(this.value));
|
|
20
|
-
}
|
|
21
|
-
flatMap(fn) {
|
|
22
|
-
return fn(this.value);
|
|
23
|
-
}
|
|
24
|
-
mapErr(_fn) {
|
|
25
|
-
return new Ok(this.value);
|
|
26
|
-
}
|
|
27
|
-
match(handlers) {
|
|
28
|
-
return handlers.ok(this.value);
|
|
29
|
-
}
|
|
30
|
-
unwrap() {
|
|
31
|
-
return this.value;
|
|
32
|
-
}
|
|
33
|
-
unwrapOr(_fallback) {
|
|
34
|
-
return this.value;
|
|
35
|
-
}
|
|
36
|
-
}
|
|
37
|
-
// oxlint-disable-next-line max-classes-per-file -- Result monad requires paired Ok/Err classes
|
|
38
|
-
class Err {
|
|
39
|
-
error;
|
|
40
|
-
constructor(error) {
|
|
41
|
-
this.error = error;
|
|
42
|
-
}
|
|
43
|
-
// oxlint-disable-next-line class-methods-use-this -- type guard for Result discriminated union
|
|
44
|
-
isOk() {
|
|
45
|
-
return false;
|
|
46
|
-
}
|
|
47
|
-
// oxlint-disable-next-line class-methods-use-this -- type guard for Result discriminated union
|
|
48
|
-
isErr() {
|
|
49
|
-
return true;
|
|
50
|
-
}
|
|
51
|
-
map(_fn) {
|
|
52
|
-
return new Err(this.error);
|
|
53
|
-
}
|
|
54
|
-
flatMap(_fn) {
|
|
55
|
-
return new Err(this.error);
|
|
56
|
-
}
|
|
57
|
-
mapErr(fn) {
|
|
58
|
-
return new Err(fn(this.error));
|
|
59
|
-
}
|
|
60
|
-
match(handlers) {
|
|
61
|
-
return handlers.err(this.error);
|
|
62
|
-
}
|
|
63
|
-
unwrap() {
|
|
64
|
-
throw this.error instanceof Error
|
|
65
|
-
? this.error
|
|
66
|
-
: new Error(String(this.error));
|
|
67
|
-
}
|
|
68
|
-
// oxlint-disable-next-line class-methods-use-this -- symmetric API with Ok.unwrapOr
|
|
69
|
-
unwrapOr(fallback) {
|
|
70
|
-
return fallback;
|
|
71
|
-
}
|
|
72
|
-
}
|
|
73
|
-
// eslint-disable-next-line @typescript-eslint/no-namespace
|
|
74
|
-
export const Result = {
|
|
75
|
-
combine(results) {
|
|
76
|
-
const values = [];
|
|
77
|
-
for (const result of results) {
|
|
78
|
-
if (result.isErr()) {
|
|
79
|
-
return new Err(result.error);
|
|
80
|
-
}
|
|
81
|
-
values.push(result.value);
|
|
82
|
-
}
|
|
83
|
-
return new Ok(values);
|
|
84
|
-
},
|
|
85
|
-
err(error) {
|
|
86
|
-
return new Err(error);
|
|
87
|
-
},
|
|
88
|
-
/**
|
|
89
|
-
* Wrap a fetch call in a Result, mapping failures to TrailsError subclasses.
|
|
90
|
-
*
|
|
91
|
-
* Network errors become NetworkError. Abort signals become CancelledError.
|
|
92
|
-
* HTTP error status codes map to the appropriate error category.
|
|
93
|
-
*/
|
|
94
|
-
async fromFetch(input, init) {
|
|
95
|
-
// Lazy import avoids a circular dependency (fetch.ts imports Result)
|
|
96
|
-
const { fromFetch: fetchImpl } = await import('./fetch.js');
|
|
97
|
-
return fetchImpl(input, init);
|
|
98
|
-
},
|
|
99
|
-
/**
|
|
100
|
-
* Parse a JSON string, returning a Result instead of throwing.
|
|
101
|
-
*/
|
|
102
|
-
fromJson(json) {
|
|
103
|
-
try {
|
|
104
|
-
return new Ok(JSON.parse(json));
|
|
105
|
-
}
|
|
106
|
-
catch (error) {
|
|
107
|
-
return new Err(new ValidationError('Invalid JSON', {
|
|
108
|
-
cause: error instanceof Error ? error : new Error(String(error)),
|
|
109
|
-
context: { input: json.slice(0, 200) },
|
|
110
|
-
}));
|
|
111
|
-
}
|
|
112
|
-
},
|
|
113
|
-
ok(value) {
|
|
114
|
-
return new Ok(value);
|
|
115
|
-
},
|
|
116
|
-
/**
|
|
117
|
-
* Stringify a value to JSON, returning a Result. Handles circular references.
|
|
118
|
-
*/
|
|
119
|
-
toJson(value) {
|
|
120
|
-
try {
|
|
121
|
-
// Track the current ancestor chain, not every object ever visited.
|
|
122
|
-
// This allows shared references in a DAG while still detecting cycles.
|
|
123
|
-
const stack = [];
|
|
124
|
-
const keys = [];
|
|
125
|
-
const json = JSON.stringify(value, function json(key, val) {
|
|
126
|
-
if (stack.length > 0) {
|
|
127
|
-
// `this` is the object that contains `key`. Trim the stack back
|
|
128
|
-
// to `this` so we only track the current ancestor path.
|
|
129
|
-
const thisIndex = stack.lastIndexOf(this);
|
|
130
|
-
stack.splice(thisIndex + 1);
|
|
131
|
-
keys.splice(thisIndex);
|
|
132
|
-
}
|
|
133
|
-
if (typeof val === 'object' && val !== null) {
|
|
134
|
-
if (stack.includes(val)) {
|
|
135
|
-
return '[Circular]';
|
|
136
|
-
}
|
|
137
|
-
stack.push(val);
|
|
138
|
-
keys.push(key);
|
|
139
|
-
}
|
|
140
|
-
return val;
|
|
141
|
-
});
|
|
142
|
-
if (json === undefined) {
|
|
143
|
-
return new Err(new InternalError('Value is not JSON-serializable', {
|
|
144
|
-
context: { type: typeof value },
|
|
145
|
-
}));
|
|
146
|
-
}
|
|
147
|
-
return new Ok(json);
|
|
148
|
-
}
|
|
149
|
-
catch (error) {
|
|
150
|
-
return new Err(new InternalError('Failed to stringify value', {
|
|
151
|
-
cause: error instanceof Error ? error : new Error(String(error)),
|
|
152
|
-
}));
|
|
153
|
-
}
|
|
154
|
-
},
|
|
155
|
-
};
|
|
156
|
-
//# sourceMappingURL=result.js.map
|
package/dist/result.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"result.js","sourceRoot":"","sources":["../src/result.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,OAAO,EAAE,aAAa,EAAE,eAAe,EAAE,MAAM,aAAa,CAAC;AAE7D,MAAM,EAAE;IACG,KAAK,CAAI;IAElB,YAAY,KAAQ;QAClB,IAAI,CAAC,KAAK,GAAG,KAAK,CAAC;IACrB,CAAC;IAED,+FAA+F;IAC/F,IAAI;QACF,OAAO,IAAI,CAAC;IACd,CAAC;IAED,+FAA+F;IAC/F,KAAK;QACH,OAAO,KAAK,CAAC;IACf,CAAC;IAED,GAAG,CAAI,EAAmB;QACxB,OAAO,IAAI,EAAE,CAAC,EAAE,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,CAAC;IAChC,CAAC;IAED,OAAO,CAAW,EAA8B;QAC9C,OAAO,EAAE,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC;IACxB,CAAC;IAED,MAAM,CAAI,GAAoB;QAC5B,OAAO,IAAI,EAAE,CAAC,IAAI,CAA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I,aAAa,CAAC,2BAA2B,EAAE;gBAC7C,KAAK,EAAE,KAAK,YAAY,KAAK,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,IAAI,KAAK,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC;aACjE,CAAC,CACH,CAAC;QACJ,CAAC;IACH,CAAC;CACO,CAAC"}
|
package/dist/run.d.ts
DELETED
|
@@ -1,27 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Headless trail execution without mounting a trailhead.
|
|
3
|
-
*
|
|
4
|
-
* Looks up a trail by ID in a topo, then delegates to `executeTrail`.
|
|
5
|
-
* Returns a `Result` and never throws.
|
|
6
|
-
*/
|
|
7
|
-
import type { Topo } from './topo.js';
|
|
8
|
-
import type { ExecuteTrailOptions } from './execute.js';
|
|
9
|
-
import { Result } from './result.js';
|
|
10
|
-
/** Options forwarded to `executeTrail` from `run`. */
|
|
11
|
-
export type RunOptions = ExecuteTrailOptions;
|
|
12
|
-
/**
|
|
13
|
-
* Execute a trail by ID from a topo without mounting a trailhead.
|
|
14
|
-
*
|
|
15
|
-
* Resolves the trail from the topo, then runs it through the standard
|
|
16
|
-
* `executeTrail` pipeline. Returns `Result.err(NotFoundError)` if the
|
|
17
|
-
* trail ID is not registered. Never throws — unexpected exceptions are
|
|
18
|
-
* returned as `Result.err(InternalError)`.
|
|
19
|
-
*
|
|
20
|
-
* @example
|
|
21
|
-
* ```typescript
|
|
22
|
-
* const result = await run(myTopo, 'greet', { name: 'Alice' });
|
|
23
|
-
* if (result.isOk()) console.log(result.value);
|
|
24
|
-
* ```
|
|
25
|
-
*/
|
|
26
|
-
export declare const run: (topo: Topo, id: string, input: unknown, options?: RunOptions) => Promise<Result<unknown, Error>>;
|
|
27
|
-
//# sourceMappingURL=run.d.ts.map
|
package/dist/run.d.ts.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"run.d.ts","sourceRoot":"","sources":["../src/run.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,KAAK,EAAE,IAAI,EAAE,MAAM,WAAW,CAAC;AAEtC,OAAO,KAAK,EAAE,mBAAmB,EAAE,MAAM,cAAc,CAAC;AAExD,OAAO,EAAE,MAAM,EAAE,MAAM,aAAa,CAAC;AAMrC,sDAAsD;AACtD,MAAM,MAAM,UAAU,GAAG,mBAAmB,CAAC;AAM7C;;;;;;;;;;;;;GAaG;AACH,eAAO,MAAM,GAAG,GACd,MAAM,IAAI,EACV,IAAI,MAAM,EACV,OAAO,OAAO,EACd,UAAU,UAAU,KACnB,OAAO,CAAC,MAAM,CAAC,OAAO,EAAE,KAAK,CAAC,CAUhC,CAAC"}
|
package/dist/run.js
DELETED
|
@@ -1,34 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Headless trail execution without mounting a trailhead.
|
|
3
|
-
*
|
|
4
|
-
* Looks up a trail by ID in a topo, then delegates to `executeTrail`.
|
|
5
|
-
* Returns a `Result` and never throws.
|
|
6
|
-
*/
|
|
7
|
-
import { executeTrail } from './execute.js';
|
|
8
|
-
import { NotFoundError } from './errors.js';
|
|
9
|
-
import { Result } from './result.js';
|
|
10
|
-
// ---------------------------------------------------------------------------
|
|
11
|
-
// run()
|
|
12
|
-
// ---------------------------------------------------------------------------
|
|
13
|
-
/**
|
|
14
|
-
* Execute a trail by ID from a topo without mounting a trailhead.
|
|
15
|
-
*
|
|
16
|
-
* Resolves the trail from the topo, then runs it through the standard
|
|
17
|
-
* `executeTrail` pipeline. Returns `Result.err(NotFoundError)` if the
|
|
18
|
-
* trail ID is not registered. Never throws — unexpected exceptions are
|
|
19
|
-
* returned as `Result.err(InternalError)`.
|
|
20
|
-
*
|
|
21
|
-
* @example
|
|
22
|
-
* ```typescript
|
|
23
|
-
* const result = await run(myTopo, 'greet', { name: 'Alice' });
|
|
24
|
-
* if (result.isOk()) console.log(result.value);
|
|
25
|
-
* ```
|
|
26
|
-
*/
|
|
27
|
-
export const run = (topo, id, input, options) => {
|
|
28
|
-
const trail = topo.get(id);
|
|
29
|
-
if (trail === undefined) {
|
|
30
|
-
return Promise.resolve(Result.err(new NotFoundError(`Trail "${id}" not found in topo "${topo.name}"`)));
|
|
31
|
-
}
|
|
32
|
-
return executeTrail(trail, input, options);
|
|
33
|
-
};
|
|
34
|
-
//# sourceMappingURL=run.js.map
|
package/dist/run.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"run.js","sourceRoot":"","sources":["../src/run.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAGH,OAAO,EAAE,YAAY,EAAE,MAAM,cAAc,CAAC;AAE5C,OAAO,EAAE,aAAa,EAAE,MAAM,aAAa,CAAC;AAC5C,OAAO,EAAE,MAAM,EAAE,MAAM,aAAa,CAAC;AASrC,8EAA8E;AAC9E,QAAQ;AACR,8EAA8E;AAE9E;;;;;;;;;;;;;GAaG;AACH,MAAM,CAAC,MAAM,GAAG,GAAG,CACjB,IAAU,EACV,EAAU,EACV,KAAc,EACd,OAAoB,EACa,EAAE;IACnC,MAAM,KAAK,GAAG,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,CAAC;IAC3B,IAAI,KAAK,KAAK,SAAS,EAAE,CAAC;QACxB,OAAO,OAAO,CAAC,OAAO,CACpB,MAAM,CAAC,GAAG,CACR,IAAI,aAAa,CAAC,UAAU,EAAE,wBAAwB,IAAI,CAAC,IAAI,GAAG,CAAC,CACpE,CACF,CAAC;IACJ,CAAC;IACD,OAAO,YAAY,CAAC,KAAK,EAAE,KAAK,EAAE,OAAO,CAAC,CAAC;AAC7C,CAAC,CAAC"}
|
package/dist/serialization.d.ts
DELETED
|
@@ -1,27 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Serialization utilities for @ontrails/core
|
|
3
|
-
*
|
|
4
|
-
* Safe JSON parsing/stringifying and error serialization/deserialization
|
|
5
|
-
* for transport across process boundaries.
|
|
6
|
-
*/
|
|
7
|
-
import type { ErrorCategory, TrailsError } from './errors.js';
|
|
8
|
-
import { ValidationError, InternalError } from './errors.js';
|
|
9
|
-
import { Result } from './result.js';
|
|
10
|
-
export interface SerializedError {
|
|
11
|
-
readonly name: string;
|
|
12
|
-
readonly message: string;
|
|
13
|
-
readonly category?: ErrorCategory | undefined;
|
|
14
|
-
readonly retryable?: boolean | undefined;
|
|
15
|
-
readonly retryAfter?: number | undefined;
|
|
16
|
-
readonly context?: Record<string, unknown> | undefined;
|
|
17
|
-
readonly stack?: string | undefined;
|
|
18
|
-
}
|
|
19
|
-
/** Extract structured data from an Error for transport. */
|
|
20
|
-
export declare const serializeError: (error: Error) => SerializedError;
|
|
21
|
-
/** Reconstruct a TrailsError from serialized data. */
|
|
22
|
-
export declare const deserializeError: (data: SerializedError) => TrailsError;
|
|
23
|
-
/** Parse a JSON string, returning a Result instead of throwing. */
|
|
24
|
-
export declare const safeParse: (json: string) => Result<unknown, ValidationError>;
|
|
25
|
-
/** Stringify a value, returning a Result. Handles circular references. */
|
|
26
|
-
export declare const safeStringify: (value: unknown) => Result<string, InternalError>;
|
|
27
|
-
//# sourceMappingURL=serialization.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"serialization.d.ts","sourceRoot":"","sources":["../src/serialization.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,KAAK,EAAE,aAAa,EAAE,WAAW,EAAE,MAAM,aAAa,CAAC;AAC9D,OAAO,EACL,eAAe,EAUf,aAAa,EAId,MAAM,aAAa,CAAC;AACrB,OAAO,EAAE,MAAM,EAAE,MAAM,aAAa,CAAC;AAMrC,MAAM,WAAW,eAAe;IAC9B,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IACtB,QAAQ,CAAC,OAAO,EAAE,MAAM,CAAC;IACzB,QAAQ,CAAC,QAAQ,CAAC,EAAE,aAAa,GAAG,SAAS,CAAC;IAC9C,QAAQ,CAAC,SAAS,CAAC,EAAE,OAAO,GAAG,SAAS,CAAC;IACzC,QAAQ,CAAC,UAAU,CAAC,EAAE,MAAM,GAAG,SAAS,CAAC;IACzC,QAAQ,CAAC,OAAO,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAAG,SAAS,CAAC;IACvD,QAAQ,CAAC,KAAK,CAAC,EAAE,MAAM,GAAG,SAAS,CAAC;CACrC;AAqFD,2DAA2D;AAC3D,eAAO,MAAM,cAAc,GAAI,OAAO,KAAK,KAAG,eAmB7C,CAAC;AAEF,sDAAsD;AACtD,eAAO,MAAM,gBAAgB,GAAI,MAAM,eAAe,KAAG,WAkBxD,CAAC;AAMF,mEAAmE;AACnE,eAAO,MAAM,SAAS,GAAI,MAAM,MAAM,KAAG,MAAM,CAAC,OAAO,EAAE,eAAe,CAWvE,CAAC;AAEF,0EAA0E;AAC1E,eAAO,MAAM,aAAa,GACxB,OAAO,OAAO,KACb,MAAM,CAAC,MAAM,EAAE,aAAa,CAwC9B,CAAC"}
|
package/dist/serialization.js
DELETED
|
@@ -1,153 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Serialization utilities for @ontrails/core
|
|
3
|
-
*
|
|
4
|
-
* Safe JSON parsing/stringifying and error serialization/deserialization
|
|
5
|
-
* for transport across process boundaries.
|
|
6
|
-
*/
|
|
7
|
-
import { ValidationError, AmbiguousError, AssertionError, NotFoundError, AlreadyExistsError, ConflictError, PermissionError, TimeoutError, RateLimitError, NetworkError, InternalError, AuthError, CancelledError, isTrailsError, } from './errors.js';
|
|
8
|
-
import { Result } from './result.js';
|
|
9
|
-
// ---------------------------------------------------------------------------
|
|
10
|
-
// Internal helpers (defined before usage)
|
|
11
|
-
// ---------------------------------------------------------------------------
|
|
12
|
-
/** Build options object without including undefined context. */
|
|
13
|
-
const buildOpts = (context) => {
|
|
14
|
-
if (context !== undefined) {
|
|
15
|
-
return { context };
|
|
16
|
-
}
|
|
17
|
-
return {};
|
|
18
|
-
};
|
|
19
|
-
const errorFactories = {
|
|
20
|
-
auth: (msg, opts) => new AuthError(msg, opts),
|
|
21
|
-
cancelled: (msg, opts) => new CancelledError(msg, opts),
|
|
22
|
-
conflict: (msg, opts) => new ConflictError(msg, opts),
|
|
23
|
-
internal: (msg, opts) => new InternalError(msg, opts),
|
|
24
|
-
network: (msg, opts) => new NetworkError(msg, opts),
|
|
25
|
-
not_found: (msg, opts) => new NotFoundError(msg, opts),
|
|
26
|
-
permission: (msg, opts) => new PermissionError(msg, opts),
|
|
27
|
-
rate_limit: (msg, opts, retryAfter) => {
|
|
28
|
-
const rlOpts = {
|
|
29
|
-
...opts,
|
|
30
|
-
};
|
|
31
|
-
if (retryAfter !== undefined) {
|
|
32
|
-
rlOpts.retryAfter = retryAfter;
|
|
33
|
-
}
|
|
34
|
-
return new RateLimitError(msg, rlOpts);
|
|
35
|
-
},
|
|
36
|
-
timeout: (msg, opts) => new TimeoutError(msg, opts),
|
|
37
|
-
validation: (msg, opts) => new ValidationError(msg, opts),
|
|
38
|
-
};
|
|
39
|
-
const createErrorByCategory = (category, message, context, retryAfter) => {
|
|
40
|
-
const opts = buildOpts(context);
|
|
41
|
-
const factory = errorFactories[category] ?? errorFactories.internal;
|
|
42
|
-
return factory(message, opts, retryAfter);
|
|
43
|
-
};
|
|
44
|
-
/** Map error class names to their constructors for precise round-tripping. */
|
|
45
|
-
const errorConstructorsByName = {
|
|
46
|
-
AlreadyExistsError: (msg, opts) => new AlreadyExistsError(msg, opts),
|
|
47
|
-
AmbiguousError: (msg, opts) => new AmbiguousError(msg, opts),
|
|
48
|
-
AssertionError: (msg, opts) => new AssertionError(msg, opts),
|
|
49
|
-
AuthError: (msg, opts) => new AuthError(msg, opts),
|
|
50
|
-
CancelledError: (msg, opts) => new CancelledError(msg, opts),
|
|
51
|
-
ConflictError: (msg, opts) => new ConflictError(msg, opts),
|
|
52
|
-
InternalError: (msg, opts) => new InternalError(msg, opts),
|
|
53
|
-
NetworkError: (msg, opts) => new NetworkError(msg, opts),
|
|
54
|
-
NotFoundError: (msg, opts) => new NotFoundError(msg, opts),
|
|
55
|
-
PermissionError: (msg, opts) => new PermissionError(msg, opts),
|
|
56
|
-
RateLimitError: (msg, opts, retryAfter) => {
|
|
57
|
-
const rlOpts = {
|
|
58
|
-
...opts,
|
|
59
|
-
};
|
|
60
|
-
if (retryAfter !== undefined) {
|
|
61
|
-
rlOpts.retryAfter = retryAfter;
|
|
62
|
-
}
|
|
63
|
-
return new RateLimitError(msg, rlOpts);
|
|
64
|
-
},
|
|
65
|
-
TimeoutError: (msg, opts) => new TimeoutError(msg, opts),
|
|
66
|
-
ValidationError: (msg, opts) => new ValidationError(msg, opts),
|
|
67
|
-
};
|
|
68
|
-
// ---------------------------------------------------------------------------
|
|
69
|
-
// Error serialization
|
|
70
|
-
// ---------------------------------------------------------------------------
|
|
71
|
-
/** Extract structured data from an Error for transport. */
|
|
72
|
-
export const serializeError = (error) => {
|
|
73
|
-
const result = {
|
|
74
|
-
message: error.message,
|
|
75
|
-
name: error.name,
|
|
76
|
-
stack: error.stack,
|
|
77
|
-
};
|
|
78
|
-
if (isTrailsError(error)) {
|
|
79
|
-
return {
|
|
80
|
-
...result,
|
|
81
|
-
category: error.category,
|
|
82
|
-
context: error.context,
|
|
83
|
-
retryAfter: error instanceof RateLimitError ? error.retryAfter : undefined,
|
|
84
|
-
retryable: error.retryable,
|
|
85
|
-
};
|
|
86
|
-
}
|
|
87
|
-
return result;
|
|
88
|
-
};
|
|
89
|
-
/** Reconstruct a TrailsError from serialized data. */
|
|
90
|
-
export const deserializeError = (data) => {
|
|
91
|
-
const opts = buildOpts(data.context);
|
|
92
|
-
const nameFactory = errorConstructorsByName[data.name];
|
|
93
|
-
const error = nameFactory
|
|
94
|
-
? nameFactory(data.message, opts, data.retryAfter)
|
|
95
|
-
: createErrorByCategory(data.category ?? 'internal', data.message, data.context, data.retryAfter);
|
|
96
|
-
if (data.stack) {
|
|
97
|
-
error.stack = data.stack;
|
|
98
|
-
}
|
|
99
|
-
return error;
|
|
100
|
-
};
|
|
101
|
-
// ---------------------------------------------------------------------------
|
|
102
|
-
// Safe JSON
|
|
103
|
-
// ---------------------------------------------------------------------------
|
|
104
|
-
/** Parse a JSON string, returning a Result instead of throwing. */
|
|
105
|
-
export const safeParse = (json) => {
|
|
106
|
-
try {
|
|
107
|
-
return Result.ok(JSON.parse(json));
|
|
108
|
-
}
|
|
109
|
-
catch (error) {
|
|
110
|
-
return Result.err(new ValidationError('Invalid JSON', {
|
|
111
|
-
cause: error instanceof Error ? error : new Error(String(error)),
|
|
112
|
-
context: { input: json.slice(0, 200) },
|
|
113
|
-
}));
|
|
114
|
-
}
|
|
115
|
-
};
|
|
116
|
-
/** Stringify a value, returning a Result. Handles circular references. */
|
|
117
|
-
export const safeStringify = (value) => {
|
|
118
|
-
try {
|
|
119
|
-
// Track the current ancestor chain, not every object ever visited.
|
|
120
|
-
// This allows shared references in a DAG while still detecting cycles.
|
|
121
|
-
const stack = [];
|
|
122
|
-
const keys = [];
|
|
123
|
-
const json = JSON.stringify(value, function json(key, val) {
|
|
124
|
-
if (stack.length > 0) {
|
|
125
|
-
// `this` is the object that contains `key`. Trim the stack back
|
|
126
|
-
// to `this` so we only track the current ancestor path.
|
|
127
|
-
const thisIndex = stack.lastIndexOf(this);
|
|
128
|
-
stack.splice(thisIndex + 1);
|
|
129
|
-
keys.splice(thisIndex);
|
|
130
|
-
}
|
|
131
|
-
if (typeof val === 'object' && val !== null) {
|
|
132
|
-
if (stack.includes(val)) {
|
|
133
|
-
return '[Circular]';
|
|
134
|
-
}
|
|
135
|
-
stack.push(val);
|
|
136
|
-
keys.push(key);
|
|
137
|
-
}
|
|
138
|
-
return val;
|
|
139
|
-
});
|
|
140
|
-
if (json === undefined) {
|
|
141
|
-
return Result.err(new InternalError('Value is not JSON-serializable', {
|
|
142
|
-
context: { type: typeof value },
|
|
143
|
-
}));
|
|
144
|
-
}
|
|
145
|
-
return Result.ok(json);
|
|
146
|
-
}
|
|
147
|
-
catch (error) {
|
|
148
|
-
return Result.err(new InternalError('Failed to stringify value', {
|
|
149
|
-
cause: error instanceof Error ? error : new Error(String(error)),
|
|
150
|
-
}));
|
|
151
|
-
}
|
|
152
|
-
};
|
|
153
|
-
//# sourceMappingURL=serialization.js.map
|