@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/cloudflare.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
@@ -1483,7 +1600,7 @@ var AutoExecutor = class _AutoExecutor {
1483
1600
  step,
1484
1601
  stepCount: this.stepCount
1485
1602
  });
1486
- return step.out;
1603
+ return lazyStep.parseOut(step.out);
1487
1604
  }
1488
1605
  const resultStep = await lazyStep.getResultStep(NO_CONCURRENCY, this.stepCount);
1489
1606
  await this.debug?.log("INFO", "RUN_SINGLE", {
@@ -1558,7 +1675,9 @@ var AutoExecutor = class _AutoExecutor {
1558
1675
  case "last": {
1559
1676
  const parallelResultSteps = sortedSteps.filter((step) => step.stepId >= initialStepCount).slice(0, parallelSteps.length);
1560
1677
  validateParallelSteps(parallelSteps, parallelResultSteps);
1561
- return parallelResultSteps.map((step) => step.out);
1678
+ return parallelResultSteps.map(
1679
+ (step, index) => parallelSteps[index].parseOut(step.out)
1680
+ );
1562
1681
  }
1563
1682
  }
1564
1683
  const fillValue = void 0;
@@ -2399,7 +2518,7 @@ var WorkflowContext = class {
2399
2518
  */
2400
2519
  async run(stepName, stepFunction) {
2401
2520
  const wrappedStepFunction = () => this.executor.wrapStep(stepName, stepFunction);
2402
- return this.addStep(new LazyFunctionStep(stepName, wrappedStepFunction));
2521
+ return await this.addStep(new LazyFunctionStep(stepName, wrappedStepFunction));
2403
2522
  }
2404
2523
  /**
2405
2524
  * Stops the execution for the duration provided.
@@ -2470,43 +2589,27 @@ var WorkflowContext = class {
2470
2589
  * }
2471
2590
  */
2472
2591
  async call(stepName, settings) {
2473
- const { url, method = "GET", body, headers = {}, retries = 0, timeout, flowControl } = settings;
2474
- const result = await this.addStep(
2592
+ const {
2593
+ url,
2594
+ method = "GET",
2595
+ body: requestBody,
2596
+ headers = {},
2597
+ retries = 0,
2598
+ timeout,
2599
+ flowControl
2600
+ } = settings;
2601
+ return await this.addStep(
2475
2602
  new LazyCallStep(
2476
2603
  stepName,
2477
2604
  url,
2478
2605
  method,
2479
- body,
2606
+ requestBody,
2480
2607
  headers,
2481
2608
  retries,
2482
2609
  timeout,
2483
2610
  flowControl
2484
2611
  )
2485
2612
  );
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
2613
  }
2511
2614
  /**
2512
2615
  * Pauses workflow execution until a specific event occurs or a timeout is reached.
@@ -2545,15 +2648,7 @@ var WorkflowContext = class {
2545
2648
  async waitForEvent(stepName, eventId, options = {}) {
2546
2649
  const { timeout = "7d" } = options;
2547
2650
  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
- }
2651
+ return await this.addStep(new LazyWaitForEventStep(stepName, eventId, timeoutStr));
2557
2652
  }
2558
2653
  /**
2559
2654
  * Notify workflow runs waiting for an event
@@ -2577,24 +2672,12 @@ var WorkflowContext = class {
2577
2672
  * @returns notify response which has event id, event data and list of waiters which were notified
2578
2673
  */
2579
2674
  async notify(stepName, eventId, eventData) {
2580
- const result = await this.addStep(
2675
+ return await this.addStep(
2581
2676
  new LazyNotifyStep(stepName, eventId, eventData, this.qstashClient.http)
2582
2677
  );
2583
- try {
2584
- return {
2585
- ...result,
2586
- eventData: JSON.parse(result.eventData)
2587
- };
2588
- } catch {
2589
- return result;
2590
- }
2591
2678
  }
2592
2679
  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
- };
2680
+ return await this.addStep(new LazyInvokeStep(stepName, settings));
2598
2681
  }
2599
2682
  /**
2600
2683
  * Cancel the current workflow run
@@ -2753,10 +2836,6 @@ var processRawSteps = (rawSteps) => {
2753
2836
  const stepsToDecode = encodedSteps.filter((step) => step.callType === "step");
2754
2837
  const otherSteps = stepsToDecode.map((rawStep) => {
2755
2838
  const step = JSON.parse(decodeBase64(rawStep.body));
2756
- try {
2757
- step.out = JSON.parse(step.out);
2758
- } catch {
2759
- }
2760
2839
  if (step.waitEventId) {
2761
2840
  const newOut = {
2762
2841
  eventData: step.out ? decodeBase64(step.out) : void 0,
package/cloudflare.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
  // platforms/cloudflare.ts
8
8
  var getArgs = (args) => {
package/express.d.mts CHANGED
@@ -1,7 +1,7 @@
1
1
  import * as express_serve_static_core from 'express-serve-static-core';
2
- import { R as RouteFunction, W as WorkflowServeOptions, t as InvokableWorkflow } from './types-CYhDXnf8.mjs';
2
+ import { R as RouteFunction, W as WorkflowServeOptions, t as InvokableWorkflow } from './types-Dg_9L83G.mjs';
3
3
  import { Router } from 'express';
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/express.d.ts CHANGED
@@ -1,7 +1,7 @@
1
1
  import * as express_serve_static_core from 'express-serve-static-core';
2
- import { R as RouteFunction, W as WorkflowServeOptions, t as InvokableWorkflow } from './types-CYhDXnf8.js';
2
+ import { R as RouteFunction, W as WorkflowServeOptions, t as InvokableWorkflow } from './types-Dg_9L83G.js';
3
3
  import { Router } from 'express';
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';