@dxos/functions 0.5.3-main.5a4243f → 0.5.3-main.5e3471c

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