@dxos/functions 0.5.3-main.79e0565 → 0.5.3-main.7e64682

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 (52) hide show
  1. package/dist/lib/browser/chunk-CMMH5BA6.mjs +86 -0
  2. package/dist/lib/browser/chunk-CMMH5BA6.mjs.map +7 -0
  3. package/dist/lib/browser/index.mjs +211 -204
  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-3C2FP4J4.cjs +103 -0
  9. package/dist/lib/node/chunk-3C2FP4J4.cjs.map +7 -0
  10. package/dist/lib/node/index.cjs +218 -203
  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.map +1 -1
  18. package/dist/types/src/handler.d.ts.map +1 -1
  19. package/dist/types/src/runtime/dev-server.d.ts.map +1 -1
  20. package/dist/types/src/runtime/scheduler.d.ts +1 -1
  21. package/dist/types/src/runtime/scheduler.d.ts.map +1 -1
  22. package/dist/types/src/testing/setup.d.ts.map +1 -1
  23. package/dist/types/src/trigger/trigger-registry.d.ts +2 -5
  24. package/dist/types/src/trigger/trigger-registry.d.ts.map +1 -1
  25. package/dist/types/src/trigger/type/subscription-trigger.d.ts.map +1 -1
  26. package/dist/types/src/trigger/type/timer-trigger.d.ts.map +1 -1
  27. package/dist/types/src/trigger/type/webhook-trigger.d.ts.map +1 -1
  28. package/dist/types/src/trigger/type/websocket-trigger.d.ts.map +1 -1
  29. package/dist/types/src/types.d.ts +43 -33
  30. package/dist/types/src/types.d.ts.map +1 -1
  31. package/package.json +30 -16
  32. package/schema/functions.json +5 -0
  33. package/src/browser/index.ts +5 -0
  34. package/src/function/function-registry.ts +5 -5
  35. package/src/runtime/dev-server.ts +4 -4
  36. package/src/runtime/scheduler.test.ts +14 -9
  37. package/src/runtime/scheduler.ts +14 -9
  38. package/src/testing/functions-integration.test.ts +1 -0
  39. package/src/testing/setup.ts +8 -10
  40. package/src/trigger/trigger-registry.test.ts +30 -13
  41. package/src/trigger/trigger-registry.ts +59 -37
  42. package/src/trigger/type/subscription-trigger.ts +13 -7
  43. package/src/trigger/type/timer-trigger.ts +4 -3
  44. package/src/trigger/type/webhook-trigger.ts +3 -2
  45. package/src/trigger/type/websocket-trigger.ts +4 -3
  46. package/src/types.ts +39 -30
  47. package/dist/types/src/util.d.ts +0 -15
  48. package/dist/types/src/util.d.ts.map +0 -1
  49. package/dist/types/src/util.test.d.ts +0 -2
  50. package/dist/types/src/util.test.d.ts.map +0 -1
  51. package/src/util.test.ts +0 -43
  52. package/src/util.ts +0 -48
@@ -29,22 +29,23 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
29
29
  var node_exports = {};
30
30
  __export(node_exports, {
31
31
  DevServer: () => DevServer,
32
- FunctionDef: () => FunctionDef,
33
- FunctionManifestSchema: () => FunctionManifestSchema,
32
+ FUNCTION_SCHEMA: () => import_chunk_3C2FP4J4.FUNCTION_SCHEMA,
33
+ FunctionDef: () => import_chunk_3C2FP4J4.FunctionDef,
34
+ FunctionManifestSchema: () => import_chunk_3C2FP4J4.FunctionManifestSchema,
34
35
  FunctionRegistry: () => FunctionRegistry,
35
- FunctionTrigger: () => FunctionTrigger,
36
+ FunctionTrigger: () => import_chunk_3C2FP4J4.FunctionTrigger,
36
37
  Scheduler: () => Scheduler,
37
38
  TriggerRegistry: () => TriggerRegistry,
38
39
  subscriptionHandler: () => subscriptionHandler
39
40
  });
40
41
  module.exports = __toCommonJS(node_exports);
42
+ var import_chunk_3C2FP4J4 = require("./chunk-3C2FP4J4.cjs");
41
43
  var import_async = require("@dxos/async");
42
44
  var import_echo = require("@dxos/client/echo");
43
45
  var import_context = require("@dxos/context");
44
46
  var import_keys = require("@dxos/keys");
45
47
  var import_log = require("@dxos/log");
46
48
  var import_util = require("@dxos/util");
47
- var import_echo_schema = require("@dxos/echo-schema");
48
49
  var import_client = require("@dxos/client");
49
50
  var import_log2 = require("@dxos/log");
50
51
  var import_util2 = require("@dxos/util");
@@ -62,13 +63,14 @@ var import_log4 = require("@dxos/log");
62
63
  var import_async4 = require("@dxos/async");
63
64
  var import_echo2 = require("@dxos/client/echo");
64
65
  var import_context4 = require("@dxos/context");
65
- var import_echo_schema2 = require("@dxos/echo-schema");
66
+ var import_echo_schema = require("@dxos/echo-schema");
66
67
  var import_invariant2 = require("@dxos/invariant");
67
68
  var import_keys2 = require("@dxos/keys");
68
69
  var import_log5 = require("@dxos/log");
69
70
  var import_util3 = require("@dxos/util");
70
71
  var import_types = require("@braneframe/types");
71
72
  var import_async5 = require("@dxos/async");
