@upstash/workflow 1.4.0-rc → 1.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 +9 -0
- package/astro.d.mts +18 -2
- package/astro.d.ts +18 -2
- package/astro.js +129 -29
- package/astro.mjs +1 -1
- package/{chunk-UH6O5IJY.mjs → chunk-J3PXPHSB.mjs} +129 -29
- package/cloudflare.d.mts +16 -2
- package/cloudflare.d.ts +16 -2
- package/cloudflare.js +129 -29
- package/cloudflare.mjs +1 -1
- package/docs/agents/examples.mdx +8 -0
- package/docs/agents/features.mdx +374 -0
- package/docs/agents/getting-started.mdx +139 -0
- package/docs/agents/overview.mdx +40 -0
- package/docs/agents/patterns/evaluator-optimizer.mdx +72 -0
- package/docs/agents/patterns/orchestrator-workers.mdx +95 -0
- package/docs/agents/patterns/parallelization.mdx +77 -0
- package/docs/agents/patterns/prompt-chaining.mdx +84 -0
- package/docs/basics/best-practices/at-least-one-step.mdx +57 -0
- package/docs/basics/best-practices/business-logic-in-steps.mdx +52 -0
- package/docs/basics/best-practices/deterministic-code.mdx +132 -0
- package/docs/basics/best-practices/idempotency.mdx +38 -0
- package/docs/basics/best-practices/nested-context-methods.mdx +38 -0
- package/docs/basics/best-practices/overview.mdx +35 -0
- package/docs/basics/best-practices/promise-any.mdx +9 -0
- package/docs/basics/best-practices/return-step-results.mdx +80 -0
- package/docs/basics/best-practices/workflow-limits.mdx +62 -0
- package/docs/basics/client/cancel.mdx +137 -0
- package/docs/basics/client/dlq/callback.mdx +21 -0
- package/docs/basics/client/dlq/delete.mdx +120 -0
- package/docs/basics/client/dlq/list.mdx +96 -0
- package/docs/basics/client/dlq/restart.mdx +164 -0
- package/docs/basics/client/dlq/resume.mdx +167 -0
- package/docs/basics/client/logs.mdx +139 -0
- package/docs/basics/client/notify.mdx +79 -0
- package/docs/basics/client/trigger.mdx +139 -0
- package/docs/basics/client/waiters.mdx +31 -0
- package/docs/basics/client.mdx +40 -0
- package/docs/basics/how.mdx +65 -0
- package/docs/basics/serve/advanced.mdx +435 -0
- package/docs/basics/serve.mdx +142 -0
- package/docs/changelog.mdx +174 -0
- package/docs/examples/allInOne.mdx +377 -0
- package/docs/examples/authWebhook.mdx +567 -0
- package/docs/examples/customRetry.mdx +309 -0
- package/docs/examples/customerOnboarding.mdx +230 -0
- package/docs/examples/dynamicWorkflow.mdx +203 -0
- package/docs/examples/eCommerceOrderFulfillment.mdx +281 -0
- package/docs/examples/imageProcessing.mdx +339 -0
- package/docs/examples/paymentRetry.mdx +438 -0
- package/docs/examples/waitForEvent.mdx +201 -0
- package/docs/features/dlq/callback.mdx +42 -0
- package/docs/features/dlq/delete.mdx +29 -0
- package/docs/features/dlq/restart.mdx +33 -0
- package/docs/features/dlq/resume.mdx +37 -0
- package/docs/features/dlq.mdx +39 -0
- package/docs/features/failure-callback.mdx +102 -0
- package/docs/features/failureFunction/advanced.mdx +36 -0
- package/docs/features/failureFunction/reliability.mdx +33 -0
- package/docs/features/flow-control/monitor.mdx +22 -0
- package/docs/features/flow-control/parallelism.mdx +45 -0
- package/docs/features/flow-control/rate-period.mdx +46 -0
- package/docs/features/flow-control.mdx +164 -0
- package/docs/features/invoke/serveMany.mdx +109 -0
- package/docs/features/invoke.mdx +39 -0
- package/docs/features/notify.mdx +127 -0
- package/docs/features/parallel-steps.mdx +41 -0
- package/docs/features/retries/prevent-retries.mdx +140 -0
- package/docs/features/retries.mdx +93 -0
- package/docs/features/wait-for-event.mdx +104 -0
- package/docs/features/wait.mdx +44 -0
- package/docs/features/webhooks.mdx +116 -0
- package/docs/getstarted.mdx +200 -0
- package/docs/howto/cancel.mdx +50 -0
- package/docs/howto/changes.mdx +33 -0
- package/docs/howto/configure.mdx +42 -0
- package/docs/howto/failures.mdx +185 -0
- package/docs/howto/get-token.mdx +18 -0
- package/docs/howto/local-development/development-server.mdx +130 -0
- package/docs/howto/local-development/local-tunnel.mdx +73 -0
- package/docs/howto/middlewares.mdx +263 -0
- package/docs/howto/migrations.mdx +335 -0
- package/docs/howto/multi-region.mdx +107 -0
- package/docs/howto/parallel-runs.mdx +52 -0
- package/docs/howto/readonly-token.mdx +19 -0
- package/docs/howto/realtime/basic.mdx +315 -0
- package/docs/howto/realtime/human-in-the-loop.mdx +454 -0
- package/docs/howto/redact-fields.mdx +61 -0
- package/docs/howto/retry-now.mdx +41 -0
- package/docs/howto/schedule.mdx +104 -0
- package/docs/howto/security.mdx +143 -0
- package/docs/howto/singleton.mdx +137 -0
- package/docs/howto/start.mdx +69 -0
- package/docs/howto/use-webhooks.mdx +256 -0
- package/docs/integrations/aisdk.mdx +316 -0
- package/docs/integrations/anthropic.mdx +51 -0
- package/docs/integrations/datadog.mdx +69 -0
- package/docs/integrations/openai.mdx +87 -0
- package/docs/integrations/prometheus.mdx +65 -0
- package/docs/integrations/resend.mdx +82 -0
- package/docs/llms-txt.mdx +4 -0
- package/docs/openapi.yaml +2944 -0
- package/docs/pricing.mdx +22 -0
- package/docs/quickstarts/astro.mdx +185 -0
- package/docs/quickstarts/cloudflare-workers.mdx +190 -0
- package/docs/quickstarts/express.mdx +190 -0
- package/docs/quickstarts/fastapi.mdx +282 -0
- package/docs/quickstarts/flask.mdx +282 -0
- package/docs/quickstarts/hono.mdx +221 -0
- package/docs/quickstarts/nextjs-fastapi.mdx +147 -0
- package/docs/quickstarts/nextjs-flask.mdx +147 -0
- package/docs/quickstarts/nuxt.mdx +169 -0
- package/docs/quickstarts/platforms.mdx +78 -0
- package/docs/quickstarts/solidjs.mdx +161 -0
- package/docs/quickstarts/svelte.mdx +170 -0
- package/docs/quickstarts/tanstack-start.mdx +205 -0
- package/docs/quickstarts/vercel-nextjs.mdx +233 -0
- package/docs/roadmap.mdx +8 -0
- package/docs/sdk/workflow-js.mdx +4 -0
- package/docs/sdk/workflow-py.mdx +4 -0
- package/docs/steps/api.mdx +64 -0
- package/docs/steps/call.mdx +183 -0
- package/docs/steps/cancel.mdx +25 -0
- package/docs/steps/createWebhook.mdx +40 -0
- package/docs/steps/invoke.mdx +100 -0
- package/docs/steps/notify.mdx +105 -0
- package/docs/steps/run.mdx +150 -0
- package/docs/steps/sleep.mdx +97 -0
- package/docs/steps/sleepUntil.mdx +88 -0
- package/docs/steps/waitForEvent.mdx +60 -0
- package/docs/steps/waitForWebhook.mdx +78 -0
- package/docs/steps.mdx +108 -0
- package/docs/troubleshooting/general.mdx +331 -0
- package/docs/troubleshooting/vercel.mdx +82 -0
- package/express.d.mts +18 -2
- package/express.d.ts +18 -2
- package/express.js +129 -29
- package/express.mjs +1 -1
- package/h3.d.mts +18 -2
- package/h3.d.ts +18 -2
- package/h3.js +129 -29
- package/h3.mjs +1 -1
- package/hono.d.mts +16 -2
- package/hono.d.ts +16 -2
- package/hono.js +129 -29
- package/hono.mjs +1 -1
- package/index.d.mts +49 -21
- package/index.d.ts +49 -21
- package/index.js +145 -41
- package/index.mjs +17 -13
- package/nextjs.d.mts +39 -2
- package/nextjs.d.ts +39 -2
- package/nextjs.js +129 -29
- package/nextjs.mjs +1 -1
- package/package.json +1 -1
- package/react-router.d.mts +15 -2
- package/react-router.d.ts +15 -2
- package/react-router.js +129 -29
- package/react-router.mjs +1 -1
- package/{serve-many-CbIV7145.d.mts → serve-many-BKukvLNl.d.mts} +1 -1
- package/{serve-many-BCtZg31b.d.ts → serve-many-CrLKQpuZ.d.ts} +1 -1
- package/solidjs.d.mts +9 -1
- package/solidjs.d.ts +9 -1
- package/solidjs.js +129 -29
- package/solidjs.mjs +1 -1
- package/src/platforms/astro.ts +84 -0
- package/src/platforms/cloudflare.ts +127 -0
- package/src/platforms/express.ts +147 -0
- package/src/platforms/h3.ts +105 -0
- package/src/platforms/hono.ts +103 -0
- package/src/platforms/nextjs.ts +193 -0
- package/src/platforms/react-router.ts +92 -0
- package/src/platforms/solidjs.ts +59 -0
- package/src/platforms/svelte.ts +99 -0
- package/src/platforms/tanstack.ts +96 -0
- package/src/src/client/dlq.ts +387 -0
- package/src/src/client/filter-types.ts +166 -0
- package/src/src/client/index.ts +350 -0
- package/src/src/client/types.ts +501 -0
- package/src/src/client/utils.ts +307 -0
- package/src/src/constants.ts +82 -0
- package/src/src/context/api/anthropic.ts +58 -0
- package/src/src/context/api/base.ts +52 -0
- package/src/src/context/api/index.ts +24 -0
- package/src/src/context/api/openai.ts +125 -0
- package/src/src/context/api/resend.ts +57 -0
- package/src/src/context/auto-executor.ts +707 -0
- package/src/src/context/context.ts +681 -0
- package/src/src/context/index.ts +1 -0
- package/src/src/context/provider.ts +37 -0
- package/src/src/context/steps.ts +965 -0
- package/src/src/error.ts +203 -0
- package/src/src/index.ts +13 -0
- package/src/src/middleware/default-callbacks.ts +20 -0
- package/src/src/middleware/index.ts +2 -0
- package/src/src/middleware/logging.ts +39 -0
- package/src/src/middleware/manager.ts +148 -0
- package/src/src/middleware/middleware.ts +46 -0
- package/src/src/middleware/types.ts +72 -0
- package/src/src/qstash/headers.ts +397 -0
- package/src/src/qstash/step-config.ts +268 -0
- package/src/src/qstash/submit-steps.ts +284 -0
- package/src/src/serve/authorization.ts +118 -0
- package/src/src/serve/index.ts +475 -0
- package/src/src/serve/multi-region/handlers.ts +225 -0
- package/src/src/serve/multi-region/utils.ts +88 -0
- package/src/src/serve/options.ts +241 -0
- package/src/src/serve/serve-many.ts +117 -0
- package/src/src/serve/utils.ts +11 -0
- package/src/src/types.ts +590 -0
- package/src/src/utils.ts +141 -0
- package/src/src/workflow-parser.ts +472 -0
- package/src/src/workflow-requests.ts +575 -0
- package/svelte.d.mts +16 -2
- package/svelte.d.ts +16 -2
- package/svelte.js +129 -29
- package/svelte.mjs +1 -1
- package/tanstack.d.mts +14 -2
- package/tanstack.d.ts +14 -2
- package/tanstack.js +129 -29
- package/tanstack.mjs +1 -1
- package/{types-ffLPVG5_.d.ts → types-CrgK5P6p.d.mts} +124 -24
- package/{types-ffLPVG5_.d.mts → types-CrgK5P6p.d.ts} +124 -24
|
@@ -0,0 +1,284 @@
|
|
|
1
|
+
import { NO_CONCURRENCY, WORKFLOW_STEP_CONFIG_CALL_TYPE } from "../constants";
|
|
2
|
+
import { attachStepNameToError, WorkflowAbort } from "../error";
|
|
3
|
+
import { Step, StepSettings, Telemetry } from "../types";
|
|
4
|
+
import { WorkflowContext } from "../context";
|
|
5
|
+
import { BaseLazyStep } from "../context/steps";
|
|
6
|
+
import { getHeaders } from "./headers";
|
|
7
|
+
import { DispatchDebug, DispatchLifecycle } from "../middleware/types";
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Submits parallel steps to QStash.
|
|
11
|
+
*
|
|
12
|
+
* Each plan step carries the step-level settings (flow control, retries)
|
|
13
|
+
* of its target step, since the delivery of a plan step is what executes
|
|
14
|
+
* the target step.
|
|
15
|
+
*
|
|
16
|
+
* @param context workflow context
|
|
17
|
+
* @param steps list of lazy steps to submit
|
|
18
|
+
* @param initialStepCount initial step count
|
|
19
|
+
* @param invokeCount current invoke count
|
|
20
|
+
* @param telemetry optional telemetry information
|
|
21
|
+
* @param dispatchDebug debug event dispatcher
|
|
22
|
+
*/
|
|
23
|
+
export const submitParallelSteps = async ({
|
|
24
|
+
context,
|
|
25
|
+
steps,
|
|
26
|
+
initialStepCount,
|
|
27
|
+
invokeCount,
|
|
28
|
+
telemetry,
|
|
29
|
+
dispatchDebug,
|
|
30
|
+
}: {
|
|
31
|
+
context: WorkflowContext;
|
|
32
|
+
steps: BaseLazyStep[];
|
|
33
|
+
initialStepCount: number;
|
|
34
|
+
invokeCount: number;
|
|
35
|
+
telemetry?: Telemetry;
|
|
36
|
+
dispatchDebug: DispatchDebug;
|
|
37
|
+
}) => {
|
|
38
|
+
const planSteps = steps.map((step, index) =>
|
|
39
|
+
step.getPlanStep(steps.length, initialStepCount + index)
|
|
40
|
+
);
|
|
41
|
+
|
|
42
|
+
await dispatchDebug("onInfo", {
|
|
43
|
+
info: `Submitting ${planSteps.length} parallel steps.`,
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
const result = (await context.qstashClient.batch(
|
|
47
|
+
planSteps.map((planStep, index) => {
|
|
48
|
+
const { headers } = getHeaders({
|
|
49
|
+
initHeaderValue: "false",
|
|
50
|
+
workflowConfig: {
|
|
51
|
+
workflowRunId: context.workflowRunId,
|
|
52
|
+
workflowUrl: context.url,
|
|
53
|
+
workflowRunCreatedAt: context.workflowRunCreatedAt,
|
|
54
|
+
telemetry,
|
|
55
|
+
},
|
|
56
|
+
invokeCount,
|
|
57
|
+
stepSettings: steps[index].stepSettings,
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
return {
|
|
61
|
+
headers,
|
|
62
|
+
method: "POST",
|
|
63
|
+
url: context.url,
|
|
64
|
+
body: JSON.stringify(planStep),
|
|
65
|
+
notBefore: planStep.sleepUntil,
|
|
66
|
+
delay: planStep.sleepFor,
|
|
67
|
+
};
|
|
68
|
+
})
|
|
69
|
+
)) as { messageId: string }[];
|
|
70
|
+
|
|
71
|
+
if (result && result.length > 0) {
|
|
72
|
+
await dispatchDebug("onInfo", {
|
|
73
|
+
info: `Submitted ${planSteps.length} parallel steps. messageIds: ${result
|
|
74
|
+
.filter((r) => r)
|
|
75
|
+
.map((r) => r.messageId)
|
|
76
|
+
.join(", ")}.`,
|
|
77
|
+
});
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
throw new WorkflowAbort(planSteps[0].stepName, planSteps[0]);
|
|
81
|
+
};
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* Executes a lazy step and returns its result step, without submitting
|
|
85
|
+
* it to QStash.
|
|
86
|
+
*
|
|
87
|
+
* @param lazyStep lazy step to execute
|
|
88
|
+
* @param stepId step ID
|
|
89
|
+
* @param concurrency concurrency level
|
|
90
|
+
* @param dispatchLifecycle lifecycle event dispatcher
|
|
91
|
+
*/
|
|
92
|
+
export const executeStep = async ({
|
|
93
|
+
lazyStep,
|
|
94
|
+
stepId,
|
|
95
|
+
concurrency,
|
|
96
|
+
dispatchLifecycle,
|
|
97
|
+
}: {
|
|
98
|
+
lazyStep: BaseLazyStep;
|
|
99
|
+
stepId: number;
|
|
100
|
+
concurrency: number;
|
|
101
|
+
dispatchLifecycle: DispatchLifecycle;
|
|
102
|
+
}): Promise<Step> => {
|
|
103
|
+
await dispatchLifecycle("beforeExecution", {
|
|
104
|
+
stepName: lazyStep.stepName,
|
|
105
|
+
});
|
|
106
|
+
|
|
107
|
+
try {
|
|
108
|
+
return await lazyStep.getResultStep(concurrency, stepId);
|
|
109
|
+
} catch (error) {
|
|
110
|
+
// The step function threw. Remember which step failed so the serve handler
|
|
111
|
+
// can report it to QStash via the `Upstash-Error-Step-Name` header.
|
|
112
|
+
attachStepNameToError(error, lazyStep.stepName);
|
|
113
|
+
throw error;
|
|
114
|
+
}
|
|
115
|
+
};
|
|
116
|
+
|
|
117
|
+
/**
|
|
118
|
+
* Submits an executed step's result to QStash.
|
|
119
|
+
*
|
|
120
|
+
* When `nextStepSettings` is given, they are attached to the request:
|
|
121
|
+
* the delivery of this request is what executes the next step, so this
|
|
122
|
+
* is how a step's settings are applied without an extra step config
|
|
123
|
+
* request.
|
|
124
|
+
*
|
|
125
|
+
* @param context workflow context
|
|
126
|
+
* @param lazyStep lazy step which was executed
|
|
127
|
+
* @param resultStep result step to submit
|
|
128
|
+
* @param invokeCount current invoke count
|
|
129
|
+
* @param concurrency concurrency level
|
|
130
|
+
* @param telemetry optional telemetry information
|
|
131
|
+
* @param dispatchDebug debug event dispatcher
|
|
132
|
+
* @param nextStepSettings step-level settings of the next step
|
|
133
|
+
*/
|
|
134
|
+
export const submitStepResult = async ({
|
|
135
|
+
context,
|
|
136
|
+
lazyStep,
|
|
137
|
+
resultStep,
|
|
138
|
+
invokeCount,
|
|
139
|
+
concurrency,
|
|
140
|
+
telemetry,
|
|
141
|
+
dispatchDebug,
|
|
142
|
+
nextStepSettings,
|
|
143
|
+
}: {
|
|
144
|
+
context: WorkflowContext;
|
|
145
|
+
lazyStep: BaseLazyStep;
|
|
146
|
+
resultStep: Step;
|
|
147
|
+
invokeCount: number;
|
|
148
|
+
concurrency: number;
|
|
149
|
+
telemetry?: Telemetry;
|
|
150
|
+
dispatchDebug: DispatchDebug;
|
|
151
|
+
nextStepSettings?: StepSettings;
|
|
152
|
+
}) => {
|
|
153
|
+
const { headers } = lazyStep.getHeaders({
|
|
154
|
+
context,
|
|
155
|
+
step: resultStep,
|
|
156
|
+
invokeCount,
|
|
157
|
+
telemetry,
|
|
158
|
+
stepSettings: nextStepSettings,
|
|
159
|
+
});
|
|
160
|
+
|
|
161
|
+
const body = lazyStep.getBody({
|
|
162
|
+
context,
|
|
163
|
+
step: resultStep,
|
|
164
|
+
headers,
|
|
165
|
+
invokeCount,
|
|
166
|
+
telemetry,
|
|
167
|
+
});
|
|
168
|
+
|
|
169
|
+
const submitResult = await lazyStep.submitStep({
|
|
170
|
+
context,
|
|
171
|
+
body,
|
|
172
|
+
headers,
|
|
173
|
+
isParallel: concurrency !== NO_CONCURRENCY,
|
|
174
|
+
invokeCount,
|
|
175
|
+
step: resultStep,
|
|
176
|
+
telemetry,
|
|
177
|
+
});
|
|
178
|
+
|
|
179
|
+
if (submitResult && submitResult[0]) {
|
|
180
|
+
await dispatchDebug("onInfo", {
|
|
181
|
+
info: `Submitted step "${resultStep.stepName}" with messageId: ${submitResult[0].messageId}.`,
|
|
182
|
+
});
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
return resultStep;
|
|
186
|
+
};
|
|
187
|
+
|
|
188
|
+
/**
|
|
189
|
+
* Publishes a step config request: a hidden helper request which makes
|
|
190
|
+
* QStash call the workflow endpoint again, this time with the
|
|
191
|
+
* step-level settings of the step which is about to execute.
|
|
192
|
+
*
|
|
193
|
+
* A step's settings must be on the request whose delivery executes the
|
|
194
|
+
* step. The request delivered to the endpoint right now was published
|
|
195
|
+
* before the step was known, so QStash applied the run's configuration
|
|
196
|
+
* to it instead. Rather than executing the step in this
|
|
197
|
+
* delivery, we publish this request with the settings: QStash delivers
|
|
198
|
+
* it step-configured, and that delivery
|
|
199
|
+
* executes the step.
|
|
200
|
+
*
|
|
201
|
+
* The request has the `stepConfig` call type: QStash doesn't treat it as
|
|
202
|
+
* a step and hides it from the step logs.
|
|
203
|
+
*
|
|
204
|
+
* It is published with content based deduplication, and its body carries
|
|
205
|
+
* the target step id. Together those make a second publish for the same
|
|
206
|
+
* step collapse into the first — which is what happens when the delivery
|
|
207
|
+
* that published one is retried — while leaving the requests of two
|
|
208
|
+
* different steps distinct. The deduplication hash covers the workflow
|
|
209
|
+
* run id and its creation time (`Upstash-Workflow-CreatedAt`), so two runs
|
|
210
|
+
* never collide with each other, even when they share a run id.
|
|
211
|
+
*
|
|
212
|
+
* @param context workflow context
|
|
213
|
+
* @param lazyStep lazy step whose settings are applied
|
|
214
|
+
* @param targetStep id of the step the resulting delivery will execute
|
|
215
|
+
* @param invokeCount current invoke count
|
|
216
|
+
* @param telemetry optional telemetry information
|
|
217
|
+
* @param dispatchDebug debug event dispatcher
|
|
218
|
+
*/
|
|
219
|
+
export const publishStepConfigRequest = async ({
|
|
220
|
+
context,
|
|
221
|
+
lazyStep,
|
|
222
|
+
targetStep,
|
|
223
|
+
invokeCount,
|
|
224
|
+
telemetry,
|
|
225
|
+
dispatchDebug,
|
|
226
|
+
}: {
|
|
227
|
+
context: WorkflowContext;
|
|
228
|
+
lazyStep: BaseLazyStep;
|
|
229
|
+
targetStep: number;
|
|
230
|
+
invokeCount: number;
|
|
231
|
+
telemetry?: Telemetry;
|
|
232
|
+
dispatchDebug: DispatchDebug;
|
|
233
|
+
}) => {
|
|
234
|
+
const { headers } = getHeaders({
|
|
235
|
+
initHeaderValue: "false",
|
|
236
|
+
workflowConfig: {
|
|
237
|
+
workflowRunId: context.workflowRunId,
|
|
238
|
+
workflowRunCreatedAt: context.workflowRunCreatedAt,
|
|
239
|
+
workflowUrl: context.url,
|
|
240
|
+
telemetry,
|
|
241
|
+
},
|
|
242
|
+
invokeCount,
|
|
243
|
+
stepSettings: lazyStep.stepSettings,
|
|
244
|
+
});
|
|
245
|
+
|
|
246
|
+
await dispatchDebug("onInfo", {
|
|
247
|
+
info:
|
|
248
|
+
`Step "${lazyStep.stepName}" (${targetStep}) has step-level settings which the current` +
|
|
249
|
+
` delivery does not carry. Requesting a delivery which does.`,
|
|
250
|
+
});
|
|
251
|
+
|
|
252
|
+
const result = (await context.qstashClient.publishJSON({
|
|
253
|
+
headers: {
|
|
254
|
+
...headers,
|
|
255
|
+
"Upstash-Workflow-CallType": WORKFLOW_STEP_CONFIG_CALL_TYPE,
|
|
256
|
+
"Upstash-Forward-Upstash-Workflow-StepId": targetStep.toString(),
|
|
257
|
+
"Upstash-Forward-Upstash-Workflow-StepName": lazyStep.stepName,
|
|
258
|
+
},
|
|
259
|
+
method: "POST",
|
|
260
|
+
body: { targetStep, invokeCount },
|
|
261
|
+
url: context.url,
|
|
262
|
+
contentBasedDeduplication: true,
|
|
263
|
+
})) as { messageId?: string; deduplicated?: boolean };
|
|
264
|
+
|
|
265
|
+
if (result?.deduplicated) {
|
|
266
|
+
// A step config request for this step was already published. Either
|
|
267
|
+
// the delivery which published it failed and QStash is retrying it,
|
|
268
|
+
// in which case the original still produces that delivery and
|
|
269
|
+
// there is nothing to do here — or this SDK asked twice for the same
|
|
270
|
+
// step, which stalls the run: nothing new was published, so no
|
|
271
|
+
// further delivery follows. Worth a warning either way, since it is
|
|
272
|
+
// the only trace the second case leaves.
|
|
273
|
+
await dispatchDebug("onWarning", {
|
|
274
|
+
warning:
|
|
275
|
+
`A step config request for step "${lazyStep.stepName}" (${targetStep}) was already` +
|
|
276
|
+
` published and this one was deduplicated. Expected when the delivery which published` +
|
|
277
|
+
` it is being retried; otherwise the run stops here.`,
|
|
278
|
+
});
|
|
279
|
+
} else if (result?.messageId) {
|
|
280
|
+
await dispatchDebug("onInfo", {
|
|
281
|
+
info: `Requested a delivery under the settings of step "${lazyStep.stepName}" with messageId: ${result.messageId}.`,
|
|
282
|
+
});
|
|
283
|
+
}
|
|
284
|
+
};
|
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
import type { Err, Ok } from "neverthrow";
|
|
2
|
+
import { err, ok } from "neverthrow";
|
|
3
|
+
import {
|
|
4
|
+
isInstanceOf,
|
|
5
|
+
WorkflowAuthError,
|
|
6
|
+
WorkflowNonRetryableError,
|
|
7
|
+
WorkflowRetryAfterError,
|
|
8
|
+
} from "../error";
|
|
9
|
+
import { RouteFunction } from "../types";
|
|
10
|
+
import { WorkflowContext } from "../context";
|
|
11
|
+
import type { EffectiveConfig } from "../qstash/step-config";
|
|
12
|
+
import { BaseLazyStep } from "../context/steps";
|
|
13
|
+
import { Client } from "@upstash/qstash";
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Workflow context which throws WorkflowAbort before running the steps.
|
|
17
|
+
*
|
|
18
|
+
* Used for making a dry run before running any steps to check authentication.
|
|
19
|
+
*
|
|
20
|
+
* Consider an endpoint like this:
|
|
21
|
+
* ```ts
|
|
22
|
+
* export const { POST } = serve(async (context) => {
|
|
23
|
+
* if (context.headers.get("authentication") !== "Bearer secretPassword") {
|
|
24
|
+
* console.error("Authentication failed.");
|
|
25
|
+
* return;
|
|
26
|
+
* }
|
|
27
|
+
*
|
|
28
|
+
* // ...
|
|
29
|
+
* })
|
|
30
|
+
* ```
|
|
31
|
+
*
|
|
32
|
+
* the serve method will first call the routeFunction with an DisabledWorkflowContext.
|
|
33
|
+
* Here is the action we take in different cases
|
|
34
|
+
* - "step-found": we will run the workflow related sections of `serve`.
|
|
35
|
+
* - "run-ended": simply return success and end the workflow
|
|
36
|
+
* - error: returns 500.
|
|
37
|
+
*/
|
|
38
|
+
export class DisabledWorkflowContext<
|
|
39
|
+
TInitialPayload = unknown,
|
|
40
|
+
> extends WorkflowContext<TInitialPayload> {
|
|
41
|
+
private static readonly disabledMessage = "disabled-qstash-worklfow-run";
|
|
42
|
+
public readonly disabled = true;
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* overwrite the WorkflowContext.addStep method to always raise WorkflowAuthError
|
|
46
|
+
* error in order to stop the execution whenever we encounter a step.
|
|
47
|
+
*
|
|
48
|
+
* @param _step
|
|
49
|
+
*/
|
|
50
|
+
protected async addStep<TResult = unknown>(
|
|
51
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
52
|
+
_step: BaseLazyStep<TResult>
|
|
53
|
+
): Promise<TResult> {
|
|
54
|
+
throw new WorkflowAuthError(DisabledWorkflowContext.disabledMessage);
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* overwrite cancel method to throw WorkflowAuthError with the disabledMessage
|
|
59
|
+
*/
|
|
60
|
+
public async cancel() {
|
|
61
|
+
throw new WorkflowAuthError(DisabledWorkflowContext.disabledMessage);
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* copies the passed context to create a DisabledWorkflowContext. Then, runs the
|
|
66
|
+
* route function with the new context.
|
|
67
|
+
*
|
|
68
|
+
* - returns "run-ended" if there are no steps found or
|
|
69
|
+
* if the auth failed and user called `return`
|
|
70
|
+
* - returns "step-found" if DisabledWorkflowContext.addStep is called.
|
|
71
|
+
* - if there is another error, returns the error.
|
|
72
|
+
*
|
|
73
|
+
* @param routeFunction
|
|
74
|
+
*/
|
|
75
|
+
public static async tryAuthentication<TInitialPayload = unknown>(
|
|
76
|
+
routeFunction: RouteFunction<TInitialPayload>,
|
|
77
|
+
context: WorkflowContext<TInitialPayload>,
|
|
78
|
+
effectiveConfig?: EffectiveConfig
|
|
79
|
+
): Promise<Ok<"step-found" | "run-ended", never> | Err<never, Error>> {
|
|
80
|
+
const disabledContext = new DisabledWorkflowContext({
|
|
81
|
+
qstashClient: new Client({
|
|
82
|
+
baseUrl: "disabled-client",
|
|
83
|
+
token: "disabled-client",
|
|
84
|
+
devMode: false,
|
|
85
|
+
}),
|
|
86
|
+
workflowRunId: context.workflowRunId,
|
|
87
|
+
workflowRunCreatedAt: context.workflowRunCreatedAt,
|
|
88
|
+
headers: context.headers,
|
|
89
|
+
steps: [],
|
|
90
|
+
url: context.url,
|
|
91
|
+
initialPayload: context.requestPayload,
|
|
92
|
+
env: context.env,
|
|
93
|
+
label: context.labels,
|
|
94
|
+
retried: context.retried,
|
|
95
|
+
// the route function runs for real here until it reaches a step, so
|
|
96
|
+
// it observes the same configuration as it will on the real context
|
|
97
|
+
effectiveConfig,
|
|
98
|
+
});
|
|
99
|
+
|
|
100
|
+
try {
|
|
101
|
+
await routeFunction(disabledContext);
|
|
102
|
+
} catch (error) {
|
|
103
|
+
if (
|
|
104
|
+
(isInstanceOf(error, WorkflowAuthError) && error.stepName === this.disabledMessage) ||
|
|
105
|
+
isInstanceOf(error, WorkflowNonRetryableError) ||
|
|
106
|
+
isInstanceOf(error, WorkflowRetryAfterError)
|
|
107
|
+
) {
|
|
108
|
+
return ok("step-found");
|
|
109
|
+
}
|
|
110
|
+
console.warn(
|
|
111
|
+
"Upstash Workflow: Received an error while authorizing request. Please avoid throwing errors before the first step of your workflow."
|
|
112
|
+
);
|
|
113
|
+
return err(error as Error);
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
return ok("run-ended");
|
|
117
|
+
}
|
|
118
|
+
}
|