@dxos/functions 0.6.10 → 0.6.11-staging.30cf5ba
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/lib/browser/chunk-IBJYIBKT.mjs +581 -0
- package/dist/lib/browser/chunk-IBJYIBKT.mjs.map +7 -0
- package/dist/lib/browser/{chunk-YSDC6YCF.mjs → chunk-XOBJR3A6.mjs} +2 -1
- package/dist/lib/browser/{chunk-YSDC6YCF.mjs.map → chunk-XOBJR3A6.mjs.map} +3 -3
- package/dist/lib/browser/index.mjs +9 -7
- package/dist/lib/browser/index.mjs.map +1 -1
- package/dist/lib/browser/meta.json +1 -1
- package/dist/lib/browser/testing/index.mjs +522 -11
- package/dist/lib/browser/testing/index.mjs.map +4 -4
- package/dist/lib/browser/types.mjs +1 -1
- package/dist/lib/node/{chunk-3E6PY6JH.cjs → chunk-GGTHSME4.cjs} +5 -4
- package/dist/lib/node/{chunk-3E6PY6JH.cjs.map → chunk-GGTHSME4.cjs.map} +3 -3
- package/dist/lib/node/chunk-V7JNSENS.cjs +604 -0
- package/dist/lib/node/chunk-V7JNSENS.cjs.map +7 -0
- package/dist/lib/node/index.cjs +14 -12
- package/dist/lib/node/index.cjs.map +1 -1
- package/dist/lib/node/meta.json +1 -1
- package/dist/lib/node/testing/index.cjs +521 -13
- package/dist/lib/node/testing/index.cjs.map +4 -4
- package/dist/lib/node/types.cjs +5 -5
- package/dist/lib/node/types.cjs.map +1 -1
- package/dist/types/src/index.d.ts +0 -1
- package/dist/types/src/index.d.ts.map +1 -1
- package/dist/types/src/runtime/scheduler.d.ts.map +1 -1
- package/dist/types/src/trigger/index.d.ts +1 -0
- package/dist/types/src/trigger/index.d.ts.map +1 -1
- package/dist/types/src/trigger/trigger-registry.d.ts +5 -4
- package/dist/types/src/trigger/trigger-registry.d.ts.map +1 -1
- package/dist/types/src/trigger/type/index.d.ts +0 -1
- package/dist/types/src/trigger/type/index.d.ts.map +1 -1
- package/dist/types/src/types.d.ts +4 -0
- package/dist/types/src/types.d.ts.map +1 -1
- package/package.json +14 -14
- package/src/index.ts +1 -1
- package/src/runtime/scheduler.ts +6 -3
- package/src/trigger/index.ts +1 -0
- package/src/trigger/trigger-registry.ts +14 -7
- package/src/trigger/type/index.ts +1 -1
- package/src/types.ts +1 -0
- package/dist/lib/browser/chunk-OERXFETS.mjs +0 -1120
- package/dist/lib/browser/chunk-OERXFETS.mjs.map +0 -7
- package/dist/lib/node/chunk-ITQU6E54.cjs +0 -1133
- package/dist/lib/node/chunk-ITQU6E54.cjs.map +0 -7
|
@@ -37,17 +37,30 @@ __export(testing_exports, {
|
|
|
37
37
|
triggerWebhook: () => triggerWebhook
|
|
38
38
|
});
|
|
39
39
|
module.exports = __toCommonJS(testing_exports);
|
|
40
|
-
var
|
|
41
|
-
var
|
|
40
|
+
var import_chunk_V7JNSENS = require("../chunk-V7JNSENS.cjs");
|
|
41
|
+
var import_chunk_GGTHSME4 = require("../chunk-GGTHSME4.cjs");
|
|
42
42
|
var import_get_port_please = require("get-port-please");
|
|
43
43
|
var import_node_path = __toESM(require("node:path"));
|
|
44
44
|
var import_async = require("@dxos/async");
|
|
45
45
|
var import_client = require("@dxos/client");
|
|
46
46
|
var import_util = require("@dxos/util");
|
|
47
47
|
var import_echo_schema = require("@dxos/echo-schema");
|
|
48
|
+
var import_express = __toESM(require("express"));
|
|
49
|
+
var import_get_port_please2 = require("get-port-please");
|
|
50
|
+
var import_node_path2 = require("node:path");
|
|
51
|
+
var import_async2 = require("@dxos/async");
|
|
52
|
+
var import_context = require("@dxos/context");
|
|
53
|
+
var import_invariant = require("@dxos/invariant");
|
|
54
|
+
var import_log = require("@dxos/log");
|
|
55
|
+
var import_node_path3 = __toESM(require("node:path"));
|
|
56
|
+
var import_async3 = require("@dxos/async");
|
|
48
57
|
var import_echo = require("@dxos/client/echo");
|
|
58
|
+
var import_context2 = require("@dxos/context");
|
|
59
|
+
var import_echo_protocol = require("@dxos/echo-protocol");
|
|
60
|
+
var import_log2 = require("@dxos/log");
|
|
61
|
+
var import_echo2 = require("@dxos/client/echo");
|
|
49
62
|
var import_testing = require("@dxos/client/testing");
|
|
50
|
-
var
|
|
63
|
+
var import_invariant2 = require("@dxos/invariant");
|
|
51
64
|
var import_services = require("@dxos/protocols/proto/dxos/client/services");
|
|
52
65
|
var TestType = class extends (0, import_echo_schema.TypedObject)({
|
|
53
66
|
typename: "example.com/type/Test",
|
|
@@ -56,6 +69,501 @@ var TestType = class extends (0, import_echo_schema.TypedObject)({
|
|
|
56
69
|
title: import_echo_schema.S.String
|
|
57
70
|
}) {
|
|
58
71
|
};
|
|
72
|
+
var __dxlog_file = "/home/runner/work/dxos/dxos/packages/core/functions/src/runtime/dev-server.ts";
|
|
73
|
+
var FN_TIMEOUT = 2e4;
|
|
74
|
+
var DevServer = class {
|
|
75
|
+
constructor(_client, _functionsRegistry, _options) {
|
|
76
|
+
this._client = _client;
|
|
77
|
+
this._functionsRegistry = _functionsRegistry;
|
|
78
|
+
this._options = _options;
|
|
79
|
+
this._ctx = createContext();
|
|
80
|
+
this._handlers = {};
|
|
81
|
+
this._seq = 0;
|
|
82
|
+
this.update = new import_async2.Event();
|
|
83
|
+
}
|
|
84
|
+
get stats() {
|
|
85
|
+
return {
|
|
86
|
+
seq: this._seq
|
|
87
|
+
};
|
|
88
|
+
}
|
|
89
|
+
get endpoint() {
|
|
90
|
+
(0, import_invariant.invariant)(this._port, void 0, {
|
|
91
|
+
F: __dxlog_file,
|
|
92
|
+
L: 60,
|
|
93
|
+
S: this,
|
|
94
|
+
A: [
|
|
95
|
+
"this._port",
|
|
96
|
+
""
|
|
97
|
+
]
|
|
98
|
+
});
|
|
99
|
+
return `http://localhost:${this._port}`;
|
|
100
|
+
}
|
|
101
|
+
get proxy() {
|
|
102
|
+
return this._proxy;
|
|
103
|
+
}
|
|
104
|
+
get functions() {
|
|
105
|
+
return Object.values(this._handlers);
|
|
106
|
+
}
|
|
107
|
+
async start() {
|
|
108
|
+
(0, import_invariant.invariant)(!this._server, void 0, {
|
|
109
|
+
F: __dxlog_file,
|
|
110
|
+
L: 73,
|
|
111
|
+
S: this,
|
|
112
|
+
A: [
|
|
113
|
+
"!this._server",
|
|
114
|
+
""
|
|
115
|
+
]
|
|
116
|
+
});
|
|
117
|
+
import_log.log.info("starting...", void 0, {
|
|
118
|
+
F: __dxlog_file,
|
|
119
|
+
L: 74,
|
|
120
|
+
S: this,
|
|
121
|
+
C: (f, a) => f(...a)
|
|
122
|
+
});
|
|
123
|
+
this._ctx = createContext();
|
|
124
|
+
const app = (0, import_express.default)();
|
|
125
|
+
app.use(import_express.default.json());
|
|
126
|
+
app.post("/:path", async (req, res) => {
|
|
127
|
+
const { path: path3 } = req.params;
|
|
128
|
+
try {
|
|
129
|
+
import_log.log.info("calling", {
|
|
130
|
+
path: path3
|
|
131
|
+
}, {
|
|
132
|
+
F: __dxlog_file,
|
|
133
|
+
L: 84,
|
|
134
|
+
S: this,
|
|
135
|
+
C: (f, a) => f(...a)
|
|
136
|
+
});
|
|
137
|
+
if (this._options.reload) {
|
|
138
|
+
const { def } = this._handlers["/" + path3];
|
|
139
|
+
await this._load(def, true);
|
|
140
|
+
}
|
|
141
|
+
res.statusCode = await (0, import_async2.asyncTimeout)(this.invoke("/" + path3, req.body), FN_TIMEOUT);
|
|
142
|
+
res.end();
|
|
143
|
+
} catch (err) {
|
|
144
|
+
import_log.log.catch(err, void 0, {
|
|
145
|
+
F: __dxlog_file,
|
|
146
|
+
L: 94,
|
|
147
|
+
S: this,
|
|
148
|
+
C: (f, a) => f(...a)
|
|
149
|
+
});
|
|
150
|
+
res.statusCode = 500;
|
|
151
|
+
res.end();
|
|
152
|
+
}
|
|
153
|
+
});
|
|
154
|
+
this._port = this._options.port ?? await (0, import_get_port_please2.getPort)({
|
|
155
|
+
host: "localhost",
|
|
156
|
+
port: 7200,
|
|
157
|
+
portRange: [
|
|
158
|
+
7200,
|
|
159
|
+
7299
|
|
160
|
+
]
|
|
161
|
+
});
|
|
162
|
+
this._server = app.listen(this._port);
|
|
163
|
+
try {
|
|
164
|
+
const { registrationId, endpoint } = await this._client.services.services.FunctionRegistryService.register({
|
|
165
|
+
endpoint: this.endpoint
|
|
166
|
+
});
|
|
167
|
+
import_log.log.info("registered", {
|
|
168
|
+
endpoint
|
|
169
|
+
}, {
|
|
170
|
+
F: __dxlog_file,
|
|
171
|
+
L: 109,
|
|
172
|
+
S: this,
|
|
173
|
+
C: (f, a) => f(...a)
|
|
174
|
+
});
|
|
175
|
+
this._proxy = endpoint;
|
|
176
|
+
this._functionServiceRegistration = registrationId;
|
|
177
|
+
await this._handleNewFunctions(this._functionsRegistry.getUniqueByUri());
|
|
178
|
+
this._ctx.onDispose(this._functionsRegistry.registered.on(({ added }) => this._handleNewFunctions(added)));
|
|
179
|
+
} catch (err) {
|
|
180
|
+
await this.stop();
|
|
181
|
+
throw new Error("FunctionRegistryService not available (check plugin is configured).");
|
|
182
|
+
}
|
|
183
|
+
import_log.log.info("started", {
|
|
184
|
+
port: this._port
|
|
185
|
+
}, {
|
|
186
|
+
F: __dxlog_file,
|
|
187
|
+
L: 121,
|
|
188
|
+
S: this,
|
|
189
|
+
C: (f, a) => f(...a)
|
|
190
|
+
});
|
|
191
|
+
}
|
|
192
|
+
async stop() {
|
|
193
|
+
if (!this._server) {
|
|
194
|
+
return;
|
|
195
|
+
}
|
|
196
|
+
import_log.log.info("stopping...", void 0, {
|
|
197
|
+
F: __dxlog_file,
|
|
198
|
+
L: 129,
|
|
199
|
+
S: this,
|
|
200
|
+
C: (f, a) => f(...a)
|
|
201
|
+
});
|
|
202
|
+
await this._ctx.dispose();
|
|
203
|
+
const trigger = new import_async2.Trigger();
|
|
204
|
+
this._server.close(async () => {
|
|
205
|
+
import_log.log.info("server stopped", void 0, {
|
|
206
|
+
F: __dxlog_file,
|
|
207
|
+
L: 134,
|
|
208
|
+
S: this,
|
|
209
|
+
C: (f, a) => f(...a)
|
|
210
|
+
});
|
|
211
|
+
try {
|
|
212
|
+
if (this._functionServiceRegistration) {
|
|
213
|
+
(0, import_invariant.invariant)(this._client.services.services.FunctionRegistryService, void 0, {
|
|
214
|
+
F: __dxlog_file,
|
|
215
|
+
L: 137,
|
|
216
|
+
S: this,
|
|
217
|
+
A: [
|
|
218
|
+
"this._client.services.services.FunctionRegistryService",
|
|
219
|
+
""
|
|
220
|
+
]
|
|
221
|
+
});
|
|
222
|
+
await this._client.services.services.FunctionRegistryService.unregister({
|
|
223
|
+
registrationId: this._functionServiceRegistration
|
|
224
|
+
});
|
|
225
|
+
import_log.log.info("unregistered", {
|
|
226
|
+
registrationId: this._functionServiceRegistration
|
|
227
|
+
}, {
|
|
228
|
+
F: __dxlog_file,
|
|
229
|
+
L: 142,
|
|
230
|
+
S: this,
|
|
231
|
+
C: (f, a) => f(...a)
|
|
232
|
+
});
|
|
233
|
+
this._functionServiceRegistration = void 0;
|
|
234
|
+
this._proxy = void 0;
|
|
235
|
+
}
|
|
236
|
+
trigger.wake();
|
|
237
|
+
} catch (err) {
|
|
238
|
+
trigger.throw(err);
|
|
239
|
+
}
|
|
240
|
+
});
|
|
241
|
+
await trigger.wait();
|
|
242
|
+
this._port = void 0;
|
|
243
|
+
this._server = void 0;
|
|
244
|
+
import_log.log.info("stopped", void 0, {
|
|
245
|
+
F: __dxlog_file,
|
|
246
|
+
L: 156,
|
|
247
|
+
S: this,
|
|
248
|
+
C: (f, a) => f(...a)
|
|
249
|
+
});
|
|
250
|
+
}
|
|
251
|
+
async _handleNewFunctions(newFunctions) {
|
|
252
|
+
newFunctions.forEach((def) => this._load(def));
|
|
253
|
+
await this._safeUpdateRegistration();
|
|
254
|
+
(0, import_log.log)("new functions loaded", {
|
|
255
|
+
newFunctions
|
|
256
|
+
}, {
|
|
257
|
+
F: __dxlog_file,
|
|
258
|
+
L: 162,
|
|
259
|
+
S: this,
|
|
260
|
+
C: (f, a) => f(...a)
|
|
261
|
+
});
|
|
262
|
+
}
|
|
263
|
+
/**
|
|
264
|
+
* Load function.
|
|
265
|
+
*/
|
|
266
|
+
async _load(def, force) {
|
|
267
|
+
const { uri, route, handler } = def;
|
|
268
|
+
const filePath = (0, import_node_path2.join)(this._options.baseDir, handler);
|
|
269
|
+
import_log.log.info("loading", {
|
|
270
|
+
uri,
|
|
271
|
+
force
|
|
272
|
+
}, {
|
|
273
|
+
F: __dxlog_file,
|
|
274
|
+
L: 171,
|
|
275
|
+
S: this,
|
|
276
|
+
C: (f, a) => f(...a)
|
|
277
|
+
});
|
|
278
|
+
if (force) {
|
|
279
|
+
Object.keys(import_chunk_GGTHSME4.__require.cache).filter((key) => key.startsWith(filePath)).forEach((key) => {
|
|
280
|
+
delete import_chunk_GGTHSME4.__require.cache[key];
|
|
281
|
+
});
|
|
282
|
+
}
|
|
283
|
+
const module2 = (0, import_chunk_GGTHSME4.__require)(filePath);
|
|
284
|
+
if (typeof module2.default !== "function") {
|
|
285
|
+
throw new Error(`Handler must export default function: ${uri}`);
|
|
286
|
+
}
|
|
287
|
+
this._handlers[route] = {
|
|
288
|
+
def,
|
|
289
|
+
handler: module2.default
|
|
290
|
+
};
|
|
291
|
+
}
|
|
292
|
+
async _safeUpdateRegistration() {
|
|
293
|
+
(0, import_invariant.invariant)(this._functionServiceRegistration, void 0, {
|
|
294
|
+
F: __dxlog_file,
|
|
295
|
+
L: 193,
|
|
296
|
+
S: this,
|
|
297
|
+
A: [
|
|
298
|
+
"this._functionServiceRegistration",
|
|
299
|
+
""
|
|
300
|
+
]
|
|
301
|
+
});
|
|
302
|
+
try {
|
|
303
|
+
await this._client.services.services.FunctionRegistryService.updateRegistration({
|
|
304
|
+
registrationId: this._functionServiceRegistration,
|
|
305
|
+
functions: this.functions.map(({ def: { id, route } }) => ({
|
|
306
|
+
id,
|
|
307
|
+
route
|
|
308
|
+
}))
|
|
309
|
+
});
|
|
310
|
+
} catch (err) {
|
|
311
|
+
import_log.log.catch(err, void 0, {
|
|
312
|
+
F: __dxlog_file,
|
|
313
|
+
L: 200,
|
|
314
|
+
S: this,
|
|
315
|
+
C: (f, a) => f(...a)
|
|
316
|
+
});
|
|
317
|
+
}
|
|
318
|
+
}
|
|
319
|
+
/**
|
|
320
|
+
* Invoke function.
|
|
321
|
+
*/
|
|
322
|
+
async invoke(path3, data) {
|
|
323
|
+
const seq = ++this._seq;
|
|
324
|
+
const now = Date.now();
|
|
325
|
+
import_log.log.info("req", {
|
|
326
|
+
seq,
|
|
327
|
+
path: path3
|
|
328
|
+
}, {
|
|
329
|
+
F: __dxlog_file,
|
|
330
|
+
L: 211,
|
|
331
|
+
S: this,
|
|
332
|
+
C: (f, a) => f(...a)
|
|
333
|
+
});
|
|
334
|
+
const statusCode = await this._invoke(path3, {
|
|
335
|
+
data
|
|
336
|
+
});
|
|
337
|
+
import_log.log.info("res", {
|
|
338
|
+
seq,
|
|
339
|
+
path: path3,
|
|
340
|
+
statusCode,
|
|
341
|
+
duration: Date.now() - now
|
|
342
|
+
}, {
|
|
343
|
+
F: __dxlog_file,
|
|
344
|
+
L: 214,
|
|
345
|
+
S: this,
|
|
346
|
+
C: (f, a) => f(...a)
|
|
347
|
+
});
|
|
348
|
+
this.update.emit(statusCode);
|
|
349
|
+
return statusCode;
|
|
350
|
+
}
|
|
351
|
+
async _invoke(path3, event) {
|
|
352
|
+
const { handler } = this._handlers[path3] ?? {};
|
|
353
|
+
(0, import_invariant.invariant)(handler, `invalid path: ${path3}`, {
|
|
354
|
+
F: __dxlog_file,
|
|
355
|
+
L: 221,
|
|
356
|
+
S: this,
|
|
357
|
+
A: [
|
|
358
|
+
"handler",
|
|
359
|
+
"`invalid path: ${path}`"
|
|
360
|
+
]
|
|
361
|
+
});
|
|
362
|
+
const context = {
|
|
363
|
+
client: this._client,
|
|
364
|
+
dataDir: this._options.dataDir
|
|
365
|
+
};
|
|
366
|
+
let statusCode = 200;
|
|
367
|
+
const response = {
|
|
368
|
+
status: (code) => {
|
|
369
|
+
statusCode = code;
|
|
370
|
+
return response;
|
|
371
|
+
}
|
|
372
|
+
};
|
|
373
|
+
await handler({
|
|
374
|
+
context,
|
|
375
|
+
event,
|
|
376
|
+
response
|
|
377
|
+
});
|
|
378
|
+
return statusCode;
|
|
379
|
+
}
|
|
380
|
+
};
|
|
381
|
+
var createContext = () => new import_context.Context({
|
|
382
|
+
name: "DevServer"
|
|
383
|
+
}, {
|
|
384
|
+
F: __dxlog_file,
|
|
385
|
+
L: 240
|
|
386
|
+
});
|
|
387
|
+
var __dxlog_file2 = "/home/runner/work/dxos/dxos/packages/core/functions/src/runtime/scheduler.ts";
|
|
388
|
+
var Scheduler = class {
|
|
389
|
+
constructor(functions, triggers, _options = {}) {
|
|
390
|
+
this.functions = functions;
|
|
391
|
+
this.triggers = triggers;
|
|
392
|
+
this._options = _options;
|
|
393
|
+
this._ctx = createContext2();
|
|
394
|
+
this._functionUriToCallMutex = /* @__PURE__ */ new Map();
|
|
395
|
+
this.functions.registered.on(async ({ space, added }) => {
|
|
396
|
+
await this._safeActivateTriggers(space, this.triggers.getInactiveTriggers(space), added);
|
|
397
|
+
});
|
|
398
|
+
this.triggers.registered.on(async ({ space, triggers: triggers2 }) => {
|
|
399
|
+
await this._safeActivateTriggers(space, triggers2, this.functions.getFunctions(space));
|
|
400
|
+
});
|
|
401
|
+
}
|
|
402
|
+
async start() {
|
|
403
|
+
await this._ctx.dispose();
|
|
404
|
+
this._ctx = createContext2();
|
|
405
|
+
await this.functions.open(this._ctx);
|
|
406
|
+
await this.triggers.open(this._ctx);
|
|
407
|
+
}
|
|
408
|
+
async stop() {
|
|
409
|
+
await this._ctx.dispose();
|
|
410
|
+
await this.functions.close();
|
|
411
|
+
await this.triggers.close();
|
|
412
|
+
}
|
|
413
|
+
// TODO(burdon): Remove and update registries directly?
|
|
414
|
+
async register(space, manifest) {
|
|
415
|
+
await this.functions.register(space, manifest.functions);
|
|
416
|
+
await this.triggers.register(space, manifest);
|
|
417
|
+
}
|
|
418
|
+
async _safeActivateTriggers(space, triggers, functions) {
|
|
419
|
+
const mountTasks = triggers.map((trigger) => {
|
|
420
|
+
return this.activate(space, functions, trigger);
|
|
421
|
+
});
|
|
422
|
+
await Promise.all(mountTasks).catch(import_log2.log.catch);
|
|
423
|
+
}
|
|
424
|
+
/**
|
|
425
|
+
* Activate trigger.
|
|
426
|
+
*/
|
|
427
|
+
// TODO(burdon): How are triggers deactivated?
|
|
428
|
+
async activate(space, functions, trigger) {
|
|
429
|
+
const definition = functions.find((def) => def.uri === trigger.function);
|
|
430
|
+
if (!definition) {
|
|
431
|
+
import_log2.log.info("function is not found for trigger", {
|
|
432
|
+
trigger
|
|
433
|
+
}, {
|
|
434
|
+
F: __dxlog_file2,
|
|
435
|
+
L: 85,
|
|
436
|
+
S: this,
|
|
437
|
+
C: (f, a) => f(...a)
|
|
438
|
+
});
|
|
439
|
+
return;
|
|
440
|
+
}
|
|
441
|
+
const execFunction = async (args) => {
|
|
442
|
+
const mutex = this._functionUriToCallMutex.get(definition.uri) ?? new import_async3.Mutex();
|
|
443
|
+
this._functionUriToCallMutex.set(definition.uri, mutex);
|
|
444
|
+
import_log2.log.info("function triggered, waiting for mutex", {
|
|
445
|
+
uri: definition.uri
|
|
446
|
+
}, {
|
|
447
|
+
F: __dxlog_file2,
|
|
448
|
+
L: 93,
|
|
449
|
+
S: this,
|
|
450
|
+
C: (f, a) => f(...a)
|
|
451
|
+
});
|
|
452
|
+
return mutex.executeSynchronized(async () => {
|
|
453
|
+
import_log2.log.info("mutex acquired", {
|
|
454
|
+
uri: definition.uri
|
|
455
|
+
}, {
|
|
456
|
+
F: __dxlog_file2,
|
|
457
|
+
L: 95,
|
|
458
|
+
S: this,
|
|
459
|
+
C: (f, a) => f(...a)
|
|
460
|
+
});
|
|
461
|
+
await (0, import_echo.loadObjectReferences)(trigger, (t) => Object.values(t.meta ?? {}));
|
|
462
|
+
const meta = {};
|
|
463
|
+
for (const [key, value] of Object.entries(trigger.meta ?? {})) {
|
|
464
|
+
if (value instanceof import_echo_protocol.Reference) {
|
|
465
|
+
const object = await space.db.loadObjectById(value.objectId);
|
|
466
|
+
if (object) {
|
|
467
|
+
meta[key] = object;
|
|
468
|
+
}
|
|
469
|
+
} else {
|
|
470
|
+
meta[key] = value;
|
|
471
|
+
}
|
|
472
|
+
}
|
|
473
|
+
return this._execFunction(definition, trigger, {
|
|
474
|
+
meta,
|
|
475
|
+
data: {
|
|
476
|
+
...args,
|
|
477
|
+
spaceKey: space.key
|
|
478
|
+
}
|
|
479
|
+
});
|
|
480
|
+
});
|
|
481
|
+
};
|
|
482
|
+
await this.triggers.activate(space, trigger, execFunction);
|
|
483
|
+
(0, import_log2.log)("activated trigger", {
|
|
484
|
+
space: space.key,
|
|
485
|
+
trigger
|
|
486
|
+
}, {
|
|
487
|
+
F: __dxlog_file2,
|
|
488
|
+
L: 119,
|
|
489
|
+
S: this,
|
|
490
|
+
C: (f, a) => f(...a)
|
|
491
|
+
});
|
|
492
|
+
}
|
|
493
|
+
/**
|
|
494
|
+
* Invoke function RPC.
|
|
495
|
+
*/
|
|
496
|
+
async _execFunction(def, trigger, { meta, data }) {
|
|
497
|
+
let status = 0;
|
|
498
|
+
try {
|
|
499
|
+
const payload = Object.assign({}, meta && {
|
|
500
|
+
meta
|
|
501
|
+
}, data);
|
|
502
|
+
const { endpoint, callback } = this._options;
|
|
503
|
+
if (endpoint) {
|
|
504
|
+
const url = import_node_path3.default.join(endpoint, def.route);
|
|
505
|
+
import_log2.log.info("exec", {
|
|
506
|
+
function: def.uri,
|
|
507
|
+
url,
|
|
508
|
+
triggerType: trigger.spec.type
|
|
509
|
+
}, {
|
|
510
|
+
F: __dxlog_file2,
|
|
511
|
+
L: 139,
|
|
512
|
+
S: this,
|
|
513
|
+
C: (f, a) => f(...a)
|
|
514
|
+
});
|
|
515
|
+
const response = await fetch(url, {
|
|
516
|
+
method: "POST",
|
|
517
|
+
headers: {
|
|
518
|
+
"Content-Type": "application/json"
|
|
519
|
+
},
|
|
520
|
+
body: JSON.stringify(payload)
|
|
521
|
+
});
|
|
522
|
+
status = response.status;
|
|
523
|
+
} else if (callback) {
|
|
524
|
+
import_log2.log.info("exec", {
|
|
525
|
+
function: def.uri
|
|
526
|
+
}, {
|
|
527
|
+
F: __dxlog_file2,
|
|
528
|
+
L: 150,
|
|
529
|
+
S: this,
|
|
530
|
+
C: (f, a) => f(...a)
|
|
531
|
+
});
|
|
532
|
+
status = await callback(payload) ?? 200;
|
|
533
|
+
}
|
|
534
|
+
if (status && status >= 400) {
|
|
535
|
+
throw new Error(`Response: ${status}`);
|
|
536
|
+
}
|
|
537
|
+
import_log2.log.info("done", {
|
|
538
|
+
function: def.uri,
|
|
539
|
+
status
|
|
540
|
+
}, {
|
|
541
|
+
F: __dxlog_file2,
|
|
542
|
+
L: 160,
|
|
543
|
+
S: this,
|
|
544
|
+
C: (f, a) => f(...a)
|
|
545
|
+
});
|
|
546
|
+
} catch (err) {
|
|
547
|
+
import_log2.log.error("error", {
|
|
548
|
+
function: def.uri,
|
|
549
|
+
error: err.message
|
|
550
|
+
}, {
|
|
551
|
+
F: __dxlog_file2,
|
|
552
|
+
L: 162,
|
|
553
|
+
S: this,
|
|
554
|
+
C: (f, a) => f(...a)
|
|
555
|
+
});
|
|
556
|
+
status = 500;
|
|
557
|
+
}
|
|
558
|
+
return status;
|
|
559
|
+
}
|
|
560
|
+
};
|
|
561
|
+
var createContext2 = () => new import_context2.Context({
|
|
562
|
+
name: "FunctionScheduler"
|
|
563
|
+
}, {
|
|
564
|
+
F: __dxlog_file2,
|
|
565
|
+
L: 170
|
|
566
|
+
});
|
|
59
567
|
var createInitializedClients = async (testBuilder, count = 1, config) => {
|
|
60
568
|
const clients = (0, import_util.range)(count).map(() => new import_client.Client({
|
|
61
569
|
config,
|
|
@@ -69,8 +577,8 @@ var createInitializedClients = async (testBuilder, count = 1, config) => {
|
|
|
69
577
|
});
|
|
70
578
|
await client.spaces.isReady.wait();
|
|
71
579
|
client.addTypes([
|
|
72
|
-
|
|
73
|
-
|
|
580
|
+
import_chunk_GGTHSME4.FunctionDef,
|
|
581
|
+
import_chunk_GGTHSME4.FunctionTrigger,
|
|
74
582
|
TestType
|
|
75
583
|
]);
|
|
76
584
|
return client;
|
|
@@ -99,7 +607,7 @@ var createFunctionRuntime = async (testBuilder, pluginInitializer) => {
|
|
|
99
607
|
};
|
|
100
608
|
var startFunctionsHost = async (testBuilder, pluginInitializer, options) => {
|
|
101
609
|
const functionRuntime = await createFunctionRuntime(testBuilder, pluginInitializer);
|
|
102
|
-
const functionsRegistry = new
|
|
610
|
+
const functionsRegistry = new import_chunk_V7JNSENS.FunctionRegistry(functionRuntime);
|
|
103
611
|
const devServer = await startDevServer(testBuilder, functionRuntime, functionsRegistry, options);
|
|
104
612
|
const scheduler = await startScheduler(testBuilder, functionRuntime, devServer, functionsRegistry);
|
|
105
613
|
return {
|
|
@@ -113,8 +621,8 @@ var startFunctionsHost = async (testBuilder, pluginInitializer, options) => {
|
|
|
113
621
|
};
|
|
114
622
|
};
|
|
115
623
|
var startScheduler = async (testBuilder, client, devServer, functionRegistry) => {
|
|
116
|
-
const triggerRegistry = new
|
|
117
|
-
const scheduler = new
|
|
624
|
+
const triggerRegistry = new import_chunk_V7JNSENS.TriggerRegistry(client);
|
|
625
|
+
const scheduler = new Scheduler(functionRegistry, triggerRegistry, {
|
|
118
626
|
endpoint: devServer.endpoint
|
|
119
627
|
});
|
|
120
628
|
await scheduler.start();
|
|
@@ -122,7 +630,7 @@ var startScheduler = async (testBuilder, client, devServer, functionRegistry) =>
|
|
|
122
630
|
return scheduler;
|
|
123
631
|
};
|
|
124
632
|
var startDevServer = async (testBuilder, client, functionRegistry, options) => {
|
|
125
|
-
const server = new
|
|
633
|
+
const server = new DevServer(client, functionRegistry, {
|
|
126
634
|
baseDir: import_node_path.default.join(__dirname, "../testing"),
|
|
127
635
|
port: await (0, import_get_port_please.getRandomPort)("127.0.0.1"),
|
|
128
636
|
...options
|
|
@@ -131,11 +639,11 @@ var startDevServer = async (testBuilder, client, functionRegistry, options) => {
|
|
|
131
639
|
testBuilder.ctx.onDispose(() => server.stop());
|
|
132
640
|
return server;
|
|
133
641
|
};
|
|
134
|
-
var
|
|
642
|
+
var __dxlog_file3 = "/home/runner/work/dxos/dxos/packages/core/functions/src/testing/util.ts";
|
|
135
643
|
var triggerWebhook = async (space, uri) => {
|
|
136
|
-
const trigger = (await space.db.query(
|
|
137
|
-
(0,
|
|
138
|
-
F:
|
|
644
|
+
const trigger = (await space.db.query(import_echo2.Filter.schema(import_chunk_GGTHSME4.FunctionTrigger, (t) => t.function === uri)).run()).objects[0];
|
|
645
|
+
(0, import_invariant2.invariant)(trigger.spec.type === "webhook", void 0, {
|
|
646
|
+
F: __dxlog_file3,
|
|
139
647
|
L: 17,
|
|
140
648
|
S: void 0,
|
|
141
649
|
A: [
|