73
+ var import_echo3 = require("@dxos/client/echo");
72
74
  var import_echo_db = require("@dxos/echo-db");
73
75
  var import_log6 = require("@dxos/log");
74
76
  var import_cron = require("cron");
@@ -80,89 +82,6 @@ var import_log8 = require("@dxos/log");
80
82
  var import_ws = __toESM(require("ws"));
81
83
  var import_async7 = require("@dxos/async");
82
84
  var import_log9 = require("@dxos/log");
83
- var __require = /* @__PURE__ */ ((x) => typeof require !== "undefined" ? require : typeof Proxy !== "undefined" ? new Proxy(x, {
84
- get: (a, b) => (typeof require !== "undefined" ? require : a)[b]
85
- }) : x)(function(x) {
86
- if (typeof require !== "undefined")
87
- return require.apply(this, arguments);
88
- throw Error('Dynamic require of "' + x + '" is not supported');
89
- });
90
- var SubscriptionTriggerSchema = import_echo_schema.S.struct({
91
- type: import_echo_schema.S.literal("subscription"),
92
- // TODO(burdon): Define query DSL (from ECHO).
93
- filter: import_echo_schema.S.array(import_echo_schema.S.struct({
94
- type: import_echo_schema.S.string,
95
- props: import_echo_schema.S.optional(import_echo_schema.S.record(import_echo_schema.S.string, import_echo_schema.S.any))
96
- })),
97
- options: import_echo_schema.S.optional(import_echo_schema.S.struct({
98
- // Watch changes to object (not just creation).
99
- deep: import_echo_schema.S.optional(import_echo_schema.S.boolean),
100
- // Debounce changes (delay in ms).
101
- delay: import_echo_schema.S.optional(import_echo_schema.S.number)
102
- }))
103
- });
104
- var TimerTriggerSchema = import_echo_schema.S.struct({
105
- type: import_echo_schema.S.literal("timer"),
106
- cron: import_echo_schema.S.string
107
- });
108
- var WebhookTriggerSchema = import_echo_schema.S.mutable(import_echo_schema.S.struct({
109
- type: import_echo_schema.S.literal("webhook"),
110
- method: import_echo_schema.S.string,
111
- // Assigned port.
112
- port: import_echo_schema.S.optional(import_echo_schema.S.number)
113
- }));
114
- var WebsocketTriggerSchema = import_echo_schema.S.struct({
115
- type: import_echo_schema.S.literal("websocket"),
116
- url: import_echo_schema.S.string,
117
- init: import_echo_schema.S.optional(import_echo_schema.S.record(import_echo_schema.S.string, import_echo_schema.S.any))
118
- });
119
- var TriggerSpecSchema = import_echo_schema.S.union(TimerTriggerSchema, WebhookTriggerSchema, WebsocketTriggerSchema, SubscriptionTriggerSchema);
120
- var FunctionDef = class extends (0, import_echo_schema.TypedObject)({
121
- typename: "dxos.org/type/FunctionDef",
122
- version: "0.1.0"
123
- })({
124
- uri: import_echo_schema.S.string,
125
- description: import_echo_schema.S.optional(import_echo_schema.S.string),
126
- route: import_echo_schema.S.string,
127
- // TODO(burdon): NPM/GitHub/Docker/CF URL?
128
- handler: import_echo_schema.S.string
129
- }) {
130
- };
131
- var FunctionTrigger = class extends (0, import_echo_schema.TypedObject)({
132
- typename: "dxos.org/type/FunctionTrigger",
133
- version: "0.1.0"
134
- })({
135
- function: import_echo_schema.S.string.pipe(import_echo_schema.S.description("Function URI.")),
136
- // Context is merged into the event data passed to the function.
137
- meta: import_echo_schema.S.optional(import_echo_schema.S.object),
138
- spec: TriggerSpecSchema
139
- }) {
140
- };
141
- var FunctionManifestSchema = import_echo_schema.S.struct({
142
- functions: import_echo_schema.S.optional(import_echo_schema.S.mutable(import_echo_schema.S.array((0, import_echo_schema.RawObject)(FunctionDef)))),
143
- triggers: import_echo_schema.S.optional(import_echo_schema.S.mutable(import_echo_schema.S.array((0, import_echo_schema.RawObject)(FunctionTrigger))))
144
- });
145
- var diff = (previous, next, comparator) => {
146
- const remaining = [
147
- ...previous
148
- ];
149
- const result = {
150
- added: [],
151
- updated: [],
152
- removed: remaining
153
- };
154
- for (const object of next) {
155
- const index = remaining.findIndex((item) => comparator(item, object));
156
- if (index === -1) {
157
- result.added.push(object);
158
- } else {
159
- result.updated.push(remaining[index]);
160
- remaining.splice(index, 1);
161
- }
162
- }
163
- return result;
164
- };
165
- var intersection = (a, b, comparator) => a.filter((a2) => b.find((b2) => comparator(a2, b2)) !== void 0);
166
85
  var __dxlog_file = "/home/runner/work/dxos/dxos/packages/core/functions/src/function/function-registry.ts";
