@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/solidjs.js CHANGED
@@ -149,22 +149,21 @@ function getWorkflowRunId(id) {
149
149
  return `wfr_${id ?? nanoid()}`;
150
150
  }
151
151
  function decodeBase64(base64) {
152
+ const binString = atob(base64);
152
153
  try {
153
- const binString = atob(base64);
154
154
  const intArray = Uint8Array.from(binString, (m) => m.codePointAt(0));
155
155
  return new TextDecoder().decode(intArray);
156
156
  } catch (error) {
157
157
  console.warn(
158
158
  `Upstash Qstash: Failed while decoding base64 "${base64}". Decoding with atob and returning it instead. ${error}`
159
159
  );
160
- return atob(base64);
160
+ return binString;
161
161
  }
162
162
  }
163
163
 
164
164
  // src/context/steps.ts
165
- var BaseLazyStep = class {
165
+ var BaseLazyStep = class _BaseLazyStep {
166
166
  stepName;
167
- // will be set in the subclasses
168
167
  constructor(stepName) {
169
168
  if (!stepName) {
170
169
  throw new WorkflowError(
@@ -173,10 +172,58 @@ var BaseLazyStep = class {
173
172
  }
174
173
  this.stepName = stepName;
175
174
  }
175
+ /**
176
+ * parse the out field of a step result.
177
+ *
178
+ * will be called when returning the steps to the context from auto executor
179
+ *
180
+ * @param out field of the step
181
+ * @returns parsed out field
182
+ */
183
+ parseOut(out) {
184
+ if (out === void 0) {
185
+ if (this.allowUndefinedOut) {
186
+ return void 0;
187
+ } else {
188
+ throw new WorkflowError(
189
+ `Error while parsing output of ${this.stepType} step. Expected a string, but got: undefined`
190
+ );
191
+ }
192
+ }
193
+ if (typeof out === "object") {
194
+ if (this.stepType !== "Wait") {
195
+ console.warn(
196
+ `Error while parsing ${this.stepType} step output. Expected a string, but got object. Please reach out to Upstash Support.`
197
+ );
198
+ return out;
199
+ }
200
+ return {
201
+ ...out,
202
+ eventData: _BaseLazyStep.tryParsing(out.eventData)
203
+ };
204
+ }
205
+ if (typeof out !== "string") {
206
+ throw new WorkflowError(
207
+ `Error while parsing output of ${this.stepType} step. Expected a string or undefined, but got: ${typeof out}`
208
+ );
209
+ }
210
+ return this.safeParseOut(out);
211
+ }
212
+ safeParseOut(out) {
213
+ return _BaseLazyStep.tryParsing(out);
214
+ }
215
+ static tryParsing(stepOut) {
216
+ try {
217
+ return JSON.parse(stepOut);
218
+ } catch {
219
+ return stepOut;
220
+ }
221
+ }
176
222
  };
177
223
  var LazyFunctionStep = class extends BaseLazyStep {
178
224
  stepFunction;
179
225
  stepType = "Run";
226
+ allowUndefinedOut = true;
180
227
  constructor(stepName, stepFunction) {
181
228
  super(stepName);
182
229
  this.stepFunction = stepFunction;
@@ -207,6 +254,7 @@ var LazyFunctionStep = class extends BaseLazyStep {
207
254
  var LazySleepStep = class extends BaseLazyStep {
208
255
  sleep;
209
256
  stepType = "SleepFor";
257
+ allowUndefinedOut = true;
210
258
  constructor(stepName, sleep) {
211
259
  super(stepName);
212
260
  this.sleep = sleep;
@@ -234,6 +282,7 @@ var LazySleepStep = class extends BaseLazyStep {
234
282
  var LazySleepUntilStep = class extends BaseLazyStep {
235
283
  sleepUntil;
236
284
  stepType = "SleepUntil";
285
+ allowUndefinedOut = true;
237
286
  constructor(stepName, sleepUntil) {
238
287
  super(stepName);
239
288
  this.sleepUntil = sleepUntil;
@@ -257,8 +306,11 @@ var LazySleepUntilStep = class extends BaseLazyStep {
257
306
  concurrent
258
307
  });
259
308
  }
309
+ safeParseOut() {
310
+ return void 0;
311
+ }
260
312
  };
261
- var LazyCallStep = class extends BaseLazyStep {
313
+ var LazyCallStep = class _LazyCallStep extends BaseLazyStep {
262
314
  url;
263
315
  method;
264
316
  body;
@@ -267,6 +319,7 @@ var LazyCallStep = class extends BaseLazyStep {
267
319
  timeout;
268
320
  flowControl;
269
321
  stepType = "Call";
322
+ allowUndefinedOut = false;
270
323
  constructor(stepName, url, method, body, headers, retries, timeout, flowControl) {
271
324
  super(stepName);
272
325
  this.url = url;
@@ -298,11 +351,53 @@ var LazyCallStep = class extends BaseLazyStep {
298
351
  callHeaders: this.headers
299
352
  });
300
353
  }
354
+ safeParseOut(out) {
355
+ const { header, status, body } = JSON.parse(out);
356
+ const responseHeaders = new Headers(header);
357
+ if (_LazyCallStep.isText(responseHeaders.get("content-type"))) {
358
+ const bytes = new Uint8Array(out.length);
359
+ for (let i = 0; i < out.length; i++) {
360
+ bytes[i] = out.charCodeAt(i);
361
+ }
362
+ const processedResult = new TextDecoder().decode(bytes);
363
+ const newBody = JSON.parse(processedResult).body;
364
+ return {
365
+ status,
366
+ header,
367
+ body: BaseLazyStep.tryParsing(newBody)
368
+ };
369
+ } else {
370
+ return { header, status, body };
371
+ }
372
+ }
373
+ static applicationHeaders = /* @__PURE__ */ new Set([
374
+ "application/json",
375
+ "application/xml",
376
+ "application/javascript",
377
+ "application/x-www-form-urlencoded",
378
+ "application/xhtml+xml",
379
+ "application/ld+json",
380
+ "application/rss+xml",
381
+ "application/atom+xml"
382
+ ]);
383
+ static isText = (contentTypeHeader) => {
384
+ if (!contentTypeHeader) {
385
+ return false;
386
+ }
387
+ if (_LazyCallStep.applicationHeaders.has(contentTypeHeader)) {
388
+ return true;
389
+ }
390
+ if (contentTypeHeader.startsWith("text/")) {
391
+ return true;
392
+ }
393
+ return false;
394
+ };
301
395
  };
302
396
  var LazyWaitForEventStep = class extends BaseLazyStep {
303
397
  eventId;
304
398
  timeout;
305
399
  stepType = "Wait";
400
+ allowUndefinedOut = false;
306
401
  constructor(stepName, eventId, timeout) {
307
402
  super(stepName);
308
403
  this.eventId = eventId;
@@ -329,6 +424,13 @@ var LazyWaitForEventStep = class extends BaseLazyStep {
329
424
  concurrent
330
425
  });
331
426
  }
427
+ safeParseOut(out) {
428
+ const result = JSON.parse(out);
429
+ return {
430
+ ...result,
431
+ eventData: BaseLazyStep.tryParsing(result.eventData)
432
+ };
433
+ }
332
434
  };
333
435
  var LazyNotifyStep = class extends LazyFunctionStep {
334
436
  stepType = "Notify";
@@ -342,10 +444,18 @@ var LazyNotifyStep = class extends LazyFunctionStep {
342
444
  };
343
445
  });
344
446
  }
447
+ safeParseOut(out) {
448
+ const result = JSON.parse(out);
449
+ return {
450
+ ...result,
451
+ eventData: BaseLazyStep.tryParsing(result.eventData)
452
+ };
453
+ }
345
454
  };
346
455
  var LazyInvokeStep = class extends BaseLazyStep {
347
456
  stepType = "Invoke";
348
457
  params;
458
+ allowUndefinedOut = false;
349
459
  constructor(stepName, {
350
460
  workflow,
351
461
  body,
@@ -385,6 +495,13 @@ var LazyInvokeStep = class extends BaseLazyStep {
385
495
  concurrent
386
496
  });
387
497
  }
498
+ safeParseOut(out) {
499
+ const result = JSON.parse(out);
500
+ return {
501
+ ...result,
502
+ body: BaseLazyStep.tryParsing(result.body)
503
+ };
504
+ }
388
505
  };
389
506
 
390
507
  // node_modules/neverthrow/dist/index.es.js
@@ -1070,7 +1187,8 @@ var getHeaders = ({
1070
1187
  flowControl,
1071
1188
  callFlowControl
1072
1189
  }) => {
1073
- const contentType = (userHeaders ? userHeaders.get("Content-Type") : void 0) ?? DEFAULT_CONTENT_TYPE;
1190
+ const callHeaders = new Headers(step?.callHeaders);
1191
+ const contentType = (callHeaders.get("content-type") ? callHeaders.get("content-type") : userHeaders?.get("Content-Type") ? userHeaders.get("Content-Type") : void 0) ?? DEFAULT_CONTENT_TYPE;
1074
1192
  const baseHeaders = {
1075
1193
  [WORKFLOW_INIT_HEADER]: initHeaderValue,
1076
1194
  [WORKFLOW_ID_HEADER]: workflowRunId,
@@ -1420,7 +1538,7 @@ var AutoExecutor = class _AutoExecutor {
1420
1538
  step,
1421
1539
  stepCount: this.stepCount
1422
1540
  });
1423
- return step.out;
1541
+ return lazyStep.parseOut(step.out);
1424
1542
  }
1425
1543
  const resultStep = await lazyStep.getResultStep(NO_CONCURRENCY, this.stepCount);
1426
1544
  await this.debug?.log("INFO", "RUN_SINGLE", {
@@ -1495,7 +1613,9 @@ var AutoExecutor = class _AutoExecutor {
1495
1613
  case "last": {
1496
1614
  const parallelResultSteps = sortedSteps.filter((step) => step.stepId >= initialStepCount).slice(0, parallelSteps.length);
1497
1615
  validateParallelSteps(parallelSteps, parallelResultSteps);
1498
- return parallelResultSteps.map((step) => step.out);
1616
+ return parallelResultSteps.map(
1617
+ (step, index) => parallelSteps[index].parseOut(step.out)
1618
+ );
1499
1619
  }
1500
1620
  }
1501
1621
  const fillValue = void 0;
@@ -1598,7 +1718,7 @@ var AutoExecutor = class _AutoExecutor {
1598
1718
  });
1599
1719
  throw new WorkflowAbort(invokeStep.stepName, invokeStep);
1600
1720
  }
1601
- const result = await this.context.qstashClient.batchJSON(
1721
+ const result = await this.context.qstashClient.batch(
1602
1722
  steps.map((singleStep, index) => {
1603
1723
  const lazyStep = lazySteps[index];
1604
1724
  const { headers } = getHeaders({
@@ -1628,7 +1748,7 @@ var AutoExecutor = class _AutoExecutor {
1628
1748
  {
1629
1749
  headers,
1630
1750
  method: singleStep.callMethod,
1631
- body: singleStep.callBody,
1751
+ body: JSON.stringify(singleStep.callBody),
1632
1752
  url: singleStep.callUrl
1633
1753
  }
1634
1754
  ) : (
@@ -1638,7 +1758,7 @@ var AutoExecutor = class _AutoExecutor {
1638
1758
  {
1639
1759
  headers,
1640
1760
  method: "POST",
1641
- body: singleStep,
1761
+ body: JSON.stringify(singleStep),
1642
1762
  url: this.context.url,
1643
1763
  notBefore: willWait ? singleStep.sleepUntil : void 0,
1644
1764
  delay: willWait ? singleStep.sleepFor : void 0
@@ -1646,8 +1766,9 @@ var AutoExecutor = class _AutoExecutor {
1646
1766
  );
1647
1767
  })
1648
1768
  );
1769
+ const _result = result;
1649
1770
  await this.debug?.log("INFO", "SUBMIT_STEP", {
1650
- messageIds: result.map((message) => {
1771
+ messageIds: _result.map((message) => {
1651
1772
  return {
1652
1773
  message: message.messageId
1653
1774
  };
@@ -2336,7 +2457,7 @@ var WorkflowContext = class {
2336
2457
  */
2337
2458
  async run(stepName, stepFunction) {
2338
2459
  const wrappedStepFunction = () => this.executor.wrapStep(stepName, stepFunction);
2339
- return this.addStep(new LazyFunctionStep(stepName, wrappedStepFunction));
2460
+ return await this.addStep(new LazyFunctionStep(stepName, wrappedStepFunction));
2340
2461
  }
2341
2462
  /**
2342
2463
  * Stops the execution for the duration provided.
@@ -2407,43 +2528,27 @@ var WorkflowContext = class {
2407
2528
  * }
2408
2529
  */
2409
2530
  async call(stepName, settings) {
2410
- const { url, method = "GET", body, headers = {}, retries = 0, timeout, flowControl } = settings;
2411
- const result = await this.addStep(
2531
+ const {
2532
+ url,
2533
+ method = "GET",
2534
+ body: requestBody,
2535
+ headers = {},
2536
+ retries = 0,
2537
+ timeout,
2538
+ flowControl
2539
+ } = settings;
2540
+ return await this.addStep(
2412
2541
  new LazyCallStep(
2413
2542
  stepName,
2414
2543
  url,
2415
2544
  method,
2416
- body,
2545
+ requestBody,
2417
2546
  headers,
2418
2547
  retries,
2419
2548
  timeout,
2420
2549
  flowControl
2421
2550
  )
2422
2551
  );
2423
- if (typeof result === "string") {
2424
- try {
2425
- const body2 = JSON.parse(result);
2426
- return {
2427
- status: 200,
2428
- header: {},
2429
- body: body2
2430
- };
2431
- } catch {
2432
- return {
2433
- status: 200,
2434
- header: {},
2435
- body: result
2436
- };
2437
- }
2438
- }
2439
- try {
2440
- return {
2441
- ...result,
2442
- body: JSON.parse(result.body)
2443
- };
2444
- } catch {
2445
- return result;
2446
- }
2447
2552
  }
2448
2553
  /**
2449
2554
  * Pauses workflow execution until a specific event occurs or a timeout is reached.
@@ -2482,15 +2587,7 @@ var WorkflowContext = class {
2482
2587
  async waitForEvent(stepName, eventId, options = {}) {
2483
2588
  const { timeout = "7d" } = options;
2484
2589
  const timeoutStr = typeof timeout === "string" ? timeout : `${timeout}s`;
2485
- const result = await this.addStep(new LazyWaitForEventStep(stepName, eventId, timeoutStr));
2486
- try {
2487
- return {
2488
- ...result,
2489
- eventData: JSON.parse(result.eventData)
2490
- };
2491
- } catch {
2492
- return result;
2493
- }
2590
+ return await this.addStep(new LazyWaitForEventStep(stepName, eventId, timeoutStr));
2494
2591
  }
2495
2592
  /**
2496
2593
  * Notify workflow runs waiting for an event
@@ -2514,24 +2611,12 @@ var WorkflowContext = class {
2514
2611
  * @returns notify response which has event id, event data and list of waiters which were notified
2515
2612
  */
2516
2613
  async notify(stepName, eventId, eventData) {
2517
- const result = await this.addStep(
2614
+ return await this.addStep(
2518
2615
  new LazyNotifyStep(stepName, eventId, eventData, this.qstashClient.http)
2519
2616
  );
2520
- try {
2521
- return {
2522
- ...result,
2523
- eventData: JSON.parse(result.eventData)
2524
- };
2525
- } catch {
2526
- return result;
2527
- }
2528
2617
  }
2529
2618
  async invoke(stepName, settings) {
2530
- const result = await this.addStep(new LazyInvokeStep(stepName, settings));
2531
- return {
2532
- ...result,
2533
- body: result.body ? JSON.parse(result.body) : void 0
2534
- };
2619
+ return await this.addStep(new LazyInvokeStep(stepName, settings));
2535
2620
  }
2536
2621
  /**
2537
2622
  * Cancel the current workflow run
@@ -2690,10 +2775,6 @@ var processRawSteps = (rawSteps) => {
2690
2775
  const stepsToDecode = encodedSteps.filter((step) => step.callType === "step");
2691
2776
  const otherSteps = stepsToDecode.map((rawStep) => {
2692
2777
  const step = JSON.parse(decodeBase64(rawStep.body));
2693
- try {
2694
- step.out = JSON.parse(step.out);
2695
- } catch {
2696
- }
2697
2778
  if (step.waitEventId) {
2698
2779
  const newOut = {
2699
2780
  eventData: step.out ? decodeBase64(step.out) : void 0,
package/solidjs.mjs CHANGED
@@ -1,7 +1,7 @@
1
1
  import {
2
2
  SDK_TELEMETRY,
3
3
  serveBase
4
- } from "./chunk-GFNR743S.mjs";
4
+ } from "./chunk-WQAJ2RSZ.mjs";
5
5
 
6
6
  // platforms/solidjs.ts
7
7
  var serve = (routeFunction, options) => {
package/svelte.d.mts CHANGED
@@ -1,7 +1,7 @@
1
1
  import * as _sveltejs_kit from '@sveltejs/kit';
2
2
  import { RequestHandler } from '@sveltejs/kit';
3
- import { R as RouteFunction, k as PublicServeOptions, t as InvokableWorkflow } from './types-CYhDXnf8.mjs';
4
- import { s as serveManyBase } from './serve-many-BVDpPsF-.mjs';
3
+ import { R as RouteFunction, k as PublicServeOptions, t as InvokableWorkflow } from './types-DS9q8FyV.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/svelte.d.ts CHANGED
@@ -1,7 +1,7 @@
1
1
  import * as _sveltejs_kit from '@sveltejs/kit';
2
2
  import { RequestHandler } from '@sveltejs/kit';
3
- import { R as RouteFunction, k as PublicServeOptions, t as InvokableWorkflow } from './types-CYhDXnf8.js';
4
- import { s as serveManyBase } from './serve-many-e4zufyXN.js';
3
+ import { R as RouteFunction, k as PublicServeOptions, t as InvokableWorkflow } from './types-DS9q8FyV.js';
4
+ import { s as serveManyBase } from './serve-many-DNnLsDIp.js';
5
5
  import '@upstash/qstash';
6
6
  import 'zod';
7
7
  import 'ai';