@vite-hub/workflow 0.1.0 → 0.3.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +144 -43
- package/dist/{app-CzPoP84f.js → app-CmMq8l4t.js} +1 -1
- package/dist/client-CU5rELC0.js +658 -0
- package/dist/{cloudflare-nF4abPd4.js → cloudflare-BfFnNWB0.js} +2 -2
- package/dist/{cloudflare-env-BNrYo29x.js → cloudflare-env-CGLx7lUd.js} +44 -42
- package/dist/cloudflare-env-lookup-CfX6wZKb.d.ts +4 -0
- package/dist/{cloudflare-env-CIjdFTv6.d.ts → cloudflare-shared-CTqh_a2s.d.ts} +2 -2
- package/dist/{config-IJMb0eeZ.js → config-DCvRDlxA.js} +41 -40
- package/dist/definition.d.ts +19 -0
- package/dist/definition.js +31 -0
- package/dist/delay--ESDybVT.js +81 -0
- package/dist/dev-recorder-CTN5Fkao.js +10 -0
- package/dist/durability.d.ts +23 -0
- package/dist/durability.js +62 -0
- package/dist/error-diagnostics-8YNJ37rP.js +286 -0
- package/dist/error-diagnostics-DSkCJC_L.js +266 -0
- package/dist/errors-DCqRnZpN.js +35 -0
- package/dist/errors-DVwFb6Fd.d.ts +56 -0
- package/dist/index.d.ts +13 -60
- package/dist/index.js +6 -17
- package/dist/object-Da1zyPCR.js +11 -0
- package/dist/openworkflow-TEaNk0LV.d.ts +50 -0
- package/dist/openworkflow-defaults-DDig2M4s.js +26 -0
- package/dist/openworkflow-storage-Dj9zUAsk.js +28 -0
- package/dist/openworkflow.d.ts +2 -0
- package/dist/openworkflow.js +2 -0
- package/dist/{operations-tNUdj3G4.d.ts → operation-definition-8gOe-ubD.d.ts} +37 -15
- package/dist/operations-B0L8kV5s.js +329 -0
- package/dist/operations.d.ts +40 -6
- package/dist/operations.js +163 -736
- package/dist/{out-CsaJxg8m.js → out-fRNaVslw.js} +31 -1429
- package/dist/run-error-DBWqiJHH.js +110 -0
- package/dist/runtime/cloudflare-runner.d.ts +3 -5
- package/dist/runtime/cloudflare-runner.js +24 -42
- package/dist/runtime/cloudflare-shared.d.ts +2 -2
- package/dist/runtime/cloudflare-shared.js +1 -1
- package/dist/runtime/cloudflare-vite.d.ts +3 -2
- package/dist/runtime/cloudflare-vite.js +47 -1
- package/dist/runtime/dev-runs.d.ts +4 -0
- package/dist/runtime/dev-runs.js +73 -0
- package/dist/runtime/execute.d.ts +20 -4
- package/dist/runtime/execute.js +118 -12
- package/dist/runtime/openworkflow-worker.d.ts +29 -2
- package/dist/runtime/openworkflow-worker.js +156 -38
- package/dist/runtime/openworkflow.d.ts +2 -21
- package/dist/runtime/openworkflow.js +273 -36
- package/dist/runtime/state.d.ts +3 -1
- package/dist/runtime/state.js +11 -7
- package/dist/runtime/vercel-vite.d.ts +1 -1
- package/dist/runtime/vercel-vite.js +7 -4
- package/dist/types-CnKVDnEp.d.ts +241 -0
- package/dist/{vercel-D-ZNtB_u.js → vercel-BZU_O6ay.js} +51 -22
- package/dist/vite.d.ts +219 -27
- package/dist/vite.js +1787 -1183
- package/package.json +28 -50
- package/dist/client-DsGM3M4U.js +0 -389
- package/dist/cloudflare-shared-CaxrMD1A.d.ts +0 -1
- package/dist/cloudflare-vite-B3IabBsv.js +0 -47
- package/dist/error-diagnostics-A_8Kp-48.js +0 -116
- package/dist/error-diagnostics-pwNFvAHP.js +0 -67
- package/dist/operations-B_rYG71W.js +0 -230
- package/dist/provider-operation-CW2cYfVy.js +0 -65
- package/dist/runtime-type-DBCvIh5x.js +0 -7
- package/dist/types-D82WmfZa.d.ts +0 -180
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
import { n as createWorkflowError } from "./errors-DCqRnZpN.js";
|
|
2
|
+
import { t as getWorkflowDevRecorder } from "./dev-recorder-CTN5Fkao.js";
|
|
3
|
+
import { t as redactInspectionText } from "./inspect-B72iFyj-.js";
|
|
4
|
+
import { getViteHubErrorShape } from "@vite-hub/runtime";
|
|
5
|
+
//#region src/runtime/provider-operation.ts
|
|
6
|
+
function isWorkflowBoundaryError(error) {
|
|
7
|
+
if (getViteHubErrorShape(error)) return true;
|
|
8
|
+
if (typeof error !== "object" || error === null) return false;
|
|
9
|
+
try {
|
|
10
|
+
return "name" in error && error.name === "AbortError";
|
|
11
|
+
} catch {
|
|
12
|
+
return false;
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
function getWorkflowProviderStatus(error) {
|
|
16
|
+
if (typeof error !== "object" || error === null) return void 0;
|
|
17
|
+
try {
|
|
18
|
+
const value = error;
|
|
19
|
+
const response = typeof value.response === "object" && value.response !== null ? value.response : void 0;
|
|
20
|
+
const status = value.status ?? value.statusCode ?? response?.status;
|
|
21
|
+
return typeof status === "number" && Number.isInteger(status) && status >= 400 && status <= 599 ? status : void 0;
|
|
22
|
+
} catch {
|
|
23
|
+
return;
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
const maxReasonLength = 200;
|
|
27
|
+
/**
|
|
28
|
+
* A short reason that is safe to send to clients: the provider error name and the first line of its message. URLs,
|
|
29
|
+
* file paths, and credentials are removed, and long opaque values (ids, tokens) are replaced.
|
|
30
|
+
*/
|
|
31
|
+
function getWorkflowProviderReason(error) {
|
|
32
|
+
if (typeof error !== "object" || error === null) return void 0;
|
|
33
|
+
let name;
|
|
34
|
+
let message;
|
|
35
|
+
try {
|
|
36
|
+
name = Reflect.get(error, "name");
|
|
37
|
+
message = Reflect.get(error, "message");
|
|
38
|
+
} catch {
|
|
39
|
+
return;
|
|
40
|
+
}
|
|
41
|
+
const firstLine = typeof message === "string" ? message.split(/\r?\n/, 1)[0].trim() : "";
|
|
42
|
+
const safeName = typeof name === "string" && /^[A-Za-z][\w$]{0,63}$/.test(name) ? name : void 0;
|
|
43
|
+
if (!firstLine) return safeName;
|
|
44
|
+
const text = redactInspectionText(firstLine).replace(/\b[a-z][a-z0-9+.-]*:\/\/\S+/gi, "[url]").replace(/(?:\b[A-Za-z]:)?(?:[\\/][\w.@~+-]+){2,}[\\/]?/g, "[path]").replace(/[\w+/=-]{32,}/g, "[redacted]");
|
|
45
|
+
const reason = safeName ? `${safeName}: ${text}` : text;
|
|
46
|
+
return reason.length > maxReasonLength ? `${reason.slice(0, 199)}…` : reason;
|
|
47
|
+
}
|
|
48
|
+
/** `vite dev` installs the run history. Elsewhere, `NODE_ENV` says whether the app runs a development server. */
|
|
49
|
+
function isDevelopment() {
|
|
50
|
+
return Boolean(getWorkflowDevRecorder()) || globalThis.process?.env?.NODE_ENV === "development";
|
|
51
|
+
}
|
|
52
|
+
async function runWorkflowProviderOperation(provider, operation, run, options = {}) {
|
|
53
|
+
try {
|
|
54
|
+
return await run();
|
|
55
|
+
} catch (error) {
|
|
56
|
+
if (options.boundaryError?.(error) || isWorkflowBoundaryError(error)) throw error;
|
|
57
|
+
const status = getWorkflowProviderStatus(error);
|
|
58
|
+
const acknowledgement = options.acknowledgement?.(error, status);
|
|
59
|
+
const reason = getWorkflowProviderReason(error);
|
|
60
|
+
const details = {
|
|
61
|
+
operation,
|
|
62
|
+
provider
|
|
63
|
+
};
|
|
64
|
+
if (acknowledgement) details.acknowledgement = acknowledgement;
|
|
65
|
+
if (reason) details.reason = reason;
|
|
66
|
+
if (status !== void 0) details.status = status;
|
|
67
|
+
if (isDevelopment()) console.error(`[vitehub:workflow] Workflow ${operation} failed in the ${provider} provider.`, error);
|
|
68
|
+
throw createWorkflowError({
|
|
69
|
+
cause: error,
|
|
70
|
+
code: "WORKFLOW_PROVIDER_OPERATION_FAILED",
|
|
71
|
+
details
|
|
72
|
+
});
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
function safeWorkflowName(name) {
|
|
76
|
+
return /^[A-Za-z0-9][A-Za-z0-9._-]{0,127}$/.test(name) ? name : void 0;
|
|
77
|
+
}
|
|
78
|
+
//#endregion
|
|
79
|
+
//#region src/runtime/run-error.ts
|
|
80
|
+
function ownString(value, key) {
|
|
81
|
+
try {
|
|
82
|
+
const member = Reflect.get(value, key);
|
|
83
|
+
return typeof member === "string" && member ? member : void 0;
|
|
84
|
+
} catch {
|
|
85
|
+
return;
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
/**
|
|
89
|
+
* Converts the failure that a provider stores to the JSON-safe `error` of a failed run. It keeps `message`, `code`,
|
|
90
|
+
* and a `name` other than `Error`, and drops the stack. A failure without a message gets a generic message, so every
|
|
91
|
+
* failed run has an `error`.
|
|
92
|
+
*/
|
|
93
|
+
function toWorkflowRunError(value) {
|
|
94
|
+
if (typeof value === "string" && value) return { message: value };
|
|
95
|
+
if (typeof value === "object" && value !== null) {
|
|
96
|
+
const message = ownString(value, "message");
|
|
97
|
+
if (message !== void 0) {
|
|
98
|
+
const code = ownString(value, "code");
|
|
99
|
+
const name = ownString(value, "name");
|
|
100
|
+
return {
|
|
101
|
+
...code ? { code } : {},
|
|
102
|
+
message,
|
|
103
|
+
...name && name !== "Error" ? { name } : {}
|
|
104
|
+
};
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
return { message: "Workflow run failed." };
|
|
108
|
+
}
|
|
109
|
+
//#endregion
|
|
110
|
+
export { safeWorkflowName as a, runWorkflowProviderOperation as i, getWorkflowProviderStatus as n, isWorkflowBoundaryError as r, toWorkflowRunError as t };
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { t as CloudflareWorkerEnv } from "../cloudflare-env-
|
|
1
|
+
import { T as WorkflowProviderStep, l as ResolvedWorkflowOptions, m as WorkflowDefinitionRegistry } from "../types-CnKVDnEp.js";
|
|
2
|
+
import { t as CloudflareWorkerEnv } from "../cloudflare-env-lookup-CfX6wZKb.js";
|
|
3
3
|
//#region src/runtime/cloudflare-runner.d.ts
|
|
4
4
|
export interface CloudflareWorkflowEvent {
|
|
5
5
|
id?: string;
|
|
@@ -8,9 +8,7 @@ export interface CloudflareWorkflowEvent {
|
|
|
8
8
|
}
|
|
9
9
|
export interface RunCloudflareWorkflowOptions {
|
|
10
10
|
config: false | ResolvedWorkflowOptions | undefined;
|
|
11
|
-
createNonRetryableError?: (error: Error
|
|
12
|
-
isRetryable: false;
|
|
13
|
-
}) => Error;
|
|
11
|
+
createNonRetryableError?: (error: Error) => Error;
|
|
14
12
|
env: CloudflareWorkerEnv;
|
|
15
13
|
event: CloudflareWorkflowEvent;
|
|
16
14
|
name: string;
|
|
@@ -1,47 +1,17 @@
|
|
|
1
|
-
import { t as workflowErrorDiagnostics } from "../error-diagnostics-
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
1
|
+
import { t as workflowErrorDiagnostics } from "../error-diagnostics-8YNJ37rP.js";
|
|
2
|
+
import { n as createWorkflowError, r as isNonRetryableError } from "../errors-DCqRnZpN.js";
|
|
3
|
+
import { r as runWithActiveCloudflareEnv } from "../cloudflare-env-CGLx7lUd.js";
|
|
4
|
+
import { t as fromCloudflareStartParams } from "../delay--ESDybVT.js";
|
|
4
5
|
import { runSerializedWorkflowHandler } from "./execute.js";
|
|
5
6
|
import { loadWorkflowDefinition, runWithWorkflowRuntimeEvent, setWorkflowRuntimeConfig, setWorkflowRuntimeRegistry } from "./state.js";
|
|
6
7
|
//#region src/runtime/cloudflare-runner.ts
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
try {
|
|
10
|
-
const candidate = error;
|
|
11
|
-
if (Reflect.get(candidate, "isRetryable") !== false) return false;
|
|
12
|
-
const nativeIsError = Reflect.get(Error, "isError");
|
|
13
|
-
if (hasRuntimeType(nativeIsError, "function")) return nativeIsError(candidate) === true;
|
|
14
|
-
const clone = Reflect.get(globalThis, "structuredClone");
|
|
15
|
-
if (!hasRuntimeType(clone, "function")) return false;
|
|
16
|
-
try {
|
|
17
|
-
return clone(candidate) instanceof Error;
|
|
18
|
-
} catch {
|
|
19
|
-
const descriptors = /* @__PURE__ */ new Map();
|
|
20
|
-
try {
|
|
21
|
-
for (const key of Reflect.ownKeys(candidate)) {
|
|
22
|
-
const descriptor = Reflect.getOwnPropertyDescriptor(candidate, key);
|
|
23
|
-
if (!descriptor?.configurable || !("value" in descriptor)) continue;
|
|
24
|
-
descriptors.set(key, descriptor);
|
|
25
|
-
Reflect.defineProperty(candidate, key, {
|
|
26
|
-
...descriptor,
|
|
27
|
-
value: void 0
|
|
28
|
-
});
|
|
29
|
-
}
|
|
30
|
-
try {
|
|
31
|
-
return clone(candidate) instanceof Error;
|
|
32
|
-
} catch {
|
|
33
|
-
return false;
|
|
34
|
-
}
|
|
35
|
-
} finally {
|
|
36
|
-
for (const [key, descriptor] of descriptors) Reflect.defineProperty(candidate, key, descriptor);
|
|
37
|
-
}
|
|
38
|
-
}
|
|
39
|
-
} catch {
|
|
40
|
-
return false;
|
|
41
|
-
}
|
|
42
|
-
}
|
|
8
|
+
/** The Cloudflare errors that a step already made. They pass the run boundary as they are, not wrapped again. */
|
|
9
|
+
const cloudflareNonRetryableErrors = /* @__PURE__ */ new WeakSet();
|
|
43
10
|
function translateNonRetryableError(error, createNonRetryableError) {
|
|
44
|
-
|
|
11
|
+
if (!createNonRetryableError || !isNonRetryableError(error) || cloudflareNonRetryableErrors.has(error)) return error;
|
|
12
|
+
const translated = createNonRetryableError(error);
|
|
13
|
+
cloudflareNonRetryableErrors.add(translated);
|
|
14
|
+
return translated;
|
|
45
15
|
}
|
|
46
16
|
function wrapCloudflareWorkflowStep(step, createNonRetryableError, env, runtimeEvent) {
|
|
47
17
|
if (!step?.do) return step;
|
|
@@ -55,7 +25,8 @@ function wrapCloudflareWorkflowStep(step, createNonRetryableError, env, runtimeE
|
|
|
55
25
|
}
|
|
56
26
|
})));
|
|
57
27
|
},
|
|
58
|
-
...step.sleep ? { sleep: step.sleep
|
|
28
|
+
...step.sleep ? { sleep: async (name, milliseconds) => await step.sleep(name, milliseconds) } : {},
|
|
29
|
+
...step.sleepUntil ? { sleepUntil: async (name, date) => await step.sleepUntil(name, date) } : {}
|
|
59
30
|
};
|
|
60
31
|
}
|
|
61
32
|
async function runCloudflareWorkflow({ config, createNonRetryableError, env, event, name, registry, step }) {
|
|
@@ -70,11 +41,22 @@ async function runCloudflareWorkflow({ config, createNonRetryableError, env, eve
|
|
|
70
41
|
return await runWithActiveCloudflareEnv(env, async () => {
|
|
71
42
|
const definition = await loadWorkflowDefinition(name);
|
|
72
43
|
if (!definition) throw workflowErrorDiagnostics.WORKFLOW_R0015({ message: `Missing workflow definition: ${name}` });
|
|
44
|
+
const { payload, startAt } = fromCloudflareStartParams(event?.payload);
|
|
45
|
+
if (startAt !== void 0) {
|
|
46
|
+
if (!step?.sleepUntil) throw createWorkflowError({
|
|
47
|
+
code: "WORKFLOW_OPERATION_UNSUPPORTED",
|
|
48
|
+
details: {
|
|
49
|
+
operation: "delay",
|
|
50
|
+
provider: "cloudflare"
|
|
51
|
+
}
|
|
52
|
+
});
|
|
53
|
+
await step.sleepUntil("vitehub-delay", new Date(startAt));
|
|
54
|
+
}
|
|
73
55
|
try {
|
|
74
56
|
return await runWithWorkflowRuntimeEvent(runtimeEvent, () => runSerializedWorkflowHandler({
|
|
75
57
|
id: event?.instanceId || event?.id,
|
|
76
58
|
name,
|
|
77
|
-
payload
|
|
59
|
+
payload,
|
|
78
60
|
provider: "cloudflare",
|
|
79
61
|
step: providerStep
|
|
80
62
|
}, definition));
|
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import "../cloudflare-shared-
|
|
1
|
+
import { t as CloudflareWorkerEnv } from "../cloudflare-env-lookup-CfX6wZKb.js";
|
|
2
|
+
import { i as setActiveCloudflareEnv, n as createCloudflareRuntimeEvent, r as runWithActiveCloudflareEnv, t as CloudflareWorkerExecutionContext } from "../cloudflare-shared-CTqh_a2s.js";
|
|
3
3
|
export { type CloudflareWorkerEnv, type CloudflareWorkerExecutionContext, createCloudflareRuntimeEvent, runWithActiveCloudflareEnv, setActiveCloudflareEnv };
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { i as setActiveCloudflareEnv, r as runWithActiveCloudflareEnv, t as createCloudflareRuntimeEvent } from "../cloudflare-env-CGLx7lUd.js";
|
|
2
2
|
export { createCloudflareRuntimeEvent, runWithActiveCloudflareEnv, setActiveCloudflareEnv };
|
|
@@ -1,6 +1,7 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { l as ResolvedWorkflowOptions, m as WorkflowDefinitionRegistry } from "../types-CnKVDnEp.js";
|
|
2
|
+
import { t as CloudflareWorkerEnv } from "../cloudflare-env-lookup-CfX6wZKb.js";
|
|
3
|
+
import { t as CloudflareWorkerExecutionContext } from "../cloudflare-shared-CTqh_a2s.js";
|
|
2
4
|
import { t as VitehubApp } from "../app-BojnC_yf.js";
|
|
3
|
-
import { n as CloudflareWorkerExecutionContext, t as CloudflareWorkerEnv } from "../cloudflare-env-CIjdFTv6.js";
|
|
4
5
|
//#region src/runtime/cloudflare-vite.d.ts
|
|
5
6
|
export type CloudflareWorkerApp = VitehubApp;
|
|
6
7
|
export interface WorkflowCloudflareWorkerOptions {
|
|
@@ -1,2 +1,48 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { r as runWithActiveCloudflareEnv, t as createCloudflareRuntimeEvent } from "../cloudflare-env-CGLx7lUd.js";
|
|
2
|
+
import { t as normalizeWorkflowOptions } from "../config-DCvRDlxA.js";
|
|
3
|
+
import { runWithWorkflowRuntimeEvent, setWorkflowRuntimeConfig, setWorkflowRuntimeRegistry } from "./state.js";
|
|
4
|
+
import { t as resolveAppFetch } from "../app-CmMq8l4t.js";
|
|
5
|
+
import "./cloudflare-shared.js";
|
|
6
|
+
//#region ../internal/src/runtime/cloudflare-hosted.ts
|
|
7
|
+
function createWebRequestHandler(appHandler) {
|
|
8
|
+
return async (request, context) => await Promise.resolve(appHandler ? appHandler(request, context) : new Response(JSON.stringify({
|
|
9
|
+
status: 404,
|
|
10
|
+
message: `Cannot find any route matching [${request.method}] ${request.url}`
|
|
11
|
+
}), {
|
|
12
|
+
headers: { "content-type": "application/json;charset=UTF-8" },
|
|
13
|
+
status: 404,
|
|
14
|
+
statusText: "Not Found"
|
|
15
|
+
}));
|
|
16
|
+
}
|
|
17
|
+
function createCloudflareHostedWorker(adapter) {
|
|
18
|
+
const requestHandler = createWebRequestHandler(resolveAppFetch(adapter.label, adapter.app));
|
|
19
|
+
return { async fetch(request, env, context) {
|
|
20
|
+
const handle = async (handlerContext) => await Promise.resolve(requestHandler(request, handlerContext));
|
|
21
|
+
if (adapter.onRequest) return await adapter.onRequest({
|
|
22
|
+
env,
|
|
23
|
+
executionContext: context,
|
|
24
|
+
handle
|
|
25
|
+
});
|
|
26
|
+
return await handle();
|
|
27
|
+
} };
|
|
28
|
+
}
|
|
29
|
+
//#endregion
|
|
30
|
+
//#region src/runtime/cloudflare-vite.ts
|
|
31
|
+
function installWorkflowCloudflareRuntime(options = {}) {
|
|
32
|
+
const workflowConfig = options.workflow === false ? false : normalizeWorkflowOptions(options.workflow, { hosting: "cloudflare" });
|
|
33
|
+
setWorkflowRuntimeConfig(workflowConfig);
|
|
34
|
+
setWorkflowRuntimeRegistry(options.registry);
|
|
35
|
+
}
|
|
36
|
+
function createWorkflowCloudflareWorker(options = {}) {
|
|
37
|
+
return createCloudflareHostedWorker({
|
|
38
|
+
app: options.app,
|
|
39
|
+
label: "workflow",
|
|
40
|
+
async onRequest({ env, executionContext, handle }) {
|
|
41
|
+
installWorkflowCloudflareRuntime(options);
|
|
42
|
+
const runtimeEvent = createCloudflareRuntimeEvent(env, executionContext);
|
|
43
|
+
return await runWithActiveCloudflareEnv(env, () => runWithWorkflowRuntimeEvent(runtimeEvent, () => handle(runtimeEvent.context)));
|
|
44
|
+
}
|
|
45
|
+
});
|
|
46
|
+
}
|
|
47
|
+
//#endregion
|
|
2
48
|
export { createWorkflowCloudflareWorker, installWorkflowCloudflareRuntime };
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
import { n as setWorkflowDevRecorder, t as getWorkflowDevRecorder } from "../dev-recorder-CTN5Fkao.js";
|
|
2
|
+
import { AsyncLocalStorage } from "node:async_hooks";
|
|
3
|
+
//#region src/runtime/dev-runs.ts
|
|
4
|
+
function createWorkflowDevRecorder() {
|
|
5
|
+
const runs = /* @__PURE__ */ new Map();
|
|
6
|
+
const activeRun = new AsyncLocalStorage();
|
|
7
|
+
return {
|
|
8
|
+
clear() {
|
|
9
|
+
const count = runs.size;
|
|
10
|
+
runs.clear();
|
|
11
|
+
return count;
|
|
12
|
+
},
|
|
13
|
+
list: () => [...runs.values()].reverse(),
|
|
14
|
+
start(workflow, id, provider) {
|
|
15
|
+
const key = JSON.stringify([workflow, id]);
|
|
16
|
+
const record = {
|
|
17
|
+
createdAt: /* @__PURE__ */ new Date(),
|
|
18
|
+
id,
|
|
19
|
+
provider,
|
|
20
|
+
status: "queued",
|
|
21
|
+
steps: [],
|
|
22
|
+
workflow
|
|
23
|
+
};
|
|
24
|
+
runs.delete(key);
|
|
25
|
+
runs.set(key, record);
|
|
26
|
+
while (runs.size > 200) runs.delete(runs.keys().next().value);
|
|
27
|
+
return async (run) => {
|
|
28
|
+
record.startedAt = /* @__PURE__ */ new Date();
|
|
29
|
+
record.status = "running";
|
|
30
|
+
try {
|
|
31
|
+
const result = await activeRun.run(record, run);
|
|
32
|
+
record.result = result;
|
|
33
|
+
record.status = "completed";
|
|
34
|
+
return result;
|
|
35
|
+
} catch (error) {
|
|
36
|
+
record.error = error;
|
|
37
|
+
record.status = "failed";
|
|
38
|
+
throw error;
|
|
39
|
+
} finally {
|
|
40
|
+
record.completedAt = /* @__PURE__ */ new Date();
|
|
41
|
+
}
|
|
42
|
+
};
|
|
43
|
+
},
|
|
44
|
+
async step(name, run) {
|
|
45
|
+
const record = activeRun.getStore();
|
|
46
|
+
if (!record || name === record.workflow) return await run();
|
|
47
|
+
const step = {
|
|
48
|
+
name,
|
|
49
|
+
startedAt: /* @__PURE__ */ new Date(),
|
|
50
|
+
status: "running"
|
|
51
|
+
};
|
|
52
|
+
record.steps.push(step);
|
|
53
|
+
try {
|
|
54
|
+
const output = await run();
|
|
55
|
+
step.output = output;
|
|
56
|
+
step.status = "completed";
|
|
57
|
+
return output;
|
|
58
|
+
} catch (error) {
|
|
59
|
+
step.error = error;
|
|
60
|
+
step.status = "failed";
|
|
61
|
+
throw error;
|
|
62
|
+
} finally {
|
|
63
|
+
step.completedAt = /* @__PURE__ */ new Date();
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
};
|
|
67
|
+
}
|
|
68
|
+
/** Turns on the run history. Only the dev Nitro plugin calls it. A second call keeps the history. */
|
|
69
|
+
function enableWorkflowDevRuns() {
|
|
70
|
+
if (!getWorkflowDevRecorder()) setWorkflowDevRecorder(createWorkflowDevRecorder());
|
|
71
|
+
}
|
|
72
|
+
//#endregion
|
|
73
|
+
export { enableWorkflowDevRuns };
|
|
@@ -1,12 +1,28 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
1
|
+
import { I as WorkflowStepFunction, L as WorkflowStepOptions, f as WorkflowDefinition, w as WorkflowProviderContext } from "../types-CnKVDnEp.js";
|
|
2
|
+
import { t as NonRetryableError } from "../errors-DVwFb6Fd.js";
|
|
3
3
|
//#region src/runtime/execute.d.ts
|
|
4
|
-
export declare function runProviderStep<TResult>(step:
|
|
4
|
+
export declare function runProviderStep<TResult>(step: WorkflowProviderContext["step"], name: string, run: () => TResult | Promise<TResult>, options?: WorkflowStepOptions): Promise<TResult>;
|
|
5
|
+
/** `01.create-user.ts` becomes the `createUser` step. */
|
|
6
|
+
export declare function toStepPropertyName(file: string): string;
|
|
5
7
|
export declare function createWorkflowSteps(context: WorkflowProviderContext, stepModules: Array<{
|
|
6
8
|
name: string;
|
|
7
9
|
run: WorkflowStepFunction;
|
|
8
10
|
}>): Record<string, WorkflowStepFunction>;
|
|
11
|
+
/**
|
|
12
|
+
* The step result that says the step threw a `NonRetryableError`. A provider that retries every error, such as
|
|
13
|
+
* OpenWorkflow, ends the step with it, so it does not run the step again. The caller throws the error outside the
|
|
14
|
+
* step, and a replay reads the stored result and throws the same error.
|
|
15
|
+
*/
|
|
16
|
+
export interface NonRetryableStepResult {
|
|
17
|
+
"~vitehubNonRetryableError": {
|
|
18
|
+
code?: string;
|
|
19
|
+
message: string;
|
|
20
|
+
name: string;
|
|
21
|
+
};
|
|
22
|
+
}
|
|
23
|
+
export declare function toNonRetryableStepResult(error: unknown): NonRetryableStepResult | undefined;
|
|
24
|
+
export declare function readNonRetryableStepResult(value: unknown): NonRetryableError | undefined;
|
|
9
25
|
export declare function runWorkflowHandler<TPayload, TResult>(context: WorkflowProviderContext<TPayload>, definition: WorkflowDefinition<TPayload, TResult>): Promise<Response>;
|
|
10
26
|
/** Encode inside the root step so both checkpoints and final outputs survive replay. */
|
|
11
|
-
export declare function runSerializedWorkflowHandler<TPayload, TResult>(context: WorkflowProviderContext<TPayload>, definition: WorkflowDefinition<TPayload, TResult>): Promise<
|
|
27
|
+
export declare function runSerializedWorkflowHandler<TPayload, TResult>(context: WorkflowProviderContext<TPayload>, definition: WorkflowDefinition<TPayload, TResult>): Promise<unknown>;
|
|
12
28
|
//#endregion
|
package/dist/runtime/execute.js
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
|
-
import { t as workflowErrorDiagnostics } from "../error-diagnostics-
|
|
2
|
-
import {
|
|
1
|
+
import { t as workflowErrorDiagnostics } from "../error-diagnostics-8YNJ37rP.js";
|
|
2
|
+
import { r as isNonRetryableError, t as NonRetryableError } from "../errors-DCqRnZpN.js";
|
|
3
|
+
import { t as getWorkflowDevRecorder } from "../dev-recorder-CTN5Fkao.js";
|
|
4
|
+
import { isResponse, serializeResponse, toResponse } from "@vite-hub/runtime";
|
|
3
5
|
//#region src/runtime/execute.ts
|
|
4
6
|
const defaultStepOptions = { retries: {
|
|
5
7
|
backoff: "exponential",
|
|
@@ -8,8 +10,10 @@ const defaultStepOptions = { retries: {
|
|
|
8
10
|
} };
|
|
9
11
|
async function runProviderStep(step, name, run, options = defaultStepOptions) {
|
|
10
12
|
if (typeof step?.do === "function") return await step.do(name, options, run);
|
|
11
|
-
|
|
13
|
+
const recorder = getWorkflowDevRecorder();
|
|
14
|
+
return recorder ? await recorder.step(name, run) : await run();
|
|
12
15
|
}
|
|
16
|
+
/** `01.create-user.ts` becomes the `createUser` step. */
|
|
13
17
|
function toStepPropertyName(file) {
|
|
14
18
|
return file.split("/").pop().replace(/\.(?:c|m)?[jt]s$/i, "").replace(/^\d+[.-]?/, "").replace(/[-_.]+([a-z0-9])/gi, (_, char) => char.toUpperCase());
|
|
15
19
|
}
|
|
@@ -24,25 +28,127 @@ function createWorkflowSteps(context, stepModules) {
|
|
|
24
28
|
return [propertyName, async (input) => await runProviderStep(context.step, `${context.name}/${module.name}`, () => module.run(input))];
|
|
25
29
|
}));
|
|
26
30
|
}
|
|
31
|
+
/** The units of a sleep duration, in milliseconds. Cloudflare reads a month as 30 days and a year as 365.25 days. */
|
|
32
|
+
const durationUnits = {
|
|
33
|
+
second: 1e3,
|
|
34
|
+
minute: 6e4,
|
|
35
|
+
hour: 36e5,
|
|
36
|
+
day: 864e5,
|
|
37
|
+
week: 6048e5,
|
|
38
|
+
month: 2592e6,
|
|
39
|
+
year: 315576e5
|
|
40
|
+
};
|
|
41
|
+
const durationPattern = /^(\d+(?:\.\d+)?) (second|minute|hour|day|week|month|year)s?$/;
|
|
42
|
+
/** A sleep target: a number of milliseconds, or a date. */
|
|
43
|
+
function toSleepTarget(name, until) {
|
|
44
|
+
if (Object.prototype.toString.call(until) === "[object Date]") {
|
|
45
|
+
const date = new Date(until.getTime());
|
|
46
|
+
if (Number.isFinite(date.getTime())) return date;
|
|
47
|
+
} else if (typeof until === "number") {
|
|
48
|
+
if (Number.isFinite(until) && until >= 0) return until;
|
|
49
|
+
} else if (typeof until === "string") {
|
|
50
|
+
const match = durationPattern.exec(until);
|
|
51
|
+
if (match) return Number(match[1]) * durationUnits[match[2]];
|
|
52
|
+
}
|
|
53
|
+
const value = typeof until === "string" ? JSON.stringify(until) : String(until);
|
|
54
|
+
throw workflowErrorDiagnostics.WORKFLOW_SLEEP_DURATION_INVALID({
|
|
55
|
+
name,
|
|
56
|
+
value
|
|
57
|
+
});
|
|
58
|
+
}
|
|
59
|
+
/** Node fires a timer longer than this at once, so a longer wait uses more than one timer. */
|
|
60
|
+
const maxTimerDelay = 2147483647;
|
|
61
|
+
async function waitUntil(time) {
|
|
62
|
+
for (let left = time - Date.now(); left > 0; left = time - Date.now()) await new Promise((resolve) => setTimeout(resolve, Math.min(left, maxTimerDelay)));
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* `context.sleep()` of a folder Workflow. It names each sleep by call order (`sleep-1`, `sleep-2`), so the orchestrator
|
|
66
|
+
* must call it in the same order on each replay. Without a provider sleep, the run is inline and waits with a timer.
|
|
67
|
+
*/
|
|
68
|
+
function createSleep(context) {
|
|
69
|
+
let count = 0;
|
|
70
|
+
return async (until) => {
|
|
71
|
+
const target = toSleepTarget(context.name, until);
|
|
72
|
+
const name = `sleep-${++count}`;
|
|
73
|
+
const step = context.step;
|
|
74
|
+
if (step?.sleep) {
|
|
75
|
+
if (typeof target === "number") return await step.sleep(name, target);
|
|
76
|
+
if (step.sleepUntil) return await step.sleepUntil(name, target);
|
|
77
|
+
return await step.sleep(name, Math.max(0, target.getTime() - Date.now()));
|
|
78
|
+
}
|
|
79
|
+
const time = typeof target === "number" ? Date.now() + target : target.getTime();
|
|
80
|
+
await runProviderStep(void 0, name, async () => await waitUntil(time));
|
|
81
|
+
};
|
|
82
|
+
}
|
|
27
83
|
/**
|
|
28
|
-
* Gives every handler a step runner. Generated folder Workflows replace this
|
|
29
|
-
*
|
|
84
|
+
* Gives every handler `context.sleep()` and a step runner. Generated folder Workflows replace this empty runner with
|
|
85
|
+
* their step files. The context keeps the provider step at runtime, so the generated step runner can use it.
|
|
30
86
|
*/
|
|
31
|
-
function toExecutionContext(context) {
|
|
87
|
+
function toExecutionContext(context, sleep) {
|
|
32
88
|
return {
|
|
33
89
|
...context,
|
|
90
|
+
sleep,
|
|
34
91
|
steps: {}
|
|
35
92
|
};
|
|
36
93
|
}
|
|
94
|
+
/** The root step result that says the handler called `context.sleep()`. A replay reads it and fails the same way. */
|
|
95
|
+
const rootStepSleepResult = { "~vitehubRootStepSleep": true };
|
|
96
|
+
function toNonRetryableStepResult(error) {
|
|
97
|
+
if (!isNonRetryableError(error)) return void 0;
|
|
98
|
+
const code = Reflect.get(error, "code");
|
|
99
|
+
return { "~vitehubNonRetryableError": {
|
|
100
|
+
...typeof code === "string" ? { code } : {},
|
|
101
|
+
message: error.message,
|
|
102
|
+
name: error.name
|
|
103
|
+
} };
|
|
104
|
+
}
|
|
105
|
+
function readNonRetryableStepResult(value) {
|
|
106
|
+
if (typeof value !== "object" || value === null || !Object.hasOwn(value, "~vitehubNonRetryableError")) return void 0;
|
|
107
|
+
const { code, message, name } = value["~vitehubNonRetryableError"];
|
|
108
|
+
const error = new NonRetryableError(message);
|
|
109
|
+
error.name = name;
|
|
110
|
+
return code === void 0 ? error : Object.assign(error, { code });
|
|
111
|
+
}
|
|
112
|
+
/**
|
|
113
|
+
* Runs the handler in its root step, or alone for a folder Workflow (`rootStep: false`). A sleep inside the root step
|
|
114
|
+
* fails the run with `WORKFLOW_ROOT_STEP_SLEEP` on every provider. The root step returns a marker instead of throwing,
|
|
115
|
+
* so the provider does not retry it and run the code before the sleep again.
|
|
116
|
+
*/
|
|
117
|
+
async function runInRootStep(context, definition, encode) {
|
|
118
|
+
if (definition.options?.rootStep === false) return await encode(await definition.handler(context.payload, toExecutionContext(context, createSleep(context))));
|
|
119
|
+
let slept = false;
|
|
120
|
+
const sleepError = () => workflowErrorDiagnostics.WORKFLOW_ROOT_STEP_SLEEP({ name: context.name });
|
|
121
|
+
const sleep = async () => {
|
|
122
|
+
slept = true;
|
|
123
|
+
throw sleepError();
|
|
124
|
+
};
|
|
125
|
+
const output = await runProviderStep(context.step, context.name, async () => {
|
|
126
|
+
try {
|
|
127
|
+
const value = await encode(await definition.handler(context.payload, toExecutionContext(context, sleep)));
|
|
128
|
+
return slept ? rootStepSleepResult : value;
|
|
129
|
+
} catch (error) {
|
|
130
|
+
if (slept) return rootStepSleepResult;
|
|
131
|
+
throw error;
|
|
132
|
+
}
|
|
133
|
+
});
|
|
134
|
+
if (typeof output === "object" && output !== null && "~vitehubRootStepSleep" in output) throw sleepError();
|
|
135
|
+
return output;
|
|
136
|
+
}
|
|
37
137
|
async function runWorkflowHandler(context, definition) {
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
138
|
+
return toResponse(await runInRootStep(context, definition, (value) => value));
|
|
139
|
+
}
|
|
140
|
+
/**
|
|
141
|
+
* The stored result of a run. A `Response` from the handler is stored as a serialized Response. Any other value is
|
|
142
|
+
* stored as JSON, so every provider returns the same plain value.
|
|
143
|
+
*/
|
|
144
|
+
async function toStoredWorkflowResult(value) {
|
|
145
|
+
if (isResponse(value)) return await serializeResponse(value);
|
|
146
|
+
const json = JSON.stringify(value);
|
|
147
|
+
return json === void 0 ? void 0 : JSON.parse(json);
|
|
41
148
|
}
|
|
42
149
|
/** Encode inside the root step so both checkpoints and final outputs survive replay. */
|
|
43
150
|
async function runSerializedWorkflowHandler(context, definition) {
|
|
44
|
-
|
|
45
|
-
return definition.options?.rootStep === false ? await run() : await runProviderStep(context.step, context.name, run);
|
|
151
|
+
return await runInRootStep(context, definition, toStoredWorkflowResult);
|
|
46
152
|
}
|
|
47
153
|
//#endregion
|
|
48
|
-
export { createWorkflowSteps, runProviderStep, runSerializedWorkflowHandler, runWorkflowHandler };
|
|
154
|
+
export { createWorkflowSteps, readNonRetryableStepResult, runProviderStep, runSerializedWorkflowHandler, runWorkflowHandler, toNonRetryableStepResult, toStepPropertyName };
|
|
@@ -1,6 +1,7 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { getOpenWorkflowRuntime } from "
|
|
1
|
+
import { l as ResolvedWorkflowOptions, m as WorkflowDefinitionRegistry } from "../types-CnKVDnEp.js";
|
|
2
|
+
import { o as getOpenWorkflowRuntime } from "../openworkflow-TEaNk0LV.js";
|
|
3
3
|
import { ViteHubError } from "@vite-hub/runtime";
|
|
4
|
+
import { ShutdownApp } from "@vite-hub/runtime/internal/shutdown";
|
|
4
5
|
//#region src/runtime/openworkflow-worker.d.ts
|
|
5
6
|
type OpenWorkflowWorker = ReturnType<Awaited<ReturnType<typeof getOpenWorkflowRuntime>>["client"]["newWorker"]>;
|
|
6
7
|
export interface CreateOpenWorkflowWorkerOptions {
|
|
@@ -15,4 +16,30 @@ export interface StartOpenWorkflowWorkerOptions extends CreateOpenWorkflowWorker
|
|
|
15
16
|
}
|
|
16
17
|
export declare function createOpenWorkflowWorker(options?: CreateOpenWorkflowWorkerOptions): Promise<OpenWorkflowWorker>;
|
|
17
18
|
export declare function startOpenWorkflowWorker(options?: StartOpenWorkflowWorkerOptions): Promise<OpenWorkflowWorker>;
|
|
19
|
+
/** The part of a Nitro app that `installOpenWorkflow()` uses. */
|
|
20
|
+
export interface OpenWorkflowWorkerApp extends ShutdownApp {
|
|
21
|
+
captureError?: (error: Error, context: {
|
|
22
|
+
tags: string[];
|
|
23
|
+
}) => void;
|
|
24
|
+
}
|
|
25
|
+
export interface InstallOpenWorkflowOptions {
|
|
26
|
+
/**
|
|
27
|
+
* Returns the connection URL of the Postgres Default Database. The generated plugin passes it when the build has
|
|
28
|
+
* one, and OpenWorkflow then keeps its runs there unless its own storage is configured.
|
|
29
|
+
*/
|
|
30
|
+
databaseUrl?: () => string | undefined;
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* Installs OpenWorkflow in a long-lived Nitro server. The generated Workflow startup plugin calls it after it installs
|
|
34
|
+
* the runtime config and registry. It loads OpenWorkflow from the server bundle, then starts the worker unless
|
|
35
|
+
* `workflow.worker` is `false`. The worker stops when the server stops.
|
|
36
|
+
*
|
|
37
|
+
* The worker registers the discovered Workflow Definitions of the registry, which every server of the build has.
|
|
38
|
+
* When the storage is not ready, setup runs again until it works, with a delay that grows to 30 seconds. Each failure
|
|
39
|
+
* is reported. A failure that only a configuration change fixes, such as `WORKFLOW_OPENWORKFLOW_DATABASE_URL_MISSING`, stops the retries. The server keeps serving requests, and queued runs wait in storage until the worker starts.
|
|
40
|
+
* The returned promise resolves to `true` when the worker runs, and to `false` when it is off or the server closed
|
|
41
|
+
* first. When the plugin runs again in the same process, as `vite dev` does after a file changes, the previous worker
|
|
42
|
+
* stops taking runs, finishes the runs that it executes on its own backend, and the new one uses the changed definitions.
|
|
43
|
+
*/
|
|
44
|
+
export declare function installOpenWorkflow(app: OpenWorkflowWorkerApp, options?: InstallOpenWorkflowOptions): Promise<boolean>;
|
|
18
45
|
//#endregion
|