@upstash/workflow 0.2.9 → 0.2.10-unicode-rc

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
@@ -1795,7 +1912,7 @@ var AutoExecutor = class _AutoExecutor {
1795
1912
  step,
1796
1913
  stepCount: this.stepCount
1797
1914
  });
1798
- return step.out;
1915
+ return lazyStep.parseOut(step.out);
1799
1916
  }
1800
1917
  const resultStep = await lazyStep.getResultStep(NO_CONCURRENCY, this.stepCount);
1801
1918
  await this.debug?.log("INFO", "RUN_SINGLE", {
@@ -1870,7 +1987,9 @@ var AutoExecutor = class _AutoExecutor {
1870
1987
  case "last": {
1871
1988
  const parallelResultSteps = sortedSteps.filter((step) => step.stepId >= initialStepCount).slice(0, parallelSteps.length);
1872
1989
  validateParallelSteps(parallelSteps, parallelResultSteps);
1873
- return parallelResultSteps.map((step) => step.out);
1990
+ return parallelResultSteps.map(
1991
+ (step, index) => parallelSteps[index].parseOut(step.out)
1992
+ );
1874
1993
  }
1875
1994
  }
1876
1995
  const fillValue = void 0;
@@ -2711,7 +2830,7 @@ var WorkflowContext = class {
2711
2830
  */
2712
2831
  async run(stepName, stepFunction) {
2713
2832
  const wrappedStepFunction = () => this.executor.wrapStep(stepName, stepFunction);
2714
- return this.addStep(new LazyFunctionStep(stepName, wrappedStepFunction));
2833
+ return await this.addStep(new LazyFunctionStep(stepName, wrappedStepFunction));
2715
2834
  }
2716
2835
  /**
2717
2836
  * Stops the execution for the duration provided.
@@ -2782,43 +2901,27 @@ var WorkflowContext = class {
2782
2901
  * }
2783
2902
  */
2784
2903
  async call(stepName, settings) {
2785
- const { url, method = "GET", body, headers = {}, retries = 0, timeout, flowControl } = settings;
2786
- const result = await this.addStep(
2904
+ const {
2905
+ url,
2906
+ method = "GET",
2907
+ body: requestBody,
2908
+ headers = {},
2909
+ retries = 0,
2910
+ timeout,
2911
+ flowControl
2912
+ } = settings;
2913
+ return await this.addStep(
2787
2914
  new LazyCallStep(
2788
2915
  stepName,
2789
2916
  url,
2790
2917
  method,
2791
- body,
2918
+ requestBody,
2792
2919
  headers,
2793
2920
  retries,
2794
2921
  timeout,
2795
2922
  flowControl
2796
2923
  )
2797
2924
  );
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
2925
  }
2823
2926
  /**
2824
2927
  * Pauses workflow execution until a specific event occurs or a timeout is reached.
@@ -2857,15 +2960,7 @@ var WorkflowContext = class {
2857
2960
  async waitForEvent(stepName, eventId, options = {}) {
2858
2961
  const { timeout = "7d" } = options;
2859
2962
  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
- }
2963
+ return await this.addStep(new LazyWaitForEventStep(stepName, eventId, timeoutStr));
2869
2964
  }
2870
2965
  /**
2871
2966
  * Notify workflow runs waiting for an event
@@ -2889,24 +2984,12 @@ var WorkflowContext = class {
2889
2984
  * @returns notify response which has event id, event data and list of waiters which were notified
2890
2985
  */
2891
2986
  async notify(stepName, eventId, eventData) {
2892
- const result = await this.addStep(
2987
+ return await this.addStep(
2893
2988
  new LazyNotifyStep(stepName, eventId, eventData, this.qstashClient.http)
2894
2989
  );
2895
- try {
2896
- return {
2897
- ...result,
2898
- eventData: JSON.parse(result.eventData)
2899
- };
2900
- } catch {
2901
- return result;
2902
- }
2903
2990
  }
2904
2991
  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
- };
2992
+ return await this.addStep(new LazyInvokeStep(stepName, settings));
2910
2993
  }
2911
2994
  /**
2912
2995
  * Cancel the current workflow run
@@ -3065,10 +3148,6 @@ var processRawSteps = (rawSteps) => {
3065
3148
  const stepsToDecode = encodedSteps.filter((step) => step.callType === "step");
3066
3149
  const otherSteps = stepsToDecode.map((rawStep) => {
3067
3150
  const step = JSON.parse(decodeBase64(rawStep.body));
3068
- try {
3069
- step.out = JSON.parse(step.out);
3070
- } catch {
3071
- }
3072
3151
  if (step.waitEventId) {
3073
3152
  const newOut = {
3074
3153
  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-IPXJZU3K.mjs";
5
+ } from "./chunk-N2WV5VCD.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-Dg_9L83G.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-wMUWrSIP.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-Dg_9L83G.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-jCRazho9.js';
5
5
  import '@upstash/qstash';
6
6
  import 'zod';
7
7
  import 'ai';