@trigger.dev/sdk 2.2.7 → 2.2.9
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/dist/index.d.ts +256 -33
- package/dist/index.js +2287 -1572
- package/dist/index.js.map +1 -1
- package/package.json +3 -3
package/dist/index.js
CHANGED
|
@@ -70,6 +70,8 @@ __export(src_exports, {
|
|
|
70
70
|
MissingConnectionNotification: () => MissingConnectionNotification,
|
|
71
71
|
MissingConnectionResolvedNotification: () => MissingConnectionResolvedNotification,
|
|
72
72
|
TriggerClient: () => TriggerClient,
|
|
73
|
+
WebhookSource: () => WebhookSource,
|
|
74
|
+
WebhookTrigger: () => WebhookTrigger,
|
|
73
75
|
cronTrigger: () => cronTrigger,
|
|
74
76
|
eventTrigger: () => eventTrigger,
|
|
75
77
|
intervalTrigger: () => intervalTrigger,
|
|
@@ -80,6 +82,7 @@ __export(src_exports, {
|
|
|
80
82
|
omit: () => omit,
|
|
81
83
|
redactString: () => redactString,
|
|
82
84
|
retry: () => retry,
|
|
85
|
+
slugifyId: () => slugifyId,
|
|
83
86
|
verifyHmacSha256: () => verifyHmacSha256,
|
|
84
87
|
verifyRequestSignature: () => verifyRequestSignature,
|
|
85
88
|
waitForEventSchema: () => waitForEventSchema
|
|
@@ -163,7 +166,11 @@ var Job = class {
|
|
|
163
166
|
startPosition: "latest",
|
|
164
167
|
enabled: this.enabled,
|
|
165
168
|
preprocessRuns: this.trigger.preprocessRuns,
|
|
166
|
-
internal
|
|
169
|
+
internal,
|
|
170
|
+
concurrencyLimit: typeof this.options.concurrencyLimit === "number" ? this.options.concurrencyLimit : typeof this.options.concurrencyLimit === "object" ? {
|
|
171
|
+
id: this.options.concurrencyLimit.id,
|
|
172
|
+
limit: this.options.concurrencyLimit.limit
|
|
173
|
+
} : void 0
|
|
167
174
|
};
|
|
168
175
|
}
|
|
169
176
|
async invoke(param1, param2 = void 0, param3 = void 0) {
|
|
@@ -292,1721 +299,2068 @@ var import_core8 = require("@trigger.dev/core");
|
|
|
292
299
|
var import_colorette = require("colorette");
|
|
293
300
|
|
|
294
301
|
// src/apiClient.ts
|
|
302
|
+
var import_core3 = require("@trigger.dev/core");
|
|
303
|
+
var import_zod3 = require("zod");
|
|
304
|
+
|
|
305
|
+
// src/io.ts
|
|
306
|
+
var import_core2 = require("@trigger.dev/core");
|
|
307
|
+
var import_core_backend = require("@trigger.dev/core-backend");
|
|
308
|
+
var import_node_async_hooks2 = require("async_hooks");
|
|
309
|
+
var import_node_crypto = require("crypto");
|
|
310
|
+
|
|
311
|
+
// src/errors.ts
|
|
312
|
+
var ResumeWithTaskError = class {
|
|
313
|
+
constructor(task) {
|
|
314
|
+
this.task = task;
|
|
315
|
+
}
|
|
316
|
+
};
|
|
317
|
+
__name(ResumeWithTaskError, "ResumeWithTaskError");
|
|
318
|
+
var ResumeWithParallelTaskError = class {
|
|
319
|
+
constructor(task, childErrors) {
|
|
320
|
+
this.task = task;
|
|
321
|
+
this.childErrors = childErrors;
|
|
322
|
+
}
|
|
323
|
+
};
|
|
324
|
+
__name(ResumeWithParallelTaskError, "ResumeWithParallelTaskError");
|
|
325
|
+
var RetryWithTaskError = class {
|
|
326
|
+
constructor(cause, task, retryAt) {
|
|
327
|
+
this.cause = cause;
|
|
328
|
+
this.task = task;
|
|
329
|
+
this.retryAt = retryAt;
|
|
330
|
+
}
|
|
331
|
+
};
|
|
332
|
+
__name(RetryWithTaskError, "RetryWithTaskError");
|
|
333
|
+
var CanceledWithTaskError = class {
|
|
334
|
+
constructor(task) {
|
|
335
|
+
this.task = task;
|
|
336
|
+
}
|
|
337
|
+
};
|
|
338
|
+
__name(CanceledWithTaskError, "CanceledWithTaskError");
|
|
339
|
+
var YieldExecutionError = class {
|
|
340
|
+
constructor(key) {
|
|
341
|
+
this.key = key;
|
|
342
|
+
}
|
|
343
|
+
};
|
|
344
|
+
__name(YieldExecutionError, "YieldExecutionError");
|
|
345
|
+
var AutoYieldExecutionError = class {
|
|
346
|
+
constructor(location, timeRemaining, timeElapsed) {
|
|
347
|
+
this.location = location;
|
|
348
|
+
this.timeRemaining = timeRemaining;
|
|
349
|
+
this.timeElapsed = timeElapsed;
|
|
350
|
+
}
|
|
351
|
+
};
|
|
352
|
+
__name(AutoYieldExecutionError, "AutoYieldExecutionError");
|
|
353
|
+
var AutoYieldWithCompletedTaskExecutionError = class {
|
|
354
|
+
constructor(id, properties, data, output) {
|
|
355
|
+
this.id = id;
|
|
356
|
+
this.properties = properties;
|
|
357
|
+
this.data = data;
|
|
358
|
+
this.output = output;
|
|
359
|
+
}
|
|
360
|
+
};
|
|
361
|
+
__name(AutoYieldWithCompletedTaskExecutionError, "AutoYieldWithCompletedTaskExecutionError");
|
|
362
|
+
var ParsedPayloadSchemaError = class {
|
|
363
|
+
constructor(schemaErrors) {
|
|
364
|
+
this.schemaErrors = schemaErrors;
|
|
365
|
+
}
|
|
366
|
+
};
|
|
367
|
+
__name(ParsedPayloadSchemaError, "ParsedPayloadSchemaError");
|
|
368
|
+
function isTriggerError(err) {
|
|
369
|
+
return err instanceof ResumeWithTaskError || err instanceof RetryWithTaskError || err instanceof CanceledWithTaskError || err instanceof YieldExecutionError || err instanceof AutoYieldExecutionError || err instanceof AutoYieldWithCompletedTaskExecutionError || err instanceof ResumeWithParallelTaskError;
|
|
370
|
+
}
|
|
371
|
+
__name(isTriggerError, "isTriggerError");
|
|
372
|
+
var ErrorWithTask = class extends Error {
|
|
373
|
+
constructor(cause, message) {
|
|
374
|
+
super(message);
|
|
375
|
+
this.cause = cause;
|
|
376
|
+
}
|
|
377
|
+
};
|
|
378
|
+
__name(ErrorWithTask, "ErrorWithTask");
|
|
379
|
+
|
|
380
|
+
// src/retry.ts
|
|
295
381
|
var import_core = require("@trigger.dev/core");
|
|
296
|
-
var
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
382
|
+
var retry = {
|
|
383
|
+
standardBackoff: {
|
|
384
|
+
limit: 8,
|
|
385
|
+
factor: 1.8,
|
|
386
|
+
minTimeoutInMs: 500,
|
|
387
|
+
maxTimeoutInMs: 3e4,
|
|
388
|
+
randomize: true
|
|
389
|
+
},
|
|
390
|
+
exponentialBackoff: {
|
|
391
|
+
limit: 8,
|
|
392
|
+
factor: 2,
|
|
393
|
+
minTimeoutInMs: 1e3,
|
|
394
|
+
maxTimeoutInMs: 3e4,
|
|
395
|
+
randomize: true
|
|
307
396
|
}
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
})
|
|
324
|
-
});
|
|
325
|
-
if (response.status >= 400 && response.status < 500) {
|
|
326
|
-
const body = await response.json();
|
|
327
|
-
throw new Error(body.error);
|
|
397
|
+
};
|
|
398
|
+
|
|
399
|
+
// src/status.ts
|
|
400
|
+
var TriggerStatus = class {
|
|
401
|
+
constructor(id, io) {
|
|
402
|
+
this.id = id;
|
|
403
|
+
this.io = io;
|
|
404
|
+
}
|
|
405
|
+
async update(key, status) {
|
|
406
|
+
const properties = [];
|
|
407
|
+
if (status.label) {
|
|
408
|
+
properties.push({
|
|
409
|
+
label: "Label",
|
|
410
|
+
text: status.label
|
|
411
|
+
});
|
|
328
412
|
}
|
|
329
|
-
if (
|
|
330
|
-
|
|
413
|
+
if (status.state) {
|
|
414
|
+
properties.push({
|
|
415
|
+
label: "State",
|
|
416
|
+
text: status.state
|
|
417
|
+
});
|
|
331
418
|
}
|
|
332
|
-
return await
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
return await zodfetchWithVersions({
|
|
340
|
-
[import_core.API_VERSIONS.LAZY_LOADED_CACHED_TASKS]: import_core.RunTaskResponseWithCachedTasksBodySchema
|
|
341
|
-
}, import_core.ServerTaskSchema, `${__privateGet(this, _apiUrl)}/api/v1/runs/${runId}/tasks`, {
|
|
342
|
-
method: "POST",
|
|
343
|
-
headers: {
|
|
344
|
-
"Content-Type": "application/json",
|
|
345
|
-
Authorization: `Bearer ${apiKey}`,
|
|
346
|
-
"Idempotency-Key": task.idempotencyKey,
|
|
347
|
-
"X-Cached-Tasks-Cursor": options.cachedTasksCursor ?? "",
|
|
348
|
-
"Trigger-Version": import_core.API_VERSIONS.LAZY_LOADED_CACHED_TASKS
|
|
419
|
+
return await this.io.runTask(key, async (task) => {
|
|
420
|
+
return await this.io.triggerClient.updateStatus(this.io.runId, this.id, status);
|
|
421
|
+
}, {
|
|
422
|
+
name: status.label ?? `Status update`,
|
|
423
|
+
icon: "bell",
|
|
424
|
+
params: {
|
|
425
|
+
...status
|
|
349
426
|
},
|
|
350
|
-
|
|
427
|
+
properties
|
|
351
428
|
});
|
|
352
429
|
}
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
430
|
+
};
|
|
431
|
+
__name(TriggerStatus, "TriggerStatus");
|
|
432
|
+
|
|
433
|
+
// src/types.ts
|
|
434
|
+
var import_zod = require("zod");
|
|
435
|
+
var EventSpecificationExampleSchema = import_zod.z.object({
|
|
436
|
+
id: import_zod.z.string(),
|
|
437
|
+
name: import_zod.z.string(),
|
|
438
|
+
icon: import_zod.z.string().optional(),
|
|
439
|
+
payload: import_zod.z.any()
|
|
440
|
+
});
|
|
441
|
+
function waitForEventSchema(schema) {
|
|
442
|
+
return import_zod.z.object({
|
|
443
|
+
id: import_zod.z.string(),
|
|
444
|
+
name: import_zod.z.string(),
|
|
445
|
+
source: import_zod.z.string(),
|
|
446
|
+
payload: schema,
|
|
447
|
+
timestamp: import_zod.z.coerce.date(),
|
|
448
|
+
context: import_zod.z.any().optional(),
|
|
449
|
+
accountId: import_zod.z.string().optional()
|
|
450
|
+
});
|
|
451
|
+
}
|
|
452
|
+
__name(waitForEventSchema, "waitForEventSchema");
|
|
453
|
+
|
|
454
|
+
// src/io.ts
|
|
455
|
+
var import_zod2 = require("zod");
|
|
456
|
+
|
|
457
|
+
// src/store/keyValueStore.ts
|
|
458
|
+
var _namespacedKey, namespacedKey_fn, _sharedProperties, sharedProperties_fn;
|
|
459
|
+
var KeyValueStore = class {
|
|
460
|
+
constructor(apiClient, type = null, namespace = "") {
|
|
461
|
+
__privateAdd(this, _namespacedKey);
|
|
462
|
+
__privateAdd(this, _sharedProperties);
|
|
463
|
+
this.apiClient = apiClient;
|
|
464
|
+
this.type = type;
|
|
465
|
+
this.namespace = namespace;
|
|
466
|
+
}
|
|
467
|
+
async delete(param1, param2) {
|
|
468
|
+
const runStore = runLocalStorage.getStore();
|
|
469
|
+
if (!runStore) {
|
|
470
|
+
if (typeof param1 !== "string") {
|
|
471
|
+
throw new Error("Please use the store without a cacheKey when accessing from outside a run.");
|
|
472
|
+
}
|
|
473
|
+
return await this.apiClient.store.delete(__privateMethod(this, _namespacedKey, namespacedKey_fn).call(this, param1));
|
|
474
|
+
}
|
|
475
|
+
const { io } = runStore;
|
|
476
|
+
if (!param2) {
|
|
477
|
+
throw new Error("Please provide a non-empty key when accessing the store from inside a run.");
|
|
478
|
+
}
|
|
479
|
+
return await io.runTask(param1, async (task) => {
|
|
480
|
+
return await this.apiClient.store.delete(__privateMethod(this, _namespacedKey, namespacedKey_fn).call(this, param2));
|
|
481
|
+
}, {
|
|
482
|
+
name: "Key-Value Store Delete",
|
|
483
|
+
icon: "database-minus",
|
|
484
|
+
params: {
|
|
485
|
+
key: param2
|
|
364
486
|
},
|
|
365
|
-
|
|
487
|
+
properties: __privateMethod(this, _sharedProperties, sharedProperties_fn).call(this, param2),
|
|
488
|
+
style: {
|
|
489
|
+
style: "minimal"
|
|
490
|
+
}
|
|
366
491
|
});
|
|
367
492
|
}
|
|
368
|
-
async
|
|
369
|
-
const
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
493
|
+
async get(param1, param2) {
|
|
494
|
+
const runStore = runLocalStorage.getStore();
|
|
495
|
+
if (!runStore) {
|
|
496
|
+
if (typeof param1 !== "string") {
|
|
497
|
+
throw new Error("Please use the store without a cacheKey when accessing from outside a run.");
|
|
498
|
+
}
|
|
499
|
+
return await this.apiClient.store.get(__privateMethod(this, _namespacedKey, namespacedKey_fn).call(this, param1));
|
|
500
|
+
}
|
|
501
|
+
const { io } = runStore;
|
|
502
|
+
if (!param2) {
|
|
503
|
+
throw new Error("Please provide a non-empty key when accessing the store from inside a run.");
|
|
504
|
+
}
|
|
505
|
+
return await io.runTask(param1, async (task) => {
|
|
506
|
+
return await this.apiClient.store.get(__privateMethod(this, _namespacedKey, namespacedKey_fn).call(this, param2));
|
|
507
|
+
}, {
|
|
508
|
+
name: "Key-Value Store Get",
|
|
509
|
+
icon: "database-export",
|
|
510
|
+
params: {
|
|
511
|
+
key: param2
|
|
380
512
|
},
|
|
381
|
-
|
|
513
|
+
properties: __privateMethod(this, _sharedProperties, sharedProperties_fn).call(this, param2),
|
|
514
|
+
style: {
|
|
515
|
+
style: "minimal"
|
|
516
|
+
}
|
|
382
517
|
});
|
|
383
518
|
}
|
|
384
|
-
async
|
|
385
|
-
const
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
519
|
+
async has(param1, param2) {
|
|
520
|
+
const runStore = runLocalStorage.getStore();
|
|
521
|
+
if (!runStore) {
|
|
522
|
+
if (typeof param1 !== "string") {
|
|
523
|
+
throw new Error("Please use the store without a cacheKey when accessing from outside a run.");
|
|
524
|
+
}
|
|
525
|
+
return await this.apiClient.store.has(__privateMethod(this, _namespacedKey, namespacedKey_fn).call(this, param1));
|
|
526
|
+
}
|
|
527
|
+
const { io } = runStore;
|
|
528
|
+
if (!param2) {
|
|
529
|
+
throw new Error("Please provide a non-empty key when accessing the store from inside a run.");
|
|
530
|
+
}
|
|
531
|
+
return await io.runTask(param1, async (task) => {
|
|
532
|
+
return await this.apiClient.store.has(__privateMethod(this, _namespacedKey, namespacedKey_fn).call(this, param2));
|
|
533
|
+
}, {
|
|
534
|
+
name: "Key-Value Store Has",
|
|
535
|
+
icon: "database-search",
|
|
536
|
+
params: {
|
|
537
|
+
key: param2
|
|
394
538
|
},
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
}
|
|
539
|
+
properties: __privateMethod(this, _sharedProperties, sharedProperties_fn).call(this, param2),
|
|
540
|
+
style: {
|
|
541
|
+
style: "minimal"
|
|
542
|
+
}
|
|
399
543
|
});
|
|
400
544
|
}
|
|
401
|
-
async
|
|
402
|
-
const
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
return await zodfetch(import_core.ApiEventLogSchema.array(), `${__privateGet(this, _apiUrl)}/api/v1/events/bulk`, {
|
|
407
|
-
method: "POST",
|
|
408
|
-
headers: {
|
|
409
|
-
"Content-Type": "application/json",
|
|
410
|
-
Authorization: `Bearer ${apiKey}`
|
|
411
|
-
},
|
|
412
|
-
body: JSON.stringify({
|
|
413
|
-
events,
|
|
414
|
-
options
|
|
415
|
-
})
|
|
416
|
-
});
|
|
417
|
-
}
|
|
418
|
-
async cancelEvent(eventId) {
|
|
419
|
-
const apiKey = await __privateMethod(this, _apiKey, apiKey_fn).call(this);
|
|
420
|
-
__privateGet(this, _logger).debug("Cancelling event", {
|
|
421
|
-
eventId
|
|
422
|
-
});
|
|
423
|
-
return await zodfetch(import_core.ApiEventLogSchema, `${__privateGet(this, _apiUrl)}/api/v1/events/${eventId}/cancel`, {
|
|
424
|
-
method: "POST",
|
|
425
|
-
headers: {
|
|
426
|
-
"Content-Type": "application/json",
|
|
427
|
-
Authorization: `Bearer ${apiKey}`
|
|
545
|
+
async set(param1, param2, param3) {
|
|
546
|
+
const runStore = runLocalStorage.getStore();
|
|
547
|
+
if (!runStore) {
|
|
548
|
+
if (typeof param1 !== "string") {
|
|
549
|
+
throw new Error("Please use the store without a cacheKey when accessing from outside a run.");
|
|
428
550
|
}
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
return await
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
551
|
+
return await this.apiClient.store.set(__privateMethod(this, _namespacedKey, namespacedKey_fn).call(this, param1), param2);
|
|
552
|
+
}
|
|
553
|
+
const { io } = runStore;
|
|
554
|
+
if (!param2 || typeof param2 !== "string") {
|
|
555
|
+
throw new Error("Please provide a non-empty key when accessing the store from inside a run.");
|
|
556
|
+
}
|
|
557
|
+
const value = param3;
|
|
558
|
+
return await io.runTask(param1, async (task) => {
|
|
559
|
+
return await this.apiClient.store.set(__privateMethod(this, _namespacedKey, namespacedKey_fn).call(this, param2), value);
|
|
560
|
+
}, {
|
|
561
|
+
name: "Key-Value Store Set",
|
|
562
|
+
icon: "database-plus",
|
|
563
|
+
params: {
|
|
564
|
+
key: param2,
|
|
565
|
+
value
|
|
566
|
+
},
|
|
567
|
+
properties: [
|
|
568
|
+
...__privateMethod(this, _sharedProperties, sharedProperties_fn).call(this, param2),
|
|
569
|
+
...typeof value !== "object" || value === null ? [
|
|
570
|
+
{
|
|
571
|
+
label: "value",
|
|
572
|
+
text: String(value) ?? "undefined"
|
|
573
|
+
}
|
|
574
|
+
] : []
|
|
575
|
+
],
|
|
576
|
+
style: {
|
|
577
|
+
style: "minimal"
|
|
441
578
|
}
|
|
442
579
|
});
|
|
443
580
|
}
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
581
|
+
};
|
|
582
|
+
__name(KeyValueStore, "KeyValueStore");
|
|
583
|
+
_namespacedKey = new WeakSet();
|
|
584
|
+
namespacedKey_fn = /* @__PURE__ */ __name(function(key) {
|
|
585
|
+
const parts = [];
|
|
586
|
+
if (this.type) {
|
|
587
|
+
parts.push(this.type);
|
|
588
|
+
}
|
|
589
|
+
if (this.namespace) {
|
|
590
|
+
parts.push(this.namespace);
|
|
591
|
+
}
|
|
592
|
+
parts.push(key);
|
|
593
|
+
return parts.join(":");
|
|
594
|
+
}, "#namespacedKey");
|
|
595
|
+
_sharedProperties = new WeakSet();
|
|
596
|
+
sharedProperties_fn = /* @__PURE__ */ __name(function(key1) {
|
|
597
|
+
return [
|
|
598
|
+
{
|
|
599
|
+
label: "namespace",
|
|
600
|
+
text: this.type ?? "env"
|
|
601
|
+
},
|
|
602
|
+
{
|
|
603
|
+
label: "key",
|
|
604
|
+
text: key1
|
|
605
|
+
}
|
|
606
|
+
];
|
|
607
|
+
}, "#sharedProperties");
|
|
608
|
+
|
|
609
|
+
// src/io.ts
|
|
610
|
+
var JSONOutputSerializer = class {
|
|
611
|
+
serialize(value) {
|
|
612
|
+
return JSON.stringify(value);
|
|
458
613
|
}
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
__privateGet(this, _logger).debug("activating http source", {
|
|
462
|
-
source
|
|
463
|
-
});
|
|
464
|
-
const response = await zodfetch(import_core.TriggerSourceSchema, `${__privateGet(this, _apiUrl)}/api/v2/${client}/sources/${key}`, {
|
|
465
|
-
method: "PUT",
|
|
466
|
-
headers: {
|
|
467
|
-
"Content-Type": "application/json",
|
|
468
|
-
Authorization: `Bearer ${apiKey}`
|
|
469
|
-
},
|
|
470
|
-
body: JSON.stringify(source)
|
|
471
|
-
});
|
|
472
|
-
return response;
|
|
614
|
+
deserialize(value) {
|
|
615
|
+
return value ? JSON.parse(value) : void 0;
|
|
473
616
|
}
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
617
|
+
};
|
|
618
|
+
__name(JSONOutputSerializer, "JSONOutputSerializer");
|
|
619
|
+
var _addToCachedTasks, addToCachedTasks_fn, _detectAutoYield, detectAutoYield_fn, _forceYield, forceYield_fn, _getTimeElapsed, getTimeElapsed_fn, _getRemainingTimeInMillis, getRemainingTimeInMillis_fn;
|
|
620
|
+
var IO = class {
|
|
621
|
+
constructor(options) {
|
|
622
|
+
__privateAdd(this, _addToCachedTasks);
|
|
623
|
+
__privateAdd(this, _detectAutoYield);
|
|
624
|
+
__privateAdd(this, _forceYield);
|
|
625
|
+
__privateAdd(this, _getTimeElapsed);
|
|
626
|
+
__privateAdd(this, _getRemainingTimeInMillis);
|
|
627
|
+
__publicField(this, "_outputSerializer", new JSONOutputSerializer());
|
|
628
|
+
__publicField(this, "_visitedCacheKeys", /* @__PURE__ */ new Set());
|
|
629
|
+
__publicField(this, "brb", this.yield.bind(this));
|
|
630
|
+
this._id = options.id;
|
|
631
|
+
this._jobId = options.jobId;
|
|
632
|
+
this._apiClient = options.apiClient;
|
|
633
|
+
this._triggerClient = options.client;
|
|
634
|
+
this._logger = options.logger ?? new import_core2.Logger("trigger.dev", options.logLevel);
|
|
635
|
+
this._cachedTasks = /* @__PURE__ */ new Map();
|
|
636
|
+
this._jobLogger = options.jobLogger;
|
|
637
|
+
this._jobLogLevel = options.jobLogLevel;
|
|
638
|
+
this._timeOrigin = options.timeOrigin;
|
|
639
|
+
this._executionTimeout = options.executionTimeout;
|
|
640
|
+
this._envStore = new KeyValueStore(options.apiClient);
|
|
641
|
+
this._jobStore = new KeyValueStore(options.apiClient, "job", options.jobId);
|
|
642
|
+
this._runStore = new KeyValueStore(options.apiClient, "run", options.id);
|
|
643
|
+
this._stats = {
|
|
644
|
+
initialCachedTasks: 0,
|
|
645
|
+
lazyLoadedCachedTasks: 0,
|
|
646
|
+
executedTasks: 0,
|
|
647
|
+
cachedTaskHits: 0,
|
|
648
|
+
cachedTaskMisses: 0,
|
|
649
|
+
noopCachedTaskHits: 0,
|
|
650
|
+
noopCachedTaskMisses: 0
|
|
483
651
|
};
|
|
484
|
-
if (
|
|
485
|
-
|
|
652
|
+
if (options.cachedTasks) {
|
|
653
|
+
options.cachedTasks.forEach((task) => {
|
|
654
|
+
this._cachedTasks.set(task.idempotencyKey, task);
|
|
655
|
+
});
|
|
656
|
+
this._stats.initialCachedTasks = options.cachedTasks.length;
|
|
486
657
|
}
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
658
|
+
this._taskStorage = new import_node_async_hooks2.AsyncLocalStorage();
|
|
659
|
+
this._context = options.context;
|
|
660
|
+
this._yieldedExecutions = options.yieldedExecutions ?? [];
|
|
661
|
+
if (options.noopTasksSet) {
|
|
662
|
+
this._noopTasksBloomFilter = import_core_backend.BloomFilter.deserialize(options.noopTasksSet, import_core_backend.BloomFilter.NOOP_TASK_SET_SIZE);
|
|
663
|
+
}
|
|
664
|
+
this._cachedTasksCursor = options.cachedTasksCursor;
|
|
665
|
+
this._serverVersion = options.serverVersion ?? "unversioned";
|
|
493
666
|
}
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
__privateGet(this, _logger).debug("registering schedule", {
|
|
497
|
-
id,
|
|
498
|
-
payload
|
|
499
|
-
});
|
|
500
|
-
const response = await zodfetch(import_core.RegisterScheduleResponseBodySchema, `${__privateGet(this, _apiUrl)}/api/v1/${client}/schedules/${id}/registrations`, {
|
|
501
|
-
method: "POST",
|
|
502
|
-
headers: {
|
|
503
|
-
"Content-Type": "application/json",
|
|
504
|
-
Authorization: `Bearer ${apiKey}`
|
|
505
|
-
},
|
|
506
|
-
body: JSON.stringify({
|
|
507
|
-
id: key,
|
|
508
|
-
...payload
|
|
509
|
-
})
|
|
510
|
-
});
|
|
511
|
-
return response;
|
|
667
|
+
get stats() {
|
|
668
|
+
return this._stats;
|
|
512
669
|
}
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
__privateGet(this, _logger).debug("unregistering schedule", {
|
|
516
|
-
id
|
|
517
|
-
});
|
|
518
|
-
const response = await zodfetch(import_zod.z.object({
|
|
519
|
-
ok: import_zod.z.boolean()
|
|
520
|
-
}), `${__privateGet(this, _apiUrl)}/api/v1/${client}/schedules/${id}/registrations/${encodeURIComponent(key)}`, {
|
|
521
|
-
method: "DELETE",
|
|
522
|
-
headers: {
|
|
523
|
-
"Content-Type": "application/json",
|
|
524
|
-
Authorization: `Bearer ${apiKey}`
|
|
525
|
-
}
|
|
526
|
-
});
|
|
527
|
-
return response;
|
|
670
|
+
get runId() {
|
|
671
|
+
return this._id;
|
|
528
672
|
}
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
__privateGet(this, _logger).debug("getting auth", {
|
|
532
|
-
id
|
|
533
|
-
});
|
|
534
|
-
const response = await zodfetch(import_core.ConnectionAuthSchema, `${__privateGet(this, _apiUrl)}/api/v1/${client}/auth/${id}`, {
|
|
535
|
-
method: "GET",
|
|
536
|
-
headers: {
|
|
537
|
-
Accept: "application/json",
|
|
538
|
-
Authorization: `Bearer ${apiKey}`
|
|
539
|
-
}
|
|
540
|
-
}, {
|
|
541
|
-
optional: true
|
|
542
|
-
});
|
|
543
|
-
return response;
|
|
673
|
+
get triggerClient() {
|
|
674
|
+
return this._triggerClient;
|
|
544
675
|
}
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
676
|
+
get logger() {
|
|
677
|
+
return new IOLogger(async (level, message, data) => {
|
|
678
|
+
let logLevel = "info";
|
|
679
|
+
if (import_core2.Logger.satisfiesLogLevel(logLevel, this._jobLogLevel)) {
|
|
680
|
+
await this.runTask([
|
|
681
|
+
message,
|
|
682
|
+
level
|
|
683
|
+
], async (task) => {
|
|
684
|
+
switch (level) {
|
|
685
|
+
case "LOG": {
|
|
686
|
+
this._jobLogger?.log(message, data);
|
|
687
|
+
logLevel = "log";
|
|
688
|
+
break;
|
|
689
|
+
}
|
|
690
|
+
case "DEBUG": {
|
|
691
|
+
this._jobLogger?.debug(message, data);
|
|
692
|
+
logLevel = "debug";
|
|
693
|
+
break;
|
|
694
|
+
}
|
|
695
|
+
case "INFO": {
|
|
696
|
+
this._jobLogger?.info(message, data);
|
|
697
|
+
logLevel = "info";
|
|
698
|
+
break;
|
|
699
|
+
}
|
|
700
|
+
case "WARN": {
|
|
701
|
+
this._jobLogger?.warn(message, data);
|
|
702
|
+
logLevel = "warn";
|
|
703
|
+
break;
|
|
704
|
+
}
|
|
705
|
+
case "ERROR": {
|
|
706
|
+
this._jobLogger?.error(message, data);
|
|
707
|
+
logLevel = "error";
|
|
708
|
+
break;
|
|
709
|
+
}
|
|
710
|
+
}
|
|
711
|
+
}, {
|
|
712
|
+
name: "log",
|
|
713
|
+
icon: "log",
|
|
714
|
+
description: message,
|
|
715
|
+
params: data,
|
|
716
|
+
properties: [
|
|
717
|
+
{
|
|
718
|
+
label: "Level",
|
|
719
|
+
text: level
|
|
720
|
+
}
|
|
721
|
+
],
|
|
722
|
+
style: {
|
|
723
|
+
style: "minimal",
|
|
724
|
+
variant: level.toLowerCase()
|
|
725
|
+
},
|
|
726
|
+
noop: true
|
|
727
|
+
});
|
|
554
728
|
}
|
|
555
729
|
});
|
|
556
730
|
}
|
|
557
|
-
async
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
});
|
|
562
|
-
return await zodfetch(import_core.GetRunSchema, (0, import_core.urlWithSearchParams)(`${__privateGet(this, _apiUrl)}/api/v1/runs/${runId}`, options), {
|
|
563
|
-
method: "GET",
|
|
564
|
-
headers: {
|
|
565
|
-
Authorization: `Bearer ${apiKey}`
|
|
731
|
+
async random(cacheKey, { min = 0, max = 1, round = false } = {}) {
|
|
732
|
+
return await this.runTask(cacheKey, async (task) => {
|
|
733
|
+
if (min > max) {
|
|
734
|
+
throw new Error(`Lower bound can't be higher than upper bound - min: ${min}, max: ${max}`);
|
|
566
735
|
}
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
736
|
+
if (min === max) {
|
|
737
|
+
await this.logger.warn(`Lower and upper bounds are identical. The return value is not random and will always be: ${min}`);
|
|
738
|
+
}
|
|
739
|
+
const withinBounds = (max - min) * Math.random() + min;
|
|
740
|
+
if (!round) {
|
|
741
|
+
return withinBounds;
|
|
742
|
+
}
|
|
743
|
+
if (!Number.isInteger(min) || !Number.isInteger(max)) {
|
|
744
|
+
await this.logger.warn("Rounding enabled with floating-point bounds. This may cause unexpected skew and boundary inclusivity.");
|
|
745
|
+
}
|
|
746
|
+
const rounded = Math.round(withinBounds);
|
|
747
|
+
return rounded;
|
|
748
|
+
}, {
|
|
749
|
+
name: "random",
|
|
750
|
+
icon: "dice-5-filled",
|
|
751
|
+
params: {
|
|
752
|
+
min,
|
|
753
|
+
max,
|
|
754
|
+
round
|
|
755
|
+
},
|
|
756
|
+
properties: [
|
|
757
|
+
...min === 0 ? [] : [
|
|
758
|
+
{
|
|
759
|
+
label: "min",
|
|
760
|
+
text: String(min)
|
|
761
|
+
}
|
|
762
|
+
],
|
|
763
|
+
...max === 1 ? [] : [
|
|
764
|
+
{
|
|
765
|
+
label: "max",
|
|
766
|
+
text: String(max)
|
|
767
|
+
}
|
|
768
|
+
],
|
|
769
|
+
...round === false ? [] : [
|
|
770
|
+
{
|
|
771
|
+
label: "round",
|
|
772
|
+
text: String(round)
|
|
773
|
+
}
|
|
774
|
+
]
|
|
775
|
+
],
|
|
776
|
+
style: {
|
|
777
|
+
style: "minimal"
|
|
579
778
|
}
|
|
580
779
|
});
|
|
581
780
|
}
|
|
582
|
-
async
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
781
|
+
async wait(cacheKey, seconds) {
|
|
782
|
+
return await this.runTask(cacheKey, async (task) => {
|
|
783
|
+
}, {
|
|
784
|
+
name: "wait",
|
|
785
|
+
icon: "clock",
|
|
786
|
+
params: {
|
|
787
|
+
seconds
|
|
788
|
+
},
|
|
789
|
+
noop: true,
|
|
790
|
+
delayUntil: new Date(Date.now() + seconds * 1e3),
|
|
791
|
+
style: {
|
|
792
|
+
style: "minimal"
|
|
591
793
|
}
|
|
592
794
|
});
|
|
593
795
|
}
|
|
594
|
-
async
|
|
595
|
-
const
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
796
|
+
async waitForEvent(cacheKey, event, options) {
|
|
797
|
+
const timeoutInSeconds = options?.timeoutInSeconds ?? 60 * 60;
|
|
798
|
+
return await this.runTask(cacheKey, async (task, io) => {
|
|
799
|
+
if (!task.callbackUrl) {
|
|
800
|
+
throw new Error("No callbackUrl found on task");
|
|
801
|
+
}
|
|
802
|
+
await this.triggerClient.createEphemeralEventDispatcher({
|
|
803
|
+
url: task.callbackUrl,
|
|
804
|
+
name: event.name,
|
|
805
|
+
filter: event.filter,
|
|
806
|
+
contextFilter: event.contextFilter,
|
|
807
|
+
source: event.source,
|
|
808
|
+
accountId: event.accountId,
|
|
809
|
+
timeoutInSeconds
|
|
810
|
+
});
|
|
811
|
+
return {};
|
|
812
|
+
}, {
|
|
813
|
+
name: "Wait for Event",
|
|
814
|
+
icon: "custom-event",
|
|
815
|
+
params: {
|
|
816
|
+
name: event.name,
|
|
817
|
+
source: event.source,
|
|
818
|
+
filter: event.filter,
|
|
819
|
+
contextFilter: event.contextFilter,
|
|
820
|
+
accountId: event.accountId
|
|
821
|
+
},
|
|
822
|
+
callback: {
|
|
823
|
+
enabled: true,
|
|
824
|
+
timeoutInSeconds
|
|
825
|
+
},
|
|
826
|
+
properties: [
|
|
827
|
+
{
|
|
828
|
+
label: "Event",
|
|
829
|
+
text: event.name
|
|
830
|
+
},
|
|
831
|
+
{
|
|
832
|
+
label: "Timeout",
|
|
833
|
+
text: `${timeoutInSeconds}s`
|
|
834
|
+
},
|
|
835
|
+
...event.source ? [
|
|
836
|
+
{
|
|
837
|
+
label: "Source",
|
|
838
|
+
text: event.source
|
|
839
|
+
}
|
|
840
|
+
] : [],
|
|
841
|
+
...event.accountId ? [
|
|
842
|
+
{
|
|
843
|
+
label: "Account ID",
|
|
844
|
+
text: event.accountId
|
|
845
|
+
}
|
|
846
|
+
] : []
|
|
847
|
+
],
|
|
848
|
+
parseOutput: (output) => {
|
|
849
|
+
return waitForEventSchema(event.schema ?? import_zod2.z.any()).parse(output);
|
|
603
850
|
}
|
|
604
851
|
});
|
|
605
852
|
}
|
|
606
|
-
async
|
|
607
|
-
const
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
const body = {
|
|
612
|
-
payload,
|
|
613
|
-
context: options.context ?? {},
|
|
614
|
-
options: {
|
|
615
|
-
accountId: options.accountId,
|
|
616
|
-
callbackUrl: options.callbackUrl
|
|
853
|
+
async waitForRequest(cacheKey, callback, options) {
|
|
854
|
+
const timeoutInSeconds = options?.timeoutInSeconds ?? 60 * 60;
|
|
855
|
+
return await this.runTask(cacheKey, async (task, io) => {
|
|
856
|
+
if (!task.callbackUrl) {
|
|
857
|
+
throw new Error("No callbackUrl found on task");
|
|
617
858
|
}
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
859
|
+
task.outputProperties = [
|
|
860
|
+
{
|
|
861
|
+
label: "Callback URL",
|
|
862
|
+
text: task.callbackUrl
|
|
863
|
+
}
|
|
864
|
+
];
|
|
865
|
+
return callback(task.callbackUrl);
|
|
866
|
+
}, {
|
|
867
|
+
name: "Wait for Request",
|
|
868
|
+
icon: "clock",
|
|
869
|
+
callback: {
|
|
870
|
+
enabled: true,
|
|
871
|
+
timeoutInSeconds: options?.timeoutInSeconds
|
|
627
872
|
},
|
|
628
|
-
|
|
873
|
+
properties: [
|
|
874
|
+
{
|
|
875
|
+
label: "Timeout",
|
|
876
|
+
text: `${timeoutInSeconds}s`
|
|
877
|
+
}
|
|
878
|
+
]
|
|
629
879
|
});
|
|
630
880
|
}
|
|
631
|
-
async
|
|
632
|
-
const
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
|
|
881
|
+
async createStatus(cacheKey, initialStatus) {
|
|
882
|
+
const id = typeof cacheKey === "string" ? cacheKey : cacheKey.join("-");
|
|
883
|
+
const status = new TriggerStatus(id, this);
|
|
884
|
+
await status.update(cacheKey, initialStatus);
|
|
885
|
+
return status;
|
|
886
|
+
}
|
|
887
|
+
async backgroundFetch(cacheKey, url, requestInit, options) {
|
|
888
|
+
const urlObject = new URL(url);
|
|
889
|
+
return await this.runTask(cacheKey, async (task) => {
|
|
890
|
+
console.log("task context", task.context);
|
|
891
|
+
return task.output;
|
|
892
|
+
}, {
|
|
893
|
+
name: `fetch ${urlObject.hostname}${urlObject.pathname}`,
|
|
894
|
+
params: {
|
|
895
|
+
url,
|
|
896
|
+
requestInit,
|
|
897
|
+
retry: options?.retry,
|
|
898
|
+
timeout: options?.timeout
|
|
641
899
|
},
|
|
642
|
-
|
|
900
|
+
operation: "fetch",
|
|
901
|
+
icon: "background",
|
|
902
|
+
noop: false,
|
|
903
|
+
properties: [
|
|
904
|
+
{
|
|
905
|
+
label: "url",
|
|
906
|
+
text: url,
|
|
907
|
+
url
|
|
908
|
+
},
|
|
909
|
+
{
|
|
910
|
+
label: "method",
|
|
911
|
+
text: requestInit?.method ?? "GET"
|
|
912
|
+
},
|
|
913
|
+
{
|
|
914
|
+
label: "background",
|
|
915
|
+
text: "true"
|
|
916
|
+
},
|
|
917
|
+
...options?.timeout ? [
|
|
918
|
+
{
|
|
919
|
+
label: "timeout",
|
|
920
|
+
text: `${options.timeout.durationInMs}ms`
|
|
921
|
+
}
|
|
922
|
+
] : []
|
|
923
|
+
],
|
|
924
|
+
retry: {
|
|
925
|
+
limit: 0
|
|
926
|
+
}
|
|
643
927
|
});
|
|
644
|
-
return response;
|
|
645
928
|
}
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
},
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
|
|
666
|
-
|
|
667
|
-
|
|
668
|
-
|
|
669
|
-
|
|
670
|
-
|
|
671
|
-
|
|
672
|
-
|
|
673
|
-
};
|
|
674
|
-
}
|
|
675
|
-
return {
|
|
676
|
-
status: "valid",
|
|
677
|
-
apiKey
|
|
678
|
-
};
|
|
679
|
-
}
|
|
680
|
-
__name(getApiKey, "getApiKey");
|
|
681
|
-
async function zodfetchWithVersions(versionedSchemaMap, unversionedSchema, url, requestInit, options, retryCount = 0) {
|
|
682
|
-
const response = await fetch(url, {
|
|
683
|
-
...requestInit,
|
|
684
|
-
cache: "no-cache"
|
|
685
|
-
});
|
|
686
|
-
if ((!requestInit || requestInit.method === "GET") && response.status === 404 && options?.optional) {
|
|
687
|
-
return;
|
|
929
|
+
async backgroundPoll(cacheKey, params) {
|
|
930
|
+
const urlObject = new URL(params.url);
|
|
931
|
+
return await this.runTask(cacheKey, async (task) => {
|
|
932
|
+
return task.output;
|
|
933
|
+
}, {
|
|
934
|
+
name: `poll ${urlObject.hostname}${urlObject.pathname}`,
|
|
935
|
+
params,
|
|
936
|
+
operation: "fetch-poll",
|
|
937
|
+
icon: "clock-bolt",
|
|
938
|
+
noop: false,
|
|
939
|
+
properties: [
|
|
940
|
+
{
|
|
941
|
+
label: "url",
|
|
942
|
+
text: params.url
|
|
943
|
+
},
|
|
944
|
+
{
|
|
945
|
+
label: "interval",
|
|
946
|
+
text: `${params.interval}s`
|
|
947
|
+
},
|
|
948
|
+
{
|
|
949
|
+
label: "timeout",
|
|
950
|
+
text: `${params.timeout}s`
|
|
951
|
+
}
|
|
952
|
+
],
|
|
953
|
+
retry: {
|
|
954
|
+
limit: 0
|
|
955
|
+
}
|
|
956
|
+
});
|
|
688
957
|
}
|
|
689
|
-
|
|
690
|
-
const
|
|
691
|
-
|
|
958
|
+
async backgroundFetchResponse(cacheKey, url, requestInit, options) {
|
|
959
|
+
const urlObject = new URL(url);
|
|
960
|
+
return await this.runTask(cacheKey, async (task) => {
|
|
961
|
+
return task.output;
|
|
962
|
+
}, {
|
|
963
|
+
name: `fetch response ${urlObject.hostname}${urlObject.pathname}`,
|
|
964
|
+
params: {
|
|
965
|
+
url,
|
|
966
|
+
requestInit,
|
|
967
|
+
retry: options?.retry,
|
|
968
|
+
timeout: options?.timeout
|
|
969
|
+
},
|
|
970
|
+
operation: "fetch-response",
|
|
971
|
+
icon: "background",
|
|
972
|
+
noop: false,
|
|
973
|
+
properties: [
|
|
974
|
+
{
|
|
975
|
+
label: "url",
|
|
976
|
+
text: url,
|
|
977
|
+
url
|
|
978
|
+
},
|
|
979
|
+
{
|
|
980
|
+
label: "method",
|
|
981
|
+
text: requestInit?.method ?? "GET"
|
|
982
|
+
},
|
|
983
|
+
{
|
|
984
|
+
label: "background",
|
|
985
|
+
text: "true"
|
|
986
|
+
},
|
|
987
|
+
...options?.timeout ? [
|
|
988
|
+
{
|
|
989
|
+
label: "timeout",
|
|
990
|
+
text: `${options.timeout.durationInMs}ms`
|
|
991
|
+
}
|
|
992
|
+
] : []
|
|
993
|
+
],
|
|
994
|
+
retry: {
|
|
995
|
+
limit: 0
|
|
996
|
+
}
|
|
997
|
+
});
|
|
692
998
|
}
|
|
693
|
-
|
|
694
|
-
|
|
695
|
-
|
|
696
|
-
|
|
999
|
+
async sendEvent(cacheKey, event, options) {
|
|
1000
|
+
return await this.runTask(cacheKey, async (task) => {
|
|
1001
|
+
return await this._triggerClient.sendEvent(event, options);
|
|
1002
|
+
}, {
|
|
1003
|
+
name: "Send Event",
|
|
1004
|
+
params: {
|
|
1005
|
+
event,
|
|
1006
|
+
options
|
|
1007
|
+
},
|
|
1008
|
+
icon: "send",
|
|
1009
|
+
properties: [
|
|
1010
|
+
{
|
|
1011
|
+
label: "name",
|
|
1012
|
+
text: event.name
|
|
1013
|
+
},
|
|
1014
|
+
...event?.id ? [
|
|
1015
|
+
{
|
|
1016
|
+
label: "ID",
|
|
1017
|
+
text: event.id
|
|
1018
|
+
}
|
|
1019
|
+
] : [],
|
|
1020
|
+
...sendEventOptionsProperties(options)
|
|
1021
|
+
]
|
|
1022
|
+
});
|
|
697
1023
|
}
|
|
698
|
-
|
|
699
|
-
|
|
1024
|
+
async sendEvents(cacheKey, events, options) {
|
|
1025
|
+
return await this.runTask(cacheKey, async (task) => {
|
|
1026
|
+
return await this._triggerClient.sendEvents(events, options);
|
|
1027
|
+
}, {
|
|
1028
|
+
name: "Send Multiple Events",
|
|
1029
|
+
params: {
|
|
1030
|
+
events,
|
|
1031
|
+
options
|
|
1032
|
+
},
|
|
1033
|
+
icon: "send",
|
|
1034
|
+
properties: [
|
|
1035
|
+
{
|
|
1036
|
+
label: "Total Events",
|
|
1037
|
+
text: String(events.length)
|
|
1038
|
+
},
|
|
1039
|
+
...sendEventOptionsProperties(options)
|
|
1040
|
+
]
|
|
1041
|
+
});
|
|
700
1042
|
}
|
|
701
|
-
|
|
702
|
-
|
|
703
|
-
|
|
704
|
-
|
|
705
|
-
|
|
706
|
-
|
|
707
|
-
|
|
1043
|
+
async getEvent(cacheKey, id) {
|
|
1044
|
+
return await this.runTask(cacheKey, async (task) => {
|
|
1045
|
+
return await this._triggerClient.getEvent(id);
|
|
1046
|
+
}, {
|
|
1047
|
+
name: "getEvent",
|
|
1048
|
+
params: {
|
|
1049
|
+
id
|
|
1050
|
+
},
|
|
1051
|
+
properties: [
|
|
1052
|
+
{
|
|
1053
|
+
label: "id",
|
|
1054
|
+
text: id
|
|
1055
|
+
}
|
|
1056
|
+
]
|
|
1057
|
+
});
|
|
708
1058
|
}
|
|
709
|
-
|
|
710
|
-
|
|
711
|
-
|
|
1059
|
+
async cancelEvent(cacheKey, eventId) {
|
|
1060
|
+
return await this.runTask(cacheKey, async (task) => {
|
|
1061
|
+
return await this._triggerClient.cancelEvent(eventId);
|
|
1062
|
+
}, {
|
|
1063
|
+
name: "cancelEvent",
|
|
1064
|
+
params: {
|
|
1065
|
+
eventId
|
|
1066
|
+
},
|
|
1067
|
+
properties: [
|
|
1068
|
+
{
|
|
1069
|
+
label: "id",
|
|
1070
|
+
text: eventId
|
|
1071
|
+
}
|
|
1072
|
+
]
|
|
1073
|
+
});
|
|
712
1074
|
}
|
|
713
|
-
|
|
714
|
-
|
|
715
|
-
|
|
716
|
-
|
|
717
|
-
|
|
718
|
-
|
|
719
|
-
|
|
720
|
-
|
|
721
|
-
|
|
722
|
-
|
|
723
|
-
|
|
724
|
-
|
|
725
|
-
|
|
1075
|
+
async updateSource(cacheKey, options) {
|
|
1076
|
+
return this.runTask(cacheKey, async (task) => {
|
|
1077
|
+
return await this._apiClient.updateSource(this._triggerClient.id, options.key, options);
|
|
1078
|
+
}, {
|
|
1079
|
+
name: "Update Source",
|
|
1080
|
+
description: "Update Source",
|
|
1081
|
+
properties: [
|
|
1082
|
+
{
|
|
1083
|
+
label: "key",
|
|
1084
|
+
text: options.key
|
|
1085
|
+
}
|
|
1086
|
+
],
|
|
1087
|
+
params: options,
|
|
1088
|
+
redact: {
|
|
1089
|
+
paths: [
|
|
1090
|
+
"secret"
|
|
1091
|
+
]
|
|
1092
|
+
}
|
|
1093
|
+
});
|
|
726
1094
|
}
|
|
727
|
-
|
|
728
|
-
|
|
729
|
-
|
|
1095
|
+
async updateWebhook(cacheKey, options) {
|
|
1096
|
+
return this.runTask(cacheKey, async (task) => {
|
|
1097
|
+
return await this._apiClient.updateWebhook(options.key, options);
|
|
1098
|
+
}, {
|
|
1099
|
+
name: "Update Webhook Source",
|
|
1100
|
+
icon: "refresh",
|
|
1101
|
+
properties: [
|
|
1102
|
+
{
|
|
1103
|
+
label: "key",
|
|
1104
|
+
text: options.key
|
|
1105
|
+
}
|
|
1106
|
+
],
|
|
1107
|
+
params: options
|
|
1108
|
+
});
|
|
730
1109
|
}
|
|
731
|
-
|
|
732
|
-
|
|
733
|
-
|
|
734
|
-
|
|
1110
|
+
async registerInterval(cacheKey, dynamicSchedule, id, options) {
|
|
1111
|
+
return await this.runTask(cacheKey, async (task) => {
|
|
1112
|
+
return dynamicSchedule.register(id, {
|
|
1113
|
+
type: "interval",
|
|
1114
|
+
options
|
|
1115
|
+
});
|
|
1116
|
+
}, {
|
|
1117
|
+
name: "register-interval",
|
|
1118
|
+
properties: [
|
|
1119
|
+
{
|
|
1120
|
+
label: "schedule",
|
|
1121
|
+
text: dynamicSchedule.id
|
|
1122
|
+
},
|
|
1123
|
+
{
|
|
1124
|
+
label: "id",
|
|
1125
|
+
text: id
|
|
1126
|
+
},
|
|
1127
|
+
{
|
|
1128
|
+
label: "seconds",
|
|
1129
|
+
text: options.seconds.toString()
|
|
1130
|
+
}
|
|
1131
|
+
],
|
|
1132
|
+
params: options
|
|
1133
|
+
});
|
|
735
1134
|
}
|
|
736
|
-
|
|
737
|
-
|
|
738
|
-
|
|
739
|
-
|
|
740
|
-
|
|
741
|
-
|
|
742
|
-
|
|
743
|
-
|
|
744
|
-
|
|
745
|
-
|
|
746
|
-
|
|
747
|
-
|
|
748
|
-
|
|
749
|
-
|
|
750
|
-
|
|
751
|
-
|
|
752
|
-
constructor(task) {
|
|
753
|
-
this.task = task;
|
|
754
|
-
}
|
|
755
|
-
};
|
|
756
|
-
__name(ResumeWithTaskError, "ResumeWithTaskError");
|
|
757
|
-
var ResumeWithParallelTaskError = class {
|
|
758
|
-
constructor(task, childErrors) {
|
|
759
|
-
this.task = task;
|
|
760
|
-
this.childErrors = childErrors;
|
|
761
|
-
}
|
|
762
|
-
};
|
|
763
|
-
__name(ResumeWithParallelTaskError, "ResumeWithParallelTaskError");
|
|
764
|
-
var RetryWithTaskError = class {
|
|
765
|
-
constructor(cause, task, retryAt) {
|
|
766
|
-
this.cause = cause;
|
|
767
|
-
this.task = task;
|
|
768
|
-
this.retryAt = retryAt;
|
|
769
|
-
}
|
|
770
|
-
};
|
|
771
|
-
__name(RetryWithTaskError, "RetryWithTaskError");
|
|
772
|
-
var CanceledWithTaskError = class {
|
|
773
|
-
constructor(task) {
|
|
774
|
-
this.task = task;
|
|
775
|
-
}
|
|
776
|
-
};
|
|
777
|
-
__name(CanceledWithTaskError, "CanceledWithTaskError");
|
|
778
|
-
var YieldExecutionError = class {
|
|
779
|
-
constructor(key) {
|
|
780
|
-
this.key = key;
|
|
781
|
-
}
|
|
782
|
-
};
|
|
783
|
-
__name(YieldExecutionError, "YieldExecutionError");
|
|
784
|
-
var AutoYieldExecutionError = class {
|
|
785
|
-
constructor(location, timeRemaining, timeElapsed) {
|
|
786
|
-
this.location = location;
|
|
787
|
-
this.timeRemaining = timeRemaining;
|
|
788
|
-
this.timeElapsed = timeElapsed;
|
|
789
|
-
}
|
|
790
|
-
};
|
|
791
|
-
__name(AutoYieldExecutionError, "AutoYieldExecutionError");
|
|
792
|
-
var AutoYieldWithCompletedTaskExecutionError = class {
|
|
793
|
-
constructor(id, properties, data, output) {
|
|
794
|
-
this.id = id;
|
|
795
|
-
this.properties = properties;
|
|
796
|
-
this.data = data;
|
|
797
|
-
this.output = output;
|
|
798
|
-
}
|
|
799
|
-
};
|
|
800
|
-
__name(AutoYieldWithCompletedTaskExecutionError, "AutoYieldWithCompletedTaskExecutionError");
|
|
801
|
-
var ParsedPayloadSchemaError = class {
|
|
802
|
-
constructor(schemaErrors) {
|
|
803
|
-
this.schemaErrors = schemaErrors;
|
|
804
|
-
}
|
|
805
|
-
};
|
|
806
|
-
__name(ParsedPayloadSchemaError, "ParsedPayloadSchemaError");
|
|
807
|
-
function isTriggerError(err) {
|
|
808
|
-
return err instanceof ResumeWithTaskError || err instanceof RetryWithTaskError || err instanceof CanceledWithTaskError || err instanceof YieldExecutionError || err instanceof AutoYieldExecutionError || err instanceof AutoYieldWithCompletedTaskExecutionError || err instanceof ResumeWithParallelTaskError;
|
|
809
|
-
}
|
|
810
|
-
__name(isTriggerError, "isTriggerError");
|
|
811
|
-
var ErrorWithTask = class extends Error {
|
|
812
|
-
constructor(cause, message) {
|
|
813
|
-
super(message);
|
|
814
|
-
this.cause = cause;
|
|
1135
|
+
async unregisterInterval(cacheKey, dynamicSchedule, id) {
|
|
1136
|
+
return await this.runTask(cacheKey, async (task) => {
|
|
1137
|
+
return dynamicSchedule.unregister(id);
|
|
1138
|
+
}, {
|
|
1139
|
+
name: "unregister-interval",
|
|
1140
|
+
properties: [
|
|
1141
|
+
{
|
|
1142
|
+
label: "schedule",
|
|
1143
|
+
text: dynamicSchedule.id
|
|
1144
|
+
},
|
|
1145
|
+
{
|
|
1146
|
+
label: "id",
|
|
1147
|
+
text: id
|
|
1148
|
+
}
|
|
1149
|
+
]
|
|
1150
|
+
});
|
|
815
1151
|
}
|
|
816
|
-
|
|
817
|
-
|
|
818
|
-
|
|
819
|
-
|
|
820
|
-
|
|
821
|
-
|
|
822
|
-
|
|
823
|
-
|
|
824
|
-
|
|
825
|
-
|
|
826
|
-
|
|
827
|
-
|
|
828
|
-
|
|
829
|
-
|
|
830
|
-
|
|
831
|
-
|
|
832
|
-
|
|
833
|
-
|
|
834
|
-
|
|
835
|
-
|
|
836
|
-
|
|
837
|
-
|
|
1152
|
+
async registerCron(cacheKey, dynamicSchedule, id, options) {
|
|
1153
|
+
return await this.runTask(cacheKey, async (task) => {
|
|
1154
|
+
return dynamicSchedule.register(id, {
|
|
1155
|
+
type: "cron",
|
|
1156
|
+
options
|
|
1157
|
+
});
|
|
1158
|
+
}, {
|
|
1159
|
+
name: "register-cron",
|
|
1160
|
+
properties: [
|
|
1161
|
+
{
|
|
1162
|
+
label: "schedule",
|
|
1163
|
+
text: dynamicSchedule.id
|
|
1164
|
+
},
|
|
1165
|
+
{
|
|
1166
|
+
label: "id",
|
|
1167
|
+
text: id
|
|
1168
|
+
},
|
|
1169
|
+
{
|
|
1170
|
+
label: "cron",
|
|
1171
|
+
text: options.cron
|
|
1172
|
+
}
|
|
1173
|
+
],
|
|
1174
|
+
params: options
|
|
1175
|
+
});
|
|
838
1176
|
}
|
|
839
|
-
|
|
840
|
-
return this.
|
|
1177
|
+
async unregisterCron(cacheKey, dynamicSchedule, id) {
|
|
1178
|
+
return await this.runTask(cacheKey, async (task) => {
|
|
1179
|
+
return dynamicSchedule.unregister(id);
|
|
1180
|
+
}, {
|
|
1181
|
+
name: "unregister-cron",
|
|
1182
|
+
properties: [
|
|
1183
|
+
{
|
|
1184
|
+
label: "schedule",
|
|
1185
|
+
text: dynamicSchedule.id
|
|
1186
|
+
},
|
|
1187
|
+
{
|
|
1188
|
+
label: "id",
|
|
1189
|
+
text: id
|
|
1190
|
+
}
|
|
1191
|
+
]
|
|
1192
|
+
});
|
|
841
1193
|
}
|
|
842
|
-
|
|
843
|
-
return
|
|
844
|
-
|
|
845
|
-
|
|
846
|
-
|
|
847
|
-
|
|
1194
|
+
async registerTrigger(cacheKey, trigger, id, params) {
|
|
1195
|
+
return await this.runTask(cacheKey, async (task) => {
|
|
1196
|
+
const registration = await this.runTask("register-source", async (subtask1) => {
|
|
1197
|
+
return trigger.register(id, params);
|
|
1198
|
+
}, {
|
|
1199
|
+
name: "register-source"
|
|
1200
|
+
});
|
|
1201
|
+
return {
|
|
1202
|
+
id: registration.id,
|
|
1203
|
+
key: registration.source.key
|
|
1204
|
+
};
|
|
1205
|
+
}, {
|
|
1206
|
+
name: "register-trigger",
|
|
1207
|
+
properties: [
|
|
1208
|
+
{
|
|
1209
|
+
label: "trigger",
|
|
1210
|
+
text: trigger.id
|
|
1211
|
+
},
|
|
1212
|
+
{
|
|
1213
|
+
label: "id",
|
|
1214
|
+
text: id
|
|
1215
|
+
}
|
|
1216
|
+
],
|
|
1217
|
+
params
|
|
848
1218
|
});
|
|
849
1219
|
}
|
|
850
|
-
async
|
|
851
|
-
if (!
|
|
1220
|
+
async getAuth(cacheKey, clientId) {
|
|
1221
|
+
if (!clientId) {
|
|
852
1222
|
return;
|
|
853
|
-
|
|
854
|
-
|
|
855
|
-
return this.
|
|
1223
|
+
}
|
|
1224
|
+
return this.runTask(cacheKey, async (task) => {
|
|
1225
|
+
return await this._triggerClient.getAuth(clientId);
|
|
1226
|
+
}, {
|
|
1227
|
+
name: "get-auth"
|
|
856
1228
|
});
|
|
857
1229
|
}
|
|
858
|
-
|
|
859
|
-
|
|
860
|
-
|
|
861
|
-
|
|
862
|
-
|
|
863
|
-
enabled: this.options.enabled ?? true,
|
|
864
|
-
event: this.options.event,
|
|
865
|
-
immediateResponseFilter: this.options.respondWith?.filter,
|
|
866
|
-
skipTriggeringRuns: this.options.respondWith?.skipTriggeringRuns,
|
|
867
|
-
source: this.options.event.source
|
|
868
|
-
};
|
|
869
|
-
}
|
|
870
|
-
};
|
|
871
|
-
__name(HttpEndpoint, "HttpEndpoint");
|
|
872
|
-
var HttpTrigger = /* @__PURE__ */ __name(class HttpTrigger2 {
|
|
873
|
-
constructor(options) {
|
|
874
|
-
this.options = options;
|
|
875
|
-
}
|
|
876
|
-
toJSON() {
|
|
877
|
-
return {
|
|
878
|
-
type: "static",
|
|
879
|
-
title: this.options.endpointId,
|
|
880
|
-
properties: this.options.event.properties,
|
|
881
|
-
rule: {
|
|
882
|
-
event: `httpendpoint.${this.options.endpointId}`,
|
|
883
|
-
payload: this.options.filter ?? {},
|
|
884
|
-
source: this.options.event.source
|
|
885
|
-
},
|
|
886
|
-
link: `http-endpoints/${this.options.endpointId}`,
|
|
887
|
-
help: {
|
|
888
|
-
noRuns: {
|
|
889
|
-
text: "To start triggering runs click here to setup your HTTP Endpoint with the external API service you want to receive webhooks from.",
|
|
890
|
-
link: `http-endpoints/${this.options.endpointId}`
|
|
891
|
-
}
|
|
1230
|
+
async parallel(cacheKey, items, callback, options) {
|
|
1231
|
+
const results = await this.runTask(cacheKey, async (task) => {
|
|
1232
|
+
const outcomes = await Promise.allSettled(items.map((item, index) => spaceOut(() => callback(item, index), index, 15)));
|
|
1233
|
+
if (outcomes.every((outcome) => outcome.status === "fulfilled")) {
|
|
1234
|
+
return outcomes.map((outcome) => outcome.value);
|
|
892
1235
|
}
|
|
893
|
-
|
|
894
|
-
|
|
895
|
-
|
|
896
|
-
|
|
897
|
-
|
|
898
|
-
|
|
1236
|
+
const nonInternalErrors = outcomes.filter((outcome) => outcome.status === "rejected" && !isTriggerError(outcome.reason)).map((outcome) => outcome);
|
|
1237
|
+
if (nonInternalErrors.length > 0) {
|
|
1238
|
+
throw nonInternalErrors[0].reason;
|
|
1239
|
+
}
|
|
1240
|
+
const internalErrors = outcomes.filter((outcome) => outcome.status === "rejected" && isTriggerError(outcome.reason)).map((outcome) => outcome).map((outcome) => outcome.reason);
|
|
1241
|
+
throw new ResumeWithParallelTaskError(task, internalErrors);
|
|
1242
|
+
}, {
|
|
1243
|
+
name: "parallel",
|
|
1244
|
+
parallel: true,
|
|
1245
|
+
...options ?? {}
|
|
1246
|
+
});
|
|
1247
|
+
return results;
|
|
899
1248
|
}
|
|
900
|
-
|
|
901
|
-
|
|
902
|
-
|
|
903
|
-
|
|
904
|
-
|
|
905
|
-
|
|
906
|
-
|
|
907
|
-
|
|
908
|
-
|
|
909
|
-
|
|
910
|
-
|
|
911
|
-
|
|
912
|
-
|
|
913
|
-
|
|
914
|
-
|
|
915
|
-
|
|
916
|
-
|
|
917
|
-
|
|
918
|
-
|
|
919
|
-
|
|
920
|
-
properties: options.properties,
|
|
921
|
-
examples: options.examples ? options.examples : [
|
|
922
|
-
{
|
|
923
|
-
id: "basic-request",
|
|
924
|
-
name: "Basic Request",
|
|
925
|
-
icon: "http-post",
|
|
926
|
-
payload: {
|
|
927
|
-
url: "https://cloud.trigger.dev",
|
|
928
|
-
method: "POST",
|
|
929
|
-
headers: {
|
|
930
|
-
"Content-Type": "application/json"
|
|
931
|
-
},
|
|
932
|
-
rawBody: JSON.stringify({
|
|
933
|
-
foo: "bar"
|
|
934
|
-
})
|
|
935
|
-
}
|
|
936
|
-
}
|
|
937
|
-
],
|
|
938
|
-
parsePayload: (rawPayload) => {
|
|
939
|
-
const result = import_core2.RequestWithRawBodySchema.safeParse(rawPayload);
|
|
940
|
-
if (!result.success) {
|
|
941
|
-
throw new ParsedPayloadSchemaError(formatSchemaErrors(result.error.issues));
|
|
942
|
-
}
|
|
943
|
-
return new Request(new URL(result.data.url), {
|
|
944
|
-
method: result.data.method,
|
|
945
|
-
headers: result.data.headers,
|
|
946
|
-
body: result.data.rawBody
|
|
947
|
-
});
|
|
1249
|
+
async runTask(cacheKey, callback, options, onError) {
|
|
1250
|
+
__privateMethod(this, _detectAutoYield, detectAutoYield_fn).call(this, "start_task", 500);
|
|
1251
|
+
const parentId = this._taskStorage.getStore()?.taskId;
|
|
1252
|
+
if (parentId) {
|
|
1253
|
+
this._logger.debug("Using parent task", {
|
|
1254
|
+
parentId,
|
|
1255
|
+
cacheKey,
|
|
1256
|
+
options
|
|
1257
|
+
});
|
|
1258
|
+
}
|
|
1259
|
+
const idempotencyKey = await generateIdempotencyKey([
|
|
1260
|
+
this._id,
|
|
1261
|
+
parentId ?? "",
|
|
1262
|
+
cacheKey
|
|
1263
|
+
].flat());
|
|
1264
|
+
if (this._visitedCacheKeys.has(idempotencyKey)) {
|
|
1265
|
+
if (typeof cacheKey === "string") {
|
|
1266
|
+
throw new Error(`Task with cacheKey "${cacheKey}" has already been executed in this run. Each task must have a unique cacheKey.`);
|
|
1267
|
+
} else {
|
|
1268
|
+
throw new Error(`Task with cacheKey "${cacheKey.join("-")}" has already been executed in this run. Each task must have a unique cacheKey.`);
|
|
948
1269
|
}
|
|
949
1270
|
}
|
|
950
|
-
|
|
951
|
-
|
|
952
|
-
|
|
953
|
-
|
|
954
|
-
|
|
955
|
-
var import_core4 = require("@trigger.dev/core");
|
|
956
|
-
var import_core_backend = require("@trigger.dev/core-backend");
|
|
957
|
-
var import_node_async_hooks2 = require("async_hooks");
|
|
958
|
-
var import_node_crypto = require("crypto");
|
|
959
|
-
|
|
960
|
-
// src/retry.ts
|
|
961
|
-
var import_core3 = require("@trigger.dev/core");
|
|
962
|
-
var retry = {
|
|
963
|
-
standardBackoff: {
|
|
964
|
-
limit: 8,
|
|
965
|
-
factor: 1.8,
|
|
966
|
-
minTimeoutInMs: 500,
|
|
967
|
-
maxTimeoutInMs: 3e4,
|
|
968
|
-
randomize: true
|
|
969
|
-
},
|
|
970
|
-
exponentialBackoff: {
|
|
971
|
-
limit: 8,
|
|
972
|
-
factor: 2,
|
|
973
|
-
minTimeoutInMs: 1e3,
|
|
974
|
-
maxTimeoutInMs: 3e4,
|
|
975
|
-
randomize: true
|
|
976
|
-
}
|
|
977
|
-
};
|
|
978
|
-
|
|
979
|
-
// src/status.ts
|
|
980
|
-
var TriggerStatus = class {
|
|
981
|
-
constructor(id, io) {
|
|
982
|
-
this.id = id;
|
|
983
|
-
this.io = io;
|
|
984
|
-
}
|
|
985
|
-
async update(key, status) {
|
|
986
|
-
const properties = [];
|
|
987
|
-
if (status.label) {
|
|
988
|
-
properties.push({
|
|
989
|
-
label: "Label",
|
|
990
|
-
text: status.label
|
|
1271
|
+
this._visitedCacheKeys.add(idempotencyKey);
|
|
1272
|
+
const cachedTask = this._cachedTasks.get(idempotencyKey);
|
|
1273
|
+
if (cachedTask && cachedTask.status === "COMPLETED") {
|
|
1274
|
+
this._logger.debug("Using completed cached task", {
|
|
1275
|
+
idempotencyKey
|
|
991
1276
|
});
|
|
1277
|
+
this._stats.cachedTaskHits++;
|
|
1278
|
+
return options?.parseOutput ? options.parseOutput(cachedTask.output) : cachedTask.output;
|
|
992
1279
|
}
|
|
993
|
-
if (
|
|
994
|
-
|
|
995
|
-
|
|
996
|
-
|
|
997
|
-
|
|
1280
|
+
if (options?.noop && this._noopTasksBloomFilter) {
|
|
1281
|
+
if (this._noopTasksBloomFilter.test(idempotencyKey)) {
|
|
1282
|
+
this._logger.debug("task idempotency key exists in noopTasksBloomFilter", {
|
|
1283
|
+
idempotencyKey
|
|
1284
|
+
});
|
|
1285
|
+
this._stats.noopCachedTaskHits++;
|
|
1286
|
+
return {};
|
|
1287
|
+
}
|
|
998
1288
|
}
|
|
999
|
-
|
|
1000
|
-
|
|
1289
|
+
const runOptions = {
|
|
1290
|
+
...options ?? {},
|
|
1291
|
+
parseOutput: void 0
|
|
1292
|
+
};
|
|
1293
|
+
const response = await this._apiClient.runTask(this._id, {
|
|
1294
|
+
idempotencyKey,
|
|
1295
|
+
displayKey: typeof cacheKey === "string" ? cacheKey : void 0,
|
|
1296
|
+
noop: false,
|
|
1297
|
+
...runOptions ?? {},
|
|
1298
|
+
parentId
|
|
1001
1299
|
}, {
|
|
1002
|
-
|
|
1003
|
-
icon: "bell",
|
|
1004
|
-
params: {
|
|
1005
|
-
...status
|
|
1006
|
-
},
|
|
1007
|
-
properties
|
|
1300
|
+
cachedTasksCursor: this._cachedTasksCursor
|
|
1008
1301
|
});
|
|
1009
|
-
|
|
1010
|
-
|
|
1011
|
-
|
|
1012
|
-
|
|
1013
|
-
// src/types.ts
|
|
1014
|
-
var import_zod2 = require("zod");
|
|
1015
|
-
var EventSpecificationExampleSchema = import_zod2.z.object({
|
|
1016
|
-
id: import_zod2.z.string(),
|
|
1017
|
-
name: import_zod2.z.string(),
|
|
1018
|
-
icon: import_zod2.z.string().optional(),
|
|
1019
|
-
payload: import_zod2.z.any()
|
|
1020
|
-
});
|
|
1021
|
-
function waitForEventSchema(schema) {
|
|
1022
|
-
return import_zod2.z.object({
|
|
1023
|
-
id: import_zod2.z.string(),
|
|
1024
|
-
name: import_zod2.z.string(),
|
|
1025
|
-
source: import_zod2.z.string(),
|
|
1026
|
-
payload: schema,
|
|
1027
|
-
timestamp: import_zod2.z.coerce.date(),
|
|
1028
|
-
context: import_zod2.z.any().optional(),
|
|
1029
|
-
accountId: import_zod2.z.string().optional()
|
|
1030
|
-
});
|
|
1031
|
-
}
|
|
1032
|
-
__name(waitForEventSchema, "waitForEventSchema");
|
|
1033
|
-
|
|
1034
|
-
// src/io.ts
|
|
1035
|
-
var import_zod3 = require("zod");
|
|
1036
|
-
var JSONOutputSerializer = class {
|
|
1037
|
-
serialize(value) {
|
|
1038
|
-
return JSON.stringify(value);
|
|
1039
|
-
}
|
|
1040
|
-
deserialize(value) {
|
|
1041
|
-
return value ? JSON.parse(value) : void 0;
|
|
1042
|
-
}
|
|
1043
|
-
};
|
|
1044
|
-
__name(JSONOutputSerializer, "JSONOutputSerializer");
|
|
1045
|
-
var _addToCachedTasks, addToCachedTasks_fn, _detectAutoYield, detectAutoYield_fn, _forceYield, forceYield_fn, _getTimeElapsed, getTimeElapsed_fn, _getRemainingTimeInMillis, getRemainingTimeInMillis_fn;
|
|
1046
|
-
var IO = class {
|
|
1047
|
-
constructor(options) {
|
|
1048
|
-
__privateAdd(this, _addToCachedTasks);
|
|
1049
|
-
__privateAdd(this, _detectAutoYield);
|
|
1050
|
-
__privateAdd(this, _forceYield);
|
|
1051
|
-
__privateAdd(this, _getTimeElapsed);
|
|
1052
|
-
__privateAdd(this, _getRemainingTimeInMillis);
|
|
1053
|
-
__publicField(this, "_outputSerializer", new JSONOutputSerializer());
|
|
1054
|
-
__publicField(this, "_visitedCacheKeys", /* @__PURE__ */ new Set());
|
|
1055
|
-
__publicField(this, "brb", this.yield.bind(this));
|
|
1056
|
-
this._id = options.id;
|
|
1057
|
-
this._apiClient = options.apiClient;
|
|
1058
|
-
this._triggerClient = options.client;
|
|
1059
|
-
this._logger = options.logger ?? new import_core4.Logger("trigger.dev", options.logLevel);
|
|
1060
|
-
this._cachedTasks = /* @__PURE__ */ new Map();
|
|
1061
|
-
this._jobLogger = options.jobLogger;
|
|
1062
|
-
this._jobLogLevel = options.jobLogLevel;
|
|
1063
|
-
this._timeOrigin = options.timeOrigin;
|
|
1064
|
-
this._executionTimeout = options.executionTimeout;
|
|
1065
|
-
this._stats = {
|
|
1066
|
-
initialCachedTasks: 0,
|
|
1067
|
-
lazyLoadedCachedTasks: 0,
|
|
1068
|
-
executedTasks: 0,
|
|
1069
|
-
cachedTaskHits: 0,
|
|
1070
|
-
cachedTaskMisses: 0,
|
|
1071
|
-
noopCachedTaskHits: 0,
|
|
1072
|
-
noopCachedTaskMisses: 0
|
|
1073
|
-
};
|
|
1074
|
-
if (options.cachedTasks) {
|
|
1075
|
-
options.cachedTasks.forEach((task) => {
|
|
1076
|
-
this._cachedTasks.set(task.idempotencyKey, task);
|
|
1302
|
+
const task = response.version === import_core2.API_VERSIONS.LAZY_LOADED_CACHED_TASKS ? response.body.task : response.body;
|
|
1303
|
+
if (task.forceYield) {
|
|
1304
|
+
this._logger.debug("Forcing yield after run task", {
|
|
1305
|
+
idempotencyKey
|
|
1077
1306
|
});
|
|
1078
|
-
this
|
|
1307
|
+
__privateMethod(this, _forceYield, forceYield_fn).call(this, "after_run_task");
|
|
1079
1308
|
}
|
|
1080
|
-
|
|
1081
|
-
|
|
1082
|
-
|
|
1083
|
-
|
|
1084
|
-
|
|
1309
|
+
if (response.version === import_core2.API_VERSIONS.LAZY_LOADED_CACHED_TASKS) {
|
|
1310
|
+
this._cachedTasksCursor = response.body.cachedTasks?.cursor;
|
|
1311
|
+
for (const cachedTask2 of response.body.cachedTasks?.tasks ?? []) {
|
|
1312
|
+
if (!this._cachedTasks.has(cachedTask2.idempotencyKey)) {
|
|
1313
|
+
this._cachedTasks.set(cachedTask2.idempotencyKey, cachedTask2);
|
|
1314
|
+
this._logger.debug("Injecting lazy loaded task into task cache", {
|
|
1315
|
+
idempotencyKey: cachedTask2.idempotencyKey
|
|
1316
|
+
});
|
|
1317
|
+
this._stats.lazyLoadedCachedTasks++;
|
|
1318
|
+
}
|
|
1319
|
+
}
|
|
1085
1320
|
}
|
|
1086
|
-
|
|
1087
|
-
|
|
1088
|
-
|
|
1089
|
-
|
|
1090
|
-
|
|
1091
|
-
|
|
1092
|
-
|
|
1093
|
-
|
|
1094
|
-
|
|
1095
|
-
|
|
1096
|
-
|
|
1097
|
-
|
|
1098
|
-
|
|
1099
|
-
|
|
1100
|
-
|
|
1101
|
-
|
|
1102
|
-
|
|
1103
|
-
|
|
1104
|
-
|
|
1105
|
-
|
|
1106
|
-
|
|
1107
|
-
|
|
1108
|
-
|
|
1109
|
-
|
|
1110
|
-
|
|
1111
|
-
|
|
1112
|
-
|
|
1113
|
-
|
|
1114
|
-
|
|
1115
|
-
|
|
1116
|
-
|
|
1117
|
-
|
|
1118
|
-
|
|
1119
|
-
|
|
1120
|
-
|
|
1121
|
-
|
|
1122
|
-
|
|
1123
|
-
|
|
1124
|
-
|
|
1125
|
-
|
|
1321
|
+
if (task.status === "CANCELED") {
|
|
1322
|
+
this._logger.debug("Task canceled", {
|
|
1323
|
+
idempotencyKey,
|
|
1324
|
+
task
|
|
1325
|
+
});
|
|
1326
|
+
throw new CanceledWithTaskError(task);
|
|
1327
|
+
}
|
|
1328
|
+
if (task.status === "COMPLETED") {
|
|
1329
|
+
if (task.noop) {
|
|
1330
|
+
this._logger.debug("Noop Task completed", {
|
|
1331
|
+
idempotencyKey
|
|
1332
|
+
});
|
|
1333
|
+
this._noopTasksBloomFilter?.add(task.idempotencyKey);
|
|
1334
|
+
} else {
|
|
1335
|
+
this._logger.debug("Cache miss", {
|
|
1336
|
+
idempotencyKey
|
|
1337
|
+
});
|
|
1338
|
+
this._stats.cachedTaskMisses++;
|
|
1339
|
+
__privateMethod(this, _addToCachedTasks, addToCachedTasks_fn).call(this, task);
|
|
1340
|
+
}
|
|
1341
|
+
return options?.parseOutput ? options.parseOutput(task.output) : task.output;
|
|
1342
|
+
}
|
|
1343
|
+
if (task.status === "ERRORED") {
|
|
1344
|
+
this._logger.debug("Task errored", {
|
|
1345
|
+
idempotencyKey,
|
|
1346
|
+
task
|
|
1347
|
+
});
|
|
1348
|
+
throw new ErrorWithTask(task, task.error ?? task?.output ? JSON.stringify(task.output) : "Task errored");
|
|
1349
|
+
}
|
|
1350
|
+
__privateMethod(this, _detectAutoYield, detectAutoYield_fn).call(this, "before_execute_task", 1500);
|
|
1351
|
+
const executeTask = /* @__PURE__ */ __name(async () => {
|
|
1352
|
+
try {
|
|
1353
|
+
const result = await callback(task, this);
|
|
1354
|
+
if (task.status === "WAITING" && task.callbackUrl) {
|
|
1355
|
+
this._logger.debug("Waiting for remote callback", {
|
|
1356
|
+
idempotencyKey,
|
|
1357
|
+
task
|
|
1358
|
+
});
|
|
1359
|
+
return {};
|
|
1360
|
+
}
|
|
1361
|
+
const output = this._outputSerializer.serialize(result);
|
|
1362
|
+
this._logger.debug("Completing using output", {
|
|
1363
|
+
idempotencyKey,
|
|
1364
|
+
task
|
|
1365
|
+
});
|
|
1366
|
+
__privateMethod(this, _detectAutoYield, detectAutoYield_fn).call(this, "before_complete_task", 500, task, output);
|
|
1367
|
+
const completedTask = await this._apiClient.completeTask(this._id, task.id, {
|
|
1368
|
+
output,
|
|
1369
|
+
properties: task.outputProperties ?? void 0
|
|
1370
|
+
});
|
|
1371
|
+
if (completedTask.forceYield) {
|
|
1372
|
+
this._logger.debug("Forcing yield after task completed", {
|
|
1373
|
+
idempotencyKey
|
|
1374
|
+
});
|
|
1375
|
+
__privateMethod(this, _forceYield, forceYield_fn).call(this, "after_complete_task");
|
|
1376
|
+
}
|
|
1377
|
+
this._stats.executedTasks++;
|
|
1378
|
+
if (completedTask.status === "CANCELED") {
|
|
1379
|
+
throw new CanceledWithTaskError(completedTask);
|
|
1380
|
+
}
|
|
1381
|
+
__privateMethod(this, _detectAutoYield, detectAutoYield_fn).call(this, "after_complete_task", 500);
|
|
1382
|
+
const deserializedOutput = this._outputSerializer.deserialize(output);
|
|
1383
|
+
return options?.parseOutput ? options.parseOutput(deserializedOutput) : deserializedOutput;
|
|
1384
|
+
} catch (error) {
|
|
1385
|
+
if (isTriggerError(error)) {
|
|
1386
|
+
throw error;
|
|
1387
|
+
}
|
|
1388
|
+
let skipRetrying = false;
|
|
1389
|
+
if (onError) {
|
|
1390
|
+
try {
|
|
1391
|
+
const onErrorResult = onError(error, task, this);
|
|
1392
|
+
if (onErrorResult) {
|
|
1393
|
+
if (onErrorResult instanceof Error) {
|
|
1394
|
+
error = onErrorResult;
|
|
1395
|
+
} else {
|
|
1396
|
+
skipRetrying = !!onErrorResult.skipRetrying;
|
|
1397
|
+
if (onErrorResult.retryAt && !skipRetrying) {
|
|
1398
|
+
const parsedError2 = import_core2.ErrorWithStackSchema.safeParse(onErrorResult.error);
|
|
1399
|
+
throw new RetryWithTaskError(parsedError2.success ? parsedError2.data : {
|
|
1400
|
+
message: "Unknown error"
|
|
1401
|
+
}, task, onErrorResult.retryAt);
|
|
1402
|
+
}
|
|
1403
|
+
}
|
|
1126
1404
|
}
|
|
1127
|
-
|
|
1128
|
-
|
|
1129
|
-
|
|
1130
|
-
break;
|
|
1405
|
+
} catch (innerError) {
|
|
1406
|
+
if (isTriggerError(innerError)) {
|
|
1407
|
+
throw innerError;
|
|
1131
1408
|
}
|
|
1409
|
+
error = innerError;
|
|
1132
1410
|
}
|
|
1133
|
-
}
|
|
1134
|
-
|
|
1135
|
-
|
|
1136
|
-
|
|
1137
|
-
|
|
1138
|
-
|
|
1139
|
-
|
|
1140
|
-
|
|
1141
|
-
|
|
1411
|
+
}
|
|
1412
|
+
if (error instanceof ErrorWithTask) {
|
|
1413
|
+
await this._apiClient.failTask(this._id, task.id, {
|
|
1414
|
+
error: error.cause.output
|
|
1415
|
+
});
|
|
1416
|
+
}
|
|
1417
|
+
const parsedError = import_core2.ErrorWithStackSchema.safeParse(error);
|
|
1418
|
+
if (options?.retry && !skipRetrying) {
|
|
1419
|
+
const retryAt = (0, import_core.calculateRetryAt)(options.retry, task.attempts - 1);
|
|
1420
|
+
if (retryAt) {
|
|
1421
|
+
throw new RetryWithTaskError(parsedError.success ? parsedError.data : {
|
|
1422
|
+
message: "Unknown error"
|
|
1423
|
+
}, task, retryAt);
|
|
1424
|
+
}
|
|
1425
|
+
}
|
|
1426
|
+
if (parsedError.success) {
|
|
1427
|
+
await this._apiClient.failTask(this._id, task.id, {
|
|
1428
|
+
error: parsedError.data
|
|
1429
|
+
});
|
|
1430
|
+
} else {
|
|
1431
|
+
const message = typeof error === "string" ? error : JSON.stringify(error);
|
|
1432
|
+
await this._apiClient.failTask(this._id, task.id, {
|
|
1433
|
+
error: {
|
|
1434
|
+
name: "Unknown error",
|
|
1435
|
+
message
|
|
1142
1436
|
}
|
|
1143
|
-
|
|
1144
|
-
|
|
1145
|
-
|
|
1146
|
-
variant: level.toLowerCase()
|
|
1147
|
-
},
|
|
1148
|
-
noop: true
|
|
1149
|
-
});
|
|
1150
|
-
}
|
|
1151
|
-
});
|
|
1152
|
-
}
|
|
1153
|
-
async random(cacheKey, { min = 0, max = 1, round = false } = {}) {
|
|
1154
|
-
return await this.runTask(cacheKey, async (task) => {
|
|
1155
|
-
if (min > max) {
|
|
1156
|
-
throw new Error(`Lower bound can't be higher than upper bound - min: ${min}, max: ${max}`);
|
|
1157
|
-
}
|
|
1158
|
-
if (min === max) {
|
|
1159
|
-
await this.logger.warn(`Lower and upper bounds are identical. The return value is not random and will always be: ${min}`);
|
|
1160
|
-
}
|
|
1161
|
-
const withinBounds = (max - min) * Math.random() + min;
|
|
1162
|
-
if (!round) {
|
|
1163
|
-
return withinBounds;
|
|
1164
|
-
}
|
|
1165
|
-
if (!Number.isInteger(min) || !Number.isInteger(max)) {
|
|
1166
|
-
await this.logger.warn("Rounding enabled with floating-point bounds. This may cause unexpected skew and boundary inclusivity.");
|
|
1437
|
+
});
|
|
1438
|
+
}
|
|
1439
|
+
throw error;
|
|
1167
1440
|
}
|
|
1168
|
-
|
|
1169
|
-
|
|
1170
|
-
|
|
1171
|
-
|
|
1172
|
-
|
|
1173
|
-
|
|
1174
|
-
|
|
1175
|
-
|
|
1176
|
-
|
|
1177
|
-
|
|
1178
|
-
properties: [
|
|
1179
|
-
...min === 0 ? [] : [
|
|
1180
|
-
{
|
|
1181
|
-
label: "min",
|
|
1182
|
-
text: String(min)
|
|
1183
|
-
}
|
|
1184
|
-
],
|
|
1185
|
-
...max === 1 ? [] : [
|
|
1186
|
-
{
|
|
1187
|
-
label: "max",
|
|
1188
|
-
text: String(max)
|
|
1189
|
-
}
|
|
1190
|
-
],
|
|
1191
|
-
...round === false ? [] : [
|
|
1192
|
-
{
|
|
1193
|
-
label: "round",
|
|
1194
|
-
text: String(round)
|
|
1195
|
-
}
|
|
1196
|
-
]
|
|
1197
|
-
],
|
|
1198
|
-
style: {
|
|
1199
|
-
style: "minimal"
|
|
1441
|
+
}, "executeTask");
|
|
1442
|
+
if (task.status === "WAITING") {
|
|
1443
|
+
this._logger.debug("Task waiting", {
|
|
1444
|
+
idempotencyKey,
|
|
1445
|
+
task
|
|
1446
|
+
});
|
|
1447
|
+
if (task.callbackUrl) {
|
|
1448
|
+
await this._taskStorage.run({
|
|
1449
|
+
taskId: task.id
|
|
1450
|
+
}, executeTask);
|
|
1200
1451
|
}
|
|
1201
|
-
|
|
1452
|
+
throw new ResumeWithTaskError(task);
|
|
1453
|
+
}
|
|
1454
|
+
if (task.status === "RUNNING" && typeof task.operation === "string") {
|
|
1455
|
+
this._logger.debug("Task running operation", {
|
|
1456
|
+
idempotencyKey,
|
|
1457
|
+
task
|
|
1458
|
+
});
|
|
1459
|
+
throw new ResumeWithTaskError(task);
|
|
1460
|
+
}
|
|
1461
|
+
return this._taskStorage.run({
|
|
1462
|
+
taskId: task.id
|
|
1463
|
+
}, executeTask);
|
|
1202
1464
|
}
|
|
1203
|
-
|
|
1204
|
-
|
|
1205
|
-
|
|
1206
|
-
|
|
1207
|
-
|
|
1208
|
-
|
|
1209
|
-
|
|
1210
|
-
|
|
1211
|
-
|
|
1212
|
-
delayUntil: new Date(Date.now() + seconds * 1e3),
|
|
1213
|
-
style: {
|
|
1214
|
-
style: "minimal"
|
|
1215
|
-
}
|
|
1216
|
-
});
|
|
1465
|
+
yield(cacheKey) {
|
|
1466
|
+
if (!(0, import_core2.supportsFeature)("yieldExecution", this._serverVersion)) {
|
|
1467
|
+
console.warn("[trigger.dev] io.yield() is not support by the version of the Trigger.dev server you are using, you will need to upgrade your self-hosted Trigger.dev instance.");
|
|
1468
|
+
return;
|
|
1469
|
+
}
|
|
1470
|
+
if (this._yieldedExecutions.includes(cacheKey)) {
|
|
1471
|
+
return;
|
|
1472
|
+
}
|
|
1473
|
+
throw new YieldExecutionError(cacheKey);
|
|
1217
1474
|
}
|
|
1218
|
-
async
|
|
1219
|
-
|
|
1220
|
-
|
|
1221
|
-
|
|
1222
|
-
|
|
1475
|
+
async try(tryCallback, catchCallback) {
|
|
1476
|
+
try {
|
|
1477
|
+
return await tryCallback();
|
|
1478
|
+
} catch (error) {
|
|
1479
|
+
if (isTriggerError(error)) {
|
|
1480
|
+
throw error;
|
|
1223
1481
|
}
|
|
1224
|
-
await
|
|
1225
|
-
|
|
1226
|
-
|
|
1227
|
-
|
|
1228
|
-
|
|
1229
|
-
|
|
1230
|
-
|
|
1231
|
-
|
|
1232
|
-
|
|
1233
|
-
|
|
1234
|
-
|
|
1235
|
-
|
|
1236
|
-
|
|
1237
|
-
|
|
1238
|
-
|
|
1239
|
-
|
|
1240
|
-
|
|
1241
|
-
|
|
1242
|
-
|
|
1243
|
-
|
|
1244
|
-
|
|
1245
|
-
|
|
1246
|
-
|
|
1247
|
-
|
|
1248
|
-
|
|
1249
|
-
|
|
1250
|
-
|
|
1251
|
-
|
|
1252
|
-
|
|
1253
|
-
|
|
1254
|
-
|
|
1255
|
-
|
|
1256
|
-
|
|
1257
|
-
|
|
1258
|
-
|
|
1259
|
-
|
|
1260
|
-
|
|
1261
|
-
|
|
1262
|
-
|
|
1263
|
-
|
|
1264
|
-
|
|
1265
|
-
|
|
1266
|
-
|
|
1267
|
-
|
|
1268
|
-
|
|
1269
|
-
|
|
1270
|
-
|
|
1271
|
-
|
|
1482
|
+
return await catchCallback(error);
|
|
1483
|
+
}
|
|
1484
|
+
}
|
|
1485
|
+
get store() {
|
|
1486
|
+
return {
|
|
1487
|
+
env: this._envStore,
|
|
1488
|
+
job: this._jobStore,
|
|
1489
|
+
run: this._runStore
|
|
1490
|
+
};
|
|
1491
|
+
}
|
|
1492
|
+
};
|
|
1493
|
+
__name(IO, "IO");
|
|
1494
|
+
_addToCachedTasks = new WeakSet();
|
|
1495
|
+
addToCachedTasks_fn = /* @__PURE__ */ __name(function(task) {
|
|
1496
|
+
this._cachedTasks.set(task.idempotencyKey, task);
|
|
1497
|
+
}, "#addToCachedTasks");
|
|
1498
|
+
_detectAutoYield = new WeakSet();
|
|
1499
|
+
detectAutoYield_fn = /* @__PURE__ */ __name(function(location, threshold = 1500, task1, output) {
|
|
1500
|
+
const timeRemaining = __privateMethod(this, _getRemainingTimeInMillis, getRemainingTimeInMillis_fn).call(this);
|
|
1501
|
+
if (timeRemaining && timeRemaining < threshold) {
|
|
1502
|
+
if (task1) {
|
|
1503
|
+
throw new AutoYieldWithCompletedTaskExecutionError(task1.id, task1.outputProperties ?? [], {
|
|
1504
|
+
location,
|
|
1505
|
+
timeRemaining,
|
|
1506
|
+
timeElapsed: __privateMethod(this, _getTimeElapsed, getTimeElapsed_fn).call(this)
|
|
1507
|
+
}, output);
|
|
1508
|
+
} else {
|
|
1509
|
+
throw new AutoYieldExecutionError(location, timeRemaining, __privateMethod(this, _getTimeElapsed, getTimeElapsed_fn).call(this));
|
|
1510
|
+
}
|
|
1511
|
+
}
|
|
1512
|
+
}, "#detectAutoYield");
|
|
1513
|
+
_forceYield = new WeakSet();
|
|
1514
|
+
forceYield_fn = /* @__PURE__ */ __name(function(location1) {
|
|
1515
|
+
const timeRemaining = __privateMethod(this, _getRemainingTimeInMillis, getRemainingTimeInMillis_fn).call(this);
|
|
1516
|
+
if (timeRemaining) {
|
|
1517
|
+
throw new AutoYieldExecutionError(location1, timeRemaining, __privateMethod(this, _getTimeElapsed, getTimeElapsed_fn).call(this));
|
|
1518
|
+
}
|
|
1519
|
+
}, "#forceYield");
|
|
1520
|
+
_getTimeElapsed = new WeakSet();
|
|
1521
|
+
getTimeElapsed_fn = /* @__PURE__ */ __name(function() {
|
|
1522
|
+
return performance.now() - this._timeOrigin;
|
|
1523
|
+
}, "#getTimeElapsed");
|
|
1524
|
+
_getRemainingTimeInMillis = new WeakSet();
|
|
1525
|
+
getRemainingTimeInMillis_fn = /* @__PURE__ */ __name(function() {
|
|
1526
|
+
if (this._executionTimeout) {
|
|
1527
|
+
return this._executionTimeout - (performance.now() - this._timeOrigin);
|
|
1528
|
+
}
|
|
1529
|
+
return void 0;
|
|
1530
|
+
}, "#getRemainingTimeInMillis");
|
|
1531
|
+
async function generateIdempotencyKey(keyMaterial) {
|
|
1532
|
+
const keys = keyMaterial.map((key2) => {
|
|
1533
|
+
if (typeof key2 === "string") {
|
|
1534
|
+
return key2;
|
|
1535
|
+
}
|
|
1536
|
+
return stableStringify(key2);
|
|
1537
|
+
});
|
|
1538
|
+
const key = keys.join(":");
|
|
1539
|
+
const hash = await import_node_crypto.webcrypto.subtle.digest("SHA-256", Buffer.from(key));
|
|
1540
|
+
return Buffer.from(hash).toString("hex");
|
|
1541
|
+
}
|
|
1542
|
+
__name(generateIdempotencyKey, "generateIdempotencyKey");
|
|
1543
|
+
function stableStringify(obj) {
|
|
1544
|
+
function sortKeys(obj2) {
|
|
1545
|
+
if (typeof obj2 !== "object" || obj2 === null) {
|
|
1546
|
+
return obj2;
|
|
1547
|
+
}
|
|
1548
|
+
if (Array.isArray(obj2)) {
|
|
1549
|
+
return obj2.map(sortKeys);
|
|
1550
|
+
}
|
|
1551
|
+
const sortedKeys = Object.keys(obj2).sort();
|
|
1552
|
+
const sortedObj2 = {};
|
|
1553
|
+
for (const key of sortedKeys) {
|
|
1554
|
+
sortedObj2[key] = sortKeys(obj2[key]);
|
|
1555
|
+
}
|
|
1556
|
+
return sortedObj2;
|
|
1557
|
+
}
|
|
1558
|
+
__name(sortKeys, "sortKeys");
|
|
1559
|
+
const sortedObj = sortKeys(obj);
|
|
1560
|
+
return JSON.stringify(sortedObj);
|
|
1561
|
+
}
|
|
1562
|
+
__name(stableStringify, "stableStringify");
|
|
1563
|
+
var IOLogger = class {
|
|
1564
|
+
constructor(callback) {
|
|
1565
|
+
this.callback = callback;
|
|
1566
|
+
}
|
|
1567
|
+
log(message, properties) {
|
|
1568
|
+
return this.callback("LOG", message, properties);
|
|
1569
|
+
}
|
|
1570
|
+
debug(message, properties) {
|
|
1571
|
+
return this.callback("DEBUG", message, properties);
|
|
1572
|
+
}
|
|
1573
|
+
info(message, properties) {
|
|
1574
|
+
return this.callback("INFO", message, properties);
|
|
1575
|
+
}
|
|
1576
|
+
warn(message, properties) {
|
|
1577
|
+
return this.callback("WARN", message, properties);
|
|
1578
|
+
}
|
|
1579
|
+
error(message, properties) {
|
|
1580
|
+
return this.callback("ERROR", message, properties);
|
|
1581
|
+
}
|
|
1582
|
+
};
|
|
1583
|
+
__name(IOLogger, "IOLogger");
|
|
1584
|
+
async function spaceOut(callback, index, delay) {
|
|
1585
|
+
await new Promise((resolve) => setTimeout(resolve, index * delay));
|
|
1586
|
+
return await callback();
|
|
1587
|
+
}
|
|
1588
|
+
__name(spaceOut, "spaceOut");
|
|
1589
|
+
function sendEventOptionsProperties(options) {
|
|
1590
|
+
return [
|
|
1591
|
+
...options?.accountId ? [
|
|
1592
|
+
{
|
|
1593
|
+
label: "Account ID",
|
|
1594
|
+
text: options.accountId
|
|
1595
|
+
}
|
|
1596
|
+
] : [],
|
|
1597
|
+
...options?.deliverAfter ? [
|
|
1598
|
+
{
|
|
1599
|
+
label: "Deliver After",
|
|
1600
|
+
text: `${options.deliverAfter}s`
|
|
1601
|
+
}
|
|
1602
|
+
] : [],
|
|
1603
|
+
...options?.deliverAt ? [
|
|
1604
|
+
{
|
|
1605
|
+
label: "Deliver At",
|
|
1606
|
+
text: options.deliverAt.toISOString()
|
|
1272
1607
|
}
|
|
1608
|
+
] : []
|
|
1609
|
+
];
|
|
1610
|
+
}
|
|
1611
|
+
__name(sendEventOptionsProperties, "sendEventOptionsProperties");
|
|
1612
|
+
|
|
1613
|
+
// src/store/keyValueStoreClient.ts
|
|
1614
|
+
var _serializer, _namespacedKey2, namespacedKey_fn2;
|
|
1615
|
+
var KeyValueStoreClient = class {
|
|
1616
|
+
constructor(queryStore, type = null, namespace = "") {
|
|
1617
|
+
__privateAdd(this, _namespacedKey2);
|
|
1618
|
+
__privateAdd(this, _serializer, void 0);
|
|
1619
|
+
this.queryStore = queryStore;
|
|
1620
|
+
this.type = type;
|
|
1621
|
+
this.namespace = namespace;
|
|
1622
|
+
__privateSet(this, _serializer, new JSONOutputSerializer());
|
|
1623
|
+
}
|
|
1624
|
+
async delete(key) {
|
|
1625
|
+
const result = await this.queryStore("DELETE", {
|
|
1626
|
+
key: __privateMethod(this, _namespacedKey2, namespacedKey_fn2).call(this, key)
|
|
1273
1627
|
});
|
|
1628
|
+
if (result.action !== "DELETE") {
|
|
1629
|
+
throw new Error(`Unexpected key-value store response: ${result.action}`);
|
|
1630
|
+
}
|
|
1631
|
+
return result.deleted;
|
|
1274
1632
|
}
|
|
1275
|
-
async
|
|
1276
|
-
const
|
|
1277
|
-
|
|
1278
|
-
if (!task.callbackUrl) {
|
|
1279
|
-
throw new Error("No callbackUrl found on task");
|
|
1280
|
-
}
|
|
1281
|
-
task.outputProperties = [
|
|
1282
|
-
{
|
|
1283
|
-
label: "Callback URL",
|
|
1284
|
-
text: task.callbackUrl
|
|
1285
|
-
}
|
|
1286
|
-
];
|
|
1287
|
-
return callback(task.callbackUrl);
|
|
1288
|
-
}, {
|
|
1289
|
-
name: "Wait for Request",
|
|
1290
|
-
icon: "clock",
|
|
1291
|
-
callback: {
|
|
1292
|
-
enabled: true,
|
|
1293
|
-
timeoutInSeconds: options?.timeoutInSeconds
|
|
1294
|
-
},
|
|
1295
|
-
properties: [
|
|
1296
|
-
{
|
|
1297
|
-
label: "Timeout",
|
|
1298
|
-
text: `${timeoutInSeconds}s`
|
|
1299
|
-
}
|
|
1300
|
-
]
|
|
1633
|
+
async get(key) {
|
|
1634
|
+
const result = await this.queryStore("GET", {
|
|
1635
|
+
key: __privateMethod(this, _namespacedKey2, namespacedKey_fn2).call(this, key)
|
|
1301
1636
|
});
|
|
1637
|
+
if (result.action !== "GET") {
|
|
1638
|
+
throw new Error(`Unexpected key-value store response: ${result.action}`);
|
|
1639
|
+
}
|
|
1640
|
+
return __privateGet(this, _serializer).deserialize(result.value);
|
|
1302
1641
|
}
|
|
1303
|
-
async
|
|
1304
|
-
const
|
|
1305
|
-
|
|
1306
|
-
|
|
1307
|
-
|
|
1642
|
+
async has(key) {
|
|
1643
|
+
const result = await this.queryStore("HAS", {
|
|
1644
|
+
key: __privateMethod(this, _namespacedKey2, namespacedKey_fn2).call(this, key)
|
|
1645
|
+
});
|
|
1646
|
+
if (result.action !== "HAS") {
|
|
1647
|
+
throw new Error(`Unexpected key-value store response: ${result.action}`);
|
|
1648
|
+
}
|
|
1649
|
+
return result.has;
|
|
1308
1650
|
}
|
|
1309
|
-
async
|
|
1310
|
-
const
|
|
1311
|
-
|
|
1312
|
-
|
|
1313
|
-
return task.output;
|
|
1314
|
-
}, {
|
|
1315
|
-
name: `fetch ${urlObject.hostname}${urlObject.pathname}`,
|
|
1316
|
-
params: {
|
|
1317
|
-
url,
|
|
1318
|
-
requestInit,
|
|
1319
|
-
retry: options?.retry,
|
|
1320
|
-
timeout: options?.timeout
|
|
1321
|
-
},
|
|
1322
|
-
operation: "fetch",
|
|
1323
|
-
icon: "background",
|
|
1324
|
-
noop: false,
|
|
1325
|
-
properties: [
|
|
1326
|
-
{
|
|
1327
|
-
label: "url",
|
|
1328
|
-
text: url,
|
|
1329
|
-
url
|
|
1330
|
-
},
|
|
1331
|
-
{
|
|
1332
|
-
label: "method",
|
|
1333
|
-
text: requestInit?.method ?? "GET"
|
|
1334
|
-
},
|
|
1335
|
-
{
|
|
1336
|
-
label: "background",
|
|
1337
|
-
text: "true"
|
|
1338
|
-
},
|
|
1339
|
-
...options?.timeout ? [
|
|
1340
|
-
{
|
|
1341
|
-
label: "timeout",
|
|
1342
|
-
text: `${options.timeout.durationInMs}ms`
|
|
1343
|
-
}
|
|
1344
|
-
] : []
|
|
1345
|
-
],
|
|
1346
|
-
retry: {
|
|
1347
|
-
limit: 0
|
|
1348
|
-
}
|
|
1651
|
+
async set(key, value) {
|
|
1652
|
+
const result = await this.queryStore("SET", {
|
|
1653
|
+
key: __privateMethod(this, _namespacedKey2, namespacedKey_fn2).call(this, key),
|
|
1654
|
+
value: __privateGet(this, _serializer).serialize(value)
|
|
1349
1655
|
});
|
|
1656
|
+
if (result.action !== "SET") {
|
|
1657
|
+
throw new Error(`Unexpected key-value store response: ${result.action}`);
|
|
1658
|
+
}
|
|
1659
|
+
return __privateGet(this, _serializer).deserialize(result.value);
|
|
1350
1660
|
}
|
|
1351
|
-
|
|
1352
|
-
|
|
1353
|
-
|
|
1354
|
-
|
|
1355
|
-
|
|
1356
|
-
|
|
1357
|
-
|
|
1358
|
-
|
|
1359
|
-
|
|
1360
|
-
|
|
1361
|
-
|
|
1362
|
-
|
|
1363
|
-
|
|
1364
|
-
|
|
1365
|
-
|
|
1366
|
-
|
|
1367
|
-
|
|
1368
|
-
|
|
1369
|
-
|
|
1370
|
-
|
|
1371
|
-
|
|
1372
|
-
|
|
1373
|
-
|
|
1374
|
-
|
|
1375
|
-
|
|
1376
|
-
|
|
1377
|
-
|
|
1661
|
+
};
|
|
1662
|
+
__name(KeyValueStoreClient, "KeyValueStoreClient");
|
|
1663
|
+
_serializer = new WeakMap();
|
|
1664
|
+
_namespacedKey2 = new WeakSet();
|
|
1665
|
+
namespacedKey_fn2 = /* @__PURE__ */ __name(function(key) {
|
|
1666
|
+
const parts = [];
|
|
1667
|
+
if (this.type) {
|
|
1668
|
+
parts.push(this.type);
|
|
1669
|
+
}
|
|
1670
|
+
if (this.namespace) {
|
|
1671
|
+
parts.push(this.namespace);
|
|
1672
|
+
}
|
|
1673
|
+
parts.push(key);
|
|
1674
|
+
return parts.join(":");
|
|
1675
|
+
}, "#namespacedKey");
|
|
1676
|
+
|
|
1677
|
+
// src/apiClient.ts
|
|
1678
|
+
var _apiUrl, _options, _logger, _storeClient, _queryKeyValueStore, queryKeyValueStore_fn, _apiKey, apiKey_fn;
|
|
1679
|
+
var ApiClient = class {
|
|
1680
|
+
constructor(options) {
|
|
1681
|
+
__privateAdd(this, _queryKeyValueStore);
|
|
1682
|
+
__privateAdd(this, _apiKey);
|
|
1683
|
+
__privateAdd(this, _apiUrl, void 0);
|
|
1684
|
+
__privateAdd(this, _options, void 0);
|
|
1685
|
+
__privateAdd(this, _logger, void 0);
|
|
1686
|
+
__privateAdd(this, _storeClient, void 0);
|
|
1687
|
+
__privateSet(this, _options, options);
|
|
1688
|
+
__privateSet(this, _apiUrl, __privateGet(this, _options).apiUrl ?? process.env.TRIGGER_API_URL ?? "https://api.trigger.dev");
|
|
1689
|
+
__privateSet(this, _logger, new import_core3.Logger("trigger.dev", __privateGet(this, _options).logLevel));
|
|
1690
|
+
__privateSet(this, _storeClient, new KeyValueStoreClient(__privateMethod(this, _queryKeyValueStore, queryKeyValueStore_fn).bind(this)));
|
|
1691
|
+
}
|
|
1692
|
+
async registerEndpoint(options) {
|
|
1693
|
+
const apiKey = await __privateMethod(this, _apiKey, apiKey_fn).call(this);
|
|
1694
|
+
__privateGet(this, _logger).debug("Registering endpoint", {
|
|
1695
|
+
url: options.url,
|
|
1696
|
+
name: options.name
|
|
1697
|
+
});
|
|
1698
|
+
const response = await fetch(`${__privateGet(this, _apiUrl)}/api/v1/endpoints`, {
|
|
1699
|
+
method: "POST",
|
|
1700
|
+
headers: {
|
|
1701
|
+
"Content-Type": "application/json",
|
|
1702
|
+
Authorization: `Bearer ${apiKey}`
|
|
1703
|
+
},
|
|
1704
|
+
body: JSON.stringify({
|
|
1705
|
+
url: options.url,
|
|
1706
|
+
name: options.name
|
|
1707
|
+
})
|
|
1378
1708
|
});
|
|
1709
|
+
if (response.status >= 400 && response.status < 500) {
|
|
1710
|
+
const body = await response.json();
|
|
1711
|
+
throw new Error(body.error);
|
|
1712
|
+
}
|
|
1713
|
+
if (response.status !== 200) {
|
|
1714
|
+
throw new Error(`Failed to register entry point, got status code ${response.status}`);
|
|
1715
|
+
}
|
|
1716
|
+
return await response.json();
|
|
1379
1717
|
}
|
|
1380
|
-
async
|
|
1381
|
-
const
|
|
1382
|
-
|
|
1383
|
-
|
|
1384
|
-
}
|
|
1385
|
-
|
|
1386
|
-
|
|
1387
|
-
|
|
1388
|
-
|
|
1389
|
-
|
|
1390
|
-
|
|
1718
|
+
async runTask(runId, task, options = {}) {
|
|
1719
|
+
const apiKey = await __privateMethod(this, _apiKey, apiKey_fn).call(this);
|
|
1720
|
+
__privateGet(this, _logger).debug("Running Task", {
|
|
1721
|
+
task
|
|
1722
|
+
});
|
|
1723
|
+
return await zodfetchWithVersions({
|
|
1724
|
+
[import_core3.API_VERSIONS.LAZY_LOADED_CACHED_TASKS]: import_core3.RunTaskResponseWithCachedTasksBodySchema
|
|
1725
|
+
}, import_core3.ServerTaskSchema, `${__privateGet(this, _apiUrl)}/api/v1/runs/${runId}/tasks`, {
|
|
1726
|
+
method: "POST",
|
|
1727
|
+
headers: {
|
|
1728
|
+
"Content-Type": "application/json",
|
|
1729
|
+
Authorization: `Bearer ${apiKey}`,
|
|
1730
|
+
"Idempotency-Key": task.idempotencyKey,
|
|
1731
|
+
"X-Cached-Tasks-Cursor": options.cachedTasksCursor ?? "",
|
|
1732
|
+
"Trigger-Version": import_core3.API_VERSIONS.LAZY_LOADED_CACHED_TASKS
|
|
1391
1733
|
},
|
|
1392
|
-
|
|
1393
|
-
icon: "background",
|
|
1394
|
-
noop: false,
|
|
1395
|
-
properties: [
|
|
1396
|
-
{
|
|
1397
|
-
label: "url",
|
|
1398
|
-
text: url,
|
|
1399
|
-
url
|
|
1400
|
-
},
|
|
1401
|
-
{
|
|
1402
|
-
label: "method",
|
|
1403
|
-
text: requestInit?.method ?? "GET"
|
|
1404
|
-
},
|
|
1405
|
-
{
|
|
1406
|
-
label: "background",
|
|
1407
|
-
text: "true"
|
|
1408
|
-
},
|
|
1409
|
-
...options?.timeout ? [
|
|
1410
|
-
{
|
|
1411
|
-
label: "timeout",
|
|
1412
|
-
text: `${options.timeout.durationInMs}ms`
|
|
1413
|
-
}
|
|
1414
|
-
] : []
|
|
1415
|
-
],
|
|
1416
|
-
retry: {
|
|
1417
|
-
limit: 0
|
|
1418
|
-
}
|
|
1734
|
+
body: JSON.stringify(task)
|
|
1419
1735
|
});
|
|
1420
1736
|
}
|
|
1421
|
-
async
|
|
1422
|
-
|
|
1423
|
-
|
|
1424
|
-
|
|
1425
|
-
|
|
1426
|
-
|
|
1427
|
-
|
|
1428
|
-
|
|
1737
|
+
async completeTask(runId, id, task) {
|
|
1738
|
+
const apiKey = await __privateMethod(this, _apiKey, apiKey_fn).call(this);
|
|
1739
|
+
__privateGet(this, _logger).debug("Complete Task", {
|
|
1740
|
+
task
|
|
1741
|
+
});
|
|
1742
|
+
return await zodfetch(import_core3.ServerTaskSchema, `${__privateGet(this, _apiUrl)}/api/v1/runs/${runId}/tasks/${id}/complete`, {
|
|
1743
|
+
method: "POST",
|
|
1744
|
+
headers: {
|
|
1745
|
+
"Content-Type": "application/json",
|
|
1746
|
+
Authorization: `Bearer ${apiKey}`,
|
|
1747
|
+
"Trigger-Version": import_core3.API_VERSIONS.SERIALIZED_TASK_OUTPUT
|
|
1429
1748
|
},
|
|
1430
|
-
|
|
1431
|
-
{
|
|
1432
|
-
label: "name",
|
|
1433
|
-
text: event.name
|
|
1434
|
-
},
|
|
1435
|
-
...event?.id ? [
|
|
1436
|
-
{
|
|
1437
|
-
label: "ID",
|
|
1438
|
-
text: event.id
|
|
1439
|
-
}
|
|
1440
|
-
] : [],
|
|
1441
|
-
...sendEventOptionsProperties(options)
|
|
1442
|
-
]
|
|
1749
|
+
body: JSON.stringify(task)
|
|
1443
1750
|
});
|
|
1444
1751
|
}
|
|
1445
|
-
async
|
|
1446
|
-
|
|
1447
|
-
|
|
1448
|
-
|
|
1449
|
-
|
|
1450
|
-
|
|
1451
|
-
|
|
1452
|
-
|
|
1752
|
+
async failTask(runId, id, body) {
|
|
1753
|
+
const apiKey = await __privateMethod(this, _apiKey, apiKey_fn).call(this);
|
|
1754
|
+
__privateGet(this, _logger).debug("Fail Task", {
|
|
1755
|
+
id,
|
|
1756
|
+
runId,
|
|
1757
|
+
body
|
|
1758
|
+
});
|
|
1759
|
+
return await zodfetch(import_core3.ServerTaskSchema, `${__privateGet(this, _apiUrl)}/api/v1/runs/${runId}/tasks/${id}/fail`, {
|
|
1760
|
+
method: "POST",
|
|
1761
|
+
headers: {
|
|
1762
|
+
"Content-Type": "application/json",
|
|
1763
|
+
Authorization: `Bearer ${apiKey}`
|
|
1453
1764
|
},
|
|
1454
|
-
|
|
1455
|
-
{
|
|
1456
|
-
label: "Total Events",
|
|
1457
|
-
text: String(events.length)
|
|
1458
|
-
},
|
|
1459
|
-
...sendEventOptionsProperties(options)
|
|
1460
|
-
]
|
|
1765
|
+
body: JSON.stringify(body)
|
|
1461
1766
|
});
|
|
1462
1767
|
}
|
|
1463
|
-
async
|
|
1464
|
-
|
|
1465
|
-
|
|
1466
|
-
|
|
1467
|
-
|
|
1468
|
-
|
|
1469
|
-
|
|
1768
|
+
async sendEvent(event, options = {}) {
|
|
1769
|
+
const apiKey = await __privateMethod(this, _apiKey, apiKey_fn).call(this);
|
|
1770
|
+
__privateGet(this, _logger).debug("Sending event", {
|
|
1771
|
+
event
|
|
1772
|
+
});
|
|
1773
|
+
return await zodfetch(import_core3.ApiEventLogSchema, `${__privateGet(this, _apiUrl)}/api/v1/events`, {
|
|
1774
|
+
method: "POST",
|
|
1775
|
+
headers: {
|
|
1776
|
+
"Content-Type": "application/json",
|
|
1777
|
+
Authorization: `Bearer ${apiKey}`
|
|
1470
1778
|
},
|
|
1471
|
-
|
|
1472
|
-
|
|
1473
|
-
|
|
1474
|
-
|
|
1475
|
-
}
|
|
1476
|
-
]
|
|
1779
|
+
body: JSON.stringify({
|
|
1780
|
+
event,
|
|
1781
|
+
options
|
|
1782
|
+
})
|
|
1477
1783
|
});
|
|
1478
1784
|
}
|
|
1479
|
-
async
|
|
1480
|
-
|
|
1481
|
-
|
|
1482
|
-
|
|
1483
|
-
|
|
1484
|
-
|
|
1485
|
-
|
|
1785
|
+
async sendEvents(events, options = {}) {
|
|
1786
|
+
const apiKey = await __privateMethod(this, _apiKey, apiKey_fn).call(this);
|
|
1787
|
+
__privateGet(this, _logger).debug("Sending multiple events", {
|
|
1788
|
+
events
|
|
1789
|
+
});
|
|
1790
|
+
return await zodfetch(import_core3.ApiEventLogSchema.array(), `${__privateGet(this, _apiUrl)}/api/v1/events/bulk`, {
|
|
1791
|
+
method: "POST",
|
|
1792
|
+
headers: {
|
|
1793
|
+
"Content-Type": "application/json",
|
|
1794
|
+
Authorization: `Bearer ${apiKey}`
|
|
1486
1795
|
},
|
|
1487
|
-
|
|
1488
|
-
|
|
1489
|
-
|
|
1490
|
-
|
|
1491
|
-
}
|
|
1492
|
-
]
|
|
1796
|
+
body: JSON.stringify({
|
|
1797
|
+
events,
|
|
1798
|
+
options
|
|
1799
|
+
})
|
|
1493
1800
|
});
|
|
1494
1801
|
}
|
|
1495
|
-
async
|
|
1496
|
-
|
|
1497
|
-
|
|
1498
|
-
|
|
1499
|
-
|
|
1500
|
-
|
|
1501
|
-
|
|
1502
|
-
|
|
1503
|
-
|
|
1504
|
-
|
|
1505
|
-
}
|
|
1506
|
-
],
|
|
1507
|
-
params: options,
|
|
1508
|
-
redact: {
|
|
1509
|
-
paths: [
|
|
1510
|
-
"secret"
|
|
1511
|
-
]
|
|
1802
|
+
async cancelEvent(eventId) {
|
|
1803
|
+
const apiKey = await __privateMethod(this, _apiKey, apiKey_fn).call(this);
|
|
1804
|
+
__privateGet(this, _logger).debug("Cancelling event", {
|
|
1805
|
+
eventId
|
|
1806
|
+
});
|
|
1807
|
+
return await zodfetch(import_core3.ApiEventLogSchema, `${__privateGet(this, _apiUrl)}/api/v1/events/${eventId}/cancel`, {
|
|
1808
|
+
method: "POST",
|
|
1809
|
+
headers: {
|
|
1810
|
+
"Content-Type": "application/json",
|
|
1811
|
+
Authorization: `Bearer ${apiKey}`
|
|
1512
1812
|
}
|
|
1513
1813
|
});
|
|
1514
1814
|
}
|
|
1515
|
-
async
|
|
1516
|
-
|
|
1517
|
-
|
|
1518
|
-
|
|
1519
|
-
options
|
|
1520
|
-
});
|
|
1521
|
-
}, {
|
|
1522
|
-
name: "register-interval",
|
|
1523
|
-
properties: [
|
|
1524
|
-
{
|
|
1525
|
-
label: "schedule",
|
|
1526
|
-
text: dynamicSchedule.id
|
|
1527
|
-
},
|
|
1528
|
-
{
|
|
1529
|
-
label: "id",
|
|
1530
|
-
text: id
|
|
1531
|
-
},
|
|
1532
|
-
{
|
|
1533
|
-
label: "seconds",
|
|
1534
|
-
text: options.seconds.toString()
|
|
1535
|
-
}
|
|
1536
|
-
],
|
|
1537
|
-
params: options
|
|
1815
|
+
async cancelRunsForEvent(eventId) {
|
|
1816
|
+
const apiKey = await __privateMethod(this, _apiKey, apiKey_fn).call(this);
|
|
1817
|
+
__privateGet(this, _logger).debug("Cancelling runs for event", {
|
|
1818
|
+
eventId
|
|
1538
1819
|
});
|
|
1539
|
-
|
|
1540
|
-
|
|
1541
|
-
|
|
1542
|
-
|
|
1543
|
-
|
|
1544
|
-
|
|
1545
|
-
properties: [
|
|
1546
|
-
{
|
|
1547
|
-
label: "schedule",
|
|
1548
|
-
text: dynamicSchedule.id
|
|
1549
|
-
},
|
|
1550
|
-
{
|
|
1551
|
-
label: "id",
|
|
1552
|
-
text: id
|
|
1553
|
-
}
|
|
1554
|
-
]
|
|
1820
|
+
return await zodfetch(import_core3.CancelRunsForEventSchema, `${__privateGet(this, _apiUrl)}/api/v1/events/${eventId}/cancel-runs`, {
|
|
1821
|
+
method: "POST",
|
|
1822
|
+
headers: {
|
|
1823
|
+
"Content-Type": "application/json",
|
|
1824
|
+
Authorization: `Bearer ${apiKey}`
|
|
1825
|
+
}
|
|
1555
1826
|
});
|
|
1556
1827
|
}
|
|
1557
|
-
async
|
|
1558
|
-
|
|
1559
|
-
|
|
1560
|
-
|
|
1561
|
-
|
|
1562
|
-
|
|
1563
|
-
|
|
1564
|
-
|
|
1565
|
-
|
|
1566
|
-
|
|
1567
|
-
|
|
1568
|
-
|
|
1569
|
-
|
|
1570
|
-
{
|
|
1571
|
-
label: "id",
|
|
1572
|
-
text: id
|
|
1573
|
-
},
|
|
1574
|
-
{
|
|
1575
|
-
label: "cron",
|
|
1576
|
-
text: options.cron
|
|
1577
|
-
}
|
|
1578
|
-
],
|
|
1579
|
-
params: options
|
|
1828
|
+
async updateStatus(runId, id, status) {
|
|
1829
|
+
const apiKey = await __privateMethod(this, _apiKey, apiKey_fn).call(this);
|
|
1830
|
+
__privateGet(this, _logger).debug("Update status", {
|
|
1831
|
+
id,
|
|
1832
|
+
status
|
|
1833
|
+
});
|
|
1834
|
+
return await zodfetch(import_core3.JobRunStatusRecordSchema, `${__privateGet(this, _apiUrl)}/api/v1/runs/${runId}/statuses/${id}`, {
|
|
1835
|
+
method: "PUT",
|
|
1836
|
+
headers: {
|
|
1837
|
+
"Content-Type": "application/json",
|
|
1838
|
+
Authorization: `Bearer ${apiKey}`
|
|
1839
|
+
},
|
|
1840
|
+
body: JSON.stringify(status)
|
|
1580
1841
|
});
|
|
1581
1842
|
}
|
|
1582
|
-
async
|
|
1583
|
-
|
|
1584
|
-
|
|
1585
|
-
|
|
1586
|
-
|
|
1587
|
-
|
|
1588
|
-
|
|
1589
|
-
|
|
1590
|
-
|
|
1591
|
-
}
|
|
1592
|
-
|
|
1593
|
-
|
|
1594
|
-
text: id
|
|
1595
|
-
}
|
|
1596
|
-
]
|
|
1843
|
+
async updateSource(client, key, source) {
|
|
1844
|
+
const apiKey = await __privateMethod(this, _apiKey, apiKey_fn).call(this);
|
|
1845
|
+
__privateGet(this, _logger).debug("activating http source", {
|
|
1846
|
+
source
|
|
1847
|
+
});
|
|
1848
|
+
const response = await zodfetch(import_core3.TriggerSourceSchema, `${__privateGet(this, _apiUrl)}/api/v2/${client}/sources/${key}`, {
|
|
1849
|
+
method: "PUT",
|
|
1850
|
+
headers: {
|
|
1851
|
+
"Content-Type": "application/json",
|
|
1852
|
+
Authorization: `Bearer ${apiKey}`
|
|
1853
|
+
},
|
|
1854
|
+
body: JSON.stringify(source)
|
|
1597
1855
|
});
|
|
1856
|
+
return response;
|
|
1598
1857
|
}
|
|
1599
|
-
async
|
|
1600
|
-
|
|
1601
|
-
|
|
1602
|
-
|
|
1603
|
-
|
|
1604
|
-
|
|
1605
|
-
|
|
1606
|
-
|
|
1607
|
-
|
|
1608
|
-
|
|
1609
|
-
}
|
|
1610
|
-
|
|
1611
|
-
name: "register-trigger",
|
|
1612
|
-
properties: [
|
|
1613
|
-
{
|
|
1614
|
-
label: "trigger",
|
|
1615
|
-
text: trigger.id
|
|
1616
|
-
},
|
|
1617
|
-
{
|
|
1618
|
-
label: "id",
|
|
1619
|
-
text: id
|
|
1620
|
-
}
|
|
1621
|
-
],
|
|
1622
|
-
params
|
|
1858
|
+
async updateWebhook(key, webhookData) {
|
|
1859
|
+
const apiKey = await __privateMethod(this, _apiKey, apiKey_fn).call(this);
|
|
1860
|
+
__privateGet(this, _logger).debug("activating webhook", {
|
|
1861
|
+
webhookData
|
|
1862
|
+
});
|
|
1863
|
+
const response = await zodfetch(import_core3.TriggerSourceSchema, `${__privateGet(this, _apiUrl)}/api/v1/webhooks/${key}`, {
|
|
1864
|
+
method: "PUT",
|
|
1865
|
+
headers: {
|
|
1866
|
+
"Content-Type": "application/json",
|
|
1867
|
+
Authorization: `Bearer ${apiKey}`
|
|
1868
|
+
},
|
|
1869
|
+
body: JSON.stringify(webhookData)
|
|
1623
1870
|
});
|
|
1871
|
+
return response;
|
|
1624
1872
|
}
|
|
1625
|
-
async
|
|
1626
|
-
|
|
1627
|
-
|
|
1873
|
+
async registerTrigger(client, id, key, payload, idempotencyKey) {
|
|
1874
|
+
const apiKey = await __privateMethod(this, _apiKey, apiKey_fn).call(this);
|
|
1875
|
+
__privateGet(this, _logger).debug("registering trigger", {
|
|
1876
|
+
id,
|
|
1877
|
+
payload
|
|
1878
|
+
});
|
|
1879
|
+
const headers = {
|
|
1880
|
+
"Content-Type": "application/json",
|
|
1881
|
+
Authorization: `Bearer ${apiKey}`
|
|
1882
|
+
};
|
|
1883
|
+
if (idempotencyKey) {
|
|
1884
|
+
headers["Idempotency-Key"] = idempotencyKey;
|
|
1628
1885
|
}
|
|
1629
|
-
|
|
1630
|
-
|
|
1631
|
-
|
|
1632
|
-
|
|
1886
|
+
const response = await zodfetch(import_core3.RegisterSourceEventSchemaV2, `${__privateGet(this, _apiUrl)}/api/v2/${client}/triggers/${id}/registrations/${key}`, {
|
|
1887
|
+
method: "PUT",
|
|
1888
|
+
headers,
|
|
1889
|
+
body: JSON.stringify(payload)
|
|
1633
1890
|
});
|
|
1891
|
+
return response;
|
|
1634
1892
|
}
|
|
1635
|
-
async
|
|
1636
|
-
const
|
|
1637
|
-
|
|
1638
|
-
|
|
1639
|
-
|
|
1893
|
+
async registerSchedule(client, id, key, payload) {
|
|
1894
|
+
const apiKey = await __privateMethod(this, _apiKey, apiKey_fn).call(this);
|
|
1895
|
+
__privateGet(this, _logger).debug("registering schedule", {
|
|
1896
|
+
id,
|
|
1897
|
+
payload
|
|
1898
|
+
});
|
|
1899
|
+
const response = await zodfetch(import_core3.RegisterScheduleResponseBodySchema, `${__privateGet(this, _apiUrl)}/api/v1/${client}/schedules/${id}/registrations`, {
|
|
1900
|
+
method: "POST",
|
|
1901
|
+
headers: {
|
|
1902
|
+
"Content-Type": "application/json",
|
|
1903
|
+
Authorization: `Bearer ${apiKey}`
|
|
1904
|
+
},
|
|
1905
|
+
body: JSON.stringify({
|
|
1906
|
+
id: key,
|
|
1907
|
+
...payload
|
|
1908
|
+
})
|
|
1909
|
+
});
|
|
1910
|
+
return response;
|
|
1911
|
+
}
|
|
1912
|
+
async unregisterSchedule(client, id, key) {
|
|
1913
|
+
const apiKey = await __privateMethod(this, _apiKey, apiKey_fn).call(this);
|
|
1914
|
+
__privateGet(this, _logger).debug("unregistering schedule", {
|
|
1915
|
+
id
|
|
1916
|
+
});
|
|
1917
|
+
const response = await zodfetch(import_zod3.z.object({
|
|
1918
|
+
ok: import_zod3.z.boolean()
|
|
1919
|
+
}), `${__privateGet(this, _apiUrl)}/api/v1/${client}/schedules/${id}/registrations/${encodeURIComponent(key)}`, {
|
|
1920
|
+
method: "DELETE",
|
|
1921
|
+
headers: {
|
|
1922
|
+
"Content-Type": "application/json",
|
|
1923
|
+
Authorization: `Bearer ${apiKey}`
|
|
1640
1924
|
}
|
|
1641
|
-
|
|
1642
|
-
|
|
1643
|
-
|
|
1925
|
+
});
|
|
1926
|
+
return response;
|
|
1927
|
+
}
|
|
1928
|
+
async getAuth(client, id) {
|
|
1929
|
+
const apiKey = await __privateMethod(this, _apiKey, apiKey_fn).call(this);
|
|
1930
|
+
__privateGet(this, _logger).debug("getting auth", {
|
|
1931
|
+
id
|
|
1932
|
+
});
|
|
1933
|
+
const response = await zodfetch(import_core3.ConnectionAuthSchema, `${__privateGet(this, _apiUrl)}/api/v1/${client}/auth/${id}`, {
|
|
1934
|
+
method: "GET",
|
|
1935
|
+
headers: {
|
|
1936
|
+
Accept: "application/json",
|
|
1937
|
+
Authorization: `Bearer ${apiKey}`
|
|
1644
1938
|
}
|
|
1645
|
-
const internalErrors = outcomes.filter((outcome) => outcome.status === "rejected" && isTriggerError(outcome.reason)).map((outcome) => outcome).map((outcome) => outcome.reason);
|
|
1646
|
-
throw new ResumeWithParallelTaskError(task, internalErrors);
|
|
1647
1939
|
}, {
|
|
1648
|
-
|
|
1649
|
-
parallel: true,
|
|
1650
|
-
...options ?? {}
|
|
1940
|
+
optional: true
|
|
1651
1941
|
});
|
|
1652
|
-
return
|
|
1942
|
+
return response;
|
|
1653
1943
|
}
|
|
1654
|
-
async
|
|
1655
|
-
__privateMethod(this,
|
|
1656
|
-
|
|
1657
|
-
|
|
1658
|
-
|
|
1659
|
-
|
|
1660
|
-
|
|
1661
|
-
|
|
1662
|
-
|
|
1663
|
-
}
|
|
1664
|
-
const idempotencyKey = await generateIdempotencyKey([
|
|
1665
|
-
this._id,
|
|
1666
|
-
parentId ?? "",
|
|
1667
|
-
cacheKey
|
|
1668
|
-
].flat());
|
|
1669
|
-
if (this._visitedCacheKeys.has(idempotencyKey)) {
|
|
1670
|
-
if (typeof cacheKey === "string") {
|
|
1671
|
-
throw new Error(`Task with cacheKey "${cacheKey}" has already been executed in this run. Each task must have a unique cacheKey.`);
|
|
1672
|
-
} else {
|
|
1673
|
-
throw new Error(`Task with cacheKey "${cacheKey.join("-")}" has already been executed in this run. Each task must have a unique cacheKey.`);
|
|
1674
|
-
}
|
|
1675
|
-
}
|
|
1676
|
-
this._visitedCacheKeys.add(idempotencyKey);
|
|
1677
|
-
const cachedTask = this._cachedTasks.get(idempotencyKey);
|
|
1678
|
-
if (cachedTask && cachedTask.status === "COMPLETED") {
|
|
1679
|
-
this._logger.debug("Using completed cached task", {
|
|
1680
|
-
idempotencyKey
|
|
1681
|
-
});
|
|
1682
|
-
this._stats.cachedTaskHits++;
|
|
1683
|
-
return options?.parseOutput ? options.parseOutput(cachedTask.output) : cachedTask.output;
|
|
1684
|
-
}
|
|
1685
|
-
if (options?.noop && this._noopTasksBloomFilter) {
|
|
1686
|
-
if (this._noopTasksBloomFilter.test(idempotencyKey)) {
|
|
1687
|
-
this._logger.debug("task idempotency key exists in noopTasksBloomFilter", {
|
|
1688
|
-
idempotencyKey
|
|
1689
|
-
});
|
|
1690
|
-
this._stats.noopCachedTaskHits++;
|
|
1691
|
-
return {};
|
|
1944
|
+
async getEvent(eventId) {
|
|
1945
|
+
const apiKey = await __privateMethod(this, _apiKey, apiKey_fn).call(this);
|
|
1946
|
+
__privateGet(this, _logger).debug("Getting Event", {
|
|
1947
|
+
eventId
|
|
1948
|
+
});
|
|
1949
|
+
return await zodfetch(import_core3.GetEventSchema, `${__privateGet(this, _apiUrl)}/api/v1/events/${eventId}`, {
|
|
1950
|
+
method: "GET",
|
|
1951
|
+
headers: {
|
|
1952
|
+
Authorization: `Bearer ${apiKey}`
|
|
1692
1953
|
}
|
|
1693
|
-
}
|
|
1694
|
-
const runOptions = {
|
|
1695
|
-
...options ?? {},
|
|
1696
|
-
parseOutput: void 0
|
|
1697
|
-
};
|
|
1698
|
-
const response = await this._apiClient.runTask(this._id, {
|
|
1699
|
-
idempotencyKey,
|
|
1700
|
-
displayKey: typeof cacheKey === "string" ? cacheKey : void 0,
|
|
1701
|
-
noop: false,
|
|
1702
|
-
...runOptions ?? {},
|
|
1703
|
-
parentId
|
|
1704
|
-
}, {
|
|
1705
|
-
cachedTasksCursor: this._cachedTasksCursor
|
|
1706
1954
|
});
|
|
1707
|
-
|
|
1708
|
-
|
|
1709
|
-
|
|
1710
|
-
|
|
1711
|
-
|
|
1712
|
-
|
|
1713
|
-
}
|
|
1714
|
-
|
|
1715
|
-
|
|
1716
|
-
|
|
1717
|
-
if (!this._cachedTasks.has(cachedTask2.idempotencyKey)) {
|
|
1718
|
-
this._cachedTasks.set(cachedTask2.idempotencyKey, cachedTask2);
|
|
1719
|
-
this._logger.debug("Injecting lazy loaded task into task cache", {
|
|
1720
|
-
idempotencyKey: cachedTask2.idempotencyKey
|
|
1721
|
-
});
|
|
1722
|
-
this._stats.lazyLoadedCachedTasks++;
|
|
1723
|
-
}
|
|
1955
|
+
}
|
|
1956
|
+
async getRun(runId, options) {
|
|
1957
|
+
const apiKey = await __privateMethod(this, _apiKey, apiKey_fn).call(this);
|
|
1958
|
+
__privateGet(this, _logger).debug("Getting Run", {
|
|
1959
|
+
runId
|
|
1960
|
+
});
|
|
1961
|
+
return await zodfetch(import_core3.GetRunSchema, (0, import_core3.urlWithSearchParams)(`${__privateGet(this, _apiUrl)}/api/v1/runs/${runId}`, options), {
|
|
1962
|
+
method: "GET",
|
|
1963
|
+
headers: {
|
|
1964
|
+
Authorization: `Bearer ${apiKey}`
|
|
1724
1965
|
}
|
|
1725
|
-
}
|
|
1726
|
-
|
|
1727
|
-
|
|
1728
|
-
|
|
1729
|
-
|
|
1730
|
-
|
|
1731
|
-
|
|
1732
|
-
}
|
|
1733
|
-
|
|
1734
|
-
|
|
1735
|
-
|
|
1736
|
-
|
|
1737
|
-
});
|
|
1738
|
-
this._noopTasksBloomFilter?.add(task.idempotencyKey);
|
|
1739
|
-
} else {
|
|
1740
|
-
this._logger.debug("Cache miss", {
|
|
1741
|
-
idempotencyKey
|
|
1742
|
-
});
|
|
1743
|
-
this._stats.cachedTaskMisses++;
|
|
1744
|
-
__privateMethod(this, _addToCachedTasks, addToCachedTasks_fn).call(this, task);
|
|
1966
|
+
});
|
|
1967
|
+
}
|
|
1968
|
+
async cancelRun(runId) {
|
|
1969
|
+
const apiKey = await __privateMethod(this, _apiKey, apiKey_fn).call(this);
|
|
1970
|
+
__privateGet(this, _logger).debug("Cancelling Run", {
|
|
1971
|
+
runId
|
|
1972
|
+
});
|
|
1973
|
+
return await zodfetch(import_core3.GetRunSchema, `${__privateGet(this, _apiUrl)}/api/v1/runs/${runId}/cancel`, {
|
|
1974
|
+
method: "POST",
|
|
1975
|
+
headers: {
|
|
1976
|
+
"Content-Type": "application/json",
|
|
1977
|
+
Authorization: `Bearer ${apiKey}`
|
|
1745
1978
|
}
|
|
1746
|
-
|
|
1747
|
-
|
|
1748
|
-
|
|
1749
|
-
|
|
1750
|
-
|
|
1751
|
-
|
|
1752
|
-
|
|
1753
|
-
|
|
1754
|
-
|
|
1755
|
-
|
|
1756
|
-
|
|
1757
|
-
try {
|
|
1758
|
-
const result = await callback(task, this);
|
|
1759
|
-
if (task.status === "WAITING" && task.callbackUrl) {
|
|
1760
|
-
this._logger.debug("Waiting for remote callback", {
|
|
1761
|
-
idempotencyKey,
|
|
1762
|
-
task
|
|
1763
|
-
});
|
|
1764
|
-
return {};
|
|
1765
|
-
}
|
|
1766
|
-
const output = this._outputSerializer.serialize(result);
|
|
1767
|
-
this._logger.debug("Completing using output", {
|
|
1768
|
-
idempotencyKey,
|
|
1769
|
-
task
|
|
1770
|
-
});
|
|
1771
|
-
__privateMethod(this, _detectAutoYield, detectAutoYield_fn).call(this, "before_complete_task", 500, task, output);
|
|
1772
|
-
const completedTask = await this._apiClient.completeTask(this._id, task.id, {
|
|
1773
|
-
output,
|
|
1774
|
-
properties: task.outputProperties ?? void 0
|
|
1775
|
-
});
|
|
1776
|
-
if (completedTask.forceYield) {
|
|
1777
|
-
this._logger.debug("Forcing yield after task completed", {
|
|
1778
|
-
idempotencyKey
|
|
1779
|
-
});
|
|
1780
|
-
__privateMethod(this, _forceYield, forceYield_fn).call(this, "after_complete_task");
|
|
1781
|
-
}
|
|
1782
|
-
this._stats.executedTasks++;
|
|
1783
|
-
if (completedTask.status === "CANCELED") {
|
|
1784
|
-
throw new CanceledWithTaskError(completedTask);
|
|
1785
|
-
}
|
|
1786
|
-
__privateMethod(this, _detectAutoYield, detectAutoYield_fn).call(this, "after_complete_task", 500);
|
|
1787
|
-
const deserializedOutput = this._outputSerializer.deserialize(output);
|
|
1788
|
-
return options?.parseOutput ? options.parseOutput(deserializedOutput) : deserializedOutput;
|
|
1789
|
-
} catch (error) {
|
|
1790
|
-
if (isTriggerError(error)) {
|
|
1791
|
-
throw error;
|
|
1792
|
-
}
|
|
1793
|
-
let skipRetrying = false;
|
|
1794
|
-
if (onError) {
|
|
1795
|
-
try {
|
|
1796
|
-
const onErrorResult = onError(error, task, this);
|
|
1797
|
-
if (onErrorResult) {
|
|
1798
|
-
if (onErrorResult instanceof Error) {
|
|
1799
|
-
error = onErrorResult;
|
|
1800
|
-
} else {
|
|
1801
|
-
skipRetrying = !!onErrorResult.skipRetrying;
|
|
1802
|
-
if (onErrorResult.retryAt && !skipRetrying) {
|
|
1803
|
-
const parsedError2 = import_core4.ErrorWithStackSchema.safeParse(onErrorResult.error);
|
|
1804
|
-
throw new RetryWithTaskError(parsedError2.success ? parsedError2.data : {
|
|
1805
|
-
message: "Unknown error"
|
|
1806
|
-
}, task, onErrorResult.retryAt);
|
|
1807
|
-
}
|
|
1808
|
-
}
|
|
1809
|
-
}
|
|
1810
|
-
} catch (innerError) {
|
|
1811
|
-
if (isTriggerError(innerError)) {
|
|
1812
|
-
throw innerError;
|
|
1813
|
-
}
|
|
1814
|
-
error = innerError;
|
|
1815
|
-
}
|
|
1816
|
-
}
|
|
1817
|
-
if (error instanceof ErrorWithTask) {
|
|
1818
|
-
await this._apiClient.failTask(this._id, task.id, {
|
|
1819
|
-
error: error.cause.output
|
|
1820
|
-
});
|
|
1821
|
-
}
|
|
1822
|
-
const parsedError = import_core4.ErrorWithStackSchema.safeParse(error);
|
|
1823
|
-
if (options?.retry && !skipRetrying) {
|
|
1824
|
-
const retryAt = (0, import_core3.calculateRetryAt)(options.retry, task.attempts - 1);
|
|
1825
|
-
if (retryAt) {
|
|
1826
|
-
throw new RetryWithTaskError(parsedError.success ? parsedError.data : {
|
|
1827
|
-
message: "Unknown error"
|
|
1828
|
-
}, task, retryAt);
|
|
1829
|
-
}
|
|
1830
|
-
}
|
|
1831
|
-
if (parsedError.success) {
|
|
1832
|
-
await this._apiClient.failTask(this._id, task.id, {
|
|
1833
|
-
error: parsedError.data
|
|
1834
|
-
});
|
|
1835
|
-
} else {
|
|
1836
|
-
const message = typeof error === "string" ? error : JSON.stringify(error);
|
|
1837
|
-
await this._apiClient.failTask(this._id, task.id, {
|
|
1838
|
-
error: {
|
|
1839
|
-
name: "Unknown error",
|
|
1840
|
-
message
|
|
1841
|
-
}
|
|
1842
|
-
});
|
|
1843
|
-
}
|
|
1844
|
-
throw error;
|
|
1979
|
+
});
|
|
1980
|
+
}
|
|
1981
|
+
async getRunStatuses(runId) {
|
|
1982
|
+
const apiKey = await __privateMethod(this, _apiKey, apiKey_fn).call(this);
|
|
1983
|
+
__privateGet(this, _logger).debug("Getting Run statuses", {
|
|
1984
|
+
runId
|
|
1985
|
+
});
|
|
1986
|
+
return await zodfetch(import_core3.GetRunStatusesSchema, `${__privateGet(this, _apiUrl)}/api/v1/runs/${runId}/statuses`, {
|
|
1987
|
+
method: "GET",
|
|
1988
|
+
headers: {
|
|
1989
|
+
Authorization: `Bearer ${apiKey}`
|
|
1845
1990
|
}
|
|
1846
|
-
}
|
|
1847
|
-
|
|
1848
|
-
|
|
1849
|
-
|
|
1850
|
-
|
|
1851
|
-
|
|
1852
|
-
|
|
1853
|
-
|
|
1854
|
-
|
|
1855
|
-
|
|
1991
|
+
});
|
|
1992
|
+
}
|
|
1993
|
+
async getRuns(jobSlug, options) {
|
|
1994
|
+
const apiKey = await __privateMethod(this, _apiKey, apiKey_fn).call(this);
|
|
1995
|
+
__privateGet(this, _logger).debug("Getting Runs", {
|
|
1996
|
+
jobSlug
|
|
1997
|
+
});
|
|
1998
|
+
return await zodfetch(import_core3.GetRunsSchema, (0, import_core3.urlWithSearchParams)(`${__privateGet(this, _apiUrl)}/api/v1/jobs/${jobSlug}/runs`, options), {
|
|
1999
|
+
method: "GET",
|
|
2000
|
+
headers: {
|
|
2001
|
+
Authorization: `Bearer ${apiKey}`
|
|
1856
2002
|
}
|
|
1857
|
-
|
|
1858
|
-
}
|
|
1859
|
-
if (task.status === "RUNNING" && typeof task.operation === "string") {
|
|
1860
|
-
this._logger.debug("Task running operation", {
|
|
1861
|
-
idempotencyKey,
|
|
1862
|
-
task
|
|
1863
|
-
});
|
|
1864
|
-
throw new ResumeWithTaskError(task);
|
|
1865
|
-
}
|
|
1866
|
-
return this._taskStorage.run({
|
|
1867
|
-
taskId: task.id
|
|
1868
|
-
}, executeTask);
|
|
2003
|
+
});
|
|
1869
2004
|
}
|
|
1870
|
-
|
|
1871
|
-
|
|
1872
|
-
|
|
1873
|
-
|
|
2005
|
+
async invokeJob(jobId, payload, options = {}) {
|
|
2006
|
+
const apiKey = await __privateMethod(this, _apiKey, apiKey_fn).call(this);
|
|
2007
|
+
__privateGet(this, _logger).debug("Invoking Job", {
|
|
2008
|
+
jobId
|
|
2009
|
+
});
|
|
2010
|
+
const body = {
|
|
2011
|
+
payload,
|
|
2012
|
+
context: options.context ?? {},
|
|
2013
|
+
options: {
|
|
2014
|
+
accountId: options.accountId,
|
|
2015
|
+
callbackUrl: options.callbackUrl
|
|
2016
|
+
}
|
|
2017
|
+
};
|
|
2018
|
+
return await zodfetch(import_core3.InvokeJobResponseSchema, `${__privateGet(this, _apiUrl)}/api/v1/jobs/${jobId}/invoke`, {
|
|
2019
|
+
method: "POST",
|
|
2020
|
+
headers: {
|
|
2021
|
+
"Content-Type": "application/json",
|
|
2022
|
+
Authorization: `Bearer ${apiKey}`,
|
|
2023
|
+
...options.idempotencyKey ? {
|
|
2024
|
+
"Idempotency-Key": options.idempotencyKey
|
|
2025
|
+
} : {}
|
|
2026
|
+
},
|
|
2027
|
+
body: JSON.stringify(body)
|
|
2028
|
+
});
|
|
2029
|
+
}
|
|
2030
|
+
async createEphemeralEventDispatcher(payload) {
|
|
2031
|
+
const apiKey = await __privateMethod(this, _apiKey, apiKey_fn).call(this);
|
|
2032
|
+
__privateGet(this, _logger).debug("Creating ephemeral event dispatcher", {
|
|
2033
|
+
payload
|
|
2034
|
+
});
|
|
2035
|
+
const response = await zodfetch(import_core3.EphemeralEventDispatcherResponseBodySchema, `${__privateGet(this, _apiUrl)}/api/v1/event-dispatchers/ephemeral`, {
|
|
2036
|
+
method: "POST",
|
|
2037
|
+
headers: {
|
|
2038
|
+
"Content-Type": "application/json",
|
|
2039
|
+
Authorization: `Bearer ${apiKey}`
|
|
2040
|
+
},
|
|
2041
|
+
body: JSON.stringify(payload)
|
|
2042
|
+
});
|
|
2043
|
+
return response;
|
|
2044
|
+
}
|
|
2045
|
+
get store() {
|
|
2046
|
+
return __privateGet(this, _storeClient);
|
|
2047
|
+
}
|
|
2048
|
+
};
|
|
2049
|
+
__name(ApiClient, "ApiClient");
|
|
2050
|
+
_apiUrl = new WeakMap();
|
|
2051
|
+
_options = new WeakMap();
|
|
2052
|
+
_logger = new WeakMap();
|
|
2053
|
+
_storeClient = new WeakMap();
|
|
2054
|
+
_queryKeyValueStore = new WeakSet();
|
|
2055
|
+
queryKeyValueStore_fn = /* @__PURE__ */ __name(async function(action, data) {
|
|
2056
|
+
const apiKey = await __privateMethod(this, _apiKey, apiKey_fn).call(this);
|
|
2057
|
+
__privateGet(this, _logger).debug("accessing key-value store", {
|
|
2058
|
+
action,
|
|
2059
|
+
data
|
|
2060
|
+
});
|
|
2061
|
+
const encodedKey = encodeURIComponent(data.key);
|
|
2062
|
+
const STORE_URL = `${__privateGet(this, _apiUrl)}/api/v1/store/${encodedKey}`;
|
|
2063
|
+
const authHeader = {
|
|
2064
|
+
Authorization: `Bearer ${apiKey}`
|
|
2065
|
+
};
|
|
2066
|
+
let requestInit;
|
|
2067
|
+
switch (action) {
|
|
2068
|
+
case "DELETE": {
|
|
2069
|
+
requestInit = {
|
|
2070
|
+
method: "DELETE",
|
|
2071
|
+
headers: authHeader
|
|
2072
|
+
};
|
|
2073
|
+
break;
|
|
1874
2074
|
}
|
|
1875
|
-
|
|
1876
|
-
|
|
2075
|
+
case "GET": {
|
|
2076
|
+
requestInit = {
|
|
2077
|
+
method: "GET",
|
|
2078
|
+
headers: authHeader
|
|
2079
|
+
};
|
|
2080
|
+
break;
|
|
1877
2081
|
}
|
|
1878
|
-
|
|
1879
|
-
|
|
1880
|
-
|
|
1881
|
-
|
|
1882
|
-
return
|
|
1883
|
-
|
|
1884
|
-
|
|
1885
|
-
|
|
2082
|
+
case "HAS": {
|
|
2083
|
+
const headResponse = await fetchHead(STORE_URL, {
|
|
2084
|
+
headers: authHeader
|
|
2085
|
+
});
|
|
2086
|
+
return {
|
|
2087
|
+
action: "HAS",
|
|
2088
|
+
key: encodedKey,
|
|
2089
|
+
has: !!headResponse.ok
|
|
2090
|
+
};
|
|
2091
|
+
}
|
|
2092
|
+
case "SET": {
|
|
2093
|
+
const MAX_BODY_BYTE_LENGTH = 256 * 1024;
|
|
2094
|
+
if ((data.value?.length ?? 0) > MAX_BODY_BYTE_LENGTH) {
|
|
2095
|
+
throw new Error(`Max request body size exceeded: ${MAX_BODY_BYTE_LENGTH} bytes`);
|
|
1886
2096
|
}
|
|
1887
|
-
|
|
2097
|
+
requestInit = {
|
|
2098
|
+
method: "PUT",
|
|
2099
|
+
headers: {
|
|
2100
|
+
...authHeader,
|
|
2101
|
+
"Content-Type": "text/plain"
|
|
2102
|
+
},
|
|
2103
|
+
body: data.value
|
|
2104
|
+
};
|
|
2105
|
+
break;
|
|
1888
2106
|
}
|
|
1889
|
-
|
|
1890
|
-
|
|
1891
|
-
__name(IO, "IO");
|
|
1892
|
-
_addToCachedTasks = new WeakSet();
|
|
1893
|
-
addToCachedTasks_fn = /* @__PURE__ */ __name(function(task) {
|
|
1894
|
-
this._cachedTasks.set(task.idempotencyKey, task);
|
|
1895
|
-
}, "#addToCachedTasks");
|
|
1896
|
-
_detectAutoYield = new WeakSet();
|
|
1897
|
-
detectAutoYield_fn = /* @__PURE__ */ __name(function(location, threshold = 1500, task1, output) {
|
|
1898
|
-
const timeRemaining = __privateMethod(this, _getRemainingTimeInMillis, getRemainingTimeInMillis_fn).call(this);
|
|
1899
|
-
if (timeRemaining && timeRemaining < threshold) {
|
|
1900
|
-
if (task1) {
|
|
1901
|
-
throw new AutoYieldWithCompletedTaskExecutionError(task1.id, task1.outputProperties ?? [], {
|
|
1902
|
-
location,
|
|
1903
|
-
timeRemaining,
|
|
1904
|
-
timeElapsed: __privateMethod(this, _getTimeElapsed, getTimeElapsed_fn).call(this)
|
|
1905
|
-
}, output);
|
|
1906
|
-
} else {
|
|
1907
|
-
throw new AutoYieldExecutionError(location, timeRemaining, __privateMethod(this, _getTimeElapsed, getTimeElapsed_fn).call(this));
|
|
2107
|
+
default: {
|
|
2108
|
+
(0, import_core3.assertExhaustive)(action);
|
|
1908
2109
|
}
|
|
1909
2110
|
}
|
|
1910
|
-
|
|
1911
|
-
|
|
1912
|
-
|
|
1913
|
-
|
|
1914
|
-
|
|
1915
|
-
|
|
2111
|
+
const response = await zodfetch(import_core3.KeyValueStoreResponseBodySchema, STORE_URL, requestInit);
|
|
2112
|
+
return response;
|
|
2113
|
+
}, "#queryKeyValueStore");
|
|
2114
|
+
_apiKey = new WeakSet();
|
|
2115
|
+
apiKey_fn = /* @__PURE__ */ __name(async function() {
|
|
2116
|
+
const apiKey = getApiKey(__privateGet(this, _options).apiKey);
|
|
2117
|
+
if (apiKey.status === "invalid") {
|
|
2118
|
+
throw new Error("Invalid API key");
|
|
2119
|
+
} else if (apiKey.status === "missing") {
|
|
2120
|
+
throw new Error("Missing API key");
|
|
1916
2121
|
}
|
|
1917
|
-
|
|
1918
|
-
|
|
1919
|
-
|
|
1920
|
-
|
|
1921
|
-
|
|
1922
|
-
|
|
1923
|
-
|
|
1924
|
-
|
|
1925
|
-
return this._executionTimeout - (performance.now() - this._timeOrigin);
|
|
2122
|
+
return apiKey.apiKey;
|
|
2123
|
+
}, "#apiKey");
|
|
2124
|
+
function getApiKey(key) {
|
|
2125
|
+
const apiKey = key ?? process.env.TRIGGER_API_KEY;
|
|
2126
|
+
if (!apiKey) {
|
|
2127
|
+
return {
|
|
2128
|
+
status: "missing"
|
|
2129
|
+
};
|
|
1926
2130
|
}
|
|
1927
|
-
|
|
1928
|
-
|
|
1929
|
-
|
|
1930
|
-
|
|
1931
|
-
|
|
1932
|
-
|
|
1933
|
-
|
|
1934
|
-
|
|
2131
|
+
const isValid = apiKey.match(/^tr_[a-z]+_[a-zA-Z0-9]+$/);
|
|
2132
|
+
if (!isValid) {
|
|
2133
|
+
return {
|
|
2134
|
+
status: "invalid",
|
|
2135
|
+
apiKey
|
|
2136
|
+
};
|
|
2137
|
+
}
|
|
2138
|
+
return {
|
|
2139
|
+
status: "valid",
|
|
2140
|
+
apiKey
|
|
2141
|
+
};
|
|
2142
|
+
}
|
|
2143
|
+
__name(getApiKey, "getApiKey");
|
|
2144
|
+
async function zodfetchWithVersions(versionedSchemaMap, unversionedSchema, url, requestInit, options, retryCount = 0) {
|
|
2145
|
+
const response = await fetch(url, {
|
|
2146
|
+
...requestInit,
|
|
2147
|
+
cache: "no-cache"
|
|
1935
2148
|
});
|
|
1936
|
-
|
|
1937
|
-
|
|
1938
|
-
|
|
2149
|
+
if ((!requestInit || requestInit.method === "GET") && response.status === 404 && options?.optional) {
|
|
2150
|
+
return;
|
|
2151
|
+
}
|
|
2152
|
+
if (response.status >= 400 && response.status < 500) {
|
|
2153
|
+
const body = await response.json();
|
|
2154
|
+
throw new Error(body.error);
|
|
2155
|
+
}
|
|
2156
|
+
if (response.status >= 500 && retryCount < 6) {
|
|
2157
|
+
const delay = exponentialBackoff(retryCount + 1, 2, 50, 1150, 50);
|
|
2158
|
+
await new Promise((resolve) => setTimeout(resolve, delay));
|
|
2159
|
+
return zodfetchWithVersions(versionedSchemaMap, unversionedSchema, url, requestInit, options, retryCount + 1);
|
|
2160
|
+
}
|
|
2161
|
+
if (response.status !== 200) {
|
|
2162
|
+
throw new Error(options?.errorMessage ?? `Failed to fetch ${url}, got status code ${response.status}`);
|
|
2163
|
+
}
|
|
2164
|
+
const jsonBody = await response.json();
|
|
2165
|
+
const version2 = response.headers.get("trigger-version");
|
|
2166
|
+
if (!version2) {
|
|
2167
|
+
return {
|
|
2168
|
+
version: "unversioned",
|
|
2169
|
+
body: unversionedSchema.parse(jsonBody)
|
|
2170
|
+
};
|
|
2171
|
+
}
|
|
2172
|
+
const versionedSchema = versionedSchemaMap[version2];
|
|
2173
|
+
if (!versionedSchema) {
|
|
2174
|
+
throw new Error(`Unknown version ${version2}`);
|
|
2175
|
+
}
|
|
2176
|
+
return {
|
|
2177
|
+
version: version2,
|
|
2178
|
+
body: versionedSchema.parse(jsonBody)
|
|
2179
|
+
};
|
|
1939
2180
|
}
|
|
1940
|
-
__name(
|
|
1941
|
-
function
|
|
1942
|
-
|
|
1943
|
-
|
|
1944
|
-
|
|
1945
|
-
|
|
1946
|
-
|
|
1947
|
-
|
|
1948
|
-
|
|
1949
|
-
|
|
1950
|
-
|
|
1951
|
-
|
|
1952
|
-
|
|
1953
|
-
|
|
1954
|
-
return sortedObj2;
|
|
2181
|
+
__name(zodfetchWithVersions, "zodfetchWithVersions");
|
|
2182
|
+
async function fetchHead(url, requestInitWithoutMethod, retryCount = 0) {
|
|
2183
|
+
const requestInit = {
|
|
2184
|
+
...requestInitWithoutMethod,
|
|
2185
|
+
method: "HEAD"
|
|
2186
|
+
};
|
|
2187
|
+
const response = await fetch(url, {
|
|
2188
|
+
...requestInit,
|
|
2189
|
+
cache: "no-cache"
|
|
2190
|
+
});
|
|
2191
|
+
if (response.status >= 500 && retryCount < 6) {
|
|
2192
|
+
const delay = exponentialBackoff(retryCount + 1, 2, 50, 1150, 50);
|
|
2193
|
+
await new Promise((resolve) => setTimeout(resolve, delay));
|
|
2194
|
+
return fetchHead(url, requestInitWithoutMethod, retryCount + 1);
|
|
1955
2195
|
}
|
|
1956
|
-
|
|
1957
|
-
const sortedObj = sortKeys(obj);
|
|
1958
|
-
return JSON.stringify(sortedObj);
|
|
2196
|
+
return response;
|
|
1959
2197
|
}
|
|
1960
|
-
__name(
|
|
1961
|
-
|
|
1962
|
-
|
|
1963
|
-
|
|
2198
|
+
__name(fetchHead, "fetchHead");
|
|
2199
|
+
async function zodfetch(schema, url, requestInit, options, retryCount = 0) {
|
|
2200
|
+
const response = await fetch(url, {
|
|
2201
|
+
...requestInit,
|
|
2202
|
+
cache: "no-cache"
|
|
2203
|
+
});
|
|
2204
|
+
if ((!requestInit || requestInit.method === "GET") && response.status === 404 && options?.optional) {
|
|
2205
|
+
return;
|
|
1964
2206
|
}
|
|
1965
|
-
|
|
1966
|
-
|
|
2207
|
+
if (response.status >= 400 && response.status < 500) {
|
|
2208
|
+
const body = await response.json();
|
|
2209
|
+
throw new Error(body.error);
|
|
1967
2210
|
}
|
|
1968
|
-
|
|
1969
|
-
|
|
2211
|
+
if (response.status >= 500 && retryCount < 6) {
|
|
2212
|
+
const delay = exponentialBackoff(retryCount + 1, 2, 50, 1150, 50);
|
|
2213
|
+
await new Promise((resolve) => setTimeout(resolve, delay));
|
|
2214
|
+
return zodfetch(schema, url, requestInit, options, retryCount + 1);
|
|
2215
|
+
}
|
|
2216
|
+
if (response.status !== 200) {
|
|
2217
|
+
throw new Error(options?.errorMessage ?? `Failed to fetch ${url}, got status code ${response.status}`);
|
|
2218
|
+
}
|
|
2219
|
+
const jsonBody = await response.json();
|
|
2220
|
+
return schema.parse(jsonBody);
|
|
2221
|
+
}
|
|
2222
|
+
__name(zodfetch, "zodfetch");
|
|
2223
|
+
function exponentialBackoff(retryCount, exponential, minDelay, maxDelay, jitter) {
|
|
2224
|
+
const delay = Math.min(Math.pow(exponential, retryCount) * minDelay, maxDelay);
|
|
2225
|
+
const jitterValue = Math.random() * jitter;
|
|
2226
|
+
return delay + jitterValue;
|
|
2227
|
+
}
|
|
2228
|
+
__name(exponentialBackoff, "exponentialBackoff");
|
|
2229
|
+
|
|
2230
|
+
// src/httpEndpoint.ts
|
|
2231
|
+
var import_core4 = require("@trigger.dev/core");
|
|
2232
|
+
|
|
2233
|
+
// src/utils/formatSchemaErrors.ts
|
|
2234
|
+
function formatSchemaErrors(errors) {
|
|
2235
|
+
return errors.map((error) => {
|
|
2236
|
+
const { path, message } = error;
|
|
2237
|
+
return {
|
|
2238
|
+
path: path.map(String),
|
|
2239
|
+
message
|
|
2240
|
+
};
|
|
2241
|
+
});
|
|
2242
|
+
}
|
|
2243
|
+
__name(formatSchemaErrors, "formatSchemaErrors");
|
|
2244
|
+
|
|
2245
|
+
// src/httpEndpoint.ts
|
|
2246
|
+
var HttpEndpoint = class {
|
|
2247
|
+
constructor(options) {
|
|
2248
|
+
this.options = options;
|
|
2249
|
+
}
|
|
2250
|
+
get id() {
|
|
2251
|
+
return this.options.id;
|
|
2252
|
+
}
|
|
2253
|
+
onRequest(options) {
|
|
2254
|
+
return new HttpTrigger({
|
|
2255
|
+
endpointId: this.id,
|
|
2256
|
+
event: this.options.event,
|
|
2257
|
+
filter: options?.filter,
|
|
2258
|
+
verify: this.options.verify
|
|
2259
|
+
});
|
|
2260
|
+
}
|
|
2261
|
+
async handleRequest(request) {
|
|
2262
|
+
if (!this.options.respondWith)
|
|
2263
|
+
return;
|
|
2264
|
+
return this.options.respondWith.handler(request, () => {
|
|
2265
|
+
const clonedRequest = request.clone();
|
|
2266
|
+
return this.options.verify(clonedRequest);
|
|
2267
|
+
});
|
|
2268
|
+
}
|
|
2269
|
+
toJSON() {
|
|
2270
|
+
return {
|
|
2271
|
+
id: this.id,
|
|
2272
|
+
icon: this.options.event.icon,
|
|
2273
|
+
version: "1",
|
|
2274
|
+
enabled: this.options.enabled ?? true,
|
|
2275
|
+
event: this.options.event,
|
|
2276
|
+
immediateResponseFilter: this.options.respondWith?.filter,
|
|
2277
|
+
skipTriggeringRuns: this.options.respondWith?.skipTriggeringRuns,
|
|
2278
|
+
source: this.options.event.source
|
|
2279
|
+
};
|
|
2280
|
+
}
|
|
2281
|
+
};
|
|
2282
|
+
__name(HttpEndpoint, "HttpEndpoint");
|
|
2283
|
+
var HttpTrigger = /* @__PURE__ */ __name(class HttpTrigger2 {
|
|
2284
|
+
constructor(options) {
|
|
2285
|
+
this.options = options;
|
|
2286
|
+
}
|
|
2287
|
+
toJSON() {
|
|
2288
|
+
return {
|
|
2289
|
+
type: "static",
|
|
2290
|
+
title: this.options.endpointId,
|
|
2291
|
+
properties: this.options.event.properties,
|
|
2292
|
+
rule: {
|
|
2293
|
+
event: `httpendpoint.${this.options.endpointId}`,
|
|
2294
|
+
payload: this.options.filter ?? {},
|
|
2295
|
+
source: this.options.event.source
|
|
2296
|
+
},
|
|
2297
|
+
link: `http-endpoints/${this.options.endpointId}`,
|
|
2298
|
+
help: {
|
|
2299
|
+
noRuns: {
|
|
2300
|
+
text: "To start triggering runs click here to setup your HTTP Endpoint with the external API service you want to receive webhooks from.",
|
|
2301
|
+
link: `http-endpoints/${this.options.endpointId}`
|
|
2302
|
+
}
|
|
2303
|
+
}
|
|
2304
|
+
};
|
|
2305
|
+
}
|
|
2306
|
+
get event() {
|
|
2307
|
+
return this.options.event;
|
|
1970
2308
|
}
|
|
1971
|
-
|
|
1972
|
-
return this.callback("INFO", message, properties);
|
|
2309
|
+
attachToJob(triggerClient, job) {
|
|
1973
2310
|
}
|
|
1974
|
-
|
|
1975
|
-
return
|
|
2311
|
+
get preprocessRuns() {
|
|
2312
|
+
return false;
|
|
1976
2313
|
}
|
|
1977
|
-
|
|
1978
|
-
|
|
2314
|
+
async verifyPayload(payload) {
|
|
2315
|
+
const clonedRequest = payload.clone();
|
|
2316
|
+
return this.options.verify(clonedRequest);
|
|
1979
2317
|
}
|
|
1980
|
-
};
|
|
1981
|
-
|
|
1982
|
-
|
|
1983
|
-
|
|
1984
|
-
|
|
1985
|
-
|
|
1986
|
-
|
|
1987
|
-
|
|
1988
|
-
|
|
1989
|
-
|
|
1990
|
-
|
|
1991
|
-
|
|
1992
|
-
|
|
1993
|
-
|
|
1994
|
-
|
|
1995
|
-
|
|
1996
|
-
|
|
1997
|
-
|
|
1998
|
-
|
|
1999
|
-
|
|
2000
|
-
|
|
2001
|
-
|
|
2002
|
-
|
|
2003
|
-
|
|
2004
|
-
|
|
2318
|
+
}, "HttpTrigger");
|
|
2319
|
+
function httpEndpoint(options) {
|
|
2320
|
+
const id = slugifyId(options.id);
|
|
2321
|
+
return new HttpEndpoint({
|
|
2322
|
+
id,
|
|
2323
|
+
enabled: options.enabled,
|
|
2324
|
+
respondWith: options.respondWith,
|
|
2325
|
+
verify: options.verify,
|
|
2326
|
+
event: {
|
|
2327
|
+
name: id,
|
|
2328
|
+
title: options.title ?? "HTTP Trigger",
|
|
2329
|
+
source: options.source,
|
|
2330
|
+
icon: options.icon ?? "webhook",
|
|
2331
|
+
properties: options.properties,
|
|
2332
|
+
examples: options.examples ? options.examples : [
|
|
2333
|
+
{
|
|
2334
|
+
id: "basic-request",
|
|
2335
|
+
name: "Basic Request",
|
|
2336
|
+
icon: "http-post",
|
|
2337
|
+
payload: {
|
|
2338
|
+
url: "https://cloud.trigger.dev",
|
|
2339
|
+
method: "POST",
|
|
2340
|
+
headers: {
|
|
2341
|
+
"Content-Type": "application/json"
|
|
2342
|
+
},
|
|
2343
|
+
rawBody: JSON.stringify({
|
|
2344
|
+
foo: "bar"
|
|
2345
|
+
})
|
|
2346
|
+
}
|
|
2347
|
+
}
|
|
2348
|
+
],
|
|
2349
|
+
parsePayload: (rawPayload) => {
|
|
2350
|
+
const result = import_core4.RequestWithRawBodySchema.safeParse(rawPayload);
|
|
2351
|
+
if (!result.success) {
|
|
2352
|
+
throw new ParsedPayloadSchemaError(formatSchemaErrors(result.error.issues));
|
|
2353
|
+
}
|
|
2354
|
+
return new Request(new URL(result.data.url), {
|
|
2355
|
+
method: result.data.method,
|
|
2356
|
+
headers: result.data.headers,
|
|
2357
|
+
body: result.data.rawBody
|
|
2358
|
+
});
|
|
2005
2359
|
}
|
|
2006
|
-
|
|
2007
|
-
|
|
2360
|
+
}
|
|
2361
|
+
});
|
|
2008
2362
|
}
|
|
2009
|
-
__name(
|
|
2363
|
+
__name(httpEndpoint, "httpEndpoint");
|
|
2010
2364
|
|
|
2011
2365
|
// src/ioWithIntegrations.ts
|
|
2012
2366
|
function createIOWithIntegrations(io, auths, integrations) {
|
|
@@ -2158,6 +2512,12 @@ var EventTrigger = class {
|
|
|
2158
2512
|
return false;
|
|
2159
2513
|
}
|
|
2160
2514
|
async verifyPayload(payload) {
|
|
2515
|
+
if (__privateGet(this, _options3).verify) {
|
|
2516
|
+
if (payload instanceof Request) {
|
|
2517
|
+
const clonedRequest = payload.clone();
|
|
2518
|
+
return __privateGet(this, _options3).verify(clonedRequest);
|
|
2519
|
+
}
|
|
2520
|
+
}
|
|
2161
2521
|
return {
|
|
2162
2522
|
success: true
|
|
2163
2523
|
};
|
|
@@ -2401,9 +2761,30 @@ __name(DynamicSchedule, "DynamicSchedule");
|
|
|
2401
2761
|
var import_node_events = __toESM(require("events"));
|
|
2402
2762
|
|
|
2403
2763
|
// package.json
|
|
2404
|
-
var version = "2.2.
|
|
2764
|
+
var version = "2.2.9";
|
|
2765
|
+
|
|
2766
|
+
// src/concurrencyLimit.ts
|
|
2767
|
+
var ConcurrencyLimit = class {
|
|
2768
|
+
constructor(options) {
|
|
2769
|
+
this.options = options;
|
|
2770
|
+
}
|
|
2771
|
+
get id() {
|
|
2772
|
+
return this.options.id;
|
|
2773
|
+
}
|
|
2774
|
+
get limit() {
|
|
2775
|
+
return this.options.limit;
|
|
2776
|
+
}
|
|
2777
|
+
};
|
|
2778
|
+
__name(ConcurrencyLimit, "ConcurrencyLimit");
|
|
2405
2779
|
|
|
2406
2780
|
// src/triggerClient.ts
|
|
2781
|
+
var registerWebhookEvent = /* @__PURE__ */ __name((key) => ({
|
|
2782
|
+
name: `${import_core8.REGISTER_WEBHOOK}.${key}`,
|
|
2783
|
+
title: "Register Webhook",
|
|
2784
|
+
source: "internal",
|
|
2785
|
+
icon: "webhook",
|
|
2786
|
+
parsePayload: import_core8.RegisterWebhookPayloadSchema.parse
|
|
2787
|
+
}), "registerWebhookEvent");
|
|
2407
2788
|
var registerSourceEvent = {
|
|
2408
2789
|
name: import_core8.REGISTER_SOURCE_EVENT_V2,
|
|
2409
2790
|
title: "Register Source",
|
|
@@ -2411,7 +2792,7 @@ var registerSourceEvent = {
|
|
|
2411
2792
|
icon: "register-source",
|
|
2412
2793
|
parsePayload: import_core8.RegisterSourceEventSchemaV2.parse
|
|
2413
2794
|
};
|
|
2414
|
-
var _options4, _registeredJobs, _registeredSources, _registeredHttpSourceHandlers, _registeredDynamicTriggers, _jobMetadataByDynamicTriggers, _registeredSchedules, _registeredHttpEndpoints, _authResolvers, _eventEmitter, _client2, _internalLogger, _preprocessRun, preprocessRun_fn, _executeJob, executeJob_fn, _convertErrorToExecutionResponse, convertErrorToExecutionResponse_fn, _createRunContext, createRunContext_fn, _createPreprocessRunContext, createPreprocessRunContext_fn, _handleHttpSourceRequest, handleHttpSourceRequest_fn, _handleHttpEndpointRequestForResponse, handleHttpEndpointRequestForResponse_fn, _resolveConnections, resolveConnections_fn, _resolveConnection, resolveConnection_fn, _buildJobsIndex, buildJobsIndex_fn, _buildJobIndex, buildJobIndex_fn, _buildJobIntegrations, buildJobIntegrations_fn, _buildJobIntegration, buildJobIntegration_fn, _logIOStats, logIOStats_fn, _standardResponseHeaders, standardResponseHeaders_fn, _serializeRunMetadata, serializeRunMetadata_fn, _deliverSuccessfulRunNotification, deliverSuccessfulRunNotification_fn, _deliverFailedRunNotification, deliverFailedRunNotification_fn;
|
|
2795
|
+
var _options4, _registeredJobs, _registeredSources, _registeredWebhooks, _registeredHttpSourceHandlers, _registeredWebhookSourceHandlers, _registeredDynamicTriggers, _jobMetadataByDynamicTriggers, _registeredSchedules, _registeredHttpEndpoints, _authResolvers, _envStore, _eventEmitter, _client2, _internalLogger, _preprocessRun, preprocessRun_fn, _executeJob, executeJob_fn, _convertErrorToExecutionResponse, convertErrorToExecutionResponse_fn, _createRunContext, createRunContext_fn, _createPreprocessRunContext, createPreprocessRunContext_fn, _handleHttpSourceRequest, handleHttpSourceRequest_fn, _handleHttpEndpointRequestForResponse, handleHttpEndpointRequestForResponse_fn, _handleWebhookRequest, handleWebhookRequest_fn, _resolveConnections, resolveConnections_fn, _resolveConnection, resolveConnection_fn, _buildJobsIndex, buildJobsIndex_fn, _buildJobIndex, buildJobIndex_fn, _buildJobIntegrations, buildJobIntegrations_fn, _buildJobIntegration, buildJobIntegration_fn, _logIOStats, logIOStats_fn, _standardResponseHeaders, standardResponseHeaders_fn, _serializeRunMetadata, serializeRunMetadata_fn, _deliverSuccessfulRunNotification, deliverSuccessfulRunNotification_fn, _deliverFailedRunNotification, deliverFailedRunNotification_fn;
|
|
2415
2796
|
var TriggerClient = class {
|
|
2416
2797
|
constructor(options) {
|
|
2417
2798
|
__privateAdd(this, _preprocessRun);
|
|
@@ -2421,6 +2802,7 @@ var TriggerClient = class {
|
|
|
2421
2802
|
__privateAdd(this, _createPreprocessRunContext);
|
|
2422
2803
|
__privateAdd(this, _handleHttpSourceRequest);
|
|
2423
2804
|
__privateAdd(this, _handleHttpEndpointRequestForResponse);
|
|
2805
|
+
__privateAdd(this, _handleWebhookRequest);
|
|
2424
2806
|
__privateAdd(this, _resolveConnections);
|
|
2425
2807
|
__privateAdd(this, _resolveConnection);
|
|
2426
2808
|
__privateAdd(this, _buildJobsIndex);
|
|
@@ -2435,12 +2817,15 @@ var TriggerClient = class {
|
|
|
2435
2817
|
__privateAdd(this, _options4, void 0);
|
|
2436
2818
|
__privateAdd(this, _registeredJobs, {});
|
|
2437
2819
|
__privateAdd(this, _registeredSources, {});
|
|
2820
|
+
__privateAdd(this, _registeredWebhooks, {});
|
|
2438
2821
|
__privateAdd(this, _registeredHttpSourceHandlers, {});
|
|
2822
|
+
__privateAdd(this, _registeredWebhookSourceHandlers, {});
|
|
2439
2823
|
__privateAdd(this, _registeredDynamicTriggers, {});
|
|
2440
2824
|
__privateAdd(this, _jobMetadataByDynamicTriggers, {});
|
|
2441
2825
|
__privateAdd(this, _registeredSchedules, {});
|
|
2442
2826
|
__privateAdd(this, _registeredHttpEndpoints, {});
|
|
2443
2827
|
__privateAdd(this, _authResolvers, {});
|
|
2828
|
+
__privateAdd(this, _envStore, void 0);
|
|
2444
2829
|
__privateAdd(this, _eventEmitter, new import_node_events.default());
|
|
2445
2830
|
__privateAdd(this, _client2, void 0);
|
|
2446
2831
|
__privateAdd(this, _internalLogger, void 0);
|
|
@@ -2452,6 +2837,7 @@ var TriggerClient = class {
|
|
|
2452
2837
|
"output",
|
|
2453
2838
|
"noopTasksSet"
|
|
2454
2839
|
]));
|
|
2840
|
+
__privateSet(this, _envStore, new KeyValueStore(__privateGet(this, _client2)));
|
|
2455
2841
|
}
|
|
2456
2842
|
async handleRequest(request, timeOrigin = performance.now()) {
|
|
2457
2843
|
__privateGet(this, _internalLogger).debug("handling request", {
|
|
@@ -2548,6 +2934,7 @@ var TriggerClient = class {
|
|
|
2548
2934
|
const body = {
|
|
2549
2935
|
jobs: __privateMethod(this, _buildJobsIndex, buildJobsIndex_fn).call(this),
|
|
2550
2936
|
sources: Object.values(__privateGet(this, _registeredSources)),
|
|
2937
|
+
webhooks: Object.values(__privateGet(this, _registeredWebhooks)),
|
|
2551
2938
|
dynamicTriggers: Object.values(__privateGet(this, _registeredDynamicTriggers)).map((trigger) => ({
|
|
2552
2939
|
id: trigger.id,
|
|
2553
2940
|
jobs: __privateGet(this, _jobMetadataByDynamicTriggers)[trigger.id] ?? [],
|
|
@@ -2742,6 +3129,48 @@ var TriggerClient = class {
|
|
|
2742
3129
|
headers: __privateMethod(this, _standardResponseHeaders, standardResponseHeaders_fn).call(this, timeOrigin)
|
|
2743
3130
|
};
|
|
2744
3131
|
}
|
|
3132
|
+
case "DELIVER_WEBHOOK_REQUEST": {
|
|
3133
|
+
const headers = import_core8.WebhookSourceRequestHeadersSchema.safeParse(Object.fromEntries(request.headers.entries()));
|
|
3134
|
+
if (!headers.success) {
|
|
3135
|
+
return {
|
|
3136
|
+
status: 400,
|
|
3137
|
+
body: {
|
|
3138
|
+
message: "Invalid headers"
|
|
3139
|
+
}
|
|
3140
|
+
};
|
|
3141
|
+
}
|
|
3142
|
+
const sourceRequestNeedsBody = headers.data["x-ts-http-method"] !== "GET";
|
|
3143
|
+
const sourceRequestInit = {
|
|
3144
|
+
method: headers.data["x-ts-http-method"],
|
|
3145
|
+
headers: headers.data["x-ts-http-headers"],
|
|
3146
|
+
body: sourceRequestNeedsBody ? request.body : void 0
|
|
3147
|
+
};
|
|
3148
|
+
if (sourceRequestNeedsBody) {
|
|
3149
|
+
try {
|
|
3150
|
+
sourceRequestInit.duplex = "half";
|
|
3151
|
+
} catch (error2) {
|
|
3152
|
+
}
|
|
3153
|
+
}
|
|
3154
|
+
const webhookRequest = new Request(headers.data["x-ts-http-url"], sourceRequestInit);
|
|
3155
|
+
const key = headers.data["x-ts-key"];
|
|
3156
|
+
const secret = headers.data["x-ts-secret"];
|
|
3157
|
+
const params = headers.data["x-ts-params"];
|
|
3158
|
+
const ctx = {
|
|
3159
|
+
key,
|
|
3160
|
+
secret,
|
|
3161
|
+
params
|
|
3162
|
+
};
|
|
3163
|
+
const { response, verified, error } = await __privateMethod(this, _handleWebhookRequest, handleWebhookRequest_fn).call(this, webhookRequest, ctx);
|
|
3164
|
+
return {
|
|
3165
|
+
status: 200,
|
|
3166
|
+
body: {
|
|
3167
|
+
response,
|
|
3168
|
+
verified,
|
|
3169
|
+
error
|
|
3170
|
+
},
|
|
3171
|
+
headers: __privateMethod(this, _standardResponseHeaders, standardResponseHeaders_fn).call(this, timeOrigin)
|
|
3172
|
+
};
|
|
3173
|
+
}
|
|
2745
3174
|
case "VALIDATE": {
|
|
2746
3175
|
return {
|
|
2747
3176
|
status: 200,
|
|
@@ -2806,15 +3235,18 @@ var TriggerClient = class {
|
|
|
2806
3235
|
defineDynamicTrigger(options) {
|
|
2807
3236
|
return new DynamicTrigger(this, options);
|
|
2808
3237
|
}
|
|
2809
|
-
defineHttpEndpoint(options) {
|
|
3238
|
+
defineHttpEndpoint(options, suppressWarnings = false) {
|
|
2810
3239
|
const existingHttpEndpoint = __privateGet(this, _registeredHttpEndpoints)[options.id];
|
|
2811
|
-
if (existingHttpEndpoint) {
|
|
3240
|
+
if (!suppressWarnings && existingHttpEndpoint) {
|
|
2812
3241
|
console.warn((0, import_colorette.yellow)(`[@trigger.dev/sdk] Warning: The HttpEndpoint "${existingHttpEndpoint.id}" you're attempting to define has already been defined. Please assign a different ID to the HttpEndpoint.`));
|
|
2813
3242
|
}
|
|
2814
3243
|
const endpoint = httpEndpoint(options);
|
|
2815
3244
|
__privateGet(this, _registeredHttpEndpoints)[endpoint.id] = endpoint;
|
|
2816
3245
|
return endpoint;
|
|
2817
3246
|
}
|
|
3247
|
+
defineConcurrencyLimit(options) {
|
|
3248
|
+
return new ConcurrencyLimit(options);
|
|
3249
|
+
}
|
|
2818
3250
|
attach(job) {
|
|
2819
3251
|
__privateGet(this, _registeredJobs)[job.id] = job;
|
|
2820
3252
|
job.trigger.attachToJob(this, job);
|
|
@@ -2929,6 +3361,94 @@ var TriggerClient = class {
|
|
|
2929
3361
|
});
|
|
2930
3362
|
__privateGet(this, _registeredSchedules)[key] = jobs;
|
|
2931
3363
|
}
|
|
3364
|
+
attachWebhook(options) {
|
|
3365
|
+
const { source } = options;
|
|
3366
|
+
__privateGet(this, _registeredWebhookSourceHandlers)[options.key] = {
|
|
3367
|
+
verify: source.verify.bind(source),
|
|
3368
|
+
generateEvents: source.generateEvents.bind(source)
|
|
3369
|
+
};
|
|
3370
|
+
let registeredWebhook = __privateGet(this, _registeredWebhooks)[options.key];
|
|
3371
|
+
if (!registeredWebhook) {
|
|
3372
|
+
registeredWebhook = {
|
|
3373
|
+
key: options.key,
|
|
3374
|
+
params: options.params,
|
|
3375
|
+
config: options.config,
|
|
3376
|
+
integration: {
|
|
3377
|
+
id: source.integration.id,
|
|
3378
|
+
metadata: source.integration.metadata,
|
|
3379
|
+
authSource: source.integration.authSource
|
|
3380
|
+
},
|
|
3381
|
+
httpEndpoint: {
|
|
3382
|
+
id: options.key
|
|
3383
|
+
}
|
|
3384
|
+
};
|
|
3385
|
+
} else {
|
|
3386
|
+
registeredWebhook.config = deepMergeOptions(registeredWebhook.config, options.config);
|
|
3387
|
+
}
|
|
3388
|
+
__privateGet(this, _registeredWebhooks)[options.key] = registeredWebhook;
|
|
3389
|
+
new Job(this, {
|
|
3390
|
+
id: `webhook.register.${options.key}`,
|
|
3391
|
+
name: `webhook.register.${options.key}`,
|
|
3392
|
+
version: source.version,
|
|
3393
|
+
trigger: new EventTrigger({
|
|
3394
|
+
event: registerWebhookEvent(options.key)
|
|
3395
|
+
}),
|
|
3396
|
+
integrations: {
|
|
3397
|
+
integration: source.integration
|
|
3398
|
+
},
|
|
3399
|
+
run: async (registerPayload, io, ctx) => {
|
|
3400
|
+
return await io.try(async () => {
|
|
3401
|
+
__privateGet(this, _internalLogger).debug("[webhook.register] Start");
|
|
3402
|
+
const crudOptions = {
|
|
3403
|
+
io,
|
|
3404
|
+
ctx: registerPayload
|
|
3405
|
+
};
|
|
3406
|
+
if (!registerPayload.active) {
|
|
3407
|
+
__privateGet(this, _internalLogger).debug("[webhook.register] Not active, run create");
|
|
3408
|
+
await io.try(async () => {
|
|
3409
|
+
await source.crud.create(crudOptions);
|
|
3410
|
+
}, async (error) => {
|
|
3411
|
+
__privateGet(this, _internalLogger).debug("[webhook.register] Error during create, re-trying with delete first", {
|
|
3412
|
+
error
|
|
3413
|
+
});
|
|
3414
|
+
await io.runTask("create-retry", async () => {
|
|
3415
|
+
await source.crud.delete(crudOptions);
|
|
3416
|
+
await source.crud.create(crudOptions);
|
|
3417
|
+
});
|
|
3418
|
+
});
|
|
3419
|
+
return await io.updateWebhook("update-webhook-success", {
|
|
3420
|
+
key: options.key,
|
|
3421
|
+
active: true,
|
|
3422
|
+
config: registerPayload.config.desired
|
|
3423
|
+
});
|
|
3424
|
+
}
|
|
3425
|
+
__privateGet(this, _internalLogger).debug("[webhook.register] Already active, run update");
|
|
3426
|
+
if (source.crud.update) {
|
|
3427
|
+
await source.crud.update(crudOptions);
|
|
3428
|
+
} else {
|
|
3429
|
+
__privateGet(this, _internalLogger).debug("[webhook.register] Run delete and create instead of update");
|
|
3430
|
+
await source.crud.delete(crudOptions);
|
|
3431
|
+
await source.crud.create(crudOptions);
|
|
3432
|
+
}
|
|
3433
|
+
return await io.updateWebhook("update-webhook-success", {
|
|
3434
|
+
key: options.key,
|
|
3435
|
+
active: true,
|
|
3436
|
+
config: registerPayload.config.desired
|
|
3437
|
+
});
|
|
3438
|
+
}, async (error) => {
|
|
3439
|
+
__privateGet(this, _internalLogger).debug("[webhook.register] Error", {
|
|
3440
|
+
error
|
|
3441
|
+
});
|
|
3442
|
+
await io.updateWebhook("update-webhook-error", {
|
|
3443
|
+
key: options.key,
|
|
3444
|
+
active: false
|
|
3445
|
+
});
|
|
3446
|
+
throw error;
|
|
3447
|
+
});
|
|
3448
|
+
},
|
|
3449
|
+
__internal: true
|
|
3450
|
+
});
|
|
3451
|
+
}
|
|
2932
3452
|
async registerTrigger(id, key, options, idempotencyKey) {
|
|
2933
3453
|
return __privateGet(this, _client2).registerTrigger(this.id, id, key, options, idempotencyKey);
|
|
2934
3454
|
}
|
|
@@ -2977,6 +3497,11 @@ var TriggerClient = class {
|
|
|
2977
3497
|
async createEphemeralEventDispatcher(payload) {
|
|
2978
3498
|
return __privateGet(this, _client2).createEphemeralEventDispatcher(payload);
|
|
2979
3499
|
}
|
|
3500
|
+
get store() {
|
|
3501
|
+
return {
|
|
3502
|
+
env: __privateGet(this, _envStore)
|
|
3503
|
+
};
|
|
3504
|
+
}
|
|
2980
3505
|
authorized(apiKey) {
|
|
2981
3506
|
if (typeof apiKey !== "string") {
|
|
2982
3507
|
return "missing-header";
|
|
@@ -2995,12 +3520,15 @@ __name(TriggerClient, "TriggerClient");
|
|
|
2995
3520
|
_options4 = new WeakMap();
|
|
2996
3521
|
_registeredJobs = new WeakMap();
|
|
2997
3522
|
_registeredSources = new WeakMap();
|
|
3523
|
+
_registeredWebhooks = new WeakMap();
|
|
2998
3524
|
_registeredHttpSourceHandlers = new WeakMap();
|
|
3525
|
+
_registeredWebhookSourceHandlers = new WeakMap();
|
|
2999
3526
|
_registeredDynamicTriggers = new WeakMap();
|
|
3000
3527
|
_jobMetadataByDynamicTriggers = new WeakMap();
|
|
3001
3528
|
_registeredSchedules = new WeakMap();
|
|
3002
3529
|
_registeredHttpEndpoints = new WeakMap();
|
|
3003
3530
|
_authResolvers = new WeakMap();
|
|
3531
|
+
_envStore = new WeakMap();
|
|
3004
3532
|
_eventEmitter = new WeakMap();
|
|
3005
3533
|
_client2 = new WeakMap();
|
|
3006
3534
|
_internalLogger = new WeakMap();
|
|
@@ -3025,6 +3553,7 @@ executeJob_fn = /* @__PURE__ */ __name(async function(body1, job1, timeOrigin, t
|
|
|
3025
3553
|
const context = __privateMethod(this, _createRunContext, createRunContext_fn).call(this, body1);
|
|
3026
3554
|
const io = new IO({
|
|
3027
3555
|
id: body1.run.id,
|
|
3556
|
+
jobId: job1.id,
|
|
3028
3557
|
cachedTasks: body1.tasks,
|
|
3029
3558
|
cachedTasksCursor: body1.cachedTaskCursor,
|
|
3030
3559
|
yieldedExecutions: body1.yieldedExecutions ?? [],
|
|
@@ -3369,8 +3898,44 @@ handleHttpEndpointRequestForResponse_fn = /* @__PURE__ */ __name(async function(
|
|
|
3369
3898
|
response
|
|
3370
3899
|
};
|
|
3371
3900
|
}, "#handleHttpEndpointRequestForResponse");
|
|
3901
|
+
_handleWebhookRequest = new WeakSet();
|
|
3902
|
+
handleWebhookRequest_fn = /* @__PURE__ */ __name(async function(request, ctx) {
|
|
3903
|
+
__privateGet(this, _internalLogger).debug("Handling webhook request", {
|
|
3904
|
+
ctx
|
|
3905
|
+
});
|
|
3906
|
+
const okResponse = {
|
|
3907
|
+
status: 200,
|
|
3908
|
+
body: {
|
|
3909
|
+
ok: true
|
|
3910
|
+
}
|
|
3911
|
+
};
|
|
3912
|
+
const handlers = __privateGet(this, _registeredWebhookSourceHandlers)[ctx.key];
|
|
3913
|
+
if (!handlers) {
|
|
3914
|
+
__privateGet(this, _internalLogger).debug("No handler registered for webhook", {
|
|
3915
|
+
ctx
|
|
3916
|
+
});
|
|
3917
|
+
return {
|
|
3918
|
+
response: okResponse,
|
|
3919
|
+
verified: false
|
|
3920
|
+
};
|
|
3921
|
+
}
|
|
3922
|
+
const { verify, generateEvents } = handlers;
|
|
3923
|
+
const verifyResult = await verify(request, this, ctx);
|
|
3924
|
+
if (!verifyResult.success) {
|
|
3925
|
+
return {
|
|
3926
|
+
response: okResponse,
|
|
3927
|
+
verified: false,
|
|
3928
|
+
error: verifyResult.reason
|
|
3929
|
+
};
|
|
3930
|
+
}
|
|
3931
|
+
await generateEvents(request, this, ctx);
|
|
3932
|
+
return {
|
|
3933
|
+
response: okResponse,
|
|
3934
|
+
verified: true
|
|
3935
|
+
};
|
|
3936
|
+
}, "#handleWebhookRequest");
|
|
3372
3937
|
_resolveConnections = new WeakSet();
|
|
3373
|
-
resolveConnections_fn = /* @__PURE__ */ __name(async function(
|
|
3938
|
+
resolveConnections_fn = /* @__PURE__ */ __name(async function(ctx1, integrations, connections) {
|
|
3374
3939
|
if (!integrations) {
|
|
3375
3940
|
return {
|
|
3376
3941
|
ok: true,
|
|
@@ -3380,7 +3945,7 @@ resolveConnections_fn = /* @__PURE__ */ __name(async function(ctx, integrations,
|
|
|
3380
3945
|
const resolvedAuthResults = await Promise.all(Object.keys(integrations).map(async (key) => {
|
|
3381
3946
|
const integration = integrations[key];
|
|
3382
3947
|
const auth = (connections ?? {})[key];
|
|
3383
|
-
const result = await __privateMethod(this, _resolveConnection, resolveConnection_fn).call(this,
|
|
3948
|
+
const result = await __privateMethod(this, _resolveConnection, resolveConnection_fn).call(this, ctx1, integration, auth);
|
|
3384
3949
|
if (result.ok) {
|
|
3385
3950
|
return {
|
|
3386
3951
|
ok: true,
|
|
@@ -3422,7 +3987,7 @@ resolveConnections_fn = /* @__PURE__ */ __name(async function(ctx, integrations,
|
|
|
3422
3987
|
}
|
|
3423
3988
|
}, "#resolveConnections");
|
|
3424
3989
|
_resolveConnection = new WeakSet();
|
|
3425
|
-
resolveConnection_fn = /* @__PURE__ */ __name(async function(
|
|
3990
|
+
resolveConnection_fn = /* @__PURE__ */ __name(async function(ctx2, integration, auth) {
|
|
3426
3991
|
if (auth) {
|
|
3427
3992
|
return {
|
|
3428
3993
|
ok: true,
|
|
@@ -3443,7 +4008,7 @@ resolveConnection_fn = /* @__PURE__ */ __name(async function(ctx1, integration,
|
|
|
3443
4008
|
};
|
|
3444
4009
|
}
|
|
3445
4010
|
try {
|
|
3446
|
-
const resolvedAuth = await authResolver(
|
|
4011
|
+
const resolvedAuth = await authResolver(ctx2, integration);
|
|
3447
4012
|
if (!resolvedAuth) {
|
|
3448
4013
|
return {
|
|
3449
4014
|
ok: false,
|
|
@@ -3497,7 +4062,11 @@ buildJobIndex_fn = /* @__PURE__ */ __name(function(job2) {
|
|
|
3497
4062
|
startPosition: "latest",
|
|
3498
4063
|
enabled: job2.enabled,
|
|
3499
4064
|
preprocessRuns: job2.trigger.preprocessRuns,
|
|
3500
|
-
internal
|
|
4065
|
+
internal,
|
|
4066
|
+
concurrencyLimit: typeof job2.options.concurrencyLimit === "number" ? job2.options.concurrencyLimit : typeof job2.options.concurrencyLimit === "object" ? {
|
|
4067
|
+
id: job2.options.concurrencyLimit.id,
|
|
4068
|
+
limit: job2.options.concurrencyLimit.limit
|
|
4069
|
+
} : void 0
|
|
3501
4070
|
};
|
|
3502
4071
|
}, "#buildJobIndex");
|
|
3503
4072
|
_buildJobIntegrations = new WeakSet();
|
|
@@ -3874,9 +4443,152 @@ function invokeTrigger(options) {
|
|
|
3874
4443
|
}
|
|
3875
4444
|
__name(invokeTrigger, "invokeTrigger");
|
|
3876
4445
|
|
|
4446
|
+
// src/triggers/webhook.ts
|
|
4447
|
+
var import_core11 = require("@trigger.dev/core");
|
|
4448
|
+
var import_node_crypto2 = require("crypto");
|
|
4449
|
+
var _shortHash, shortHash_fn;
|
|
4450
|
+
var WebhookSource = class {
|
|
4451
|
+
constructor(options) {
|
|
4452
|
+
__privateAdd(this, _shortHash);
|
|
4453
|
+
this.options = options;
|
|
4454
|
+
}
|
|
4455
|
+
async generateEvents(request, client, ctx) {
|
|
4456
|
+
return this.options.generateEvents({
|
|
4457
|
+
request,
|
|
4458
|
+
client,
|
|
4459
|
+
ctx
|
|
4460
|
+
});
|
|
4461
|
+
}
|
|
4462
|
+
filter(params, config) {
|
|
4463
|
+
return this.options.filter?.(params, config) ?? {};
|
|
4464
|
+
}
|
|
4465
|
+
properties(params) {
|
|
4466
|
+
return this.options.properties?.(params) ?? [];
|
|
4467
|
+
}
|
|
4468
|
+
get crud() {
|
|
4469
|
+
return this.options.crud;
|
|
4470
|
+
}
|
|
4471
|
+
async register(params, registerEvent, io, ctx) {
|
|
4472
|
+
if (!this.options.register) {
|
|
4473
|
+
return;
|
|
4474
|
+
}
|
|
4475
|
+
const updates = await this.options.register({
|
|
4476
|
+
...registerEvent,
|
|
4477
|
+
params
|
|
4478
|
+
}, io, ctx);
|
|
4479
|
+
return updates;
|
|
4480
|
+
}
|
|
4481
|
+
async verify(request, client, ctx) {
|
|
4482
|
+
if (this.options.verify) {
|
|
4483
|
+
const clonedRequest = request.clone();
|
|
4484
|
+
return this.options.verify({
|
|
4485
|
+
request: clonedRequest,
|
|
4486
|
+
client,
|
|
4487
|
+
ctx
|
|
4488
|
+
});
|
|
4489
|
+
}
|
|
4490
|
+
return {
|
|
4491
|
+
success: true
|
|
4492
|
+
};
|
|
4493
|
+
}
|
|
4494
|
+
key(params) {
|
|
4495
|
+
const parts = [
|
|
4496
|
+
"webhook"
|
|
4497
|
+
];
|
|
4498
|
+
parts.push(this.options.key(params));
|
|
4499
|
+
parts.push(this.integration.id);
|
|
4500
|
+
return `${this.options.id}-${__privateMethod(this, _shortHash, shortHash_fn).call(this, parts.join(""))}`;
|
|
4501
|
+
}
|
|
4502
|
+
get integration() {
|
|
4503
|
+
return this.options.integration;
|
|
4504
|
+
}
|
|
4505
|
+
get integrationConfig() {
|
|
4506
|
+
return {
|
|
4507
|
+
id: this.integration.id,
|
|
4508
|
+
metadata: this.integration.metadata
|
|
4509
|
+
};
|
|
4510
|
+
}
|
|
4511
|
+
get id() {
|
|
4512
|
+
return this.options.id;
|
|
4513
|
+
}
|
|
4514
|
+
get version() {
|
|
4515
|
+
return this.options.version;
|
|
4516
|
+
}
|
|
4517
|
+
};
|
|
4518
|
+
__name(WebhookSource, "WebhookSource");
|
|
4519
|
+
_shortHash = new WeakSet();
|
|
4520
|
+
shortHash_fn = /* @__PURE__ */ __name(function(str) {
|
|
4521
|
+
const hash = (0, import_node_crypto2.createHash)("sha1").update(str).digest("hex");
|
|
4522
|
+
return hash.slice(0, 7);
|
|
4523
|
+
}, "#shortHash");
|
|
4524
|
+
var WebhookTrigger = class {
|
|
4525
|
+
constructor(options) {
|
|
4526
|
+
this.options = options;
|
|
4527
|
+
}
|
|
4528
|
+
get event() {
|
|
4529
|
+
return this.options.event;
|
|
4530
|
+
}
|
|
4531
|
+
get source() {
|
|
4532
|
+
return this.options.source;
|
|
4533
|
+
}
|
|
4534
|
+
get key() {
|
|
4535
|
+
return slugifyId(this.options.source.key(this.options.params));
|
|
4536
|
+
}
|
|
4537
|
+
toJSON() {
|
|
4538
|
+
return {
|
|
4539
|
+
type: "static",
|
|
4540
|
+
title: "Webhook",
|
|
4541
|
+
rule: {
|
|
4542
|
+
event: this.event.name,
|
|
4543
|
+
payload: (0, import_core11.deepMergeFilters)(this.options.source.filter(this.options.params, this.options.config), this.event.filter ?? {}),
|
|
4544
|
+
source: this.event.source
|
|
4545
|
+
},
|
|
4546
|
+
properties: this.options.source.properties(this.options.params),
|
|
4547
|
+
link: `http-endpoints/${this.key}`
|
|
4548
|
+
};
|
|
4549
|
+
}
|
|
4550
|
+
filter(eventFilter) {
|
|
4551
|
+
const { event, ...optionsWithoutEvent } = this.options;
|
|
4552
|
+
const { filter, ...eventWithoutFilter } = event;
|
|
4553
|
+
return new WebhookTrigger({
|
|
4554
|
+
...optionsWithoutEvent,
|
|
4555
|
+
event: {
|
|
4556
|
+
...eventWithoutFilter,
|
|
4557
|
+
filter: (0, import_core11.deepMergeFilters)(filter ?? {}, eventFilter)
|
|
4558
|
+
}
|
|
4559
|
+
});
|
|
4560
|
+
}
|
|
4561
|
+
attachToJob(triggerClient, job) {
|
|
4562
|
+
triggerClient.defineHttpEndpoint({
|
|
4563
|
+
id: this.key,
|
|
4564
|
+
source: "trigger.dev",
|
|
4565
|
+
icon: this.event.icon,
|
|
4566
|
+
verify: async () => ({
|
|
4567
|
+
success: true
|
|
4568
|
+
})
|
|
4569
|
+
}, true);
|
|
4570
|
+
triggerClient.attachWebhook({
|
|
4571
|
+
key: this.key,
|
|
4572
|
+
source: this.options.source,
|
|
4573
|
+
event: this.options.event,
|
|
4574
|
+
params: this.options.params,
|
|
4575
|
+
config: this.options.config
|
|
4576
|
+
});
|
|
4577
|
+
}
|
|
4578
|
+
get preprocessRuns() {
|
|
4579
|
+
return true;
|
|
4580
|
+
}
|
|
4581
|
+
async verifyPayload(payload) {
|
|
4582
|
+
return {
|
|
4583
|
+
success: true
|
|
4584
|
+
};
|
|
4585
|
+
}
|
|
4586
|
+
};
|
|
4587
|
+
__name(WebhookTrigger, "WebhookTrigger");
|
|
4588
|
+
|
|
3877
4589
|
// src/security.ts
|
|
3878
4590
|
var import_crypto = __toESM(require("crypto"));
|
|
3879
|
-
async function verifyRequestSignature({ request, headerName, secret, algorithm }) {
|
|
4591
|
+
async function verifyRequestSignature({ request, headerName, headerEncoding = "hex", secret, algorithm }) {
|
|
3880
4592
|
if (!secret) {
|
|
3881
4593
|
return {
|
|
3882
4594
|
success: false,
|
|
@@ -3892,7 +4604,7 @@ async function verifyRequestSignature({ request, headerName, secret, algorithm }
|
|
|
3892
4604
|
}
|
|
3893
4605
|
switch (algorithm) {
|
|
3894
4606
|
case "sha256":
|
|
3895
|
-
const success = verifyHmacSha256(headerValue, secret, await request.text());
|
|
4607
|
+
const success = verifyHmacSha256(headerValue, headerEncoding, secret, await request.text());
|
|
3896
4608
|
if (success) {
|
|
3897
4609
|
return {
|
|
3898
4610
|
success
|
|
@@ -3908,9 +4620,9 @@ async function verifyRequestSignature({ request, headerName, secret, algorithm }
|
|
|
3908
4620
|
}
|
|
3909
4621
|
}
|
|
3910
4622
|
__name(verifyRequestSignature, "verifyRequestSignature");
|
|
3911
|
-
function verifyHmacSha256(headerValue, secret, body) {
|
|
3912
|
-
const bodyDigest = import_crypto.default.createHmac("sha256", secret).update(body).digest(
|
|
3913
|
-
const signature = headerValue?.replace("sha256=", "") ?? "";
|
|
4623
|
+
function verifyHmacSha256(headerValue, headerEncoding, secret, body) {
|
|
4624
|
+
const bodyDigest = import_crypto.default.createHmac("sha256", secret).update(body).digest(headerEncoding);
|
|
4625
|
+
const signature = headerValue?.replace("hmac-sha256=", "").replace("sha256=", "") ?? "";
|
|
3914
4626
|
return signature === bodyDigest;
|
|
3915
4627
|
}
|
|
3916
4628
|
__name(verifyHmacSha256, "verifyHmacSha256");
|
|
@@ -3942,6 +4654,8 @@ __name(redactString, "redactString");
|
|
|
3942
4654
|
MissingConnectionNotification,
|
|
3943
4655
|
MissingConnectionResolvedNotification,
|
|
3944
4656
|
TriggerClient,
|
|
4657
|
+
WebhookSource,
|
|
4658
|
+
WebhookTrigger,
|
|
3945
4659
|
cronTrigger,
|
|
3946
4660
|
eventTrigger,
|
|
3947
4661
|
intervalTrigger,
|
|
@@ -3952,6 +4666,7 @@ __name(redactString, "redactString");
|
|
|
3952
4666
|
omit,
|
|
3953
4667
|
redactString,
|
|
3954
4668
|
retry,
|
|
4669
|
+
slugifyId,
|
|
3955
4670
|
verifyHmacSha256,
|
|
3956
4671
|
verifyRequestSignature,
|
|
3957
4672
|
waitForEventSchema
|