@upstash/workflow 0.1.3-crpyto-canary-2 → 0.1.3

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,5 +1,5 @@
1
1
  import { APIContext, APIRoute } from 'astro';
2
- import { b as WorkflowContext, W as WorkflowServeOptions } from './types-CI-2skYU.mjs';
2
+ import { b as WorkflowContext, W as WorkflowServeOptions } from './types-CQuc-j8n.mjs';
3
3
  import '@upstash/qstash';
4
4
 
5
5
  declare function serve<TInitialPayload = unknown>(routeFunction: (workflowContext: WorkflowContext<TInitialPayload>, apiContext: APIContext) => Promise<void>, options?: Omit<WorkflowServeOptions<Response, TInitialPayload>, "onStepFinish">): {
package/astro.d.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import { APIContext, APIRoute } from 'astro';
2
- import { b as WorkflowContext, W as WorkflowServeOptions } from './types-CI-2skYU.js';
2
+ import { b as WorkflowContext, W as WorkflowServeOptions } from './types-CQuc-j8n.js';
3
3
  import '@upstash/qstash';
4
4
 
5
5
  declare function serve<TInitialPayload = unknown>(routeFunction: (workflowContext: WorkflowContext<TInitialPayload>, apiContext: APIContext) => Promise<void>, options?: Omit<WorkflowServeOptions<Response, TInitialPayload>, "onStepFinish">): {
package/astro.js CHANGED
@@ -64,6 +64,190 @@ var formatWorkflowError = (error) => {
64
64
  };
65
65
  };
66
66
 
67
+ // src/client/utils.ts
68
+ var makeNotifyRequest = async (requester, eventId, eventData) => {
69
+ const result = await requester.request({
70
+ path: ["v2", "notify", eventId],
71
+ method: "POST",
72
+ body: typeof eventData === "string" ? eventData : JSON.stringify(eventData)
73
+ });
74
+ return result;
75
+ };
76
+
77
+ // src/context/steps.ts
78
+ var BaseLazyStep = class {
79
+ stepName;
80
+ // will be set in the subclasses
81
+ constructor(stepName) {
82
+ this.stepName = stepName;
83
+ }
84
+ };
85
+ var LazyFunctionStep = class extends BaseLazyStep {
86
+ stepFunction;
87
+ stepType = "Run";
88
+ constructor(stepName, stepFunction) {
89
+ super(stepName);
90
+ this.stepFunction = stepFunction;
91
+ }
92
+ getPlanStep(concurrent, targetStep) {
93
+ return {
94
+ stepId: 0,
95
+ stepName: this.stepName,
96
+ stepType: this.stepType,
97
+ concurrent,
98
+ targetStep
99
+ };
100
+ }
101
+ async getResultStep(concurrent, stepId) {
102
+ let result = this.stepFunction();
103
+ if (result instanceof Promise) {
104
+ result = await result;
105
+ }
106
+ return {
107
+ stepId,
108
+ stepName: this.stepName,
109
+ stepType: this.stepType,
110
+ out: result,
111
+ concurrent
112
+ };
113
+ }
114
+ };
115
+ var LazySleepStep = class extends BaseLazyStep {
116
+ sleep;
117
+ stepType = "SleepFor";
118
+ constructor(stepName, sleep) {
119
+ super(stepName);
120
+ this.sleep = sleep;
121
+ }
122
+ getPlanStep(concurrent, targetStep) {
123
+ return {
124
+ stepId: 0,
125
+ stepName: this.stepName,
126
+ stepType: this.stepType,
127
+ sleepFor: this.sleep,
128
+ concurrent,
129
+ targetStep
130
+ };
131
+ }
132
+ async getResultStep(concurrent, stepId) {
133
+ return await Promise.resolve({
134
+ stepId,
135
+ stepName: this.stepName,
136
+ stepType: this.stepType,
137
+ sleepFor: this.sleep,
138
+ concurrent
139
+ });
140
+ }
141
+ };
142
+ var LazySleepUntilStep = class extends BaseLazyStep {
143
+ sleepUntil;
144
+ stepType = "SleepUntil";
145
+ constructor(stepName, sleepUntil) {
146
+ super(stepName);
147
+ this.sleepUntil = sleepUntil;
148
+ }
149
+ getPlanStep(concurrent, targetStep) {
150
+ return {
151
+ stepId: 0,
152
+ stepName: this.stepName,
153
+ stepType: this.stepType,
154
+ sleepUntil: this.sleepUntil,
155
+ concurrent,
156
+ targetStep
157
+ };
158
+ }
159
+ async getResultStep(concurrent, stepId) {
160
+ return await Promise.resolve({
161
+ stepId,
162
+ stepName: this.stepName,
163
+ stepType: this.stepType,
164
+ sleepUntil: this.sleepUntil,
165
+ concurrent
166
+ });
167
+ }
168
+ };
169
+ var LazyCallStep = class extends BaseLazyStep {
170
+ url;
171
+ method;
172
+ body;
173
+ headers;
174
+ stepType = "Call";
175
+ retries;
176
+ constructor(stepName, url, method, body, headers, retries) {
177
+ super(stepName);
178
+ this.url = url;
179
+ this.method = method;
180
+ this.body = body;
181
+ this.headers = headers;
182
+ this.retries = retries;
183
+ }
184
+ getPlanStep(concurrent, targetStep) {
185
+ return {
186
+ stepId: 0,
187
+ stepName: this.stepName,
188
+ stepType: this.stepType,
189
+ concurrent,
190
+ targetStep
191
+ };
192
+ }
193
+ async getResultStep(concurrent, stepId) {
194
+ return await Promise.resolve({
195
+ stepId,
196
+ stepName: this.stepName,
197
+ stepType: this.stepType,
198
+ concurrent,
199
+ callUrl: this.url,
200
+ callMethod: this.method,
201
+ callBody: this.body,
202
+ callHeaders: this.headers
203
+ });
204
+ }
205
+ };
206
+ var LazyWaitForEventStep = class extends BaseLazyStep {
207
+ eventId;
208
+ timeout;
209
+ stepType = "Wait";
210
+ constructor(stepName, eventId, timeout) {
211
+ super(stepName);
212
+ this.eventId = eventId;
213
+ this.timeout = timeout;
214
+ }
215
+ getPlanStep(concurrent, targetStep) {
216
+ return {
217
+ stepId: 0,
218
+ stepName: this.stepName,
219
+ stepType: this.stepType,
220
+ waitEventId: this.eventId,
221
+ timeout: this.timeout,
222
+ concurrent,
223
+ targetStep
224
+ };
225
+ }
226
+ async getResultStep(concurrent, stepId) {
227
+ return await Promise.resolve({
228
+ stepId,
229
+ stepName: this.stepName,
230
+ stepType: this.stepType,
231
+ waitEventId: this.eventId,
232
+ timeout: this.timeout,
233
+ concurrent
234
+ });
235
+ }
236
+ };
237
+ var LazyNotifyStep = class extends LazyFunctionStep {
238
+ stepType = "Notify";
239
+ constructor(stepName, eventId, eventData, requester) {
240
+ super(stepName, async () => {
241
+ const notifyResponse = await makeNotifyRequest(requester, eventId, eventData);
242
+ return {
243
+ eventId,
244
+ eventData,
245
+ notifyResponse
246
+ };
247
+ });
248
+ }
249
+ };
250
+
67
251
  // node_modules/neverthrow/dist/index.es.js
68
252
  var defaultErrorConfig = {
69
253
  withStackTrace: false
@@ -519,10 +703,11 @@ var triggerFirstInvocation = async (workflowContext, retries, debug) => {
519
703
  url: workflowContext.url
520
704
  });
521
705
  try {
522
- await workflowContext.qstashClient.publishJSON({
706
+ const body = typeof workflowContext.requestPayload === "string" ? workflowContext.requestPayload : JSON.stringify(workflowContext.requestPayload);
707
+ await workflowContext.qstashClient.publish({
523
708
  headers,
524
709
  method: "POST",
525
- body: workflowContext.requestPayload,
710
+ body,
526
711
  url: workflowContext.url
527
712
  });
528
713
  return ok("success");
@@ -648,7 +833,7 @@ ${atob(callbackMessage.body)}`
648
833
  );
649
834
  }
650
835
  };
651
- var getHeaders = (initHeaderValue, workflowRunId, workflowUrl, userHeaders, step, failureUrl, retries) => {
836
+ var getHeaders = (initHeaderValue, workflowRunId, workflowUrl, userHeaders, step, failureUrl, retries, callRetries) => {
652
837
  const baseHeaders = {
653
838
  [WORKFLOW_INIT_HEADER]: initHeaderValue,
654
839
  [WORKFLOW_ID_HEADER]: workflowRunId,
@@ -664,7 +849,7 @@ var getHeaders = (initHeaderValue, workflowRunId, workflowUrl, userHeaders, step
664
849
  baseHeaders["Upstash-Failure-Callback"] = failureUrl;
665
850
  }
666
851
  if (step?.callUrl) {
667
- baseHeaders["Upstash-Retries"] = "0";
852
+ baseHeaders["Upstash-Retries"] = callRetries?.toString() ?? "0";
668
853
  baseHeaders[WORKFLOW_FEATURE_HEADER] = "WF_NoDelete";
669
854
  if (retries) {
670
855
  baseHeaders["Upstash-Callback-Retries"] = retries.toString();
@@ -672,6 +857,7 @@ var getHeaders = (initHeaderValue, workflowRunId, workflowUrl, userHeaders, step
672
857
  }
673
858
  } else if (retries !== void 0) {
674
859
  baseHeaders["Upstash-Retries"] = retries.toString();
860
+ baseHeaders["Upstash-Failure-Callback-Retries"] = retries.toString();
675
861
  }
676
862
  if (userHeaders) {
677
863
  for (const header of userHeaders.keys()) {
@@ -680,6 +866,7 @@ var getHeaders = (initHeaderValue, workflowRunId, workflowUrl, userHeaders, step
680
866
  } else {
681
867
  baseHeaders[`Upstash-Forward-${header}`] = userHeaders.get(header);
682
868
  }
869
+ baseHeaders[`Upstash-Failure-Callback-Forward-${header}`] = userHeaders.get(header);
683
870
  }
684
871
  }
685
872
  const contentType = (userHeaders ? userHeaders.get("Content-Type") : void 0) ?? DEFAULT_CONTENT_TYPE;
@@ -863,7 +1050,7 @@ var AutoExecutor = class _AutoExecutor {
863
1050
  step: resultStep,
864
1051
  stepCount: this.stepCount
865
1052
  });
866
- await this.submitStepsToQStash([resultStep]);
1053
+ await this.submitStepsToQStash([resultStep], [lazyStep]);
867
1054
  return resultStep.out;
868
1055
  }
869
1056
  /**
@@ -895,7 +1082,7 @@ var AutoExecutor = class _AutoExecutor {
895
1082
  const planSteps = parallelSteps.map(
896
1083
  (parallelStep, index) => parallelStep.getPlanStep(parallelSteps.length, initialStepCount + index)
897
1084
  );
898
- await this.submitStepsToQStash(planSteps);
1085
+ await this.submitStepsToQStash(planSteps, parallelSteps);
899
1086
  break;
900
1087
  }
901
1088
  case "partial": {
@@ -908,11 +1095,12 @@ var AutoExecutor = class _AutoExecutor {
908
1095
  const stepIndex = planStep.targetStep - initialStepCount;
909
1096
  validateStep(parallelSteps[stepIndex], planStep);
910
1097
  try {
911
- const resultStep = await parallelSteps[stepIndex].getResultStep(
1098
+ const parallelStep = parallelSteps[stepIndex];
1099
+ const resultStep = await parallelStep.getResultStep(
912
1100
  parallelSteps.length,
913
1101
  planStep.targetStep
914
1102
  );
915
- await this.submitStepsToQStash([resultStep]);
1103
+ await this.submitStepsToQStash([resultStep], [parallelStep]);
916
1104
  } catch (error) {
917
1105
  if (error instanceof QStashWorkflowAbort) {
918
1106
  throw error;
@@ -973,7 +1161,7 @@ var AutoExecutor = class _AutoExecutor {
973
1161
  *
974
1162
  * @param steps steps to send
975
1163
  */
976
- async submitStepsToQStash(steps) {
1164
+ async submitStepsToQStash(steps, lazySteps) {
977
1165
  if (steps.length === 0) {
978
1166
  throw new QStashWorkflowError(
979
1167
  `Unable to submit steps to QStash. Provided list is empty. Current step: ${this.stepCount}`
@@ -1018,7 +1206,8 @@ var AutoExecutor = class _AutoExecutor {
1018
1206
  throw new QStashWorkflowAbort(steps[0].stepName, steps[0]);
1019
1207
  }
1020
1208
  const result = await this.context.qstashClient.batchJSON(
1021
- steps.map((singleStep) => {
1209
+ steps.map((singleStep, index) => {
1210
+ const lazyStep = lazySteps[index];
1022
1211
  const { headers } = getHeaders(
1023
1212
  "false",
1024
1213
  this.context.workflowRunId,
@@ -1026,7 +1215,8 @@ var AutoExecutor = class _AutoExecutor {
1026
1215
  this.context.headers,
1027
1216
  singleStep,
1028
1217
  this.context.failureUrl,
1029
- this.context.retries
1218
+ this.context.retries,
1219
+ lazyStep instanceof LazyCallStep ? lazyStep.retries : void 0
1030
1220
  );
1031
1221
  const willWait = singleStep.concurrent === NO_CONCURRENCY || singleStep.stepId === 0;
1032
1222
  singleStep.out = JSON.stringify(singleStep.out);
@@ -1139,188 +1329,6 @@ var sortSteps = (steps) => {
1139
1329
  return [...steps].sort((step, stepOther) => getStepId(step) - getStepId(stepOther));
1140
1330
  };
1141
1331
 
1142
- // src/client/utils.ts
1143
- var makeNotifyRequest = async (requester, eventId, eventData) => {
1144
- const result = await requester.request({
1145
- path: ["v2", "notify", eventId],
1146
- method: "POST",
1147
- body: typeof eventData === "string" ? eventData : JSON.stringify(eventData)
1148
- });
1149
- return result;
1150
- };
1151
-
1152
- // src/context/steps.ts
1153
- var BaseLazyStep = class {
1154
- stepName;
1155
- // will be set in the subclasses
1156
- constructor(stepName) {
1157
- this.stepName = stepName;
1158
- }
1159
- };
1160
- var LazyFunctionStep = class extends BaseLazyStep {
1161
- stepFunction;
1162
- stepType = "Run";
1163
- constructor(stepName, stepFunction) {
1164
- super(stepName);
1165
- this.stepFunction = stepFunction;
1166
- }
1167
- getPlanStep(concurrent, targetStep) {
1168
- return {
1169
- stepId: 0,
1170
- stepName: this.stepName,
1171
- stepType: this.stepType,
1172
- concurrent,
1173
- targetStep
1174
- };
1175
- }
1176
- async getResultStep(concurrent, stepId) {
1177
- let result = this.stepFunction();
1178
- if (result instanceof Promise) {
1179
- result = await result;
1180
- }
1181
- return {
1182
- stepId,
1183
- stepName: this.stepName,
1184
- stepType: this.stepType,
1185
- out: result,
1186
- concurrent
1187
- };
1188
- }
1189
- };
1190
- var LazySleepStep = class extends BaseLazyStep {
1191
- sleep;
1192
- stepType = "SleepFor";
1193
- constructor(stepName, sleep) {
1194
- super(stepName);
1195
- this.sleep = sleep;
1196
- }
1197
- getPlanStep(concurrent, targetStep) {
1198
- return {
1199
- stepId: 0,
1200
- stepName: this.stepName,
1201
- stepType: this.stepType,
1202
- sleepFor: this.sleep,
1203
- concurrent,
1204
- targetStep
1205
- };
1206
- }
1207
- async getResultStep(concurrent, stepId) {
1208
- return await Promise.resolve({
1209
- stepId,
1210
- stepName: this.stepName,
1211
- stepType: this.stepType,
1212
- sleepFor: this.sleep,
1213
- concurrent
1214
- });
1215
- }
1216
- };
1217
- var LazySleepUntilStep = class extends BaseLazyStep {
1218
- sleepUntil;
1219
- stepType = "SleepUntil";
1220
- constructor(stepName, sleepUntil) {
1221
- super(stepName);
1222
- this.sleepUntil = sleepUntil;
1223
- }
1224
- getPlanStep(concurrent, targetStep) {
1225
- return {
1226
- stepId: 0,
1227
- stepName: this.stepName,
1228
- stepType: this.stepType,
1229
- sleepUntil: this.sleepUntil,
1230
- concurrent,
1231
- targetStep
1232
- };
1233
- }
1234
- async getResultStep(concurrent, stepId) {
1235
- return await Promise.resolve({
1236
- stepId,
1237
- stepName: this.stepName,
1238
- stepType: this.stepType,
1239
- sleepUntil: this.sleepUntil,
1240
- concurrent
1241
- });
1242
- }
1243
- };
1244
- var LazyCallStep = class extends BaseLazyStep {
1245
- url;
1246
- method;
1247
- body;
1248
- headers;
1249
- stepType = "Call";
1250
- constructor(stepName, url, method, body, headers) {
1251
- super(stepName);
1252
- this.url = url;
1253
- this.method = method;
1254
- this.body = body;
1255
- this.headers = headers;
1256
- }
1257
- getPlanStep(concurrent, targetStep) {
1258
- return {
1259
- stepId: 0,
1260
- stepName: this.stepName,
1261
- stepType: this.stepType,
1262
- concurrent,
1263
- targetStep
1264
- };
1265
- }
1266
- async getResultStep(concurrent, stepId) {
1267
- return await Promise.resolve({
1268
- stepId,
1269
- stepName: this.stepName,
1270
- stepType: this.stepType,
1271
- concurrent,
1272
- callUrl: this.url,
1273
- callMethod: this.method,
1274
- callBody: this.body,
1275
- callHeaders: this.headers
1276
- });
1277
- }
1278
- };
1279
- var LazyWaitForEventStep = class extends BaseLazyStep {
1280
- eventId;
1281
- timeout;
1282
- stepType = "Wait";
1283
- constructor(stepName, eventId, timeout) {
1284
- super(stepName);
1285
- this.eventId = eventId;
1286
- this.timeout = timeout;
1287
- }
1288
- getPlanStep(concurrent, targetStep) {
1289
- return {
1290
- stepId: 0,
1291
- stepName: this.stepName,
1292
- stepType: this.stepType,
1293
- waitEventId: this.eventId,
1294
- timeout: this.timeout,
1295
- concurrent,
1296
- targetStep
1297
- };
1298
- }
1299
- async getResultStep(concurrent, stepId) {
1300
- return await Promise.resolve({
1301
- stepId,
1302
- stepName: this.stepName,
1303
- stepType: this.stepType,
1304
- waitEventId: this.eventId,
1305
- timeout: this.timeout,
1306
- concurrent
1307
- });
1308
- }
1309
- };
1310
- var LazyNotifyStep = class extends LazyFunctionStep {
1311
- stepType = "Notify";
1312
- constructor(stepName, eventId, eventData, requester) {
1313
- super(stepName, async () => {
1314
- const notifyResponse = await makeNotifyRequest(requester, eventId, eventData);
1315
- return {
1316
- eventId,
1317
- eventData,
1318
- notifyResponse
1319
- };
1320
- });
1321
- }
1322
- };
1323
-
1324
1332
  // src/context/context.ts
1325
1333
  var WorkflowContext = class {
1326
1334
  executor;
@@ -1536,11 +1544,13 @@ var WorkflowContext = class {
1536
1544
  * network call without consuming any runtime.
1537
1545
  *
1538
1546
  * ```ts
1539
- * const postResult = await context.call<string>(
1547
+ * const { status, body } = await context.call<string>(
1540
1548
  * "post call step",
1541
- * `https://www.some-endpoint.com/api`,
1542
- * "POST",
1543
- * "my-payload"
1549
+ * {
1550
+ * url: `https://www.some-endpoint.com/api`,
1551
+ * method: "POST",
1552
+ * body: "my-payload"
1553
+ * }
1544
1554
  * );
1545
1555
  * ```
1546
1556
  *
@@ -1550,9 +1560,10 @@ var WorkflowContext = class {
1550
1560
  *
1551
1561
  * @param stepName
1552
1562
  * @param url url to call
1553
- * @param method call method
1563
+ * @param method call method. "GET" by default.
1554
1564
  * @param body call body
1555
1565
  * @param headers call headers
1566
+ * @param retries number of call retries. 0 by default
1556
1567
  * @returns call result as {
1557
1568
  * status: number;
1558
1569
  * body: unknown;
@@ -1560,9 +1571,9 @@ var WorkflowContext = class {
1560
1571
  * }
1561
1572
  */
1562
1573
  async call(stepName, settings) {
1563
- const { url, method = "GET", body, headers = {} } = settings;
1574
+ const { url, method = "GET", body, headers = {}, retries = 0 } = settings;
1564
1575
  const result = await this.addStep(
1565
- new LazyCallStep(stepName, url, method, body, headers ?? {})
1576
+ new LazyCallStep(stepName, url, method, body, headers, retries)
1566
1577
  );
1567
1578
  if (typeof result === "string") {
1568
1579
  try {
@@ -1709,11 +1720,14 @@ var WorkflowLogger = class _WorkflowLogger {
1709
1720
  };
1710
1721
 
1711
1722
  // src/utils.ts
1712
- var import_crypto = __toESM(require("crypto"));
1723
+ var import_node_crypto = __toESM(require("crypto"));
1713
1724
  var NANOID_CHARS = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-_";
1714
1725
  var NANOID_LENGTH = 21;
1715
1726
  function nanoid() {
1716
- return [...import_crypto.default.getRandomValues(new Uint8Array(NANOID_LENGTH))].map((x) => NANOID_CHARS[x % NANOID_CHARS.length]).join("");
1727
+ return [...import_node_crypto.default.getRandomValues(new Uint8Array(NANOID_LENGTH))].map((x) => NANOID_CHARS[x % NANOID_CHARS.length]).join("");
1728
+ }
1729
+ function getWorkflowRunId(id) {
1730
+ return `wfr_${id ?? nanoid()}`;
1717
1731
  }
1718
1732
  function decodeBase64(base64) {
1719
1733
  try {
@@ -1819,7 +1833,7 @@ var validateRequest = (request) => {
1819
1833
  `Incompatible workflow sdk protocol version. Expected ${WORKFLOW_PROTOCOL_VERSION}, got ${versionHeader} from the request.`
1820
1834
  );
1821
1835
  }
1822
- const workflowRunId = isFirstInvocation ? `wfr_${nanoid()}` : request.headers.get(WORKFLOW_ID_HEADER) ?? "";
1836
+ const workflowRunId = isFirstInvocation ? getWorkflowRunId() : request.headers.get(WORKFLOW_ID_HEADER) ?? "";
1823
1837
  if (workflowRunId.length === 0) {
1824
1838
  throw new QStashWorkflowError("Couldn't get workflow id from header");
1825
1839
  }
package/astro.mjs CHANGED
@@ -1,6 +1,6 @@
1
1
  import {
2
2
  serve
3
- } from "./chunk-3FILROVK.mjs";
3
+ } from "./chunk-35GJPVE2.mjs";
4
4
 
5
5
  // platforms/astro.ts
6
6
  function serve2(routeFunction, options) {