@upstash/workflow 1.4.0 → 1.5.1-canary.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 +19 -2
- package/astro.d.ts +19 -2
- package/astro.js +670 -159
- package/astro.mjs +1 -1
- package/{chunk-DRHEHWMS.mjs → chunk-J3PXPHSB.mjs} +828 -317
- package/cloudflare.d.mts +17 -2
- package/cloudflare.d.ts +17 -2
- package/cloudflare.js +670 -159
- package/cloudflare.mjs +1 -1
- package/docs/agents/examples.mdx +8 -0
- package/docs/agents/features.mdx +375 -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 +575 -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 +114 -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 +2946 -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 +19 -2
- package/express.d.ts +19 -2
- package/express.js +670 -159
- package/express.mjs +1 -1
- package/h3.d.mts +19 -2
- package/h3.d.ts +19 -2
- package/h3.js +670 -159
- package/h3.mjs +1 -1
- package/hono.d.mts +17 -2
- package/hono.d.ts +17 -2
- package/hono.js +670 -159
- package/hono.mjs +1 -1
- package/index.d.mts +50 -21
- package/index.d.ts +50 -21
- package/index.js +686 -171
- package/index.mjs +17 -13
- package/nextjs.d.mts +40 -2
- package/nextjs.d.ts +40 -2
- package/nextjs.js +670 -159
- package/nextjs.mjs +1 -1
- package/package.json +1 -1
- package/react-router.d.mts +16 -2
- package/react-router.d.ts +16 -2
- package/react-router.js +670 -159
- package/react-router.mjs +1 -1
- package/{serve-many-CG3BFvO3.d.mts → serve-many-BKukvLNl.d.mts} +1 -1
- package/{serve-many-iJF1IUXk.d.ts → serve-many-CrLKQpuZ.d.ts} +1 -1
- package/solidjs.d.mts +10 -1
- package/solidjs.d.ts +10 -1
- package/solidjs.js +670 -159
- 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 +17 -2
- package/svelte.d.ts +17 -2
- package/svelte.js +670 -159
- package/svelte.mjs +1 -1
- package/tanstack.d.mts +15 -2
- package/tanstack.d.ts +15 -2
- package/tanstack.js +670 -159
- package/tanstack.mjs +1 -1
- package/{types-CekOpKvz.d.ts → types-CrgK5P6p.d.mts} +372 -32
- package/{types-CekOpKvz.d.mts → types-CrgK5P6p.d.ts} +372 -32
|
@@ -0,0 +1,965 @@
|
|
|
1
|
+
import type { Client, FlowControl, HTTPMethods } from "@upstash/qstash";
|
|
2
|
+
import type {
|
|
3
|
+
CallResponse,
|
|
4
|
+
CallSettings,
|
|
5
|
+
HeaderParams,
|
|
6
|
+
InvokeStepResponse,
|
|
7
|
+
InvokeWorkflowRequest,
|
|
8
|
+
LazyInvokeStepParams,
|
|
9
|
+
NotifyStepResponse,
|
|
10
|
+
Step,
|
|
11
|
+
StepFunction,
|
|
12
|
+
StepSettings,
|
|
13
|
+
StepType,
|
|
14
|
+
WaitRequest,
|
|
15
|
+
WaitStepResponse,
|
|
16
|
+
} from "../types";
|
|
17
|
+
import { makeNotifyRequest } from "../client/utils";
|
|
18
|
+
import type { Duration } from "../types";
|
|
19
|
+
import { WorkflowError } from "../error";
|
|
20
|
+
import {
|
|
21
|
+
decodeBase64,
|
|
22
|
+
getEventId,
|
|
23
|
+
getQStashUrl,
|
|
24
|
+
getUserIdFromToken,
|
|
25
|
+
getWorkflowRunId,
|
|
26
|
+
serializeLabel,
|
|
27
|
+
stringifyBody,
|
|
28
|
+
} from "../utils";
|
|
29
|
+
import { WorkflowContext } from "./context";
|
|
30
|
+
import { getHeaders, prepareFlowControl } from "../qstash/headers";
|
|
31
|
+
import {
|
|
32
|
+
DEFAULT_RETRIES,
|
|
33
|
+
WORKFLOW_FEATURE_HEADER,
|
|
34
|
+
WORKFLOW_INIT_HEADER,
|
|
35
|
+
WORKFLOW_LABEL_HEADER,
|
|
36
|
+
WORKFLOW_URL_HEADER,
|
|
37
|
+
} from "../constants";
|
|
38
|
+
import { getTelemetryHeaders, HeadersResponse } from "../workflow-requests";
|
|
39
|
+
|
|
40
|
+
type StepParams = { context: WorkflowContext } & Pick<HeaderParams, "telemetry"> &
|
|
41
|
+
Required<Pick<HeaderParams, "step" | "invokeCount">>;
|
|
42
|
+
type GetHeaderParams = StepParams & {
|
|
43
|
+
/**
|
|
44
|
+
* step-level settings to apply to the message being published. Set
|
|
45
|
+
* when the next step has settings and this step's submission is what
|
|
46
|
+
* will produce the delivery that executes it.
|
|
47
|
+
*/
|
|
48
|
+
stepSettings?: StepSettings;
|
|
49
|
+
};
|
|
50
|
+
type GetBodyParams = StepParams & Omit<HeadersResponse, "contentType">;
|
|
51
|
+
type SubmitStepParams = StepParams &
|
|
52
|
+
Pick<HeadersResponse, "headers"> & { body: string; isParallel: boolean };
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* Base class outlining steps. Basically, each step kind (run/sleep/sleepUntil)
|
|
56
|
+
* should have two methods: getPlanStep & getResultStep.
|
|
57
|
+
*
|
|
58
|
+
* getPlanStep works the same way for all so it's implemented here.
|
|
59
|
+
* The different step types will implement their own getResultStep method.
|
|
60
|
+
*/
|
|
61
|
+
export abstract class BaseLazyStep<TResult = unknown> {
|
|
62
|
+
public readonly stepName;
|
|
63
|
+
public abstract readonly stepType: StepType; // will be set in the subclasses
|
|
64
|
+
protected abstract readonly allowUndefinedOut: boolean;
|
|
65
|
+
protected readonly context: WorkflowContext;
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* step-level settings (flow control, retries etc.) which override the
|
|
69
|
+
* settings the workflow run was triggered with, for this step only.
|
|
70
|
+
*/
|
|
71
|
+
public readonly stepSettings?: StepSettings;
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* whether this step's result can be submitted after the route function
|
|
75
|
+
* has moved on, instead of right away.
|
|
76
|
+
*
|
|
77
|
+
* Enabled for steps whose `getResultStep` produces the final `out` —
|
|
78
|
+
* which is not the same as "needs no server round trip":
|
|
79
|
+
* `LazyCreateWebhookStep` builds its result in `getBody` and returns
|
|
80
|
+
* `out: undefined` from `getResultStep`, so deferring it would hand
|
|
81
|
+
* `undefined` to the route function.
|
|
82
|
+
*
|
|
83
|
+
* Deferring lets the route function continue and reveal the next step,
|
|
84
|
+
* so that step's settings can ride on this step's submission instead
|
|
85
|
+
* of needing a step config request of their own.
|
|
86
|
+
*/
|
|
87
|
+
public readonly supportsDeferredSubmission: boolean = false;
|
|
88
|
+
|
|
89
|
+
constructor(context: WorkflowContext, stepName: string, stepSettings?: StepSettings) {
|
|
90
|
+
this.context = context;
|
|
91
|
+
this.stepSettings = stepSettings;
|
|
92
|
+
if (!stepName) {
|
|
93
|
+
throw new WorkflowError(
|
|
94
|
+
"A workflow step name cannot be undefined or an empty string. Please provide a name for your workflow step."
|
|
95
|
+
);
|
|
96
|
+
}
|
|
97
|
+
if (typeof stepName !== "string") {
|
|
98
|
+
throw new WorkflowError(
|
|
99
|
+
`A workflow step name must be a string. Received "${stepName}" (${typeof stepName}).`
|
|
100
|
+
);
|
|
101
|
+
}
|
|
102
|
+
this.stepName = stepName;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
/**
|
|
106
|
+
* plan step to submit when step will run parallel with other
|
|
107
|
+
* steps (parallel call state `first`)
|
|
108
|
+
*
|
|
109
|
+
* @param concurrent number of steps running parallel
|
|
110
|
+
* @param targetStep target step id corresponding to this step
|
|
111
|
+
* @returns
|
|
112
|
+
*/
|
|
113
|
+
public abstract getPlanStep(concurrent: number, targetStep: number): Step<undefined>;
|
|
114
|
+
|
|
115
|
+
/**
|
|
116
|
+
* result step to submit after the step executes. Used in single step executions
|
|
117
|
+
* and when a plan step executes in parallel executions (parallel call state `partial`).
|
|
118
|
+
*
|
|
119
|
+
* @param concurrent
|
|
120
|
+
* @param stepId
|
|
121
|
+
*/
|
|
122
|
+
public abstract getResultStep(concurrent: number, stepId: number): Promise<Step<TResult>>;
|
|
123
|
+
|
|
124
|
+
/**
|
|
125
|
+
* parse the out field of a step result.
|
|
126
|
+
*
|
|
127
|
+
* will be called when returning the steps to the context from auto executor
|
|
128
|
+
*
|
|
129
|
+
* @param step step
|
|
130
|
+
* @returns parsed out field
|
|
131
|
+
*/
|
|
132
|
+
public parseOut(step: Step): TResult {
|
|
133
|
+
const out = step.out;
|
|
134
|
+
if (out === undefined) {
|
|
135
|
+
if (this.allowUndefinedOut) {
|
|
136
|
+
return this.handleUndefinedOut(step) as TResult;
|
|
137
|
+
} else {
|
|
138
|
+
throw new WorkflowError(
|
|
139
|
+
`Error while parsing output of ${this.stepType} step. Expected a string, but got: undefined`
|
|
140
|
+
);
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
if (typeof out === "object") {
|
|
145
|
+
// this is an error which should never happen.
|
|
146
|
+
console.warn(
|
|
147
|
+
`Error while parsing ${this.stepType} step output. Expected a string, but got object. Please reach out to Upstash Support.`
|
|
148
|
+
);
|
|
149
|
+
return out as TResult;
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
if (typeof out !== "string") {
|
|
153
|
+
throw new WorkflowError(
|
|
154
|
+
`Error while parsing output of ${this.stepType} step. Expected a string or undefined, but got: ${typeof out}`
|
|
155
|
+
);
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
return this.safeParseOut(out, step);
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
162
|
+
protected safeParseOut(out: string, step: Step): TResult {
|
|
163
|
+
return BaseLazyStep.tryParsing(out);
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
167
|
+
protected handleUndefinedOut(step: Step): TResult {
|
|
168
|
+
return undefined as TResult;
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
protected static tryParsing(stepOut: unknown) {
|
|
172
|
+
try {
|
|
173
|
+
return JSON.parse(stepOut as string);
|
|
174
|
+
} catch {
|
|
175
|
+
return stepOut;
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
getBody({ step }: GetBodyParams): string {
|
|
180
|
+
step.out = JSON.stringify(step.out);
|
|
181
|
+
return JSON.stringify(step);
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
getHeaders({
|
|
185
|
+
context,
|
|
186
|
+
telemetry,
|
|
187
|
+
invokeCount,
|
|
188
|
+
step,
|
|
189
|
+
stepSettings,
|
|
190
|
+
}: GetHeaderParams): HeadersResponse {
|
|
191
|
+
return getHeaders({
|
|
192
|
+
initHeaderValue: "false",
|
|
193
|
+
workflowConfig: {
|
|
194
|
+
workflowRunId: context.workflowRunId,
|
|
195
|
+
workflowUrl: context.url,
|
|
196
|
+
workflowRunCreatedAt: context.workflowRunCreatedAt,
|
|
197
|
+
useJSONContent: false,
|
|
198
|
+
telemetry,
|
|
199
|
+
},
|
|
200
|
+
invokeCount,
|
|
201
|
+
stepInfo: {
|
|
202
|
+
step,
|
|
203
|
+
lazyStep: this,
|
|
204
|
+
},
|
|
205
|
+
stepSettings,
|
|
206
|
+
});
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
async submitStep({ context, body, headers }: SubmitStepParams) {
|
|
210
|
+
return (await context.qstashClient.batch([
|
|
211
|
+
{
|
|
212
|
+
body,
|
|
213
|
+
headers,
|
|
214
|
+
method: "POST",
|
|
215
|
+
url: context.url,
|
|
216
|
+
},
|
|
217
|
+
])) as { messageId: string }[];
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
/**
|
|
222
|
+
* Lazy step definition for `context.run` case
|
|
223
|
+
*/
|
|
224
|
+
export class LazyFunctionStep<TResult = unknown> extends BaseLazyStep<TResult> {
|
|
225
|
+
private readonly stepFunction: StepFunction<TResult>;
|
|
226
|
+
stepType: StepType = "Run";
|
|
227
|
+
allowUndefinedOut = true;
|
|
228
|
+
public readonly supportsDeferredSubmission = true;
|
|
229
|
+
|
|
230
|
+
constructor(
|
|
231
|
+
context: WorkflowContext,
|
|
232
|
+
stepName: string,
|
|
233
|
+
stepFunction: StepFunction<TResult>,
|
|
234
|
+
stepSettings?: StepSettings
|
|
235
|
+
) {
|
|
236
|
+
super(context, stepName, stepSettings);
|
|
237
|
+
this.stepFunction = stepFunction;
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
public getPlanStep(concurrent: number, targetStep: number): Step<undefined> {
|
|
241
|
+
return {
|
|
242
|
+
stepId: 0,
|
|
243
|
+
stepName: this.stepName,
|
|
244
|
+
stepType: this.stepType,
|
|
245
|
+
concurrent,
|
|
246
|
+
targetStep,
|
|
247
|
+
};
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
public async getResultStep(concurrent: number, stepId: number): Promise<Step<TResult>> {
|
|
251
|
+
let result = this.stepFunction();
|
|
252
|
+
if (result instanceof Promise) {
|
|
253
|
+
result = await result;
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
return {
|
|
257
|
+
stepId,
|
|
258
|
+
stepName: this.stepName,
|
|
259
|
+
stepType: this.stepType,
|
|
260
|
+
out: result,
|
|
261
|
+
concurrent,
|
|
262
|
+
};
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
/**
|
|
267
|
+
* Lazy step definition for `context.sleep` case
|
|
268
|
+
*/
|
|
269
|
+
export class LazySleepStep extends BaseLazyStep {
|
|
270
|
+
private readonly sleep: number | Duration;
|
|
271
|
+
stepType: StepType = "SleepFor";
|
|
272
|
+
allowUndefinedOut = true;
|
|
273
|
+
public readonly supportsDeferredSubmission = true;
|
|
274
|
+
|
|
275
|
+
constructor(context: WorkflowContext, stepName: string, sleep: number | Duration) {
|
|
276
|
+
super(context, stepName);
|
|
277
|
+
this.sleep = sleep;
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
public getPlanStep(concurrent: number, targetStep: number): Step<undefined> {
|
|
281
|
+
return {
|
|
282
|
+
stepId: 0,
|
|
283
|
+
stepName: this.stepName,
|
|
284
|
+
stepType: this.stepType,
|
|
285
|
+
sleepFor: this.sleep,
|
|
286
|
+
concurrent,
|
|
287
|
+
targetStep,
|
|
288
|
+
};
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
public async getResultStep(concurrent: number, stepId: number): Promise<Step> {
|
|
292
|
+
return await Promise.resolve({
|
|
293
|
+
stepId,
|
|
294
|
+
stepName: this.stepName,
|
|
295
|
+
stepType: this.stepType,
|
|
296
|
+
sleepFor: this.sleep,
|
|
297
|
+
concurrent,
|
|
298
|
+
});
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
async submitStep({ context, body, headers, isParallel }: SubmitStepParams) {
|
|
302
|
+
return (await context.qstashClient.batch([
|
|
303
|
+
{
|
|
304
|
+
body,
|
|
305
|
+
headers,
|
|
306
|
+
method: "POST",
|
|
307
|
+
url: context.url,
|
|
308
|
+
delay: isParallel ? undefined : this.sleep,
|
|
309
|
+
},
|
|
310
|
+
])) as { messageId: string }[];
|
|
311
|
+
}
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
/**
|
|
315
|
+
* Lazy step definition for `context.sleepUntil` case
|
|
316
|
+
*/
|
|
317
|
+
export class LazySleepUntilStep extends BaseLazyStep {
|
|
318
|
+
private readonly sleepUntil: number;
|
|
319
|
+
stepType: StepType = "SleepUntil";
|
|
320
|
+
allowUndefinedOut = true;
|
|
321
|
+
public readonly supportsDeferredSubmission = true;
|
|
322
|
+
|
|
323
|
+
constructor(context: WorkflowContext, stepName: string, sleepUntil: number) {
|
|
324
|
+
super(context, stepName);
|
|
325
|
+
this.sleepUntil = sleepUntil;
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
public getPlanStep(concurrent: number, targetStep: number): Step<undefined> {
|
|
329
|
+
return {
|
|
330
|
+
stepId: 0,
|
|
331
|
+
stepName: this.stepName,
|
|
332
|
+
stepType: this.stepType,
|
|
333
|
+
sleepUntil: this.sleepUntil,
|
|
334
|
+
concurrent,
|
|
335
|
+
targetStep,
|
|
336
|
+
};
|
|
337
|
+
}
|
|
338
|
+
|
|
339
|
+
public async getResultStep(concurrent: number, stepId: number): Promise<Step> {
|
|
340
|
+
return await Promise.resolve({
|
|
341
|
+
stepId,
|
|
342
|
+
stepName: this.stepName,
|
|
343
|
+
stepType: this.stepType,
|
|
344
|
+
sleepUntil: this.sleepUntil,
|
|
345
|
+
concurrent,
|
|
346
|
+
});
|
|
347
|
+
}
|
|
348
|
+
|
|
349
|
+
protected safeParseOut() {
|
|
350
|
+
return undefined;
|
|
351
|
+
}
|
|
352
|
+
|
|
353
|
+
async submitStep({ context, body, headers, isParallel }: SubmitStepParams) {
|
|
354
|
+
return (await context.qstashClient.batch([
|
|
355
|
+
{
|
|
356
|
+
body,
|
|
357
|
+
headers,
|
|
358
|
+
method: "POST",
|
|
359
|
+
url: context.url,
|
|
360
|
+
notBefore: isParallel ? undefined : this.sleepUntil,
|
|
361
|
+
},
|
|
362
|
+
])) as { messageId: string }[];
|
|
363
|
+
}
|
|
364
|
+
}
|
|
365
|
+
|
|
366
|
+
export class LazyCallStep<TResult = unknown> extends BaseLazyStep<CallResponse<TResult>> {
|
|
367
|
+
private readonly url: string;
|
|
368
|
+
private readonly method: HTTPMethods;
|
|
369
|
+
private readonly body?: string;
|
|
370
|
+
public readonly headers: Record<string, string>;
|
|
371
|
+
public readonly retries: number;
|
|
372
|
+
public readonly retryDelay?: string;
|
|
373
|
+
public readonly timeout?: number | Duration;
|
|
374
|
+
public readonly flowControl?: FlowControl;
|
|
375
|
+
|
|
376
|
+
stepType: StepType = "Call";
|
|
377
|
+
allowUndefinedOut = false;
|
|
378
|
+
|
|
379
|
+
constructor(
|
|
380
|
+
params: {
|
|
381
|
+
context: WorkflowContext;
|
|
382
|
+
stepName: string;
|
|
383
|
+
} & CallSettings
|
|
384
|
+
) {
|
|
385
|
+
super(params.context, params.stepName);
|
|
386
|
+
this.url = params.url;
|
|
387
|
+
this.method = params.method ?? "GET";
|
|
388
|
+
this.body = params.body;
|
|
389
|
+
this.headers = params.headers ?? {};
|
|
390
|
+
this.retries = params.retries ?? 0;
|
|
391
|
+
this.retryDelay = params.retryDelay;
|
|
392
|
+
this.timeout = params.timeout;
|
|
393
|
+
this.flowControl = params.flowControl;
|
|
394
|
+
}
|
|
395
|
+
|
|
396
|
+
public getPlanStep(concurrent: number, targetStep: number): Step<undefined> {
|
|
397
|
+
return {
|
|
398
|
+
stepId: 0,
|
|
399
|
+
stepName: this.stepName,
|
|
400
|
+
stepType: this.stepType,
|
|
401
|
+
concurrent,
|
|
402
|
+
targetStep,
|
|
403
|
+
};
|
|
404
|
+
}
|
|
405
|
+
|
|
406
|
+
public async getResultStep(
|
|
407
|
+
concurrent: number,
|
|
408
|
+
stepId: number
|
|
409
|
+
): Promise<Step<CallResponse<TResult>>> {
|
|
410
|
+
return await Promise.resolve({
|
|
411
|
+
stepId,
|
|
412
|
+
stepName: this.stepName,
|
|
413
|
+
stepType: this.stepType,
|
|
414
|
+
concurrent,
|
|
415
|
+
callUrl: this.url,
|
|
416
|
+
callMethod: this.method,
|
|
417
|
+
callBody: this.body,
|
|
418
|
+
callHeaders: this.headers,
|
|
419
|
+
});
|
|
420
|
+
}
|
|
421
|
+
|
|
422
|
+
protected safeParseOut(out: string): CallResponse<TResult> {
|
|
423
|
+
const { header, status, body } = JSON.parse(out) as {
|
|
424
|
+
header: Record<string, string[]>;
|
|
425
|
+
status: number;
|
|
426
|
+
body: unknown;
|
|
427
|
+
};
|
|
428
|
+
|
|
429
|
+
const responseHeaders = new Headers(header);
|
|
430
|
+
if (LazyCallStep.isText(responseHeaders.get("content-type"))) {
|
|
431
|
+
const bytes = new Uint8Array(out.length);
|
|
432
|
+
for (let i = 0; i < out.length; i++) {
|
|
433
|
+
bytes[i] = out.charCodeAt(i);
|
|
434
|
+
}
|
|
435
|
+
|
|
436
|
+
const processedResult = new TextDecoder().decode(bytes);
|
|
437
|
+
const newBody = JSON.parse(processedResult).body;
|
|
438
|
+
|
|
439
|
+
return {
|
|
440
|
+
status,
|
|
441
|
+
header,
|
|
442
|
+
body: BaseLazyStep.tryParsing(newBody) as TResult,
|
|
443
|
+
};
|
|
444
|
+
} else {
|
|
445
|
+
return { header, status, body: body as TResult };
|
|
446
|
+
}
|
|
447
|
+
}
|
|
448
|
+
|
|
449
|
+
private static applicationContentTypes = [
|
|
450
|
+
"application/json",
|
|
451
|
+
"application/xml",
|
|
452
|
+
"application/javascript",
|
|
453
|
+
"application/x-www-form-urlencoded",
|
|
454
|
+
"application/xhtml+xml",
|
|
455
|
+
"application/ld+json",
|
|
456
|
+
"application/rss+xml",
|
|
457
|
+
"application/atom+xml",
|
|
458
|
+
];
|
|
459
|
+
|
|
460
|
+
private static isText = (contentTypeHeader: string | null) => {
|
|
461
|
+
if (!contentTypeHeader) {
|
|
462
|
+
return false;
|
|
463
|
+
}
|
|
464
|
+
if (LazyCallStep.applicationContentTypes.some((type) => contentTypeHeader.includes(type))) {
|
|
465
|
+
return true;
|
|
466
|
+
}
|
|
467
|
+
if (contentTypeHeader.startsWith("text/")) {
|
|
468
|
+
return true;
|
|
469
|
+
}
|
|
470
|
+
return false;
|
|
471
|
+
};
|
|
472
|
+
|
|
473
|
+
public getBody({ step }: GetBodyParams): string {
|
|
474
|
+
if (!step.callUrl) {
|
|
475
|
+
throw new WorkflowError("Incompatible step received in LazyCallStep.getBody");
|
|
476
|
+
}
|
|
477
|
+
|
|
478
|
+
return JSON.stringify(step.callBody);
|
|
479
|
+
}
|
|
480
|
+
|
|
481
|
+
getHeaders({ context, telemetry, invokeCount, step }: GetHeaderParams): HeadersResponse {
|
|
482
|
+
const { headers, contentType } = super.getHeaders({ context, telemetry, invokeCount, step });
|
|
483
|
+
|
|
484
|
+
headers["Upstash-Retries"] = this.retries.toString();
|
|
485
|
+
|
|
486
|
+
if (this.retryDelay) {
|
|
487
|
+
headers["Upstash-Retry-Delay"] = this.retryDelay;
|
|
488
|
+
}
|
|
489
|
+
|
|
490
|
+
// WF_DetectTrigger is not included because these requests are going to external endpoints
|
|
491
|
+
headers[WORKFLOW_FEATURE_HEADER] = "WF_NoDelete,InitialBody";
|
|
492
|
+
|
|
493
|
+
if (this.flowControl) {
|
|
494
|
+
const { flowControlKey, flowControlValue } = prepareFlowControl(this.flowControl);
|
|
495
|
+
|
|
496
|
+
headers["Upstash-Flow-Control-Key"] = flowControlKey;
|
|
497
|
+
headers["Upstash-Flow-Control-Value"] = flowControlValue;
|
|
498
|
+
}
|
|
499
|
+
|
|
500
|
+
if (this.timeout) {
|
|
501
|
+
headers["Upstash-Timeout"] = this.timeout.toString();
|
|
502
|
+
}
|
|
503
|
+
|
|
504
|
+
const forwardedHeaders = Object.fromEntries(
|
|
505
|
+
Object.entries(this.headers).map(([header, value]) => [`Upstash-Forward-${header}`, value])
|
|
506
|
+
);
|
|
507
|
+
|
|
508
|
+
return {
|
|
509
|
+
headers: {
|
|
510
|
+
...headers,
|
|
511
|
+
...forwardedHeaders,
|
|
512
|
+
|
|
513
|
+
"Upstash-Callback": context.url,
|
|
514
|
+
"Upstash-Callback-Workflow-RunId": context.workflowRunId,
|
|
515
|
+
"Upstash-Callback-Workflow-CallType": "fromCallback",
|
|
516
|
+
"Upstash-Callback-Workflow-Init": "false",
|
|
517
|
+
"Upstash-Callback-Workflow-Url": context.url,
|
|
518
|
+
"Upstash-Callback-Feature-Set": "LazyFetch,InitialBody,WF_DetectTrigger,WF_TriggerOnConfig",
|
|
519
|
+
|
|
520
|
+
"Upstash-Callback-Forward-Upstash-Workflow-Callback": "true",
|
|
521
|
+
"Upstash-Callback-Forward-Upstash-Workflow-StepId": step.stepId.toString(),
|
|
522
|
+
"Upstash-Callback-Forward-Upstash-Workflow-StepName": this.stepName,
|
|
523
|
+
"Upstash-Callback-Forward-Upstash-Workflow-StepType": this.stepType,
|
|
524
|
+
"Upstash-Callback-Forward-Upstash-Workflow-Concurrent": step.concurrent.toString(),
|
|
525
|
+
"Upstash-Callback-Forward-Upstash-Workflow-ContentType": contentType,
|
|
526
|
+
"Upstash-Workflow-CallType": "toCallback",
|
|
527
|
+
},
|
|
528
|
+
contentType,
|
|
529
|
+
};
|
|
530
|
+
}
|
|
531
|
+
|
|
532
|
+
async submitStep({ context, headers }: SubmitStepParams) {
|
|
533
|
+
return (await context.qstashClient.batch([
|
|
534
|
+
{
|
|
535
|
+
headers,
|
|
536
|
+
body: this.body,
|
|
537
|
+
method: this.method,
|
|
538
|
+
url: this.url,
|
|
539
|
+
retries: DEFAULT_RETRIES === this.retries ? undefined : this.retries,
|
|
540
|
+
retryDelay: this.retryDelay,
|
|
541
|
+
flowControl: this.flowControl,
|
|
542
|
+
},
|
|
543
|
+
])) as { messageId: string }[];
|
|
544
|
+
}
|
|
545
|
+
}
|
|
546
|
+
|
|
547
|
+
export abstract class LazyWaitEventStep<TResult> extends BaseLazyStep<TResult> {
|
|
548
|
+
private readonly eventId: string;
|
|
549
|
+
private readonly timeout: string;
|
|
550
|
+
protected allowUndefinedOut = false;
|
|
551
|
+
|
|
552
|
+
constructor(
|
|
553
|
+
context: WorkflowContext,
|
|
554
|
+
stepName: string,
|
|
555
|
+
eventId: string,
|
|
556
|
+
timeout: string // TODO: string format and accept number as smth
|
|
557
|
+
) {
|
|
558
|
+
super(context, stepName);
|
|
559
|
+
this.eventId = eventId;
|
|
560
|
+
this.timeout = timeout;
|
|
561
|
+
}
|
|
562
|
+
|
|
563
|
+
public getPlanStep(concurrent: number, targetStep: number): Step<undefined> {
|
|
564
|
+
return {
|
|
565
|
+
stepId: 0,
|
|
566
|
+
stepName: this.stepName,
|
|
567
|
+
stepType: this.stepType,
|
|
568
|
+
waitEventId: this.eventId,
|
|
569
|
+
timeout: this.timeout,
|
|
570
|
+
concurrent,
|
|
571
|
+
targetStep,
|
|
572
|
+
};
|
|
573
|
+
}
|
|
574
|
+
|
|
575
|
+
public async getResultStep(concurrent: number, stepId: number): Promise<Step<TResult>> {
|
|
576
|
+
return await Promise.resolve({
|
|
577
|
+
stepId,
|
|
578
|
+
stepName: this.stepName,
|
|
579
|
+
stepType: this.stepType,
|
|
580
|
+
waitEventId: this.eventId,
|
|
581
|
+
timeout: this.timeout,
|
|
582
|
+
concurrent,
|
|
583
|
+
});
|
|
584
|
+
}
|
|
585
|
+
|
|
586
|
+
protected abstract safeParseOut(out: string, step: Step): TResult;
|
|
587
|
+
|
|
588
|
+
public getHeaders({ context, telemetry, invokeCount, step }: GetHeaderParams): HeadersResponse {
|
|
589
|
+
const headers = super.getHeaders({ context, telemetry, invokeCount, step });
|
|
590
|
+
headers.headers["Upstash-Workflow-CallType"] = "step";
|
|
591
|
+
return headers;
|
|
592
|
+
}
|
|
593
|
+
|
|
594
|
+
public getBody({ context, step, headers, telemetry }: GetBodyParams): string {
|
|
595
|
+
if (!step.waitEventId) {
|
|
596
|
+
throw new WorkflowError("Incompatible step received in LazyWaitForEventStep.getBody");
|
|
597
|
+
}
|
|
598
|
+
|
|
599
|
+
const timeoutHeaders = {
|
|
600
|
+
// to include user headers:
|
|
601
|
+
...Object.fromEntries(Object.entries(headers).map(([header, value]) => [header, [value]])),
|
|
602
|
+
// to include telemetry headers:
|
|
603
|
+
...(telemetry
|
|
604
|
+
? Object.fromEntries(
|
|
605
|
+
Object.entries(getTelemetryHeaders(telemetry)).map(([header, value]) => [
|
|
606
|
+
header,
|
|
607
|
+
[value],
|
|
608
|
+
])
|
|
609
|
+
)
|
|
610
|
+
: {}),
|
|
611
|
+
|
|
612
|
+
// note: using WORKFLOW_ID_HEADER doesn't work, because Runid -> RunId:
|
|
613
|
+
"Upstash-Workflow-Runid": [context.workflowRunId],
|
|
614
|
+
// same for WORKFLOW_CREATED_AT_HEADER, because Createdat -> CreatedAt:
|
|
615
|
+
...(context.workflowRunCreatedAt
|
|
616
|
+
? { "Upstash-Workflow-Createdat": [context.workflowRunCreatedAt.toString()] }
|
|
617
|
+
: {}),
|
|
618
|
+
[WORKFLOW_INIT_HEADER]: ["false"],
|
|
619
|
+
[WORKFLOW_URL_HEADER]: [context.url],
|
|
620
|
+
"Upstash-Workflow-CallType": ["step"],
|
|
621
|
+
};
|
|
622
|
+
|
|
623
|
+
const waitBody: WaitRequest = {
|
|
624
|
+
url: context.url,
|
|
625
|
+
timeout: step.timeout,
|
|
626
|
+
timeoutBody: undefined,
|
|
627
|
+
timeoutUrl: context.url,
|
|
628
|
+
timeoutHeaders,
|
|
629
|
+
step: {
|
|
630
|
+
stepId: step.stepId,
|
|
631
|
+
stepType: this.stepType,
|
|
632
|
+
stepName: step.stepName,
|
|
633
|
+
concurrent: step.concurrent,
|
|
634
|
+
targetStep: step.targetStep,
|
|
635
|
+
},
|
|
636
|
+
};
|
|
637
|
+
|
|
638
|
+
return JSON.stringify(waitBody);
|
|
639
|
+
}
|
|
640
|
+
|
|
641
|
+
async submitStep({ context, body, headers }: SubmitStepParams) {
|
|
642
|
+
const result = (await context.qstashClient.http.request({
|
|
643
|
+
path: ["v2", "wait", this.eventId],
|
|
644
|
+
body: body,
|
|
645
|
+
headers,
|
|
646
|
+
method: "POST",
|
|
647
|
+
parseResponseAsJson: false,
|
|
648
|
+
})) as { messageId: string };
|
|
649
|
+
return [result];
|
|
650
|
+
}
|
|
651
|
+
}
|
|
652
|
+
|
|
653
|
+
export class LazyNotifyStep extends LazyFunctionStep<NotifyStepResponse> {
|
|
654
|
+
stepType: StepType = "Notify";
|
|
655
|
+
|
|
656
|
+
constructor(
|
|
657
|
+
context: WorkflowContext,
|
|
658
|
+
stepName: string,
|
|
659
|
+
eventId: string,
|
|
660
|
+
eventData: unknown,
|
|
661
|
+
requester: Client["http"],
|
|
662
|
+
workflowRunId?: string
|
|
663
|
+
) {
|
|
664
|
+
super(context, stepName, async () => {
|
|
665
|
+
const notifyResponse = await makeNotifyRequest(requester, eventId, eventData, workflowRunId);
|
|
666
|
+
|
|
667
|
+
return {
|
|
668
|
+
eventId,
|
|
669
|
+
eventData,
|
|
670
|
+
notifyResponse,
|
|
671
|
+
};
|
|
672
|
+
});
|
|
673
|
+
}
|
|
674
|
+
|
|
675
|
+
protected safeParseOut(out: string): NotifyStepResponse {
|
|
676
|
+
const result = JSON.parse(out) as NotifyStepResponse;
|
|
677
|
+
return {
|
|
678
|
+
...result,
|
|
679
|
+
eventData: BaseLazyStep.tryParsing(result.eventData),
|
|
680
|
+
};
|
|
681
|
+
}
|
|
682
|
+
}
|
|
683
|
+
|
|
684
|
+
export class LazyInvokeStep<TResult = unknown, TBody = unknown> extends BaseLazyStep<
|
|
685
|
+
InvokeStepResponse<TResult>
|
|
686
|
+
> {
|
|
687
|
+
stepType: StepType = "Invoke";
|
|
688
|
+
params: LazyInvokeStepParams<TBody, TResult>;
|
|
689
|
+
protected allowUndefinedOut = false;
|
|
690
|
+
/**
|
|
691
|
+
* workflow id of the invoked workflow
|
|
692
|
+
*/
|
|
693
|
+
private workflowId: string;
|
|
694
|
+
|
|
695
|
+
constructor(
|
|
696
|
+
context: WorkflowContext,
|
|
697
|
+
stepName: string,
|
|
698
|
+
params: LazyInvokeStepParams<TBody, TResult>
|
|
699
|
+
) {
|
|
700
|
+
super(context, stepName);
|
|
701
|
+
this.params = params;
|
|
702
|
+
|
|
703
|
+
const { workflowId } = params.workflow;
|
|
704
|
+
if (!workflowId) {
|
|
705
|
+
throw new WorkflowError("You can only invoke workflow which has a workflowId");
|
|
706
|
+
}
|
|
707
|
+
this.workflowId = workflowId;
|
|
708
|
+
}
|
|
709
|
+
|
|
710
|
+
public getPlanStep(concurrent: number, targetStep: number): Step<undefined> {
|
|
711
|
+
return {
|
|
712
|
+
stepId: 0,
|
|
713
|
+
stepName: this.stepName,
|
|
714
|
+
stepType: this.stepType,
|
|
715
|
+
concurrent,
|
|
716
|
+
targetStep,
|
|
717
|
+
};
|
|
718
|
+
}
|
|
719
|
+
|
|
720
|
+
/**
|
|
721
|
+
* won't be used as it's the server who will add the result step
|
|
722
|
+
* in Invoke step.
|
|
723
|
+
*/
|
|
724
|
+
public getResultStep(
|
|
725
|
+
concurrent: number,
|
|
726
|
+
stepId: number
|
|
727
|
+
): Promise<Step<InvokeStepResponse<TResult>>> {
|
|
728
|
+
return Promise.resolve({
|
|
729
|
+
stepId,
|
|
730
|
+
stepName: this.stepName,
|
|
731
|
+
stepType: this.stepType,
|
|
732
|
+
concurrent,
|
|
733
|
+
});
|
|
734
|
+
}
|
|
735
|
+
|
|
736
|
+
protected safeParseOut(out: string): InvokeStepResponse<TResult> {
|
|
737
|
+
const result = JSON.parse(out) as InvokeStepResponse<TResult>;
|
|
738
|
+
return {
|
|
739
|
+
...result,
|
|
740
|
+
body: BaseLazyStep.tryParsing(result.body),
|
|
741
|
+
};
|
|
742
|
+
}
|
|
743
|
+
|
|
744
|
+
public getBody({ context, step, telemetry, invokeCount }: GetBodyParams): string {
|
|
745
|
+
const { headers: invokerHeaders } = getHeaders({
|
|
746
|
+
initHeaderValue: "false",
|
|
747
|
+
workflowConfig: {
|
|
748
|
+
workflowRunId: context.workflowRunId,
|
|
749
|
+
workflowUrl: context.url,
|
|
750
|
+
workflowRunCreatedAt: context.workflowRunCreatedAt,
|
|
751
|
+
telemetry,
|
|
752
|
+
useJSONContent: false,
|
|
753
|
+
},
|
|
754
|
+
invokeCount,
|
|
755
|
+
});
|
|
756
|
+
|
|
757
|
+
context.qstashClient.http.headers?.forEach((value, key) => {
|
|
758
|
+
invokerHeaders[key] = value;
|
|
759
|
+
});
|
|
760
|
+
|
|
761
|
+
invokerHeaders["Upstash-Workflow-Runid"] = context.workflowRunId;
|
|
762
|
+
// QStash reads these headers as they are, without canonicalizing the keys, so the
|
|
763
|
+
// creation time is only found under its canonical key (Createdat, not CreatedAt).
|
|
764
|
+
// Without it, the invoke result of a run is deduplicated against the invoke result
|
|
765
|
+
// of a previous run with the same id, and the run never continues.
|
|
766
|
+
if (context.workflowRunCreatedAt) {
|
|
767
|
+
invokerHeaders["Upstash-Workflow-Createdat"] = context.workflowRunCreatedAt.toString();
|
|
768
|
+
}
|
|
769
|
+
|
|
770
|
+
const request: InvokeWorkflowRequest = {
|
|
771
|
+
body: stringifyBody(this.params.body),
|
|
772
|
+
headers: Object.fromEntries(
|
|
773
|
+
Object.entries(invokerHeaders).map((pairs) => [pairs[0], [pairs[1]]])
|
|
774
|
+
),
|
|
775
|
+
workflowRunId: context.workflowRunId,
|
|
776
|
+
workflowRunCreatedAt: context.workflowRunCreatedAt,
|
|
777
|
+
workflowUrl: context.url,
|
|
778
|
+
step,
|
|
779
|
+
};
|
|
780
|
+
|
|
781
|
+
return JSON.stringify(request);
|
|
782
|
+
}
|
|
783
|
+
|
|
784
|
+
getHeaders({ context, telemetry, invokeCount }: GetHeaderParams): HeadersResponse {
|
|
785
|
+
const {
|
|
786
|
+
workflow,
|
|
787
|
+
headers = {},
|
|
788
|
+
workflowRunId,
|
|
789
|
+
retries,
|
|
790
|
+
retryDelay,
|
|
791
|
+
flowControl,
|
|
792
|
+
label,
|
|
793
|
+
} = this.params;
|
|
794
|
+
const newUrl = context.url.replace(/[^/]+$/, this.workflowId);
|
|
795
|
+
|
|
796
|
+
const { headers: triggerHeaders, contentType } = getHeaders({
|
|
797
|
+
initHeaderValue: "true",
|
|
798
|
+
workflowConfig: {
|
|
799
|
+
workflowRunId: getWorkflowRunId(workflowRunId),
|
|
800
|
+
workflowUrl: newUrl,
|
|
801
|
+
// the invoked run doesn't exist yet, so the creation time of the invoker run is sent.
|
|
802
|
+
// it separates the invocations made by different runs of the invoker.
|
|
803
|
+
workflowRunCreatedAt: context.workflowRunCreatedAt,
|
|
804
|
+
retries,
|
|
805
|
+
retryDelay,
|
|
806
|
+
telemetry,
|
|
807
|
+
failureUrl: newUrl,
|
|
808
|
+
flowControl,
|
|
809
|
+
useJSONContent: workflow.useJSONContent ?? false,
|
|
810
|
+
},
|
|
811
|
+
invokeCount: invokeCount + 1,
|
|
812
|
+
userHeaders: new Headers(headers) as Headers,
|
|
813
|
+
});
|
|
814
|
+
triggerHeaders["Upstash-Workflow-Invoke"] = "true";
|
|
815
|
+
|
|
816
|
+
if (label) {
|
|
817
|
+
const labelHeader = serializeLabel(label);
|
|
818
|
+
triggerHeaders[WORKFLOW_LABEL_HEADER] = labelHeader;
|
|
819
|
+
triggerHeaders[`upstash-forward-${WORKFLOW_LABEL_HEADER}`] = labelHeader;
|
|
820
|
+
}
|
|
821
|
+
|
|
822
|
+
return { headers: triggerHeaders, contentType };
|
|
823
|
+
}
|
|
824
|
+
|
|
825
|
+
async submitStep({ context, body, headers }: SubmitStepParams) {
|
|
826
|
+
const newUrl = context.url.replace(/[^/]+$/, this.workflowId);
|
|
827
|
+
const result = (await context.qstashClient.publish({
|
|
828
|
+
headers,
|
|
829
|
+
method: "POST",
|
|
830
|
+
body,
|
|
831
|
+
url: newUrl,
|
|
832
|
+
})) as { messageId: string };
|
|
833
|
+
return [result];
|
|
834
|
+
}
|
|
835
|
+
}
|
|
836
|
+
|
|
837
|
+
export type Webhook = {
|
|
838
|
+
webhookUrl: string;
|
|
839
|
+
eventId: string;
|
|
840
|
+
};
|
|
841
|
+
|
|
842
|
+
export class LazyCreateWebhookStep extends BaseLazyStep<Webhook> {
|
|
843
|
+
stepType: StepType = "CreateWebhook";
|
|
844
|
+
protected allowUndefinedOut = false;
|
|
845
|
+
|
|
846
|
+
public getPlanStep(concurrent: number, targetStep: number): Step<undefined> {
|
|
847
|
+
return {
|
|
848
|
+
stepId: 0,
|
|
849
|
+
stepName: this.stepName,
|
|
850
|
+
stepType: this.stepType,
|
|
851
|
+
concurrent,
|
|
852
|
+
targetStep,
|
|
853
|
+
};
|
|
854
|
+
}
|
|
855
|
+
|
|
856
|
+
public async getResultStep(concurrent: number, stepId: number): Promise<Step<Webhook>> {
|
|
857
|
+
return {
|
|
858
|
+
stepId,
|
|
859
|
+
stepName: this.stepName,
|
|
860
|
+
stepType: this.stepType,
|
|
861
|
+
out: undefined,
|
|
862
|
+
concurrent,
|
|
863
|
+
};
|
|
864
|
+
}
|
|
865
|
+
|
|
866
|
+
getBody({ step, context }: GetBodyParams): string {
|
|
867
|
+
const userId = getUserIdFromToken(context.qstashClient);
|
|
868
|
+
const workflowRunId = context.workflowRunId;
|
|
869
|
+
const eventId = getEventId();
|
|
870
|
+
const qstashUrl = getQStashUrl(this.context.qstashClient);
|
|
871
|
+
|
|
872
|
+
return JSON.stringify({
|
|
873
|
+
...step,
|
|
874
|
+
out: JSON.stringify({
|
|
875
|
+
webhookUrl: `${qstashUrl}/v2/workflows/hooks/${userId}/${workflowRunId}/${eventId}`,
|
|
876
|
+
eventId,
|
|
877
|
+
}),
|
|
878
|
+
});
|
|
879
|
+
}
|
|
880
|
+
}
|
|
881
|
+
|
|
882
|
+
export type WaitForWebhookResponse =
|
|
883
|
+
| {
|
|
884
|
+
timeout: false;
|
|
885
|
+
request: Request;
|
|
886
|
+
}
|
|
887
|
+
| {
|
|
888
|
+
timeout: true;
|
|
889
|
+
request: undefined;
|
|
890
|
+
};
|
|
891
|
+
|
|
892
|
+
type WaitForWebhookOut = {
|
|
893
|
+
method: HTTPMethods;
|
|
894
|
+
header: Record<string, string[]>;
|
|
895
|
+
body: string | undefined;
|
|
896
|
+
proto: string;
|
|
897
|
+
host: string;
|
|
898
|
+
url: string;
|
|
899
|
+
};
|
|
900
|
+
|
|
901
|
+
export class LazyWaitForWebhookStep extends LazyWaitEventStep<WaitForWebhookResponse> {
|
|
902
|
+
stepType: StepType = "WaitForWebhook";
|
|
903
|
+
protected allowUndefinedOut = true;
|
|
904
|
+
|
|
905
|
+
constructor(context: WorkflowContext, stepName: string, webhook: Webhook, timeout: Duration) {
|
|
906
|
+
super(context, stepName, webhook.eventId, timeout);
|
|
907
|
+
}
|
|
908
|
+
|
|
909
|
+
protected safeParseOut(out: string): WaitForWebhookResponse {
|
|
910
|
+
const eventData = decodeBase64(out);
|
|
911
|
+
const parsedEventData = BaseLazyStep.tryParsing(eventData) as WaitForWebhookOut;
|
|
912
|
+
|
|
913
|
+
const body = parsedEventData.body;
|
|
914
|
+
const parsedBody = typeof body === "string" ? decodeBase64(body) : undefined;
|
|
915
|
+
|
|
916
|
+
const methodUpper = parsedEventData.method.toUpperCase();
|
|
917
|
+
const canHaveBody = methodUpper !== "GET" && methodUpper !== "HEAD";
|
|
918
|
+
|
|
919
|
+
const request = new Request(
|
|
920
|
+
`${parsedEventData.proto}://${parsedEventData.host}${parsedEventData.url}`,
|
|
921
|
+
{
|
|
922
|
+
method: parsedEventData.method,
|
|
923
|
+
headers: parsedEventData.header,
|
|
924
|
+
body: canHaveBody ? parsedBody : undefined,
|
|
925
|
+
}
|
|
926
|
+
);
|
|
927
|
+
|
|
928
|
+
return {
|
|
929
|
+
request,
|
|
930
|
+
timeout: false,
|
|
931
|
+
};
|
|
932
|
+
}
|
|
933
|
+
|
|
934
|
+
protected handleUndefinedOut(): WaitForWebhookResponse {
|
|
935
|
+
return {
|
|
936
|
+
timeout: true,
|
|
937
|
+
request: undefined,
|
|
938
|
+
};
|
|
939
|
+
}
|
|
940
|
+
}
|
|
941
|
+
|
|
942
|
+
export class LazyWaitForEventStep<TEventData> extends LazyWaitEventStep<
|
|
943
|
+
WaitStepResponse<TEventData>
|
|
944
|
+
> {
|
|
945
|
+
stepType: StepType = "Wait";
|
|
946
|
+
protected allowUndefinedOut = true;
|
|
947
|
+
|
|
948
|
+
private parseWaitForEventOut(
|
|
949
|
+
out: string | undefined,
|
|
950
|
+
waitTimeout: boolean | undefined
|
|
951
|
+
): WaitStepResponse<TEventData> {
|
|
952
|
+
return {
|
|
953
|
+
eventData: out ? BaseLazyStep.tryParsing(decodeBase64(out)) : undefined,
|
|
954
|
+
timeout: waitTimeout ?? false,
|
|
955
|
+
};
|
|
956
|
+
}
|
|
957
|
+
|
|
958
|
+
protected safeParseOut(out: string, step: Step): WaitStepResponse<TEventData> {
|
|
959
|
+
return this.parseWaitForEventOut(out, step.waitTimeout);
|
|
960
|
+
}
|
|
961
|
+
|
|
962
|
+
protected handleUndefinedOut(step: Step): WaitStepResponse<TEventData> {
|
|
963
|
+
return this.parseWaitForEventOut(undefined, step.waitTimeout);
|
|
964
|
+
}
|
|
965
|
+
}
|