@dxos/functions 0.5.4-main.962dfc7 → 0.5.4-main.a7f191d

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.
Files changed (42) hide show
  1. package/dist/lib/browser/chunk-ERL6PHMU.mjs +1084 -0
  2. package/dist/lib/browser/chunk-ERL6PHMU.mjs.map +7 -0
  3. package/dist/lib/browser/index.mjs +39 -1074
  4. package/dist/lib/browser/index.mjs.map +4 -4
  5. package/dist/lib/browser/meta.json +1 -1
  6. package/dist/lib/browser/testing/index.mjs +148 -0
  7. package/dist/lib/browser/testing/index.mjs.map +7 -0
  8. package/dist/lib/node/chunk-INM6XAL7.cjs +1097 -0
  9. package/dist/lib/node/chunk-INM6XAL7.cjs.map +7 -0
  10. package/dist/lib/node/index.cjs +36 -1067
  11. package/dist/lib/node/index.cjs.map +4 -4
  12. package/dist/lib/node/meta.json +1 -1
  13. package/dist/lib/node/testing/index.cjs +169 -0
  14. package/dist/lib/node/testing/index.cjs.map +7 -0
  15. package/dist/types/src/function/function-registry.d.ts +1 -0
  16. package/dist/types/src/function/function-registry.d.ts.map +1 -1
  17. package/dist/types/src/handler.d.ts +2 -2
  18. package/dist/types/src/handler.d.ts.map +1 -1
  19. package/dist/types/src/runtime/dev-server.d.ts +1 -0
  20. package/dist/types/src/runtime/dev-server.d.ts.map +1 -1
  21. package/dist/types/src/testing/index.d.ts +1 -0
  22. package/dist/types/src/testing/index.d.ts.map +1 -1
  23. package/dist/types/src/testing/manifest.d.ts +3 -0
  24. package/dist/types/src/testing/manifest.d.ts.map +1 -0
  25. package/dist/types/src/testing/plugin-init.d.ts +6 -0
  26. package/dist/types/src/testing/plugin-init.d.ts.map +1 -0
  27. package/dist/types/src/testing/setup.d.ts +11 -1
  28. package/dist/types/src/testing/setup.d.ts.map +1 -1
  29. package/dist/types/src/testing/util.d.ts +2 -0
  30. package/dist/types/src/testing/util.d.ts.map +1 -1
  31. package/package.json +23 -14
  32. package/src/function/function-registry.test.ts +14 -1
  33. package/src/function/function-registry.ts +10 -0
  34. package/src/handler.ts +22 -3
  35. package/src/runtime/dev-server.test.ts +42 -24
  36. package/src/runtime/dev-server.ts +17 -13
  37. package/src/testing/functions-integration.test.ts +8 -45
  38. package/src/testing/index.ts +1 -0
  39. package/src/testing/manifest.ts +15 -0
  40. package/src/testing/plugin-init.ts +20 -0
  41. package/src/testing/setup.ts +63 -6
  42. package/src/testing/util.ts +10 -0
@@ -1,9 +1,7 @@
1
1
  "use strict";
2
- var __create = Object.create;
3
2
  var __defProp = Object.defineProperty;
4
3
  var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
4
  var __getOwnPropNames = Object.getOwnPropertyNames;
6
- var __getProtoOf = Object.getPrototypeOf;
7
5
  var __hasOwnProp = Object.prototype.hasOwnProperty;
8
6
  var __export = (target, all) => {
9
7
  for (var name in all)
@@ -17,170 +15,58 @@ var __copyProps = (to, from, except, desc) => {
17
15
  }
18
16
  return to;
19
17
  };
20
- var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
21
- // If the importer is in node compatibility mode or this is not an ESM
22
- // file that has been converted to a CommonJS file using a Babel-
23
- // compatible transform (i.e. "__esModule" has not been set), then set
24
- // "default" to the CommonJS "module.exports" for node compatibility.
25
- isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
26
- mod
27
- ));
28
18
  var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
29
19
  var node_exports = {};
30
20
  __export(node_exports, {
31
- DevServer: () => DevServer,
21
+ DevServer: () => import_chunk_INM6XAL7.DevServer,
32
22
  FUNCTION_SCHEMA: () => import_chunk_3UYUR5N5.FUNCTION_SCHEMA,
33
23
  FunctionDef: () => import_chunk_3UYUR5N5.FunctionDef,
34
24
  FunctionManifestSchema: () => import_chunk_3UYUR5N5.FunctionManifestSchema,
35
- FunctionRegistry: () => FunctionRegistry,
25
+ FunctionRegistry: () => import_chunk_INM6XAL7.FunctionRegistry,
36
26
  FunctionTrigger: () => import_chunk_3UYUR5N5.FunctionTrigger,
37
- Scheduler: () => Scheduler,
38
- TriggerRegistry: () => TriggerRegistry,
27
+ Scheduler: () => import_chunk_INM6XAL7.Scheduler,
28
+ TriggerRegistry: () => import_chunk_INM6XAL7.TriggerRegistry,
39
29
  subscriptionHandler: () => subscriptionHandler
40
30
  });
41
31
  module.exports = __toCommonJS(node_exports);
32
+ var import_chunk_INM6XAL7 = require("./chunk-INM6XAL7.cjs");
42
33
  var import_chunk_3UYUR5N5 = require("./chunk-3UYUR5N5.cjs");
43
- var import_async = require("@dxos/async");
44
- var import_echo = require("@dxos/client/echo");
45
- var import_context = require("@dxos/context");
46
- var import_keys = require("@dxos/keys");
34
+ var import_types = require("@braneframe/types");
35
+ var import_client = require("@dxos/client");
47
36
  var import_log = require("@dxos/log");
