@upstash/workflow 0.2.0 → 0.2.1

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/svelte.js CHANGED
@@ -118,6 +118,11 @@ var BaseLazyStep = class {
118
118
  stepName;
119
119
  // will be set in the subclasses
120
120
  constructor(stepName) {
121
+ if (!stepName) {
122
+ throw new WorkflowError(
123
+ "A workflow step name cannot be undefined or an empty string. Please provide a name for your workflow step."
124
+ );
125
+ }
121
126
  this.stepName = stepName;
122
127
  }
123
128
  };
@@ -210,15 +215,17 @@ var LazyCallStep = class extends BaseLazyStep {
210
215
  method;
211
216
  body;
212
217
  headers;
213
- stepType = "Call";
214
218
  retries;
215
- constructor(stepName, url, method, body, headers, retries) {
219
+ timeout;
220
+ stepType = "Call";
221
+ constructor(stepName, url, method, body, headers, retries, timeout) {
216
222
  super(stepName);
217
223
  this.url = url;
218
224
  this.method = method;
219
225
  this.body = body;
220
226
  this.headers = headers;
221
227
  this.retries = retries;
228
+ this.timeout = timeout;
222
229
  }
223
230
  getPlanStep(concurrent, targetStep) {
224
231
  return {
@@ -830,7 +837,10 @@ var recreateUserHeaders = (headers) => {
830
837
  const pairs = headers.entries();
831
838
  for (const [header, value] of pairs) {
832
839
  const headerLowerCase = header.toLowerCase();
833
- if (!headerLowerCase.startsWith("upstash-workflow-") && !headerLowerCase.startsWith("x-vercel-") && !headerLowerCase.startsWith("x-forwarded-") && headerLowerCase !== "cf-connecting-ip") {
840
+ if (!headerLowerCase.startsWith("upstash-workflow-") && // https://vercel.com/docs/edge-network/headers/request-headers#x-vercel-id
841
+ !headerLowerCase.startsWith("x-vercel-") && !headerLowerCase.startsWith("x-forwarded-") && // https://blog.cloudflare.com/preventing-request-loops-using-cdn-loop/
842
+ headerLowerCase !== "cf-connecting-ip" && headerLowerCase !== "cdn-loop" && headerLowerCase !== "cf-ew-via" && headerLowerCase !== "cf-ray" && // For Render https://render.com
843
+ headerLowerCase !== "render-proxy-ttl") {
834
844
  filteredHeaders.append(header, value);
835
845
  }
836
846
  }
@@ -933,7 +943,7 @@ ${atob(callbackMessage.body ?? "")}`
933
943
  );
934
944
  }
935
945
  };
936
- var getHeaders = (initHeaderValue, workflowRunId, workflowUrl, userHeaders, step, failureUrl, retries, callRetries) => {
946
+ var getHeaders = (initHeaderValue, workflowRunId, workflowUrl, userHeaders, step, failureUrl, retries, callRetries, callTimeout) => {
937
947
  const baseHeaders = {
938
948
  [WORKFLOW_INIT_HEADER]: initHeaderValue,
939
949
  [WORKFLOW_ID_HEADER]: workflowRunId,
@@ -943,6 +953,9 @@ var getHeaders = (initHeaderValue, workflowRunId, workflowUrl, userHeaders, step
943
953
  if (!step?.callUrl) {
944
954
  baseHeaders[`Upstash-Forward-${WORKFLOW_PROTOCOL_VERSION_HEADER}`] = WORKFLOW_PROTOCOL_VERSION;
945
955
  }
956
+ if (callTimeout) {
957
+ baseHeaders[`Upstash-Timeout`] = callTimeout.toString();
958
+ }
946
959
  if (failureUrl) {
947
960
  baseHeaders[`Upstash-Failure-Callback-Forward-${WORKFLOW_FAILURE_HEADER}`] = "true";
948
961
  if (!step?.callUrl) {
@@ -1318,7 +1331,8 @@ var AutoExecutor = class _AutoExecutor {
1318
1331
  singleStep,
1319
1332
  this.context.failureUrl,
1320
1333
  this.context.retries,
1321
- lazyStep instanceof LazyCallStep ? lazyStep.retries : void 0
1334
+ lazyStep instanceof LazyCallStep ? lazyStep.retries : void 0,
1335
+ lazyStep instanceof LazyCallStep ? lazyStep.timeout : void 0
1322
1336
  );
1323
1337
  const willWait = singleStep.concurrent === NO_CONCURRENCY || singleStep.stepId === 0;
1324
1338
  singleStep.out = JSON.stringify(singleStep.out);
@@ -1668,6 +1682,7 @@ var WorkflowContext = class {
1668
1682
  * @param body call body
1669
1683
  * @param headers call headers
1670
1684
  * @param retries number of call retries. 0 by default
1685
+ * @param timeout max duration to wait for the endpoint to respond. in seconds.
1671
1686
  * @returns call result as {
1672
1687
  * status: number;
1673
1688
  * body: unknown;
@@ -1675,9 +1690,17 @@ var WorkflowContext = class {
1675
1690
  * }
1676
1691
  */
1677
1692
  async call(stepName, settings) {
1678
- const { url, method = "GET", body, headers = {}, retries = 0 } = settings;
1693
+ const { url, method = "GET", body, headers = {}, retries = 0, timeout } = settings;
1679
1694
  const result = await this.addStep(
1680
- new LazyCallStep(stepName, url, method, body, headers, retries)
1695
+ new LazyCallStep(
1696
+ stepName,
1697
+ url,
1698
+ method,
1699
+ body,
1700
+ headers,
1701
+ retries,
1702
+ timeout
1703
+ )
1681
1704
  );
1682
1705
  if (typeof result === "string") {
1683
1706
  try {
@@ -2077,7 +2100,7 @@ var handleFailure = async (request, requestPayload, qstashClient, initialPayload
2077
2100
  const workflowContext = new WorkflowContext({
2078
2101
  qstashClient,
2079
2102
  workflowRunId,
2080
- initialPayload: initialPayloadParser(decodeBase64(sourceBody)),
2103
+ initialPayload: sourceBody ? initialPayloadParser(decodeBase64(sourceBody)) : void 0,
2081
2104
  headers: recreateUserHeaders(new Headers(sourceHeader)),
2082
2105
  steps: [],
2083
2106
  url,
@@ -2119,10 +2142,23 @@ var processOptions = (options) => {
2119
2142
  baseUrl: environment.QSTASH_URL,
2120
2143
  token: environment.QSTASH_TOKEN
2121
2144
  }),
2122
- // eslint-disable-next-line @typescript-eslint/no-unused-vars
2123
- onStepFinish: (workflowRunId, _finishCondition) => new Response(JSON.stringify({ workflowRunId }), {
2124
- status: 200
2125
- }),
2145
+ onStepFinish: (workflowRunId, finishCondition) => {
2146
+ if (finishCondition === "auth-fail") {
2147
+ console.error(AUTH_FAIL_MESSAGE);
2148
+ return new Response(
2149
+ JSON.stringify({
2150
+ message: AUTH_FAIL_MESSAGE,
2151
+ workflowRunId
2152
+ }),
2153
+ {
2154
+ status: 400
2155
+ }
2156
+ );
2157
+ }
2158
+ return new Response(JSON.stringify({ workflowRunId }), {
2159
+ status: 200
2160
+ });
2161
+ },
2126
2162
  initialPayloadParser: (initialRequest) => {
2127
2163
  if (!initialRequest) {
2128
2164
  return void 0;
@@ -2164,6 +2200,7 @@ var determineUrls = async (request, url, baseUrl, failureFunction, failureUrl, d
2164
2200
  workflowFailureUrl
2165
2201
  };
2166
2202
  };
2203
+ var 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`;
2167
2204
 
2168
2205
  // src/serve/index.ts
2169
2206
  var serve = (routeFunction, options) => {
@@ -2240,7 +2277,11 @@ var serve = (routeFunction, options) => {
2240
2277
  await debug?.log("ERROR", "ERROR", { error: authCheck.error.message });
2241
2278
  throw authCheck.error;
2242
2279
  } else if (authCheck.value === "run-ended") {
2243
- return onStepFinish("no-workflow-id", "auth-fail");
2280
+ await debug?.log("ERROR", "ERROR", { error: AUTH_FAIL_MESSAGE });
2281
+ return onStepFinish(
2282
+ isFirstInvocation ? "no-workflow-id" : workflowContext.workflowRunId,
2283
+ "auth-fail"
2284
+ );
2244
2285
  }
2245
2286
  const callReturnCheck = await handleThirdPartyCallResult(
2246
2287
  request,
package/svelte.mjs CHANGED
@@ -1,6 +1,6 @@
1
1
  import {
2
2
  serve
3
- } from "./chunk-5R2BFC3N.mjs";
3
+ } from "./chunk-ADOBNR4O.mjs";
4
4
 
5
5
  // platforms/svelte.ts
6
6
  var serve2 = (routeFunction, options) => {
@@ -378,6 +378,7 @@ declare class WorkflowContext<TInitialPayload = unknown> {
378
378
  * @param body call body
379
379
  * @param headers call headers
380
380
  * @param retries number of call retries. 0 by default
381
+ * @param timeout max duration to wait for the endpoint to respond. in seconds.
381
382
  * @returns call result as {
382
383
  * status: number;
383
384
  * body: unknown;
@@ -390,6 +391,7 @@ declare class WorkflowContext<TInitialPayload = unknown> {
390
391
  body?: unknown;
391
392
  headers?: Record<string, string>;
392
393
  retries?: number;
394
+ timeout?: Duration | number;
393
395
  }): Promise<CallResponse<TResult>>;
394
396
  /**
395
397
  * Pauses workflow execution until a specific event occurs or a timeout is reached.
@@ -378,6 +378,7 @@ declare class WorkflowContext<TInitialPayload = unknown> {
378
378
  * @param body call body
379
379
  * @param headers call headers
380
380
  * @param retries number of call retries. 0 by default
381
+ * @param timeout max duration to wait for the endpoint to respond. in seconds.
381
382
  * @returns call result as {
382
383
  * status: number;
383
384
  * body: unknown;
@@ -390,6 +391,7 @@ declare class WorkflowContext<TInitialPayload = unknown> {
390
391
  body?: unknown;
391
392
  headers?: Record<string, string>;
392
393
  retries?: number;
394
+ timeout?: Duration | number;
393
395
  }): Promise<CallResponse<TResult>>;
394
396
  /**
395
397
  * Pauses workflow execution until a specific event occurs or a timeout is reached.