@dxos/functions 0.5.3-main.e76d664 → 0.5.3-main.eb56347

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 (92) hide show
  1. package/dist/lib/browser/chunk-P3HPDHNI.mjs +86 -0
  2. package/dist/lib/browser/chunk-P3HPDHNI.mjs.map +7 -0
  3. package/dist/lib/browser/index.mjs +852 -462
  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-KTLM3JNV.cjs +103 -0
  9. package/dist/lib/node/chunk-KTLM3JNV.cjs.map +7 -0
  10. package/dist/lib/node/index.cjs +836 -452
  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 +2 -0
  26. package/dist/types/src/index.d.ts.map +1 -1
  27. package/dist/types/src/runtime/dev-server.d.ts +16 -13
  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 +13 -27
  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 +40 -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 +152 -109
  64. package/dist/types/src/types.d.ts.map +1 -1
  65. package/package.json +33 -15
  66. package/schema/functions.json +140 -104
  67. package/src/browser/index.ts +5 -0
  68. package/src/function/function-registry.test.ts +105 -0
  69. package/src/function/function-registry.ts +90 -0
  70. package/src/function/index.ts +5 -0
  71. package/src/handler.ts +54 -31
  72. package/src/index.ts +2 -0
  73. package/src/runtime/dev-server.test.ts +60 -0
  74. package/src/runtime/dev-server.ts +104 -53
  75. package/src/runtime/scheduler.test.ts +56 -73
  76. package/src/runtime/scheduler.ts +91 -270
  77. package/src/testing/functions-integration.test.ts +99 -0
  78. package/src/testing/index.ts +7 -0
  79. package/src/testing/setup.ts +45 -0
  80. package/src/testing/test/handler.ts +15 -0
  81. package/src/testing/test/index.ts +7 -0
  82. package/src/testing/types.ts +9 -0
  83. package/src/testing/util.ts +16 -0
  84. package/src/trigger/index.ts +5 -0
  85. package/src/trigger/trigger-registry.test.ts +255 -0
  86. package/src/trigger/trigger-registry.ts +201 -0
  87. package/src/trigger/type/index.ts +8 -0
  88. package/src/trigger/type/subscription-trigger.ts +80 -0
  89. package/src/trigger/type/timer-trigger.ts +44 -0
  90. package/src/trigger/type/webhook-trigger.ts +47 -0
  91. package/src/trigger/type/websocket-trigger.ts +91 -0
  92. package/src/types.ts +84 -48