167
86
  var FunctionRegistry = class extends import_context.Resource {
168
87
  constructor(_client) {
@@ -184,22 +103,27 @@ var FunctionRegistry = class extends import_context.Resource {
184
103
  functions: functions?.length ?? 0
185
104
  }, {
186
105
  F: __dxlog_file,
187
- L: 39,
106
+ L: 38,
188
107
  S: this,
189
108
  C: (f, a) => f(...a)
190
109
  });
191
110
  if (!functions?.length) {
192
111
  return;
193
112
  }
194
- if (!space.db.graph.runtimeSchemaRegistry.hasSchema(FunctionDef)) {
195
- space.db.graph.runtimeSchemaRegistry.registerSchema(FunctionDef);
113
+ if (!space.db.graph.runtimeSchemaRegistry.hasSchema(import_chunk_3C2FP4J4.FunctionDef)) {
114
+ space.db.graph.runtimeSchemaRegistry.registerSchema(import_chunk_3C2FP4J4.FunctionDef);
196
115
  }
197
- const { objects: existing } = await space.db.query(import_echo.Filter.schema(FunctionDef)).run();
198
- const { added, removed } = diff(existing, functions, (a, b) => a.uri === b.uri);
199
- added.forEach((def) => space.db.add((0, import_echo.create)(FunctionDef, def)));
200
- removed.forEach((def) => space.db.remove(def));
116
+ const { objects: existing } = await space.db.query(import_echo.Filter.schema(import_chunk_3C2FP4J4.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_3C2FP4J4.FunctionDef, def)));
201
119
  }