48
37
  var import_util = require("@dxos/util");
49
- var import_client = require("@dxos/client");
50
- var import_log2 = require("@dxos/log");
51
- var import_util2 = require("@dxos/util");
52
- var import_express = __toESM(require("express"));
53
- var import_get_port_please = require("get-port-please");
54
- var import_node_path = require("node:path");
55
- var import_async2 = require("@dxos/async");
56
- var import_context2 = require("@dxos/context");
57
- var import_invariant = require("@dxos/invariant");
58
- var import_log3 = require("@dxos/log");
59
- var import_node_path2 = __toESM(require("node:path"));
60
- var import_async3 = require("@dxos/async");
61
- var import_context3 = require("@dxos/context");
62
- var import_log4 = require("@dxos/log");
63
- var import_async4 = require("@dxos/async");
64
- var import_echo2 = require("@dxos/client/echo");
65
- var import_context4 = require("@dxos/context");
66
- var import_echo_schema = require("@dxos/echo-schema");
67
- var import_invariant2 = require("@dxos/invariant");
68
- var import_keys2 = require("@dxos/keys");
69
- var import_log5 = require("@dxos/log");
70
- var import_util3 = require("@dxos/util");
71
- var import_types = require("@braneframe/types");
72
- var import_async5 = require("@dxos/async");
73
- var import_echo3 = require("@dxos/client/echo");
74
- var import_echo_db = require("@dxos/echo-db");
75
- var import_log6 = require("@dxos/log");
76
- var import_cron = require("cron");
77
- var import_async6 = require("@dxos/async");
78
- var import_log7 = require("@dxos/log");
79
- var import_get_port_please2 = require("get-port-please");
80
- var import_node_http = __toESM(require("node:http"));
81
- var import_log8 = require("@dxos/log");
82
- var import_ws = __toESM(require("ws"));
83
- var import_async7 = require("@dxos/async");
84
- var import_log9 = require("@dxos/log");
85
- var __dxlog_file = "/home/runner/work/dxos/dxos/packages/core/functions/src/function/function-registry.ts";
86
- var FunctionRegistry = class extends import_context.Resource {
87
- constructor(_client) {
88
- super();
89
- this._client = _client;
90
- this._functionBySpaceKey = new import_util.ComplexMap(import_keys.PublicKey.hash);
91
- this.registered = new import_async.Event();
92
- }
93
- getFunctions(space) {
94
- return this._functionBySpaceKey.get(space.key) ?? [];
95
- }
96
- /**
97
- * Loads function definitions from the manifest into the space.
98
- * We first load all the definitions from the space to deduplicate by functionId.
99
- */
100
- async register(space, functions) {
101
- (0, import_log.log)("register", {
102
- space: space.key,
103
- functions: functions?.length ?? 0
104
- }, {
105
- F: __dxlog_file,
106
- L: 38,
107
- S: this,
108
- C: (f, a) => f(...a)
109
- });
110
- if (!functions?.length) {
111
- return;
112
- }
113
- if (!space.db.graph.runtimeSchemaRegistry.hasSchema(import_chunk_3UYUR5N5.FunctionDef)) {
114
- space.db.graph.runtimeSchemaRegistry.registerSchema(import_chunk_3UYUR5N5.FunctionDef);
115
- }
116
- const { objects: existing } = await space.db.query(import_echo.Filter.schema(import_chunk_3UYUR5N5.FunctionDef)).run();
117
- const { added } = (0, import_util.diff)(existing, functions, (a, b) => a.uri === b.uri);
118
- added.forEach((def) => space.db.add((0, import_echo.create)(import_chunk_3UYUR5N5.FunctionDef, def)));
119
- }
120
- async _open() {
121
- import_log.log.info("opening...", void 0, {
122
- F: __dxlog_file,
123
- L: 54,
124
- S: this,
125
- C: (f, a) => f(...a)
126
- });
127
- const spacesSubscription = this._client.spaces.subscribe(async (spaces) => {
128
- for (const space of spaces) {
129
- if (this._functionBySpaceKey.has(space.key)) {
130
- continue;
131
- }
132
- const registered = [];
133
- this._functionBySpaceKey.set(space.key, registered);
134
- await space.waitUntilReady();
135
- if (this._ctx.disposed) {
136
- break;
137
- }
138
- this._ctx.onDispose(space.db.query(import_echo.Filter.schema(import_chunk_3UYUR5N5.FunctionDef)).subscribe(({ objects }) => {
139
- const { added } = (0, import_util.diff)(registered, objects, (a, b) => a.uri === b.uri);
140
- if (added.length > 0) {
141
- registered.push(...added);
142
- this.registered.emit({
143
- space,
144
- added
145
- });
146
- }
147
- }));
148
- }
149
- });
150
- this._ctx.onDispose(() => spacesSubscription.unsubscribe());
151
- }
152
- async _close(_) {
153
- import_log.log.info("closing...", void 0, {
154
- F: __dxlog_file,
155
- L: 87,
156
- S: this,
157
- C: (f, a) => f(...a)
158
- });
159
- this._functionBySpaceKey.clear();
160
- }
161
- };
162
- var __dxlog_file2 = "/home/runner/work/dxos/dxos/packages/core/functions/src/handler.ts";
163
- var subscriptionHandler = (handler) => {
164
- return ({ event: { data }, context, ...rest }) => {
38
+ var __dxlog_file = "/home/runner/work/dxos/dxos/packages/core/functions/src/handler.ts";
39
+ var subscriptionHandler = (handler, types) => {
40
+ return async ({ event: { data }, context, response, ...rest }) => {
165
41
  const { client } = context;
166
42
  const space = data.spaceKey ? client.spaces.get(import_client.PublicKey.from(data.spaceKey)) : void 0;
167
- const objects = space ? data.objects?.map((id) => space.db.getObjectById(id)).filter(import_util2.nonNullable) : [];
43
+ if (!space) {
44
+ import_log.log.error("Invalid space", void 0, {
45
+ F: __dxlog_file,
46
+ L: 90,
47
+ S: void 0,
48
+ C: (f, a) => f(...a)
49
+ });
50
+ return response.status(500);
51
+ }
52
+ registerTypes(space, types);
53
+ const objects = space ? data.objects?.map((id) => space.db.getObjectById(id)).filter(import_util.nonNullable) : [];
168
54
  if (!!data.spaceKey && !space) {
169
- import_log2.log.warn("invalid space", {
55
+ import_log.log.warn("invalid space", {
170
56
  data
171
57
  }, {
172
- F: __dxlog_file2,
173
- L: 91,
58
+ F: __dxlog_file,
59
+ L: 100,
174
60
  S: void 0,
175
61
  C: (f, a) => f(...a)
176
62
  });
177
63
  } else {
178
- import_log2.log.info("handler", {
64
+ import_log.log.info("handler", {
179
65
  space: space?.key.truncate(),
180
66
  objects: objects?.length
181
67
  }, {
182
- F: __dxlog_file2,
183
- L: 93,
68
+ F: __dxlog_file,
69
+ L: 102,
184
70
  S: void 0,
185
71
  C: (f, a) => f(...a)
186
72
  });
@@ -194,937 +80,20 @@ var subscriptionHandler = (handler) => {
194
80
  }
195
81
  },
196
82
  context,
83
+ response,
197
84
  ...rest
198
85
  });
199
86
  };
200
87
  };
201
- var __dxlog_file3 = "/home/runner/work/dxos/dxos/packages/core/functions/src/runtime/dev-server.ts";
202
- var DevServer = class {
203
- constructor(_client, _functionsRegistry, _options) {
204
- this._client = _client;
205
- this._functionsRegistry = _functionsRegistry;
206
- this._options = _options;
207
- this._ctx = createContext();
208
- this._handlers = {};
209
- this._seq = 0;
210
- this.update = new import_async2.Event();
211
- this._functionsRegistry.registered.on(async ({ added }) => {
212
- added.forEach((def) => this._load(def));
213
- await this._safeUpdateRegistration();
214
- (0, import_log3.log)("new functions loaded", {
215
- added
216
- }, {
217
- F: __dxlog_file3,
218
- L: 53,
219
- S: this,
220
- C: (f, a) => f(...a)
221
- });
222
- });
223
- }
224
- get stats() {
225
- return {
226
- seq: this._seq
227
- };
228
- }
229
- get endpoint() {
230
- (0, import_invariant.invariant)(this._port, void 0, {
231
- F: __dxlog_file3,
232
- L: 64,
233
- S: this,
234
- A: [
235
- "this._port",
236
- ""
237
- ]
238
- });
239
- return `http://localhost:${this._port}`;
240
- }
241
- get proxy() {
242
- return this._proxy;
243
- }
244
- get functions() {
245
- return Object.values(this._handlers);
246
- }
247
- async start() {
248
- (0, import_invariant.invariant)(!this._server, void 0, {
249
- F: __dxlog_file3,
250
- L: 77,
251
- S: this,
252
- A: [
253
- "!this._server",
254
- ""
255
- ]
256
- });
257
- import_log3.log.info("starting...", void 0, {
258
- F: __dxlog_file3,
259
- L: 78,
260
- S: this,
261
- C: (f, a) => f(...a)
262
- });
263
- this._ctx = createContext();
264
- const app = (0, import_express.default)();
265
- app.use(import_express.default.json());
266
- app.post("/:path", async (req, res) => {
267
- const { path: path2 } = req.params;
268
- try {
269
- import_log3.log.info("calling", {
270
- path: path2
271
- }, {
272
- F: __dxlog_file3,
273
- L: 88,
274
- S: this,
275
- C: (f, a) => f(...a)
276
- });
277
- if (this._options.reload) {
278
- const { def } = this._handlers["/" + path2];
279
- await this._load(def, true);
280
- }
281
- res.statusCode = await this.invoke("/" + path2, req.body);
282
- res.end();
283
- } catch (err) {
284
- import_log3.log.catch(err, void 0, {
285
- F: __dxlog_file3,
286
- L: 98,
287
- S: this,
288
- C: (f, a) => f(...a)
289
- });
290
- res.statusCode = 500;
291
- res.end();
292
- }
293
- });
294
- this._port = await (0, import_get_port_please.getPort)({
295
- host: "localhost",
296
- port: 7200,
297
- portRange: [
298
- 7200,
299
- 7299
300
- ]
301
- });
302
- this._server = app.listen(this._port);
303
- try {
304
- const { registrationId, endpoint } = await this._client.services.services.FunctionRegistryService.register({
305
- endpoint: this.endpoint
306
- });
307
- import_log3.log.info("registered", {
308
- endpoint
309
- }, {
310
- F: __dxlog_file3,
311
- L: 113,
312
- S: this,
313
- C: (f, a) => f(...a)
314
- });
315
- this._proxy = endpoint;
316
- this._functionServiceRegistration = registrationId;
317
- await this._functionsRegistry.open(this._ctx);
318
- } catch (err) {
319
- await this.stop();
320
- throw new Error("FunctionRegistryService not available (check plugin is configured).");
88
+ var registerTypes = (space, types = []) => {
89
+ const registry = space.db.graph.runtimeSchemaRegistry;
90
+ for (const type of [
91
+ ...types,
92
+ import_types.TextV0Type
93
+ ]) {
94
+ if (!registry.hasSchema(type)) {
95
+ registry.registerSchema(type);
321
96
  }
322
- import_log3.log.info("started", {
323
- port: this._port
324
- }, {
325
- F: __dxlog_file3,
326
- L: 124,
327
- S: this,
328
- C: (f, a) => f(...a)
329
- });
330
- }
331
- async stop() {
332
- (0, import_invariant.invariant)(this._server, void 0, {
333
- F: __dxlog_file3,
334
- L: 128,
335
- S: this,
336
- A: [
337
- "this._server",
338
- ""
339
- ]
340
- });
341
- import_log3.log.info("stopping...", void 0, {
342
- F: __dxlog_file3,
343
- L: 129,
344
- S: this,
345
- C: (f, a) => f(...a)
346
- });
347
- const trigger = new import_async2.Trigger();
348
- this._server.close(async () => {
349
- import_log3.log.info("server stopped", void 0, {
350
- F: __dxlog_file3,
351
- L: 133,
352
- S: this,
353
- C: (f, a) => f(...a)
354
- });
355
- try {
356
- if (this._functionServiceRegistration) {
357
- (0, import_invariant.invariant)(this._client.services.services.FunctionRegistryService, void 0, {
358
- F: __dxlog_file3,
359
- L: 136,
360
- S: this,
361
- A: [
362
- "this._client.services.services.FunctionRegistryService",
363
- ""
364
- ]
365
- });
366
- await this._client.services.services.FunctionRegistryService.unregister({
367
- registrationId: this._functionServiceRegistration
368
- });
369
- import_log3.log.info("unregistered", {
370
- registrationId: this._functionServiceRegistration
371
- }, {
372
- F: __dxlog_file3,
373
- L: 141,
374
- S: this,
375
- C: (f, a) => f(...a)
376
- });
377
- this._functionServiceRegistration = void 0;
378
- this._proxy = void 0;
379
- }
380
- trigger.wake();
381
- } catch (err) {
382
- trigger.throw(err);
383
- }
384
- });
385
- await trigger.wait();
386
- this._port = void 0;
387
- this._server = void 0;
388
- import_log3.log.info("stopped", void 0, {
389
- F: __dxlog_file3,
390
- L: 155,
391
- S: this,
392
- C: (f, a) => f(...a)
393
- });
394
- }
395
- /**
396
- * Load function.
397
- */
398
- async _load(def, force) {
399
- const { uri, route, handler } = def;
400
- const filePath = (0, import_node_path.join)(this._options.baseDir, handler);
401
- import_log3.log.info("loading", {
402
- uri,
403
- force
404
- }, {
405
- F: __dxlog_file3,
406
- L: 164,
407
- S: this,
408
- C: (f, a) => f(...a)
409
- });
410
- if (force) {
411
- Object.keys(import_chunk_3UYUR5N5.__require.cache).filter((key) => key.startsWith(filePath)).forEach((key) => {
412
- delete import_chunk_3UYUR5N5.__require.cache[key];
413
- });
414
- }
415
- const module2 = (0, import_chunk_3UYUR5N5.__require)(filePath);
416
- if (typeof module2.default !== "function") {
417
- throw new Error(`Handler must export default function: ${uri}`);
418
- }
419
- this._handlers[route] = {
420
- def,
421
- handler: module2.default
422
- };
423
- }
424
- async _safeUpdateRegistration() {
425
- (0, import_invariant.invariant)(this._functionServiceRegistration, void 0, {
426
- F: __dxlog_file3,
427
- L: 186,
428
- S: this,
429
- A: [
430
- "this._functionServiceRegistration",
431
- ""
432
- ]
433
- });
434
- try {
435
- await this._client.services.services.FunctionRegistryService.updateRegistration({
436
- registrationId: this._functionServiceRegistration,
437
- functions: this.functions.map(({ def: { id, route } }) => ({
438
- id,
439
- route
440
- }))
441
- });
442
- } catch (err) {
443
- import_log3.log.catch(err, void 0, {
444
- F: __dxlog_file3,
445
- L: 193,
446
- S: this,
447
- C: (f, a) => f(...a)
448
- });
449
- }
450
- }
451
- /**
452
- * Invoke function.
453
- */
454
- async invoke(path2, data) {
455
- const seq = ++this._seq;
456
- const now = Date.now();
457
- import_log3.log.info("req", {
458
- seq,
459
- path: path2
460
- }, {
461
- F: __dxlog_file3,
462
- L: 204,
463
- S: this,
464
- C: (f, a) => f(...a)
465
- });
466
- const statusCode = await this._invoke(path2, {
467
- data
468
- });
469
- import_log3.log.info("res", {
470
- seq,
471
- path: path2,
472
- statusCode,
473
- duration: Date.now() - now
474
- }, {
475
- F: __dxlog_file3,
476
- L: 207,
477
- S: this,
478
- C: (f, a) => f(...a)
479
- });
480
- this.update.emit(statusCode);
481
- return statusCode;
482
- }
483
- async _invoke(path2, event) {
484
- const { handler } = this._handlers[path2] ?? {};
485
- (0, import_invariant.invariant)(handler, `invalid path: ${path2}`, {
486
- F: __dxlog_file3,
487
- L: 214,
488
- S: this,
489
- A: [
490
- "handler",
491
- "`invalid path: ${path}`"
492
- ]
493
- });
494
- const context = {
495
- client: this._client,
496
- dataDir: this._options.dataDir
497
- };
498
- let statusCode = 200;
499
- const response = {
500
- status: (code) => {
501
- statusCode = code;
502
- return response;
503
- }
504
- };
505
- await handler({
506
- context,
507
- event,
508
- response
509
- });
510
- return statusCode;
511
- }
512
- };
513
- var createContext = () => new import_context2.Context({
514
- name: "DevServer"
515
- });
516
- var __dxlog_file4 = "/home/runner/work/dxos/dxos/packages/core/functions/src/runtime/scheduler.ts";
517
- var Scheduler = class {
518
- constructor(functions, triggers, _options = {}) {
519
- this.functions = functions;
520
- this.triggers = triggers;
521
- this._options = _options;
522
- this._ctx = createContext2();
523
- this._functionUriToCallMutex = /* @__PURE__ */ new Map();
524
- this.functions.registered.on(async ({ space, added }) => {
525
- await this._safeActivateTriggers(space, this.triggers.getInactiveTriggers(space), added);
526
- });
527
- this.triggers.registered.on(async ({ space, triggers: triggers2 }) => {
528
- await this._safeActivateTriggers(space, triggers2, this.functions.getFunctions(space));
529
- });
530
- }
531
- async start() {
532
- await this._ctx.dispose();
533
- this._ctx = createContext2();
534
- await this.functions.open(this._ctx);
535
- await this.triggers.open(this._ctx);
536
- }
537
- async stop() {
538
- await this._ctx.dispose();
539
- await this.functions.close();
540
- await this.triggers.close();
541
- }
542
- // TODO(burdon): Remove and update registries directly.
543
- async register(space, manifest) {
544
- await this.functions.register(space, manifest.functions);
545
- await this.triggers.register(space, manifest);
546
- }
547
- async _safeActivateTriggers(space, triggers, functions) {
548
- const mountTasks = triggers.map((trigger) => {
549
- return this.activate(space, functions, trigger);
550
- });
551
- await Promise.all(mountTasks).catch(import_log4.log.catch);
552
- }
553
- async activate(space, functions, trigger) {
554
- const definition = functions.find((def) => def.uri === trigger.function);
555
- if (!definition) {
556
- import_log4.log.info("function is not found for trigger", {
557
- trigger
558
- }, {
559
- F: __dxlog_file4,
560
- L: 78,
561
- S: this,
562
- C: (f, a) => f(...a)
563
- });
564
- return;
565
- }
566
- await this.triggers.activate(space, trigger, async (args) => {
567
- const mutex = this._functionUriToCallMutex.get(definition.uri) ?? new import_async3.Mutex();
568
- this._functionUriToCallMutex.set(definition.uri, mutex);
569
- import_log4.log.info("function triggered, waiting for mutex", {
570
- uri: definition.uri
571
- }, {
572
- F: __dxlog_file4,
573
- L: 86,
574
- S: this,
575
- C: (f, a) => f(...a)
576
- });
577
- return mutex.executeSynchronized(() => {
578
- import_log4.log.info("mutex acquired", {
579
- uri: definition.uri
580
- }, {
581
- F: __dxlog_file4,
582
- L: 88,
583
- S: this,
584
- C: (f, a) => f(...a)
585
- });
586
- return this._execFunction(definition, trigger, {
587
- meta: trigger.meta ?? {},
588
- data: {
589
- ...args,
590
- spaceKey: space.key
591
- }
592
- });
593
- });
594
- });
595
- (0, import_log4.log)("activated trigger", {
596
- space: space.key,
597
- trigger
598
- }, {
599
- F: __dxlog_file4,
600
- L: 96,
601
- S: this,
602
- C: (f, a) => f(...a)
603
- });
604
- }
605
- async _execFunction(def, trigger, { data, meta }) {
606
- let status = 0;
607
- try {
608
- const payload = Object.assign({}, meta && {
609
- meta
610
- }, data);
611
- const { endpoint, callback } = this._options;
612
- if (endpoint) {
613
- const url = import_node_path2.default.join(endpoint, def.route);
614
- import_log4.log.info("exec", {
615
- function: def.uri,
616
- url,
617
- triggerType: trigger.spec.type
618
- }, {
619
- F: __dxlog_file4,
620
- L: 113,
621
- S: this,
622
- C: (f, a) => f(...a)
623
- });
624
- const response = await fetch(url, {
625
- method: "POST",
626
- headers: {
627
- "Content-Type": "application/json"
628
- },
629
- body: JSON.stringify(payload)
630
- });
631
- status = response.status;
632
- } else if (callback) {
633
- import_log4.log.info("exec", {
634
- function: def.uri
635
- }, {
636
- F: __dxlog_file4,
637
- L: 124,
638
- S: this,
639
- C: (f, a) => f(...a)
640
- });
641
- status = await callback(payload) ?? 200;
642
- }
643
- if (status && status >= 400) {
644
- throw new Error(`Response: ${status}`);
645
- }
646
- import_log4.log.info("done", {
647
- function: def.uri,
648
- status
649
- }, {
650
- F: __dxlog_file4,
651
- L: 134,
652
- S: this,
653
- C: (f, a) => f(...a)
654
- });
655
- } catch (err) {
656
- import_log4.log.error("error", {
657
- function: def.uri,
658
- error: err.message
659
- }, {
660
- F: __dxlog_file4,
661
- L: 136,
662
- S: this,
663
- C: (f, a) => f(...a)
664
- });
665
- status = 500;
666
- }
667
- return status;
668
- }
669
- };
670
- var createContext2 = () => new import_context3.Context({
671
- name: "FunctionScheduler"
672
- });
673
- var __dxlog_file5 = "/home/runner/work/dxos/dxos/packages/core/functions/src/trigger/type/subscription-trigger.ts";
674
- var createSubscriptionTrigger = async (ctx, space, spec, callback) => {
675
- const objectIds = /* @__PURE__ */ new Set();
676
- const task = new import_async5.UpdateScheduler(ctx, async () => {
677
- if (objectIds.size > 0) {
678
- const objects = Array.from(objectIds);
679
- objectIds.clear();
680
- await callback({
681
- objects
682
- });
683
- }
684
- }, {
685
- maxFrequency: 4
686
- });
687
- const subscriptions = [];
688
- const subscription = (0, import_echo_db.createSubscription)(({ added, updated }) => {
689
- const sizeBefore = objectIds.size;
690
- for (const object of added) {
691
- objectIds.add(object.id);
692
- }
693
- for (const object of updated) {
694
- objectIds.add(object.id);
695
- }
696
- if (objectIds.size > sizeBefore) {
697
- import_log6.log.info("updated", {
698
- added: added.length,
699
- updated: updated.length
700
- }, {
701
- F: __dxlog_file5,
702
- L: 47,
703
- S: void 0,
704
- C: (f, a) => f(...a)
705
- });
706
- task.trigger();
707
- }
708
- });
709
- subscriptions.push(() => subscription.unsubscribe());
710
- const { filter, options: { deep, delay } = {} } = spec;
711
- const update = ({ objects }) => {
712
- import_log6.log.info("update", {
713
- objects: objects.length
714
- }, {
715
- F: __dxlog_file5,
716
- L: 57,
717
- S: void 0,
718
- C: (f, a) => f(...a)
719
- });
720
- subscription.update(objects);
721
- if (deep) {
722
- for (const object of objects) {
723
- const content = object.content;
724
- if (content instanceof import_types.TextV0Type) {
725
- subscriptions.push((0, import_echo_db.getAutomergeObjectCore)(content).updates.on((0, import_async5.debounce)(() => subscription.update([
726
- object
727
- ]), 1e3)));
728
- }
729
- }
730
- }
731
- };
732
- import_log6.log.info("subscription", {
733
- filter
734
- }, {
735
- F: __dxlog_file5,
736
- L: 76,
737
- S: void 0,
738
- C: (f, a) => f(...a)
739
- });
740
- if (filter) {
741
- const query = space.db.query(import_echo3.Filter.typename(filter[0].type, filter[0].props));
742
- subscriptions.push(query.subscribe(delay ? (0, import_async5.debounce)(update, delay) : update));
743
- }
744
- ctx.onDispose(() => {
745
- subscriptions.forEach((unsubscribe) => unsubscribe());
746
- });
747
- };
748
- var __dxlog_file6 = "/home/runner/work/dxos/dxos/packages/core/functions/src/trigger/type/timer-trigger.ts";
749
- var createTimerTrigger = async (ctx, space, spec, callback) => {
750
- const task = new import_async6.DeferredTask(ctx, async () => {
751
- await callback({});
752
- });
753
- let last = 0;
754
- let run = 0;
755
- const job = import_cron.CronJob.from({
756
- cronTime: spec.cron,
757
- runOnInit: false,
758
- onTick: () => {
759
- const now = Date.now();
760
- const delta = last ? now - last : 0;
761
- last = now;
762
- run++;
763
- import_log7.log.info("tick", {
764
- space: space.key.truncate(),
765
- count: run,
766
- delta
767
- }, {
768
- F: __dxlog_file6,
769
- L: 38,
770
- S: void 0,
771
- C: (f, a) => f(...a)
772
- });
773
- task.schedule();
774
- }
775
- });
776
- job.start();
777
- ctx.onDispose(() => job.stop());
778
- };
779
- var __dxlog_file7 = "/home/runner/work/dxos/dxos/packages/core/functions/src/trigger/type/webhook-trigger.ts";
780
- var createWebhookTrigger = async (ctx, space, spec, callback) => {
781
- const server = import_node_http.default.createServer(async (req, res) => {
782
- if (req.method !== spec.method) {
783
- res.statusCode = 405;
784
- return res.end();
785
- }
786
- res.statusCode = await callback({});
787
- res.end();
788
- });
789
- const port = await (0, import_get_port_please2.getPort)({
790
- random: true
791
- });
792
- server.listen(port, () => {
793
- import_log8.log.info("started webhook", {
794
- port
795
- }, {
796
- F: __dxlog_file7,
797
- L: 41,
798
- S: void 0,
799
- C: (f, a) => f(...a)
800
- });
801
- spec.port = port;
802
- });
803
- ctx.onDispose(() => {
804
- server.close();
805
- });
806
- };
807
- var __dxlog_file8 = "/home/runner/work/dxos/dxos/packages/core/functions/src/trigger/type/websocket-trigger.ts";
808
- var createWebsocketTrigger = async (ctx, space, spec, callback, options = {
809
- retryDelay: 2,
810
- maxAttempts: 5
811
- }) => {
812
- const { url, init } = spec;
813
- let ws;
814
- for (let attempt = 1; attempt <= options.maxAttempts; attempt++) {
815
- const open = new import_async7.Trigger();
816
- ws = new import_ws.default(url);
817
- Object.assign(ws, {
818
- onopen: () => {
819
- import_log9.log.info("opened", {
820
- url
821
- }, {
822
- F: __dxlog_file8,
823
- L: 40,
824
- S: void 0,
825
- C: (f, a) => f(...a)
826
- });
827
- if (spec.init) {
828
- ws.send(new TextEncoder().encode(JSON.stringify(init)));
829
- }
830
- open.wake(true);
831
- },
832
- onclose: (event) => {
833
- import_log9.log.info("closed", {
834
- url,
835
- code: event.code
836
- }, {
837
- F: __dxlog_file8,
838
- L: 49,
839
- S: void 0,
840
- C: (f, a) => f(...a)
841
- });
842
- if (event.code === 1006) {
843
- setTimeout(async () => {
844
- import_log9.log.info(`reconnecting in ${options.retryDelay}s...`, {
845
- url
846
- }, {
847
- F: __dxlog_file8,
848
- L: 54,
849
- S: void 0,
850
- C: (f, a) => f(...a)
851
- });
852
- await createWebsocketTrigger(ctx, space, spec, callback, options);
853
- }, options.retryDelay * 1e3);
854
- }
855
- open.wake(false);
856
- },
857
- onerror: (event) => {
858
- import_log9.log.catch(event.error, {
859
- url
860
- }, {
861
- F: __dxlog_file8,
862
- L: 63,
863
- S: void 0,
864
- C: (f, a) => f(...a)
865
- });
866
- },
867
- onmessage: async (event) => {
868
- try {
869
- import_log9.log.info("message", void 0, {
870
- F: __dxlog_file8,
871
- L: 68,
872
- S: void 0,
873
- C: (f, a) => f(...a)
874
- });
875
- const data = JSON.parse(new TextDecoder().decode(event.data));
876
- await callback({
877
- data
878
- });
879
- } catch (err) {
880
- import_log9.log.catch(err, {
881
- url
882
- }, {
883
- F: __dxlog_file8,
884
- L: 72,
885
- S: void 0,
886
- C: (f, a) => f(...a)
887
- });
888
- }
889
- }
890
- });
891
- const isOpen = await open.wait();
892
- if (isOpen) {
893
- break;
894
- } else {
895
- const wait = Math.pow(attempt, 2) * options.retryDelay;
896
- if (attempt < options.maxAttempts) {
897
- import_log9.log.warn(`failed to connect; trying again in ${wait}s`, {
898
- attempt
899
- }, {
900
- F: __dxlog_file8,
901
- L: 83,
902
- S: void 0,
903
- C: (f, a) => f(...a)
904
- });
905
- await (0, import_async7.sleep)(wait * 1e3);
906
- }
907
- }
908
- }
909
- ctx.onDispose(() => {
910
- ws?.close();
911
- });
912
- };
913
- var __dxlog_file9 = "/home/runner/work/dxos/dxos/packages/core/functions/src/trigger/trigger-registry.ts";
914
- var triggerHandlers = {
915
- subscription: createSubscriptionTrigger,
916
- timer: createTimerTrigger,
917
- webhook: createWebhookTrigger,
918
- websocket: createWebsocketTrigger
919
- };
920
- var TriggerRegistry = class extends import_context4.Resource {
921
- constructor(_client, _options) {
922
- super();
923
- this._client = _client;
924
- this._options = _options;
925
- this._triggersBySpaceKey = new import_util3.ComplexMap(import_keys2.PublicKey.hash);
926
- this.registered = new import_async4.Event();
927
- this.removed = new import_async4.Event();
928
- }
929
- getActiveTriggers(space) {
930
- return this._getTriggers(space, (t) => t.activationCtx != null);
931
- }
932
- getInactiveTriggers(space) {
933
- return this._getTriggers(space, (t) => t.activationCtx == null);
934
- }
935
- async activate(space, trigger, callback) {
936
- (0, import_log5.log)("activate", {
937
- space: space.key,
938
- trigger
939
- }, {
940
- F: __dxlog_file9,
941
- L: 72,
942
- S: this,
943
- C: (f, a) => f(...a)
944
- });
945
- const activationCtx = new import_context4.Context({
946
- name: `FunctionTrigger-${trigger.function}`
947
- });
948
- this._ctx.onDispose(() => activationCtx.dispose());
949
- const registeredTrigger = this._triggersBySpaceKey.get(space.key)?.find((reg) => reg.trigger.id === trigger.id);
950
- (0, import_invariant2.invariant)(registeredTrigger, `Trigger is not registered: ${trigger.function}`, {
951
- F: __dxlog_file9,
952
- L: 77,
953
- S: this,
954
- A: [
955
- "registeredTrigger",
956
- "`Trigger is not registered: ${trigger.function}`"
957
- ]
958
- });
959
- registeredTrigger.activationCtx = activationCtx;
960
- try {
961
- const options = this._options?.[trigger.spec.type];
962
- await triggerHandlers[trigger.spec.type](activationCtx, space, trigger.spec, callback, options);
963
- } catch (err) {
964
- delete registeredTrigger.activationCtx;
965
- throw err;
966
- }
967
- }
968
- /**
969
- * Loads triggers from the manifest into the space.
970
- */
971
- async register(space, manifest) {
972
- (0, import_log5.log)("register", {
973
- space: space.key
974
- }, {
975
- F: __dxlog_file9,
976
- L: 93,
977
- S: this,
978
- C: (f, a) => f(...a)
979
- });
980
- if (!manifest.triggers?.length) {
981
- return;
982
- }
983
- if (!space.db.graph.runtimeSchemaRegistry.hasSchema(import_chunk_3UYUR5N5.FunctionTrigger)) {
984
- space.db.graph.runtimeSchemaRegistry.registerSchema(import_chunk_3UYUR5N5.FunctionTrigger);
985
- }
986
- const manifestTriggers = manifest.triggers.map((trigger) => {
987
- let keys = trigger[import_echo_schema.ECHO_ATTR_META]?.keys;
988
- delete trigger[import_echo_schema.ECHO_ATTR_META];
989
- if (!keys?.length) {
990
- keys = [
991
- (0, import_echo_schema.foreignKey)("manifest", [
992
- trigger.function,
993
- trigger.spec.type
994
- ].join(":"))
995
- ];
996
- }
997
- return (0, import_echo2.create)(import_chunk_3UYUR5N5.FunctionTrigger, trigger, {
998
- keys
999
- });
1000
- });
1001
- const { objects: existing } = await space.db.query(import_echo2.Filter.schema(import_chunk_3UYUR5N5.FunctionTrigger)).run();
1002
- const { added } = (0, import_util3.diff)(existing, manifestTriggers, import_echo_schema.compareForeignKeys);
1003
- added.forEach((trigger) => {
1004
- space.db.add(trigger);
1005
- import_log5.log.info("added", {
1006
- meta: (0, import_echo2.getMeta)(trigger)
1007
- }, {
1008
- F: __dxlog_file9,
1009
- L: 120,
1010
- S: this,
1011
- C: (f, a) => f(...a)
1012
- });
1013
- });
1014
- if (added.length > 0) {
1015
- await space.db.flush();
1016
- }
1017
- }
1018
- async _open() {
1019
- import_log5.log.info("open...", void 0, {
1020
- F: __dxlog_file9,
1021
- L: 129,
1022
- S: this,
1023
- C: (f, a) => f(...a)
1024
- });
1025
- const spaceListSubscription = this._client.spaces.subscribe(async (spaces) => {
1026
- for (const space of spaces) {
1027
- if (this._triggersBySpaceKey.has(space.key)) {
1028
- continue;
1029
- }
1030
- const registered = [];
1031
- this._triggersBySpaceKey.set(space.key, registered);
1032
- await space.waitUntilReady();
1033
- if (this._ctx.disposed) {
1034
- break;
1035
- }
1036
- this._ctx.onDispose(space.db.query(import_echo2.Filter.schema(import_chunk_3UYUR5N5.FunctionTrigger)).subscribe(async ({ objects: current }) => {
1037
- import_log5.log.info("update", {
1038
- space: space.key,
1039
- registered: registered.length,
1040
- current: current.length
1041
- }, {
1042
- F: __dxlog_file9,
1043
- L: 146,
1044
- S: this,
1045
- C: (f, a) => f(...a)
1046
- });
1047
- await this._handleRemovedTriggers(space, current, registered);
1048
- this._handleNewTriggers(space, current, registered);
1049
- }));
1050
- }
1051
- });
1052
- this._ctx.onDispose(() => spaceListSubscription.unsubscribe());
1053
- import_log5.log.info("opened", void 0, {
1054
- F: __dxlog_file9,
1055
- L: 155,
1056
- S: this,
1057
- C: (f, a) => f(...a)
1058
- });
1059
- }
1060
- async _close(_) {
1061
- import_log5.log.info("close...", void 0, {
1062
- F: __dxlog_file9,
1063
- L: 159,
1064
- S: this,
1065
- C: (f, a) => f(...a)
1066
- });
1067
- this._triggersBySpaceKey.clear();
1068
- import_log5.log.info("closed", void 0, {
1069
- F: __dxlog_file9,
1070
- L: 161,
1071
- S: this,
1072
- C: (f, a) => f(...a)
1073
- });
1074
- }
1075
- _handleNewTriggers(space, current, registered) {
1076
- const added = current.filter((candidate) => {
1077
- return candidate.enabled && registered.find((reg) => reg.trigger.id === candidate.id) == null;
1078
- });
1079
- if (added.length > 0) {
1080
- const newRegisteredTriggers = added.map((trigger) => ({
1081
- trigger
1082
- }));
1083
- registered.push(...newRegisteredTriggers);
1084
- import_log5.log.info("added", () => ({
1085
- spaceKey: space.key,
1086
- triggers: added.map((trigger) => trigger.function)
1087
- }), {
1088
- F: __dxlog_file9,
1089
- L: 172,
1090
- S: this,
1091
- C: (f, a) => f(...a)
1092
- });
1093
- this.registered.emit({
1094
- space,
1095
- triggers: added
1096
- });
1097
- }
1098
- }
1099
- async _handleRemovedTriggers(space, current, registered) {
1100
- const removed = [];
1101
- for (let i = registered.length - 1; i >= 0; i--) {
1102
- const wasRemoved = current.filter((trigger) => trigger.enabled).find((trigger) => trigger.id === registered[i].trigger.id) == null;
1103
- if (wasRemoved) {
1104
- const unregistered = registered.splice(i, 1)[0];
1105
- await unregistered.activationCtx?.dispose();
1106
- removed.push(unregistered.trigger);
1107
- }
1108
- }
1109
- if (removed.length > 0) {
1110
- import_log5.log.info("removed", () => ({
1111
- spaceKey: space.key,
1112
- triggers: removed.map((trigger) => trigger.function)
1113
- }), {
1114
- F: __dxlog_file9,
1115
- L: 198,
1116
- S: this,
1117
- C: (f, a) => f(...a)
1118
- });
1119
- this.removed.emit({
1120
- space,
1121
- triggers: removed
1122
- });
1123
- }
1124
- }
1125
- _getTriggers(space, predicate) {
1126
- const allSpaceTriggers = this._triggersBySpaceKey.get(space.key) ?? [];
1127
- return allSpaceTriggers.filter(predicate).map((trigger) => trigger.trigger);
1128
97
  }
1129
98
  };
1130
99
  // Annotate the CommonJS export names for ESM import in node: