@dxos/functions 0.5.3-main.3456876 → 0.5.3-main.3b535c7

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.
@@ -44,9 +44,7 @@ var import_async = require("@dxos/async");
44
44
  var import_invariant = require("@dxos/invariant");
45
45
  var import_log2 = require("@dxos/log");
46
46
  var import_cron = require("cron");
47
- var import_get_port_please2 = require("get-port-please");
48
47
  var import_node_http = __toESM(require("node:http"));
49
- var import_node_path2 = __toESM(require("node:path"));
50
48
  var import_ws = __toESM(require("ws"));
51
49
  var import_types = require("@braneframe/types");
52
50
  var import_async2 = require("@dxos/async");
@@ -67,9 +65,9 @@ var __dxlog_file = "/home/runner/work/dxos/dxos/packages/core/functions/src/hand
67
65
  var subscriptionHandler = (handler) => {
68
66
  return ({ event, context, ...rest }) => {
69
67
  const { client } = context;
70
- const space = event.spaceKey ? client.spaces.get(import_client.PublicKey.from(event.spaceKey)) : void 0;
68
+ const space = event.space ? client.spaces.get(import_client.PublicKey.from(event.space)) : void 0;
71
69
  const objects = space && event.objects?.map((id) => space.db.getObjectById(id)).filter(import_util.nonNullable);
72
- if (!!event.spaceKey && !space) {
70
+ if (!!event.space && !space) {
73
71
  import_log.log.warn("invalid space", {
74
72
  event
75
73
  }, {
@@ -107,17 +105,11 @@ var DevServer = class {
107
105
  this._options = _options;
108
106
  this._handlers = {};
109
107
  this._seq = 0;
110
- this.update = new import_async.Event();
111
- }
112
- get stats() {
113
- return {
114
- seq: this._seq
115
- };
116
108
  }
117
109
  get endpoint() {
118
110
  (0, import_invariant.invariant)(this._port, void 0, {
119
111
  F: __dxlog_file2,
120
- L: 54,
112
+ L: 46,
121
113
  S: this,
122
114
  A: [
123
115
  "this._port",
@@ -139,7 +131,7 @@ var DevServer = class {
139
131
  } catch (err) {
140
132
  import_log2.log.error("parsing function (check manifest)", err, {
141
133
  F: __dxlog_file2,
142
- L: 71,
134
+ L: 63,
143
135
  S: this,
144
136
  C: (f, a) => f(...a)
145
137
  });
@@ -147,44 +139,29 @@ var DevServer = class {
147
139
  }
148
140
  }
149
141
  async start() {
150
- (0, import_invariant.invariant)(!this._server, void 0, {
151
- F: __dxlog_file2,
152
- L: 77,
153
- S: this,
154
- A: [
155
- "!this._server",
156
- ""
157
- ]
158
- });
159
- import_log2.log.info("starting...", void 0, {
160
- F: __dxlog_file2,
161
- L: 78,
162
- S: this,
163
- C: (f, a) => f(...a)
164
- });
165
142
  const app = (0, import_express.default)();
166
143
  app.use(import_express.default.json());
167
- app.post("/:path", async (req, res) => {
168
- const { path: path2 } = req.params;
144
+ app.post("/:name", async (req, res) => {
145
+ const { name } = req.params;
169
146
  try {
170
147
  import_log2.log.info("calling", {
171
- path: path2
148
+ name
172
149
  }, {
173
150
  F: __dxlog_file2,
174
- L: 87,
151
+ L: 75,
175
152
  S: this,
176
153
  C: (f, a) => f(...a)
177
154
  });
178
155
  if (this._options.reload) {
179
- const { def } = this._handlers["/" + path2];
156
+ const { def } = this._handlers[name];
180
157
  await this._load(def, true);
181
158
  }
182
- res.statusCode = await this.invoke("/" + path2, req.body);
159
+ res.statusCode = await this._invoke(name, req.body);
183
160
  res.end();
184
161
  } catch (err) {
185
162
  import_log2.log.catch(err, void 0, {
186
163
  F: __dxlog_file2,
187
- L: 97,
164
+ L: 84,
188
165
  S: this,
189
166
  C: (f, a) => f(...a)
190
167
  });
@@ -204,168 +181,92 @@ var DevServer = class {
204
181
  try {
205
182
  const { registrationId, endpoint } = await this._client.services.services.FunctionRegistryService.register({
206
183
  endpoint: this.endpoint,
207
- functions: this.functions.map(({ def: { id, path: path2 } }) => ({
208
- id,
209
- path: path2
184
+ functions: this.functions.map(({ def: { name } }) => ({
185
+ name
210
186
  }))
211
187
  });
212
188
  import_log2.log.info("registered", {
189
+ registrationId,
213
190
  endpoint
214
191
  }, {
215
192
  F: __dxlog_file2,
216
- L: 113,
193
+ L: 100,
217
194
  S: this,
218
195
  C: (f, a) => f(...a)
219
196
  });
197
+ this._registrationId = registrationId;
220
198
  this._proxy = endpoint;
221
- this._functionServiceRegistration = registrationId;
222
199
  } catch (err) {
223
200
  await this.stop();
224
201
  throw new Error("FunctionRegistryService not available (check plugin is configured).");
225
202
  }
226
- import_log2.log.info("started", {
227
- port: this._port
228
- }, {
229
- F: __dxlog_file2,
230
- L: 121,
231
- S: this,
232
- C: (f, a) => f(...a)
233
- });
234
203
  }
235
204
  async stop() {
236
- (0, import_invariant.invariant)(this._server, void 0, {
237
- F: __dxlog_file2,
238
- L: 125,
239
- S: this,
240
- A: [
241
- "this._server",
242
- ""
243
- ]
244
- });
245
- import_log2.log.info("stopping...", void 0, {
246
- F: __dxlog_file2,
247
- L: 126,
248
- S: this,
249
- C: (f, a) => f(...a)
250
- });
251
205
  const trigger = new import_async.Trigger();
252
- this._server.close(async () => {
253
- import_log2.log.info("server stopped", void 0, {
254
- F: __dxlog_file2,
255
- L: 130,
256
- S: this,
257
- C: (f, a) => f(...a)
258
- });
259
- try {
260
- if (this._functionServiceRegistration) {
261
- (0, import_invariant.invariant)(this._client.services.services.FunctionRegistryService, void 0, {
262
- F: __dxlog_file2,
263
- L: 133,
264
- S: this,
265
- A: [
266
- "this._client.services.services.FunctionRegistryService",
267
- ""
268
- ]
269
- });
270
- await this._client.services.services.FunctionRegistryService.unregister({
271
- registrationId: this._functionServiceRegistration
272
- });
273
- import_log2.log.info("unregistered", {
274
- registrationId: this._functionServiceRegistration
275
- }, {
276
- F: __dxlog_file2,
277
- L: 138,
278
- S: this,
279
- C: (f, a) => f(...a)
280
- });
281
- this._functionServiceRegistration = void 0;
282
- this._proxy = void 0;
283
- }
284
- trigger.wake();
285
- } catch (err) {
286
- trigger.throw(err);
206
+ this._server?.close(async () => {
207
+ if (this._registrationId) {
208
+ await this._client.services.services.FunctionRegistryService.unregister({
209
+ registrationId: this._registrationId
210
+ });
211
+ import_log2.log.info("unregistered", {
212
+ registrationId: this._registrationId
213
+ }, {
214
+ F: __dxlog_file2,
215
+ L: 117,
216
+ S: this,
217
+ C: (f, a) => f(...a)
218
+ });
219
+ this._registrationId = void 0;
220
+ this._proxy = void 0;
287
221
  }
222
+ trigger.wake();
288
223
  });
289
224
  await trigger.wait();
290
225
  this._port = void 0;
291
226
  this._server = void 0;
292
- import_log2.log.info("stopped", void 0, {
293
- F: __dxlog_file2,
294
- L: 152,
295
- S: this,
296
- C: (f, a) => f(...a)
297
- });
298
227
  }
299
228
  /**
300
229
  * Load function.
301
230
  */
302
- async _load(def, force = false) {
303
- const { id, path: path2, handler } = def;
304
- const filePath = (0, import_node_path.join)(this._options.baseDir, handler);
231
+ async _load(def, flush = false) {
232
+ const { id, name, handler } = def;
233
+ const path = (0, import_node_path.join)(this._options.directory, handler);
305
234
  import_log2.log.info("loading", {
306
- id,
307
- force
235
+ id
308
236
  }, {
309
237
  F: __dxlog_file2,
310
- L: 161,
238
+ L: 136,
311
239
  S: this,
312
240
  C: (f, a) => f(...a)
313
241
  });
314
- if (force) {
315
- Object.keys(__require.cache).filter((key) => key.startsWith(filePath)).forEach((key) => {
316
- delete __require.cache[key];
317
- });
242
+ if (flush) {
243
+ Object.keys(__require.cache).filter((key) => key.startsWith(path)).forEach((key) => delete __require.cache[key]);
318
244
  }
319
- const module2 = __require(filePath);
245
+ const module2 = __require(path);
320
246
  if (typeof module2.default !== "function") {
321
247
  throw new Error(`Handler must export default function: ${id}`);
322
248
  }
323
- this._handlers[path2] = {
249
+ this._handlers[name] = {
324
250
  def,
325
251
  handler: module2.default
326
252
  };
327
253
  }
328
254
  /**
329
- * Invoke function.
255
+ * Invoke function handler.
330
256
  */
331
- async invoke(path2, data) {
257
+ async _invoke(name, event) {
332
258
  const seq = ++this._seq;
333
259
  const now = Date.now();
334
260
  import_log2.log.info("req", {
335
261
  seq,
336
- path: path2
337
- }, {
338
- F: __dxlog_file2,
339
- L: 188,
340
- S: this,
341
- C: (f, a) => f(...a)
342
- });
343
- const statusCode = await this._invoke(path2, data);
344
- import_log2.log.info("res", {
345
- seq,
346
- path: path2,
347
- statusCode,
348
- duration: Date.now() - now
262
+ name
349
263
  }, {
350
264
  F: __dxlog_file2,
351
- L: 191,
265
+ L: 161,
352
266
  S: this,
353
267
  C: (f, a) => f(...a)
354
268
  });
355
- this.update.emit(statusCode);
356
- return statusCode;
357
- }
358
- async _invoke(path2, event) {
359
- const { handler } = this._handlers[path2] ?? {};
360
- (0, import_invariant.invariant)(handler, `invalid path: ${path2}`, {
361
- F: __dxlog_file2,
362
- L: 198,
363
- S: this,
364
- A: [
365
- "handler",
366
- "`invalid path: ${path}`"
367
- ]
368
- });
269
+ const { handler } = this._handlers[name];
369
270
  const context = {
370
271
  client: this._client,
371
272
  dataDir: this._options.dataDir
@@ -382,6 +283,17 @@ var DevServer = class {
382
283
  event,
383
284
  response
384
285
  });
286
+ import_log2.log.info("res", {
287
+ seq,
288
+ name,
289
+ statusCode,
290
+ duration: Date.now() - now
291
+ }, {
292
+ F: __dxlog_file2,
293
+ L: 178,
294
+ S: this,
295
+ C: (f, a) => f(...a)
296
+ });
385
297
  return statusCode;
386
298
  }
387
299
  };
@@ -391,13 +303,7 @@ var Scheduler = class {
391
303
  this._client = _client;
392
304
  this._manifest = _manifest;
393
305
  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
- }, []);
306
+ this._mounts = new import_util2.ComplexMap(({ id, spaceKey }) => `${spaceKey.toHex()}:${id}`);
401
307
  }
402
308
  async start() {
403
309
  this._client.spaces.subscribe(async (spaces) => {
@@ -414,18 +320,15 @@ var Scheduler = class {
414
320
  await this.unmount(id, spaceKey);
415
321
  }
416
322
  }
417
- /**
418
- * Mount trigger.
419
- */
420
323
  async mount(ctx, space, trigger) {
421
324
  const key = {
422
- spaceKey: space.key,
423
- id: trigger.function
325
+ id: trigger.function,
326
+ spaceKey: space.key
424
327
  };
425
328
  const def = this._manifest.functions.find((config) => config.id === trigger.function);
426
329
  (0, import_invariant2.invariant)(def, `Function not found: ${trigger.function}`, {
427
330
  F: __dxlog_file3,
428
- L: 83,
331
+ L: 72,
429
332
  S: this,
430
333
  A: [
431
334
  "def",
@@ -443,7 +346,7 @@ var Scheduler = class {
443
346
  trigger
444
347
  }, {
445
348
  F: __dxlog_file3,
446
- L: 89,
349
+ L: 78,
447
350
  S: this,
448
351
  C: (f, a) => f(...a)
449
352
  });
@@ -478,62 +381,44 @@ var Scheduler = class {
478
381
  // TODO(burdon): Pass in Space key (common context).
479
382
  async _execFunction(def, data) {
480
383
  try {
481
- let status = 0;
384
+ import_log3.log.info("exec", {
385
+ function: def.id
386
+ }, {
387
+ F: __dxlog_file3,
388
+ L: 117,
389
+ S: this,
390
+ C: (f, a) => f(...a)
391
+ });
482
392
  const { endpoint, callback } = this._options;
483
393
  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
488
- }, {
489
- F: __dxlog_file3,
490
- L: 133,
491
- S: this,
492
- C: (f, a) => f(...a)
493
- });
494
- const response = await fetch(url, {
394
+ await fetch(`${this._options.endpoint}/${def.name}`, {
495
395
  method: "POST",
496
396
  headers: {
497
397
  "Content-Type": "application/json"
498
398
  },
499
399
  body: JSON.stringify(data)
500
400
  });
501
- status = response.status;
502
401
  } else if (callback) {
503
- import_log3.log.info("exec", {
504
- function: def.id
505
- }, {
506
- F: __dxlog_file3,
507
- L: 144,
508
- S: this,
509
- C: (f, a) => f(...a)
510
- });
511
- status = await callback(data) ?? 200;
512
- }
513
- if (status && status >= 400) {
514
- throw new Error(`Response: ${status}`);
402
+ await callback(data);
515
403
  }
516
404
  import_log3.log.info("done", {
517
- function: def.id,
518
- status
405
+ function: def.id
519
406
  }, {
520
407
  F: __dxlog_file3,
521
- L: 154,
408
+ L: 133,
522
409
  S: this,
523
410
  C: (f, a) => f(...a)
524
411
  });
525
- return status;
526
412
  } catch (err) {
527
413
  import_log3.log.error("error", {
528
414
  function: def.id,
529
415
  error: err.message
530
416
  }, {
531
417
  F: __dxlog_file3,
532
- L: 157,
418
+ L: 135,
533
419
  S: this,
534
420
  C: (f, a) => f(...a)
535
421
  });
536
- return 500;
537
422
  }
538
423
  }
539
424
  //
@@ -548,14 +433,14 @@ var Scheduler = class {
548
433
  trigger
549
434
  }, {
550
435
  F: __dxlog_file3,
551
- L: 170,
436
+ L: 147,
552
437
  S: this,
553
438
  C: (f, a) => f(...a)
554
439
  });
555
440
  const { cron } = trigger;
556
441
  const task = new import_async2.DeferredTask(ctx, async () => {
557
442
  await this._execFunction(def, {
558
- spaceKey: space.key
443
+ space: space.key
559
444
  });
560
445
  });
561
446
  let last = 0;
@@ -574,7 +459,7 @@ var Scheduler = class {
574
459
  delta
575
460
  }, {
576
461
  F: __dxlog_file3,
577
- L: 190,
462
+ L: 167,
578
463
  S: this,
579
464
  C: (f, a) => f(...a)
580
465
  });
@@ -593,33 +478,25 @@ var Scheduler = class {
593
478
  trigger
594
479
  }, {
595
480
  F: __dxlog_file3,
596
- L: 203,
481
+ L: 180,
597
482
  S: this,
598
483
  C: (f, a) => f(...a)
599
484
  });
485
+ const { port } = trigger;
600
486
  const server = import_node_http.default.createServer(async (req, res) => {
601
- if (req.method !== trigger.method) {
602
- res.statusCode = 405;
603
- return res.end();
604
- }
605
- res.statusCode = await this._execFunction(def, {
606
- spaceKey: space.key
487
+ await this._execFunction(def, {
488
+ space: space.key
607
489
  });
608
- res.end();
609
- });
610
- const port = await (0, import_get_port_please2.getPort)({
611
- random: true
612
490
  });
613
491
  server.listen(port, () => {
614
492
  import_log3.log.info("started webhook", {
615
493
  port
616
494
  }, {
617
495
  F: __dxlog_file3,
618
- L: 226,
496
+ L: 189,
619
497
  S: this,
620
498
  C: (f, a) => f(...a)
621
499
  });
622
- trigger.port = port;
623
500
  });
624
501
  ctx.onDispose(() => {
625
502
  server.close();
@@ -627,7 +504,6 @@ var Scheduler = class {
627
504
  }
628
505
  /**
629
506
  * Websocket.
630
- * NOTE: The port must be unique, so the same hook cannot be used for multiple spaces.
631
507
  */
632
508
  async _createWebsocket(ctx, space, def, trigger, options = {
633
509
  retryDelay: 2,
@@ -638,7 +514,7 @@ var Scheduler = class {
638
514
  trigger
639
515
  }, {
640
516
  F: __dxlog_file3,
641
- L: 252,
517
+ L: 213,
642
518
  S: this,
643
519
  C: (f, a) => f(...a)
644
520
  });
@@ -653,7 +529,7 @@ var Scheduler = class {
653
529
  url
654
530
  }, {
655
531
  F: __dxlog_file3,
656
- L: 262,
532
+ L: 223,
657
533
  S: this,
658
534
  C: (f, a) => f(...a)
659
535
  });
@@ -662,14 +538,12 @@ var Scheduler = class {
662
538
  }
663
539
  open.wake(true);
664
540
  },
665
- // TODO(burdon): Config retry if server closes?
666
- onclose: (event) => {
541
+ onclose: () => {
667
542
  import_log3.log.info("closed", {
668
- url,
669
- code: event.code
543
+ url
670
544
  }, {
671
545
  F: __dxlog_file3,
672
- L: 272,
546
+ L: 232,
673
547
  S: this,
674
548
  C: (f, a) => f(...a)
675
549
  });
@@ -680,7 +554,7 @@ var Scheduler = class {
680
554
  url
681
555
  }, {
682
556
  F: __dxlog_file3,
683
- L: 277,
557
+ L: 237,
684
558
  S: this,
685
559
  C: (f, a) => f(...a)
686
560
  });
@@ -689,7 +563,7 @@ var Scheduler = class {
689
563
  try {
690
564
  const data = JSON.parse(new TextDecoder().decode(event.data));
691
565
  await this._execFunction(def, {
692
- spaceKey: space.key,
566
+ space: space.key,
693
567
  data
694
568
  });
695
569
  } catch (err) {
@@ -697,7 +571,7 @@ var Scheduler = class {
697
571
  url
698
572
  }, {
699
573
  F: __dxlog_file3,
700
- L: 285,
574
+ L: 245,
701
575
  S: this,
702
576
  C: (f, a) => f(...a)
703
577
  });
@@ -714,7 +588,7 @@ var Scheduler = class {
714
588
  attempt
715
589
  }, {
716
590
  F: __dxlog_file3,
717
- L: 296,
591
+ L: 256,
718
592
  S: this,
719
593
  C: (f, a) => f(...a)
720
594
  });
@@ -735,14 +609,14 @@ var Scheduler = class {
735
609
  trigger
736
610
  }, {
737
611
  F: __dxlog_file3,
738
- L: 311,
612
+ L: 271,
739
613
  S: this,
740
614
  C: (f, a) => f(...a)
741
615
  });
742
616
  const objectIds = /* @__PURE__ */ new Set();
743
617
  const task = new import_async2.DeferredTask(ctx, async () => {
744
618
  await this._execFunction(def, {
745
- spaceKey: space.key,
619
+ space: space.key,
746
620
  objects: Array.from(objectIds)
747
621
  });
748
622
  });
@@ -753,7 +627,7 @@ var Scheduler = class {
753
627
  updated: updated.length
754
628
  }, {
755
629
  F: __dxlog_file3,
756
- L: 321,
630
+ L: 281,
757
631
  S: this,
758
632
  C: (f, a) => f(...a)
759
633
  });
@@ -774,7 +648,7 @@ var Scheduler = class {
774
648
  objects: objects.length
775
649
  }, {
776
650
  F: __dxlog_file3,
777
- L: 342,
651
+ L: 301,
778
652
  S: this,
779
653
  C: (f, a) => f(...a)
780
654
  });
@@ -798,11 +672,9 @@ var Scheduler = class {
798
672
  var TimerTriggerSchema = S.struct({
799
673
  cron: S.string
800
674
  });
801
- var WebhookTriggerSchema = S.mutable(S.struct({
802
- method: S.string,
803
- // Assigned port.
804
- port: S.optional(S.number)
805
- }));
675
+ var WebhookTriggerSchema = S.struct({
676
+ port: S.number
677
+ });
806
678
  var WebsocketTriggerSchema = S.struct({
807
679
  url: S.string,
808
680
  init: S.optional(S.record(S.string, S.any))
@@ -823,9 +695,6 @@ var SubscriptionTriggerSchema = S.struct({
823
695
  });
824
696
  var FunctionTriggerSchema = S.struct({
825
697
  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
698
  timer: S.optional(TimerTriggerSchema),
830
699
  webhook: S.optional(WebhookTriggerSchema),
831
700
  websocket: S.optional(WebsocketTriggerSchema),
@@ -833,15 +702,14 @@ var FunctionTriggerSchema = S.struct({
833
702
  });
834
703
  var FunctionDefSchema = S.struct({
835
704
  id: S.string,
836
- // name: S.string,
837
705
  description: S.optional(S.string),
838
- path: S.string,
839
- // TODO(burdon): NPM/GitHub/Docker/CF URL?
706
+ name: S.string,
707
+ // TODO(burdon): NPM/GitHub URL?
840
708
  handler: S.string
841
709
  });
842
710
  var FunctionManifestSchema = S.struct({
843
711
  functions: S.mutable(S.array(FunctionDefSchema)),
844
- triggers: S.optional(S.mutable(S.array(FunctionTriggerSchema)))
712
+ triggers: S.mutable(S.array(FunctionTriggerSchema))
845
713
  });
846
714
  // Annotate the CommonJS export names for ESM import in node:
847
715
  0 && (module.exports = {