@dxos/functions 0.5.3-main.6f2dfea → 0.5.3-main.77c09ab

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