@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/astro.d.mts CHANGED
@@ -1,6 +1,6 @@
1
1
  import { APIContext, APIRoute } from 'astro';
2
- import { e as WorkflowContext, k as PublicServeOptions, t as InvokableWorkflow } from './types-CYhDXnf8.mjs';
3
- import { s as serveManyBase } from './serve-many-BVDpPsF-.mjs';
2
+ import { e as WorkflowContext, 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/astro.d.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  import { APIContext, APIRoute } from 'astro';
2
- import { e as WorkflowContext, k as PublicServeOptions, t as InvokableWorkflow } from './types-CYhDXnf8.js';
3
- import { s as serveManyBase } from './serve-many-e4zufyXN.js';
2
+ import { e as WorkflowContext, 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';
package/astro.js CHANGED
@@ -151,22 +151,21 @@ function getWorkflowRunId(id) {
151
151
  return `wfr_${id ?? nanoid()}`;
152
152
  }
153
153
  function decodeBase64(base64) {
154
+ const binString = atob(base64);
154
155
  try {
155
- const binString = atob(base64);
156
156
  const intArray = Uint8Array.from(binString, (m) => m.codePointAt(0));
157
157
  return new TextDecoder().decode(intArray);
158
158
  } catch (error) {
159
159
  console.warn(
160
160
  `Upstash Qstash: Failed while decoding base64 "${base64}". Decoding with atob and returning it instead. ${error}`
161
161
  );
162
- return atob(base64);
162
+ return binString;
163
163
  }
164
164
  }
165
165
 
166
166
  // src/context/steps.ts
167
- var BaseLazyStep = class {
167
+ var BaseLazyStep = class _BaseLazyStep {
168
168
  stepName;
169
- // will be set in the subclasses
170
169
  constructor(stepName) {
171
170
  if (!stepName) {
172
171
  throw new WorkflowError(
@@ -175,10 +174,58 @@ var BaseLazyStep = class {
175
174
  }
176
175
  this.stepName = stepName;
177
176
  }
177
+ /**
178
+ * parse the out field of a step result.
179
+ *
180
+ * will be called when returning the steps to the context from auto executor
181
+ *
182
+ * @param out field of the step
183
+ * @returns parsed out field
184
+ */
185
+ parseOut(out) {
186
+ if (out === void 0) {
187
+ if (this.allowUndefinedOut) {
188
+ return void 0;
189
+ } else {
190
+ throw new WorkflowError(
191
+ `Error while parsing output of ${this.stepType} step. Expected a string, but got: undefined`
192
+ );
193
+ }
194
+ }
195
+ if (typeof out === "object") {
196
+ if (this.stepType !== "Wait") {
197
+ console.warn(
198
+ `Error while parsing ${this.stepType} step output. Expected a string, but got object. Please reach out to Upstash Support.`
199
+ );
200
+ return out;
201
+ }
202
+ return {
203
+ ...out,
204
+ eventData: _BaseLazyStep.tryParsing(out.eventData)
205
+ };
206
+ }
207
+ if (typeof out !== "string") {
208
+ throw new WorkflowError(
209
+ `Error while parsing output of ${this.stepType} step. Expected a string or undefined, but got: ${typeof out}`
210
+ );
211
+ }
212
+ return this.safeParseOut(out);
213
+ }
214
+ safeParseOut(out) {
215
+ return _BaseLazyStep.tryParsing(out);
216
+ }
217
+ static tryParsing(stepOut) {
218
+ try {
219
+ return JSON.parse(stepOut);
220
+ } catch {
221
+ return stepOut;
222
+ }
223
+ }
178
224
  };
179
225
  var LazyFunctionStep = class extends BaseLazyStep {
180
226
  stepFunction;
181
227
  stepType = "Run";
228
+ allowUndefinedOut = true;
182
229
  constructor(stepName, stepFunction) {
183
230
  super(stepName);
184
231
  this.stepFunction = stepFunction;
@@ -209,6 +256,7 @@ var LazyFunctionStep = class extends BaseLazyStep {
209
256
  var LazySleepStep = class extends BaseLazyStep {
210
257
  sleep;
211
258
  stepType = "SleepFor";
259
+ allowUndefinedOut = true;
212
260
  constructor(stepName, sleep) {
213
261
  super(stepName);
214
262
  this.sleep = sleep;
@@ -236,6 +284,7 @@ var LazySleepStep = class extends BaseLazyStep {
236
284
  var LazySleepUntilStep = class extends BaseLazyStep {
237
285
  sleepUntil;
238
286
  stepType = "SleepUntil";
287
+ allowUndefinedOut = true;
239
288
  constructor(stepName, sleepUntil) {
240
289
  super(stepName);
241
290
  this.sleepUntil = sleepUntil;
@@ -259,8 +308,11 @@ var LazySleepUntilStep = class extends BaseLazyStep {
259
308
  concurrent
260
309
  });
261
310
  }
311
+ safeParseOut() {
312
+ return void 0;
313
+ }
262
314
  };
263
- var LazyCallStep = class extends BaseLazyStep {
315
+ var LazyCallStep = class _LazyCallStep extends BaseLazyStep {
264
316
  url;
265
317
  method;
266
318
  body;
@@ -269,6 +321,7 @@ var LazyCallStep = class extends BaseLazyStep {
269
321
  timeout;
270
322
  flowControl;
271
323
  stepType = "Call";
324
+ allowUndefinedOut = false;
272
325
  constructor(stepName, url, method, body, headers, retries, timeout, flowControl) {
273
326
  super(stepName);
274
327
  this.url = url;
@@ -300,11 +353,53 @@ var LazyCallStep = class extends BaseLazyStep {
300
353
  callHeaders: this.headers
301
354
  });
302
355
  }
356
+ safeParseOut(out) {
357
+ const { header, status, body } = JSON.parse(out);
358
+ const responseHeaders = new Headers(header);
359
+ if (_LazyCallStep.isText(responseHeaders.get("content-type"))) {
360
+ const bytes = new Uint8Array(out.length);
361
+ for (let i = 0; i < out.length; i++) {
362
+ bytes[i] = out.charCodeAt(i);
363
+ }
364
+ const processedResult = new TextDecoder().decode(bytes);
365
+ const newBody = JSON.parse(processedResult).body;
366
+ return {
367
+ status,
368
+ header,
369
+ body: BaseLazyStep.tryParsing(newBody)
370
+ };
371
+ } else {
372
+ return { header, status, body };
373
+ }
374
+ }
375
+ static applicationHeaders = /* @__PURE__ */ new Set([
376
+ "application/json",
377
+ "application/xml",
378
+ "application/javascript",
379
+ "application/x-www-form-urlencoded",
380
+ "application/xhtml+xml",
381
+ "application/ld+json",
382
+ "application/rss+xml",
383
+ "application/atom+xml"
384
+ ]);
385
+ static isText = (contentTypeHeader) => {
386
+ if (!contentTypeHeader) {
387
+ return false;
388
+ }
389
+ if (_LazyCallStep.applicationHeaders.has(contentTypeHeader)) {
390
+ return true;
391
+ }
392
+ if (contentTypeHeader.startsWith("text/")) {
393
+ return true;
394
+ }
395
+ return false;
396
+ };
303
397
  };
304
398
  var LazyWaitForEventStep = class extends BaseLazyStep {
305
399
  eventId;
306
400
  timeout;
307
401
  stepType = "Wait";
402
+ allowUndefinedOut = false;
308
403
  constructor(stepName, eventId, timeout) {
309
404
  super(stepName);
310
405
  this.eventId = eventId;
@@ -331,6 +426,13 @@ var LazyWaitForEventStep = class extends BaseLazyStep {
331
426
  concurrent
332
427
  });
333
428
  }
429
+ safeParseOut(out) {
430
+ const result = JSON.parse(out);
431
+ return {
432
+ ...result,
433
+ eventData: BaseLazyStep.tryParsing(result.eventData)
434
+ };
435
+ }
334
436
  };
335
437
  var LazyNotifyStep = class extends LazyFunctionStep {
336
438
  stepType = "Notify";
@@ -344,10 +446,18 @@ var LazyNotifyStep = class extends LazyFunctionStep {
344
446
  };
345
447
  });
346
448
  }
449
+ safeParseOut(out) {
450
+ const result = JSON.parse(out);
451
+ return {
452
+ ...result,
453
+ eventData: BaseLazyStep.tryParsing(result.eventData)
454
+ };
455
+ }
347
456
  };
348
457
  var LazyInvokeStep = class extends BaseLazyStep {
349
458
  stepType = "Invoke";
350
459
  params;
460
+ allowUndefinedOut = false;
351
461
  constructor(stepName, {
352
462
  workflow,
353
463
  body,
@@ -387,6 +497,13 @@ var LazyInvokeStep = class extends BaseLazyStep {
387
497
  concurrent
388
498
  });
389
499
  }
500
+ safeParseOut(out) {
501
+ const result = JSON.parse(out);
502
+ return {
503
+ ...result,
504
+ body: BaseLazyStep.tryParsing(result.body)
505
+ };
506
+ }
390
507
  };
391
508
 
392
509
  // node_modules/neverthrow/dist/index.es.js
@@ -1072,7 +1189,8 @@ var getHeaders = ({
1072
1189
  flowControl,
1073
1190
  callFlowControl
1074
1191
  }) => {
1075
- const contentType = (userHeaders ? userHeaders.get("Content-Type") : void 0) ?? DEFAULT_CONTENT_TYPE;
1192
+ const callHeaders = new Headers(step?.callHeaders);
1193
+ const contentType = (callHeaders.get("content-type") ? callHeaders.get("content-type") : userHeaders?.get("Content-Type") ? userHeaders.get("Content-Type") : void 0) ?? DEFAULT_CONTENT_TYPE;
1076
1194
  const baseHeaders = {
1077
1195
  [WORKFLOW_INIT_HEADER]: initHeaderValue,
1078
1196
  [WORKFLOW_ID_HEADER]: workflowRunId,
@@ -1483,7 +1601,7 @@ var AutoExecutor = class _AutoExecutor {
1483
1601
  step,
1484
1602
  stepCount: this.stepCount
1485
1603
  });
1486
- return step.out;
1604
+ return lazyStep.parseOut(step.out);
1487
1605
  }
1488
1606
  const resultStep = await lazyStep.getResultStep(NO_CONCURRENCY, this.stepCount);
1489
1607
  await this.debug?.log("INFO", "RUN_SINGLE", {
@@ -1558,7 +1676,9 @@ var AutoExecutor = class _AutoExecutor {
1558
1676
  case "last": {
1559
1677
  const parallelResultSteps = sortedSteps.filter((step) => step.stepId >= initialStepCount).slice(0, parallelSteps.length);
1560
1678
  validateParallelSteps(parallelSteps, parallelResultSteps);
1561
- return parallelResultSteps.map((step) => step.out);
1679
+ return parallelResultSteps.map(
1680
+ (step, index) => parallelSteps[index].parseOut(step.out)
1681
+ );
1562
1682
  }
1563
1683
  }
1564
1684
  const fillValue = void 0;
@@ -1661,7 +1781,7 @@ var AutoExecutor = class _AutoExecutor {
1661
1781
  });
1662
1782
  throw new WorkflowAbort(invokeStep.stepName, invokeStep);
1663
1783
  }
1664
- const result = await this.context.qstashClient.batchJSON(
1784
+ const result = await this.context.qstashClient.batch(
1665
1785
  steps.map((singleStep, index) => {
1666
1786
  const lazyStep = lazySteps[index];
1667
1787
  const { headers } = getHeaders({
@@ -1691,7 +1811,7 @@ var AutoExecutor = class _AutoExecutor {
1691
1811
  {
1692
1812
  headers,
1693
1813
  method: singleStep.callMethod,
1694
- body: singleStep.callBody,
1814
+ body: JSON.stringify(singleStep.callBody),
1695
1815
  url: singleStep.callUrl
1696
1816
  }
1697
1817
  ) : (
@@ -1701,7 +1821,7 @@ var AutoExecutor = class _AutoExecutor {
1701
1821
  {
1702
1822
  headers,
1703
1823
  method: "POST",
1704
- body: singleStep,
1824
+ body: JSON.stringify(singleStep),
1705
1825
  url: this.context.url,
1706
1826
  notBefore: willWait ? singleStep.sleepUntil : void 0,
1707
1827
  delay: willWait ? singleStep.sleepFor : void 0
@@ -1709,8 +1829,9 @@ var AutoExecutor = class _AutoExecutor {
1709
1829
  );
1710
1830
  })
1711
1831
  );
1832
+ const _result = result;
1712
1833
  await this.debug?.log("INFO", "SUBMIT_STEP", {
1713
- messageIds: result.map((message) => {
1834
+ messageIds: _result.map((message) => {
1714
1835
  return {
1715
1836
  message: message.messageId
1716
1837
  };
@@ -2399,7 +2520,7 @@ var WorkflowContext = class {
2399
2520
  */
2400
2521
  async run(stepName, stepFunction) {
2401
2522
  const wrappedStepFunction = () => this.executor.wrapStep(stepName, stepFunction);
2402
- return this.addStep(new LazyFunctionStep(stepName, wrappedStepFunction));
2523
+ return await this.addStep(new LazyFunctionStep(stepName, wrappedStepFunction));
2403
2524
  }
2404
2525
  /**
2405
2526
  * Stops the execution for the duration provided.
@@ -2470,43 +2591,27 @@ var WorkflowContext = class {
2470
2591
  * }
2471
2592
  */
2472
2593
  async call(stepName, settings) {
2473
- const { url, method = "GET", body, headers = {}, retries = 0, timeout, flowControl } = settings;
2474
- const result = await this.addStep(
2594
+ const {
2595
+ url,
2596
+ method = "GET",
2597
+ body: requestBody,
2598
+ headers = {},
2599
+ retries = 0,
2600
+ timeout,
2601
+ flowControl
2602
+ } = settings;
2603
+ return await this.addStep(
2475
2604
  new LazyCallStep(
2476
2605
  stepName,
2477
2606
  url,
2478
2607
  method,
2479
- body,
2608
+ requestBody,
2480
2609
  headers,
2481
2610
  retries,
2482
2611
  timeout,
2483
2612
  flowControl
2484
2613
  )
2485
2614
  );
2486
- if (typeof result === "string") {
2487
- try {
2488
- const body2 = JSON.parse(result);
2489
- return {
2490
- status: 200,
2491
- header: {},
2492
- body: body2
2493
- };
2494
- } catch {
2495
- return {
2496
- status: 200,
2497
- header: {},
2498
- body: result
2499
- };
2500
- }
2501
- }
2502
- try {
2503
- return {
2504
- ...result,
2505
- body: JSON.parse(result.body)
2506
- };
2507
- } catch {
2508
- return result;
2509
- }
2510
2615
  }
2511
2616
  /**
2512
2617
  * Pauses workflow execution until a specific event occurs or a timeout is reached.
@@ -2545,15 +2650,7 @@ var WorkflowContext = class {
2545
2650
  async waitForEvent(stepName, eventId, options = {}) {
2546
2651
  const { timeout = "7d" } = options;
2547
2652
  const timeoutStr = typeof timeout === "string" ? timeout : `${timeout}s`;
2548
- const result = await this.addStep(new LazyWaitForEventStep(stepName, eventId, timeoutStr));
2549
- try {
2550
- return {
2551
- ...result,
2552
- eventData: JSON.parse(result.eventData)
2553
- };
2554
- } catch {
2555
- return result;
2556
- }
2653
+ return await this.addStep(new LazyWaitForEventStep(stepName, eventId, timeoutStr));
2557
2654
  }
2558
2655
  /**
2559
2656
  * Notify workflow runs waiting for an event
@@ -2577,24 +2674,12 @@ var WorkflowContext = class {
2577
2674
  * @returns notify response which has event id, event data and list of waiters which were notified
2578
2675
  */
2579
2676
  async notify(stepName, eventId, eventData) {
2580
- const result = await this.addStep(
2677
+ return await this.addStep(
2581
2678
  new LazyNotifyStep(stepName, eventId, eventData, this.qstashClient.http)
2582
2679
  );
2583
- try {
2584
- return {
2585
- ...result,
2586
- eventData: JSON.parse(result.eventData)
2587
- };
2588
- } catch {
2589
- return result;
2590
- }
2591
2680
  }
2592
2681
  async invoke(stepName, settings) {
2593
- const result = await this.addStep(new LazyInvokeStep(stepName, settings));
2594
- return {
2595
- ...result,
2596
- body: result.body ? JSON.parse(result.body) : void 0
2597
- };
2682
+ return await this.addStep(new LazyInvokeStep(stepName, settings));
2598
2683
  }
2599
2684
  /**
2600
2685
  * Cancel the current workflow run
@@ -2753,10 +2838,6 @@ var processRawSteps = (rawSteps) => {
2753
2838
  const stepsToDecode = encodedSteps.filter((step) => step.callType === "step");
2754
2839
  const otherSteps = stepsToDecode.map((rawStep) => {
2755
2840
  const step = JSON.parse(decodeBase64(rawStep.body));
2756
- try {
2757
- step.out = JSON.parse(step.out);
2758
- } catch {
2759
- }
2760
2841
  if (step.waitEventId) {
2761
2842
  const newOut = {
2762
2843
  eventData: step.out ? decodeBase64(step.out) : void 0,
package/astro.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
  // platforms/astro.ts
8
8
  var telemetry = {