@upstash/workflow 0.2.10 → 0.2.11

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/express.js CHANGED
@@ -23823,22 +23823,21 @@ function getWorkflowRunId(id) {
23823
23823
  return `wfr_${id ?? nanoid()}`;
23824
23824
  }
23825
23825
  function decodeBase64(base64) {
23826
+ const binString = atob(base64);
23826
23827
  try {
23827
- const binString = atob(base64);
23828
23828
  const intArray = Uint8Array.from(binString, (m) => m.codePointAt(0));
23829
23829
  return new TextDecoder().decode(intArray);
23830
23830
  } catch (error) {
23831
23831
  console.warn(
23832
23832
  `Upstash Qstash: Failed while decoding base64 "${base64}". Decoding with atob and returning it instead. ${error}`
23833
23833
  );
23834
- return atob(base64);
23834
+ return binString;
23835
23835
  }
23836
23836
  }
23837
23837
 
23838
23838
  // src/context/steps.ts
23839
- var BaseLazyStep = class {
23839
+ var BaseLazyStep = class _BaseLazyStep {
23840
23840
  stepName;
23841
- // will be set in the subclasses
23842
23841
  constructor(stepName) {
23843
23842
  if (!stepName) {
23844
23843
  throw new WorkflowError(
@@ -23847,10 +23846,58 @@ var BaseLazyStep = class {
23847
23846
  }
23848
23847
  this.stepName = stepName;
23849
23848
  }
23849
+ /**
23850
+ * parse the out field of a step result.
23851
+ *
23852
+ * will be called when returning the steps to the context from auto executor
23853
+ *
23854
+ * @param out field of the step
23855
+ * @returns parsed out field
23856
+ */
23857
+ parseOut(out) {
23858
+ if (out === void 0) {
23859
+ if (this.allowUndefinedOut) {
23860
+ return void 0;
23861
+ } else {
23862
+ throw new WorkflowError(
23863
+ `Error while parsing output of ${this.stepType} step. Expected a string, but got: undefined`
23864
+ );
23865
+ }
23866
+ }
23867
+ if (typeof out === "object") {
23868
+ if (this.stepType !== "Wait") {
23869
+ console.warn(
23870
+ `Error while parsing ${this.stepType} step output. Expected a string, but got object. Please reach out to Upstash Support.`
23871
+ );
23872
+ return out;
23873
+ }
23874
+ return {
23875
+ ...out,
23876
+ eventData: _BaseLazyStep.tryParsing(out.eventData)
23877
+ };
23878
+ }
23879
+ if (typeof out !== "string") {
23880
+ throw new WorkflowError(
23881
+ `Error while parsing output of ${this.stepType} step. Expected a string or undefined, but got: ${typeof out}`
23882
+ );
23883
+ }
23884
+ return this.safeParseOut(out);
23885
+ }
23886
+ safeParseOut(out) {
23887
+ return _BaseLazyStep.tryParsing(out);
23888
+ }
23889
+ static tryParsing(stepOut) {
23890
+ try {
23891
+ return JSON.parse(stepOut);
23892
+ } catch {
23893
+ return stepOut;
23894
+ }
23895
+ }
23850
23896
  };
23851
23897
  var LazyFunctionStep = class extends BaseLazyStep {
23852
23898
  stepFunction;
23853
23899
  stepType = "Run";
23900
+ allowUndefinedOut = true;
23854
23901
  constructor(stepName, stepFunction) {
23855
23902
  super(stepName);
23856
23903
  this.stepFunction = stepFunction;
@@ -23881,6 +23928,7 @@ var LazyFunctionStep = class extends BaseLazyStep {
23881
23928
  var LazySleepStep = class extends BaseLazyStep {
23882
23929
  sleep;
23883
23930
  stepType = "SleepFor";
23931
+ allowUndefinedOut = true;
23884
23932
  constructor(stepName, sleep) {
23885
23933
  super(stepName);
23886
23934
  this.sleep = sleep;
@@ -23908,6 +23956,7 @@ var LazySleepStep = class extends BaseLazyStep {
23908
23956
  var LazySleepUntilStep = class extends BaseLazyStep {
23909
23957
  sleepUntil;
23910
23958
  stepType = "SleepUntil";
23959
+ allowUndefinedOut = true;
23911
23960
  constructor(stepName, sleepUntil) {
23912
23961
  super(stepName);
23913
23962
  this.sleepUntil = sleepUntil;
@@ -23931,8 +23980,11 @@ var LazySleepUntilStep = class extends BaseLazyStep {
23931
23980
  concurrent
23932
23981
  });
23933
23982
  }
23983
+ safeParseOut() {
23984
+ return void 0;
23985
+ }
23934
23986
  };
23935
- var LazyCallStep = class extends BaseLazyStep {
23987
+ var LazyCallStep = class _LazyCallStep extends BaseLazyStep {
23936
23988
  url;
23937
23989
  method;
23938
23990
  body;
@@ -23941,6 +23993,7 @@ var LazyCallStep = class extends BaseLazyStep {
23941
23993
  timeout;
23942
23994
  flowControl;
23943
23995
  stepType = "Call";
23996
+ allowUndefinedOut = false;
23944
23997
  constructor(stepName, url, method, body, headers, retries, timeout, flowControl) {
23945
23998
  super(stepName);
23946
23999
  this.url = url;
@@ -23972,11 +24025,53 @@ var LazyCallStep = class extends BaseLazyStep {
23972
24025
  callHeaders: this.headers
23973
24026
  });
23974
24027
  }
24028
+ safeParseOut(out) {
24029
+ const { header, status, body } = JSON.parse(out);
24030
+ const responseHeaders = new Headers(header);
24031
+ if (_LazyCallStep.isText(responseHeaders.get("content-type"))) {
24032
+ const bytes = new Uint8Array(out.length);
24033
+ for (let i = 0; i < out.length; i++) {
24034
+ bytes[i] = out.charCodeAt(i);
24035
+ }
24036
+ const processedResult = new TextDecoder().decode(bytes);
24037
+ const newBody = JSON.parse(processedResult).body;
24038
+ return {
24039
+ status,
24040
+ header,
24041
+ body: BaseLazyStep.tryParsing(newBody)
24042
+ };
24043
+ } else {
24044
+ return { header, status, body };
24045
+ }
24046
+ }
24047
+ static applicationHeaders = /* @__PURE__ */ new Set([
24048
+ "application/json",
24049
+ "application/xml",
24050
+ "application/javascript",
24051
+ "application/x-www-form-urlencoded",
24052
+ "application/xhtml+xml",
24053
+ "application/ld+json",
24054
+ "application/rss+xml",
24055
+ "application/atom+xml"
24056
+ ]);
24057
+ static isText = (contentTypeHeader) => {
24058
+ if (!contentTypeHeader) {
24059
+ return false;
24060
+ }
24061
+ if (_LazyCallStep.applicationHeaders.has(contentTypeHeader)) {
24062
+ return true;
24063
+ }
24064
+ if (contentTypeHeader.startsWith("text/")) {
24065
+ return true;
24066
+ }
24067
+ return false;
24068
+ };
23975
24069
  };
23976
24070
  var LazyWaitForEventStep = class extends BaseLazyStep {
23977
24071
  eventId;
23978
24072
  timeout;
23979
24073
  stepType = "Wait";
24074
+ allowUndefinedOut = false;
23980
24075
  constructor(stepName, eventId, timeout) {
23981
24076
  super(stepName);
23982
24077
  this.eventId = eventId;
@@ -24003,6 +24098,13 @@ var LazyWaitForEventStep = class extends BaseLazyStep {
24003
24098
  concurrent
24004
24099
  });
24005
24100
  }
24101
+ safeParseOut(out) {
24102
+ const result = JSON.parse(out);
24103
+ return {
24104
+ ...result,
24105
+ eventData: BaseLazyStep.tryParsing(result.eventData)
24106
+ };
24107
+ }
24006
24108
  };
24007
24109
  var LazyNotifyStep = class extends LazyFunctionStep {
24008
24110
  stepType = "Notify";
@@ -24016,10 +24118,18 @@ var LazyNotifyStep = class extends LazyFunctionStep {
24016
24118
  };
24017
24119
  });
24018
24120
  }
24121
+ safeParseOut(out) {
24122
+ const result = JSON.parse(out);
24123
+ return {
24124
+ ...result,
24125
+ eventData: BaseLazyStep.tryParsing(result.eventData)
24126
+ };
24127
+ }
24019
24128
  };
24020
24129
  var LazyInvokeStep = class extends BaseLazyStep {
24021
24130
  stepType = "Invoke";
24022
24131
  params;
24132
+ allowUndefinedOut = false;
24023
24133
  constructor(stepName, {
24024
24134
  workflow,
24025
24135
  body,
@@ -24059,6 +24169,13 @@ var LazyInvokeStep = class extends BaseLazyStep {
24059
24169
  concurrent
24060
24170
  });
24061
24171
  }
24172
+ safeParseOut(out) {
24173
+ const result = JSON.parse(out);
24174
+ return {
24175
+ ...result,
24176
+ body: BaseLazyStep.tryParsing(result.body)
24177
+ };
24178
+ }
24062
24179
  };
24063
24180
 
24064
24181
  // node_modules/neverthrow/dist/index.es.js
@@ -24744,7 +24861,8 @@ var getHeaders = ({
24744
24861
  flowControl,
24745
24862
  callFlowControl
24746
24863
  }) => {
24747
- const contentType = (userHeaders ? userHeaders.get("Content-Type") : void 0) ?? DEFAULT_CONTENT_TYPE;
24864
+ const callHeaders = new Headers(step?.callHeaders);
24865
+ const contentType = (callHeaders.get("content-type") ? callHeaders.get("content-type") : userHeaders?.get("Content-Type") ? userHeaders.get("Content-Type") : void 0) ?? DEFAULT_CONTENT_TYPE;
24748
24866
  const baseHeaders = {
24749
24867
  [WORKFLOW_INIT_HEADER]: initHeaderValue,
24750
24868
  [WORKFLOW_ID_HEADER]: workflowRunId,
@@ -25155,7 +25273,7 @@ var AutoExecutor = class _AutoExecutor {
25155
25273
  step,
25156
25274
  stepCount: this.stepCount
25157
25275
  });
25158
- return step.out;
25276
+ return lazyStep.parseOut(step.out);
25159
25277
  }
25160
25278
  const resultStep = await lazyStep.getResultStep(NO_CONCURRENCY, this.stepCount);
25161
25279
  await this.debug?.log("INFO", "RUN_SINGLE", {
@@ -25230,7 +25348,9 @@ var AutoExecutor = class _AutoExecutor {
25230
25348
  case "last": {
25231
25349
  const parallelResultSteps = sortedSteps.filter((step) => step.stepId >= initialStepCount).slice(0, parallelSteps.length);
25232
25350
  validateParallelSteps(parallelSteps, parallelResultSteps);
25233
- return parallelResultSteps.map((step) => step.out);
25351
+ return parallelResultSteps.map(
25352
+ (step, index) => parallelSteps[index].parseOut(step.out)
25353
+ );
25234
25354
  }
25235
25355
  }
25236
25356
  const fillValue = void 0;
@@ -25333,7 +25453,7 @@ var AutoExecutor = class _AutoExecutor {
25333
25453
  });
25334
25454
  throw new WorkflowAbort(invokeStep.stepName, invokeStep);
25335
25455
  }
25336
- const result = await this.context.qstashClient.batchJSON(
25456
+ const result = await this.context.qstashClient.batch(
25337
25457
  steps.map((singleStep, index) => {
25338
25458
  const lazyStep = lazySteps[index];
25339
25459
  const { headers } = getHeaders({
@@ -25363,7 +25483,7 @@ var AutoExecutor = class _AutoExecutor {
25363
25483
  {
25364
25484
  headers,
25365
25485
  method: singleStep.callMethod,
25366
- body: singleStep.callBody,
25486
+ body: JSON.stringify(singleStep.callBody),
25367
25487
  url: singleStep.callUrl
25368
25488
  }
25369
25489
  ) : (
@@ -25373,7 +25493,7 @@ var AutoExecutor = class _AutoExecutor {
25373
25493
  {
25374
25494
  headers,
25375
25495
  method: "POST",
25376
- body: singleStep,
25496
+ body: JSON.stringify(singleStep),
25377
25497
  url: this.context.url,
25378
25498
  notBefore: willWait ? singleStep.sleepUntil : void 0,
25379
25499
  delay: willWait ? singleStep.sleepFor : void 0
@@ -25381,8 +25501,9 @@ var AutoExecutor = class _AutoExecutor {
25381
25501
  );
25382
25502
  })
25383
25503
  );
25504
+ const _result = result;
25384
25505
  await this.debug?.log("INFO", "SUBMIT_STEP", {
25385
- messageIds: result.map((message) => {
25506
+ messageIds: _result.map((message) => {
25386
25507
  return {
25387
25508
  message: message.messageId
25388
25509
  };
@@ -26071,7 +26192,7 @@ var WorkflowContext = class {
26071
26192
  */
26072
26193
  async run(stepName, stepFunction) {
26073
26194
  const wrappedStepFunction = () => this.executor.wrapStep(stepName, stepFunction);
26074
- return this.addStep(new LazyFunctionStep(stepName, wrappedStepFunction));
26195
+ return await this.addStep(new LazyFunctionStep(stepName, wrappedStepFunction));
26075
26196
  }
26076
26197
  /**
26077
26198
  * Stops the execution for the duration provided.
@@ -26142,43 +26263,27 @@ var WorkflowContext = class {
26142
26263
  * }
26143
26264
  */
26144
26265
  async call(stepName, settings) {
26145
- const { url, method = "GET", body, headers = {}, retries = 0, timeout, flowControl } = settings;
26146
- const result = await this.addStep(
26266
+ const {
26267
+ url,
26268
+ method = "GET",
26269
+ body: requestBody,
26270
+ headers = {},
26271
+ retries = 0,
26272
+ timeout,
26273
+ flowControl
26274
+ } = settings;
26275
+ return await this.addStep(
26147
26276
  new LazyCallStep(
26148
26277
  stepName,
26149
26278
  url,
26150
26279
  method,
26151
- body,
26280
+ requestBody,
26152
26281
  headers,
26153
26282
  retries,
26154
26283
  timeout,
26155
26284
  flowControl
26156
26285
  )
26157
26286
  );
26158
- if (typeof result === "string") {
26159
- try {
26160
- const body2 = JSON.parse(result);
26161
- return {
26162
- status: 200,
26163
- header: {},
26164
- body: body2
26165
- };
26166
- } catch {
26167
- return {
26168
- status: 200,
26169
- header: {},
26170
- body: result
26171
- };
26172
- }
26173
- }
26174
- try {
26175
- return {
26176
- ...result,
26177
- body: JSON.parse(result.body)
26178
- };
26179
- } catch {
26180
- return result;
26181
- }
26182
26287
  }
26183
26288
  /**
26184
26289
  * Pauses workflow execution until a specific event occurs or a timeout is reached.
@@ -26217,15 +26322,7 @@ var WorkflowContext = class {
26217
26322
  async waitForEvent(stepName, eventId, options = {}) {
26218
26323
  const { timeout = "7d" } = options;
26219
26324
  const timeoutStr = typeof timeout === "string" ? timeout : `${timeout}s`;
26220
- const result = await this.addStep(new LazyWaitForEventStep(stepName, eventId, timeoutStr));
26221
- try {
26222
- return {
26223
- ...result,
26224
- eventData: JSON.parse(result.eventData)
26225
- };
26226
- } catch {
26227
- return result;
26228
- }
26325
+ return await this.addStep(new LazyWaitForEventStep(stepName, eventId, timeoutStr));
26229
26326
  }
26230
26327
  /**
26231
26328
  * Notify workflow runs waiting for an event
@@ -26249,24 +26346,12 @@ var WorkflowContext = class {
26249
26346
  * @returns notify response which has event id, event data and list of waiters which were notified
26250
26347
  */
26251
26348
  async notify(stepName, eventId, eventData) {
26252
- const result = await this.addStep(
26349
+ return await this.addStep(
26253
26350
  new LazyNotifyStep(stepName, eventId, eventData, this.qstashClient.http)
26254
26351
  );
26255
- try {
26256
- return {
26257
- ...result,
26258
- eventData: JSON.parse(result.eventData)
26259
- };
26260
- } catch {
26261
- return result;
26262
- }
26263
26352
  }
26264
26353
  async invoke(stepName, settings) {
26265
- const result = await this.addStep(new LazyInvokeStep(stepName, settings));
26266
- return {
26267
- ...result,
26268
- body: result.body ? JSON.parse(result.body) : void 0
26269
- };
26354
+ return await this.addStep(new LazyInvokeStep(stepName, settings));
26270
26355
  }
26271
26356
  /**
26272
26357
  * Cancel the current workflow run
@@ -26425,10 +26510,6 @@ var processRawSteps = (rawSteps) => {
26425
26510
  const stepsToDecode = encodedSteps.filter((step) => step.callType === "step");
26426
26511
  const otherSteps = stepsToDecode.map((rawStep) => {
26427
26512
  const step = JSON.parse(decodeBase64(rawStep.body));
26428
- try {
26429
- step.out = JSON.parse(step.out);
26430
- } catch {
26431
- }
26432
26513
  if (step.waitEventId) {
26433
26514
  const newOut = {
26434
26515
  eventData: step.out ? decodeBase64(step.out) : void 0,
package/express.mjs CHANGED
@@ -5,7 +5,7 @@ import {
5
5
  __toESM,
6
6
  serveBase,
7
7
  serveManyBase
8
- } from "./chunk-GFNR743S.mjs";
8
+ } from "./chunk-WQAJ2RSZ.mjs";
9
9
 
10
10
  // node_modules/depd/index.js
11
11
  var require_depd = __commonJS({
package/h3.d.mts CHANGED
@@ -1,6 +1,6 @@
1
1
  import * as h3 from 'h3';
2
- import { R as RouteFunction, k as PublicServeOptions, t as InvokableWorkflow } from './types-CYhDXnf8.mjs';
3
- import { s as serveManyBase } from './serve-many-BVDpPsF-.mjs';
2
+ import { R as RouteFunction, k as PublicServeOptions, t as InvokableWorkflow } from './types-DS9q8FyV.mjs';
3
+ import { s as serveManyBase } from './serve-many-Fuovl7gl.mjs';
4
4
  import '@upstash/qstash';
5
5
  import 'zod';
6
6
  import 'ai';
package/h3.d.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  import * as h3 from 'h3';
2
- import { R as RouteFunction, k as PublicServeOptions, t as InvokableWorkflow } from './types-CYhDXnf8.js';
3
- import { s as serveManyBase } from './serve-many-e4zufyXN.js';
2
+ import { R as RouteFunction, k as PublicServeOptions, t as InvokableWorkflow } from './types-DS9q8FyV.js';
3
+ import { s as serveManyBase } from './serve-many-DNnLsDIp.js';
4
4
  import '@upstash/qstash';
5
5
  import 'zod';
6
6
  import 'ai';