-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathworkflow-requests.ts
430 lines (401 loc) · 13.3 KB
/
workflow-requests.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
import type { Err, Ok } from "neverthrow";
import { err, ok } from "neverthrow";
import { WorkflowAbort, WorkflowError } from "./error";
import type { WorkflowContext } from "./context";
import {
TELEMETRY_HEADER_FRAMEWORK,
TELEMETRY_HEADER_RUNTIME,
TELEMETRY_HEADER_SDK,
WORKFLOW_ID_HEADER,
WORKFLOW_INVOKE_COUNT_HEADER,
} from "./constants";
import type {
CallResponse,
Step,
StepType,
Telemetry,
WorkflowClient,
WorkflowReceiver,
WorkflowServeOptions,
} from "./types";
import { StepTypes } from "./types";
import type { WorkflowLogger } from "./logger";
import { FlowControl, QstashError } from "@upstash/qstash";
import { getSteps } from "./client/utils";
import { getHeaders } from "./qstash/headers";
export const triggerFirstInvocation = async <TInitialPayload>({
workflowContext,
useJSONContent,
telemetry,
debug,
invokeCount,
}: {
workflowContext: WorkflowContext<TInitialPayload>;
useJSONContent?: boolean;
telemetry?: Telemetry;
debug?: WorkflowLogger;
invokeCount?: number;
}): Promise<Ok<"success" | "workflow-run-already-exists", never> | Err<never, Error>> => {
const { headers } = getHeaders({
initHeaderValue: "true",
workflowConfig: {
workflowRunId: workflowContext.workflowRunId,
workflowUrl: workflowContext.url,
failureUrl: workflowContext.failureUrl,
retries: workflowContext.retries,
telemetry,
flowControl: workflowContext.flowControl,
useJSONContent: useJSONContent ?? false,
},
invokeCount: invokeCount ?? 0,
userHeaders: workflowContext.headers,
});
// QStash doesn't forward content-type when passed in `upstash-forward-content-type`
// so we need to pass it in the headers
if (workflowContext.headers.get("content-type")) {
headers["content-type"] = workflowContext.headers.get("content-type")!;
}
if (useJSONContent) {
headers["content-type"] = "application/json";
}
try {
const body =
typeof workflowContext.requestPayload === "string"
? workflowContext.requestPayload
: JSON.stringify(workflowContext.requestPayload);
const result = await workflowContext.qstashClient.publish({
headers,
method: "POST",
body,
url: workflowContext.url,
});
if (result.deduplicated) {
await debug?.log("WARN", "SUBMIT_FIRST_INVOCATION", {
message: `Workflow run ${workflowContext.workflowRunId} already exists. A new one isn't created.`,
headers,
requestPayload: workflowContext.requestPayload,
url: workflowContext.url,
messageId: result.messageId,
});
return ok("workflow-run-already-exists");
} else {
await debug?.log("SUBMIT", "SUBMIT_FIRST_INVOCATION", {
headers,
requestPayload: workflowContext.requestPayload,
url: workflowContext.url,
messageId: result.messageId,
});
return ok("success");
}
} catch (error) {
const error_ = error as Error;
return err(error_);
}
};
export const triggerRouteFunction = async ({
onCleanup,
onStep,
onCancel,
debug,
}: {
onStep: () => Promise<unknown>;
onCleanup: (result: unknown) => Promise<void>;
onCancel: () => Promise<void>;
debug?: WorkflowLogger;
}): Promise<
Ok<"workflow-finished" | "step-finished" | "workflow-was-finished", never> | Err<never, Error>
> => {
try {
// When onStep completes successfully, it throws an exception named `WorkflowAbort`,
// indicating that the step has been successfully executed.
// This ensures that onCleanup is only called when no exception is thrown.
const result = await onStep();
await onCleanup(result);
return ok("workflow-finished");
} catch (error) {
const error_ = error as Error;
if (error instanceof QstashError && error.status === 400) {
await debug?.log("WARN", "RESPONSE_WORKFLOW", {
message: `tried to append to a cancelled workflow. exiting without publishing.`,
name: error.name,
errorMessage: error.message,
});
return ok("workflow-was-finished");
} else if (!(error_ instanceof WorkflowAbort)) {
return err(error_);
} else if (error_.cancelWorkflow) {
await onCancel();
return ok("workflow-finished");
} else {
return ok("step-finished");
}
}
};
export const triggerWorkflowDelete = async <TInitialPayload>(
workflowContext: WorkflowContext<TInitialPayload>,
result: unknown,
debug?: WorkflowLogger,
cancel = false
): Promise<void> => {
await debug?.log("SUBMIT", "SUBMIT_CLEANUP", {
deletedWorkflowRunId: workflowContext.workflowRunId,
});
await workflowContext.qstashClient.http.request({
path: ["v2", "workflows", "runs", `${workflowContext.workflowRunId}?cancel=${cancel}`],
method: "DELETE",
parseResponseAsJson: false,
body: JSON.stringify(result),
});
await debug?.log(
"SUBMIT",
"SUBMIT_CLEANUP",
`workflow run ${workflowContext.workflowRunId} deleted.`
);
};
/**
* Removes headers starting with `Upstash-Workflow-` from the headers
*
* @param headers incoming headers
* @returns headers with `Upstash-Workflow-` headers removed
*/
export const recreateUserHeaders = (headers: Headers): Headers => {
const filteredHeaders = new Headers();
const pairs = headers.entries() as unknown as [string, string][];
for (const [header, value] of pairs) {
const headerLowerCase = header.toLowerCase();
if (
!headerLowerCase.startsWith("upstash-workflow-") &&
// https://vercel.com/docs/edge-network/headers/request-headers#x-vercel-id
!headerLowerCase.startsWith("x-vercel-") &&
!headerLowerCase.startsWith("x-forwarded-") &&
// https://blog.cloudflare.com/preventing-request-loops-using-cdn-loop/
headerLowerCase !== "cf-connecting-ip" &&
headerLowerCase !== "cdn-loop" &&
headerLowerCase !== "cf-ew-via" &&
headerLowerCase !== "cf-ray" &&
// For Render https://render.com
headerLowerCase !== "render-proxy-ttl"
) {
filteredHeaders.append(header, value);
}
}
return filteredHeaders as Headers;
};
/**
* Checks if the request is from a third party call result. If so,
* calls QStash to add the result to the ongoing workflow.
*
* Otherwise, does nothing.
*
* ### How third party calls work
*
* In third party calls, we publish a message to the third party API.
* the result is then returned back to the workflow endpoint.
*
* Whenever the workflow endpoint receives a request, we first check
* if the incoming request is a third party call result coming from QStash.
* If so, we send back the result to QStash as a result step.
*
* @param request Incoming request
* @param client QStash client
* @returns
*/
export const handleThirdPartyCallResult = async ({
request,
requestPayload,
client,
workflowUrl,
failureUrl,
retries,
telemetry,
flowControl,
debug,
}: {
request: Request;
requestPayload: string;
client: WorkflowClient;
workflowUrl: string;
failureUrl: WorkflowServeOptions["failureUrl"];
retries: number;
telemetry?: Telemetry;
flowControl?: FlowControl;
debug?: WorkflowLogger;
}): Promise<
| Ok<"is-call-return" | "continue-workflow" | "call-will-retry" | "workflow-ended", never>
| Err<never, Error>
> => {
try {
if (request.headers.get("Upstash-Workflow-Callback")) {
let callbackPayload: string;
if (requestPayload) {
callbackPayload = requestPayload;
} else {
const workflowRunId = request.headers.get("upstash-workflow-runid");
const messageId = request.headers.get("upstash-message-id");
if (!workflowRunId)
throw new WorkflowError("workflow run id missing in context.call lazy fetch.");
if (!messageId) throw new WorkflowError("message id missing in context.call lazy fetch.");
const { steps, workflowRunEnded } = await getSteps(
client.http,
workflowRunId,
messageId,
debug
);
if (workflowRunEnded) {
return ok("workflow-ended");
}
const failingStep = steps.find((step) => step.messageId === messageId);
if (!failingStep)
throw new WorkflowError(
"Failed to submit the context.call. " +
(steps.length === 0
? "No steps found."
: `No step was found with matching messageId ${messageId} out of ${steps.length} steps.`)
);
callbackPayload = atob(failingStep.body);
}
const callbackMessage = JSON.parse(callbackPayload) as {
status: number;
body?: string;
retried?: number; // only set after the first try
maxRetries: number;
header: Record<string, string[]>;
};
// eslint-disable-next-line @typescript-eslint/no-magic-numbers
if (
!(callbackMessage.status >= 200 && callbackMessage.status < 300) &&
callbackMessage.maxRetries &&
callbackMessage.retried !== callbackMessage.maxRetries
) {
await debug?.log("WARN", "SUBMIT_THIRD_PARTY_RESULT", {
status: callbackMessage.status,
body: atob(callbackMessage.body ?? ""),
});
// this callback will be retried by the QStash, we just ignore it
console.warn(
`Workflow Warning: "context.call" failed with status ${callbackMessage.status}` +
` and will retry (retried ${callbackMessage.retried ?? 0} out of ${callbackMessage.maxRetries} times).` +
` Error Message:\n${atob(callbackMessage.body ?? "")}`
);
return ok("call-will-retry");
}
const workflowRunId = request.headers.get(WORKFLOW_ID_HEADER);
const stepIdString = request.headers.get("Upstash-Workflow-StepId");
const stepName = request.headers.get("Upstash-Workflow-StepName");
const stepType = request.headers.get("Upstash-Workflow-StepType") as StepType;
const concurrentString = request.headers.get("Upstash-Workflow-Concurrent");
const contentType = request.headers.get("Upstash-Workflow-ContentType");
const invokeCount = request.headers.get(WORKFLOW_INVOKE_COUNT_HEADER);
if (
!(
(
workflowRunId &&
stepIdString &&
stepName &&
StepTypes.includes(stepType) &&
concurrentString &&
contentType
)
// not adding invokeCount to required for backwards compatibility.
)
) {
throw new Error(
`Missing info in callback message source header: ${JSON.stringify({
workflowRunId,
stepIdString,
stepName,
stepType,
concurrentString,
contentType,
})}`
);
}
const userHeaders = recreateUserHeaders(request.headers as Headers);
const { headers: requestHeaders } = getHeaders({
initHeaderValue: "false",
workflowConfig: {
workflowRunId,
workflowUrl,
failureUrl,
retries,
telemetry,
flowControl,
},
userHeaders,
invokeCount: Number(invokeCount),
});
const callResponse: CallResponse = {
status: callbackMessage.status,
body: atob(callbackMessage.body ?? ""),
header: callbackMessage.header,
};
const callResultStep: Step<string> = {
stepId: Number(stepIdString),
stepName,
stepType,
out: JSON.stringify(callResponse),
concurrent: Number(concurrentString),
};
await debug?.log("SUBMIT", "SUBMIT_THIRD_PARTY_RESULT", {
step: callResultStep,
headers: requestHeaders,
url: workflowUrl,
});
const result = await client.publishJSON({
headers: requestHeaders,
method: "POST",
body: callResultStep,
url: workflowUrl,
});
await debug?.log("SUBMIT", "SUBMIT_THIRD_PARTY_RESULT", {
messageId: result.messageId,
});
return ok("is-call-return");
} else {
return ok("continue-workflow");
}
} catch (error) {
const isCallReturn = request.headers.get("Upstash-Workflow-Callback");
return err(
new WorkflowError(`Error when handling call return (isCallReturn=${isCallReturn}): ${error}`)
);
}
};
export type HeadersResponse = {
headers: Record<string, string>;
contentType: string;
};
export const getTelemetryHeaders = (telemetry: Telemetry) => {
return {
[TELEMETRY_HEADER_SDK]: telemetry.sdk,
[TELEMETRY_HEADER_FRAMEWORK]: telemetry.framework,
[TELEMETRY_HEADER_RUNTIME]: telemetry.runtime ?? "unknown",
};
};
export const verifyRequest = async (
body: string,
signature: string | null,
verifier?: WorkflowReceiver
) => {
if (!verifier) {
return;
}
try {
if (!signature) {
throw new Error("`Upstash-Signature` header is not passed.");
}
const isValid = await verifier.verify({
body,
signature,
});
if (!isValid) {
throw new Error("Signature in `Upstash-Signature` header is not valid");
}
} catch (error) {
throw new WorkflowError(
`Failed to verify that the Workflow request comes from QStash: ${error}\n\n` +
"If signature is missing, trigger the workflow endpoint by publishing your request to QStash instead of calling it directly.\n\n" +
"If you want to disable QStash Verification, you should clear env variables QSTASH_CURRENT_SIGNING_KEY and QSTASH_NEXT_SIGNING_KEY"
);
}
};