@upstash/workflow 1.4.0 → 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 +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 +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 +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,88 @@
|
|
|
1
|
+
import { WorkflowClient, WorkflowReceiver } from "../../types";
|
|
2
|
+
|
|
3
|
+
const VALID_REGIONS = ["EU_CENTRAL_1", "US_EAST_1"] as const;
|
|
4
|
+
|
|
5
|
+
export type QStashRegion = (typeof VALID_REGIONS)[number];
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Regional handler containing client and optional receiver
|
|
9
|
+
*/
|
|
10
|
+
export type RegionalHandler = {
|
|
11
|
+
client: WorkflowClient;
|
|
12
|
+
receiver?: WorkflowReceiver;
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* QStash handlers for single or multi-region mode
|
|
17
|
+
*/
|
|
18
|
+
export type QStashHandlers =
|
|
19
|
+
| {
|
|
20
|
+
mode: "single-region";
|
|
21
|
+
handlers: RegionalHandler;
|
|
22
|
+
}
|
|
23
|
+
| {
|
|
24
|
+
mode: "multi-region";
|
|
25
|
+
handlers: Record<QStashRegion, RegionalHandler>;
|
|
26
|
+
defaultRegion: QStashRegion;
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
export const DEFAULT_QSTASH_URL = "https://qstash.upstash.io";
|
|
30
|
+
|
|
31
|
+
export const getRegionFromEnvironment = (
|
|
32
|
+
environment: Record<string, string | undefined>
|
|
33
|
+
): QStashRegion | undefined => {
|
|
34
|
+
const region = environment.QSTASH_REGION as QStashRegion | undefined;
|
|
35
|
+
return normalizeRegionHeader(region);
|
|
36
|
+
};
|
|
37
|
+
|
|
38
|
+
function readEnvironmentVariables<T extends readonly string[]>(
|
|
39
|
+
environmentVariables: T,
|
|
40
|
+
environment: Record<string, string | undefined>,
|
|
41
|
+
region?: QStashRegion
|
|
42
|
+
): Record<T[number], string | undefined> {
|
|
43
|
+
const result: Record<string, string | undefined> = {};
|
|
44
|
+
|
|
45
|
+
for (const variable of environmentVariables) {
|
|
46
|
+
const key = region ? `${region}_${variable}` : variable;
|
|
47
|
+
result[variable] = environment[key];
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
return result as Record<T[number], string | undefined>;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
export function readClientEnvironmentVariables(
|
|
54
|
+
environment: Record<string, string | undefined>,
|
|
55
|
+
region?: QStashRegion
|
|
56
|
+
) {
|
|
57
|
+
return readEnvironmentVariables(["QSTASH_URL", "QSTASH_TOKEN"] as const, environment, region);
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
export function readReceiverEnvironmentVariables(
|
|
61
|
+
environment: Record<string, string | undefined>,
|
|
62
|
+
region?: QStashRegion
|
|
63
|
+
) {
|
|
64
|
+
return readEnvironmentVariables(
|
|
65
|
+
["QSTASH_CURRENT_SIGNING_KEY", "QSTASH_NEXT_SIGNING_KEY"] as const,
|
|
66
|
+
environment,
|
|
67
|
+
region
|
|
68
|
+
);
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
export function normalizeRegionHeader(region: string | undefined): QStashRegion | undefined {
|
|
72
|
+
if (!region) {
|
|
73
|
+
return undefined;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
region = region.replaceAll("-", "_").toUpperCase();
|
|
77
|
+
if (VALID_REGIONS.includes(region as QStashRegion)) {
|
|
78
|
+
return region as QStashRegion;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
console.warn(
|
|
82
|
+
`[Upstash Workflow] Invalid UPSTASH-REGION header value: "${region}". Expected one of: ${VALID_REGIONS.join(
|
|
83
|
+
", "
|
|
84
|
+
)}.`
|
|
85
|
+
);
|
|
86
|
+
|
|
87
|
+
return undefined;
|
|
88
|
+
}
|
|
@@ -0,0 +1,241 @@
|
|
|
1
|
+
import { VERSION, WORKFLOW_PROTOCOL_VERSION, WORKFLOW_PROTOCOL_VERSION_HEADER } from "../constants";
|
|
2
|
+
import type { DetailedFinishCondition, RequiredExceptFields, WorkflowServeOptions } from "../types";
|
|
3
|
+
import { formatWorkflowError, WorkflowError } from "../error";
|
|
4
|
+
import { loggingMiddleware } from "../middleware";
|
|
5
|
+
import { DispatchDebug } from "../middleware/types";
|
|
6
|
+
import { getQStashHandlerOptions } from "./multi-region/handlers";
|
|
7
|
+
import { QStashHandlers } from "./multi-region/utils";
|
|
8
|
+
|
|
9
|
+
export type ResponseData = {
|
|
10
|
+
text: string;
|
|
11
|
+
status: number;
|
|
12
|
+
headers: Record<string, string>;
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Internal options for serveBase that are not exposed to users
|
|
17
|
+
*/
|
|
18
|
+
export type InternalServeOptions<TResponse extends Response = Response> = {
|
|
19
|
+
/**
|
|
20
|
+
* Function to generate a Response from ResponseData
|
|
21
|
+
*/
|
|
22
|
+
generateResponse: (responseData: ResponseData) => TResponse;
|
|
23
|
+
/**
|
|
24
|
+
* Whether the framework should use `content-type: application/json`
|
|
25
|
+
* in `triggerFirstInvocation`.
|
|
26
|
+
*/
|
|
27
|
+
useJSONContent: boolean;
|
|
28
|
+
/**
|
|
29
|
+
* QStash handlers for single or multi-region mode
|
|
30
|
+
*/
|
|
31
|
+
qstashHandlers: QStashHandlers;
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* Creates response data based on workflow run ID and finish condition.
|
|
36
|
+
* This is an internal method that cannot be overwritten by users.
|
|
37
|
+
*
|
|
38
|
+
* @param workflowRunId - The ID of the workflow run
|
|
39
|
+
* @param detailedFinishCondition - The detailed finish condition
|
|
40
|
+
* @returns Response data with text, status, and headers
|
|
41
|
+
*/
|
|
42
|
+
export const createResponseData = (
|
|
43
|
+
workflowRunId: string,
|
|
44
|
+
detailedFinishCondition: DetailedFinishCondition
|
|
45
|
+
): ResponseData => {
|
|
46
|
+
const baseHeaders = {
|
|
47
|
+
[WORKFLOW_PROTOCOL_VERSION_HEADER]: WORKFLOW_PROTOCOL_VERSION,
|
|
48
|
+
"Upstash-workflow-sdk": VERSION,
|
|
49
|
+
};
|
|
50
|
+
|
|
51
|
+
if (detailedFinishCondition?.condition === "auth-fail") {
|
|
52
|
+
return {
|
|
53
|
+
text: JSON.stringify({
|
|
54
|
+
message: AUTH_FAIL_MESSAGE,
|
|
55
|
+
workflowRunId,
|
|
56
|
+
}),
|
|
57
|
+
status: 400,
|
|
58
|
+
headers: baseHeaders,
|
|
59
|
+
};
|
|
60
|
+
} else if (detailedFinishCondition?.condition === "non-retryable-error") {
|
|
61
|
+
return {
|
|
62
|
+
text: JSON.stringify(formatWorkflowError(detailedFinishCondition.result)),
|
|
63
|
+
status: 489,
|
|
64
|
+
headers: {
|
|
65
|
+
...baseHeaders,
|
|
66
|
+
"Upstash-NonRetryable-Error": "true",
|
|
67
|
+
},
|
|
68
|
+
};
|
|
69
|
+
} else if (detailedFinishCondition?.condition === "retry-after-error") {
|
|
70
|
+
return {
|
|
71
|
+
text: JSON.stringify(formatWorkflowError(detailedFinishCondition.result)),
|
|
72
|
+
status: 429,
|
|
73
|
+
headers: {
|
|
74
|
+
...baseHeaders,
|
|
75
|
+
"Retry-After": detailedFinishCondition.result.retryAfter.toString(),
|
|
76
|
+
},
|
|
77
|
+
};
|
|
78
|
+
} else if (detailedFinishCondition?.condition === "failure-callback-executed") {
|
|
79
|
+
return {
|
|
80
|
+
text: JSON.stringify({ result: detailedFinishCondition.result ?? undefined }),
|
|
81
|
+
status: 200,
|
|
82
|
+
headers: baseHeaders,
|
|
83
|
+
};
|
|
84
|
+
} else if (detailedFinishCondition?.condition === "failure-callback-undefined") {
|
|
85
|
+
return {
|
|
86
|
+
text: JSON.stringify({
|
|
87
|
+
workflowRunId,
|
|
88
|
+
finishCondition: detailedFinishCondition.condition,
|
|
89
|
+
}),
|
|
90
|
+
status: 200,
|
|
91
|
+
headers: {
|
|
92
|
+
...baseHeaders,
|
|
93
|
+
"Upstash-Workflow-Failure-Callback-Notfound": "true",
|
|
94
|
+
},
|
|
95
|
+
};
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
return {
|
|
99
|
+
text: JSON.stringify({
|
|
100
|
+
workflowRunId,
|
|
101
|
+
finishCondition: detailedFinishCondition.condition,
|
|
102
|
+
}),
|
|
103
|
+
status: 200,
|
|
104
|
+
headers: baseHeaders,
|
|
105
|
+
};
|
|
106
|
+
};
|
|
107
|
+
|
|
108
|
+
/**
|
|
109
|
+
* Fills the options with default values if they are not provided.
|
|
110
|
+
*
|
|
111
|
+
* Default values for:
|
|
112
|
+
* - qstashClient: QStash client created with QSTASH_URL and QSTASH_TOKEN env vars
|
|
113
|
+
* - initialPayloadParser: calls JSON.parse if initial request body exists.
|
|
114
|
+
* - receiver: a Receiver if the required env vars are set
|
|
115
|
+
* - baseUrl: env variable UPSTASH_WORKFLOW_URL
|
|
116
|
+
*
|
|
117
|
+
* @param options options including the client and initialPayloadParser
|
|
118
|
+
* @returns
|
|
119
|
+
*/
|
|
120
|
+
export const processOptions = <
|
|
121
|
+
TInitialPayload = unknown,
|
|
122
|
+
TResult = unknown,
|
|
123
|
+
TResponse extends Response = Response,
|
|
124
|
+
>(
|
|
125
|
+
options?: WorkflowServeOptions<TInitialPayload, TResult>,
|
|
126
|
+
internalOptions?: Partial<InternalServeOptions<TResponse>>
|
|
127
|
+
): RequiredExceptFields<
|
|
128
|
+
WorkflowServeOptions<TInitialPayload, TResult>,
|
|
129
|
+
"receiver" | "url" | "failureFunction" | "baseUrl" | "schema" | "middlewares" | "verbose"
|
|
130
|
+
> & { internal: InternalServeOptions<TResponse> } => {
|
|
131
|
+
const environment =
|
|
132
|
+
options?.env ?? (typeof process === "undefined" ? ({} as Record<string, string>) : process.env);
|
|
133
|
+
|
|
134
|
+
const {
|
|
135
|
+
qstashHandlers,
|
|
136
|
+
defaultClient: qstashClient,
|
|
137
|
+
defaultReceiver: receiver,
|
|
138
|
+
} = getQStashHandlerOptions({
|
|
139
|
+
environment,
|
|
140
|
+
qstashClientOption: options?.qstashClient,
|
|
141
|
+
receiverConfig:
|
|
142
|
+
options && "receiver" in options
|
|
143
|
+
? options.receiver
|
|
144
|
+
? options.receiver
|
|
145
|
+
: "set-to-undefined"
|
|
146
|
+
: "not-set",
|
|
147
|
+
});
|
|
148
|
+
|
|
149
|
+
return {
|
|
150
|
+
qstashClient,
|
|
151
|
+
initialPayloadParser: (initialRequest: string) => {
|
|
152
|
+
// if there is no payload, simply return undefined
|
|
153
|
+
if (!initialRequest) {
|
|
154
|
+
return undefined as TInitialPayload;
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
// try to parse the payload
|
|
158
|
+
try {
|
|
159
|
+
const parsed = JSON.parse(initialRequest) as TInitialPayload;
|
|
160
|
+
return options?.schema ? options.schema.parse(parsed) : parsed;
|
|
161
|
+
} catch (error) {
|
|
162
|
+
// if you get an error when parsing, return it as it is
|
|
163
|
+
// needed in plain string case.
|
|
164
|
+
if (error instanceof SyntaxError) {
|
|
165
|
+
return initialRequest as TInitialPayload;
|
|
166
|
+
}
|
|
167
|
+
// if not JSON.parse error, throw error
|
|
168
|
+
throw error;
|
|
169
|
+
}
|
|
170
|
+
},
|
|
171
|
+
receiver,
|
|
172
|
+
baseUrl: environment.UPSTASH_WORKFLOW_URL,
|
|
173
|
+
env: environment,
|
|
174
|
+
disableTelemetry: false,
|
|
175
|
+
...options,
|
|
176
|
+
// merge middlewares
|
|
177
|
+
middlewares: [options?.middlewares ?? [], options?.verbose ? [loggingMiddleware] : []].flat(),
|
|
178
|
+
internal: {
|
|
179
|
+
generateResponse:
|
|
180
|
+
internalOptions?.generateResponse ??
|
|
181
|
+
((responseData: ResponseData) => {
|
|
182
|
+
return new Response(responseData.text, {
|
|
183
|
+
status: responseData.status,
|
|
184
|
+
headers: responseData.headers,
|
|
185
|
+
}) as TResponse;
|
|
186
|
+
}),
|
|
187
|
+
useJSONContent: internalOptions?.useJSONContent ?? false,
|
|
188
|
+
qstashHandlers,
|
|
189
|
+
},
|
|
190
|
+
};
|
|
191
|
+
};
|
|
192
|
+
|
|
193
|
+
/**
|
|
194
|
+
* Determines the workflow and failure url based on the passed parameters.
|
|
195
|
+
*
|
|
196
|
+
* throws error if the url doesn't start with http:// or https://.
|
|
197
|
+
*
|
|
198
|
+
* @param request request. used to retrieve the request.url
|
|
199
|
+
* @param url user passed url (which also has the full route)
|
|
200
|
+
* @param baseUrl UPSTASH_WORKFLOW_URL env var or the user passed baseUrl. Used to replace
|
|
201
|
+
* the beginning of the final URLs before returning.
|
|
202
|
+
* @param dispatchDebug debug event dispatcher
|
|
203
|
+
* @returns workflow URL and failure URL
|
|
204
|
+
*/
|
|
205
|
+
export const determineUrls = async (
|
|
206
|
+
request: Request,
|
|
207
|
+
url: string | undefined,
|
|
208
|
+
baseUrl: string | undefined,
|
|
209
|
+
dispatchDebug: DispatchDebug
|
|
210
|
+
) => {
|
|
211
|
+
const initialWorkflowUrl = url ?? request.url;
|
|
212
|
+
const workflowUrl = baseUrl
|
|
213
|
+
? initialWorkflowUrl.replace(/^(https?:\/\/[^/]+)(\/.*)?$/, (_, matchedBaseUrl, path) => {
|
|
214
|
+
return baseUrl + ((path as string) || "");
|
|
215
|
+
})
|
|
216
|
+
: initialWorkflowUrl;
|
|
217
|
+
|
|
218
|
+
if (workflowUrl !== initialWorkflowUrl) {
|
|
219
|
+
await dispatchDebug("onInfo", {
|
|
220
|
+
info: `The workflow URL's base URL has been replaced with the provided baseUrl. Original URL: ${initialWorkflowUrl}, New URL: ${workflowUrl}`,
|
|
221
|
+
});
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
if (workflowUrl.includes("localhost")) {
|
|
225
|
+
await dispatchDebug("onInfo", {
|
|
226
|
+
info: `Workflow URL contains localhost. This can happen in local development, but shouldn't happen in production unless you have a route which contains localhost. Received: ${workflowUrl}`,
|
|
227
|
+
});
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
if (!(workflowUrl.startsWith("http://") || workflowUrl.startsWith("https://"))) {
|
|
231
|
+
throw new WorkflowError(
|
|
232
|
+
`Workflow URL should start with 'http://' or 'https://'. Recevied is '${workflowUrl}'`
|
|
233
|
+
);
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
return {
|
|
237
|
+
workflowUrl,
|
|
238
|
+
};
|
|
239
|
+
};
|
|
240
|
+
|
|
241
|
+
export const AUTH_FAIL_MESSAGE = `Failed to authenticate Workflow request. If this is unexpected, see the caveat https://upstash.com/docs/workflow/basics/caveats#avoid-non-deterministic-code-outside-context-run`;
|
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
2
|
+
import { WORKFLOW_PROTOCOL_VERSION, WORKFLOW_PROTOCOL_VERSION_HEADER } from "../constants";
|
|
3
|
+
import { WorkflowError } from "../error";
|
|
4
|
+
import { InvokableWorkflow, RouteFunction, WorkflowServeOptions } from "../types";
|
|
5
|
+
|
|
6
|
+
export type OmitOptionsInServeMany<TOptions> = Omit<
|
|
7
|
+
TOptions,
|
|
8
|
+
"env" | "url" | "schema" | "initialPayloadParser"
|
|
9
|
+
>;
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Extracts workflow ID from URL.
|
|
13
|
+
*
|
|
14
|
+
* @param url URL to extract workflow ID from
|
|
15
|
+
*/
|
|
16
|
+
const getWorkflowId = (url: string) => {
|
|
17
|
+
const components = url.split("/");
|
|
18
|
+
const lastComponent = components[components.length - 1];
|
|
19
|
+
return lastComponent.split("?")[0];
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
export const serveManyBase = <
|
|
23
|
+
THandler extends (...params: any[]) => any,
|
|
24
|
+
TOptions extends
|
|
25
|
+
OmitOptionsInServeMany<WorkflowServeOptions> = OmitOptionsInServeMany<WorkflowServeOptions>,
|
|
26
|
+
TServeParams extends [routeFunction: RouteFunction<any, any>, options: TOptions] = [
|
|
27
|
+
routeFunction: RouteFunction<any, any>,
|
|
28
|
+
options: TOptions,
|
|
29
|
+
],
|
|
30
|
+
>({
|
|
31
|
+
workflows,
|
|
32
|
+
getUrl,
|
|
33
|
+
serveMethod,
|
|
34
|
+
options,
|
|
35
|
+
}: {
|
|
36
|
+
workflows: Record<string, InvokableWorkflow<any, any>>;
|
|
37
|
+
getUrl: (...params: Parameters<THandler>) => string;
|
|
38
|
+
serveMethod: (...params: TServeParams) => THandler;
|
|
39
|
+
options?: TOptions;
|
|
40
|
+
}) => {
|
|
41
|
+
const workflowIds: (string | undefined)[] = [];
|
|
42
|
+
|
|
43
|
+
const workflowMap: Record<string, THandler> = Object.fromEntries(
|
|
44
|
+
Object.entries(workflows).map((workflow) => {
|
|
45
|
+
const workflowId = workflow[0];
|
|
46
|
+
|
|
47
|
+
if (workflowIds.includes(workflowId)) {
|
|
48
|
+
throw new WorkflowError(
|
|
49
|
+
`Duplicate workflow name found: '${workflowId}'. Please set different workflow names in serveMany.`
|
|
50
|
+
);
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
if (workflowId.includes("/")) {
|
|
54
|
+
throw new WorkflowError(
|
|
55
|
+
`Invalid workflow name found: '${workflowId}'. Workflow name cannot contain '/'.`
|
|
56
|
+
);
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
workflowIds.push(workflowId);
|
|
60
|
+
|
|
61
|
+
workflow[1].workflowId = workflowId;
|
|
62
|
+
workflow[1].options = {
|
|
63
|
+
...options,
|
|
64
|
+
...workflow[1].options,
|
|
65
|
+
};
|
|
66
|
+
|
|
67
|
+
const params = [workflow[1].routeFunction, workflow[1].options] as TServeParams;
|
|
68
|
+
const handler = serveMethod(...params);
|
|
69
|
+
|
|
70
|
+
return [workflowId, handler];
|
|
71
|
+
})
|
|
72
|
+
);
|
|
73
|
+
|
|
74
|
+
return {
|
|
75
|
+
handler: async (...params: Parameters<THandler>) => {
|
|
76
|
+
const url = getUrl(...params);
|
|
77
|
+
const pickedWorkflowId = getWorkflowId(url);
|
|
78
|
+
if (!pickedWorkflowId) {
|
|
79
|
+
return new Response(
|
|
80
|
+
`Unexpected request in serveMany. workflowId not set. Please update the URL of your request.`,
|
|
81
|
+
{
|
|
82
|
+
status: 404,
|
|
83
|
+
headers: {
|
|
84
|
+
[WORKFLOW_PROTOCOL_VERSION_HEADER]: WORKFLOW_PROTOCOL_VERSION,
|
|
85
|
+
},
|
|
86
|
+
}
|
|
87
|
+
);
|
|
88
|
+
}
|
|
89
|
+
const workflow = workflowMap[pickedWorkflowId];
|
|
90
|
+
if (!workflow) {
|
|
91
|
+
return new Response(
|
|
92
|
+
`No workflows in serveMany found for '${pickedWorkflowId}'. Please update the URL of your request.`,
|
|
93
|
+
{
|
|
94
|
+
status: 404,
|
|
95
|
+
headers: {
|
|
96
|
+
[WORKFLOW_PROTOCOL_VERSION_HEADER]: WORKFLOW_PROTOCOL_VERSION,
|
|
97
|
+
},
|
|
98
|
+
}
|
|
99
|
+
);
|
|
100
|
+
}
|
|
101
|
+
return await workflow(...params);
|
|
102
|
+
},
|
|
103
|
+
};
|
|
104
|
+
};
|
|
105
|
+
|
|
106
|
+
/**
|
|
107
|
+
* Creates a new URL by replacing the last path segment with a workflow ID.
|
|
108
|
+
*
|
|
109
|
+
* @param url base URL
|
|
110
|
+
* @param workflowId workflow ID to use in the URL
|
|
111
|
+
*/
|
|
112
|
+
export const getNewUrlFromWorkflowId = (url: string, workflowId?: string) => {
|
|
113
|
+
if (!workflowId) {
|
|
114
|
+
throw new WorkflowError("You can only call workflow which has a workflowId");
|
|
115
|
+
}
|
|
116
|
+
return url.replace(/[^/]+$/, workflowId);
|
|
117
|
+
};
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { WorkflowContext } from "../context";
|
|
2
|
+
import { DisabledWorkflowContext } from "./authorization";
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Checks if a context is a DisabledWorkflowContext.
|
|
6
|
+
*
|
|
7
|
+
* @param context workflow context to check
|
|
8
|
+
*/
|
|
9
|
+
export const isDisabledWorkflowContext = (context: WorkflowContext | DisabledWorkflowContext) => {
|
|
10
|
+
return "disabled" in context;
|
|
11
|
+
};
|