202
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
+ });
203
127
  const spacesSubscription = this._client.spaces.subscribe(async (spaces) => {
204
128
  for (const space of spaces) {
205
129
  if (this._functionBySpaceKey.has(space.key)) {
@@ -211,8 +135,8 @@ var FunctionRegistry = class extends import_context.Resource {
211
135
  if (this._ctx.disposed) {
212
136
  break;
213
137
  }
214
- this._ctx.onDispose(space.db.query(import_echo.Filter.schema(FunctionDef)).subscribe(({ objects }) => {
215
- const { added } = diff(registered, objects, (a, b) => a.uri === b.uri);
138
+ this._ctx.onDispose(space.db.query(import_echo.Filter.schema(import_chunk_3C2FP4J4.FunctionDef)).subscribe(({ objects }) => {
139
+ const { added } = (0, import_util.diff)(registered, objects, (a, b) => a.uri === b.uri);
216
140
  if (added.length > 0) {
217
141
  registered.push(...added);
218
142
  this.registered.emit({
@@ -226,6 +150,12 @@ var FunctionRegistry = class extends import_context.Resource {
226
150
  this._ctx.onDispose(() => spacesSubscription.unsubscribe());
227
151
  }
228
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
+ });
229
159
  this._functionBySpaceKey.clear();
230
160
  }
231
161
  };
@@ -285,7 +215,7 @@ var DevServer = class {
285
215
  added
286
216
  }, {
287
217
  F: __dxlog_file3,
288
- L: 52,
218
+ L: 53,
289
219
  S: this,
290
220
  C: (f, a) => f(...a)
291
221
  });
@@ -299,7 +229,7 @@ var DevServer = class {
299
229
  get endpoint() {
300
230
  (0, import_invariant.invariant)(this._port, void 0, {
301
231
  F: __dxlog_file3,
302
- L: 63,
232
+ L: 64,
303
233
  S: this,
304
234
  A: [
305
235
  "this._port",
@@ -317,7 +247,7 @@ var DevServer = class {
317
247
  async start() {
318
248
  (0, import_invariant.invariant)(!this._server, void 0, {
319
249
  F: __dxlog_file3,
320
- L: 76,
250
+ L: 77,
321
251
  S: this,
322
252
  A: [
323
253
  "!this._server",
@@ -326,7 +256,7 @@ var DevServer = class {
326
256
  });
327
257
  import_log3.log.info("starting...", void 0, {
328
258
  F: __dxlog_file3,
329
- L: 77,
259
+ L: 78,
330
260
  S: this,
331
261
  C: (f, a) => f(...a)
332
262
  });
@@ -340,7 +270,7 @@ var DevServer = class {
340
270
  path: path2
341
271
  }, {
342
272
  F: __dxlog_file3,
343
- L: 87,
273
+ L: 88,
344
274
  S: this,
345
275
  C: (f, a) => f(...a)
346
276
  });
@@ -353,7 +283,7 @@ var DevServer = class {
353
283
  } catch (err) {
354
284
  import_log3.log.catch(err, void 0, {
355
285
  F: __dxlog_file3,
356
- L: 97,
286
+ L: 98,
357
287
  S: this,
358
288
  C: (f, a) => f(...a)
359
289
  });
@@ -378,7 +308,7 @@ var DevServer = class {
378
308
  endpoint
379
309
  }, {
380
310
  F: __dxlog_file3,
381
- L: 112,
311
+ L: 113,
382
312
  S: this,
383
313
  C: (f, a) => f(...a)
384
314
  });
@@ -393,7 +323,7 @@ var DevServer = class {
393
323
  port: this._port
394
324
  }, {
395
325
  F: __dxlog_file3,
396
- L: 123,
326
+ L: 124,
397
327
  S: this,
398
328
  C: (f, a) => f(...a)
399
329
  });
@@ -401,7 +331,7 @@ var DevServer = class {
401
331
  async stop() {
402
332
  (0, import_invariant.invariant)(this._server, void 0, {
403
333
  F: __dxlog_file3,
404
- L: 127,
334
+ L: 128,
405
335
  S: this,
406
336
  A: [
407
337
  "this._server",
@@ -410,7 +340,7 @@ var DevServer = class {
410
340
  });
411
341
  import_log3.log.info("stopping...", void 0, {
412
342
  F: __dxlog_file3,
413
- L: 128,
343
+ L: 129,
414
344
  S: this,
415
345
  C: (f, a) => f(...a)
416
346
  });
@@ -418,7 +348,7 @@ var DevServer = class {
418
348
  this._server.close(async () => {
419
349
  import_log3.log.info("server stopped", void 0, {
420
350
  F: __dxlog_file3,
421
- L: 132,
351
+ L: 133,
422
352
  S: this,
423
353
  C: (f, a) => f(...a)
424
354
  });
@@ -426,7 +356,7 @@ var DevServer = class {
426
356
  if (this._functionServiceRegistration) {
427
357
  (0, import_invariant.invariant)(this._client.services.services.FunctionRegistryService, void 0, {
428
358
  F: __dxlog_file3,
429
- L: 135,
359
+ L: 136,
430
360
  S: this,
431
361
  A: [
432
362
  "this._client.services.services.FunctionRegistryService",
@@ -440,7 +370,7 @@ var DevServer = class {
440
370
  registrationId: this._functionServiceRegistration
441
371
  }, {
442
372
  F: __dxlog_file3,
443
- L: 140,
373
+ L: 141,
444
374
  S: this,
445
375
  C: (f, a) => f(...a)
446
376
  });
@@ -457,7 +387,7 @@ var DevServer = class {
457
387
  this._server = void 0;
458
388
  import_log3.log.info("stopped", void 0, {
459
389
  F: __dxlog_file3,
460
- L: 154,
390
+ L: 155,
461
391
  S: this,
462
392
  C: (f, a) => f(...a)
463
393
  });
@@ -465,7 +395,7 @@ var DevServer = class {
465
395
  /**
466
396
  * Load function.
467
397
  */
468
- async _load(def, force = false) {
398
+ async _load(def, force) {
469
399
  const { uri, route, handler } = def;
470
400
  const filePath = (0, import_node_path.join)(this._options.baseDir, handler);
471
401
  import_log3.log.info("loading", {
@@ -473,16 +403,16 @@ var DevServer = class {
473
403
  force
474
404
  }, {
475
405
  F: __dxlog_file3,
476
- L: 163,
406
+ L: 164,
477
407
  S: this,
478
408
  C: (f, a) => f(...a)
479
409
  });
480
410
  if (force) {
481
- Object.keys(__require.cache).filter((key) => key.startsWith(filePath)).forEach((key) => {
482
- delete __require.cache[key];
411
+ Object.keys(import_chunk_3C2FP4J4.__require.cache).filter((key) => key.startsWith(filePath)).forEach((key) => {
412
+ delete import_chunk_3C2FP4J4.__require.cache[key];
483
413
  });
484
414
  }
485
- const module2 = __require(filePath);
415
+ const module2 = (0, import_chunk_3C2FP4J4.__require)(filePath);
486
416
  if (typeof module2.default !== "function") {
487
417
  throw new Error(`Handler must export default function: ${uri}`);
488
418
  }
@@ -494,7 +424,7 @@ var DevServer = class {
494
424
  async _safeUpdateRegistration() {
495
425
  (0, import_invariant.invariant)(this._functionServiceRegistration, void 0, {
496
426
  F: __dxlog_file3,
497
- L: 185,
427
+ L: 186,
498
428
  S: this,
499
429
  A: [
500
430
  "this._functionServiceRegistration",
@@ -509,10 +439,10 @@ var DevServer = class {
509
439
  route
510
440
  }))
511
441
  });
512
- } catch (e) {
513
- import_log3.log.catch(e, void 0, {
442
+ } catch (err) {
443
+ import_log3.log.catch(err, void 0, {
514
444
  F: __dxlog_file3,
515
- L: 192,
445
+ L: 193,
516
446
  S: this,
517
447
  C: (f, a) => f(...a)
518
448
  });
@@ -529,7 +459,7 @@ var DevServer = class {
529
459
  path: path2
530
460
  }, {
531
461
  F: __dxlog_file3,
532
- L: 203,
462
+ L: 204,
533
463
  S: this,
534
464
  C: (f, a) => f(...a)
535
465
  });
@@ -543,7 +473,7 @@ var DevServer = class {
543
473
  duration: Date.now() - now
544
474
  }, {
545
475
  F: __dxlog_file3,
546
- L: 206,
476
+ L: 207,
547
477
  S: this,
548
478
  C: (f, a) => f(...a)
549
479
  });
@@ -554,7 +484,7 @@ var DevServer = class {
554
484
  const { handler } = this._handlers[path2] ?? {};
555
485
  (0, import_invariant.invariant)(handler, `invalid path: ${path2}`, {
556
486
  F: __dxlog_file3,
557
- L: 213,
487
+ L: 214,
558
488
  S: this,
559
489
  A: [
560
490
  "handler",
@@ -590,7 +520,7 @@ var Scheduler = class {
590
520
  this.triggers = triggers;
591
521
  this._options = _options;
592
522
  this._ctx = createContext2();
593
- this._callMutex = new import_async3.Mutex();
523
+ this._functionUriToCallMutex = /* @__PURE__ */ new Map();
594
524
  this.functions.registered.on(async ({ space, added }) => {
595
525
  await this._safeActivateTriggers(space, this.triggers.getInactiveTriggers(space), added);
596
526
  });
@@ -620,11 +550,11 @@ var Scheduler = class {
620
550
  });
621
551
  await Promise.all(mountTasks).catch(import_log4.log.catch);
622
552
  }
623
- async activate(space, functions, fnTrigger) {
624
- const definition = functions.find((def) => def.uri === fnTrigger.function);
553
+ async activate(space, functions, trigger) {
554
+ const definition = functions.find((def) => def.uri === trigger.function);
625
555
  if (!definition) {
626
556
  import_log4.log.info("function is not found for trigger", {
627
- fnTrigger
557
+ trigger
628
558
  }, {
629
559
  F: __dxlog_file4,
630
560
  L: 78,
@@ -633,12 +563,28 @@ var Scheduler = class {
633
563
  });
634
564
  return;
635
565
  }
636
- await this.triggers.activate({
637
- space
638
- }, fnTrigger, async (args) => {
639
- return this._callMutex.executeSynchronized(() => {
640
- return this._execFunction(definition, fnTrigger, {
641
- meta: fnTrigger.meta,
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 ?? {},
642
588
  data: {
643
589
  ...args,
644
590
  spaceKey: space.key
@@ -648,10 +594,10 @@ var Scheduler = class {
648
594
  });
649
595
  (0, import_log4.log)("activated trigger", {
650
596
  space: space.key,
651
- trigger: fnTrigger
597
+ trigger
652
598
  }, {
653
599
  F: __dxlog_file4,
654
- L: 91,
600
+ L: 96,
655
601
  S: this,
656
602
  C: (f, a) => f(...a)
657
603
  });
@@ -671,7 +617,7 @@ var Scheduler = class {
671
617
  triggerType: trigger.spec.type
672
618
  }, {
673
619
  F: __dxlog_file4,
674
- L: 108,
620
+ L: 113,
675
621
  S: this,
676
622
  C: (f, a) => f(...a)
677
623
  });
@@ -688,7 +634,7 @@ var Scheduler = class {
688
634
  function: def.uri
689
635
  }, {
690
636
  F: __dxlog_file4,
691
- L: 119,
637
+ L: 124,
692
638
  S: this,
693
639
  C: (f, a) => f(...a)
694
640
  });
@@ -702,7 +648,7 @@ var Scheduler = class {
702
648
  status
703
649
  }, {
704
650
  F: __dxlog_file4,
705
- L: 129,
651
+ L: 134,
706
652
  S: this,
707
653
  C: (f, a) => f(...a)
708
654
  });
@@ -712,7 +658,7 @@ var Scheduler = class {
712
658
  error: err.message
713
659
  }, {
714
660
  F: __dxlog_file4,
715
- L: 131,
661
+ L: 136,
716
662
  S: this,
717
663
  C: (f, a) => f(...a)
718
664
  });
@@ -725,7 +671,7 @@ var createContext2 = () => new import_context3.Context({
725
671
  name: "FunctionScheduler"
726
672
  });
727
673
  var __dxlog_file5 = "/home/runner/work/dxos/dxos/packages/core/functions/src/trigger/type/subscription-trigger.ts";
728
- var createSubscriptionTrigger = async (ctx, triggerCtx, spec, callback) => {
674
+ var createSubscriptionTrigger = async (ctx, space, spec, callback) => {
729
675
  const objectIds = /* @__PURE__ */ new Set();
730
676
  const task = new import_async5.UpdateScheduler(ctx, async () => {
731
677
  if (objectIds.size > 0) {
@@ -753,7 +699,7 @@ var createSubscriptionTrigger = async (ctx, triggerCtx, spec, callback) => {
753
699
  updated: updated.length
754
700
  }, {
755
701
  F: __dxlog_file5,
756
- L: 45,
702
+ L: 47,
757
703
  S: void 0,
758
704
  C: (f, a) => f(...a)
759
705
  });
@@ -763,16 +709,16 @@ var createSubscriptionTrigger = async (ctx, triggerCtx, spec, callback) => {
763
709
  subscriptions.push(() => subscription.unsubscribe());
764
710
  const { filter, options: { deep, delay } = {} } = spec;
765
711
  const update = ({ objects }) => {
712
+ import_log6.log.info("update", {
713
+ objects: objects.length
714
+ }, {
715
+ F: __dxlog_file5,
716
+ L: 57,
717
+ S: void 0,
718
+ C: (f, a) => f(...a)
719
+ });
766
720
  subscription.update(objects);
767
721
  if (deep) {
768
- import_log6.log.info("update", {
769
- objects: objects.length
770
- }, {
771
- F: __dxlog_file5,
772
- L: 59,
773
- S: void 0,
774
- C: (f, a) => f(...a)
775
- });
776
722
  for (const object of objects) {
777
723
  const content = object.content;
778
724
  if (content instanceof import_types.TextV0Type) {
@@ -783,14 +729,24 @@ var createSubscriptionTrigger = async (ctx, triggerCtx, spec, callback) => {
783
729
  }
784
730
  }
785
731
  };
786
- const query = triggerCtx.space.db.query(import_echo_db.Filter.or(filter.map(({ type, props }) => import_echo_db.Filter.typename(type, props))));
787
- subscriptions.push(query.subscribe(delay ? (0, import_async5.debounce)(update, delay) : update));
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
+ }
788
744
  ctx.onDispose(() => {
789
745
  subscriptions.forEach((unsubscribe) => unsubscribe());
790
746
  });
791
747
  };
792
748
  var __dxlog_file6 = "/home/runner/work/dxos/dxos/packages/core/functions/src/trigger/type/timer-trigger.ts";
793
- var createTimerTrigger = async (ctx, triggerContext, spec, callback) => {
749
+ var createTimerTrigger = async (ctx, space, spec, callback) => {
794
750
  const task = new import_async6.DeferredTask(ctx, async () => {
795
751
  await callback({});
796
752
  });
@@ -805,12 +761,12 @@ var createTimerTrigger = async (ctx, triggerContext, spec, callback) => {
805
761
  last = now;
806
762
  run++;
807
763
  import_log7.log.info("tick", {
808
- space: triggerContext.space.key.truncate(),
764
+ space: space.key.truncate(),
809
765
  count: run,
810
766
  delta
811
767
  }, {
812
768
  F: __dxlog_file6,
813
- L: 37,
769
+ L: 38,
814
770
  S: void 0,
815
771
  C: (f, a) => f(...a)
816
772
  });
@@ -821,7 +777,7 @@ var createTimerTrigger = async (ctx, triggerContext, spec, callback) => {
821
777
  ctx.onDispose(() => job.stop());
822
778
  };
823
779
  var __dxlog_file7 = "/home/runner/work/dxos/dxos/packages/core/functions/src/trigger/type/webhook-trigger.ts";
824
- var createWebhookTrigger = async (ctx, _, spec, callback) => {
780
+ var createWebhookTrigger = async (ctx, space, spec, callback) => {
825
781
  const server = import_node_http.default.createServer(async (req, res) => {
826
782
  if (req.method !== spec.method) {
827
783
  res.statusCode = 405;
@@ -838,7 +794,7 @@ var createWebhookTrigger = async (ctx, _, spec, callback) => {
838
794
  port
839
795
  }, {
840
796
  F: __dxlog_file7,
841
- L: 40,
797
+ L: 41,
842
798
  S: void 0,
843
799
  C: (f, a) => f(...a)
844
800
  });
@@ -849,7 +805,7 @@ var createWebhookTrigger = async (ctx, _, spec, callback) => {
849
805
  });
850
806
  };
851
807
  var __dxlog_file8 = "/home/runner/work/dxos/dxos/packages/core/functions/src/trigger/type/websocket-trigger.ts";
852
- var createWebsocketTrigger = async (ctx, triggerCtx, spec, callback, options = {
808
+ var createWebsocketTrigger = async (ctx, space, spec, callback, options = {
853
809
  retryDelay: 2,
854
810
  maxAttempts: 5
855
811
  }) => {
@@ -864,7 +820,7 @@ var createWebsocketTrigger = async (ctx, triggerCtx, spec, callback, options = {
864
820
  url
865
821
  }, {
866
822
  F: __dxlog_file8,
867
- L: 39,
823
+ L: 40,
868
824
  S: void 0,
869
825
  C: (f, a) => f(...a)
870
826
  });
@@ -879,7 +835,7 @@ var createWebsocketTrigger = async (ctx, triggerCtx, spec, callback, options = {
879
835
  code: event.code
880
836
  }, {
881
837
  F: __dxlog_file8,
882
- L: 48,
838
+ L: 49,
883
839
  S: void 0,
884
840
  C: (f, a) => f(...a)
885
841
  });
@@ -889,11 +845,11 @@ var createWebsocketTrigger = async (ctx, triggerCtx, spec, callback, options = {
889
845
  url
890
846
  }, {
891
847
  F: __dxlog_file8,
892
- L: 53,
848
+ L: 54,
893
849
  S: void 0,
894
850
  C: (f, a) => f(...a)
895
851
  });
896
- await createWebsocketTrigger(ctx, triggerCtx, spec, callback, options);
852
+ await createWebsocketTrigger(ctx, space, spec, callback, options);
897
853
  }, options.retryDelay * 1e3);
898
854
  }
899
855
  open.wake(false);
@@ -903,7 +859,7 @@ var createWebsocketTrigger = async (ctx, triggerCtx, spec, callback, options = {
903
859
  url
904
860
  }, {
905
861
  F: __dxlog_file8,
906
- L: 62,
862
+ L: 63,
907
863
  S: void 0,
908
864
  C: (f, a) => f(...a)
909
865
  });
@@ -912,7 +868,7 @@ var createWebsocketTrigger = async (ctx, triggerCtx, spec, callback, options = {
912
868
  try {
913
869
  import_log9.log.info("message", void 0, {
914
870
  F: __dxlog_file8,
915
- L: 67,
871
+ L: 68,
916
872
  S: void 0,
917
873
  C: (f, a) => f(...a)
918
874
  });
@@ -925,7 +881,7 @@ var createWebsocketTrigger = async (ctx, triggerCtx, spec, callback, options = {
925
881
  url
926
882
  }, {
927
883
  F: __dxlog_file8,
928
- L: 71,
884
+ L: 72,
929
885
  S: void 0,
930
886
  C: (f, a) => f(...a)
931
887
  });
@@ -942,7 +898,7 @@ var createWebsocketTrigger = async (ctx, triggerCtx, spec, callback, options = {
942
898
  attempt
943
899
  }, {
944
900
  F: __dxlog_file8,
945
- L: 82,
901
+ L: 83,
946
902
  S: void 0,
947
903
  C: (f, a) => f(...a)
948
904
  });
@@ -976,24 +932,24 @@ var TriggerRegistry = class extends import_context4.Resource {
976
932
  getInactiveTriggers(space) {
977
933
  return this._getTriggers(space, (t) => t.activationCtx == null);
978
934
  }
979
- async activate(triggerCtx, trigger, callback) {
935
+ async activate(space, trigger, callback) {
980
936
  (0, import_log5.log)("activate", {
981
- space: triggerCtx.space.key,
937
+ space: space.key,
982
938
  trigger
983
939
  }, {
984
940
  F: __dxlog_file9,
985
- L: 75,
941
+ L: 72,
986
942
  S: this,
987
943
  C: (f, a) => f(...a)
988
944
  });
989
945
  const activationCtx = new import_context4.Context({
990
- name: `trigger_${trigger.function}`
946
+ name: `FunctionTrigger-${trigger.function}`
991
947
  });
992
948
  this._ctx.onDispose(() => activationCtx.dispose());
993
- const registeredTrigger = this._triggersBySpaceKey.get(triggerCtx.space.key)?.find((reg) => reg.trigger.id === trigger.id);
949
+ const registeredTrigger = this._triggersBySpaceKey.get(space.key)?.find((reg) => reg.trigger.id === trigger.id);
994
950
  (0, import_invariant2.invariant)(registeredTrigger, `Trigger is not registered: ${trigger.function}`, {
995
951
  F: __dxlog_file9,
996
- L: 81,
952
+ L: 77,
997
953
  S: this,
998
954
  A: [
999
955
  "registeredTrigger",
@@ -1003,7 +959,7 @@ var TriggerRegistry = class extends import_context4.Resource {
1003
959
  registeredTrigger.activationCtx = activationCtx;
1004
960
  try {
1005
961
  const options = this._options?.[trigger.spec.type];
1006
- await triggerHandlers[trigger.spec.type](activationCtx, triggerCtx, trigger.spec, callback, options);
962
+ await triggerHandlers[trigger.spec.type](activationCtx, space, trigger.spec, callback, options);
1007
963
  } catch (err) {
1008
964
  delete registeredTrigger.activationCtx;
1009
965
  throw err;
@@ -1017,33 +973,55 @@ var TriggerRegistry = class extends import_context4.Resource {
1017
973
  space: space.key
1018
974
  }, {
1019
975
  F: __dxlog_file9,
1020
- L: 97,
976
+ L: 93,
1021
977
  S: this,
1022
978
  C: (f, a) => f(...a)
1023
979
  });
1024
980
  if (!manifest.triggers?.length) {
1025
981
  return;
1026
982
  }
1027
- if (!space.db.graph.runtimeSchemaRegistry.hasSchema(FunctionTrigger)) {
1028
- space.db.graph.runtimeSchemaRegistry.registerSchema(FunctionTrigger);
983
+ if (!space.db.graph.runtimeSchemaRegistry.hasSchema(import_chunk_3C2FP4J4.FunctionTrigger)) {
984
+ space.db.graph.runtimeSchemaRegistry.registerSchema(import_chunk_3C2FP4J4.FunctionTrigger);
1029
985
  }
1030
- const { objects: existing } = await space.db.query(import_echo2.Filter.schema(FunctionTrigger)).run();
1031
- const { added, removed } = diff(existing, manifest.triggers, (a, b) => {
1032
- const keys = b[import_echo_schema2.ECHO_ATTR_META]?.keys ?? [
1033
- (0, import_echo_schema2.foreignKey)("manifest", [
1034
- b.function,
1035
- b.spec.type
1036
- ].join("-"))
1037
- ];
1038
- return intersection((0, import_echo2.getMeta)(a)?.keys ?? [], keys, import_echo_schema2.foreignKeyEquals).length > 0;
986
+ const manifestTriggers = manifest.triggers.map((trigger) => {
987
+ let keys = trigger[import_echo_schema.ECHO_ATTR_META]?.keys;
988
+ delete trigger[import_echo_schema.ECHO_ATTR_META];
989
+ if (!keys?.length) {
990
+ keys = [
991
+ (0, import_echo_schema.foreignKey)("manifest", [
992
+ trigger.function,
993
+ trigger.spec.type
994
+ ].join(":"))
995
+ ];
996
+ }
997
+ return (0, import_echo2.create)(import_chunk_3C2FP4J4.FunctionTrigger, trigger, {
998
+ keys
999
+ });
1039
1000
  });
1001
+ const { objects: existing } = await space.db.query(import_echo2.Filter.schema(import_chunk_3C2FP4J4.FunctionTrigger)).run();
1002
+ const { added } = (0, import_util3.diff)(existing, manifestTriggers, import_echo_schema.compareForeignKeys);
1040
1003
  added.forEach((trigger) => {
1041
- const { meta, object } = (0, import_echo_schema2.splitMeta)(trigger);
1042
- space.db.add((0, import_echo2.create)(FunctionTrigger, object, meta));
1004
+ space.db.add(trigger);
1005
+ import_log5.log.info("added", {
1006
+ meta: (0, import_echo2.getMeta)(trigger)
1007
+ }, {
1008
+ F: __dxlog_file9,
1009
+ L: 120,
1010
+ S: this,
1011
+ C: (f, a) => f(...a)
1012
+ });
1043
1013
  });
1044
- removed.forEach((trigger) => space.db.remove(trigger));
1014
+ if (added.length > 0) {
1015
+ await space.db.flush();
1016
+ }
1045
1017
  }
1046
1018
  async _open() {
1019
+ import_log5.log.info("open...", void 0, {
1020
+ F: __dxlog_file9,
1021
+ L: 129,
1022
+ S: this,
1023
+ C: (f, a) => f(...a)
1024
+ });
1047
1025
  const spaceListSubscription = this._client.spaces.subscribe(async (spaces) => {
1048
1026
  for (const space of spaces) {
1049
1027
  if (this._triggersBySpaceKey.has(space.key)) {
@@ -1055,46 +1033,73 @@ var TriggerRegistry = class extends import_context4.Resource {
1055
1033
  if (this._ctx.disposed) {
1056
1034
  break;
1057
1035
  }
1058
- const functionsSubscription = space.db.query(import_echo2.Filter.schema(FunctionTrigger)).subscribe(async (triggers) => {
1059
- await this._handleRemovedTriggers(space, triggers.objects, registered);
1060
- this._handleNewTriggers(space, triggers.objects, registered);
1061
- });
1062
- this._ctx.onDispose(functionsSubscription);
1036
+ this._ctx.onDispose(space.db.query(import_echo2.Filter.schema(import_chunk_3C2FP4J4.FunctionTrigger)).subscribe(async ({ objects: current }) => {
1037
+ import_log5.log.info("update", {
1038
+ space: space.key,
1039
+ registered: registered.length,
1040
+ current: current.length
1041
+ }, {
1042
+ F: __dxlog_file9,
1043
+ L: 146,
1044
+ S: this,
1045
+ C: (f, a) => f(...a)
1046
+ });
1047
+ await this._handleRemovedTriggers(space, current, registered);
1048
+ this._handleNewTriggers(space, current, registered);
1049
+ }));
1063
1050
  }
1064
1051
  });
1065
1052
  this._ctx.onDispose(() => spaceListSubscription.unsubscribe());
1053
+ import_log5.log.info("opened", void 0, {
1054
+ F: __dxlog_file9,
1055
+ L: 155,
1056
+ S: this,
1057
+ C: (f, a) => f(...a)
1058
+ });
1066
1059
  }
1067
1060
  async _close(_) {
1061
+ import_log5.log.info("close...", void 0, {
1062
+ F: __dxlog_file9,
1063
+ L: 159,
1064
+ S: this,
1065
+ C: (f, a) => f(...a)
1066
+ });
1068
1067
  this._triggersBySpaceKey.clear();
1068
+ import_log5.log.info("closed", void 0, {
1069
+ F: __dxlog_file9,
1070
+ L: 161,
1071
+ S: this,
1072
+ C: (f, a) => f(...a)
1073
+ });
1069
1074
  }
1070
- _handleNewTriggers(space, allTriggers, registered) {
1071
- const newTriggers = allTriggers.filter((candidate) => {
1072
- return registered.find((reg) => reg.trigger.id === candidate.id) == null;
1075
+ _handleNewTriggers(space, current, registered) {
1076
+ const added = current.filter((candidate) => {
1077
+ return candidate.enabled && registered.find((reg) => reg.trigger.id === candidate.id) == null;
1073
1078
  });
1074
- if (newTriggers.length > 0) {
1075
- const newRegisteredTriggers = newTriggers.map((trigger) => ({
1079
+ if (added.length > 0) {
1080
+ const newRegisteredTriggers = added.map((trigger) => ({
1076
1081
  trigger
1077
1082
  }));
1078
1083
  registered.push(...newRegisteredTriggers);
1079
- (0, import_log5.log)("registered new triggers", () => ({
1084
+ import_log5.log.info("added", () => ({
1080
1085
  spaceKey: space.key,
1081
- functions: newTriggers.map((t) => t.function)
1086
+ triggers: added.map((trigger) => trigger.function)
1082
1087
  }), {
1083
1088
  F: __dxlog_file9,
1084
- L: 159,
1089
+ L: 172,
1085
1090
  S: this,
1086
1091
  C: (f, a) => f(...a)
1087
1092
  });
1088
1093
  this.registered.emit({
1089
1094
  space,
1090
- triggers: newTriggers
1095
+ triggers: added
1091
1096
  });
1092
1097
  }
1093
1098
  }
1094
- async _handleRemovedTriggers(space, allTriggers, registered) {
1099
+ async _handleRemovedTriggers(space, current, registered) {
1095
1100
  const removed = [];
1096
1101
  for (let i = registered.length - 1; i >= 0; i--) {
1097
- const wasRemoved = allTriggers.find((trigger) => trigger.id === registered[i].trigger.id) == null;
1102
+ const wasRemoved = current.filter((trigger) => trigger.enabled).find((trigger) => trigger.id === registered[i].trigger.id) == null;
1098
1103
  if (wasRemoved) {
1099
1104
  const unregistered = registered.splice(i, 1)[0];
1100
1105
  await unregistered.activationCtx?.dispose();
@@ -1102,6 +1107,15 @@ var TriggerRegistry = class extends import_context4.Resource {
1102
1107
  }
1103
1108
  }
1104
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
+ });
1105
1119
  this.removed.emit({
1106
1120
  space,
1107
1121
  triggers: removed
@@ -1116,6 +1130,7 @@ var TriggerRegistry = class extends import_context4.Resource {
1116
1130
  // Annotate the CommonJS export names for ESM import in node:
1117
1131
  0 && (module.exports = {
1118
1132
  DevServer,
1133
+ FUNCTION_SCHEMA,
1119
1134
  FunctionDef,
1120
1135
  FunctionManifestSchema,
1121
1136
  FunctionRegistry,