@upstash/workflow 0.2.10-unicode-rc → 0.2.10

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,21 +463,22 @@ function getWorkflowRunId(id) {
463
463
  return `wfr_${id ?? nanoid()}`;
464
464
  }
465
465
  function decodeBase64(base64) {
466
- const binString = atob(base64);
467
466
  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 binString;
474
+ return atob(base64);
475
475
  }
476
476
  }
477
477
 
478
478
  // src/context/steps.ts
479
- var BaseLazyStep = class _BaseLazyStep {
479
+ var BaseLazyStep = class {
480
480
  stepName;
481
+ // will be set in the subclasses
481
482
  constructor(stepName) {
482
483
  if (!stepName) {
483
484
  throw new WorkflowError(
@@ -486,58 +487,10 @@ var BaseLazyStep = class _BaseLazyStep {
486
487
  }
487
488
  this.stepName = stepName;
488
489
  }
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
- }
536
490
  };
537
491
  var LazyFunctionStep = class extends BaseLazyStep {
538
492
  stepFunction;
539
493
  stepType = "Run";
540
- allowUndefinedOut = true;
541
494
  constructor(stepName, stepFunction) {
542
495
  super(stepName);
543
496
  this.stepFunction = stepFunction;
@@ -568,7 +521,6 @@ var LazyFunctionStep = class extends BaseLazyStep {
568
521
  var LazySleepStep = class extends BaseLazyStep {
569
522
  sleep;
570
523
  stepType = "SleepFor";
571
- allowUndefinedOut = true;
572
524
  constructor(stepName, sleep) {
573
525
  super(stepName);
574
526
  this.sleep = sleep;
@@ -596,7 +548,6 @@ var LazySleepStep = class extends BaseLazyStep {
596
548
  var LazySleepUntilStep = class extends BaseLazyStep {
597
549
  sleepUntil;
598
550
  stepType = "SleepUntil";
599
- allowUndefinedOut = true;
600
551
  constructor(stepName, sleepUntil) {
601
552
  super(stepName);
602
553
  this.sleepUntil = sleepUntil;
@@ -620,11 +571,8 @@ var LazySleepUntilStep = class extends BaseLazyStep {
620
571
  concurrent
621
572
  });
622
573
  }
623
- safeParseOut() {
624
- return void 0;
625
- }
626
574
  };
627
- var LazyCallStep = class _LazyCallStep extends BaseLazyStep {
575
+ var LazyCallStep = class extends BaseLazyStep {
628
576
  url;
629
577
  method;
630
578
  body;
@@ -633,7 +581,6 @@ var LazyCallStep = class _LazyCallStep extends BaseLazyStep {
633
581
  timeout;
634
582
  flowControl;
635
583
  stepType = "Call";
636
- allowUndefinedOut = false;
637
584
  constructor(stepName, url, method, body, headers, retries, timeout, flowControl) {
638
585
  super(stepName);
639
586
  this.url = url;
@@ -665,53 +612,11 @@ var LazyCallStep = class _LazyCallStep extends BaseLazyStep {
665
612
  callHeaders: this.headers
666
613
  });
667
614
  }
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
- };
709
615
  };
710
616
  var LazyWaitForEventStep = class extends BaseLazyStep {
711
617
  eventId;
712
618
  timeout;
713
619
  stepType = "Wait";
714
- allowUndefinedOut = false;
715
620
  constructor(stepName, eventId, timeout) {
716
621
  super(stepName);
717
622
  this.eventId = eventId;
@@ -738,13 +643,6 @@ var LazyWaitForEventStep = class extends BaseLazyStep {
738
643
  concurrent
739
644
  });
740
645
  }
741
- safeParseOut(out) {
742
- const result = JSON.parse(out);
743
- return {
744
- ...result,
745
- eventData: BaseLazyStep.tryParsing(result.eventData)
746
- };
747
- }
748
646
  };
749
647
  var LazyNotifyStep = class extends LazyFunctionStep {
750
648
  stepType = "Notify";
@@ -758,18 +656,10 @@ var LazyNotifyStep = class extends LazyFunctionStep {
758
656
  };
759
657
  });
760
658
  }
761
- safeParseOut(out) {
762
- const result = JSON.parse(out);
763
- return {
764
- ...result,
765
- eventData: BaseLazyStep.tryParsing(result.eventData)
766
- };
767
- }
768
659
  };
769
660
  var LazyInvokeStep = class extends BaseLazyStep {
770
661
  stepType = "Invoke";
771
662
  params;
772
- allowUndefinedOut = false;
773
663
  constructor(stepName, {
774
664
  workflow,
775
665
  body,
@@ -809,13 +699,6 @@ var LazyInvokeStep = class extends BaseLazyStep {
809
699
  concurrent
810
700
  });
811
701
  }
812
- safeParseOut(out) {
813
- const result = JSON.parse(out);
814
- return {
815
- ...result,
816
- body: BaseLazyStep.tryParsing(result.body)
817
- };
818
- }
819
702
  };
820
703
 
821
704
  // node_modules/neverthrow/dist/index.es.js
@@ -1798,7 +1681,7 @@ var invokeWorkflow = async ({
1798
1681
  headers: Object.fromEntries(
1799
1682
  Object.entries(invokerHeaders).map((pairs) => [pairs[0], [pairs[1]]])
1800
1683
  ),
1801
- workflowRunId,
1684
+ workflowRunId: context.workflowRunId,
1802
1685
  workflowUrl: context.url,
1803
1686
  step: invokeStep
1804
1687
  };
@@ -1912,7 +1795,7 @@ var AutoExecutor = class _AutoExecutor {
1912
1795
  step,
1913
1796
  stepCount: this.stepCount
1914
1797
  });
1915
- return lazyStep.parseOut(step.out);
1798
+ return step.out;
1916
1799
  }
1917
1800
  const resultStep = await lazyStep.getResultStep(NO_CONCURRENCY, this.stepCount);
1918
1801
  await this.debug?.log("INFO", "RUN_SINGLE", {
@@ -1987,9 +1870,7 @@ var AutoExecutor = class _AutoExecutor {
1987
1870
  case "last": {
1988
1871
  const parallelResultSteps = sortedSteps.filter((step) => step.stepId >= initialStepCount).slice(0, parallelSteps.length);
1989
1872
  validateParallelSteps(parallelSteps, parallelResultSteps);
1990
- return parallelResultSteps.map(
1991
- (step, index) => parallelSteps[index].parseOut(step.out)
1992
- );
1873
+ return parallelResultSteps.map((step) => step.out);
1993
1874
  }
1994
1875
  }
1995
1876
  const fillValue = void 0;
@@ -2830,7 +2711,7 @@ var WorkflowContext = class {
2830
2711
  */
2831
2712
  async run(stepName, stepFunction) {
2832
2713
  const wrappedStepFunction = () => this.executor.wrapStep(stepName, stepFunction);
2833
- return await this.addStep(new LazyFunctionStep(stepName, wrappedStepFunction));
2714
+ return this.addStep(new LazyFunctionStep(stepName, wrappedStepFunction));
2834
2715
  }
2835
2716
  /**
2836
2717
  * Stops the execution for the duration provided.
@@ -2901,27 +2782,43 @@ var WorkflowContext = class {
2901
2782
  * }
2902
2783
  */
2903
2784
  async call(stepName, settings) {
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(
2785
+ const { url, method = "GET", body, headers = {}, retries = 0, timeout, flowControl } = settings;
2786
+ const result = await this.addStep(
2914
2787
  new LazyCallStep(
2915
2788
  stepName,
2916
2789
  url,
2917
2790
  method,
2918
- requestBody,
2791
+ body,
2919
2792
  headers,
2920
2793
  retries,
2921
2794
  timeout,
2922
2795
  flowControl
2923
2796
  )
2924
2797
  );
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
+ }
2925
2822
  }
2926
2823
  /**
2927
2824
  * Pauses workflow execution until a specific event occurs or a timeout is reached.
@@ -2960,7 +2857,15 @@ var WorkflowContext = class {
2960
2857
  async waitForEvent(stepName, eventId, options = {}) {
2961
2858
  const { timeout = "7d" } = options;
2962
2859
  const timeoutStr = typeof timeout === "string" ? timeout : `${timeout}s`;
2963
- return await this.addStep(new LazyWaitForEventStep(stepName, eventId, timeoutStr));
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
+ }
2964
2869
  }
2965
2870
  /**
2966
2871
  * Notify workflow runs waiting for an event
@@ -2984,12 +2889,24 @@ var WorkflowContext = class {
2984
2889
  * @returns notify response which has event id, event data and list of waiters which were notified
2985
2890
  */
2986
2891
  async notify(stepName, eventId, eventData) {
2987
- return await this.addStep(
2892
+ const result = await this.addStep(
2988
2893
  new LazyNotifyStep(stepName, eventId, eventData, this.qstashClient.http)
2989
2894
  );
2895
+ try {
2896
+ return {
2897
+ ...result,
2898
+ eventData: JSON.parse(result.eventData)
2899
+ };
2900
+ } catch {
2901
+ return result;
2902
+ }
2990
2903
  }
2991
2904
  async invoke(stepName, settings) {
2992
- return await this.addStep(new LazyInvokeStep(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
+ };
2993
2910
  }
2994
2911
  /**
2995
2912
  * Cancel the current workflow run
@@ -3148,6 +3065,10 @@ var processRawSteps = (rawSteps) => {
3148
3065
  const stepsToDecode = encodedSteps.filter((step) => step.callType === "step");
3149
3066
  const otherSteps = stepsToDecode.map((rawStep) => {
3150
3067
  const step = JSON.parse(decodeBase64(rawStep.body));
3068
+ try {
3069
+ step.out = JSON.parse(step.out);
3070
+ } catch {
3071
+ }
3151
3072
  if (step.waitEventId) {
3152
3073
  const newOut = {
3153
3074
  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-N2WV5VCD.mjs";
5
+ } from "./chunk-GFNR743S.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-Dg_9L83G.mjs';
2
+ import { R as RouteFunction, k as PublicServeOptions, t as InvokableWorkflow } from './types-CYhDXnf8.mjs';
3
3
  import { Variables } from 'hono/types';
4
- import { s as serveManyBase } from './serve-many-wMUWrSIP.mjs';
4
+ import { s as serveManyBase } from './serve-many-BVDpPsF-.mjs';
5
5
  import '@upstash/qstash';
6
6
  import 'zod';
7
7
  import 'ai';
@@ -27,7 +27,7 @@ declare const serve: <TInitialPayload = unknown, TBindings extends WorkflowBindi
27
27
  Bindings: TBindings;
28
28
  Variables: TVariables;
29
29
  }>) => Promise<Response>);
30
- declare const createWorkflow: <TInitialPayload, TResult>(routeFunction: RouteFunction<TInitialPayload, TResult>, options?: PublicServeOptions<TInitialPayload> | undefined) => InvokableWorkflow<TInitialPayload, TResult>;
30
+ declare const createWorkflow: <TInitialPayload = unknown, TResult = unknown, TBindings extends WorkflowBindings = WorkflowBindings, TVariables extends Variables = object>(routeFunction: RouteFunction<TInitialPayload, TResult>, options?: PublicServeOptions<TInitialPayload> | undefined) => InvokableWorkflow<TInitialPayload, TResult>;
31
31
  declare const serveMany: (workflows: Parameters<typeof serveManyBase>[0]["workflows"], options?: Parameters<typeof serveManyBase>[0]["options"]) => (context: Context<{
32
32
  Bindings: WorkflowBindings;
33
33
  Variables: object;
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-Dg_9L83G.js';
2
+ import { R as RouteFunction, k as PublicServeOptions, t as InvokableWorkflow } from './types-CYhDXnf8.js';
3
3
  import { Variables } from 'hono/types';
4
- import { s as serveManyBase } from './serve-many-jCRazho9.js';
4
+ import { s as serveManyBase } from './serve-many-e4zufyXN.js';
5
5
  import '@upstash/qstash';
6
6
  import 'zod';
7
7
  import 'ai';
@@ -27,7 +27,7 @@ declare const serve: <TInitialPayload = unknown, TBindings extends WorkflowBindi
27
27
  Bindings: TBindings;
28
28
  Variables: TVariables;
29
29
  }>) => Promise<Response>);
30
- declare const createWorkflow: <TInitialPayload, TResult>(routeFunction: RouteFunction<TInitialPayload, TResult>, options?: PublicServeOptions<TInitialPayload> | undefined) => InvokableWorkflow<TInitialPayload, TResult>;
30
+ declare const createWorkflow: <TInitialPayload = unknown, TResult = unknown, TBindings extends WorkflowBindings = WorkflowBindings, TVariables extends Variables = object>(routeFunction: RouteFunction<TInitialPayload, TResult>, options?: PublicServeOptions<TInitialPayload> | undefined) => InvokableWorkflow<TInitialPayload, TResult>;
31
31
  declare const serveMany: (workflows: Parameters<typeof serveManyBase>[0]["workflows"], options?: Parameters<typeof serveManyBase>[0]["options"]) => (context: Context<{
32
32
  Bindings: WorkflowBindings;
33
33
  Variables: object;