@vite-hub/workflow 0.4.1 → 0.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +28 -10
- package/dist/{app-He0GTrYZ.js → app-2Y7sBAG6.js} +1 -1
- package/dist/{client-DkwOxEiT.js → client-CgBcDlGn.js} +80 -54
- package/dist/{cloudflare-BHfClPvT.js → cloudflare-Datj_znU.js} +1 -1
- package/dist/{config-C3DvtjfJ.js → config-z1651xc2.js} +1 -1
- package/dist/definition.d.ts +1 -1
- package/dist/definition.js +1 -1
- package/dist/{delay-D1-nq_VE.js → delay-CHN4tv3i.js} +1 -2
- package/dist/durability.d.ts +1 -1
- package/dist/{error-diagnostics-CQ4BUqRe.js → error-diagnostics-C0KyI408.js} +5 -1
- package/dist/{error-diagnostics-0V2T3JFw.js → error-diagnostics-DMeaFmQD.js} +23 -5
- package/dist/{errors-DCqRnZpN.js → errors-BNxEeeCO.js} +0 -4
- package/dist/{errors-uhP8DZTi.d.ts → errors-D-fNDVg0.d.ts} +0 -5
- package/dist/index.d.ts +7 -5
- package/dist/index.js +4 -4
- package/dist/{openworkflow-BhzxV_Y3.d.ts → openworkflow-BoaENgh9.d.ts} +7 -8
- package/dist/{operation-definition-nmRu_O3V.d.ts → operation-definition-CI2PJGqu.d.ts} +8 -2
- package/dist/{operations-D_WJwjrS.js → operations-CYntpf-T.js} +27 -38
- package/dist/operations.d.ts +5 -8
- package/dist/operations.js +27 -32
- package/dist/{run-error-DBWqiJHH.js → run-error-Ctn50EYz.js} +1 -2
- package/dist/runtime/cloudflare-runner.d.ts +12 -2
- package/dist/runtime/cloudflare-runner.js +13 -6
- package/dist/runtime/cloudflare-vite.d.ts +1 -1
- package/dist/runtime/cloudflare-vite.js +2 -2
- package/dist/runtime/dev-runs.js +1 -0
- package/dist/runtime/execute.d.ts +2 -2
- package/dist/runtime/execute.js +64 -32
- package/dist/runtime/openworkflow-worker.d.ts +2 -2
- package/dist/runtime/openworkflow-worker.js +4 -9
- package/dist/runtime/openworkflow.d.ts +2 -2
- package/dist/runtime/openworkflow.js +33 -48
- package/dist/runtime/state.d.ts +1 -1
- package/dist/runtime/state.js +3 -1
- package/dist/runtime/vercel-vite.d.ts +1 -1
- package/dist/runtime/vercel-vite.js +2 -2
- package/dist/signals-C1iolifd.js +80 -0
- package/dist/{types-Dsmwsdlv.d.ts → types-BWPHeugk.d.ts} +34 -4
- package/dist/{vercel-DTvm61iY.js → vercel-BIkBFrQQ.js} +4 -12
- package/dist/vite.d.ts +12 -7
- package/dist/vite.js +3516 -1660
- package/package.json +3 -3
|
@@ -1,15 +1,14 @@
|
|
|
1
|
-
import { t as workflowErrorDiagnostics } from "../error-diagnostics-
|
|
2
|
-
import { n as createWorkflowError } from "../errors-
|
|
3
|
-
import { i as runWorkflowProviderOperation } from "../run-error-DBWqiJHH.js";
|
|
4
|
-
import { describeOpenWorkflowStorage, detachOpenWorkflowRuntimes, getOpenWorkflowRuntime, registerOpenWorkflowDefinition, setOpenWorkflowDatabaseUrl, setOpenWorkflowImporter } from "./openworkflow.js";
|
|
1
|
+
import { t as workflowErrorDiagnostics } from "../error-diagnostics-DMeaFmQD.js";
|
|
2
|
+
import { n as createWorkflowError } from "../errors-BNxEeeCO.js";
|
|
5
3
|
import { getWorkflowRuntimeConfig, getWorkflowRuntimeRegistry, loadWorkflowDefinition, setWorkflowRuntimeConfig, setWorkflowRuntimeRegistry } from "./state.js";
|
|
4
|
+
import { i as runWorkflowProviderOperation } from "../run-error-Ctn50EYz.js";
|
|
5
|
+
import { describeOpenWorkflowStorage, detachOpenWorkflowRuntimes, getOpenWorkflowRuntime, registerOpenWorkflowDefinition, setOpenWorkflowDatabaseUrl, setOpenWorkflowImporter } from "./openworkflow.js";
|
|
6
6
|
import { ViteHubError } from "@vite-hub/runtime";
|
|
7
7
|
import { onShutdown } from "@vite-hub/runtime/internal/shutdown";
|
|
8
8
|
//#region src/runtime/openworkflow-worker.ts
|
|
9
9
|
function getProcess() {
|
|
10
10
|
return globalThis.process;
|
|
11
11
|
}
|
|
12
|
-
/** Registers every Workflow Definition of the registry on the OpenWorkflow client, before a worker copies them. */
|
|
13
12
|
async function prepareOpenWorkflowWorker(options) {
|
|
14
13
|
const config = options.config ?? getWorkflowRuntimeConfig();
|
|
15
14
|
if (!config || config.driver !== "openworkflow") throw workflowErrorDiagnostics.WORKFLOW_R0017({ message: "OpenWorkflow worker requires workflow.driver \"openworkflow\"." });
|
|
@@ -138,10 +137,6 @@ const bundledOpenWorkflowModules = {
|
|
|
138
137
|
"postgres": () => import("postgres")
|
|
139
138
|
};
|
|
140
139
|
const openWorkflowStartDelayMs = 500;
|
|
141
|
-
/**
|
|
142
|
-
* Errors that a new attempt cannot fix, because the configuration must change first. `WORKFLOW_OPENWORKFLOW_SETUP_CONNECTION_UNAVAILABLE` is not one: a
|
|
143
|
-
* pool that is full with other traffic when the server starts gives a second connection later.
|
|
144
|
-
*/
|
|
145
140
|
const permanentSetupErrorCodes = /* @__PURE__ */ new Set(["WORKFLOW_OPENWORKFLOW_DATABASE_URL_MISSING", "WORKFLOW_OPENWORKFLOW_DATABASE_URL_IGNORED"]);
|
|
146
141
|
function hasPermanentSetupError(error) {
|
|
147
142
|
for (let current = error, depth = 0; current && typeof current === "object" && depth < 8; depth++) {
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import { a as getOpenWorkflowRun, c as resetOpenWorkflowRuntime, d as setOpenWorkflowImporter, i as detachOpenWorkflowRuntimes, l as
|
|
2
|
-
export { OpenWorkflowStorageSource, cancelOpenWorkflowRun, describeOpenWorkflowStorage, detachOpenWorkflowRuntimes, getOpenWorkflowRun, getOpenWorkflowRuntime, registerOpenWorkflowDefinition, resetOpenWorkflowRuntime, runOpenWorkflow, setOpenWorkflowDatabaseUrl, setOpenWorkflowImporter };
|
|
1
|
+
import { a as getOpenWorkflowRun, c as resetOpenWorkflowRuntime, d as setOpenWorkflowDatabaseUrl, f as setOpenWorkflowImporter, i as detachOpenWorkflowRuntimes, l as resumeOpenWorkflowSignal, n as cancelOpenWorkflowRun, o as getOpenWorkflowRuntime, r as describeOpenWorkflowStorage, s as registerOpenWorkflowDefinition, t as OpenWorkflowStorageSource, u as runOpenWorkflow } from "../openworkflow-BoaENgh9.js";
|
|
2
|
+
export { OpenWorkflowStorageSource, cancelOpenWorkflowRun, describeOpenWorkflowStorage, detachOpenWorkflowRuntimes, getOpenWorkflowRun, getOpenWorkflowRuntime, registerOpenWorkflowDefinition, resetOpenWorkflowRuntime, resumeOpenWorkflowSignal, runOpenWorkflow, setOpenWorkflowDatabaseUrl, setOpenWorkflowImporter };
|
|
@@ -1,11 +1,11 @@
|
|
|
1
|
-
import { t as workflowErrorDiagnostics } from "../error-diagnostics-
|
|
1
|
+
import { t as workflowErrorDiagnostics } from "../error-diagnostics-DMeaFmQD.js";
|
|
2
2
|
import { t as resolveOpenWorkflowStorage } from "../openworkflow-storage-Dj9zUAsk.js";
|
|
3
|
-
import { n as createWorkflowError } from "../errors-
|
|
3
|
+
import { n as createWorkflowError } from "../errors-BNxEeeCO.js";
|
|
4
4
|
import { readNonRetryableStepResult, resolveWorkflowStepOptions, runSerializedWorkflowHandler, toDurationMilliseconds, toNonRetryableStepResult, withWorkflowStepTimeout } from "./execute.js";
|
|
5
|
-
import { r as invariantError, t as defaultOpenWorkflowSqlitePath } from "../openworkflow-defaults-DDig2M4s.js";
|
|
6
|
-
import { i as runWorkflowProviderOperation, n as getWorkflowProviderStatus, t as toWorkflowRunError } from "../run-error-DBWqiJHH.js";
|
|
7
5
|
import { t as emitWorkflowFailed } from "../run-failed-gVGo1BaF.js";
|
|
8
|
-
import {
|
|
6
|
+
import { r as invariantError, t as defaultOpenWorkflowSqlitePath } from "../openworkflow-defaults-DDig2M4s.js";
|
|
7
|
+
import { i as runWorkflowProviderOperation, n as getWorkflowProviderStatus, t as toWorkflowRunError } from "../run-error-Ctn50EYz.js";
|
|
8
|
+
import { deserializeResponse, isSerializedResponse } from "@vite-hub/runtime/internal/response";
|
|
9
9
|
//#region src/runtime/openworkflow.ts
|
|
10
10
|
const defaultImporter = (specifier) => import(
|
|
11
11
|
/* @vite-ignore */
|
|
@@ -51,7 +51,6 @@ let databaseUrlResolver;
|
|
|
51
51
|
function setOpenWorkflowDatabaseUrl(resolve) {
|
|
52
52
|
databaseUrlResolver = resolve;
|
|
53
53
|
}
|
|
54
|
-
/** The Postgres URL of the Default Database, when the app has a Postgres Default Database. */
|
|
55
54
|
function readDatabasePostgresUrl() {
|
|
56
55
|
if (!databaseUrlResolver) return void 0;
|
|
57
56
|
const url = databaseUrlResolver()?.trim();
|
|
@@ -98,14 +97,11 @@ function getOpenWorkflowConfig(config) {
|
|
|
98
97
|
source: "default"
|
|
99
98
|
};
|
|
100
99
|
}
|
|
101
|
-
/** How long a server waits for the setup lock that another server holds, before the worker start tries again. */
|
|
102
100
|
const postgresSetupWaitMs = 12e4;
|
|
103
|
-
/** How long the server that holds the setup lock waits for the second connection that the OpenWorkflow migration needs. */
|
|
104
101
|
const postgresSecondConnectionTimeoutMs = 1e4;
|
|
105
102
|
function sleep(ms) {
|
|
106
103
|
return new Promise((resolve) => setTimeout(resolve, ms).unref?.());
|
|
107
104
|
}
|
|
108
|
-
/** Resolves `true` when `task` settles first, `false` when `ms` passes first. */
|
|
109
105
|
function settlesWithin(task, ms) {
|
|
110
106
|
let timer;
|
|
111
107
|
return Promise.race([task.then(() => true), new Promise((resolve) => {
|
|
@@ -113,23 +109,6 @@ function settlesWithin(task, ms) {
|
|
|
113
109
|
timer.unref?.();
|
|
114
110
|
})]).finally(() => clearTimeout(timer));
|
|
115
111
|
}
|
|
116
|
-
/**
|
|
117
|
-
* OpenWorkflow creates its Postgres schema without a lock. When servers start together, all but one fail with
|
|
118
|
-
* `duplicate key value violates unique constraint`, and OpenWorkflow leaves the failed migration connection open in an
|
|
119
|
-
* aborted transaction. So one server at a time sets up the schema, and the others then find it up to date.
|
|
120
|
-
*
|
|
121
|
-
* OpenWorkflow does not export its migrations, so they cannot run on the connection that holds the lock: they run on a
|
|
122
|
-
* connection that `BackendPostgres.connect()` opens.
|
|
123
|
-
* - A server tries the lock with `pg_try_advisory_xact_lock` in a short transaction. A server that does not get it
|
|
124
|
-
* commits at once and tries again later, so a waiting server holds no connection.
|
|
125
|
-
* - The server that gets it first checks that it can open a second connection. A pool of one connection, such as a
|
|
126
|
-
* pooler with one server connection, cannot run the migration while the lock holds that connection, so the server
|
|
127
|
-
* stops with `WORKFLOW_OPENWORKFLOW_SETUP_CONNECTION_UNAVAILABLE` before the setup starts.
|
|
128
|
-
* - Then it holds the lock until OpenWorkflow finished the setup.
|
|
129
|
-
* A transaction lock also works through a pooler in transaction mode, such as PgBouncer.
|
|
130
|
-
* OpenWorkflow 0.11.0 locks its setup with a session lock
|
|
131
|
-
* (https://github.com/openworkflowdev/openworkflow/commit/eb544db), which such a pooler does not keep, so this lock stays.
|
|
132
|
-
*/
|
|
133
112
|
async function withPostgresSetupLock(postgres, url, schema, setup) {
|
|
134
113
|
const sql = postgres.default(url, {
|
|
135
114
|
idle_timeout: 1,
|
|
@@ -164,10 +143,6 @@ async function withPostgresSetupLock(postgres, url, schema, setup) {
|
|
|
164
143
|
await sql.end({ timeout: 5 });
|
|
165
144
|
}
|
|
166
145
|
}
|
|
167
|
-
/**
|
|
168
|
-
* The backend that the OpenWorkflow client and its workers use. OpenWorkflow retries a failed run by its retry policy, so
|
|
169
|
-
* only `failWorkflowRun()` knows when a run ends as `failed`. This wrapper sends `workflow.failed` then.
|
|
170
|
-
*/
|
|
171
146
|
function reportFailedRuns(backend, definitions) {
|
|
172
147
|
const failWorkflowRun = async (params) => {
|
|
173
148
|
const run = await backend.failWorkflowRun(params);
|
|
@@ -222,10 +197,6 @@ function readRowId(row) {
|
|
|
222
197
|
const id = Reflect.get(row, "id");
|
|
223
198
|
return typeof id === "string" ? id : void 0;
|
|
224
199
|
}
|
|
225
|
-
/**
|
|
226
|
-
* ViteHub starts a run with the caller's id as the OpenWorkflow idempotency key, and OpenWorkflow gives the run its own
|
|
227
|
-
* UUID. The OpenWorkflow API reads runs by UUID only, so this lookup reads the key from the backend connection.
|
|
228
|
-
*/
|
|
229
200
|
function createRunIdLookup(backend, options) {
|
|
230
201
|
const connection = Reflect.get(backend, options.backend === "sqlite" ? "db" : "pg");
|
|
231
202
|
return async (name, key) => {
|
|
@@ -269,10 +240,6 @@ async function getOpenWorkflowRuntime(config) {
|
|
|
269
240
|
});
|
|
270
241
|
return resolved;
|
|
271
242
|
}
|
|
272
|
-
/**
|
|
273
|
-
* OpenWorkflow counts attempts, and `limit` counts the retries after the first attempt. OpenWorkflow caps each wait at
|
|
274
|
-
* `maximumInterval`, 100 seconds by default, so the cap is the longest wait that the policy asks for.
|
|
275
|
-
*/
|
|
276
243
|
function toOpenWorkflowRetryPolicy(name, options) {
|
|
277
244
|
const { backoff, delay, limit } = resolveWorkflowStepOptions(name, options).retries;
|
|
278
245
|
const initial = toDurationMilliseconds(delay);
|
|
@@ -284,7 +251,7 @@ function toOpenWorkflowRetryPolicy(name, options) {
|
|
|
284
251
|
maximumInterval: `${initial * coefficient ** Math.max(limit - 1, 0)}ms`
|
|
285
252
|
};
|
|
286
253
|
}
|
|
287
|
-
function createOpenWorkflowProviderStep(step) {
|
|
254
|
+
function createOpenWorkflowProviderStep(step, workflow) {
|
|
288
255
|
return {
|
|
289
256
|
async do(name, options, run) {
|
|
290
257
|
const output = await step.run({
|
|
@@ -303,9 +270,33 @@ function createOpenWorkflowProviderStep(step) {
|
|
|
303
270
|
if (error) throw error;
|
|
304
271
|
return output;
|
|
305
272
|
},
|
|
306
|
-
sleep: async (name, milliseconds) => await step.sleep(name, `${milliseconds}ms`)
|
|
273
|
+
sleep: async (name, milliseconds) => await step.sleep(name, `${milliseconds}ms`),
|
|
274
|
+
async waitForSignal(name, token) {
|
|
275
|
+
const received = await step.waitForSignal({
|
|
276
|
+
name,
|
|
277
|
+
signal: token
|
|
278
|
+
});
|
|
279
|
+
if (received === null) throw workflowErrorDiagnostics.WORKFLOW_SIGNAL_TIMEOUT({ name: workflow });
|
|
280
|
+
return received.data;
|
|
281
|
+
}
|
|
307
282
|
};
|
|
308
283
|
}
|
|
284
|
+
/**
|
|
285
|
+
* Sends `value` to a live run that waits for the signal `token`. OpenWorkflow drops early signals and can match
|
|
286
|
+
* a wait left behind by cancellation, so check the run before sending.
|
|
287
|
+
*/
|
|
288
|
+
async function resumeOpenWorkflowSignal(config, workflow, id, token, value) {
|
|
289
|
+
const runtime = await getOpenWorkflowRuntime(config);
|
|
290
|
+
return await runWorkflowProviderOperation("openworkflow", "resume-signal", async () => {
|
|
291
|
+
const run = await runtime.backend.getWorkflowRun({ workflowRunId: await runtime.findRunId(workflow, id) ?? id });
|
|
292
|
+
if (!run || run.workflowName !== workflow || terminalOpenWorkflowStatuses.has(normalizeOpenWorkflowStatus(run.status))) return false;
|
|
293
|
+
const { workflowRunIds } = await runtime.client.sendSignal({
|
|
294
|
+
data: value,
|
|
295
|
+
signal: token
|
|
296
|
+
});
|
|
297
|
+
return workflowRunIds.includes(run.id);
|
|
298
|
+
});
|
|
299
|
+
}
|
|
309
300
|
async function registerOpenWorkflowDefinition(runtime, name, definition) {
|
|
310
301
|
const existing = runtime.workflows.get(name);
|
|
311
302
|
if (existing) return existing;
|
|
@@ -318,7 +309,7 @@ async function registerOpenWorkflowDefinition(runtime, name, definition) {
|
|
|
318
309
|
name,
|
|
319
310
|
payload: input,
|
|
320
311
|
provider: "openworkflow",
|
|
321
|
-
step: createOpenWorkflowProviderStep(step)
|
|
312
|
+
step: createOpenWorkflowProviderStep(step, name)
|
|
322
313
|
}, openWorkflowDefinition);
|
|
323
314
|
});
|
|
324
315
|
runtime.workflows.set(name, workflow);
|
|
@@ -361,12 +352,10 @@ function serializeOpenWorkflowRun(run, name) {
|
|
|
361
352
|
status
|
|
362
353
|
};
|
|
363
354
|
}
|
|
364
|
-
/** OpenWorkflow stores `name`, `message`, and `stack`. The stack has server paths, so the step keeps the message only. */
|
|
365
355
|
function toWorkflowRunStepError(error) {
|
|
366
356
|
const message = typeof error === "object" && error !== null ? Reflect.get(error, "message") : void 0;
|
|
367
357
|
return { message: typeof message === "string" && message ? message : "Workflow step failed." };
|
|
368
358
|
}
|
|
369
|
-
/** OpenWorkflow stores one row per step attempt. A run reports each step once, with its latest attempt. */
|
|
370
359
|
function toWorkflowRunSteps(attempts) {
|
|
371
360
|
const steps = /* @__PURE__ */ new Map();
|
|
372
361
|
for (const attempt of attempts) {
|
|
@@ -449,10 +438,6 @@ const terminalOpenWorkflowStatuses = /* @__PURE__ */ new Set([
|
|
|
449
438
|
"completed",
|
|
450
439
|
"failed"
|
|
451
440
|
]);
|
|
452
|
-
/**
|
|
453
|
-
* Cancels a pending or running OpenWorkflow run of this Workflow and returns its state afterwards. A run of another
|
|
454
|
-
* Workflow, or an unknown run, reports `unknown` without being cancelled. A finished run keeps and reports its status.
|
|
455
|
-
*/
|
|
456
441
|
async function cancelOpenWorkflowRun(config, name, id) {
|
|
457
442
|
const current = await getOpenWorkflowRun(config, name, id);
|
|
458
443
|
if (current.status === "unknown" || terminalOpenWorkflowStatuses.has(current.status)) return current;
|
|
@@ -494,4 +479,4 @@ function setOpenWorkflowImporter(importer) {
|
|
|
494
479
|
openWorkflowImporter = importer;
|
|
495
480
|
}
|
|
496
481
|
//#endregion
|
|
497
|
-
export { cancelOpenWorkflowRun, describeOpenWorkflowStorage, detachOpenWorkflowRuntimes, getOpenWorkflowRun, getOpenWorkflowRuntime, registerOpenWorkflowDefinition, resetOpenWorkflowRuntime, runOpenWorkflow, setOpenWorkflowDatabaseUrl, setOpenWorkflowImporter };
|
|
482
|
+
export { cancelOpenWorkflowRun, describeOpenWorkflowStorage, detachOpenWorkflowRuntimes, getOpenWorkflowRun, getOpenWorkflowRuntime, registerOpenWorkflowDefinition, resetOpenWorkflowRuntime, resumeOpenWorkflowSignal, runOpenWorkflow, setOpenWorkflowDatabaseUrl, setOpenWorkflowImporter };
|
package/dist/runtime/state.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { f as WorkflowDefinition, l as ResolvedWorkflowOptions, m as WorkflowDefinitionRegistry } from "../types-
|
|
1
|
+
import { f as WorkflowDefinition, l as ResolvedWorkflowOptions, m as WorkflowDefinitionRegistry } from "../types-BWPHeugk.js";
|
|
2
2
|
//#region src/runtime/state.d.ts
|
|
3
3
|
export interface WorkflowRunState<TResult = unknown> {
|
|
4
4
|
error?: unknown;
|
package/dist/runtime/state.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
|
-
import { t as workflowErrorDiagnostics } from "../error-diagnostics-
|
|
1
|
+
import { t as workflowErrorDiagnostics } from "../error-diagnostics-DMeaFmQD.js";
|
|
2
2
|
import { n as setWorkflowDevRecorder } from "../dev-recorder-CTN5Fkao.js";
|
|
3
|
+
import { a as resetInlineWorkflowSignals } from "../signals-C1iolifd.js";
|
|
3
4
|
import { AsyncLocalStorage } from "node:async_hooks";
|
|
4
5
|
//#region src/runtime/state.ts
|
|
5
6
|
const RUNS_LIMIT = 1024;
|
|
@@ -243,6 +244,7 @@ function resetWorkflowRuntime() {
|
|
|
243
244
|
inFlightRuns.clear();
|
|
244
245
|
fallbackRunKeys.clear();
|
|
245
246
|
completedRuns.clear();
|
|
247
|
+
resetInlineWorkflowSignals();
|
|
246
248
|
setWorkflowDevRecorder(void 0);
|
|
247
249
|
}
|
|
248
250
|
//#endregion
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { l as ResolvedWorkflowOptions, m as WorkflowDefinitionRegistry } from "../types-
|
|
1
|
+
import { l as ResolvedWorkflowOptions, m as WorkflowDefinitionRegistry } from "../types-BWPHeugk.js";
|
|
2
2
|
import { t as VitehubApp } from "../app-BojnC_yf.js";
|
|
3
3
|
import { IncomingMessage, ServerResponse } from "node:http";
|
|
4
4
|
//#region src/runtime/vercel.d.ts
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { runWithWorkflowRuntimeEvent, setWorkflowRuntimeConfig, setWorkflowRuntimeRegistry } from "./state.js";
|
|
2
|
-
import { i as setVercelWorkflowRuntimeModules } from "../vercel-
|
|
3
|
-
import { t as resolveAppFetch } from "../app-
|
|
2
|
+
import { i as setVercelWorkflowRuntimeModules } from "../vercel-BIkBFrQQ.js";
|
|
3
|
+
import { t as resolveAppFetch } from "../app-2Y7sBAG6.js";
|
|
4
4
|
import { waitUntil } from "@vercel/functions";
|
|
5
5
|
import { H3 } from "h3";
|
|
6
6
|
//#region ../internal/src/runtime/vercel-hosted.ts
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
import { t as workflowErrorDiagnostics } from "./error-diagnostics-DMeaFmQD.js";
|
|
2
|
+
//#region src/runtime/signals.ts
|
|
3
|
+
const tokenPrefix = "vhsig1.";
|
|
4
|
+
const hosts = /* @__PURE__ */ new Set([
|
|
5
|
+
"cloudflare",
|
|
6
|
+
"inline",
|
|
7
|
+
"openworkflow"
|
|
8
|
+
]);
|
|
9
|
+
function toBase64Url(text) {
|
|
10
|
+
let binary = "";
|
|
11
|
+
for (const byte of new TextEncoder().encode(text)) binary += String.fromCharCode(byte);
|
|
12
|
+
return btoa(binary).replaceAll("+", "-").replaceAll("/", "_").replace(/=+$/, "");
|
|
13
|
+
}
|
|
14
|
+
function fromBase64Url(value) {
|
|
15
|
+
const binary = atob(value.replaceAll("-", "+").replaceAll("_", "/"));
|
|
16
|
+
return new TextDecoder("utf-8", { fatal: true }).decode(Uint8Array.from(binary, (char) => char.charCodeAt(0)));
|
|
17
|
+
}
|
|
18
|
+
function createWorkflowSignalToken({ host, run, signal, workflow }) {
|
|
19
|
+
return `${tokenPrefix}${toBase64Url(JSON.stringify([
|
|
20
|
+
host,
|
|
21
|
+
workflow,
|
|
22
|
+
run,
|
|
23
|
+
signal
|
|
24
|
+
]))}`;
|
|
25
|
+
}
|
|
26
|
+
/** The address in a token of `context.createSignal()`, or undefined for any other string. */
|
|
27
|
+
function readWorkflowSignalToken(token) {
|
|
28
|
+
if (!token.startsWith(tokenPrefix)) return void 0;
|
|
29
|
+
let value;
|
|
30
|
+
try {
|
|
31
|
+
value = JSON.parse(fromBase64Url(token.slice(7)));
|
|
32
|
+
} catch {
|
|
33
|
+
return;
|
|
34
|
+
}
|
|
35
|
+
if (!Array.isArray(value) || value.length !== 4 || !value.every((part) => typeof part === "string" && part !== "")) return void 0;
|
|
36
|
+
const [host, workflow, run, signal] = value;
|
|
37
|
+
if (!hosts.has(host)) return void 0;
|
|
38
|
+
return {
|
|
39
|
+
host,
|
|
40
|
+
run,
|
|
41
|
+
signal,
|
|
42
|
+
workflow
|
|
43
|
+
};
|
|
44
|
+
}
|
|
45
|
+
const inlineSignals = /* @__PURE__ */ new Map();
|
|
46
|
+
/** Adds the inline signal of `token`, so a `resumeWorkflow()` call before the wait keeps its value. */
|
|
47
|
+
function openInlineWorkflowSignal(token) {
|
|
48
|
+
if (!inlineSignals.has(token)) inlineSignals.set(token, {});
|
|
49
|
+
}
|
|
50
|
+
/** Forgets the inline signals of a run that ended. A later `resumeWorkflow()` call fails with `WORKFLOW_SIGNAL_NOT_WAITING`. */
|
|
51
|
+
function closeInlineWorkflowSignals(tokens) {
|
|
52
|
+
for (const token of tokens) inlineSignals.delete(token);
|
|
53
|
+
}
|
|
54
|
+
/** Waits in memory for the value of `resumeWorkflow(token, value)`. */
|
|
55
|
+
async function waitForInlineWorkflowSignal(token) {
|
|
56
|
+
const entry = inlineSignals.get(token) ?? {};
|
|
57
|
+
inlineSignals.set(token, entry);
|
|
58
|
+
if (entry.sent) {
|
|
59
|
+
inlineSignals.delete(token);
|
|
60
|
+
return entry.sent.value;
|
|
61
|
+
}
|
|
62
|
+
return await new Promise((resolve) => {
|
|
63
|
+
entry.resolve = (value) => {
|
|
64
|
+
inlineSignals.delete(token);
|
|
65
|
+
resolve(value);
|
|
66
|
+
};
|
|
67
|
+
});
|
|
68
|
+
}
|
|
69
|
+
/** Sends `value` to the inline signal of `token`. Fails when no run of this process has the signal open. */
|
|
70
|
+
function resumeInlineWorkflowSignal(token, address, value) {
|
|
71
|
+
const entry = inlineSignals.get(token);
|
|
72
|
+
if (!entry || entry.sent) throw workflowErrorDiagnostics.WORKFLOW_SIGNAL_NOT_WAITING({ run: address.run });
|
|
73
|
+
if (entry.resolve) entry.resolve(value);
|
|
74
|
+
else entry.sent = { value };
|
|
75
|
+
}
|
|
76
|
+
function resetInlineWorkflowSignals() {
|
|
77
|
+
inlineSignals.clear();
|
|
78
|
+
}
|
|
79
|
+
//#endregion
|
|
80
|
+
export { resetInlineWorkflowSignals as a, readWorkflowSignalToken as i, createWorkflowSignalToken as n, resumeInlineWorkflowSignal as o, openInlineWorkflowSignal as r, waitForInlineWorkflowSignal as s, closeInlineWorkflowSignals as t };
|
|
@@ -11,6 +11,11 @@ interface CloudflareWorkflowBinding<TPayload = unknown> {
|
|
|
11
11
|
interface CloudflareWorkflowInstance {
|
|
12
12
|
id: string;
|
|
13
13
|
status: () => Promise<unknown>;
|
|
14
|
+
/** Sends an event that `step.waitForEvent()` of the instance waits for. */
|
|
15
|
+
sendEvent?: (event: {
|
|
16
|
+
payload: unknown;
|
|
17
|
+
type: string;
|
|
18
|
+
}) => Promise<void>;
|
|
14
19
|
/** Stops the instance. Cloudflare rejects the call when the instance has already finished. */
|
|
15
20
|
terminate: () => Promise<void>;
|
|
16
21
|
}
|
|
@@ -144,6 +149,24 @@ interface WorkflowProviderStep {
|
|
|
144
149
|
sleep?: (name: string, milliseconds: number) => Promise<void>;
|
|
145
150
|
/** Sleeps until a date. Without it, ViteHub sleeps for the time that is left until the date. */
|
|
146
151
|
sleepUntil?: (name: string, date: Date) => Promise<void>;
|
|
152
|
+
/**
|
|
153
|
+
* Waits durably until `resumeWorkflow(token, value)` sends the signal, and returns the value. ViteHub names each
|
|
154
|
+
* wait by call order (`signal-1`, `signal-2`).
|
|
155
|
+
*/
|
|
156
|
+
waitForSignal?: (name: string, token: string) => Promise<unknown>;
|
|
157
|
+
}
|
|
158
|
+
/**
|
|
159
|
+
* A signal of one run, from `context.createSignal()`. Give `token` to the code that will resume the run, such as the
|
|
160
|
+
* webhook URL of an outside service. `wait()` returns the value of `resumeWorkflow(token, value)`.
|
|
161
|
+
*/
|
|
162
|
+
interface WorkflowSignal<TValue = unknown> {
|
|
163
|
+
/**
|
|
164
|
+
* The token of this signal. It is the same on each replay of the run. It is not a secret: check the caller, such as
|
|
165
|
+
* a webhook signature, before you pass a token from a request to `resumeWorkflow()`.
|
|
166
|
+
*/
|
|
167
|
+
readonly token: string;
|
|
168
|
+
/** Waits until `resumeWorkflow(token, value)` sends the signal, and returns the value. */
|
|
169
|
+
wait: () => Promise<TValue>;
|
|
147
170
|
}
|
|
148
171
|
type WorkflowDurationUnit = "second" | "minute" | "hour" | "day" | "week" | "month" | "year";
|
|
149
172
|
/**
|
|
@@ -190,8 +213,15 @@ interface WorkflowProviderContext<TPayload = unknown> {
|
|
|
190
213
|
}
|
|
191
214
|
/** The one object that a Workflow handler receives: the `payload` of the run, the run data, `sleep`, and `steps`. */
|
|
192
215
|
interface WorkflowExecutionContext<TPayload = unknown> extends Omit<WorkflowProviderContext<TPayload>, "step"> {
|
|
193
|
-
/**
|
|
194
|
-
|
|
216
|
+
/**
|
|
217
|
+
* Makes a signal that the run can wait for. `resumeWorkflow(signal.token, value)` ends the wait of
|
|
218
|
+
* `signal.wait()`. Only a folder Workflow's `index.ts` can wait: in a single-file Workflow, `wait()` throws
|
|
219
|
+
* `WORKFLOW_ROOT_STEP_SIGNAL`. The wait is durable on Cloudflare and OpenWorkflow. Inline runs (`vite dev`) wait in
|
|
220
|
+
* memory. Vercel does not support signals yet: `createSignal()` throws `WORKFLOW_SIGNAL_VERCEL_UNSUPPORTED`.
|
|
221
|
+
*/
|
|
222
|
+
createSignal: <TValue = unknown>() => WorkflowSignal<TValue>;
|
|
223
|
+
/** The provider step runner. Use `context.sleep()` to sleep and `context.createSignal()` to wait for a signal. */
|
|
224
|
+
step?: Omit<WorkflowProviderStep, "sleep" | "sleepUntil" | "waitForSignal">;
|
|
195
225
|
/**
|
|
196
226
|
* Sleeps for a duration such as `'1 day'` or a number of milliseconds, or until a `Date`. Only a folder Workflow's
|
|
197
227
|
* `index.ts` can sleep. In a single-file Workflow it throws `WORKFLOW_ROOT_STEP_SLEEP`. The sleep is durable on
|
|
@@ -236,7 +266,7 @@ type WorkflowStartArguments<TPayload> = undefined extends TPayload ? [payload?:
|
|
|
236
266
|
* Codes that `startWorkflow()`, `getWorkflowRun()`, `cancelWorkflow()`, `resumeWorkflow()`, and the methods of a
|
|
237
267
|
* WorkflowHandle return in the `[error, data]` tuple. Config errors throw instead.
|
|
238
268
|
*/
|
|
239
|
-
type WorkflowResultErrorCode = "RUNTIME_DURATION_INVALID" | "WORKFLOW_CLOUDFLARE_RUN_ID_INVALID" | "WORKFLOW_OPERATION_UNSUPPORTED" | "WORKFLOW_PROVIDER_OPERATION_FAILED" | "WORKFLOW_R0014" | "WORKFLOW_RUN_ID_UNSUPPORTED" | "WORKFLOW_START_DELAY_INVALID";
|
|
269
|
+
type WorkflowResultErrorCode = "RUNTIME_DURATION_INVALID" | "WORKFLOW_CLOUDFLARE_RUN_ID_INVALID" | "WORKFLOW_OPERATION_UNSUPPORTED" | "WORKFLOW_PROVIDER_OPERATION_FAILED" | "WORKFLOW_R0014" | "WORKFLOW_RUN_ID_UNSUPPORTED" | "WORKFLOW_SIGNAL_NOT_WAITING" | "WORKFLOW_SIGNAL_TOKEN_INVALID" | "WORKFLOW_START_DELAY_INVALID";
|
|
240
270
|
/** `cancel()`, `getRun()`, and `start()` return `[error, data]`, as `cancelWorkflow()`, `getWorkflowRun()`, and `startWorkflow()` do. */
|
|
241
271
|
interface WorkflowHandle<TPayload = unknown, TResult = unknown> {
|
|
242
272
|
cancel: (id: string) => Promise<Result<WorkflowRun<unknown, unknown>, WorkflowResultErrorCode>>;
|
|
@@ -279,4 +309,4 @@ interface DiscoveredWorkflowDefinition {
|
|
|
279
309
|
steps?: string[];
|
|
280
310
|
}
|
|
281
311
|
//#endregion
|
|
282
|
-
export { WorkflowRun as A,
|
|
312
|
+
export { WorkflowRun as A, WorkflowStartOptions as B, WorkflowName as C, WorkflowProviderStep as D, WorkflowProviderContext as E, WorkflowSharedOptions as F, WorkflowStepOptions as G, WorkflowStepBackoff as H, WorkflowSignal as I, WorkflowSteps as J, WorkflowStepRegistry as K, WorkflowSignalResult as L, WorkflowRunStatus as M, WorkflowRunStep as N, WorkflowRegistry as O, WorkflowRuntimeConfigValue as P, WorkflowStartArguments as R, WorkflowModuleDriverOptions as S, WorkflowPayload as T, WorkflowStepContext as U, WorkflowStep as V, WorkflowStepFunction as W, WorkflowDuration as _, InferredWorkflowDriverOptions as a, WorkflowHandle as b, OpenWorkflowWorkerOptions as c, WorkflowCreateOptions as d, WorkflowDefinition as f, WorkflowDriverOptions as g, WorkflowDriver as h, DiscoveredWorkflowDefinition as i, WorkflowRunError as j, WorkflowResultErrorCode as k, ResolvedWorkflowOptions as l, WorkflowDefinitionRegistry as m, CloudflareWorkflowDriverOptions as n, OpenWorkflowDriverOptions as o, WorkflowDefinitionOptions as p, WorkflowStepRetries as q, CloudflareWorkflowInstance as r, OpenWorkflowPostgresOptions as s, CloudflareWorkflowBinding as t, VercelWorkflowDriverOptions as u, WorkflowDurationUnit as v, WorkflowOptions as w, WorkflowHandler as x, WorkflowExecutionContext as y, WorkflowStartDelay as z };
|
|
@@ -1,17 +1,13 @@
|
|
|
1
|
-
import { t as workflowErrorDiagnostics } from "./error-diagnostics-
|
|
2
|
-
import { n as createWorkflowError } from "./errors-
|
|
3
|
-
import { a as workflowStartAtKey } from "./delay-
|
|
4
|
-
import { a as safeWorkflowName, i as runWorkflowProviderOperation, r as isWorkflowBoundaryError, t as toWorkflowRunError } from "./run-error-
|
|
1
|
+
import { t as workflowErrorDiagnostics } from "./error-diagnostics-DMeaFmQD.js";
|
|
2
|
+
import { n as createWorkflowError } from "./errors-BNxEeeCO.js";
|
|
3
|
+
import { a as workflowStartAtKey } from "./delay-CHN4tv3i.js";
|
|
4
|
+
import { a as safeWorkflowName, i as runWorkflowProviderOperation, r as isWorkflowBoundaryError, t as toWorkflowRunError } from "./run-error-Ctn50EYz.js";
|
|
5
5
|
//#region src/runtime/vercel.ts
|
|
6
6
|
async function loadVercelWorkflowRuntime() {
|
|
7
7
|
const importVercelWorkflow = new Function("specifier", "return import(specifier)");
|
|
8
8
|
const [api, runtime] = await Promise.all([importVercelWorkflow("workflow/api"), importVercelWorkflow("workflow/runtime")]);
|
|
9
9
|
return createVercelWorkflowRuntime(api, runtime);
|
|
10
10
|
}
|
|
11
|
-
/**
|
|
12
|
-
* The largest step page that Vercel's step listing accepts. A larger page fails with HTTP 400. Workflow DevKit's own
|
|
13
|
-
* CLI uses the same bound (`packages/cli/src/lib/inspect/flag-bounds.ts` in vercel/workflow).
|
|
14
|
-
*/
|
|
15
11
|
const vercelStepPageLimit = 100;
|
|
16
12
|
function createVercelWorkflowRuntime(api, runtime) {
|
|
17
13
|
const { getRun, resumeHook, start } = api;
|
|
@@ -151,10 +147,6 @@ async function readRunIdentity(run) {
|
|
|
151
147
|
workflowName
|
|
152
148
|
};
|
|
153
149
|
}
|
|
154
|
-
/**
|
|
155
|
-
* The error of a failed native run. Workflow DevKit rejects `returnValue` of a failed run with
|
|
156
|
-
* `WorkflowRunFailedError`, which keeps the thrown value in `cause`.
|
|
157
|
-
*/
|
|
158
150
|
async function readVercelRunError(run) {
|
|
159
151
|
try {
|
|
160
152
|
await run.returnValue;
|
package/dist/vite.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { i as DiscoveredWorkflowDefinition, w as WorkflowOptions } from "./types-
|
|
2
|
-
import { n as OperationContributionEntry } from "./operation-definition-
|
|
1
|
+
import { i as DiscoveredWorkflowDefinition, w as WorkflowOptions } from "./types-BWPHeugk.js";
|
|
2
|
+
import { n as OperationContributionEntry } from "./operation-definition-CI2PJGqu.js";
|
|
3
3
|
import { Plugin, ResolvedConfig, Rolldown, UserConfig } from "vite";
|
|
4
4
|
//#region src/internal/runtime-modules.d.ts
|
|
5
5
|
type WorkflowRuntimeModuleName = "cloudflare-runner" | "cloudflare-vite" | "dev-runs" | "execute" | "openworkflow-worker" | "state" | "vercel-vite";
|
|
@@ -63,6 +63,8 @@ interface ViteHubResourceSummary {
|
|
|
63
63
|
owner: string;
|
|
64
64
|
/** Absent when no id is known yet. */
|
|
65
65
|
source?: ViteHubResourceIdSource;
|
|
66
|
+
/** The resource is in the Wrangler config of a standalone Provider Output Worker, not in Nitro's. */
|
|
67
|
+
standalone?: boolean;
|
|
66
68
|
}
|
|
67
69
|
interface ViteHubInspectionContributor {
|
|
68
70
|
definitions?: readonly ViteHubDefinitionInspector[];
|
|
@@ -516,6 +518,14 @@ interface WorkflowInspectionOptions {
|
|
|
516
518
|
}
|
|
517
519
|
/** Lists user-facing Workflow Definitions. Generated Agent recovery Workflows are internal and stay hidden. */
|
|
518
520
|
export declare function inspectWorkflowDefinitions(options: WorkflowInspectionOptions): ViteHubDefinitionSummary[];
|
|
521
|
+
/**
|
|
522
|
+
* Lists the Cloudflare Workflows that the build binds, with the same binding and Workflow names as the generated Wrangler
|
|
523
|
+
* config. Agent recovery Workflows are listed too, since the Worker needs their bindings. Reads local files only.
|
|
524
|
+
*/
|
|
525
|
+
export declare function inspectWorkflowResources(options: WorkflowInspectionOptions & {
|
|
526
|
+
binding?: string;
|
|
527
|
+
name?: string;
|
|
528
|
+
}): ViteHubResourceSummary[];
|
|
519
529
|
/** Console section that lists discovered Workflow Definitions. `vitehub inspect definitions` reads the same data. */
|
|
520
530
|
export declare const workflowConsoleSection: ViteHubConsoleSectionContribution<WorkflowInspectionOptions>;
|
|
521
531
|
//#endregion
|
|
@@ -550,15 +560,10 @@ export type WorkflowVitePlugin = Plugin & Partial<Pick<ProviderOutputEnvironment
|
|
|
550
560
|
};
|
|
551
561
|
};
|
|
552
562
|
interface InternalWorkflowModuleOptions {
|
|
553
|
-
/** The module that exports `getPostgresConnectionUrl()`. Default `@vite-hub/database`. */
|
|
554
563
|
databaseImport?: string;
|
|
555
564
|
hosting?: string;
|
|
556
565
|
importBase?: string;
|
|
557
566
|
providerImportAliases?: Record<string, string>;
|
|
558
|
-
/**
|
|
559
|
-
* The app that the Workflow Provider Output serves next to the Workflows. `true` finds `src/server.ts` or
|
|
560
|
-
* `src/worker.ts`, a file name such as `"server-workflow.ts"` picks that file in `src`, and `false` serves no app.
|
|
561
|
-
*/
|
|
562
567
|
includeUserAppEntry?: boolean | string;
|
|
563
568
|
workspaceDependencyRuntimeImports?: {
|
|
564
569
|
shellWorkspace?: string;
|