@vite-hub/workflow 0.0.4 → 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 +168 -47
- package/dist/{_app-DgwN4ulW.d.ts → app-BojnC_yf.d.ts} +1 -4
- package/dist/{app-BZpq2cwC.js → app-CmMq8l4t.js} +2 -1
- package/dist/client-CU5rELC0.js +658 -0
- package/dist/{cloudflare-D0Di-QYL.js → cloudflare-BfFnNWB0.js} +20 -4
- package/dist/cloudflare-env-CGLx7lUd.js +115 -0
- 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-DCvRDlxA.js +145 -0
- 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 +23 -71
- package/dist/index.js +23 -378
- package/dist/inspect-B72iFyj-.js +70 -0
- 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/operation-definition-8gOe-ubD.d.ts +234 -0
- package/dist/operations-B0L8kV5s.js +329 -0
- package/dist/operations.d.ts +71 -0
- package/dist/operations.js +314 -0
- package/dist/out-fRNaVslw.js +20357 -0
- package/dist/rolldown-runtime-CMFfr-1z.js +26 -0
- package/dist/run-error-DBWqiJHH.js +110 -0
- package/dist/runtime/cloudflare-runner.d.ts +7 -19
- package/dist/runtime/cloudflare-runner.js +27 -10
- package/dist/runtime/cloudflare-shared.d.ts +2 -1
- package/dist/runtime/cloudflare-shared.js +1 -1
- package/dist/runtime/cloudflare-vite.d.ts +10 -11
- 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 +24 -7
- package/dist/runtime/execute.js +135 -6
- package/dist/runtime/openworkflow-worker.d.ts +34 -9
- package/dist/runtime/openworkflow-worker.js +156 -37
- package/dist/runtime/openworkflow.d.ts +2 -23
- package/dist/runtime/openworkflow.js +289 -46
- package/dist/runtime/state.d.ts +22 -21
- package/dist/runtime/state.js +99 -25
- package/dist/runtime/vercel-vite.d.ts +10 -12
- package/dist/runtime/vercel-vite.js +7 -4
- package/dist/types-CnKVDnEp.d.ts +241 -0
- package/dist/{vercel-B0MuIVgJ.js → vercel-BZU_O6ay.js} +78 -24
- package/dist/vite.d.ts +529 -23
- package/dist/vite.js +2789 -1609
- package/package.json +62 -34
- package/dist/cloudflare-env-DjxQNbrx.js +0 -58
- package/dist/cloudflare-shared-BVVgDSdq.d.ts +0 -1
- package/dist/cloudflare-vite-BkDgB6Sr.js +0 -46
- package/dist/config-BHknO_rT.js +0 -136
- package/dist/provider-operation-CW2cYfVy.js +0 -65
- package/dist/runtime-type-DGUn8Ple.js +0 -37
- package/dist/types-B9RdRkKh.d.ts +0 -169
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { createRequire } from "node:module";
|
|
2
|
+
//#region \0rolldown/runtime.js
|
|
3
|
+
var __create = Object.create;
|
|
4
|
+
var __defProp = Object.defineProperty;
|
|
5
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
6
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
7
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
8
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
9
|
+
var __commonJSMin = (cb, mod) => () => (mod || (cb((mod = { exports: {} }).exports, mod), cb = null), mod.exports);
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") for (var keys = __getOwnPropNames(from), i = 0, n = keys.length, key; i < n; i++) {
|
|
12
|
+
key = keys[i];
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, {
|
|
14
|
+
get: ((k) => from[k]).bind(null, key),
|
|
15
|
+
enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
|
|
16
|
+
});
|
|
17
|
+
}
|
|
18
|
+
return to;
|
|
19
|
+
};
|
|
20
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule || !__hasOwnProp.call(mod, "default") ? __defProp(target, "default", {
|
|
21
|
+
value: mod,
|
|
22
|
+
enumerable: true
|
|
23
|
+
}) : target, mod));
|
|
24
|
+
var __require = /* #__PURE__ */ (() => createRequire(import.meta.url))();
|
|
25
|
+
//#endregion
|
|
26
|
+
export { __require as n, __toESM as r, __commonJSMin as t };
|
|
@@ -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,31 +1,19 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { t as CloudflareWorkerEnv } from "../cloudflare-env-
|
|
3
|
-
|
|
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";
|
|
4
3
|
//#region src/runtime/cloudflare-runner.d.ts
|
|
5
|
-
interface CloudflareWorkflowEvent {
|
|
4
|
+
export interface CloudflareWorkflowEvent {
|
|
6
5
|
id?: string;
|
|
7
6
|
instanceId?: string;
|
|
8
7
|
payload?: unknown;
|
|
9
8
|
}
|
|
10
|
-
interface RunCloudflareWorkflowOptions {
|
|
9
|
+
export interface RunCloudflareWorkflowOptions {
|
|
11
10
|
config: false | ResolvedWorkflowOptions | undefined;
|
|
12
|
-
createNonRetryableError?: (error: Error
|
|
13
|
-
isRetryable: false;
|
|
14
|
-
}) => Error;
|
|
11
|
+
createNonRetryableError?: (error: Error) => Error;
|
|
15
12
|
env: CloudflareWorkerEnv;
|
|
16
13
|
event: CloudflareWorkflowEvent;
|
|
17
14
|
name: string;
|
|
18
15
|
registry: WorkflowDefinitionRegistry;
|
|
19
16
|
step?: WorkflowProviderStep;
|
|
20
17
|
}
|
|
21
|
-
declare function runCloudflareWorkflow({
|
|
22
|
-
|
|
23
|
-
createNonRetryableError,
|
|
24
|
-
env,
|
|
25
|
-
event,
|
|
26
|
-
name,
|
|
27
|
-
registry,
|
|
28
|
-
step
|
|
29
|
-
}: RunCloudflareWorkflowOptions): Promise<unknown>;
|
|
30
|
-
//#endregion
|
|
31
|
-
export { CloudflareWorkflowEvent, RunCloudflareWorkflowOptions, runCloudflareWorkflow };
|
|
18
|
+
export declare function runCloudflareWorkflow({ config, createNonRetryableError, env, event, name, registry, step }: RunCloudflareWorkflowOptions): Promise<unknown>;
|
|
19
|
+
//#endregion
|
|
@@ -1,12 +1,17 @@
|
|
|
1
|
-
import {
|
|
2
|
-
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";
|
|
5
|
+
import { runSerializedWorkflowHandler } from "./execute.js";
|
|
3
6
|
import { loadWorkflowDefinition, runWithWorkflowRuntimeEvent, setWorkflowRuntimeConfig, setWorkflowRuntimeRegistry } from "./state.js";
|
|
4
7
|
//#region src/runtime/cloudflare-runner.ts
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
}
|
|
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();
|
|
8
10
|
function translateNonRetryableError(error, createNonRetryableError) {
|
|
9
|
-
|
|
11
|
+
if (!createNonRetryableError || !isNonRetryableError(error) || cloudflareNonRetryableErrors.has(error)) return error;
|
|
12
|
+
const translated = createNonRetryableError(error);
|
|
13
|
+
cloudflareNonRetryableErrors.add(translated);
|
|
14
|
+
return translated;
|
|
10
15
|
}
|
|
11
16
|
function wrapCloudflareWorkflowStep(step, createNonRetryableError, env, runtimeEvent) {
|
|
12
17
|
if (!step?.do) return step;
|
|
@@ -20,7 +25,8 @@ function wrapCloudflareWorkflowStep(step, createNonRetryableError, env, runtimeE
|
|
|
20
25
|
}
|
|
21
26
|
})));
|
|
22
27
|
},
|
|
23
|
-
...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) } : {}
|
|
24
30
|
};
|
|
25
31
|
}
|
|
26
32
|
async function runCloudflareWorkflow({ config, createNonRetryableError, env, event, name, registry, step }) {
|
|
@@ -34,12 +40,23 @@ async function runCloudflareWorkflow({ config, createNonRetryableError, env, eve
|
|
|
34
40
|
};
|
|
35
41
|
return await runWithActiveCloudflareEnv(env, async () => {
|
|
36
42
|
const definition = await loadWorkflowDefinition(name);
|
|
37
|
-
if (!definition) throw
|
|
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
|
+
}
|
|
38
55
|
try {
|
|
39
|
-
return await runWithWorkflowRuntimeEvent(runtimeEvent, () =>
|
|
56
|
+
return await runWithWorkflowRuntimeEvent(runtimeEvent, () => runSerializedWorkflowHandler({
|
|
40
57
|
id: event?.instanceId || event?.id,
|
|
41
58
|
name,
|
|
42
|
-
payload
|
|
59
|
+
payload,
|
|
43
60
|
provider: "cloudflare",
|
|
44
61
|
step: providerStep
|
|
45
62
|
}, definition));
|
|
@@ -1,2 +1,3 @@
|
|
|
1
|
-
import {
|
|
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";
|
|
2
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,18 +1,17 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { t as
|
|
3
|
-
import {
|
|
4
|
-
|
|
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";
|
|
4
|
+
import { t as VitehubApp } from "../app-BojnC_yf.js";
|
|
5
5
|
//#region src/runtime/cloudflare-vite.d.ts
|
|
6
|
-
type CloudflareWorkerApp =
|
|
7
|
-
interface WorkflowCloudflareWorkerOptions {
|
|
6
|
+
export type CloudflareWorkerApp = VitehubApp;
|
|
7
|
+
export interface WorkflowCloudflareWorkerOptions {
|
|
8
8
|
app?: CloudflareWorkerApp;
|
|
9
9
|
registry?: WorkflowDefinitionRegistry;
|
|
10
10
|
workflow?: false | ResolvedWorkflowOptions;
|
|
11
11
|
}
|
|
12
|
-
interface WorkflowCloudflareWorker {
|
|
12
|
+
export interface WorkflowCloudflareWorker {
|
|
13
13
|
fetch: (request: Request, env: CloudflareWorkerEnv, context: CloudflareWorkerExecutionContext) => Promise<Response>;
|
|
14
14
|
}
|
|
15
|
-
declare function installWorkflowCloudflareRuntime(options?: WorkflowCloudflareWorkerOptions): void;
|
|
16
|
-
declare function createWorkflowCloudflareWorker(options?: WorkflowCloudflareWorkerOptions): WorkflowCloudflareWorker;
|
|
17
|
-
//#endregion
|
|
18
|
-
export { CloudflareWorkerApp, WorkflowCloudflareWorker, WorkflowCloudflareWorkerOptions, createWorkflowCloudflareWorker, installWorkflowCloudflareRuntime };
|
|
15
|
+
export declare function installWorkflowCloudflareRuntime(options?: WorkflowCloudflareWorkerOptions): void;
|
|
16
|
+
export declare function createWorkflowCloudflareWorker(options?: WorkflowCloudflareWorkerOptions): WorkflowCloudflareWorker;
|
|
17
|
+
//#endregion
|
|
@@ -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,11 +1,28 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
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
|
-
declare function runProviderStep<TResult>(step:
|
|
5
|
-
|
|
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;
|
|
7
|
+
export declare function createWorkflowSteps(context: WorkflowProviderContext, stepModules: Array<{
|
|
6
8
|
name: string;
|
|
7
9
|
run: WorkflowStepFunction;
|
|
8
10
|
}>): Record<string, WorkflowStepFunction>;
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
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;
|
|
25
|
+
export declare function runWorkflowHandler<TPayload, TResult>(context: WorkflowProviderContext<TPayload>, definition: WorkflowDefinition<TPayload, TResult>): Promise<Response>;
|
|
26
|
+
/** Encode inside the root step so both checkpoints and final outputs survive replay. */
|
|
27
|
+
export declare function runSerializedWorkflowHandler<TPayload, TResult>(context: WorkflowProviderContext<TPayload>, definition: WorkflowDefinition<TPayload, TResult>): Promise<unknown>;
|
|
28
|
+
//#endregion
|
package/dist/runtime/execute.js
CHANGED
|
@@ -1,3 +1,7 @@
|
|
|
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";
|
|
1
5
|
//#region src/runtime/execute.ts
|
|
2
6
|
const defaultStepOptions = { retries: {
|
|
3
7
|
backoff: "exponential",
|
|
@@ -6,20 +10,145 @@ const defaultStepOptions = { retries: {
|
|
|
6
10
|
} };
|
|
7
11
|
async function runProviderStep(step, name, run, options = defaultStepOptions) {
|
|
8
12
|
if (typeof step?.do === "function") return await step.do(name, options, run);
|
|
9
|
-
|
|
13
|
+
const recorder = getWorkflowDevRecorder();
|
|
14
|
+
return recorder ? await recorder.step(name, run) : await run();
|
|
10
15
|
}
|
|
16
|
+
/** `01.create-user.ts` becomes the `createUser` step. */
|
|
11
17
|
function toStepPropertyName(file) {
|
|
12
18
|
return file.split("/").pop().replace(/\.(?:c|m)?[jt]s$/i, "").replace(/^\d+[.-]?/, "").replace(/[-_.]+([a-z0-9])/gi, (_, char) => char.toUpperCase());
|
|
13
19
|
}
|
|
14
20
|
function createWorkflowSteps(context, stepModules) {
|
|
21
|
+
const names = /* @__PURE__ */ new Map();
|
|
15
22
|
return Object.fromEntries(stepModules.map((module) => {
|
|
16
|
-
|
|
23
|
+
const propertyName = toStepPropertyName(module.name);
|
|
24
|
+
if (!propertyName) throw workflowErrorDiagnostics.WORKFLOW_C0011({ message: `[vitehub] Workflow step file ${JSON.stringify(module.name)} normalizes to an empty step property. Rename the file so the step property is non-empty.` });
|
|
25
|
+
const previousName = names.get(propertyName);
|
|
26
|
+
if (previousName) throw workflowErrorDiagnostics.WORKFLOW_C0010({ message: `[vitehub] Workflow step files ${JSON.stringify(previousName)} and ${JSON.stringify(module.name)} both normalize to the \`${propertyName}\` step property. Rename one file so each step property is unique.` });
|
|
27
|
+
names.set(propertyName, module.name);
|
|
28
|
+
return [propertyName, async (input) => await runProviderStep(context.step, `${context.name}/${module.name}`, () => module.run(input))];
|
|
17
29
|
}));
|
|
18
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
|
+
}
|
|
83
|
+
/**
|
|
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.
|
|
86
|
+
*/
|
|
87
|
+
function toExecutionContext(context, sleep) {
|
|
88
|
+
return {
|
|
89
|
+
...context,
|
|
90
|
+
sleep,
|
|
91
|
+
steps: {}
|
|
92
|
+
};
|
|
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
|
+
}
|
|
19
137
|
async function runWorkflowHandler(context, definition) {
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
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);
|
|
148
|
+
}
|
|
149
|
+
/** Encode inside the root step so both checkpoints and final outputs survive replay. */
|
|
150
|
+
async function runSerializedWorkflowHandler(context, definition) {
|
|
151
|
+
return await runInRootStep(context, definition, toStoredWorkflowResult);
|
|
23
152
|
}
|
|
24
153
|
//#endregion
|
|
25
|
-
export { createWorkflowSteps, runProviderStep, runWorkflowHandler };
|
|
154
|
+
export { createWorkflowSteps, readNonRetryableStepResult, runProviderStep, runSerializedWorkflowHandler, runWorkflowHandler, toNonRetryableStepResult, toStepPropertyName };
|