@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/h3.js CHANGED
@@ -463,22 +463,21 @@ function getWorkflowRunId(id) {
463
463
  return `wfr_${id ?? nanoid()}`;
464
464
  }
465
465
  function decodeBase64(base64) {
466
+ const binString = atob(base64);
466
467
  try {
467
- const binString = atob(base64);
468
468
  const intArray = Uint8Array.from(binString, (m) => m.codePointAt(0));
469
469
  return new TextDecoder().decode(intArray);
470
470
  } catch (error) {
471
471
  console.warn(
472
472
  `Upstash Qstash: Failed while decoding base64 "${base64}". Decoding with atob and returning it instead. ${error}`
473
473
  );
474
- return atob(base64);
474
+ return binString;
475
475
  }
476
476
  }
477
477
 
478
478
  // src/context/steps.ts
479
- var BaseLazyStep = class {
479
+ var BaseLazyStep = class _BaseLazyStep {
480
480
  stepName;
481
- // will be set in the subclasses
482
481
  constructor(stepName) {
483
482
  if (!stepName) {
484
483
  throw new WorkflowError(
@@ -487,10 +486,58 @@ var BaseLazyStep = class {
487
486
  }
488
487
  this.stepName = stepName;
489
488
  }
489
+ /**
490
+ * parse the out field of a step result.
491
+ *
492
+ * will be called when returning the steps to the context from auto executor
493
+ *
494
+ * @param out field of the step
495
+ * @returns parsed out field
496
+ */
497
+ parseOut(out) {
498
+ if (out === void 0) {
499
+ if (this.allowUndefinedOut) {
500
+ return void 0;
501
+ } else {
502
+ throw new WorkflowError(
503
+ `Error while parsing output of ${this.stepType} step. Expected a string, but got: undefined`
504
+ );
505
+ }
506
+ }
507
+ if (typeof out === "object") {
508
+ if (this.stepType !== "Wait") {
509
+ console.warn(
510
+ `Error while parsing ${this.stepType} step output. Expected a string, but got object. Please reach out to Upstash Support.`
511
+ );
512
+ return out;
513
+ }
514
+ return {
515
+ ...out,
516
+ eventData: _BaseLazyStep.tryParsing(out.eventData)
517
+ };
518
+ }
519
+ if (typeof out !== "string") {
520
+ throw new WorkflowError(
521
+ `Error while parsing output of ${this.stepType} step. Expected a string or undefined, but got: ${typeof out}`
522
+ );
523
+ }
524
+ return this.safeParseOut(out);
525
+ }
526
+ safeParseOut(out) {
527
+ return _BaseLazyStep.tryParsing(out);
528
+ }
529
+ static tryParsing(stepOut) {
530
+ try {
531
+ return JSON.parse(stepOut);
532
+ } catch {
533
+ return stepOut;
534
+ }
535
+ }
490
536
  };
491
537
  var LazyFunctionStep = class extends BaseLazyStep {
492
538
  stepFunction;
493
539
  stepType = "Run";
540
+ allowUndefinedOut = true;
494
541
  constructor(stepName, stepFunction) {
495
542
  super(stepName);
496
543
  this.stepFunction = stepFunction;
@@ -521,6 +568,7 @@ var LazyFunctionStep = class extends BaseLazyStep {
521
568
  var LazySleepStep = class extends BaseLazyStep {
522
569
  sleep;
523
570
  stepType = "SleepFor";
571
+ allowUndefinedOut = true;
524
572
  constructor(stepName, sleep) {
525
573
  super(stepName);
526
574
  this.sleep = sleep;
@@ -548,6 +596,7 @@ var LazySleepStep = class extends BaseLazyStep {
548
596
  var LazySleepUntilStep = class extends BaseLazyStep {
549
597
  sleepUntil;
550
598
  stepType = "SleepUntil";
599
+ allowUndefinedOut = true;
551
600
  constructor(stepName, sleepUntil) {
552
601
  super(stepName);
553
602
  this.sleepUntil = sleepUntil;
@@ -571,8 +620,11 @@ var LazySleepUntilStep = class extends BaseLazyStep {
571
620
  concurrent
572
621
  });
573
622
  }
623
+ safeParseOut() {
624
+ return void 0;
625
+ }
574
626
  };
575
- var LazyCallStep = class extends BaseLazyStep {
627
+ var LazyCallStep = class _LazyCallStep extends BaseLazyStep {
576
628
  url;
577
629
  method;
578
630
  body;
@@ -581,6 +633,7 @@ var LazyCallStep = class extends BaseLazyStep {
581
633
  timeout;
582
634
  flowControl;
583
635
  stepType = "Call";
636
+ allowUndefinedOut = false;
584
637
  constructor(stepName, url, method, body, headers, retries, timeout, flowControl) {
585
638
  super(stepName);
586
639
  this.url = url;
@@ -612,11 +665,53 @@ var LazyCallStep = class extends BaseLazyStep {
612
665
  callHeaders: this.headers
613
666
  });
614
667
  }
668
+ safeParseOut(out) {
669
+ const { header, status, body } = JSON.parse(out);
670
+ const responseHeaders = new Headers(header);
671
+ if (_LazyCallStep.isText(responseHeaders.get("content-type"))) {
672
+ const bytes = new Uint8Array(out.length);
673
+ for (let i = 0; i < out.length; i++) {
674
+ bytes[i] = out.charCodeAt(i);
675
+ }
676
+ const processedResult = new TextDecoder().decode(bytes);
677
+ const newBody = JSON.parse(processedResult).body;
678
+ return {
679
+ status,
680
+ header,
681
+ body: BaseLazyStep.tryParsing(newBody)
682
+ };
683
+ } else {
684
+ return { header, status, body };
685
+ }
686
+ }
687
+ static applicationHeaders = /* @__PURE__ */ new Set([
688
+ "application/json",
689
+ "application/xml",
690
+ "application/javascript",
691
+ "application/x-www-form-urlencoded",
692
+ "application/xhtml+xml",
693
+ "application/ld+json",
694
+ "application/rss+xml",
695
+ "application/atom+xml"
696
+ ]);
697
+ static isText = (contentTypeHeader) => {
698
+ if (!contentTypeHeader) {
699
+ return false;
700
+ }
701
+ if (_LazyCallStep.applicationHeaders.has(contentTypeHeader)) {
702
+ return true;
703
+ }
704
+ if (contentTypeHeader.startsWith("text/")) {
705
+ return true;
706
+ }
707
+ return false;
708
+ };
615
709
  };
616
710
  var LazyWaitForEventStep = class extends BaseLazyStep {
617
711
  eventId;
618
712
  timeout;
619
713
  stepType = "Wait";
714
+ allowUndefinedOut = false;
620
715
  constructor(stepName, eventId, timeout) {
621
716
  super(stepName);
622
717
  this.eventId = eventId;
@@ -643,6 +738,13 @@ var LazyWaitForEventStep = class extends BaseLazyStep {
643
738
  concurrent
644
739
  });
645
740
  }
741
+ safeParseOut(out) {
742
+ const result = JSON.parse(out);
743
+ return {
744
+ ...result,
745
+ eventData: BaseLazyStep.tryParsing(result.eventData)
746
+ };
747
+ }
646
748
  };
647
749
  var LazyNotifyStep = class extends LazyFunctionStep {
648
750
  stepType = "Notify";
@@ -656,10 +758,18 @@ var LazyNotifyStep = class extends LazyFunctionStep {
656
758
  };
657
759
  });
658
760
  }
761
+ safeParseOut(out) {
762
+ const result = JSON.parse(out);
763
+ return {
764
+ ...result,
765
+ eventData: BaseLazyStep.tryParsing(result.eventData)
766
+ };
767
+ }
659
768
  };
660
769
  var LazyInvokeStep = class extends BaseLazyStep {
661
770
  stepType = "Invoke";
662
771
  params;
772
+ allowUndefinedOut = false;
663
773
  constructor(stepName, {
664
774
  workflow,
665
775
  body,
@@ -699,6 +809,13 @@ var LazyInvokeStep = class extends BaseLazyStep {
699
809
  concurrent
700
810
  });
701
811
  }
812
+ safeParseOut(out) {
813
+ const result = JSON.parse(out);
814
+ return {
815
+ ...result,
816
+ body: BaseLazyStep.tryParsing(result.body)
817
+ };
818
+ }
702
819
  };
703
820
 
704
821
  // node_modules/neverthrow/dist/index.es.js
@@ -1384,7 +1501,8 @@ var getHeaders = ({
1384
1501
  flowControl,
1385
1502
  callFlowControl
1386
1503
  }) => {
1387
- const contentType = (userHeaders ? userHeaders.get("Content-Type") : void 0) ?? DEFAULT_CONTENT_TYPE;
1504
+ const callHeaders = new Headers(step?.callHeaders);
1505
+ const contentType = (callHeaders.get("content-type") ? callHeaders.get("content-type") : userHeaders?.get("Content-Type") ? userHeaders.get("Content-Type") : void 0) ?? DEFAULT_CONTENT_TYPE;
1388
1506
  const baseHeaders = {
1389
1507
  [WORKFLOW_INIT_HEADER]: initHeaderValue,
1390
1508
  [WORKFLOW_ID_HEADER]: workflowRunId,
@@ -1795,7 +1913,7 @@ var AutoExecutor = class _AutoExecutor {
1795
1913
  step,
1796
1914
  stepCount: this.stepCount
1797
1915
  });
1798
- return step.out;
1916
+ return lazyStep.parseOut(step.out);
1799
1917
  }
1800
1918
  const resultStep = await lazyStep.getResultStep(NO_CONCURRENCY, this.stepCount);
1801
1919
  await this.debug?.log("INFO", "RUN_SINGLE", {
@@ -1870,7 +1988,9 @@ var AutoExecutor = class _AutoExecutor {
1870
1988
  case "last": {
1871
1989
  const parallelResultSteps = sortedSteps.filter((step) => step.stepId >= initialStepCount).slice(0, parallelSteps.length);
1872
1990
  validateParallelSteps(parallelSteps, parallelResultSteps);
1873
- return parallelResultSteps.map((step) => step.out);
1991
+ return parallelResultSteps.map(
1992
+ (step, index) => parallelSteps[index].parseOut(step.out)
1993
+ );
1874
1994
  }
1875
1995
  }
1876
1996
  const fillValue = void 0;
@@ -1973,7 +2093,7 @@ var AutoExecutor = class _AutoExecutor {
1973
2093
  });
1974
2094
  throw new WorkflowAbort(invokeStep.stepName, invokeStep);
1975
2095
  }
1976
- const result = await this.context.qstashClient.batchJSON(
2096
+ const result = await this.context.qstashClient.batch(
1977
2097
  steps.map((singleStep, index) => {
1978
2098
  const lazyStep = lazySteps[index];
1979
2099
  const { headers } = getHeaders({
@@ -2003,7 +2123,7 @@ var AutoExecutor = class _AutoExecutor {
2003
2123
  {
2004
2124
  headers,
2005
2125
  method: singleStep.callMethod,
2006
- body: singleStep.callBody,
2126
+ body: JSON.stringify(singleStep.callBody),
2007
2127
  url: singleStep.callUrl
2008
2128
  }
2009
2129
  ) : (
@@ -2013,7 +2133,7 @@ var AutoExecutor = class _AutoExecutor {
2013
2133
  {
2014
2134
  headers,
2015
2135
  method: "POST",
2016
- body: singleStep,
2136
+ body: JSON.stringify(singleStep),
2017
2137
  url: this.context.url,
2018
2138
  notBefore: willWait ? singleStep.sleepUntil : void 0,
2019
2139
  delay: willWait ? singleStep.sleepFor : void 0
@@ -2021,8 +2141,9 @@ var AutoExecutor = class _AutoExecutor {
2021
2141
  );
2022
2142
  })
2023
2143
  );
2144
+ const _result = result;
2024
2145
  await this.debug?.log("INFO", "SUBMIT_STEP", {
2025
- messageIds: result.map((message) => {
2146
+ messageIds: _result.map((message) => {
2026
2147
  return {
2027
2148
  message: message.messageId
2028
2149
  };
@@ -2711,7 +2832,7 @@ var WorkflowContext = class {
2711
2832
  */
2712
2833
  async run(stepName, stepFunction) {
2713
2834
  const wrappedStepFunction = () => this.executor.wrapStep(stepName, stepFunction);
2714
- return this.addStep(new LazyFunctionStep(stepName, wrappedStepFunction));
2835
+ return await this.addStep(new LazyFunctionStep(stepName, wrappedStepFunction));
2715
2836
  }
2716
2837
  /**
2717
2838
  * Stops the execution for the duration provided.
@@ -2782,43 +2903,27 @@ var WorkflowContext = class {
2782
2903
  * }
2783
2904
  */
2784
2905
  async call(stepName, settings) {
2785
- const { url, method = "GET", body, headers = {}, retries = 0, timeout, flowControl } = settings;
2786
- const result = await this.addStep(
2906
+ const {
2907
+ url,
2908
+ method = "GET",
2909
+ body: requestBody,
2910
+ headers = {},
2911
+ retries = 0,
2912
+ timeout,
2913
+ flowControl
2914
+ } = settings;
2915
+ return await this.addStep(
2787
2916
  new LazyCallStep(
2788
2917
  stepName,
2789
2918
  url,
2790
2919
  method,
2791
- body,
2920
+ requestBody,
2792
2921
  headers,
2793
2922
  retries,
2794
2923
  timeout,
2795
2924
  flowControl
2796
2925
  )
2797
2926
  );
2798
- if (typeof result === "string") {
2799
- try {
2800
- const body2 = JSON.parse(result);
2801
- return {
2802
- status: 200,
2803
- header: {},
2804
- body: body2
2805
- };
2806
- } catch {
2807
- return {
2808
- status: 200,
2809
- header: {},
2810
- body: result
2811
- };
2812
- }
2813
- }
2814
- try {
2815
- return {
2816
- ...result,
2817
- body: JSON.parse(result.body)
2818
- };
2819
- } catch {
2820
- return result;
2821
- }
2822
2927
  }
2823
2928
  /**
2824
2929
  * Pauses workflow execution until a specific event occurs or a timeout is reached.
@@ -2857,15 +2962,7 @@ var WorkflowContext = class {
2857
2962
  async waitForEvent(stepName, eventId, options = {}) {
2858
2963
  const { timeout = "7d" } = options;
2859
2964
  const timeoutStr = typeof timeout === "string" ? timeout : `${timeout}s`;
2860
- const result = await this.addStep(new LazyWaitForEventStep(stepName, eventId, timeoutStr));
2861
- try {
2862
- return {
2863
- ...result,
2864
- eventData: JSON.parse(result.eventData)
2865
- };
2866
- } catch {
2867
- return result;
2868
- }
2965
+ return await this.addStep(new LazyWaitForEventStep(stepName, eventId, timeoutStr));
2869
2966
  }
2870
2967
  /**
2871
2968
  * Notify workflow runs waiting for an event
@@ -2889,24 +2986,12 @@ var WorkflowContext = class {
2889
2986
  * @returns notify response which has event id, event data and list of waiters which were notified
2890
2987
  */
2891
2988
  async notify(stepName, eventId, eventData) {
2892
- const result = await this.addStep(
2989
+ return await this.addStep(
2893
2990
  new LazyNotifyStep(stepName, eventId, eventData, this.qstashClient.http)
2894
2991
  );
2895
- try {
2896
- return {
2897
- ...result,
2898
- eventData: JSON.parse(result.eventData)
2899
- };
2900
- } catch {
2901
- return result;
2902
- }
2903
2992
  }
2904
2993
  async invoke(stepName, settings) {
2905
- const result = await this.addStep(new LazyInvokeStep(stepName, settings));
2906
- return {
2907
- ...result,
2908
- body: result.body ? JSON.parse(result.body) : void 0
2909
- };
2994
+ return await this.addStep(new LazyInvokeStep(stepName, settings));
2910
2995
  }
2911
2996
  /**
2912
2997
  * Cancel the current workflow run
@@ -3065,10 +3150,6 @@ var processRawSteps = (rawSteps) => {
3065
3150
  const stepsToDecode = encodedSteps.filter((step) => step.callType === "step");
3066
3151
  const otherSteps = stepsToDecode.map((rawStep) => {
3067
3152
  const step = JSON.parse(decodeBase64(rawStep.body));
3068
- try {
3069
- step.out = JSON.parse(step.out);
3070
- } catch {
3071
- }
3072
3153
  if (step.waitEventId) {
3073
3154
  const newOut = {
3074
3155
  eventData: step.out ? decodeBase64(step.out) : void 0,
package/h3.mjs CHANGED
@@ -2,7 +2,7 @@ import {
2
2
  SDK_TELEMETRY,
3
3
  serveBase,
4
4
  serveManyBase
5
- } from "./chunk-GFNR743S.mjs";
5
+ } from "./chunk-WQAJ2RSZ.mjs";
6
6
 
7
7
  // node_modules/defu/dist/defu.mjs
8
8
  function isPlainObject(value) {
package/hono.d.mts CHANGED
@@ -1,7 +1,7 @@
1
1
  import { Context } from 'hono';
2
- import { R as RouteFunction, k as PublicServeOptions, t as InvokableWorkflow } from './types-CYhDXnf8.mjs';
2
+ import { R as RouteFunction, k as PublicServeOptions, t as InvokableWorkflow } from './types-DS9q8FyV.mjs';
3
3
  import { Variables } from 'hono/types';
4
- import { s as serveManyBase } from './serve-many-BVDpPsF-.mjs';
4
+ import { s as serveManyBase } from './serve-many-Fuovl7gl.mjs';
5
5
  import '@upstash/qstash';
6
6
  import 'zod';
7
7
  import 'ai';
package/hono.d.ts CHANGED
@@ -1,7 +1,7 @@
1
1
  import { Context } from 'hono';
2
- import { R as RouteFunction, k as PublicServeOptions, t as InvokableWorkflow } from './types-CYhDXnf8.js';
2
+ import { R as RouteFunction, k as PublicServeOptions, t as InvokableWorkflow } from './types-DS9q8FyV.js';
3
3
  import { Variables } from 'hono/types';
4
- import { s as serveManyBase } from './serve-many-e4zufyXN.js';
4
+ import { s as serveManyBase } from './serve-many-DNnLsDIp.js';
5
5
  import '@upstash/qstash';
6
6
  import 'zod';
7
7
  import 'ai';