@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/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
@@ -1420,7 +1537,7 @@ var AutoExecutor = class _AutoExecutor {
1420
1537
  step,
1421
1538
  stepCount: this.stepCount
1422
1539
  });
1423
- return step.out;
1540
+ return lazyStep.parseOut(step.out);
1424
1541
  }
1425
1542
  const resultStep = await lazyStep.getResultStep(NO_CONCURRENCY, this.stepCount);
1426
1543
  await this.debug?.log("INFO", "RUN_SINGLE", {
@@ -1495,7 +1612,9 @@ var AutoExecutor = class _AutoExecutor {
1495
1612
  case "last": {
1496
1613
  const parallelResultSteps = sortedSteps.filter((step) => step.stepId >= initialStepCount).slice(0, parallelSteps.length);
1497
1614
  validateParallelSteps(parallelSteps, parallelResultSteps);
1498
- return parallelResultSteps.map((step) => step.out);
1615
+ return parallelResultSteps.map(
1616
+ (step, index) => parallelSteps[index].parseOut(step.out)
1617
+ );
1499
1618
  }
1500
1619
  }
1501
1620
  const fillValue = void 0;
@@ -2336,7 +2455,7 @@ var WorkflowContext = class {
2336
2455
  */
2337
2456
  async run(stepName, stepFunction) {
2338
2457
  const wrappedStepFunction = () => this.executor.wrapStep(stepName, stepFunction);
2339
- return this.addStep(new LazyFunctionStep(stepName, wrappedStepFunction));
2458
+ return await this.addStep(new LazyFunctionStep(stepName, wrappedStepFunction));
2340
2459
  }
2341
2460
  /**
2342
2461
  * Stops the execution for the duration provided.
@@ -2407,43 +2526,27 @@ var WorkflowContext = class {
2407
2526
  * }
2408
2527
  */
2409
2528
  async call(stepName, settings) {
2410
- const { url, method = "GET", body, headers = {}, retries = 0, timeout, flowControl } = settings;
2411
- const result = await this.addStep(
2529
+ const {
2530
+ url,
2531
+ method = "GET",
2532
+ body: requestBody,
2533
+ headers = {},
2534
+ retries = 0,
2535
+ timeout,
2536
+ flowControl
2537
+ } = settings;
2538
+ return await this.addStep(
2412
2539
  new LazyCallStep(
2413
2540
  stepName,
2414
2541
  url,
2415
2542
  method,
2416
- body,
2543
+ requestBody,
2417
2544
  headers,
2418
2545
  retries,
2419
2546
  timeout,
2420
2547
  flowControl
2421
2548
  )
2422
2549
  );
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
2550
  }
2448
2551
  /**
2449
2552
  * Pauses workflow execution until a specific event occurs or a timeout is reached.
@@ -2482,15 +2585,7 @@ var WorkflowContext = class {
2482
2585
  async waitForEvent(stepName, eventId, options = {}) {
2483
2586
  const { timeout = "7d" } = options;
2484
2587
  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
- }
2588
+ return await this.addStep(new LazyWaitForEventStep(stepName, eventId, timeoutStr));
2494
2589
  }
2495
2590
  /**
2496
2591
  * Notify workflow runs waiting for an event
@@ -2514,24 +2609,12 @@ var WorkflowContext = class {
2514
2609
  * @returns notify response which has event id, event data and list of waiters which were notified
2515
2610
  */
2516
2611
  async notify(stepName, eventId, eventData) {
2517
- const result = await this.addStep(
2612
+ return await this.addStep(
2518
2613
  new LazyNotifyStep(stepName, eventId, eventData, this.qstashClient.http)
2519
2614
  );
2520
- try {
2521
- return {
2522
- ...result,
2523
- eventData: JSON.parse(result.eventData)
2524
- };
2525
- } catch {
2526
- return result;
2527
- }
2528
2615
  }
2529
2616
  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
- };
2617
+ return await this.addStep(new LazyInvokeStep(stepName, settings));
2535
2618
  }
2536
2619
  /**
2537
2620
  * Cancel the current workflow run
@@ -2690,10 +2773,6 @@ var processRawSteps = (rawSteps) => {
2690
2773
  const stepsToDecode = encodedSteps.filter((step) => step.callType === "step");
2691
2774
  const otherSteps = stepsToDecode.map((rawStep) => {
2692
2775
  const step = JSON.parse(decodeBase64(rawStep.body));
2693
- try {
2694
- step.out = JSON.parse(step.out);
2695
- } catch {
2696
- }
2697
2776
  if (step.waitEventId) {
2698
2777
  const newOut = {
2699
2778
  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-IPXJZU3K.mjs";
4
+ } from "./chunk-N2WV5VCD.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-Dg_9L83G.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/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-Dg_9L83G.js';
4
+ import { s as serveManyBase } from './serve-many-jCRazho9.js';
5
5
  import '@upstash/qstash';
6
6
  import 'zod';
7
7
  import 'ai';