@trigger.dev/sdk 0.1.1
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/LICENSE +201 -0
- package/README.md +3 -0
- package/dist/index.d.ts +73 -0
- package/dist/index.js +1233 -0
- package/dist/index.js.map +1 -0
- package/package.json +46 -0
package/dist/index.js
ADDED
|
@@ -0,0 +1,1233 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
7
|
+
var __export = (target, all) => {
|
|
8
|
+
for (var name2 in all)
|
|
9
|
+
__defProp(target, name2, { get: all[name2], enumerable: true });
|
|
10
|
+
};
|
|
11
|
+
var __copyProps = (to, from, except, desc) => {
|
|
12
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
13
|
+
for (let key of __getOwnPropNames(from))
|
|
14
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
15
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
16
|
+
}
|
|
17
|
+
return to;
|
|
18
|
+
};
|
|
19
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
20
|
+
var __accessCheck = (obj, member, msg) => {
|
|
21
|
+
if (!member.has(obj))
|
|
22
|
+
throw TypeError("Cannot " + msg);
|
|
23
|
+
};
|
|
24
|
+
var __privateGet = (obj, member, getter) => {
|
|
25
|
+
__accessCheck(obj, member, "read from private field");
|
|
26
|
+
return getter ? getter.call(obj) : member.get(obj);
|
|
27
|
+
};
|
|
28
|
+
var __privateAdd = (obj, member, value) => {
|
|
29
|
+
if (member.has(obj))
|
|
30
|
+
throw TypeError("Cannot add the same private member more than once");
|
|
31
|
+
member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
|
|
32
|
+
};
|
|
33
|
+
var __privateSet = (obj, member, value, setter) => {
|
|
34
|
+
__accessCheck(obj, member, "write to private field");
|
|
35
|
+
setter ? setter.call(obj, value) : member.set(obj, value);
|
|
36
|
+
return value;
|
|
37
|
+
};
|
|
38
|
+
var __privateMethod = (obj, member, method) => {
|
|
39
|
+
__accessCheck(obj, member, "access private method");
|
|
40
|
+
return method;
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
// src/index.ts
|
|
44
|
+
var src_exports = {};
|
|
45
|
+
__export(src_exports, {
|
|
46
|
+
Trigger: () => Trigger,
|
|
47
|
+
customEvent: () => customEvent,
|
|
48
|
+
getTriggerRun: () => getTriggerRun
|
|
49
|
+
});
|
|
50
|
+
module.exports = __toCommonJS(src_exports);
|
|
51
|
+
|
|
52
|
+
// ../internal-bridge/src/schemas/host.ts
|
|
53
|
+
var import_zod7 = require("zod");
|
|
54
|
+
|
|
55
|
+
// ../common-schemas/src/json.ts
|
|
56
|
+
var import_zod = require("zod");
|
|
57
|
+
var LiteralSchema = import_zod.z.union([
|
|
58
|
+
import_zod.z.string(),
|
|
59
|
+
import_zod.z.number(),
|
|
60
|
+
import_zod.z.boolean(),
|
|
61
|
+
import_zod.z.null()
|
|
62
|
+
]);
|
|
63
|
+
var JsonSchema = import_zod.z.lazy(() => import_zod.z.union([
|
|
64
|
+
LiteralSchema,
|
|
65
|
+
import_zod.z.array(JsonSchema),
|
|
66
|
+
import_zod.z.record(JsonSchema)
|
|
67
|
+
]));
|
|
68
|
+
var SerializableSchema = import_zod.z.union([
|
|
69
|
+
import_zod.z.string(),
|
|
70
|
+
import_zod.z.number(),
|
|
71
|
+
import_zod.z.boolean(),
|
|
72
|
+
import_zod.z.null(),
|
|
73
|
+
import_zod.z.date()
|
|
74
|
+
]);
|
|
75
|
+
var SerializableJsonSchema = import_zod.z.lazy(() => import_zod.z.union([
|
|
76
|
+
SerializableSchema,
|
|
77
|
+
import_zod.z.array(SerializableJsonSchema),
|
|
78
|
+
import_zod.z.record(SerializableJsonSchema)
|
|
79
|
+
]));
|
|
80
|
+
|
|
81
|
+
// ../common-schemas/src/error.ts
|
|
82
|
+
var import_zod2 = require("zod");
|
|
83
|
+
var ErrorSchema = import_zod2.z.object({
|
|
84
|
+
name: import_zod2.z.string(),
|
|
85
|
+
message: import_zod2.z.string(),
|
|
86
|
+
stackTrace: import_zod2.z.string().optional()
|
|
87
|
+
});
|
|
88
|
+
|
|
89
|
+
// ../common-schemas/src/logs.ts
|
|
90
|
+
var import_zod3 = require("zod");
|
|
91
|
+
var LogMessageSchema = import_zod3.z.object({
|
|
92
|
+
level: import_zod3.z.enum([
|
|
93
|
+
"DEBUG",
|
|
94
|
+
"INFO",
|
|
95
|
+
"WARN",
|
|
96
|
+
"ERROR"
|
|
97
|
+
]),
|
|
98
|
+
message: import_zod3.z.string(),
|
|
99
|
+
properties: JsonSchema.default({})
|
|
100
|
+
});
|
|
101
|
+
|
|
102
|
+
// ../common-schemas/src/waits.ts
|
|
103
|
+
var import_zod4 = require("zod");
|
|
104
|
+
var DelaySchema = import_zod4.z.object({
|
|
105
|
+
type: import_zod4.z.literal("DELAY"),
|
|
106
|
+
seconds: import_zod4.z.number().optional(),
|
|
107
|
+
minutes: import_zod4.z.number().optional(),
|
|
108
|
+
hours: import_zod4.z.number().optional(),
|
|
109
|
+
days: import_zod4.z.number().optional()
|
|
110
|
+
});
|
|
111
|
+
var ScheduledForSchema = import_zod4.z.object({
|
|
112
|
+
type: import_zod4.z.literal("SCHEDULE_FOR"),
|
|
113
|
+
scheduledFor: import_zod4.z.string().datetime()
|
|
114
|
+
});
|
|
115
|
+
var WaitSchema = import_zod4.z.discriminatedUnion("type", [
|
|
116
|
+
DelaySchema,
|
|
117
|
+
ScheduledForSchema
|
|
118
|
+
]);
|
|
119
|
+
|
|
120
|
+
// ../common-schemas/src/events.ts
|
|
121
|
+
var import_zod5 = require("zod");
|
|
122
|
+
var CustomEventSchema = import_zod5.z.object({
|
|
123
|
+
name: import_zod5.z.string(),
|
|
124
|
+
payload: JsonSchema,
|
|
125
|
+
context: JsonSchema.optional(),
|
|
126
|
+
timestamp: import_zod5.z.string().datetime().optional()
|
|
127
|
+
});
|
|
128
|
+
var SerializableCustomEventSchema = import_zod5.z.object({
|
|
129
|
+
name: import_zod5.z.string(),
|
|
130
|
+
payload: SerializableJsonSchema,
|
|
131
|
+
context: SerializableJsonSchema.optional(),
|
|
132
|
+
timestamp: import_zod5.z.string().datetime().optional()
|
|
133
|
+
});
|
|
134
|
+
var EventMatcherSchema = import_zod5.z.union([
|
|
135
|
+
import_zod5.z.array(import_zod5.z.string()),
|
|
136
|
+
import_zod5.z.array(import_zod5.z.number()),
|
|
137
|
+
import_zod5.z.array(import_zod5.z.boolean())
|
|
138
|
+
]);
|
|
139
|
+
var EventFilterSchema = import_zod5.z.lazy(() => import_zod5.z.record(import_zod5.z.union([
|
|
140
|
+
EventMatcherSchema,
|
|
141
|
+
EventFilterSchema
|
|
142
|
+
])));
|
|
143
|
+
|
|
144
|
+
// ../common-schemas/src/triggers.ts
|
|
145
|
+
var import_zod6 = require("zod");
|
|
146
|
+
var CustomEventTriggerSchema = import_zod6.z.object({
|
|
147
|
+
type: import_zod6.z.literal("CUSTOM_EVENT"),
|
|
148
|
+
service: import_zod6.z.literal("trigger"),
|
|
149
|
+
name: import_zod6.z.string(),
|
|
150
|
+
filter: EventFilterSchema
|
|
151
|
+
});
|
|
152
|
+
var WebhookEventTriggerSchema = import_zod6.z.object({
|
|
153
|
+
type: import_zod6.z.literal("WEBHOOK"),
|
|
154
|
+
service: import_zod6.z.string(),
|
|
155
|
+
name: import_zod6.z.string(),
|
|
156
|
+
filter: EventFilterSchema,
|
|
157
|
+
source: JsonSchema
|
|
158
|
+
});
|
|
159
|
+
var HttpEventTriggerSchema = import_zod6.z.object({
|
|
160
|
+
type: import_zod6.z.literal("HTTP_ENDPOINT"),
|
|
161
|
+
service: import_zod6.z.literal("trigger"),
|
|
162
|
+
name: import_zod6.z.string(),
|
|
163
|
+
filter: EventFilterSchema
|
|
164
|
+
});
|
|
165
|
+
var ScheduledEventTriggerSchema = import_zod6.z.object({
|
|
166
|
+
type: import_zod6.z.literal("SCHEDULE"),
|
|
167
|
+
service: import_zod6.z.literal("trigger"),
|
|
168
|
+
name: import_zod6.z.string(),
|
|
169
|
+
filter: EventFilterSchema
|
|
170
|
+
});
|
|
171
|
+
var TriggerMetadataSchema = import_zod6.z.discriminatedUnion("type", [
|
|
172
|
+
CustomEventTriggerSchema,
|
|
173
|
+
WebhookEventTriggerSchema,
|
|
174
|
+
HttpEventTriggerSchema,
|
|
175
|
+
ScheduledEventTriggerSchema
|
|
176
|
+
]);
|
|
177
|
+
|
|
178
|
+
// ../internal-bridge/src/schemas/host.ts
|
|
179
|
+
var HostRPCSchema = {
|
|
180
|
+
TRIGGER_WORKFLOW: {
|
|
181
|
+
request: import_zod7.z.object({
|
|
182
|
+
id: import_zod7.z.string(),
|
|
183
|
+
trigger: import_zod7.z.object({
|
|
184
|
+
input: JsonSchema.default({}),
|
|
185
|
+
context: JsonSchema.default({})
|
|
186
|
+
}),
|
|
187
|
+
meta: import_zod7.z.object({
|
|
188
|
+
environment: import_zod7.z.string(),
|
|
189
|
+
workflowId: import_zod7.z.string(),
|
|
190
|
+
organizationId: import_zod7.z.string(),
|
|
191
|
+
apiKey: import_zod7.z.string()
|
|
192
|
+
})
|
|
193
|
+
}),
|
|
194
|
+
response: import_zod7.z.boolean()
|
|
195
|
+
},
|
|
196
|
+
RESOLVE_REQUEST: {
|
|
197
|
+
request: import_zod7.z.object({
|
|
198
|
+
id: import_zod7.z.string(),
|
|
199
|
+
key: import_zod7.z.string(),
|
|
200
|
+
output: JsonSchema.default({}),
|
|
201
|
+
meta: import_zod7.z.object({
|
|
202
|
+
environment: import_zod7.z.string(),
|
|
203
|
+
workflowId: import_zod7.z.string(),
|
|
204
|
+
organizationId: import_zod7.z.string(),
|
|
205
|
+
apiKey: import_zod7.z.string(),
|
|
206
|
+
runId: import_zod7.z.string()
|
|
207
|
+
})
|
|
208
|
+
}),
|
|
209
|
+
response: import_zod7.z.boolean()
|
|
210
|
+
},
|
|
211
|
+
RESOLVE_DELAY: {
|
|
212
|
+
request: import_zod7.z.object({
|
|
213
|
+
id: import_zod7.z.string(),
|
|
214
|
+
key: import_zod7.z.string(),
|
|
215
|
+
meta: import_zod7.z.object({
|
|
216
|
+
environment: import_zod7.z.string(),
|
|
217
|
+
workflowId: import_zod7.z.string(),
|
|
218
|
+
organizationId: import_zod7.z.string(),
|
|
219
|
+
apiKey: import_zod7.z.string(),
|
|
220
|
+
runId: import_zod7.z.string()
|
|
221
|
+
})
|
|
222
|
+
}),
|
|
223
|
+
response: import_zod7.z.boolean()
|
|
224
|
+
},
|
|
225
|
+
REJECT_REQUEST: {
|
|
226
|
+
request: import_zod7.z.object({
|
|
227
|
+
id: import_zod7.z.string(),
|
|
228
|
+
key: import_zod7.z.string(),
|
|
229
|
+
error: JsonSchema.default({}),
|
|
230
|
+
meta: import_zod7.z.object({
|
|
231
|
+
environment: import_zod7.z.string(),
|
|
232
|
+
workflowId: import_zod7.z.string(),
|
|
233
|
+
organizationId: import_zod7.z.string(),
|
|
234
|
+
apiKey: import_zod7.z.string(),
|
|
235
|
+
runId: import_zod7.z.string()
|
|
236
|
+
})
|
|
237
|
+
}),
|
|
238
|
+
response: import_zod7.z.boolean()
|
|
239
|
+
}
|
|
240
|
+
};
|
|
241
|
+
|
|
242
|
+
// ../internal-bridge/src/schemas/server.ts
|
|
243
|
+
var import_zod8 = require("zod");
|
|
244
|
+
var ServerRPCSchema = {
|
|
245
|
+
INITIALIZE_DELAY: {
|
|
246
|
+
request: import_zod8.z.object({
|
|
247
|
+
runId: import_zod8.z.string(),
|
|
248
|
+
key: import_zod8.z.string(),
|
|
249
|
+
wait: WaitSchema,
|
|
250
|
+
timestamp: import_zod8.z.string()
|
|
251
|
+
}),
|
|
252
|
+
response: import_zod8.z.boolean()
|
|
253
|
+
},
|
|
254
|
+
SEND_REQUEST: {
|
|
255
|
+
request: import_zod8.z.object({
|
|
256
|
+
runId: import_zod8.z.string(),
|
|
257
|
+
key: import_zod8.z.string(),
|
|
258
|
+
request: import_zod8.z.object({
|
|
259
|
+
service: import_zod8.z.string(),
|
|
260
|
+
endpoint: import_zod8.z.string(),
|
|
261
|
+
params: import_zod8.z.any()
|
|
262
|
+
}),
|
|
263
|
+
timestamp: import_zod8.z.string()
|
|
264
|
+
}),
|
|
265
|
+
response: import_zod8.z.boolean()
|
|
266
|
+
},
|
|
267
|
+
SEND_LOG: {
|
|
268
|
+
request: import_zod8.z.object({
|
|
269
|
+
runId: import_zod8.z.string(),
|
|
270
|
+
key: import_zod8.z.string(),
|
|
271
|
+
log: import_zod8.z.object({
|
|
272
|
+
message: import_zod8.z.string(),
|
|
273
|
+
level: import_zod8.z.enum([
|
|
274
|
+
"DEBUG",
|
|
275
|
+
"INFO",
|
|
276
|
+
"WARN",
|
|
277
|
+
"ERROR"
|
|
278
|
+
]),
|
|
279
|
+
properties: import_zod8.z.string().optional()
|
|
280
|
+
}),
|
|
281
|
+
timestamp: import_zod8.z.string()
|
|
282
|
+
}),
|
|
283
|
+
response: import_zod8.z.boolean()
|
|
284
|
+
},
|
|
285
|
+
SEND_EVENT: {
|
|
286
|
+
request: import_zod8.z.object({
|
|
287
|
+
runId: import_zod8.z.string(),
|
|
288
|
+
key: import_zod8.z.string(),
|
|
289
|
+
event: CustomEventSchema,
|
|
290
|
+
timestamp: import_zod8.z.string()
|
|
291
|
+
}),
|
|
292
|
+
response: import_zod8.z.boolean()
|
|
293
|
+
},
|
|
294
|
+
INITIALIZE_HOST: {
|
|
295
|
+
request: import_zod8.z.object({
|
|
296
|
+
apiKey: import_zod8.z.string(),
|
|
297
|
+
workflowId: import_zod8.z.string(),
|
|
298
|
+
workflowName: import_zod8.z.string(),
|
|
299
|
+
trigger: TriggerMetadataSchema,
|
|
300
|
+
packageVersion: import_zod8.z.string(),
|
|
301
|
+
packageName: import_zod8.z.string()
|
|
302
|
+
}),
|
|
303
|
+
response: import_zod8.z.discriminatedUnion("type", [
|
|
304
|
+
import_zod8.z.object({
|
|
305
|
+
type: import_zod8.z.literal("success")
|
|
306
|
+
}),
|
|
307
|
+
import_zod8.z.object({
|
|
308
|
+
type: import_zod8.z.literal("error"),
|
|
309
|
+
message: import_zod8.z.string()
|
|
310
|
+
})
|
|
311
|
+
]).nullable()
|
|
312
|
+
},
|
|
313
|
+
START_WORKFLOW_RUN: {
|
|
314
|
+
request: import_zod8.z.object({
|
|
315
|
+
runId: import_zod8.z.string(),
|
|
316
|
+
timestamp: import_zod8.z.string()
|
|
317
|
+
}),
|
|
318
|
+
response: import_zod8.z.boolean()
|
|
319
|
+
},
|
|
320
|
+
COMPLETE_WORKFLOW_RUN: {
|
|
321
|
+
request: import_zod8.z.object({
|
|
322
|
+
runId: import_zod8.z.string(),
|
|
323
|
+
output: import_zod8.z.string(),
|
|
324
|
+
timestamp: import_zod8.z.string()
|
|
325
|
+
}),
|
|
326
|
+
response: import_zod8.z.boolean()
|
|
327
|
+
},
|
|
328
|
+
SEND_WORKFLOW_ERROR: {
|
|
329
|
+
request: import_zod8.z.object({
|
|
330
|
+
runId: import_zod8.z.string(),
|
|
331
|
+
error: import_zod8.z.object({
|
|
332
|
+
name: import_zod8.z.string(),
|
|
333
|
+
message: import_zod8.z.string(),
|
|
334
|
+
stackTrace: import_zod8.z.string().optional()
|
|
335
|
+
}),
|
|
336
|
+
timestamp: import_zod8.z.string()
|
|
337
|
+
}),
|
|
338
|
+
response: import_zod8.z.boolean()
|
|
339
|
+
}
|
|
340
|
+
};
|
|
341
|
+
|
|
342
|
+
// ../internal-bridge/src/schemas/common.ts
|
|
343
|
+
var import_zod9 = require("zod");
|
|
344
|
+
var MESSAGE_META = import_zod9.z.object({
|
|
345
|
+
data: import_zod9.z.any(),
|
|
346
|
+
id: import_zod9.z.string(),
|
|
347
|
+
type: import_zod9.z.union([
|
|
348
|
+
import_zod9.z.literal("ACK"),
|
|
349
|
+
import_zod9.z.literal("MESSAGE")
|
|
350
|
+
])
|
|
351
|
+
});
|
|
352
|
+
var TriggerEnvironmentSchema = import_zod9.z.enum([
|
|
353
|
+
"live",
|
|
354
|
+
"development"
|
|
355
|
+
]);
|
|
356
|
+
|
|
357
|
+
// ../internal-bridge/src/zodRPC.ts
|
|
358
|
+
var import_zod10 = require("zod");
|
|
359
|
+
var import_node_crypto = require("crypto");
|
|
360
|
+
var RPCMessageSchema = import_zod10.z.object({
|
|
361
|
+
id: import_zod10.z.string(),
|
|
362
|
+
methodName: import_zod10.z.string(),
|
|
363
|
+
data: import_zod10.z.any(),
|
|
364
|
+
kind: import_zod10.z.enum([
|
|
365
|
+
"CALL",
|
|
366
|
+
"RESPONSE"
|
|
367
|
+
])
|
|
368
|
+
});
|
|
369
|
+
var _connection, _sender, _receiver, _handlers, _pendingCalls, _onMessage, onMessage_fn, _onCall, onCall_fn, _onResponse, onResponse_fn, _handleCall, handleCall_fn, _handleResponse, handleResponse_fn;
|
|
370
|
+
var ZodRPC = class {
|
|
371
|
+
constructor(options) {
|
|
372
|
+
__privateAdd(this, _onMessage);
|
|
373
|
+
__privateAdd(this, _onCall);
|
|
374
|
+
__privateAdd(this, _onResponse);
|
|
375
|
+
__privateAdd(this, _handleCall);
|
|
376
|
+
__privateAdd(this, _handleResponse);
|
|
377
|
+
__privateAdd(this, _connection, void 0);
|
|
378
|
+
__privateAdd(this, _sender, void 0);
|
|
379
|
+
__privateAdd(this, _receiver, void 0);
|
|
380
|
+
__privateAdd(this, _handlers, void 0);
|
|
381
|
+
__privateAdd(this, _pendingCalls, /* @__PURE__ */ new Map());
|
|
382
|
+
__privateSet(this, _connection, options.connection);
|
|
383
|
+
__privateSet(this, _sender, options.sender);
|
|
384
|
+
__privateSet(this, _receiver, options.receiver);
|
|
385
|
+
__privateSet(this, _handlers, options.handlers);
|
|
386
|
+
__privateGet(this, _connection).onMessage.attach(__privateMethod(this, _onMessage, onMessage_fn).bind(this));
|
|
387
|
+
}
|
|
388
|
+
resetConnection(connection) {
|
|
389
|
+
__privateGet(this, _connection).onMessage.detach();
|
|
390
|
+
__privateSet(this, _connection, connection);
|
|
391
|
+
__privateGet(this, _connection).onMessage.attach(__privateMethod(this, _onMessage, onMessage_fn).bind(this));
|
|
392
|
+
}
|
|
393
|
+
send(key, data) {
|
|
394
|
+
const id = generateStableId(__privateGet(this, _connection).id, key, data);
|
|
395
|
+
const message = packageMessage({
|
|
396
|
+
id,
|
|
397
|
+
methodName: key,
|
|
398
|
+
data
|
|
399
|
+
});
|
|
400
|
+
return new Promise((resolve, reject) => {
|
|
401
|
+
__privateGet(this, _pendingCalls).set(id, (rawResponseText) => {
|
|
402
|
+
try {
|
|
403
|
+
const parsed = __privateGet(this, _sender)[key]["response"].parse(rawResponseText);
|
|
404
|
+
return resolve(parsed);
|
|
405
|
+
} catch (err) {
|
|
406
|
+
reject(err);
|
|
407
|
+
}
|
|
408
|
+
});
|
|
409
|
+
__privateGet(this, _connection).send(message).catch((err) => reject(err));
|
|
410
|
+
});
|
|
411
|
+
}
|
|
412
|
+
};
|
|
413
|
+
__name(ZodRPC, "ZodRPC");
|
|
414
|
+
_connection = new WeakMap();
|
|
415
|
+
_sender = new WeakMap();
|
|
416
|
+
_receiver = new WeakMap();
|
|
417
|
+
_handlers = new WeakMap();
|
|
418
|
+
_pendingCalls = new WeakMap();
|
|
419
|
+
_onMessage = new WeakSet();
|
|
420
|
+
onMessage_fn = /* @__PURE__ */ __name(async function(rawData) {
|
|
421
|
+
try {
|
|
422
|
+
const data = RPCMessageSchema.parse(JSON.parse(rawData));
|
|
423
|
+
if (data.kind === "CALL") {
|
|
424
|
+
await __privateMethod(this, _onCall, onCall_fn).call(this, data);
|
|
425
|
+
}
|
|
426
|
+
if (data.kind === "RESPONSE") {
|
|
427
|
+
await __privateMethod(this, _onResponse, onResponse_fn).call(this, data);
|
|
428
|
+
}
|
|
429
|
+
} catch (err) {
|
|
430
|
+
console.error(err);
|
|
431
|
+
}
|
|
432
|
+
}, "#onMessage");
|
|
433
|
+
_onCall = new WeakSet();
|
|
434
|
+
onCall_fn = /* @__PURE__ */ __name(async function(message) {
|
|
435
|
+
try {
|
|
436
|
+
await __privateMethod(this, _handleCall, handleCall_fn).call(this, message);
|
|
437
|
+
} catch (callError) {
|
|
438
|
+
if (callError instanceof import_zod10.ZodError) {
|
|
439
|
+
console.error(`[ZodRPC][foobar] Received invalid call:
|
|
440
|
+
${JSON.stringify(message)}: `, callError.errors);
|
|
441
|
+
} else {
|
|
442
|
+
console.error(`[ZodRPC] Error handling call:
|
|
443
|
+
${JSON.stringify(message)}: `, callError);
|
|
444
|
+
}
|
|
445
|
+
}
|
|
446
|
+
}, "#onCall");
|
|
447
|
+
_onResponse = new WeakSet();
|
|
448
|
+
onResponse_fn = /* @__PURE__ */ __name(async function(message1) {
|
|
449
|
+
try {
|
|
450
|
+
await __privateMethod(this, _handleResponse, handleResponse_fn).call(this, message1);
|
|
451
|
+
} catch (callError) {
|
|
452
|
+
if (callError instanceof import_zod10.ZodError) {
|
|
453
|
+
console.error(`[ZodRPC] Received invalid response
|
|
454
|
+
|
|
455
|
+
${JSON.stringify(message1)}: `, callError.flatten());
|
|
456
|
+
} else {
|
|
457
|
+
console.error(`[ZodRPC] Error handling response
|
|
458
|
+
|
|
459
|
+
${JSON.stringify(message1)}: `, callError);
|
|
460
|
+
}
|
|
461
|
+
}
|
|
462
|
+
}, "#onResponse");
|
|
463
|
+
_handleCall = new WeakSet();
|
|
464
|
+
handleCall_fn = /* @__PURE__ */ __name(async function(message2) {
|
|
465
|
+
const receiver = __privateGet(this, _receiver);
|
|
466
|
+
const methodName = message2.methodName;
|
|
467
|
+
const method = __privateGet(this, _receiver)[methodName];
|
|
468
|
+
if (!method) {
|
|
469
|
+
throw new Error(`There is no method for ${message2.methodName}`);
|
|
470
|
+
}
|
|
471
|
+
const inputs = method.request.parse(message2.data);
|
|
472
|
+
const handler = __privateGet(this, _handlers)[methodName];
|
|
473
|
+
const returnValue = await handler(inputs);
|
|
474
|
+
const preparedResponseText = packageResponse({
|
|
475
|
+
id: message2.id,
|
|
476
|
+
methodName,
|
|
477
|
+
data: returnValue
|
|
478
|
+
});
|
|
479
|
+
try {
|
|
480
|
+
await __privateGet(this, _connection).send(preparedResponseText);
|
|
481
|
+
} catch (err) {
|
|
482
|
+
console.error("Failed sending response", preparedResponseText, err);
|
|
483
|
+
}
|
|
484
|
+
return;
|
|
485
|
+
}, "#handleCall");
|
|
486
|
+
_handleResponse = new WeakSet();
|
|
487
|
+
handleResponse_fn = /* @__PURE__ */ __name(async function(message3) {
|
|
488
|
+
const responseCallback = __privateGet(this, _pendingCalls).get(message3.id);
|
|
489
|
+
if (!responseCallback)
|
|
490
|
+
return;
|
|
491
|
+
responseCallback(message3.data);
|
|
492
|
+
__privateGet(this, _pendingCalls).delete(message3.id);
|
|
493
|
+
}, "#handleResponse");
|
|
494
|
+
function generateStableId(connId, reqKey, reqData) {
|
|
495
|
+
const serializedData = JSON.stringify(reqData);
|
|
496
|
+
const inputString = connId + reqKey + serializedData;
|
|
497
|
+
const hash = (0, import_node_crypto.createHash)("sha256").update(inputString).digest("hex");
|
|
498
|
+
return hash;
|
|
499
|
+
}
|
|
500
|
+
__name(generateStableId, "generateStableId");
|
|
501
|
+
function packageMessage({ id, methodName, data }) {
|
|
502
|
+
const callerData = {
|
|
503
|
+
id,
|
|
504
|
+
kind: "CALL",
|
|
505
|
+
data,
|
|
506
|
+
methodName
|
|
507
|
+
};
|
|
508
|
+
return JSON.stringify(callerData);
|
|
509
|
+
}
|
|
510
|
+
__name(packageMessage, "packageMessage");
|
|
511
|
+
function packageResponse({ id, methodName, data }) {
|
|
512
|
+
const preparedResponseText = {
|
|
513
|
+
id,
|
|
514
|
+
kind: "RESPONSE",
|
|
515
|
+
methodName,
|
|
516
|
+
data
|
|
517
|
+
};
|
|
518
|
+
return JSON.stringify(preparedResponseText);
|
|
519
|
+
}
|
|
520
|
+
__name(packageResponse, "packageResponse");
|
|
521
|
+
|
|
522
|
+
// ../internal-bridge/src/logger.ts
|
|
523
|
+
var logLevels = [
|
|
524
|
+
"log",
|
|
525
|
+
"error",
|
|
526
|
+
"warn",
|
|
527
|
+
"info",
|
|
528
|
+
"debug"
|
|
529
|
+
];
|
|
530
|
+
var _name, _level;
|
|
531
|
+
var Logger = class {
|
|
532
|
+
constructor(name2, level = "info") {
|
|
533
|
+
__privateAdd(this, _name, void 0);
|
|
534
|
+
__privateAdd(this, _level, void 0);
|
|
535
|
+
__privateSet(this, _name, name2);
|
|
536
|
+
__privateSet(this, _level, logLevels.indexOf(process.env.TRIGGER_LOG_LEVEL ?? level));
|
|
537
|
+
}
|
|
538
|
+
log(...args) {
|
|
539
|
+
if (__privateGet(this, _level) < 0)
|
|
540
|
+
return;
|
|
541
|
+
console.log(`[${formattedDateTime()}] [${__privateGet(this, _name)}] `, ...args);
|
|
542
|
+
}
|
|
543
|
+
error(...args) {
|
|
544
|
+
if (__privateGet(this, _level) < 1)
|
|
545
|
+
return;
|
|
546
|
+
console.error(`[${formattedDateTime()}] [${__privateGet(this, _name)}] `, ...args);
|
|
547
|
+
}
|
|
548
|
+
warn(...args) {
|
|
549
|
+
if (__privateGet(this, _level) < 2)
|
|
550
|
+
return;
|
|
551
|
+
console.warn(`[${formattedDateTime()}] [${__privateGet(this, _name)}] `, ...args);
|
|
552
|
+
}
|
|
553
|
+
info(...args) {
|
|
554
|
+
if (__privateGet(this, _level) < 3)
|
|
555
|
+
return;
|
|
556
|
+
console.info(`[${formattedDateTime()}] [${__privateGet(this, _name)}] `, ...args);
|
|
557
|
+
}
|
|
558
|
+
debug(...args) {
|
|
559
|
+
if (__privateGet(this, _level) < 4)
|
|
560
|
+
return;
|
|
561
|
+
console.debug(`[${formattedDateTime()}] [${__privateGet(this, _name)}] `, ...args);
|
|
562
|
+
}
|
|
563
|
+
};
|
|
564
|
+
__name(Logger, "Logger");
|
|
565
|
+
_name = new WeakMap();
|
|
566
|
+
_level = new WeakMap();
|
|
567
|
+
function formattedDateTime() {
|
|
568
|
+
const date = new Date();
|
|
569
|
+
const hours = date.getHours();
|
|
570
|
+
const minutes = date.getMinutes();
|
|
571
|
+
const seconds = date.getSeconds();
|
|
572
|
+
const milliseconds = date.getMilliseconds();
|
|
573
|
+
const formattedHours = hours < 10 ? `0${hours}` : hours;
|
|
574
|
+
const formattedMinutes = minutes < 10 ? `0${minutes}` : minutes;
|
|
575
|
+
const formattedSeconds = seconds < 10 ? `0${seconds}` : seconds;
|
|
576
|
+
const formattedMilliseconds = milliseconds < 10 ? `00${milliseconds}` : milliseconds < 100 ? `0${milliseconds}` : milliseconds;
|
|
577
|
+
return `${formattedHours}:${formattedMinutes}:${formattedSeconds}.${formattedMilliseconds}`;
|
|
578
|
+
}
|
|
579
|
+
__name(formattedDateTime, "formattedDateTime");
|
|
580
|
+
|
|
581
|
+
// src/client.ts
|
|
582
|
+
var import_uuid2 = require("uuid");
|
|
583
|
+
var import_ws = require("ws");
|
|
584
|
+
|
|
585
|
+
// package.json
|
|
586
|
+
var name = "@trigger.dev/sdk";
|
|
587
|
+
var version = "0.1.1";
|
|
588
|
+
|
|
589
|
+
// src/connection.ts
|
|
590
|
+
var import_uuid = require("uuid");
|
|
591
|
+
var import_evt = require("evt");
|
|
592
|
+
var TimeoutError = class extends Error {
|
|
593
|
+
};
|
|
594
|
+
__name(TimeoutError, "TimeoutError");
|
|
595
|
+
var NotConnectedError = class extends Error {
|
|
596
|
+
};
|
|
597
|
+
__name(NotConnectedError, "NotConnectedError");
|
|
598
|
+
var _socket, _connectTimeout, _sendTimeout, _pingTimeout, _isAuthenticated, _timeouts, _isClosed, _pendingMessages, _logger, _pingIntervalHandle, _pingIntervalMs, _closeUnresponsiveConnectionTimeoutMs, _startPingInterval, startPingInterval_fn, _ping, ping_fn;
|
|
599
|
+
var HostConnection = class {
|
|
600
|
+
constructor(socket, options) {
|
|
601
|
+
__privateAdd(this, _startPingInterval);
|
|
602
|
+
__privateAdd(this, _ping);
|
|
603
|
+
__privateAdd(this, _socket, void 0);
|
|
604
|
+
__privateAdd(this, _connectTimeout, void 0);
|
|
605
|
+
__privateAdd(this, _sendTimeout, void 0);
|
|
606
|
+
__privateAdd(this, _pingTimeout, void 0);
|
|
607
|
+
__privateAdd(this, _isAuthenticated, false);
|
|
608
|
+
__privateAdd(this, _timeouts, void 0);
|
|
609
|
+
__privateAdd(this, _isClosed, false);
|
|
610
|
+
__privateAdd(this, _pendingMessages, /* @__PURE__ */ new Map());
|
|
611
|
+
__privateAdd(this, _logger, void 0);
|
|
612
|
+
__privateAdd(this, _pingIntervalHandle, void 0);
|
|
613
|
+
__privateAdd(this, _pingIntervalMs, 3e4);
|
|
614
|
+
__privateAdd(this, _closeUnresponsiveConnectionTimeoutMs, 3 * 60 * 1e3);
|
|
615
|
+
__privateSet(this, _socket, socket);
|
|
616
|
+
this.id = options?.id ?? (0, import_uuid.v4)();
|
|
617
|
+
this.onMessage = new import_evt.Evt();
|
|
618
|
+
this.onAuthenticated = new import_evt.Evt();
|
|
619
|
+
this.onClose = new import_evt.Evt();
|
|
620
|
+
this.onOpen = new import_evt.Evt();
|
|
621
|
+
this.onError = new import_evt.Evt();
|
|
622
|
+
__privateSet(this, _connectTimeout, options?.connectTimeout ?? 5e3);
|
|
623
|
+
__privateSet(this, _sendTimeout, options?.sendTimeout ?? 5e3);
|
|
624
|
+
__privateSet(this, _pingTimeout, options?.pingTimeout ?? 5e3);
|
|
625
|
+
__privateSet(this, _timeouts, /* @__PURE__ */ new Set());
|
|
626
|
+
__privateSet(this, _logger, new Logger("trigger.dev connection"));
|
|
627
|
+
this.onClose.attach(() => {
|
|
628
|
+
__privateSet(this, _isClosed, true);
|
|
629
|
+
if (__privateGet(this, _pingIntervalHandle)) {
|
|
630
|
+
clearInterval(__privateGet(this, _pingIntervalHandle));
|
|
631
|
+
__privateSet(this, _pingIntervalHandle, void 0);
|
|
632
|
+
}
|
|
633
|
+
for (const timeout of __privateGet(this, _timeouts)) {
|
|
634
|
+
clearTimeout(timeout);
|
|
635
|
+
}
|
|
636
|
+
__privateGet(this, _timeouts).clear();
|
|
637
|
+
});
|
|
638
|
+
__privateGet(this, _socket).onopen = () => {
|
|
639
|
+
__privateSet(this, _isClosed, false);
|
|
640
|
+
this.onOpen.post();
|
|
641
|
+
__privateMethod(this, _startPingInterval, startPingInterval_fn).call(this);
|
|
642
|
+
};
|
|
643
|
+
__privateGet(this, _socket).onclose = (ev) => {
|
|
644
|
+
this.onClose.post([
|
|
645
|
+
ev.code,
|
|
646
|
+
ev.reason
|
|
647
|
+
]);
|
|
648
|
+
};
|
|
649
|
+
__privateGet(this, _socket).onerror = (ev) => {
|
|
650
|
+
const message = "message" in ev ? ev.message : "Unknown error";
|
|
651
|
+
this.onError.post(new Error(message));
|
|
652
|
+
};
|
|
653
|
+
__privateGet(this, _socket).onmessage = (event) => {
|
|
654
|
+
if (__privateGet(this, _isClosed))
|
|
655
|
+
return;
|
|
656
|
+
const data = JSON.parse(event.data.toString());
|
|
657
|
+
const metadata = MESSAGE_META.parse(data);
|
|
658
|
+
if (metadata.type === "ACK") {
|
|
659
|
+
const pendingMessage = __privateGet(this, _pendingMessages).get(metadata.id);
|
|
660
|
+
if (pendingMessage) {
|
|
661
|
+
pendingMessage.onAckReceived();
|
|
662
|
+
__privateGet(this, _pendingMessages).delete(metadata.id);
|
|
663
|
+
}
|
|
664
|
+
}
|
|
665
|
+
if (metadata.type === "MESSAGE") {
|
|
666
|
+
socket.send(JSON.stringify({
|
|
667
|
+
type: "ACK",
|
|
668
|
+
id: metadata.id
|
|
669
|
+
}));
|
|
670
|
+
if (metadata.data === "AUTHENTICATED") {
|
|
671
|
+
__privateSet(this, _isAuthenticated, true);
|
|
672
|
+
this.onAuthenticated.post();
|
|
673
|
+
return;
|
|
674
|
+
}
|
|
675
|
+
this.onMessage.post(metadata.data);
|
|
676
|
+
}
|
|
677
|
+
};
|
|
678
|
+
if ("pong" in socket) {
|
|
679
|
+
socket.on("pong", (buf) => {
|
|
680
|
+
const id = buf.toString();
|
|
681
|
+
const pendingMessage = __privateGet(this, _pendingMessages).get(id);
|
|
682
|
+
if (pendingMessage?.data === "ping") {
|
|
683
|
+
pendingMessage.onAckReceived();
|
|
684
|
+
}
|
|
685
|
+
});
|
|
686
|
+
}
|
|
687
|
+
}
|
|
688
|
+
async connect() {
|
|
689
|
+
__privateGet(this, _logger).debug("[connect] Attempting to connect");
|
|
690
|
+
return new Promise((resolve, reject) => {
|
|
691
|
+
if (__privateGet(this, _socket).readyState === __privateGet(this, _socket).OPEN && __privateGet(this, _isAuthenticated)) {
|
|
692
|
+
__privateGet(this, _logger).debug("[connect] Already connected, resolving");
|
|
693
|
+
return resolve();
|
|
694
|
+
}
|
|
695
|
+
const failTimeout = setTimeout(() => {
|
|
696
|
+
__privateGet(this, _logger).debug("[connect] Connection timed out, rejecting");
|
|
697
|
+
reject(new TimeoutError());
|
|
698
|
+
}, __privateGet(this, _connectTimeout));
|
|
699
|
+
__privateGet(this, _timeouts).add(failTimeout);
|
|
700
|
+
this.onAuthenticated.attach(() => {
|
|
701
|
+
clearTimeout(failTimeout);
|
|
702
|
+
__privateGet(this, _timeouts).delete(failTimeout);
|
|
703
|
+
__privateGet(this, _logger).debug("[connect] Connected, resolving");
|
|
704
|
+
resolve();
|
|
705
|
+
});
|
|
706
|
+
});
|
|
707
|
+
}
|
|
708
|
+
async send(data) {
|
|
709
|
+
if (__privateGet(this, _isClosed))
|
|
710
|
+
throw new NotConnectedError();
|
|
711
|
+
return new Promise((resolve, reject) => {
|
|
712
|
+
const id = (0, import_uuid.v4)();
|
|
713
|
+
const failTimeout = setTimeout(() => {
|
|
714
|
+
reject(new TimeoutError());
|
|
715
|
+
}, __privateGet(this, _sendTimeout));
|
|
716
|
+
__privateGet(this, _timeouts).add(failTimeout);
|
|
717
|
+
__privateGet(this, _pendingMessages).set(id, {
|
|
718
|
+
data,
|
|
719
|
+
onAckReceived: () => {
|
|
720
|
+
clearTimeout(failTimeout);
|
|
721
|
+
__privateGet(this, _timeouts).delete(failTimeout);
|
|
722
|
+
resolve();
|
|
723
|
+
}
|
|
724
|
+
});
|
|
725
|
+
__privateGet(this, _socket).send(JSON.stringify({
|
|
726
|
+
id,
|
|
727
|
+
data,
|
|
728
|
+
type: "MESSAGE"
|
|
729
|
+
}));
|
|
730
|
+
});
|
|
731
|
+
}
|
|
732
|
+
close(code, reason) {
|
|
733
|
+
__privateSet(this, _isClosed, true);
|
|
734
|
+
this.onMessage.detach();
|
|
735
|
+
return __privateGet(this, _socket).close(code, reason);
|
|
736
|
+
}
|
|
737
|
+
};
|
|
738
|
+
__name(HostConnection, "HostConnection");
|
|
739
|
+
_socket = new WeakMap();
|
|
740
|
+
_connectTimeout = new WeakMap();
|
|
741
|
+
_sendTimeout = new WeakMap();
|
|
742
|
+
_pingTimeout = new WeakMap();
|
|
743
|
+
_isAuthenticated = new WeakMap();
|
|
744
|
+
_timeouts = new WeakMap();
|
|
745
|
+
_isClosed = new WeakMap();
|
|
746
|
+
_pendingMessages = new WeakMap();
|
|
747
|
+
_logger = new WeakMap();
|
|
748
|
+
_pingIntervalHandle = new WeakMap();
|
|
749
|
+
_pingIntervalMs = new WeakMap();
|
|
750
|
+
_closeUnresponsiveConnectionTimeoutMs = new WeakMap();
|
|
751
|
+
_startPingInterval = new WeakSet();
|
|
752
|
+
startPingInterval_fn = /* @__PURE__ */ __name(function() {
|
|
753
|
+
let lastSuccessfulPing = new Date();
|
|
754
|
+
__privateSet(this, _pingIntervalHandle, setInterval(async () => {
|
|
755
|
+
if (!__privateGet(this, _socket).OPEN) {
|
|
756
|
+
if (__privateGet(this, _pingIntervalHandle)) {
|
|
757
|
+
clearInterval(__privateGet(this, _pingIntervalHandle));
|
|
758
|
+
__privateSet(this, _pingIntervalHandle, void 0);
|
|
759
|
+
}
|
|
760
|
+
return;
|
|
761
|
+
}
|
|
762
|
+
try {
|
|
763
|
+
await __privateMethod(this, _ping, ping_fn).call(this);
|
|
764
|
+
lastSuccessfulPing = new Date();
|
|
765
|
+
} catch (err) {
|
|
766
|
+
__privateGet(this, _logger).warn("Pong not received in time");
|
|
767
|
+
if (!(err instanceof TimeoutError)) {
|
|
768
|
+
__privateGet(this, _logger).error(err);
|
|
769
|
+
}
|
|
770
|
+
if (lastSuccessfulPing.getTime() < new Date().getTime() - __privateGet(this, _closeUnresponsiveConnectionTimeoutMs)) {
|
|
771
|
+
__privateGet(this, _logger).error("No pong received in last three minutes, closing connection to Interval and retrying...");
|
|
772
|
+
if (__privateGet(this, _pingIntervalHandle)) {
|
|
773
|
+
clearInterval(__privateGet(this, _pingIntervalHandle));
|
|
774
|
+
__privateSet(this, _pingIntervalHandle, void 0);
|
|
775
|
+
}
|
|
776
|
+
__privateGet(this, _socket).close();
|
|
777
|
+
}
|
|
778
|
+
}
|
|
779
|
+
}, __privateGet(this, _pingIntervalMs)));
|
|
780
|
+
}, "#startPingInterval");
|
|
781
|
+
_ping = new WeakSet();
|
|
782
|
+
ping_fn = /* @__PURE__ */ __name(async function() {
|
|
783
|
+
if (!__privateGet(this, _socket).OPEN) {
|
|
784
|
+
throw new NotConnectedError();
|
|
785
|
+
}
|
|
786
|
+
if (!("ping" in __privateGet(this, _socket))) {
|
|
787
|
+
throw new Error("ping not supported in this underlying websocket connection");
|
|
788
|
+
}
|
|
789
|
+
const socket = __privateGet(this, _socket);
|
|
790
|
+
return new Promise((resolve, reject) => {
|
|
791
|
+
const id = (0, import_uuid.v4)();
|
|
792
|
+
const failTimeout = setTimeout(() => {
|
|
793
|
+
reject(new TimeoutError("Pong not received in time"));
|
|
794
|
+
}, __privateGet(this, _pingTimeout));
|
|
795
|
+
__privateGet(this, _timeouts).add(failTimeout);
|
|
796
|
+
__privateGet(this, _pendingMessages).set(id, {
|
|
797
|
+
data: "ping",
|
|
798
|
+
onAckReceived: () => {
|
|
799
|
+
clearTimeout(failTimeout);
|
|
800
|
+
__privateGet(this, _timeouts).delete(failTimeout);
|
|
801
|
+
__privateGet(this, _logger).debug(`Resolving ping`);
|
|
802
|
+
resolve();
|
|
803
|
+
}
|
|
804
|
+
});
|
|
805
|
+
__privateGet(this, _logger).debug(`Sending ping ${id} to ${socket.url}`);
|
|
806
|
+
socket.ping(id, void 0, (err) => {
|
|
807
|
+
if (err) {
|
|
808
|
+
reject(err);
|
|
809
|
+
}
|
|
810
|
+
});
|
|
811
|
+
});
|
|
812
|
+
}, "#ping");
|
|
813
|
+
|
|
814
|
+
// src/localStorage.ts
|
|
815
|
+
var import_node_async_hooks = require("async_hooks");
|
|
816
|
+
var triggerRunLocalStorage = new import_node_async_hooks.AsyncLocalStorage();
|
|
817
|
+
|
|
818
|
+
// src/logger.ts
|
|
819
|
+
var ContextLogger = class {
|
|
820
|
+
constructor(callback) {
|
|
821
|
+
this.callback = callback;
|
|
822
|
+
}
|
|
823
|
+
debug(message, properties) {
|
|
824
|
+
return this.callback("DEBUG", message, properties);
|
|
825
|
+
}
|
|
826
|
+
info(message, properties) {
|
|
827
|
+
return this.callback("INFO", message, properties);
|
|
828
|
+
}
|
|
829
|
+
warn(message, properties) {
|
|
830
|
+
return this.callback("WARN", message, properties);
|
|
831
|
+
}
|
|
832
|
+
error(message, properties) {
|
|
833
|
+
return this.callback("ERROR", message, properties);
|
|
834
|
+
}
|
|
835
|
+
};
|
|
836
|
+
__name(ContextLogger, "ContextLogger");
|
|
837
|
+
|
|
838
|
+
// src/client.ts
|
|
839
|
+
var _trigger, _options, _connection2, _serverRPC, _apiKey, _endpoint, _isConnected, _retryIntervalMs, _logger2, _closedByUser, _responseCompleteCallbacks, _waitForCallbacks, _initializeConnection, initializeConnection_fn, _initializeRPC, initializeRPC_fn, _initializeHost, initializeHost_fn, _send, send_fn;
|
|
840
|
+
var TriggerClient = class {
|
|
841
|
+
constructor(trigger, options) {
|
|
842
|
+
__privateAdd(this, _initializeConnection);
|
|
843
|
+
__privateAdd(this, _initializeRPC);
|
|
844
|
+
__privateAdd(this, _initializeHost);
|
|
845
|
+
__privateAdd(this, _send);
|
|
846
|
+
__privateAdd(this, _trigger, void 0);
|
|
847
|
+
__privateAdd(this, _options, void 0);
|
|
848
|
+
__privateAdd(this, _connection2, void 0);
|
|
849
|
+
__privateAdd(this, _serverRPC, void 0);
|
|
850
|
+
__privateAdd(this, _apiKey, void 0);
|
|
851
|
+
__privateAdd(this, _endpoint, void 0);
|
|
852
|
+
__privateAdd(this, _isConnected, false);
|
|
853
|
+
__privateAdd(this, _retryIntervalMs, 3e3);
|
|
854
|
+
__privateAdd(this, _logger2, void 0);
|
|
855
|
+
__privateAdd(this, _closedByUser, false);
|
|
856
|
+
__privateAdd(this, _responseCompleteCallbacks, /* @__PURE__ */ new Map());
|
|
857
|
+
__privateAdd(this, _waitForCallbacks, /* @__PURE__ */ new Map());
|
|
858
|
+
__privateSet(this, _trigger, trigger);
|
|
859
|
+
__privateSet(this, _options, options);
|
|
860
|
+
const apiKey = __privateGet(this, _options).apiKey ?? process.env.TRIGGER_API_KEY;
|
|
861
|
+
if (!apiKey) {
|
|
862
|
+
throw new Error("Cannot connect to Trigger because of invalid API Key: Please include an API Key in the `apiKey` option or in the `TRIGGER_API_KEY` environment variable.");
|
|
863
|
+
}
|
|
864
|
+
__privateSet(this, _apiKey, apiKey);
|
|
865
|
+
__privateSet(this, _endpoint, __privateGet(this, _options).endpoint ?? "wss://wss.trigger.dev/ws");
|
|
866
|
+
__privateSet(this, _logger2, new Logger("trigger.dev", __privateGet(this, _options).logLevel));
|
|
867
|
+
}
|
|
868
|
+
async listen(instanceId) {
|
|
869
|
+
await __privateMethod(this, _initializeConnection, initializeConnection_fn).call(this, instanceId);
|
|
870
|
+
__privateMethod(this, _initializeRPC, initializeRPC_fn).call(this);
|
|
871
|
+
__privateMethod(this, _initializeHost, initializeHost_fn).call(this);
|
|
872
|
+
}
|
|
873
|
+
close() {
|
|
874
|
+
__privateSet(this, _closedByUser, true);
|
|
875
|
+
if (__privateGet(this, _serverRPC)) {
|
|
876
|
+
__privateSet(this, _serverRPC, void 0);
|
|
877
|
+
}
|
|
878
|
+
__privateGet(this, _connection2)?.close();
|
|
879
|
+
__privateSet(this, _isConnected, false);
|
|
880
|
+
}
|
|
881
|
+
};
|
|
882
|
+
__name(TriggerClient, "TriggerClient");
|
|
883
|
+
_trigger = new WeakMap();
|
|
884
|
+
_options = new WeakMap();
|
|
885
|
+
_connection2 = new WeakMap();
|
|
886
|
+
_serverRPC = new WeakMap();
|
|
887
|
+
_apiKey = new WeakMap();
|
|
888
|
+
_endpoint = new WeakMap();
|
|
889
|
+
_isConnected = new WeakMap();
|
|
890
|
+
_retryIntervalMs = new WeakMap();
|
|
891
|
+
_logger2 = new WeakMap();
|
|
892
|
+
_closedByUser = new WeakMap();
|
|
893
|
+
_responseCompleteCallbacks = new WeakMap();
|
|
894
|
+
_waitForCallbacks = new WeakMap();
|
|
895
|
+
_initializeConnection = new WeakSet();
|
|
896
|
+
initializeConnection_fn = /* @__PURE__ */ __name(async function(instanceId) {
|
|
897
|
+
const id = instanceId ?? (0, import_uuid2.v4)();
|
|
898
|
+
__privateGet(this, _logger2).debug("Initializing connection", {
|
|
899
|
+
id,
|
|
900
|
+
endpoint: __privateGet(this, _endpoint)
|
|
901
|
+
});
|
|
902
|
+
const headers = {
|
|
903
|
+
Authorization: `Bearer ${__privateGet(this, _apiKey)}`
|
|
904
|
+
};
|
|
905
|
+
const connection = new HostConnection(new import_ws.WebSocket(__privateGet(this, _endpoint), {
|
|
906
|
+
headers,
|
|
907
|
+
followRedirects: true
|
|
908
|
+
}), {
|
|
909
|
+
id
|
|
910
|
+
});
|
|
911
|
+
connection.onClose.attach(async ([code, reason]) => {
|
|
912
|
+
if (__privateGet(this, _closedByUser)) {
|
|
913
|
+
__privateGet(this, _logger2).debug("Connection closed by user, so we won't reconnect");
|
|
914
|
+
__privateSet(this, _closedByUser, false);
|
|
915
|
+
return;
|
|
916
|
+
}
|
|
917
|
+
__privateGet(this, _logger2).error(`\u{1F6A9} Could not connect to trigger.dev (code ${code})`);
|
|
918
|
+
if (reason) {
|
|
919
|
+
__privateGet(this, _logger2).error("Reason:", reason);
|
|
920
|
+
}
|
|
921
|
+
if (!__privateGet(this, _isConnected))
|
|
922
|
+
return;
|
|
923
|
+
__privateGet(this, _logger2).log("\u{1F50C} Reconnecting to trigger.dev...");
|
|
924
|
+
__privateSet(this, _isConnected, false);
|
|
925
|
+
while (!__privateGet(this, _isConnected)) {
|
|
926
|
+
__privateMethod(this, _initializeConnection, initializeConnection_fn).call(this, id).then(() => {
|
|
927
|
+
__privateGet(this, _logger2).log("\u26A1 Reconnection successful");
|
|
928
|
+
}).catch(() => {
|
|
929
|
+
});
|
|
930
|
+
__privateGet(this, _logger2).debug(`Reconnection failed, retrying in ${Math.round(__privateGet(this, _retryIntervalMs) / 1e3)} seconds`, id);
|
|
931
|
+
await new Promise((resolve) => setTimeout(resolve, __privateGet(this, _retryIntervalMs)));
|
|
932
|
+
}
|
|
933
|
+
});
|
|
934
|
+
await connection.connect();
|
|
935
|
+
__privateGet(this, _logger2).debug("Connection initialized", id);
|
|
936
|
+
__privateSet(this, _connection2, connection);
|
|
937
|
+
__privateSet(this, _isConnected, true);
|
|
938
|
+
if (__privateGet(this, _serverRPC)) {
|
|
939
|
+
__privateGet(this, _serverRPC).resetConnection(connection);
|
|
940
|
+
await __privateMethod(this, _initializeHost, initializeHost_fn).call(this);
|
|
941
|
+
}
|
|
942
|
+
}, "#initializeConnection");
|
|
943
|
+
_initializeRPC = new WeakSet();
|
|
944
|
+
initializeRPC_fn = /* @__PURE__ */ __name(async function() {
|
|
945
|
+
if (!__privateGet(this, _connection2)) {
|
|
946
|
+
throw new Error("Cannot initialize RPC without a connection");
|
|
947
|
+
}
|
|
948
|
+
const serverRPC = new ZodRPC({
|
|
949
|
+
connection: __privateGet(this, _connection2),
|
|
950
|
+
sender: ServerRPCSchema,
|
|
951
|
+
receiver: HostRPCSchema,
|
|
952
|
+
handlers: {
|
|
953
|
+
RESOLVE_DELAY: async (data) => {
|
|
954
|
+
__privateGet(this, _logger2).debug("Handling RESOLVE_DELAY", data);
|
|
955
|
+
const waitCallbacks = __privateGet(this, _waitForCallbacks).get(messageKey(data.meta.runId, data.key));
|
|
956
|
+
if (!waitCallbacks) {
|
|
957
|
+
__privateGet(this, _logger2).debug(`Could not find wait callbacks for wait ID ${messageKey(data.meta.runId, data.key)}. This can happen when a workflow run is resumed`);
|
|
958
|
+
return true;
|
|
959
|
+
}
|
|
960
|
+
const { resolve } = waitCallbacks;
|
|
961
|
+
resolve();
|
|
962
|
+
return true;
|
|
963
|
+
},
|
|
964
|
+
RESOLVE_REQUEST: async (data) => {
|
|
965
|
+
__privateGet(this, _logger2).debug("Handling RESOLVE_REQUEST", data);
|
|
966
|
+
const requestCallbacks = __privateGet(this, _responseCompleteCallbacks).get(messageKey(data.meta.runId, data.key));
|
|
967
|
+
if (!requestCallbacks) {
|
|
968
|
+
__privateGet(this, _logger2).debug(`Could not find request callbacks for request ID ${messageKey(data.meta.runId, data.key)}. This can happen when a workflow run is resumed`);
|
|
969
|
+
return true;
|
|
970
|
+
}
|
|
971
|
+
const { resolve } = requestCallbacks;
|
|
972
|
+
resolve(data.output);
|
|
973
|
+
return true;
|
|
974
|
+
},
|
|
975
|
+
REJECT_REQUEST: async (data) => {
|
|
976
|
+
__privateGet(this, _logger2).debug("Handling REJECT_REQUEST", data);
|
|
977
|
+
const requestCallbacks = __privateGet(this, _responseCompleteCallbacks).get(messageKey(data.meta.runId, data.key));
|
|
978
|
+
if (!requestCallbacks) {
|
|
979
|
+
__privateGet(this, _logger2).debug(`Could not find request callbacks for request ID ${messageKey(data.meta.runId, data.key)}. This can happen when a workflow run is resumed`);
|
|
980
|
+
return true;
|
|
981
|
+
}
|
|
982
|
+
const { reject } = requestCallbacks;
|
|
983
|
+
reject(data.error);
|
|
984
|
+
return true;
|
|
985
|
+
},
|
|
986
|
+
TRIGGER_WORKFLOW: async (data) => {
|
|
987
|
+
__privateGet(this, _logger2).debug("Handling TRIGGER_WORKFLOW", data);
|
|
988
|
+
const ctx = {
|
|
989
|
+
id: data.id,
|
|
990
|
+
environment: data.meta.environment,
|
|
991
|
+
apiKey: data.meta.apiKey,
|
|
992
|
+
organizationId: data.meta.organizationId,
|
|
993
|
+
logger: new ContextLogger(async (level, message, properties) => {
|
|
994
|
+
await serverRPC.send("SEND_LOG", {
|
|
995
|
+
runId: data.id,
|
|
996
|
+
key: message,
|
|
997
|
+
log: {
|
|
998
|
+
level,
|
|
999
|
+
message,
|
|
1000
|
+
properties: JSON.stringify(properties ?? {})
|
|
1001
|
+
},
|
|
1002
|
+
timestamp: String(highPrecisionTimestamp())
|
|
1003
|
+
});
|
|
1004
|
+
}),
|
|
1005
|
+
fireEvent: async (key, event) => {
|
|
1006
|
+
await serverRPC.send("SEND_EVENT", {
|
|
1007
|
+
runId: data.id,
|
|
1008
|
+
key,
|
|
1009
|
+
event: JSON.parse(JSON.stringify(event)),
|
|
1010
|
+
timestamp: String(highPrecisionTimestamp())
|
|
1011
|
+
});
|
|
1012
|
+
},
|
|
1013
|
+
waitFor: async (key, options) => {
|
|
1014
|
+
const result = new Promise((resolve, reject) => {
|
|
1015
|
+
__privateGet(this, _waitForCallbacks).set(messageKey(data.id, key), {
|
|
1016
|
+
resolve,
|
|
1017
|
+
reject
|
|
1018
|
+
});
|
|
1019
|
+
});
|
|
1020
|
+
await serverRPC.send("INITIALIZE_DELAY", {
|
|
1021
|
+
runId: data.id,
|
|
1022
|
+
key,
|
|
1023
|
+
wait: {
|
|
1024
|
+
type: "DELAY",
|
|
1025
|
+
seconds: options.seconds,
|
|
1026
|
+
minutes: options.minutes,
|
|
1027
|
+
hours: options.hours,
|
|
1028
|
+
days: options.days
|
|
1029
|
+
},
|
|
1030
|
+
timestamp: String(highPrecisionTimestamp())
|
|
1031
|
+
});
|
|
1032
|
+
await result;
|
|
1033
|
+
return;
|
|
1034
|
+
},
|
|
1035
|
+
waitUntil: async (key, date) => {
|
|
1036
|
+
const result = new Promise((resolve, reject) => {
|
|
1037
|
+
__privateGet(this, _waitForCallbacks).set(messageKey(data.id, key), {
|
|
1038
|
+
resolve,
|
|
1039
|
+
reject
|
|
1040
|
+
});
|
|
1041
|
+
});
|
|
1042
|
+
await serverRPC.send("INITIALIZE_DELAY", {
|
|
1043
|
+
runId: data.id,
|
|
1044
|
+
key,
|
|
1045
|
+
wait: {
|
|
1046
|
+
type: "SCHEDULE_FOR",
|
|
1047
|
+
scheduledFor: date.toISOString()
|
|
1048
|
+
},
|
|
1049
|
+
timestamp: String(highPrecisionTimestamp())
|
|
1050
|
+
});
|
|
1051
|
+
await result;
|
|
1052
|
+
return;
|
|
1053
|
+
}
|
|
1054
|
+
};
|
|
1055
|
+
const eventData = __privateGet(this, _options).on.schema.parse(data.trigger.input);
|
|
1056
|
+
__privateGet(this, _logger2).debug("Parsed event data", eventData);
|
|
1057
|
+
triggerRunLocalStorage.run({
|
|
1058
|
+
performRequest: async (key, options) => {
|
|
1059
|
+
const result = new Promise((resolve, reject) => {
|
|
1060
|
+
__privateGet(this, _responseCompleteCallbacks).set(messageKey(data.id, key), {
|
|
1061
|
+
resolve,
|
|
1062
|
+
reject
|
|
1063
|
+
});
|
|
1064
|
+
});
|
|
1065
|
+
await serverRPC.send("SEND_REQUEST", {
|
|
1066
|
+
runId: data.id,
|
|
1067
|
+
key,
|
|
1068
|
+
request: {
|
|
1069
|
+
service: options.service,
|
|
1070
|
+
endpoint: options.endpoint,
|
|
1071
|
+
params: options.params
|
|
1072
|
+
},
|
|
1073
|
+
timestamp: String(highPrecisionTimestamp())
|
|
1074
|
+
});
|
|
1075
|
+
const output = await result;
|
|
1076
|
+
return options.response.schema.parse(output);
|
|
1077
|
+
}
|
|
1078
|
+
}, () => {
|
|
1079
|
+
__privateGet(this, _logger2).debug("Running trigger...");
|
|
1080
|
+
serverRPC.send("START_WORKFLOW_RUN", {
|
|
1081
|
+
runId: data.id,
|
|
1082
|
+
timestamp: String(highPrecisionTimestamp())
|
|
1083
|
+
}).then(() => {
|
|
1084
|
+
return __privateGet(this, _trigger).options.run(eventData, ctx).then((output) => {
|
|
1085
|
+
return serverRPC.send("COMPLETE_WORKFLOW_RUN", {
|
|
1086
|
+
runId: data.id,
|
|
1087
|
+
output: JSON.stringify(output),
|
|
1088
|
+
timestamp: String(highPrecisionTimestamp())
|
|
1089
|
+
});
|
|
1090
|
+
}).catch((anyError) => {
|
|
1091
|
+
const parseAnyError = /* @__PURE__ */ __name((error2) => {
|
|
1092
|
+
if (error2 instanceof Error) {
|
|
1093
|
+
return {
|
|
1094
|
+
name: error2.name,
|
|
1095
|
+
message: error2.message,
|
|
1096
|
+
stackTrace: error2.stack
|
|
1097
|
+
};
|
|
1098
|
+
}
|
|
1099
|
+
console.error(anyError);
|
|
1100
|
+
return {
|
|
1101
|
+
name: "UnknownError",
|
|
1102
|
+
message: "An unknown error occurred"
|
|
1103
|
+
};
|
|
1104
|
+
}, "parseAnyError");
|
|
1105
|
+
const error = parseAnyError(anyError);
|
|
1106
|
+
return serverRPC.send("SEND_WORKFLOW_ERROR", {
|
|
1107
|
+
runId: data.id,
|
|
1108
|
+
error,
|
|
1109
|
+
timestamp: String(highPrecisionTimestamp())
|
|
1110
|
+
});
|
|
1111
|
+
});
|
|
1112
|
+
}).catch((anyError) => {
|
|
1113
|
+
return serverRPC.send("SEND_WORKFLOW_ERROR", {
|
|
1114
|
+
runId: data.id,
|
|
1115
|
+
error: anyError,
|
|
1116
|
+
timestamp: String(highPrecisionTimestamp())
|
|
1117
|
+
});
|
|
1118
|
+
});
|
|
1119
|
+
});
|
|
1120
|
+
return true;
|
|
1121
|
+
}
|
|
1122
|
+
}
|
|
1123
|
+
});
|
|
1124
|
+
__privateGet(this, _logger2).debug("Successfully initialized RPC with server");
|
|
1125
|
+
__privateSet(this, _serverRPC, serverRPC);
|
|
1126
|
+
}, "#initializeRPC");
|
|
1127
|
+
_initializeHost = new WeakSet();
|
|
1128
|
+
initializeHost_fn = /* @__PURE__ */ __name(async function() {
|
|
1129
|
+
if (!__privateGet(this, _connection2)) {
|
|
1130
|
+
throw new Error("Cannot initialize host without a connection");
|
|
1131
|
+
}
|
|
1132
|
+
if (!__privateGet(this, _serverRPC)) {
|
|
1133
|
+
throw new Error("Cannot initialize host without an RPC connection");
|
|
1134
|
+
}
|
|
1135
|
+
const response = await __privateMethod(this, _send, send_fn).call(this, "INITIALIZE_HOST", {
|
|
1136
|
+
apiKey: __privateGet(this, _apiKey),
|
|
1137
|
+
workflowId: __privateGet(this, _trigger).id,
|
|
1138
|
+
workflowName: __privateGet(this, _trigger).name,
|
|
1139
|
+
trigger: __privateGet(this, _trigger).on.metadata,
|
|
1140
|
+
packageVersion: version,
|
|
1141
|
+
packageName: name
|
|
1142
|
+
});
|
|
1143
|
+
if (response?.type === "error") {
|
|
1144
|
+
throw new Error(response.message);
|
|
1145
|
+
}
|
|
1146
|
+
__privateGet(this, _logger2).debug("Successfully initialized workflow with server");
|
|
1147
|
+
}, "#initializeHost");
|
|
1148
|
+
_send = new WeakSet();
|
|
1149
|
+
send_fn = /* @__PURE__ */ __name(async function(methodName, request) {
|
|
1150
|
+
if (!__privateGet(this, _serverRPC))
|
|
1151
|
+
throw new Error("serverRPC not initialized");
|
|
1152
|
+
while (true) {
|
|
1153
|
+
try {
|
|
1154
|
+
__privateGet(this, _logger2).debug(`Sending RPC request to server: ${methodName}`, request);
|
|
1155
|
+
return await __privateGet(this, _serverRPC).send(methodName, request);
|
|
1156
|
+
} catch (err) {
|
|
1157
|
+
if (err instanceof TimeoutError) {
|
|
1158
|
+
__privateGet(this, _logger2).log(`RPC call timed out, retrying in ${Math.round(__privateGet(this, _retryIntervalMs) / 1e3)}s...`);
|
|
1159
|
+
__privateGet(this, _logger2).error(err);
|
|
1160
|
+
await sleep(__privateGet(this, _retryIntervalMs));
|
|
1161
|
+
} else {
|
|
1162
|
+
throw err;
|
|
1163
|
+
}
|
|
1164
|
+
}
|
|
1165
|
+
}
|
|
1166
|
+
}, "#send");
|
|
1167
|
+
var sleep = /* @__PURE__ */ __name((ms) => new Promise((resolve) => setTimeout(resolve, ms)), "sleep");
|
|
1168
|
+
var messageKey = /* @__PURE__ */ __name((runId, key) => `${runId}:${key}`, "messageKey");
|
|
1169
|
+
function highPrecisionTimestamp() {
|
|
1170
|
+
const [seconds, nanoseconds] = process.hrtime();
|
|
1171
|
+
return seconds * 1e9 + nanoseconds;
|
|
1172
|
+
}
|
|
1173
|
+
__name(highPrecisionTimestamp, "highPrecisionTimestamp");
|
|
1174
|
+
|
|
1175
|
+
// src/trigger/index.ts
|
|
1176
|
+
var _client;
|
|
1177
|
+
var Trigger = class {
|
|
1178
|
+
constructor(options) {
|
|
1179
|
+
__privateAdd(this, _client, void 0);
|
|
1180
|
+
this.options = options;
|
|
1181
|
+
}
|
|
1182
|
+
async listen() {
|
|
1183
|
+
if (!__privateGet(this, _client)) {
|
|
1184
|
+
__privateSet(this, _client, new TriggerClient(this, this.options));
|
|
1185
|
+
}
|
|
1186
|
+
return __privateGet(this, _client).listen();
|
|
1187
|
+
}
|
|
1188
|
+
get id() {
|
|
1189
|
+
return this.options.id;
|
|
1190
|
+
}
|
|
1191
|
+
get name() {
|
|
1192
|
+
return this.options.name;
|
|
1193
|
+
}
|
|
1194
|
+
get endpoint() {
|
|
1195
|
+
return this.options.endpoint;
|
|
1196
|
+
}
|
|
1197
|
+
get on() {
|
|
1198
|
+
return this.options.on;
|
|
1199
|
+
}
|
|
1200
|
+
};
|
|
1201
|
+
__name(Trigger, "Trigger");
|
|
1202
|
+
_client = new WeakMap();
|
|
1203
|
+
|
|
1204
|
+
// src/events.ts
|
|
1205
|
+
function customEvent(options) {
|
|
1206
|
+
return {
|
|
1207
|
+
metadata: {
|
|
1208
|
+
type: "CUSTOM_EVENT",
|
|
1209
|
+
service: "trigger",
|
|
1210
|
+
name: options.name,
|
|
1211
|
+
filter: {
|
|
1212
|
+
event: [
|
|
1213
|
+
options.name
|
|
1214
|
+
]
|
|
1215
|
+
}
|
|
1216
|
+
},
|
|
1217
|
+
schema: options.schema
|
|
1218
|
+
};
|
|
1219
|
+
}
|
|
1220
|
+
__name(customEvent, "customEvent");
|
|
1221
|
+
|
|
1222
|
+
// src/index.ts
|
|
1223
|
+
function getTriggerRun() {
|
|
1224
|
+
return triggerRunLocalStorage.getStore();
|
|
1225
|
+
}
|
|
1226
|
+
__name(getTriggerRun, "getTriggerRun");
|
|
1227
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
1228
|
+
0 && (module.exports = {
|
|
1229
|
+
Trigger,
|
|
1230
|
+
customEvent,
|
|
1231
|
+
getTriggerRun
|
|
1232
|
+
});
|
|
1233
|
+
//# sourceMappingURL=index.js.map
|