@trigger.dev/sdk 0.0.0-prerelease-20230922203841 → 0.0.0-prerelease-20231005170730
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 +465 -20
- package/dist/index.js +291 -76
- package/dist/index.js.map +1 -1
- package/package.json +3 -4
package/dist/index.js
CHANGED
|
@@ -5,6 +5,7 @@ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
|
5
5
|
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
6
|
var __getProtoOf = Object.getPrototypeOf;
|
|
7
7
|
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
8
9
|
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
9
10
|
var __export = (target, all) => {
|
|
10
11
|
for (var name in all)
|
|
@@ -23,6 +24,10 @@ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__ge
|
|
|
23
24
|
mod
|
|
24
25
|
));
|
|
25
26
|
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
27
|
+
var __publicField = (obj, key, value) => {
|
|
28
|
+
__defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
|
|
29
|
+
return value;
|
|
30
|
+
};
|
|
26
31
|
var __accessCheck = (obj, member, msg) => {
|
|
27
32
|
if (!member.has(obj))
|
|
28
33
|
throw TypeError("Cannot " + msg);
|
|
@@ -189,17 +194,21 @@ var ApiClient = class {
|
|
|
189
194
|
}
|
|
190
195
|
return await response.json();
|
|
191
196
|
}
|
|
192
|
-
async runTask(runId, task) {
|
|
197
|
+
async runTask(runId, task, options = {}) {
|
|
193
198
|
const apiKey = await __privateMethod(this, _apiKey, apiKey_fn).call(this);
|
|
194
199
|
__privateGet(this, _logger).debug("Running Task", {
|
|
195
200
|
task
|
|
196
201
|
});
|
|
197
|
-
return await
|
|
202
|
+
return await zodfetchWithVersions({
|
|
203
|
+
[import_core.API_VERSIONS.LAZY_LOADED_CACHED_TASKS]: import_core.RunTaskResponseWithCachedTasksBodySchema
|
|
204
|
+
}, import_core.ServerTaskSchema, `${__privateGet(this, _apiUrl)}/api/v1/runs/${runId}/tasks`, {
|
|
198
205
|
method: "POST",
|
|
199
206
|
headers: {
|
|
200
207
|
"Content-Type": "application/json",
|
|
201
208
|
Authorization: `Bearer ${apiKey}`,
|
|
202
|
-
"Idempotency-Key": task.idempotencyKey
|
|
209
|
+
"Idempotency-Key": task.idempotencyKey,
|
|
210
|
+
"X-Cached-Tasks-Cursor": options.cachedTasksCursor ?? "",
|
|
211
|
+
"Trigger-Version": import_core.API_VERSIONS.LAZY_LOADED_CACHED_TASKS
|
|
203
212
|
},
|
|
204
213
|
body: JSON.stringify(task)
|
|
205
214
|
});
|
|
@@ -385,6 +394,19 @@ var ApiClient = class {
|
|
|
385
394
|
}
|
|
386
395
|
});
|
|
387
396
|
}
|
|
397
|
+
async cancelRun(runId) {
|
|
398
|
+
const apiKey = await __privateMethod(this, _apiKey, apiKey_fn).call(this);
|
|
399
|
+
__privateGet(this, _logger).debug("Cancelling Run", {
|
|
400
|
+
runId
|
|
401
|
+
});
|
|
402
|
+
return await zodfetch(import_core.GetRunSchema, `${__privateGet(this, _apiUrl)}/api/v1/runs/${runId}/cancel`, {
|
|
403
|
+
method: "POST",
|
|
404
|
+
headers: {
|
|
405
|
+
"Content-Type": "application/json",
|
|
406
|
+
Authorization: `Bearer ${apiKey}`
|
|
407
|
+
}
|
|
408
|
+
});
|
|
409
|
+
}
|
|
388
410
|
async getRunStatuses(runId) {
|
|
389
411
|
const apiKey = await __privateMethod(this, _apiKey, apiKey_fn).call(this);
|
|
390
412
|
__privateGet(this, _logger).debug("Getting Run statuses", {
|
|
@@ -444,6 +466,36 @@ function getApiKey(key) {
|
|
|
444
466
|
};
|
|
445
467
|
}
|
|
446
468
|
__name(getApiKey, "getApiKey");
|
|
469
|
+
async function zodfetchWithVersions(versionedSchemaMap, unversionedSchema, url, requestInit, options) {
|
|
470
|
+
const response = await (0, import_node_fetch.default)(url, requestInit);
|
|
471
|
+
if ((!requestInit || requestInit.method === "GET") && response.status === 404 && options?.optional) {
|
|
472
|
+
return;
|
|
473
|
+
}
|
|
474
|
+
if (response.status >= 400 && response.status < 500) {
|
|
475
|
+
const body = await response.json();
|
|
476
|
+
throw new Error(body.error);
|
|
477
|
+
}
|
|
478
|
+
if (response.status !== 200) {
|
|
479
|
+
throw new Error(options?.errorMessage ?? `Failed to fetch ${url}, got status code ${response.status}`);
|
|
480
|
+
}
|
|
481
|
+
const jsonBody = await response.json();
|
|
482
|
+
const version = response.headers.get("trigger-version");
|
|
483
|
+
if (!version) {
|
|
484
|
+
return {
|
|
485
|
+
version: "unversioned",
|
|
486
|
+
body: unversionedSchema.parse(jsonBody)
|
|
487
|
+
};
|
|
488
|
+
}
|
|
489
|
+
const versionedSchema = versionedSchemaMap[version];
|
|
490
|
+
if (!versionedSchema) {
|
|
491
|
+
throw new Error(`Unknown version ${version}`);
|
|
492
|
+
}
|
|
493
|
+
return {
|
|
494
|
+
version,
|
|
495
|
+
body: versionedSchema.parse(jsonBody)
|
|
496
|
+
};
|
|
497
|
+
}
|
|
498
|
+
__name(zodfetchWithVersions, "zodfetchWithVersions");
|
|
447
499
|
async function zodfetch(schema, url, requestInit, options) {
|
|
448
500
|
const response = await (0, import_node_fetch.default)(url, requestInit);
|
|
449
501
|
if ((!requestInit || requestInit.method === "GET") && response.status === 404 && options?.optional) {
|
|
@@ -482,8 +534,20 @@ var CanceledWithTaskError = class {
|
|
|
482
534
|
}
|
|
483
535
|
};
|
|
484
536
|
__name(CanceledWithTaskError, "CanceledWithTaskError");
|
|
537
|
+
var YieldExecutionError = class {
|
|
538
|
+
constructor(key) {
|
|
539
|
+
this.key = key;
|
|
540
|
+
}
|
|
541
|
+
};
|
|
542
|
+
__name(YieldExecutionError, "YieldExecutionError");
|
|
543
|
+
var ParsedPayloadSchemaError = class {
|
|
544
|
+
constructor(schemaErrors) {
|
|
545
|
+
this.schemaErrors = schemaErrors;
|
|
546
|
+
}
|
|
547
|
+
};
|
|
548
|
+
__name(ParsedPayloadSchemaError, "ParsedPayloadSchemaError");
|
|
485
549
|
function isTriggerError(err) {
|
|
486
|
-
return err instanceof ResumeWithTaskError || err instanceof RetryWithTaskError || err instanceof CanceledWithTaskError;
|
|
550
|
+
return err instanceof ResumeWithTaskError || err instanceof RetryWithTaskError || err instanceof CanceledWithTaskError || err instanceof YieldExecutionError;
|
|
487
551
|
}
|
|
488
552
|
__name(isTriggerError, "isTriggerError");
|
|
489
553
|
|
|
@@ -543,6 +607,7 @@ var _addToCachedTasks, addToCachedTasks_fn;
|
|
|
543
607
|
var IO = class {
|
|
544
608
|
constructor(options) {
|
|
545
609
|
__privateAdd(this, _addToCachedTasks);
|
|
610
|
+
__publicField(this, "brb", this.yield.bind(this));
|
|
546
611
|
this._id = options.id;
|
|
547
612
|
this._apiClient = options.apiClient;
|
|
548
613
|
this._triggerClient = options.client;
|
|
@@ -550,13 +615,32 @@ var IO = class {
|
|
|
550
615
|
this._cachedTasks = /* @__PURE__ */ new Map();
|
|
551
616
|
this._jobLogger = options.jobLogger;
|
|
552
617
|
this._jobLogLevel = options.jobLogLevel;
|
|
618
|
+
this._stats = {
|
|
619
|
+
initialCachedTasks: 0,
|
|
620
|
+
lazyLoadedCachedTasks: 0,
|
|
621
|
+
executedTasks: 0,
|
|
622
|
+
cachedTaskHits: 0,
|
|
623
|
+
cachedTaskMisses: 0,
|
|
624
|
+
noopCachedTaskHits: 0,
|
|
625
|
+
noopCachedTaskMisses: 0
|
|
626
|
+
};
|
|
553
627
|
if (options.cachedTasks) {
|
|
554
628
|
options.cachedTasks.forEach((task) => {
|
|
555
629
|
this._cachedTasks.set(task.idempotencyKey, task);
|
|
556
630
|
});
|
|
631
|
+
this._stats.initialCachedTasks = options.cachedTasks.length;
|
|
557
632
|
}
|
|
558
633
|
this._taskStorage = new import_node_async_hooks.AsyncLocalStorage();
|
|
559
634
|
this._context = options.context;
|
|
635
|
+
this._yieldedExecutions = options.yieldedExecutions ?? [];
|
|
636
|
+
if (options.noopTasksSet) {
|
|
637
|
+
this._noopTasksBloomFilter = import_core3.BloomFilter.deserialize(options.noopTasksSet, import_core3.BloomFilter.NOOP_TASK_SET_SIZE);
|
|
638
|
+
}
|
|
639
|
+
this._cachedTasksCursor = options.cachedTasksCursor;
|
|
640
|
+
this._serverVersion = options.serverVersion ?? "unversioned";
|
|
641
|
+
}
|
|
642
|
+
get stats() {
|
|
643
|
+
return this._stats;
|
|
560
644
|
}
|
|
561
645
|
get runId() {
|
|
562
646
|
return this._id;
|
|
@@ -567,38 +651,38 @@ var IO = class {
|
|
|
567
651
|
get logger() {
|
|
568
652
|
return new IOLogger(async (level, message, data) => {
|
|
569
653
|
let logLevel = "info";
|
|
570
|
-
switch (level) {
|
|
571
|
-
case "LOG": {
|
|
572
|
-
this._jobLogger?.log(message, data);
|
|
573
|
-
logLevel = "log";
|
|
574
|
-
break;
|
|
575
|
-
}
|
|
576
|
-
case "DEBUG": {
|
|
577
|
-
this._jobLogger?.debug(message, data);
|
|
578
|
-
logLevel = "debug";
|
|
579
|
-
break;
|
|
580
|
-
}
|
|
581
|
-
case "INFO": {
|
|
582
|
-
this._jobLogger?.info(message, data);
|
|
583
|
-
logLevel = "info";
|
|
584
|
-
break;
|
|
585
|
-
}
|
|
586
|
-
case "WARN": {
|
|
587
|
-
this._jobLogger?.warn(message, data);
|
|
588
|
-
logLevel = "warn";
|
|
589
|
-
break;
|
|
590
|
-
}
|
|
591
|
-
case "ERROR": {
|
|
592
|
-
this._jobLogger?.error(message, data);
|
|
593
|
-
logLevel = "error";
|
|
594
|
-
break;
|
|
595
|
-
}
|
|
596
|
-
}
|
|
597
654
|
if (import_core3.Logger.satisfiesLogLevel(logLevel, this._jobLogLevel)) {
|
|
598
655
|
await this.runTask([
|
|
599
656
|
message,
|
|
600
657
|
level
|
|
601
658
|
], async (task) => {
|
|
659
|
+
switch (level) {
|
|
660
|
+
case "LOG": {
|
|
661
|
+
this._jobLogger?.log(message, data);
|
|
662
|
+
logLevel = "log";
|
|
663
|
+
break;
|
|
664
|
+
}
|
|
665
|
+
case "DEBUG": {
|
|
666
|
+
this._jobLogger?.debug(message, data);
|
|
667
|
+
logLevel = "debug";
|
|
668
|
+
break;
|
|
669
|
+
}
|
|
670
|
+
case "INFO": {
|
|
671
|
+
this._jobLogger?.info(message, data);
|
|
672
|
+
logLevel = "info";
|
|
673
|
+
break;
|
|
674
|
+
}
|
|
675
|
+
case "WARN": {
|
|
676
|
+
this._jobLogger?.warn(message, data);
|
|
677
|
+
logLevel = "warn";
|
|
678
|
+
break;
|
|
679
|
+
}
|
|
680
|
+
case "ERROR": {
|
|
681
|
+
this._jobLogger?.error(message, data);
|
|
682
|
+
logLevel = "error";
|
|
683
|
+
break;
|
|
684
|
+
}
|
|
685
|
+
}
|
|
602
686
|
}, {
|
|
603
687
|
name: "log",
|
|
604
688
|
icon: "log",
|
|
@@ -883,18 +967,42 @@ var IO = class {
|
|
|
883
967
|
const cachedTask = this._cachedTasks.get(idempotencyKey);
|
|
884
968
|
if (cachedTask && cachedTask.status === "COMPLETED") {
|
|
885
969
|
this._logger.debug("Using completed cached task", {
|
|
886
|
-
idempotencyKey
|
|
887
|
-
cachedTask
|
|
970
|
+
idempotencyKey
|
|
888
971
|
});
|
|
972
|
+
this._stats.cachedTaskHits++;
|
|
889
973
|
return cachedTask.output;
|
|
890
974
|
}
|
|
891
|
-
|
|
975
|
+
if (options?.noop && this._noopTasksBloomFilter) {
|
|
976
|
+
if (this._noopTasksBloomFilter.test(idempotencyKey)) {
|
|
977
|
+
this._logger.debug("task idempotency key exists in noopTasksBloomFilter", {
|
|
978
|
+
idempotencyKey
|
|
979
|
+
});
|
|
980
|
+
this._stats.noopCachedTaskHits++;
|
|
981
|
+
return {};
|
|
982
|
+
}
|
|
983
|
+
}
|
|
984
|
+
const response = await this._apiClient.runTask(this._id, {
|
|
892
985
|
idempotencyKey,
|
|
893
|
-
displayKey: typeof key === "string" ? key :
|
|
986
|
+
displayKey: typeof key === "string" ? key : void 0,
|
|
894
987
|
noop: false,
|
|
895
988
|
...options ?? {},
|
|
896
989
|
parentId
|
|
990
|
+
}, {
|
|
991
|
+
cachedTasksCursor: this._cachedTasksCursor
|
|
897
992
|
});
|
|
993
|
+
const task = response.version === import_core3.API_VERSIONS.LAZY_LOADED_CACHED_TASKS ? response.body.task : response.body;
|
|
994
|
+
if (response.version === import_core3.API_VERSIONS.LAZY_LOADED_CACHED_TASKS) {
|
|
995
|
+
this._cachedTasksCursor = response.body.cachedTasks?.cursor;
|
|
996
|
+
for (const cachedTask2 of response.body.cachedTasks?.tasks ?? []) {
|
|
997
|
+
if (!this._cachedTasks.has(cachedTask2.idempotencyKey)) {
|
|
998
|
+
this._cachedTasks.set(cachedTask2.idempotencyKey, cachedTask2);
|
|
999
|
+
this._logger.debug("Injecting lazy loaded task into task cache", {
|
|
1000
|
+
idempotencyKey: cachedTask2.idempotencyKey
|
|
1001
|
+
});
|
|
1002
|
+
this._stats.lazyLoadedCachedTasks++;
|
|
1003
|
+
}
|
|
1004
|
+
}
|
|
1005
|
+
}
|
|
898
1006
|
if (task.status === "CANCELED") {
|
|
899
1007
|
this._logger.debug("Task canceled", {
|
|
900
1008
|
idempotencyKey,
|
|
@@ -903,11 +1011,18 @@ var IO = class {
|
|
|
903
1011
|
throw new CanceledWithTaskError(task);
|
|
904
1012
|
}
|
|
905
1013
|
if (task.status === "COMPLETED") {
|
|
906
|
-
|
|
907
|
-
|
|
908
|
-
|
|
909
|
-
|
|
910
|
-
|
|
1014
|
+
if (task.noop) {
|
|
1015
|
+
this._logger.debug("Noop Task completed", {
|
|
1016
|
+
idempotencyKey
|
|
1017
|
+
});
|
|
1018
|
+
this._noopTasksBloomFilter?.add(task.idempotencyKey);
|
|
1019
|
+
} else {
|
|
1020
|
+
this._logger.debug("Cache miss", {
|
|
1021
|
+
idempotencyKey
|
|
1022
|
+
});
|
|
1023
|
+
this._stats.cachedTaskMisses++;
|
|
1024
|
+
__privateMethod(this, _addToCachedTasks, addToCachedTasks_fn).call(this, task);
|
|
1025
|
+
}
|
|
911
1026
|
return task.output;
|
|
912
1027
|
}
|
|
913
1028
|
if (task.status === "ERRORED") {
|
|
@@ -917,23 +1032,16 @@ var IO = class {
|
|
|
917
1032
|
});
|
|
918
1033
|
throw new Error(task.error ?? task?.output ? JSON.stringify(task.output) : "Task errored");
|
|
919
1034
|
}
|
|
920
|
-
if (task.status === "WAITING") {
|
|
921
|
-
this._logger.debug("Task waiting", {
|
|
922
|
-
idempotencyKey,
|
|
923
|
-
task
|
|
924
|
-
});
|
|
925
|
-
throw new ResumeWithTaskError(task);
|
|
926
|
-
}
|
|
927
|
-
if (task.status === "RUNNING" && typeof task.operation === "string") {
|
|
928
|
-
this._logger.debug("Task running operation", {
|
|
929
|
-
idempotencyKey,
|
|
930
|
-
task
|
|
931
|
-
});
|
|
932
|
-
throw new ResumeWithTaskError(task);
|
|
933
|
-
}
|
|
934
1035
|
const executeTask = /* @__PURE__ */ __name(async () => {
|
|
935
1036
|
try {
|
|
936
1037
|
const result = await callback(task, this);
|
|
1038
|
+
if (task.status === "WAITING" && task.callbackUrl) {
|
|
1039
|
+
this._logger.debug("Waiting for remote callback", {
|
|
1040
|
+
idempotencyKey,
|
|
1041
|
+
task
|
|
1042
|
+
});
|
|
1043
|
+
return {};
|
|
1044
|
+
}
|
|
937
1045
|
const output = import_core3.SerializableJsonSchema.parse(result);
|
|
938
1046
|
this._logger.debug("Completing using output", {
|
|
939
1047
|
idempotencyKey,
|
|
@@ -943,6 +1051,7 @@ var IO = class {
|
|
|
943
1051
|
output: output ?? void 0,
|
|
944
1052
|
properties: task.outputProperties ?? void 0
|
|
945
1053
|
});
|
|
1054
|
+
this._stats.executedTasks++;
|
|
946
1055
|
if (completedTask.status === "CANCELED") {
|
|
947
1056
|
throw new CanceledWithTaskError(completedTask);
|
|
948
1057
|
}
|
|
@@ -999,10 +1108,39 @@ var IO = class {
|
|
|
999
1108
|
throw error;
|
|
1000
1109
|
}
|
|
1001
1110
|
}, "executeTask");
|
|
1111
|
+
if (task.status === "WAITING") {
|
|
1112
|
+
this._logger.debug("Task waiting", {
|
|
1113
|
+
idempotencyKey,
|
|
1114
|
+
task
|
|
1115
|
+
});
|
|
1116
|
+
if (task.callbackUrl) {
|
|
1117
|
+
await this._taskStorage.run({
|
|
1118
|
+
taskId: task.id
|
|
1119
|
+
}, executeTask);
|
|
1120
|
+
}
|
|
1121
|
+
throw new ResumeWithTaskError(task);
|
|
1122
|
+
}
|
|
1123
|
+
if (task.status === "RUNNING" && typeof task.operation === "string") {
|
|
1124
|
+
this._logger.debug("Task running operation", {
|
|
1125
|
+
idempotencyKey,
|
|
1126
|
+
task
|
|
1127
|
+
});
|
|
1128
|
+
throw new ResumeWithTaskError(task);
|
|
1129
|
+
}
|
|
1002
1130
|
return this._taskStorage.run({
|
|
1003
1131
|
taskId: task.id
|
|
1004
1132
|
}, executeTask);
|
|
1005
1133
|
}
|
|
1134
|
+
yield(key) {
|
|
1135
|
+
if (!(0, import_core3.supportsFeature)("yieldExecution", this._serverVersion)) {
|
|
1136
|
+
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.");
|
|
1137
|
+
return;
|
|
1138
|
+
}
|
|
1139
|
+
if (this._yieldedExecutions.includes(key)) {
|
|
1140
|
+
return;
|
|
1141
|
+
}
|
|
1142
|
+
throw new YieldExecutionError(key);
|
|
1143
|
+
}
|
|
1006
1144
|
async try(tryCallback, catchCallback) {
|
|
1007
1145
|
try {
|
|
1008
1146
|
return await tryCallback();
|
|
@@ -1210,6 +1348,20 @@ _options2 = new WeakMap();
|
|
|
1210
1348
|
|
|
1211
1349
|
// src/triggers/eventTrigger.ts
|
|
1212
1350
|
var import_core5 = require("@trigger.dev/core");
|
|
1351
|
+
|
|
1352
|
+
// src/utils/formatSchemaErrors.ts
|
|
1353
|
+
function formatSchemaErrors(errors) {
|
|
1354
|
+
return errors.map((error) => {
|
|
1355
|
+
const { path, message } = error;
|
|
1356
|
+
return {
|
|
1357
|
+
path: path.map(String),
|
|
1358
|
+
message
|
|
1359
|
+
};
|
|
1360
|
+
});
|
|
1361
|
+
}
|
|
1362
|
+
__name(formatSchemaErrors, "formatSchemaErrors");
|
|
1363
|
+
|
|
1364
|
+
// src/triggers/eventTrigger.ts
|
|
1213
1365
|
var _options3;
|
|
1214
1366
|
var EventTrigger = class {
|
|
1215
1367
|
constructor(options) {
|
|
@@ -1250,7 +1402,11 @@ function eventTrigger(options) {
|
|
|
1250
1402
|
examples: options.examples,
|
|
1251
1403
|
parsePayload: (rawPayload) => {
|
|
1252
1404
|
if (options.schema) {
|
|
1253
|
-
|
|
1405
|
+
const results = options.schema.safeParse(rawPayload);
|
|
1406
|
+
if (!results.success) {
|
|
1407
|
+
throw new ParsedPayloadSchemaError(formatSchemaErrors(results.error.issues));
|
|
1408
|
+
}
|
|
1409
|
+
return results.data;
|
|
1254
1410
|
}
|
|
1255
1411
|
return rawPayload;
|
|
1256
1412
|
}
|
|
@@ -1459,7 +1615,7 @@ var registerSourceEvent = {
|
|
|
1459
1615
|
icon: "register-source",
|
|
1460
1616
|
parsePayload: import_core7.RegisterSourceEventSchemaV2.parse
|
|
1461
1617
|
};
|
|
1462
|
-
var _options4, _registeredJobs, _registeredSources, _registeredHttpSourceHandlers, _registeredDynamicTriggers, _jobMetadataByDynamicTriggers, _registeredSchedules, _authResolvers, _client2, _internalLogger, _preprocessRun, preprocessRun_fn, _executeJob, executeJob_fn, _createRunContext, createRunContext_fn, _createPreprocessRunContext, createPreprocessRunContext_fn, _handleHttpSourceRequest, handleHttpSourceRequest_fn, _resolveConnections, resolveConnections_fn, _resolveConnection, resolveConnection_fn, _buildJobsIndex, buildJobsIndex_fn, _buildJobIndex, buildJobIndex_fn, _buildJobIntegrations, buildJobIntegrations_fn, _buildJobIntegration, buildJobIntegration_fn;
|
|
1618
|
+
var _options4, _registeredJobs, _registeredSources, _registeredHttpSourceHandlers, _registeredDynamicTriggers, _jobMetadataByDynamicTriggers, _registeredSchedules, _authResolvers, _client2, _internalLogger, _preprocessRun, preprocessRun_fn, _executeJob, executeJob_fn, _createRunContext, createRunContext_fn, _createPreprocessRunContext, createPreprocessRunContext_fn, _handleHttpSourceRequest, handleHttpSourceRequest_fn, _resolveConnections, resolveConnections_fn, _resolveConnection, resolveConnection_fn, _buildJobsIndex, buildJobsIndex_fn, _buildJobIndex, buildJobIndex_fn, _buildJobIntegrations, buildJobIntegrations_fn, _buildJobIntegration, buildJobIntegration_fn, _logIOStats, logIOStats_fn, _standardResponseHeaders, standardResponseHeaders_get;
|
|
1463
1619
|
var TriggerClient = class {
|
|
1464
1620
|
constructor(options) {
|
|
1465
1621
|
__privateAdd(this, _preprocessRun);
|
|
@@ -1473,6 +1629,8 @@ var TriggerClient = class {
|
|
|
1473
1629
|
__privateAdd(this, _buildJobIndex);
|
|
1474
1630
|
__privateAdd(this, _buildJobIntegrations);
|
|
1475
1631
|
__privateAdd(this, _buildJobIntegration);
|
|
1632
|
+
__privateAdd(this, _logIOStats);
|
|
1633
|
+
__privateAdd(this, _standardResponseHeaders);
|
|
1476
1634
|
__privateAdd(this, _options4, void 0);
|
|
1477
1635
|
__privateAdd(this, _registeredJobs, {});
|
|
1478
1636
|
__privateAdd(this, _registeredSources, {});
|
|
@@ -1486,7 +1644,10 @@ var TriggerClient = class {
|
|
|
1486
1644
|
this.id = options.id;
|
|
1487
1645
|
__privateSet(this, _options4, options);
|
|
1488
1646
|
__privateSet(this, _client2, new ApiClient(__privateGet(this, _options4)));
|
|
1489
|
-
__privateSet(this, _internalLogger, new import_core7.Logger("trigger.dev", __privateGet(this, _options4).verbose ? "debug" : "log"
|
|
1647
|
+
__privateSet(this, _internalLogger, new import_core7.Logger("trigger.dev", __privateGet(this, _options4).verbose ? "debug" : "log", [
|
|
1648
|
+
"output",
|
|
1649
|
+
"noopTasksSet"
|
|
1650
|
+
]));
|
|
1490
1651
|
}
|
|
1491
1652
|
async handleRequest(request) {
|
|
1492
1653
|
__privateGet(this, _internalLogger).debug("handling request", {
|
|
@@ -1495,6 +1656,7 @@ var TriggerClient = class {
|
|
|
1495
1656
|
method: request.method
|
|
1496
1657
|
});
|
|
1497
1658
|
const apiKey = request.headers.get("x-trigger-api-key");
|
|
1659
|
+
const triggerVersion = request.headers.get("x-trigger-version");
|
|
1498
1660
|
const authorization = this.authorized(apiKey);
|
|
1499
1661
|
switch (authorization) {
|
|
1500
1662
|
case "authorized": {
|
|
@@ -1505,7 +1667,8 @@ var TriggerClient = class {
|
|
|
1505
1667
|
status: 401,
|
|
1506
1668
|
body: {
|
|
1507
1669
|
message: "Unauthorized: client missing apiKey"
|
|
1508
|
-
}
|
|
1670
|
+
},
|
|
1671
|
+
headers: __privateGet(this, _standardResponseHeaders, standardResponseHeaders_get)
|
|
1509
1672
|
};
|
|
1510
1673
|
}
|
|
1511
1674
|
case "missing-header": {
|
|
@@ -1513,7 +1676,8 @@ var TriggerClient = class {
|
|
|
1513
1676
|
status: 401,
|
|
1514
1677
|
body: {
|
|
1515
1678
|
message: "Unauthorized: missing x-trigger-api-key header"
|
|
1516
|
-
}
|
|
1679
|
+
},
|
|
1680
|
+
headers: __privateGet(this, _standardResponseHeaders, standardResponseHeaders_get)
|
|
1517
1681
|
};
|
|
1518
1682
|
}
|
|
1519
1683
|
case "unauthorized": {
|
|
@@ -1521,7 +1685,8 @@ var TriggerClient = class {
|
|
|
1521
1685
|
status: 401,
|
|
1522
1686
|
body: {
|
|
1523
1687
|
message: `Forbidden: client apiKey mismatch: Make sure you are using the correct API Key for your environment`
|
|
1524
|
-
}
|
|
1688
|
+
},
|
|
1689
|
+
headers: __privateGet(this, _standardResponseHeaders, standardResponseHeaders_get)
|
|
1525
1690
|
};
|
|
1526
1691
|
}
|
|
1527
1692
|
}
|
|
@@ -1530,7 +1695,8 @@ var TriggerClient = class {
|
|
|
1530
1695
|
status: 405,
|
|
1531
1696
|
body: {
|
|
1532
1697
|
message: "Method not allowed (only POST is allowed)"
|
|
1533
|
-
}
|
|
1698
|
+
},
|
|
1699
|
+
headers: __privateGet(this, _standardResponseHeaders, standardResponseHeaders_get)
|
|
1534
1700
|
};
|
|
1535
1701
|
}
|
|
1536
1702
|
const action = request.headers.get("x-trigger-action");
|
|
@@ -1539,7 +1705,8 @@ var TriggerClient = class {
|
|
|
1539
1705
|
status: 400,
|
|
1540
1706
|
body: {
|
|
1541
1707
|
message: "Missing x-trigger-action header"
|
|
1542
|
-
}
|
|
1708
|
+
},
|
|
1709
|
+
headers: __privateGet(this, _standardResponseHeaders, standardResponseHeaders_get)
|
|
1543
1710
|
};
|
|
1544
1711
|
}
|
|
1545
1712
|
switch (action) {
|
|
@@ -1551,7 +1718,8 @@ var TriggerClient = class {
|
|
|
1551
1718
|
body: {
|
|
1552
1719
|
ok: false,
|
|
1553
1720
|
error: "Missing endpoint ID"
|
|
1554
|
-
}
|
|
1721
|
+
},
|
|
1722
|
+
headers: __privateGet(this, _standardResponseHeaders, standardResponseHeaders_get)
|
|
1555
1723
|
};
|
|
1556
1724
|
}
|
|
1557
1725
|
if (this.id !== endpointId) {
|
|
@@ -1560,14 +1728,16 @@ var TriggerClient = class {
|
|
|
1560
1728
|
body: {
|
|
1561
1729
|
ok: false,
|
|
1562
1730
|
error: `Endpoint ID mismatch error. Expected ${this.id}, got ${endpointId}`
|
|
1563
|
-
}
|
|
1731
|
+
},
|
|
1732
|
+
headers: __privateGet(this, _standardResponseHeaders, standardResponseHeaders_get)
|
|
1564
1733
|
};
|
|
1565
1734
|
}
|
|
1566
1735
|
return {
|
|
1567
1736
|
status: 200,
|
|
1568
1737
|
body: {
|
|
1569
1738
|
ok: true
|
|
1570
|
-
}
|
|
1739
|
+
},
|
|
1740
|
+
headers: __privateGet(this, _standardResponseHeaders, standardResponseHeaders_get)
|
|
1571
1741
|
};
|
|
1572
1742
|
}
|
|
1573
1743
|
case "INDEX_ENDPOINT": {
|
|
@@ -1589,7 +1759,8 @@ var TriggerClient = class {
|
|
|
1589
1759
|
};
|
|
1590
1760
|
return {
|
|
1591
1761
|
status: 200,
|
|
1592
|
-
body
|
|
1762
|
+
body,
|
|
1763
|
+
headers: __privateGet(this, _standardResponseHeaders, standardResponseHeaders_get)
|
|
1593
1764
|
};
|
|
1594
1765
|
}
|
|
1595
1766
|
case "INITIALIZE_TRIGGER": {
|
|
@@ -1614,7 +1785,8 @@ var TriggerClient = class {
|
|
|
1614
1785
|
}
|
|
1615
1786
|
return {
|
|
1616
1787
|
status: 200,
|
|
1617
|
-
body: dynamicTrigger.registeredTriggerForParams(body.data.params)
|
|
1788
|
+
body: dynamicTrigger.registeredTriggerForParams(body.data.params),
|
|
1789
|
+
headers: __privateGet(this, _standardResponseHeaders, standardResponseHeaders_get)
|
|
1618
1790
|
};
|
|
1619
1791
|
}
|
|
1620
1792
|
case "EXECUTE_JOB": {
|
|
@@ -1637,10 +1809,11 @@ var TriggerClient = class {
|
|
|
1637
1809
|
}
|
|
1638
1810
|
};
|
|
1639
1811
|
}
|
|
1640
|
-
const results = await __privateMethod(this, _executeJob, executeJob_fn).call(this, execution.data, job);
|
|
1812
|
+
const results = await __privateMethod(this, _executeJob, executeJob_fn).call(this, execution.data, job, triggerVersion);
|
|
1641
1813
|
return {
|
|
1642
1814
|
status: 200,
|
|
1643
|
-
body: results
|
|
1815
|
+
body: results,
|
|
1816
|
+
headers: __privateGet(this, _standardResponseHeaders, standardResponseHeaders_get)
|
|
1644
1817
|
};
|
|
1645
1818
|
}
|
|
1646
1819
|
case "PREPROCESS_RUN": {
|
|
@@ -1669,7 +1842,8 @@ var TriggerClient = class {
|
|
|
1669
1842
|
body: {
|
|
1670
1843
|
abort: results.abort,
|
|
1671
1844
|
properties: results.properties
|
|
1672
|
-
}
|
|
1845
|
+
},
|
|
1846
|
+
headers: __privateGet(this, _standardResponseHeaders, standardResponseHeaders_get)
|
|
1673
1847
|
};
|
|
1674
1848
|
}
|
|
1675
1849
|
case "DELIVER_HTTP_SOURCE_REQUEST": {
|
|
@@ -1718,7 +1892,8 @@ var TriggerClient = class {
|
|
|
1718
1892
|
events,
|
|
1719
1893
|
response,
|
|
1720
1894
|
metadata
|
|
1721
|
-
}
|
|
1895
|
+
},
|
|
1896
|
+
headers: __privateGet(this, _standardResponseHeaders, standardResponseHeaders_get)
|
|
1722
1897
|
};
|
|
1723
1898
|
}
|
|
1724
1899
|
case "VALIDATE": {
|
|
@@ -1727,7 +1902,8 @@ var TriggerClient = class {
|
|
|
1727
1902
|
body: {
|
|
1728
1903
|
ok: true,
|
|
1729
1904
|
endpointId: this.id
|
|
1730
|
-
}
|
|
1905
|
+
},
|
|
1906
|
+
headers: __privateGet(this, _standardResponseHeaders, standardResponseHeaders_get)
|
|
1731
1907
|
};
|
|
1732
1908
|
}
|
|
1733
1909
|
}
|
|
@@ -1735,7 +1911,8 @@ var TriggerClient = class {
|
|
|
1735
1911
|
status: 405,
|
|
1736
1912
|
body: {
|
|
1737
1913
|
message: "Method not allowed"
|
|
1738
|
-
}
|
|
1914
|
+
},
|
|
1915
|
+
headers: __privateGet(this, _standardResponseHeaders, standardResponseHeaders_get)
|
|
1739
1916
|
};
|
|
1740
1917
|
}
|
|
1741
1918
|
defineJob(options) {
|
|
@@ -1892,6 +2069,9 @@ var TriggerClient = class {
|
|
|
1892
2069
|
async getRun(runId, options) {
|
|
1893
2070
|
return __privateGet(this, _client2).getRun(runId, options);
|
|
1894
2071
|
}
|
|
2072
|
+
async cancelRun(runId) {
|
|
2073
|
+
return __privateGet(this, _client2).cancelRun(runId);
|
|
2074
|
+
}
|
|
1895
2075
|
async getRuns(jobSlug, options) {
|
|
1896
2076
|
return __privateGet(this, _client2).getRuns(jobSlug, options);
|
|
1897
2077
|
}
|
|
@@ -1934,22 +2114,27 @@ preprocessRun_fn = /* @__PURE__ */ __name(async function(body, job) {
|
|
|
1934
2114
|
};
|
|
1935
2115
|
}, "#preprocessRun");
|
|
1936
2116
|
_executeJob = new WeakSet();
|
|
1937
|
-
executeJob_fn = /* @__PURE__ */ __name(async function(body1, job1) {
|
|
2117
|
+
executeJob_fn = /* @__PURE__ */ __name(async function(body1, job1, triggerVersion) {
|
|
1938
2118
|
__privateGet(this, _internalLogger).debug("executing job", {
|
|
1939
2119
|
execution: body1,
|
|
1940
2120
|
job: job1.id,
|
|
1941
|
-
version: job1.version
|
|
2121
|
+
version: job1.version,
|
|
2122
|
+
triggerVersion
|
|
1942
2123
|
});
|
|
1943
2124
|
const context = __privateMethod(this, _createRunContext, createRunContext_fn).call(this, body1);
|
|
1944
2125
|
const io = new IO({
|
|
1945
2126
|
id: body1.run.id,
|
|
1946
2127
|
cachedTasks: body1.tasks,
|
|
2128
|
+
cachedTasksCursor: body1.cachedTaskCursor,
|
|
2129
|
+
yieldedExecutions: body1.yieldedExecutions ?? [],
|
|
2130
|
+
noopTasksSet: body1.noopTasksSet,
|
|
1947
2131
|
apiClient: __privateGet(this, _client2),
|
|
1948
2132
|
logger: __privateGet(this, _internalLogger),
|
|
1949
2133
|
client: this,
|
|
1950
2134
|
context,
|
|
1951
2135
|
jobLogLevel: job1.logLevel ?? __privateGet(this, _options4).logLevel ?? "info",
|
|
1952
|
-
jobLogger: __privateGet(this, _options4).ioLogLocalEnabled ? new import_core7.Logger(job1.id, job1.logLevel ?? __privateGet(this, _options4).logLevel ?? "info") : void 0
|
|
2136
|
+
jobLogger: __privateGet(this, _options4).ioLogLocalEnabled ? new import_core7.Logger(job1.id, job1.logLevel ?? __privateGet(this, _options4).logLevel ?? "info") : void 0,
|
|
2137
|
+
serverVersion: triggerVersion
|
|
1953
2138
|
});
|
|
1954
2139
|
const resolvedConnections = await __privateMethod(this, _resolveConnections, resolveConnections_fn).call(this, context, job1.options.integrations, body1.connections);
|
|
1955
2140
|
if (!resolvedConnections.ok) {
|
|
@@ -1966,11 +2151,29 @@ executeJob_fn = /* @__PURE__ */ __name(async function(body1, job1) {
|
|
|
1966
2151
|
}, () => {
|
|
1967
2152
|
return job1.options.run(job1.trigger.event.parsePayload(body1.event.payload ?? {}), ioWithConnections, context);
|
|
1968
2153
|
});
|
|
2154
|
+
if (__privateGet(this, _options4).verbose) {
|
|
2155
|
+
__privateMethod(this, _logIOStats, logIOStats_fn).call(this, io.stats);
|
|
2156
|
+
}
|
|
1969
2157
|
return {
|
|
1970
2158
|
status: "SUCCESS",
|
|
1971
2159
|
output
|
|
1972
2160
|
};
|
|
1973
2161
|
} catch (error) {
|
|
2162
|
+
if (__privateGet(this, _options4).verbose) {
|
|
2163
|
+
__privateMethod(this, _logIOStats, logIOStats_fn).call(this, io.stats);
|
|
2164
|
+
}
|
|
2165
|
+
if (error instanceof YieldExecutionError) {
|
|
2166
|
+
return {
|
|
2167
|
+
status: "YIELD_EXECUTION",
|
|
2168
|
+
key: error.key
|
|
2169
|
+
};
|
|
2170
|
+
}
|
|
2171
|
+
if (error instanceof ParsedPayloadSchemaError) {
|
|
2172
|
+
return {
|
|
2173
|
+
status: "INVALID_PAYLOAD",
|
|
2174
|
+
errors: error.schemaErrors
|
|
2175
|
+
};
|
|
2176
|
+
}
|
|
1974
2177
|
if (error instanceof ResumeWithTaskError) {
|
|
1975
2178
|
return {
|
|
1976
2179
|
status: "RESUME_WITH_TASK",
|
|
@@ -2288,6 +2491,18 @@ buildJobIntegration_fn = /* @__PURE__ */ __name(function(integration1) {
|
|
|
2288
2491
|
authSource
|
|
2289
2492
|
};
|
|
2290
2493
|
}, "#buildJobIntegration");
|
|
2494
|
+
_logIOStats = new WeakSet();
|
|
2495
|
+
logIOStats_fn = /* @__PURE__ */ __name(function(stats) {
|
|
2496
|
+
__privateGet(this, _internalLogger).debug("IO stats", {
|
|
2497
|
+
stats
|
|
2498
|
+
});
|
|
2499
|
+
}, "#logIOStats");
|
|
2500
|
+
_standardResponseHeaders = new WeakSet();
|
|
2501
|
+
standardResponseHeaders_get = /* @__PURE__ */ __name(function() {
|
|
2502
|
+
return {
|
|
2503
|
+
"Trigger-Version": import_core7.API_VERSIONS.LAZY_LOADED_CACHED_TASKS
|
|
2504
|
+
};
|
|
2505
|
+
}, "#standardResponseHeaders");
|
|
2291
2506
|
function dynamicTriggerRegisterSourceJobId(id) {
|
|
2292
2507
|
return `register-dynamic-trigger-${id}`;
|
|
2293
2508
|
}
|