@@ -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-P3HPDHNI.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: 68,
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: 70,
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: 52,
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: 63,
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: 76,
197
+ S: this,
198
+ A: [
199
+ "!this._server",
200
+ ""
201
+ ]
202
+ });
203
+ log3.info("starting...", void 0, {
204
+ F: __dxlog_file3,
205
+ L: 77,
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: 87,
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: 97,
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: 112,
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: 123,
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: 127,
281
+ S: this,
282
+ A: [
283
+ "this._server",
284
+ ""
285
+ ]
286
+ });
287
+ log3.info("stopping...", void 0, {
288
+ F: __dxlog_file3,
289
+ L: 128,
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: 132,
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: 135,
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: 140,
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: 154,
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: 163,
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: 185,
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 (e) {
389
+ log3.catch(e, void 0, {
390
+ F: __dxlog_file3,
391
+ L: 192,
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: 203,
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: 206,
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: 213,
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,453 +453,638 @@ 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 http from "@dxos/node-std/http";
269
- import WebSocket from "ws";
270
- import { TextV0Type } from "@braneframe/types";
271
- import { debounce, DeferredTask, sleep, Trigger as Trigger2 } from "@dxos/async";
272
- import { createSubscription, Filter, getAutomergeObjectCore } from "@dxos/client/echo";
273
- import { Context } from "@dxos/context";
274
- import { invariant as invariant2 } from "@dxos/invariant";
275
- import { log as log3 } from "@dxos/log";
276
- import { ComplexMap } from "@dxos/util";
277
- 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";
278
469
  var Scheduler = class {
279
- constructor(_client, _manifest, _options = {}) {
280
- this._client = _client;
281
- this._manifest = _manifest;
470
+ constructor(functions, triggers, _options = {}) {
471
+ this.functions = functions;
472
+ this.triggers = triggers;
282
473
  this._options = _options;
283
- 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
+ });
284
482
  }
285
483
  async start() {
286
- this._client.spaces.subscribe(async (spaces) => {
287
- for (const space of spaces) {
288
- await space.waitUntilReady();
289
- for (const trigger of this._manifest.triggers ?? []) {
290
- await this.mount(new Context(), space, trigger);
291
- }
292
- }
293
- });
484
+ await this._ctx.dispose();
485
+ this._ctx = createContext2();
486
+ await this.functions.open(this._ctx);
487
+ await this.triggers.open(this._ctx);
294
488
  }
295
489
  async stop() {
296
- for (const { id, spaceKey } of this._mounts.keys()) {
297
- await this.unmount(id, spaceKey);
298
- }
490
+ await this._ctx.dispose();
491
+ await this.functions.close();
492
+ await this.triggers.close();
299
493
  }
300
- async mount(ctx, space, trigger) {
301
- const key = {
302
- id: trigger.function,
303
- spaceKey: space.key
304
- };
305
- const def = this._manifest.functions.find((config) => config.id === trigger.function);
306
- invariant2(def, `Function not found: ${trigger.function}`, {
307
- F: __dxlog_file3,
308
- L: 72,
309
- S: this,
310
- A: [
311
- "def",
312
- "`Function not found: ${trigger.function}`"
313
- ]
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);
314
502
  });
315
- const exists = this._mounts.get(key);
316
- if (!exists) {
317
- this._mounts.set(key, {
318
- ctx,
319
- trigger
320
- });
321
- log3("mount", {
322
- space: space.key,
323
- trigger
503
+ await Promise.all(mountTasks).catch(log4.catch);
504
+ }
505
+ async activate(space, functions, fnTrigger) {
506
+ const definition = functions.find((def) => def.uri === fnTrigger.function);
507
+ if (!definition) {
508
+ log4.info("function is not found for trigger", {
509
+ fnTrigger
324
510
  }, {
325
- F: __dxlog_file3,
511
+ F: __dxlog_file4,
326
512
  L: 78,
327
513
  S: this,
328
514
  C: (f, a) => f(...a)
329
515
  });
330
- if (ctx.disposed) {
331
- return;
332
- }
333
- if (trigger.timer) {
334
- await this._createTimer(ctx, space, def, trigger.timer);
335
- }
336
- if (trigger.webhook) {
337
- await this._createWebhook(ctx, space, def, trigger.webhook);
338
- }
339
- if (trigger.websocket) {
340
- await this._createWebsocket(ctx, space, def, trigger.websocket);
341
- }
342
- if (trigger.subscription) {
343
- await this._createSubscription(ctx, space, def, trigger.subscription);
344
- }
345
- }
346
- }
347
- async unmount(id, spaceKey) {
348
- const key = {
349
- id,
350
- spaceKey
351
- };
352
- const { ctx } = this._mounts.get(key) ?? {};
353
- if (ctx) {
354
- this._mounts.delete(key);
355
- await ctx.dispose();
516
+ return;
356
517
  }
357
- }
358
- // TODO(burdon): Pass in Space key (common context).
359
- async _execFunction(def, data) {
360
- try {
361
- log3.info("exec", {
362
- function: def.id
518
+ await this.triggers.activate({
519
+ space
520
+ }, fnTrigger, async (args) => {
521
+ const mutex = this._functionUriToCallMutex.get(definition.uri) ?? new Mutex();
522
+ this._functionUriToCallMutex.set(definition.uri, mutex);
523
+ log4.info("function triggered, waiting for mutex", {
524
+ uri: definition.uri
363
525
  }, {
364
- F: __dxlog_file3,
365
- L: 117,
526
+ F: __dxlog_file4,
527
+ L: 86,
366
528
  S: this,
367
529
  C: (f, a) => f(...a)
368
530
  });
531
+ return mutex.executeSynchronized(() => {
532
+ log4.info("mutex acquired", {
533
+ uri: definition.uri
534
+ }, {
535
+ F: __dxlog_file4,
536
+ L: 88,
537
+ S: this,
538
+ C: (f, a) => f(...a)
539
+ });
540
+ return this._execFunction(definition, fnTrigger, {
541
+ meta: fnTrigger.meta,
542
+ data: {
543
+ ...args,
544
+ spaceKey: space.key
545
+ }
546
+ });
547
+ });
548
+ });
549
+ log4("activated trigger", {
550
+ space: space.key,
551
+ trigger: fnTrigger
552
+ }, {
553
+ F: __dxlog_file4,
554
+ L: 96,
555
+ S: this,
556
+ C: (f, a) => f(...a)
557
+ });
558
+ }
559
+ async _execFunction(def, trigger, { data, meta }) {
560
+ let status = 0;
561
+ try {
562
+ const payload = Object.assign({}, meta && {
563
+ meta
564
+ }, data);
369
565
  const { endpoint, callback } = this._options;
370
566
  if (endpoint) {
371
- await fetch(`${this._options.endpoint}/${def.name}`, {
567
+ const url = path.join(endpoint, def.route);
568
+ log4.info("exec", {
569
+ function: def.uri,
570
+ url,
571
+ triggerType: trigger.spec.type
572
+ }, {
573
+ F: __dxlog_file4,
574
+ L: 113,
575
+ S: this,
576
+ C: (f, a) => f(...a)
577
+ });
578
+ const response = await fetch(url, {
372
579
  method: "POST",
373
580
  headers: {
374
581
  "Content-Type": "application/json"
375
582
  },
376
- body: JSON.stringify(data)
583
+ body: JSON.stringify(payload)
377
584
  });
585
+ status = response.status;
378
586
  } else if (callback) {
379
- await callback(data);
587
+ log4.info("exec", {
588
+ function: def.uri
589
+ }, {
590
+ F: __dxlog_file4,
591
+ L: 124,
592
+ S: this,
593
+ C: (f, a) => f(...a)
594
+ });
595
+ status = await callback(payload) ?? 200;
380
596
  }
381
- log3.info("done", {
382
- function: def.id
597
+ if (status && status >= 400) {
598
+ throw new Error(`Response: ${status}`);
599
+ }
600
+ log4.info("done", {
601
+ function: def.uri,
602
+ status
383
603
  }, {
384
- F: __dxlog_file3,
385
- L: 133,
604
+ F: __dxlog_file4,
605
+ L: 134,
386
606
  S: this,
387
607
  C: (f, a) => f(...a)
388
608
  });
389
609
  } catch (err) {
390
- log3.error("error", {
391
- function: def.id,
610
+ log4.error("error", {
611
+ function: def.uri,
392
612
  error: err.message
393
613
  }, {
394
- F: __dxlog_file3,
395
- L: 135,
614
+ F: __dxlog_file4,
615
+ L: 136,
396
616
  S: this,
397
617
  C: (f, a) => f(...a)
398
618
  });
619
+ status = 500;
399
620
  }
621
+ return status;
400
622
  }
401
- //
402
- // Triggers
403
- //
404
- /**
405
- * Cron timer.
406
- */
407
- async _createTimer(ctx, space, def, trigger) {
408
- log3.info("timer", {
409
- space: space.key,
410
- trigger
623
+ };
624
+ var createContext2 = () => new Context2({
625
+ name: "FunctionScheduler"
626
+ });
627
+
628
+ // packages/core/functions/src/trigger/trigger-registry.ts
629
+ import { Event as Event3 } from "@dxos/async";
630
+ import { create as create2, Filter as Filter3, getMeta } from "@dxos/client/echo";
631
+ import { Context as Context3, Resource as Resource2 } from "@dxos/context";
632
+ import { ECHO_ATTR_META, foreignKey, foreignKeyEquals, splitMeta } from "@dxos/echo-schema";
633
+ import { invariant as invariant2 } from "@dxos/invariant";
634
+ import { PublicKey as PublicKey3 } from "@dxos/keys";
635
+ import { log as log9 } from "@dxos/log";
636
+ import { ComplexMap as ComplexMap2, diff as diff2, intersection } from "@dxos/util";
637
+
638
+ // packages/core/functions/src/trigger/type/subscription-trigger.ts
639
+ import { TextV0Type } from "@braneframe/types";
640
+ import { debounce, UpdateScheduler } from "@dxos/async";
641
+ import { createSubscription, Filter as Filter2, getAutomergeObjectCore } from "@dxos/echo-db";
642
+ import { log as log5 } from "@dxos/log";
643
+ var __dxlog_file5 = "/home/runner/work/dxos/dxos/packages/core/functions/src/trigger/type/subscription-trigger.ts";
644
+ var createSubscriptionTrigger = async (ctx, triggerCtx, spec, callback) => {
645
+ const objectIds = /* @__PURE__ */ new Set();
646
+ const task = new UpdateScheduler(ctx, async () => {
647
+ if (objectIds.size > 0) {
648
+ const objects = Array.from(objectIds);
649
+ objectIds.clear();
650
+ await callback({
651
+ objects
652
+ });
653
+ }
654
+ }, {
655
+ maxFrequency: 4
656
+ });
657
+ const subscriptions = [];
658
+ const subscription = createSubscription(({ added, updated }) => {
659
+ const sizeBefore = objectIds.size;
660
+ for (const object of added) {
661
+ objectIds.add(object.id);
662
+ }
663
+ for (const object of updated) {
664
+ objectIds.add(object.id);
665
+ }
666
+ if (objectIds.size > sizeBefore) {
667
+ log5.info("updated", {
668
+ added: added.length,
669
+ updated: updated.length
670
+ }, {
671
+ F: __dxlog_file5,
672
+ L: 45,
673
+ S: void 0,
674
+ C: (f, a) => f(...a)
675
+ });
676
+ task.trigger();
677
+ }
678
+ });
679
+ subscriptions.push(() => subscription.unsubscribe());
680
+ const { filter, options: { deep, delay } = {} } = spec;
681
+ const update = ({ objects }) => {
682
+ subscription.update(objects);
683
+ if (deep) {
684
+ log5.info("update", {
685
+ objects: objects.length
686
+ }, {
687
+ F: __dxlog_file5,
688
+ L: 59,
689
+ S: void 0,
690
+ C: (f, a) => f(...a)
691
+ });
692
+ for (const object of objects) {
693
+ const content = object.content;
694
+ if (content instanceof TextV0Type) {
695
+ subscriptions.push(getAutomergeObjectCore(content).updates.on(debounce(() => subscription.update([
696
+ object
697
+ ]), 1e3)));
698
+ }
699
+ }
700
+ }
701
+ };
702
+ const query = triggerCtx.space.db.query(Filter2.or(filter.map(({ type, props }) => Filter2.typename(type, props))));
703
+ subscriptions.push(query.subscribe(delay ? debounce(update, delay) : update));
704
+ ctx.onDispose(() => {
705
+ subscriptions.forEach((unsubscribe) => unsubscribe());
706
+ });
707
+ };
708
+
709
+ // packages/core/functions/src/trigger/type/timer-trigger.ts
710
+ import { CronJob } from "cron";
711
+ import { DeferredTask } from "@dxos/async";
712
+ import { log as log6 } from "@dxos/log";
713
+ var __dxlog_file6 = "/home/runner/work/dxos/dxos/packages/core/functions/src/trigger/type/timer-trigger.ts";
714
+ var createTimerTrigger = async (ctx, triggerContext, spec, callback) => {
715
+ const task = new DeferredTask(ctx, async () => {
716
+ await callback({});
717
+ });
718
+ let last = 0;
719
+ let run = 0;
720
+ const job = CronJob.from({
721
+ cronTime: spec.cron,
722
+ runOnInit: false,
723
+ onTick: () => {
724
+ const now = Date.now();
725
+ const delta = last ? now - last : 0;
726
+ last = now;
727
+ run++;
728
+ log6.info("tick", {
729
+ space: triggerContext.space.key.truncate(),
730
+ count: run,
731
+ delta
732
+ }, {
733
+ F: __dxlog_file6,
734
+ L: 37,
735
+ S: void 0,
736
+ C: (f, a) => f(...a)
737
+ });
738
+ task.schedule();
739
+ }
740
+ });
741
+ job.start();
742
+ ctx.onDispose(() => job.stop());
743
+ };
744
+
745
+ // packages/core/functions/src/trigger/type/webhook-trigger.ts
746
+ import { getPort as getPort2 } from "get-port-please";
747
+ import http from "@dxos/node-std/http";
748
+ import { log as log7 } from "@dxos/log";
749
+ var __dxlog_file7 = "/home/runner/work/dxos/dxos/packages/core/functions/src/trigger/type/webhook-trigger.ts";
750
+ var createWebhookTrigger = async (ctx, _, spec, callback) => {
751
+ const server = http.createServer(async (req, res) => {
752
+ if (req.method !== spec.method) {
753
+ res.statusCode = 405;
754
+ return res.end();
755
+ }
756
+ res.statusCode = await callback({});
757
+ res.end();
758
+ });
759
+ const port = await getPort2({
760
+ random: true
761
+ });
762
+ server.listen(port, () => {
763
+ log7.info("started webhook", {
764
+ port
411
765
  }, {
412
- F: __dxlog_file3,
413
- L: 147,
414
- S: this,
766
+ F: __dxlog_file7,
767
+ L: 40,
768
+ S: void 0,
415
769
  C: (f, a) => f(...a)
416
770
  });
417
- const { cron } = trigger;
418
- const task = new DeferredTask(ctx, async () => {
419
- await this._execFunction(def, {
420
- space: space.key
421
- });
422
- });
423
- let last = 0;
424
- let run = 0;
425
- const job = CronJob.from({
426
- cronTime: cron,
427
- runOnInit: false,
428
- onTick: () => {
429
- const now = Date.now();
430
- const delta = last ? now - last : 0;
431
- last = now;
432
- run++;
433
- log3.info("tick", {
434
- space: space.key.truncate(),
435
- count: run,
436
- delta
771
+ spec.port = port;
772
+ });
773
+ ctx.onDispose(() => {
774
+ server.close();
775
+ });
776
+ };
777
+
778
+ // packages/core/functions/src/trigger/type/websocket-trigger.ts
779
+ import WebSocket from "ws";
780
+ import { sleep, Trigger as Trigger2 } from "@dxos/async";
781
+ import { log as log8 } from "@dxos/log";
782
+ var __dxlog_file8 = "/home/runner/work/dxos/dxos/packages/core/functions/src/trigger/type/websocket-trigger.ts";
783
+ var createWebsocketTrigger = async (ctx, triggerCtx, spec, callback, options = {
784
+ retryDelay: 2,
785
+ maxAttempts: 5
786
+ }) => {
787
+ const { url, init } = spec;
788
+ let ws;
789
+ for (let attempt = 1; attempt <= options.maxAttempts; attempt++) {
790
+ const open = new Trigger2();
791
+ ws = new WebSocket(url);
792
+ Object.assign(ws, {
793
+ onopen: () => {
794
+ log8.info("opened", {
795
+ url
437
796
  }, {
438
- F: __dxlog_file3,
439
- L: 167,
440
- S: this,
797
+ F: __dxlog_file8,
798
+ L: 39,
799
+ S: void 0,
800
+ C: (f, a) => f(...a)
801
+ });
802
+ if (spec.init) {
803
+ ws.send(new TextEncoder().encode(JSON.stringify(init)));
804
+ }
805
+ open.wake(true);
806
+ },
807
+ onclose: (event) => {
808
+ log8.info("closed", {
809
+ url,
810
+ code: event.code
811
+ }, {
812
+ F: __dxlog_file8,
813
+ L: 48,
814
+ S: void 0,
815
+ C: (f, a) => f(...a)
816
+ });
817
+ if (event.code === 1006) {
818
+ setTimeout(async () => {
819
+ log8.info(`reconnecting in ${options.retryDelay}s...`, {
820
+ url
821
+ }, {
822
+ F: __dxlog_file8,
823
+ L: 53,
824
+ S: void 0,
825
+ C: (f, a) => f(...a)
826
+ });
827
+ await createWebsocketTrigger(ctx, triggerCtx, spec, callback, options);
828
+ }, options.retryDelay * 1e3);
829
+ }
830
+ open.wake(false);
831
+ },
832
+ onerror: (event) => {
833
+ log8.catch(event.error, {
834
+ url
835
+ }, {
836
+ F: __dxlog_file8,
837
+ L: 62,
838
+ S: void 0,
441
839
  C: (f, a) => f(...a)
442
840
  });
443
- task.schedule();
841
+ },
842
+ onmessage: async (event) => {
843
+ try {
844
+ log8.info("message", void 0, {
845
+ F: __dxlog_file8,
846
+ L: 67,
847
+ S: void 0,
848
+ C: (f, a) => f(...a)
849
+ });
850
+ const data = JSON.parse(new TextDecoder().decode(event.data));
851
+ await callback({
852
+ data
853
+ });
854
+ } catch (err) {
855
+ log8.catch(err, {
856
+ url
857
+ }, {
858
+ F: __dxlog_file8,
859
+ L: 71,
860
+ S: void 0,
861
+ C: (f, a) => f(...a)
862
+ });
863
+ }
444
864
  }
445
865
  });
446
- job.start();
447
- ctx.onDispose(() => job.stop());
866
+ const isOpen = await open.wait();
867
+ if (isOpen) {
868
+ break;
869
+ } else {
870
+ const wait = Math.pow(attempt, 2) * options.retryDelay;
871
+ if (attempt < options.maxAttempts) {
872
+ log8.warn(`failed to connect; trying again in ${wait}s`, {
873
+ attempt
874
+ }, {
875
+ F: __dxlog_file8,
876
+ L: 82,
877
+ S: void 0,
878
+ C: (f, a) => f(...a)
879
+ });
880
+ await sleep(wait * 1e3);
881
+ }
882
+ }
448
883
  }
449
- /**
450
- * Webhook.
451
- */
452
- async _createWebhook(ctx, space, def, trigger) {
453
- log3.info("webhook", {
454
- space: space.key,
884
+ ctx.onDispose(() => {
885
+ ws?.close();
886
+ });
887
+ };
888
+
889
+ // packages/core/functions/src/trigger/trigger-registry.ts
890
+ var __dxlog_file9 = "/home/runner/work/dxos/dxos/packages/core/functions/src/trigger/trigger-registry.ts";
891
+ var triggerHandlers = {
892
+ subscription: createSubscriptionTrigger,
893
+ timer: createTimerTrigger,
894
+ webhook: createWebhookTrigger,
895
+ websocket: createWebsocketTrigger
896
+ };
897
+ var TriggerRegistry = class extends Resource2 {
898
+ constructor(_client, _options) {
899
+ super();
900
+ this._client = _client;
901
+ this._options = _options;
902
+ this._triggersBySpaceKey = new ComplexMap2(PublicKey3.hash);
903
+ this.registered = new Event3();
904
+ this.removed = new Event3();
905
+ }
906
+ getActiveTriggers(space) {
907
+ return this._getTriggers(space, (t) => t.activationCtx != null);
908
+ }
909
+ getInactiveTriggers(space) {
910
+ return this._getTriggers(space, (t) => t.activationCtx == null);
911
+ }
912
+ async activate(triggerCtx, trigger, callback) {
913
+ log9("activate", {
914
+ space: triggerCtx.space.key,
455
915
  trigger
456
916
  }, {
457
- F: __dxlog_file3,
458
- L: 180,
917
+ F: __dxlog_file9,
918
+ L: 74,
459
919
  S: this,
460
920
  C: (f, a) => f(...a)
461
921
  });
462
- const { port } = trigger;
463
- const server = http.createServer(async (req, res) => {
464
- await this._execFunction(def, {
465
- space: space.key
466
- });
467
- });
468
- server.listen(port, () => {
469
- log3.info("started webhook", {
470
- port
471
- }, {
472
- F: __dxlog_file3,
473
- L: 189,
474
- S: this,
475
- C: (f, a) => f(...a)
476
- });
922
+ const activationCtx = new Context3({
923
+ name: `trigger_${trigger.function}`
477
924
  });
478
- ctx.onDispose(() => {
479
- server.close();
925
+ this._ctx.onDispose(() => activationCtx.dispose());
926
+ const registeredTrigger = this._triggersBySpaceKey.get(triggerCtx.space.key)?.find((reg) => reg.trigger.id === trigger.id);
927
+ invariant2(registeredTrigger, `Trigger is not registered: ${trigger.function}`, {
928
+ F: __dxlog_file9,
929
+ L: 80,
930
+ S: this,
931
+ A: [
932
+ "registeredTrigger",
933
+ "`Trigger is not registered: ${trigger.function}`"
934
+ ]
480
935
  });
936
+ registeredTrigger.activationCtx = activationCtx;
937
+ try {
938
+ const options = this._options?.[trigger.spec.type];
939
+ await triggerHandlers[trigger.spec.type](activationCtx, triggerCtx, trigger.spec, callback, options);
940
+ } catch (err) {
941
+ delete registeredTrigger.activationCtx;
942
+ throw err;
943
+ }
481
944
  }
482
945
  /**
483
- * Websocket.
946
+ * Loads triggers from the manifest into the space.
484
947
  */
485
- async _createWebsocket(ctx, space, def, trigger, options = {
486
- retryDelay: 2,
487
- maxAttempts: 5
488
- }) {
489
- log3.info("websocket", {
490
- space: space.key,
491
- trigger
948
+ async register(space, manifest) {
949
+ log9("register", {
950
+ space: space.key
492
951
  }, {
493
- F: __dxlog_file3,
494
- L: 213,
952
+ F: __dxlog_file9,
953
+ L: 96,
495
954
  S: this,
496
955
  C: (f, a) => f(...a)
497
956
  });
498
- const { url } = trigger;
499
- let ws;
500
- for (let attempt = 1; attempt <= options.maxAttempts; attempt++) {
501
- const open = new Trigger2();
502
- ws = new WebSocket(url);
503
- Object.assign(ws, {
504
- onopen: () => {
505
- log3.info("opened", {
506
- url
507
- }, {
508
- F: __dxlog_file3,
509
- L: 223,
510
- S: this,
511
- C: (f, a) => f(...a)
512
- });
513
- if (trigger.init) {
514
- ws.send(new TextEncoder().encode(JSON.stringify(trigger.init)));
515
- }
516
- open.wake(true);
517
- },
518
- onclose: () => {
519
- log3.info("closed", {
520
- url
521
- }, {
522
- F: __dxlog_file3,
523
- L: 232,
524
- S: this,
525
- C: (f, a) => f(...a)
526
- });
527
- open.wake(false);
528
- },
529
- onerror: (event) => {
530
- log3.catch(event.error, {
531
- url
532
- }, {
533
- F: __dxlog_file3,
534
- L: 237,
535
- S: this,
536
- C: (f, a) => f(...a)
537
- });
538
- },
539
- onmessage: async (event) => {
540
- try {
541
- const data = JSON.parse(new TextDecoder().decode(event.data));
542
- await this._execFunction(def, {
543
- space: space.key,
544
- data
545
- });
546
- } catch (err) {
547
- log3.catch(err, {
548
- url
549
- }, {
550
- F: __dxlog_file3,
551
- L: 245,
552
- S: this,
553
- C: (f, a) => f(...a)
554
- });
555
- }
957
+ if (!manifest.triggers?.length) {
958
+ return;
959
+ }
960
+ if (!space.db.graph.runtimeSchemaRegistry.hasSchema(FunctionTrigger)) {
961
+ space.db.graph.runtimeSchemaRegistry.registerSchema(FunctionTrigger);
962
+ }
963
+ const { objects: existing } = await space.db.query(Filter3.schema(FunctionTrigger)).run();
964
+ const { added } = diff2(existing, manifest.triggers, (a, b) => {
965
+ const keys = b[ECHO_ATTR_META]?.keys ?? [
966
+ foreignKey("manifest", [
967
+ b.function,
968
+ b.spec.type
969
+ ].join("-"))
970
+ ];
971
+ return intersection(getMeta(a)?.keys ?? [], keys, foreignKeyEquals).length > 0;
972
+ });
973
+ added.forEach((trigger) => {
974
+ const { meta, object } = splitMeta(trigger);
975
+ space.db.add(create2(FunctionTrigger, object, meta));
976
+ });
977
+ }
978
+ async _open() {
979
+ log9.info("open...", void 0, {
980
+ F: __dxlog_file9,
981
+ L: 121,
982
+ S: this,
983
+ C: (f, a) => f(...a)
984
+ });
985
+ const spaceListSubscription = this._client.spaces.subscribe(async (spaces) => {
986
+ for (const space of spaces) {
987
+ if (this._triggersBySpaceKey.has(space.key)) {
988
+ continue;
556
989
  }
557
- });
558
- const isOpen = await open.wait();
559
- if (isOpen) {
560
- break;
561
- } else {
562
- const wait = Math.pow(attempt, 2) * options.retryDelay;
563
- if (attempt < options.maxAttempts) {
564
- log3.warn(`failed to connect; trying again in ${wait}s`, {
565
- attempt
990
+ const registered = [];
991
+ this._triggersBySpaceKey.set(space.key, registered);
992
+ await space.waitUntilReady();
993
+ if (this._ctx.disposed) {
994
+ break;
995
+ }
996
+ this._ctx.onDispose(space.db.query(Filter3.schema(FunctionTrigger)).subscribe(async (triggers) => {
997
+ log9.info("update", {
998
+ space: space.key,
999
+ triggers: triggers.objects.length
566
1000
  }, {
567
- F: __dxlog_file3,
568
- L: 256,
1001
+ F: __dxlog_file9,
1002
+ L: 138,
569
1003
  S: this,
570
1004
  C: (f, a) => f(...a)
571
1005
  });
572
- await sleep(wait * 1e3);
573
- }
1006
+ await this._handleRemovedTriggers(space, triggers.objects, registered);
1007
+ this._handleNewTriggers(space, triggers.objects, registered);
1008
+ }));
574
1009
  }
575
- }
576
- ctx.onDispose(() => {
577
- ws?.close();
578
1010
  });
1011
+ this._ctx.onDispose(() => spaceListSubscription.unsubscribe());
579
1012
  }
580
- /**
581
- * ECHO subscription.
582
- */
583
- async _createSubscription(ctx, space, def, trigger) {
584
- log3.info("subscription", {
585
- space: space.key,
586
- trigger
587
- }, {
588
- F: __dxlog_file3,
589
- L: 271,
1013
+ async _close(_) {
1014
+ log9.info("close...", void 0, {
1015
+ F: __dxlog_file9,
1016
+ L: 150,
590
1017
  S: this,
591
1018
  C: (f, a) => f(...a)
592
1019
  });
593
- const objectIds = /* @__PURE__ */ new Set();
594
- const task = new DeferredTask(ctx, async () => {
595
- await this._execFunction(def, {
596
- space: space.key,
597
- objects: Array.from(objectIds)
598
- });
1020
+ this._triggersBySpaceKey.clear();
1021
+ }
1022
+ _handleNewTriggers(space, allTriggers, registered) {
1023
+ const newTriggers = allTriggers.filter((candidate) => {
1024
+ return registered.find((reg) => reg.trigger.id === candidate.id) == null;
599
1025
  });
600
- const subscriptions = [];
601
- const subscription = createSubscription(({ added, updated }) => {
602
- log3.info("updated", {
603
- added: added.length,
604
- updated: updated.length
605
- }, {
606
- F: __dxlog_file3,
607
- L: 281,
1026
+ if (newTriggers.length > 0) {
1027
+ const newRegisteredTriggers = newTriggers.map((trigger) => ({
1028
+ trigger
1029
+ }));
1030
+ registered.push(...newRegisteredTriggers);
1031
+ log9.info("added", () => ({
1032
+ spaceKey: space.key,
1033
+ triggers: newTriggers.map((trigger) => trigger.function)
1034
+ }), {
1035
+ F: __dxlog_file9,
1036
+ L: 162,
608
1037
  S: this,
609
1038
  C: (f, a) => f(...a)
610
1039
  });
611
- for (const object of added) {
612
- objectIds.add(object.id);
613
- }
614
- for (const object of updated) {
615
- objectIds.add(object.id);
616
- }
617
- task.schedule();
618
- });
619
- subscriptions.push(() => subscription.unsubscribe());
620
- const { filter, options: { deep, delay } = {} } = trigger;
621
- const update = ({ objects }) => {
622
- subscription.update(objects);
623
- if (deep) {
624
- log3.info("update", {
625
- objects: objects.length
626
- }, {
627
- F: __dxlog_file3,
628
- L: 301,
629
- S: this,
630
- C: (f, a) => f(...a)
631
- });
632
- for (const object of objects) {
633
- const content = object.content;
634
- if (content instanceof TextV0Type) {
635
- subscriptions.push(getAutomergeObjectCore(content).updates.on(debounce(() => subscription.update([
636
- object
637
- ]), 1e3)));
638
- }
1040
+ this.registered.emit({
1041
+ space,
1042
+ triggers: newTriggers
1043
+ });
1044
+ }
1045
+ }
1046
+ async _handleRemovedTriggers(space, allTriggers, registered) {
1047
+ const removed = [];
1048
+ for (let i = registered.length - 1; i >= 0; i--) {
1049
+ const wasRemoved = allTriggers.find((trigger) => trigger.id === registered[i].trigger.id) == null;
1050
+ if (wasRemoved) {
1051
+ if (removed.length) {
1052
+ log9.info("removed", () => ({
1053
+ spaceKey: space.key,
1054
+ triggers: removed.map((trigger) => trigger.function)
1055
+ }), {
1056
+ F: __dxlog_file9,
1057
+ L: 181,
1058
+ S: this,
1059
+ C: (f, a) => f(...a)
1060
+ });
639
1061
  }
1062
+ const unregistered = registered.splice(i, 1)[0];
1063
+ await unregistered.activationCtx?.dispose();
1064
+ removed.push(unregistered.trigger);
640
1065
  }
641
- };
642
- const query = space.db.query(Filter.or(filter.map(({ type, props }) => Filter.typename(type, props))));
643
- subscriptions.push(query.subscribe(delay ? debounce(update, delay) : update));
644
- ctx.onDispose(() => {
645
- subscriptions.forEach((unsubscribe) => unsubscribe());
646
- });
1066
+ }
1067
+ if (removed.length > 0) {
1068
+ this.removed.emit({
1069
+ space,
1070
+ triggers: removed
1071
+ });
1072
+ }
1073
+ }
1074
+ _getTriggers(space, predicate) {
1075
+ const allSpaceTriggers = this._triggersBySpaceKey.get(space.key) ?? [];
1076
+ return allSpaceTriggers.filter(predicate).map((trigger) => trigger.trigger);
647
1077
  }
648
1078
  };
649
-
650
- // packages/core/functions/src/types.ts
651
- import * as S from "@effect/schema/Schema";
652
- var TimerTriggerSchema = S.struct({
653
- cron: S.string
654
- });
655
- var WebhookTriggerSchema = S.struct({
656
- port: S.number
657
- });
658
- var WebsocketTriggerSchema = S.struct({
659
- url: S.string,
660
- init: S.optional(S.record(S.string, S.any))
661
- });
662
- var SubscriptionTriggerSchema = S.struct({
663
- spaceKey: S.optional(S.string),
664
- // TODO(burdon): Define query DSL.
665
- filter: S.array(S.struct({
666
- type: S.string,
667
- props: S.optional(S.record(S.string, S.any))
668
- })),
669
- options: S.optional(S.struct({
670
- // Watch changes to object (not just creation).
671
- deep: S.optional(S.boolean),
672
- // Debounce changes (delay in ms).
673
- delay: S.optional(S.number)
674
- }))
675
- });
676
- var FunctionTriggerSchema = S.struct({
677
- function: S.string.pipe(S.description("Function ID/URI.")),
678
- timer: S.optional(TimerTriggerSchema),
679
- webhook: S.optional(WebhookTriggerSchema),
680
- websocket: S.optional(WebsocketTriggerSchema),
681
- subscription: S.optional(SubscriptionTriggerSchema)
682
- });
683
- var FunctionDefSchema = S.struct({
684
- id: S.string,
685
- description: S.optional(S.string),
686
- name: S.string,
687
- // TODO(burdon): NPM/GitHub URL?
688
- handler: S.string
689
- });
690
- var FunctionManifestSchema = S.struct({
691
- functions: S.mutable(S.array(FunctionDefSchema)),
692
- triggers: S.mutable(S.array(FunctionTriggerSchema))
693
- });
694
1079
  export {
695
1080
  DevServer,
1081
+ FUNCTION_SCHEMA,
1082
+ FunctionDef,
696
1083
  FunctionManifestSchema,
1084
+ FunctionRegistry,
1085
+ FunctionTrigger,
697
1086
  Scheduler,
1087
+ TriggerRegistry,
698
1088
  subscriptionHandler
699
1089
  };
700
1090
  //# sourceMappingURL=index.mjs.map