@upstash/workflow 0.2.10-hono-generics → 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.
@@ -114,15 +114,15 @@ function getWorkflowRunId(id) {
114
114
  return `wfr_${id ?? nanoid()}`;
115
115
  }
116
116
  function decodeBase64(base64) {
117
+ const binString = atob(base64);
117
118
  try {
118
- const binString = atob(base64);
119
119
  const intArray = Uint8Array.from(binString, (m) => m.codePointAt(0));
120
120
  return new TextDecoder().decode(intArray);
121
121
  } catch (error) {
122
122
  console.warn(
123
123
  `Upstash Qstash: Failed while decoding base64 "${base64}". Decoding with atob and returning it instead. ${error}`
124
124
  );
125
- return atob(base64);
125
+ return binString;
126
126
  }
127
127
  }
128
128
 
@@ -1290,9 +1290,8 @@ var WorkflowTool = class {
1290
1290
  };
1291
1291
 
1292
1292
  // src/context/steps.ts
1293
- var BaseLazyStep = class {
1293
+ var BaseLazyStep = class _BaseLazyStep {
1294
1294
  stepName;
1295
- // will be set in the subclasses
1296
1295
  constructor(stepName) {
1297
1296
  if (!stepName) {
1298
1297
  throw new WorkflowError(
@@ -1301,10 +1300,58 @@ var BaseLazyStep = class {
1301
1300
  }
1302
1301
  this.stepName = stepName;
1303
1302
  }
1303
+ /**
1304
+ * parse the out field of a step result.
1305
+ *
1306
+ * will be called when returning the steps to the context from auto executor
1307
+ *
1308
+ * @param out field of the step
1309
+ * @returns parsed out field
1310
+ */
1311
+ parseOut(out) {
1312
+ if (out === void 0) {
1313
+ if (this.allowUndefinedOut) {
1314
+ return void 0;
1315
+ } else {
1316
+ throw new WorkflowError(
1317
+ `Error while parsing output of ${this.stepType} step. Expected a string, but got: undefined`
1318
+ );
1319
+ }
1320
+ }
1321
+ if (typeof out === "object") {
1322
+ if (this.stepType !== "Wait") {
1323
+ console.warn(
1324
+ `Error while parsing ${this.stepType} step output. Expected a string, but got object. Please reach out to Upstash Support.`
1325
+ );
1326
+ return out;
1327
+ }
1328
+ return {
1329
+ ...out,
1330
+ eventData: _BaseLazyStep.tryParsing(out.eventData)
1331
+ };
1332
+ }
1333
+ if (typeof out !== "string") {
1334
+ throw new WorkflowError(
1335
+ `Error while parsing output of ${this.stepType} step. Expected a string or undefined, but got: ${typeof out}`
1336
+ );
1337
+ }
1338
+ return this.safeParseOut(out);
1339
+ }
1340
+ safeParseOut(out) {
1341
+ return _BaseLazyStep.tryParsing(out);
1342
+ }
1343
+ static tryParsing(stepOut) {
1344
+ try {
1345
+ return JSON.parse(stepOut);
1346
+ } catch {
1347
+ return stepOut;
1348
+ }
1349
+ }
1304
1350
  };
1305
1351
  var LazyFunctionStep = class extends BaseLazyStep {
1306
1352
  stepFunction;
1307
1353
  stepType = "Run";
1354
+ allowUndefinedOut = true;
1308
1355
  constructor(stepName, stepFunction) {
1309
1356
  super(stepName);
1310
1357
  this.stepFunction = stepFunction;
@@ -1335,6 +1382,7 @@ var LazyFunctionStep = class extends BaseLazyStep {
1335
1382
  var LazySleepStep = class extends BaseLazyStep {
1336
1383
  sleep;
1337
1384
  stepType = "SleepFor";
1385
+ allowUndefinedOut = true;
1338
1386
  constructor(stepName, sleep) {
1339
1387
  super(stepName);
1340
1388
  this.sleep = sleep;
@@ -1362,6 +1410,7 @@ var LazySleepStep = class extends BaseLazyStep {
1362
1410
  var LazySleepUntilStep = class extends BaseLazyStep {
1363
1411
  sleepUntil;
1364
1412
  stepType = "SleepUntil";
1413
+ allowUndefinedOut = true;
1365
1414
  constructor(stepName, sleepUntil) {
1366
1415
  super(stepName);
1367
1416
  this.sleepUntil = sleepUntil;
@@ -1385,8 +1434,11 @@ var LazySleepUntilStep = class extends BaseLazyStep {
1385
1434
  concurrent
1386
1435
  });
1387
1436
  }
1437
+ safeParseOut() {
1438
+ return void 0;
1439
+ }
1388
1440
  };
1389
- var LazyCallStep = class extends BaseLazyStep {
1441
+ var LazyCallStep = class _LazyCallStep extends BaseLazyStep {
1390
1442
  url;
1391
1443
  method;
1392
1444
  body;
@@ -1395,6 +1447,7 @@ var LazyCallStep = class extends BaseLazyStep {
1395
1447
  timeout;
1396
1448
  flowControl;
1397
1449
  stepType = "Call";
1450
+ allowUndefinedOut = false;
1398
1451
  constructor(stepName, url, method, body, headers, retries, timeout, flowControl) {
1399
1452
  super(stepName);
1400
1453
  this.url = url;
@@ -1426,11 +1479,53 @@ var LazyCallStep = class extends BaseLazyStep {
1426
1479
  callHeaders: this.headers
1427
1480
  });
1428
1481
  }
1482
+ safeParseOut(out) {
1483
+ const { header, status, body } = JSON.parse(out);
1484
+ const responseHeaders = new Headers(header);
1485
+ if (_LazyCallStep.isText(responseHeaders.get("content-type"))) {
1486
+ const bytes = new Uint8Array(out.length);
1487
+ for (let i = 0; i < out.length; i++) {
1488
+ bytes[i] = out.charCodeAt(i);
1489
+ }
1490
+ const processedResult = new TextDecoder().decode(bytes);
1491
+ const newBody = JSON.parse(processedResult).body;
1492
+ return {
1493
+ status,
1494
+ header,
1495
+ body: BaseLazyStep.tryParsing(newBody)
1496
+ };
1497
+ } else {
1498
+ return { header, status, body };
1499
+ }
1500
+ }
1501
+ static applicationHeaders = /* @__PURE__ */ new Set([
1502
+ "application/json",
1503
+ "application/xml",
1504
+ "application/javascript",
1505
+ "application/x-www-form-urlencoded",
1506
+ "application/xhtml+xml",
1507
+ "application/ld+json",
1508
+ "application/rss+xml",
1509
+ "application/atom+xml"
1510
+ ]);
1511
+ static isText = (contentTypeHeader) => {
1512
+ if (!contentTypeHeader) {
1513
+ return false;
1514
+ }
1515
+ if (_LazyCallStep.applicationHeaders.has(contentTypeHeader)) {
1516
+ return true;
1517
+ }
1518
+ if (contentTypeHeader.startsWith("text/")) {
1519
+ return true;
1520
+ }
1521
+ return false;
1522
+ };
1429
1523
  };
1430
1524
  var LazyWaitForEventStep = class extends BaseLazyStep {
1431
1525
  eventId;
1432
1526
  timeout;
1433
1527
  stepType = "Wait";
1528
+ allowUndefinedOut = false;
1434
1529
  constructor(stepName, eventId, timeout) {
1435
1530
  super(stepName);
1436
1531
  this.eventId = eventId;
@@ -1457,6 +1552,13 @@ var LazyWaitForEventStep = class extends BaseLazyStep {
1457
1552
  concurrent
1458
1553
  });
1459
1554
  }
1555
+ safeParseOut(out) {
1556
+ const result = JSON.parse(out);
1557
+ return {
1558
+ ...result,
1559
+ eventData: BaseLazyStep.tryParsing(result.eventData)
1560
+ };
1561
+ }
1460
1562
  };
1461
1563
  var LazyNotifyStep = class extends LazyFunctionStep {
1462
1564
  stepType = "Notify";
@@ -1470,10 +1572,18 @@ var LazyNotifyStep = class extends LazyFunctionStep {
1470
1572
  };
1471
1573
  });
1472
1574
  }
1575
+ safeParseOut(out) {
1576
+ const result = JSON.parse(out);
1577
+ return {
1578
+ ...result,
1579
+ eventData: BaseLazyStep.tryParsing(result.eventData)
1580
+ };
1581
+ }
1473
1582
  };
1474
1583
  var LazyInvokeStep = class extends BaseLazyStep {
1475
1584
  stepType = "Invoke";
1476
1585
  params;
1586
+ allowUndefinedOut = false;
1477
1587
  constructor(stepName, {
1478
1588
  workflow,
1479
1589
  body,
@@ -1513,6 +1623,13 @@ var LazyInvokeStep = class extends BaseLazyStep {
1513
1623
  concurrent
1514
1624
  });
1515
1625
  }
1626
+ safeParseOut(out) {
1627
+ const result = JSON.parse(out);
1628
+ return {
1629
+ ...result,
1630
+ body: BaseLazyStep.tryParsing(result.body)
1631
+ };
1632
+ }
1516
1633
  };
1517
1634
 
1518
1635
  // src/context/auto-executor.ts
@@ -1618,7 +1735,7 @@ var AutoExecutor = class _AutoExecutor {
1618
1735
  step,
1619
1736
  stepCount: this.stepCount
1620
1737
  });
1621
- return step.out;
1738
+ return lazyStep.parseOut(step.out);
1622
1739
  }
1623
1740
  const resultStep = await lazyStep.getResultStep(NO_CONCURRENCY, this.stepCount);
1624
1741
  await this.debug?.log("INFO", "RUN_SINGLE", {
@@ -1693,7 +1810,9 @@ var AutoExecutor = class _AutoExecutor {
1693
1810
  case "last": {
1694
1811
  const parallelResultSteps = sortedSteps.filter((step) => step.stepId >= initialStepCount).slice(0, parallelSteps.length);
1695
1812
  validateParallelSteps(parallelSteps, parallelResultSteps);
1696
- return parallelResultSteps.map((step) => step.out);
1813
+ return parallelResultSteps.map(
1814
+ (step, index) => parallelSteps[index].parseOut(step.out)
1815
+ );
1697
1816
  }
1698
1817
  }
1699
1818
  const fillValue = void 0;
@@ -2441,7 +2560,7 @@ var WorkflowContext = class {
2441
2560
  */
2442
2561
  async run(stepName, stepFunction) {
2443
2562
  const wrappedStepFunction = () => this.executor.wrapStep(stepName, stepFunction);
2444
- return this.addStep(new LazyFunctionStep(stepName, wrappedStepFunction));
2563
+ return await this.addStep(new LazyFunctionStep(stepName, wrappedStepFunction));
2445
2564
  }
2446
2565
  /**
2447
2566
  * Stops the execution for the duration provided.
@@ -2512,43 +2631,27 @@ var WorkflowContext = class {
2512
2631
  * }
2513
2632
  */
2514
2633
  async call(stepName, settings) {
2515
- const { url, method = "GET", body, headers = {}, retries = 0, timeout, flowControl } = settings;
2516
- const result = await this.addStep(
2634
+ const {
2635
+ url,
2636
+ method = "GET",
2637
+ body: requestBody,
2638
+ headers = {},
2639
+ retries = 0,
2640
+ timeout,
2641
+ flowControl
2642
+ } = settings;
2643
+ return await this.addStep(
2517
2644
  new LazyCallStep(
2518
2645
  stepName,
2519
2646
  url,
2520
2647
  method,
2521
- body,
2648
+ requestBody,
2522
2649
  headers,
2523
2650
  retries,
2524
2651
  timeout,
2525
2652
  flowControl
2526
2653
  )
2527
2654
  );
2528
- if (typeof result === "string") {
2529
- try {
2530
- const body2 = JSON.parse(result);
2531
- return {
2532
- status: 200,
2533
- header: {},
2534
- body: body2
2535
- };
2536
- } catch {
2537
- return {
2538
- status: 200,
2539
- header: {},
2540
- body: result
2541
- };
2542
- }
2543
- }
2544
- try {
2545
- return {
2546
- ...result,
2547
- body: JSON.parse(result.body)
2548
- };
2549
- } catch {
2550
- return result;
2551
- }
2552
2655
  }
2553
2656
  /**
2554
2657
  * Pauses workflow execution until a specific event occurs or a timeout is reached.
@@ -2587,15 +2690,7 @@ var WorkflowContext = class {
2587
2690
  async waitForEvent(stepName, eventId, options = {}) {
2588
2691
  const { timeout = "7d" } = options;
2589
2692
  const timeoutStr = typeof timeout === "string" ? timeout : `${timeout}s`;
2590
- const result = await this.addStep(new LazyWaitForEventStep(stepName, eventId, timeoutStr));
2591
- try {
2592
- return {
2593
- ...result,
2594
- eventData: JSON.parse(result.eventData)
2595
- };
2596
- } catch {
2597
- return result;
2598
- }
2693
+ return await this.addStep(new LazyWaitForEventStep(stepName, eventId, timeoutStr));
2599
2694
  }
2600
2695
  /**
2601
2696
  * Notify workflow runs waiting for an event
@@ -2619,24 +2714,12 @@ var WorkflowContext = class {
2619
2714
  * @returns notify response which has event id, event data and list of waiters which were notified
2620
2715
  */
2621
2716
  async notify(stepName, eventId, eventData) {
2622
- const result = await this.addStep(
2717
+ return await this.addStep(
2623
2718
  new LazyNotifyStep(stepName, eventId, eventData, this.qstashClient.http)
2624
2719
  );
2625
- try {
2626
- return {
2627
- ...result,
2628
- eventData: JSON.parse(result.eventData)
2629
- };
2630
- } catch {
2631
- return result;
2632
- }
2633
2720
  }
2634
2721
  async invoke(stepName, settings) {
2635
- const result = await this.addStep(new LazyInvokeStep(stepName, settings));
2636
- return {
2637
- ...result,
2638
- body: result.body ? JSON.parse(result.body) : void 0
2639
- };
2722
+ return await this.addStep(new LazyInvokeStep(stepName, settings));
2640
2723
  }
2641
2724
  /**
2642
2725
  * Cancel the current workflow run
@@ -2795,10 +2878,6 @@ var processRawSteps = (rawSteps) => {
2795
2878
  const stepsToDecode = encodedSteps.filter((step) => step.callType === "step");
2796
2879
  const otherSteps = stepsToDecode.map((rawStep) => {
2797
2880
  const step = JSON.parse(decodeBase64(rawStep.body));
2798
- try {
2799
- step.out = JSON.parse(step.out);
2800
- } catch {
2801
- }
2802
2881
  if (step.waitEventId) {
2803
2882
  const newOut = {
2804
2883
  eventData: step.out ? decodeBase64(step.out) : void 0,
package/cloudflare.d.mts CHANGED
@@ -1,5 +1,5 @@
1
- import { R as RouteFunction, k as PublicServeOptions, t as InvokableWorkflow } from './types-CYhDXnf8.mjs';
2
- import { s as serveManyBase } from './serve-many-BVDpPsF-.mjs';
1
+ import { R as RouteFunction, k as PublicServeOptions, t as InvokableWorkflow } from './types-Dg_9L83G.mjs';
2
+ import { s as serveManyBase } from './serve-many-wMUWrSIP.mjs';
3
3
  import '@upstash/qstash';
4
4
  import 'zod';
5
5
  import 'ai';
package/cloudflare.d.ts CHANGED
@@ -1,5 +1,5 @@
1
- import { R as RouteFunction, k as PublicServeOptions, t as InvokableWorkflow } from './types-CYhDXnf8.js';
2
- import { s as serveManyBase } from './serve-many-e4zufyXN.js';
1
+ import { R as RouteFunction, k as PublicServeOptions, t as InvokableWorkflow } from './types-Dg_9L83G.js';
2
+ import { s as serveManyBase } from './serve-many-jCRazho9.js';
3
3
  import '@upstash/qstash';
4
4
  import 'zod';
5
5
  import 'ai';