@dxos/functions 0.5.3-main.59db342 → 0.5.3-main.5e3471c

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (97) hide show
  1. package/dist/lib/browser/chunk-4D4I3YMJ.mjs +86 -0
  2. package/dist/lib/browser/chunk-4D4I3YMJ.mjs.map +7 -0
  3. package/dist/lib/browser/index.mjs +915 -297
  4. package/dist/lib/browser/index.mjs.map +4 -4
  5. package/dist/lib/browser/meta.json +1 -1
  6. package/dist/lib/browser/types.mjs +14 -0
  7. package/dist/lib/browser/types.mjs.map +7 -0
  8. package/dist/lib/node/chunk-3UYUR5N5.cjs +103 -0
  9. package/dist/lib/node/chunk-3UYUR5N5.cjs.map +7 -0
  10. package/dist/lib/node/index.cjs +901 -290
  11. package/dist/lib/node/index.cjs.map +4 -4
  12. package/dist/lib/node/meta.json +1 -1
  13. package/dist/lib/node/types.cjs +35 -0
  14. package/dist/lib/node/types.cjs.map +7 -0
  15. package/dist/types/src/browser/index.d.ts +2 -0
  16. package/dist/types/src/browser/index.d.ts.map +1 -0
  17. package/dist/types/src/function/function-registry.d.ts +24 -0
  18. package/dist/types/src/function/function-registry.d.ts.map +1 -0
  19. package/dist/types/src/function/function-registry.test.d.ts +2 -0
  20. package/dist/types/src/function/function-registry.test.d.ts.map +1 -0
  21. package/dist/types/src/function/index.d.ts +2 -0
  22. package/dist/types/src/function/index.d.ts.map +1 -0
  23. package/dist/types/src/handler.d.ts +33 -12
  24. package/dist/types/src/handler.d.ts.map +1 -1
  25. package/dist/types/src/index.d.ts +3 -1
  26. package/dist/types/src/index.d.ts.map +1 -1
  27. package/dist/types/src/runtime/dev-server.d.ts +15 -7
  28. package/dist/types/src/runtime/dev-server.d.ts.map +1 -1
  29. package/dist/types/src/runtime/dev-server.test.d.ts +2 -0
  30. package/dist/types/src/runtime/dev-server.test.d.ts.map +1 -0
  31. package/dist/types/src/runtime/scheduler.d.ts +15 -15
  32. package/dist/types/src/runtime/scheduler.d.ts.map +1 -1
  33. package/dist/types/src/testing/functions-integration.test.d.ts +2 -0
  34. package/dist/types/src/testing/functions-integration.test.d.ts.map +1 -0
  35. package/dist/types/src/testing/index.d.ts +4 -0
  36. package/dist/types/src/testing/index.d.ts.map +1 -0
  37. package/dist/types/src/testing/setup.d.ts +5 -0
  38. package/dist/types/src/testing/setup.d.ts.map +1 -0
  39. package/dist/types/src/testing/test/handler.d.ts +4 -0
  40. package/dist/types/src/testing/test/handler.d.ts.map +1 -0
  41. package/dist/types/src/testing/test/index.d.ts +3 -0
  42. package/dist/types/src/testing/test/index.d.ts.map +1 -0
  43. package/dist/types/src/testing/types.d.ts +9 -0
  44. package/dist/types/src/testing/types.d.ts.map +1 -0
  45. package/dist/types/src/testing/util.d.ts +3 -0
  46. package/dist/types/src/testing/util.d.ts.map +1 -0
  47. package/dist/types/src/trigger/index.d.ts +2 -0
  48. package/dist/types/src/trigger/index.d.ts.map +1 -0
  49. package/dist/types/src/trigger/trigger-registry.d.ts +37 -0
  50. package/dist/types/src/trigger/trigger-registry.d.ts.map +1 -0
  51. package/dist/types/src/trigger/trigger-registry.test.d.ts +2 -0
  52. package/dist/types/src/trigger/trigger-registry.test.d.ts.map +1 -0
  53. package/dist/types/src/trigger/type/index.d.ts +5 -0
  54. package/dist/types/src/trigger/type/index.d.ts.map +1 -0
  55. package/dist/types/src/trigger/type/subscription-trigger.d.ts +4 -0
  56. package/dist/types/src/trigger/type/subscription-trigger.d.ts.map +1 -0
  57. package/dist/types/src/trigger/type/timer-trigger.d.ts +4 -0
  58. package/dist/types/src/trigger/type/timer-trigger.d.ts.map +1 -0
  59. package/dist/types/src/trigger/type/webhook-trigger.d.ts +4 -0
  60. package/dist/types/src/trigger/type/webhook-trigger.d.ts.map +1 -0
  61. package/dist/types/src/trigger/type/websocket-trigger.d.ts +13 -0
  62. package/dist/types/src/trigger/type/websocket-trigger.d.ts.map +1 -0
  63. package/dist/types/src/types.d.ts +215 -0
  64. package/dist/types/src/types.d.ts.map +1 -0
  65. package/dist/types/tools/schema.d.ts +2 -0
  66. package/dist/types/tools/schema.d.ts.map +1 -0
  67. package/package.json +38 -13
  68. package/schema/functions.json +211 -0
  69. package/src/browser/index.ts +5 -0
  70. package/src/function/function-registry.test.ts +105 -0
  71. package/src/function/function-registry.ts +90 -0
  72. package/src/function/index.ts +5 -0
  73. package/src/handler.ts +56 -26
  74. package/src/index.ts +3 -1
  75. package/src/runtime/dev-server.test.ts +60 -0
  76. package/src/runtime/dev-server.ts +104 -53
  77. package/src/runtime/scheduler.test.ts +159 -21
  78. package/src/runtime/scheduler.ts +87 -150
  79. package/src/testing/functions-integration.test.ts +100 -0
  80. package/src/testing/index.ts +7 -0
  81. package/src/testing/setup.ts +43 -0
  82. package/src/testing/test/handler.ts +15 -0
  83. package/src/testing/test/index.ts +7 -0
  84. package/src/testing/types.ts +9 -0
  85. package/src/testing/util.ts +16 -0
  86. package/src/trigger/index.ts +5 -0
  87. package/src/trigger/trigger-registry.test.ts +272 -0
  88. package/src/trigger/trigger-registry.ts +211 -0
  89. package/src/trigger/type/index.ts +8 -0
  90. package/src/trigger/type/subscription-trigger.ts +86 -0
  91. package/src/trigger/type/timer-trigger.ts +45 -0
  92. package/src/trigger/type/webhook-trigger.ts +48 -0
  93. package/src/trigger/type/websocket-trigger.ts +92 -0
  94. package/src/types.ts +115 -0
  95. package/dist/types/src/manifest.d.ts +0 -26
  96. package/dist/types/src/manifest.d.ts.map +0 -1
  97. package/src/manifest.ts +0 -42
@@ -7,18 +7,19 @@ import { getPort } from 'get-port-please';
7
7
  import type http from 'http';
8
8
  import { join } from 'node:path';
9
9
 
10
- import { Trigger } from '@dxos/async';
10
+ import { Event, Trigger } from '@dxos/async';
11
11
  import { type Client } from '@dxos/client';
12
+ import { Context } from '@dxos/context';
12
13
  import { invariant } from '@dxos/invariant';
13
14
  import { log } from '@dxos/log';
14
15
 
15
- import { type FunctionContext, type FunctionHandler, type Response } from '../handler';
16
- import { type FunctionDef, type FunctionManifest } from '../manifest';
16
+ import { type FunctionRegistry } from '../function';
17
+ import { type FunctionContext, type FunctionEvent, type FunctionHandler, type FunctionResponse } from '../handler';
18
+ import { type FunctionDef } from '../types';
17
19
 
18
20
  export type DevServerOptions = {
21
+ baseDir: string;
19
22
  port?: number;
20
- directory: string;
21
- manifest: FunctionManifest;
22
23
  reload?: boolean;
23
24
  dataDir?: string;
24
25
  };
@@ -27,20 +28,37 @@ export type DevServerOptions = {
27
28
  * Functions dev server provides a local HTTP server for testing functions.
28
29
  */
29
30
  export class DevServer {
31
+ private _ctx = createContext();
32
+
30
33
  // Function handlers indexed by name (URL path).
31
34
  private readonly _handlers: Record<string, { def: FunctionDef; handler: FunctionHandler<any> }> = {};
32
35
 
33
36
  private _server?: http.Server;
34
37
  private _port?: number;
35
- private _registrationId?: string;
38
+ private _functionServiceRegistration?: string;
36
39
  private _proxy?: string;
37
40
  private _seq = 0;
38
41
 
39
- // prettier-ignore
42
+ public readonly update = new Event<number>();
43
+
40
44
  constructor(
41
45
  private readonly _client: Client,
46
+ private readonly _functionsRegistry: FunctionRegistry,
42
47
  private readonly _options: DevServerOptions,
43
- ) {}
48
+ ) {
49
+ // TODO(burdon): Add/remove listener in start/stop.
50
+ this._functionsRegistry.registered.on(async ({ added }) => {
51
+ added.forEach((def) => this._load(def));
52
+ await this._safeUpdateRegistration();
53
+ log('new functions loaded', { added });
54
+ });
55
+ }
56
+
57
+ get stats() {
58
+ return {
59
+ seq: this._seq,
60
+ };
61
+ }
44
62
 
45
63
  get endpoint() {
46
64
  invariant(this._port);
@@ -55,30 +73,26 @@ export class DevServer {
55
73
  return Object.values(this._handlers);
56
74
  }
57
75
 
58
- async initialize() {
59
- for (const def of this._options.manifest.functions) {
60
- try {
61
- await this._load(def);
62
- } catch (err) {
63
- log.error('parsing function (check manifest)', err);
64
- }
65
- }
66
- }
67
-
68
76
  async start() {
77
+ invariant(!this._server);
78
+ log.info('starting...');
79
+ this._ctx = createContext();
80
+
81
+ // TODO(burdon): Move to hono.
69
82
  const app = express();
70
83
  app.use(express.json());
71
84
 
72
- app.post('/:name', async (req, res) => {
73
- const { name } = req.params;
85
+ app.post('/:path', async (req, res) => {
86
+ const { path } = req.params;
74
87
  try {
75
- log.info('calling', { name });
88
+ log.info('calling', { path });
76
89
  if (this._options.reload) {
77
- const { def } = this._handlers[name];
90
+ const { def } = this._handlers['/' + path];
78
91
  await this._load(def, true);
79
92
  }
80
93
 
81
- res.statusCode = await this._invoke(name, req.body);
94
+ // TODO(burdon): Get function context.
95
+ res.statusCode = await this.invoke('/' + path, req.body);
82
96
  res.end();
83
97
  } catch (err: any) {
84
98
  log.catch(err);
@@ -94,80 +108,117 @@ export class DevServer {
94
108
  // Register functions.
95
109
  const { registrationId, endpoint } = await this._client.services.services.FunctionRegistryService!.register({
96
110
  endpoint: this.endpoint,
97
- functions: this.functions.map(({ def: { name } }) => ({ name })),
98
111
  });
99
112
 
100
- log.info('registered', { registrationId, endpoint });
101
- this._registrationId = registrationId;
113
+ log.info('registered', { endpoint });
102
114
  this._proxy = endpoint;
115
+ this._functionServiceRegistration = registrationId;
116
+
117
+ // Open after registration, so that it can be updated with the list of function definitions.
118
+ await this._functionsRegistry.open(this._ctx);
103
119
  } catch (err: any) {
104
120
  await this.stop();
105
121
  throw new Error('FunctionRegistryService not available (check plugin is configured).');
106
122
  }
123
+
124
+ log.info('started', { port: this._port });
107
125
  }
108
126
 
109
127
  async stop() {
128
+ invariant(this._server);
129
+ log.info('stopping...');
130
+
110
131
  const trigger = new Trigger();
111
- this._server?.close(async () => {
112
- if (this._registrationId) {
113
- await this._client.services.services.FunctionRegistryService!.unregister({
114
- registrationId: this._registrationId,
115
- });
132
+ this._server.close(async () => {
133
+ log.info('server stopped');
134
+ try {
135
+ if (this._functionServiceRegistration) {
136
+ invariant(this._client.services.services.FunctionRegistryService);
137
+ await this._client.services.services.FunctionRegistryService.unregister({
138
+ registrationId: this._functionServiceRegistration,
139
+ });
140
+
141
+ log.info('unregistered', { registrationId: this._functionServiceRegistration });
142
+ this._functionServiceRegistration = undefined;
143
+ this._proxy = undefined;
144
+ }
116
145
 
117
- log.info('unregistered', { registrationId: this._registrationId });
118
- this._registrationId = undefined;
119
- this._proxy = undefined;
146
+ trigger.wake();
147
+ } catch (err) {
148
+ trigger.throw(err as Error);
120
149
  }
121
-
122
- trigger.wake();
123
150
  });
124
151
 
125
152
  await trigger.wait();
126
153
  this._port = undefined;
127
154
  this._server = undefined;
155
+ log.info('stopped');
128
156
  }
129
157
 
130
158
  /**
131
159
  * Load function.
132
160
  */
133
- private async _load(def: FunctionDef, flush = false) {
134
- const { id, name, handler } = def;
135
- const path = join(this._options.directory, handler);
136
- log.info('loading', { id });
161
+ private async _load(def: FunctionDef, force?: boolean | undefined) {
162
+ const { uri, route, handler } = def;
163
+ const filePath = join(this._options.baseDir, handler);
164
+ log.info('loading', { uri, force });
137
165
 
138
166
  // Remove from cache.
139
- if (flush) {
167
+ if (force) {
140
168
  Object.keys(require.cache)
141
- .filter((key) => key.startsWith(path))
142
- .forEach((key) => delete require.cache[key]);
169
+ .filter((key) => key.startsWith(filePath))
170
+ .forEach((key) => {
171
+ delete require.cache[key];
172
+ });
143
173
  }
144
174
 
175
+ // TODO(burdon): Import types.
145
176
  // eslint-disable-next-line @typescript-eslint/no-var-requires
146
- const module = require(path);
177
+ const module = require(filePath);
147
178
  if (typeof module.default !== 'function') {
148
- throw new Error(`Handler must export default function: ${id}`);
179
+ throw new Error(`Handler must export default function: ${uri}`);
149
180
  }
150
181
 
151
- this._handlers[name] = { def, handler: module.default };
182
+ this._handlers[route] = { def, handler: module.default };
183
+ }
184
+
185
+ private async _safeUpdateRegistration(): Promise<void> {
186
+ invariant(this._functionServiceRegistration);
187
+ try {
188
+ await this._client.services.services.FunctionRegistryService!.updateRegistration({
189
+ registrationId: this._functionServiceRegistration,
190
+ functions: this.functions.map(({ def: { id, route } }) => ({ id, route })),
191
+ });
192
+ } catch (err) {
193
+ log.catch(err);
194
+ }
152
195
  }
153
196
 
154
197
  /**
155
- * Invoke function handler.
198
+ * Invoke function.
156
199
  */
157
- private async _invoke(name: string, event: any) {
200
+ public async invoke(path: string, data: any): Promise<number> {
158
201
  const seq = ++this._seq;
159
202
  const now = Date.now();
160
203
 
161
- log.info('req', { seq, name });
162
- const { handler } = this._handlers[name];
204
+ log.info('req', { seq, path });
205
+ const statusCode = await this._invoke(path, { data });
206
+
207
+ log.info('res', { seq, path, statusCode, duration: Date.now() - now });
208
+ this.update.emit(statusCode);
209
+ return statusCode;
210
+ }
163
211
 
212
+ private async _invoke(path: string, event: FunctionEvent) {
213
+ const { handler } = this._handlers[path] ?? {};
214
+ invariant(handler, `invalid path: ${path}`);
164
215
  const context: FunctionContext = {
165
216
  client: this._client,
166
217
  dataDir: this._options.dataDir,
167
218
  };
168
219
 
169
220
  let statusCode = 200;
170
- const response: Response = {
221
+ const response: FunctionResponse = {
171
222
  status: (code: number) => {
172
223
  statusCode = code;
173
224
  return response;
@@ -175,8 +226,8 @@ export class DevServer {
175
226
  };
176
227
 
177
228
  await handler({ context, event, response });
178
- log.info('res', { seq, name, statusCode, duration: Date.now() - now });
179
-
180
229
  return statusCode;
181
230
  }
182
231
  }
232
+
233
+ const createContext = () => new Context({ name: 'DevServer' });
@@ -3,56 +3,194 @@
3
3
  //
4
4
 
5
5
  import { expect } from 'chai';
6
+ import WebSocket from 'ws';
6
7
 
7
8
  import { Trigger } from '@dxos/async';
8
- import { Client } from '@dxos/client';
9
+ import { type Client } from '@dxos/client';
9
10
  import { TestBuilder } from '@dxos/client/testing';
11
+ import { create } from '@dxos/echo-schema';
10
12
  import { describe, test } from '@dxos/test';
11
13
 
12
- import { Scheduler } from './scheduler';
13
- import { type FunctionManifest } from '../manifest';
14
+ import { Scheduler, type SchedulerOptions } from './scheduler';
15
+ import { FunctionRegistry } from '../function';
16
+ import { createInitializedClients, TestType, triggerWebhook } from '../testing';
17
+ import { TriggerRegistry } from '../trigger';
18
+ import { type FunctionManifest } from '../types';
14
19
 
20
+ // TODO(burdon): Test we can add and remove triggers.
15
21
  describe('scheduler', () => {
16
- test.only('callback', async () => {
17
- const testBuilder = new TestBuilder();
18
- const client = new Client({ services: testBuilder.createLocal() });
19
- await client.initialize();
20
- await client.halo.createIdentity();
22
+ let testBuilder: TestBuilder;
23
+ let client: Client;
24
+ before(async () => {
25
+ testBuilder = new TestBuilder();
26
+ client = (await createInitializedClients(testBuilder, 1))[0];
27
+ });
28
+ after(async () => {
29
+ await testBuilder.destroy();
30
+ });
31
+
32
+ const createScheduler = (callback: SchedulerOptions['callback']) => {
33
+ const scheduler = new Scheduler(new FunctionRegistry(client), new TriggerRegistry(client), { callback });
34
+ after(async () => {
35
+ await scheduler.stop();
36
+ });
37
+
38
+ return scheduler;
39
+ };
21
40
 
41
+ test('timer', async () => {
22
42
  const manifest: FunctionManifest = {
23
43
  functions: [
24
44
  {
25
- id: 'example.com/function/test',
26
- name: 'test',
45
+ uri: 'example.com/function/test',
46
+ route: '/test',
27
47
  handler: 'test',
28
48
  },
29
49
  ],
30
50
  triggers: [
31
51
  {
32
52
  function: 'example.com/function/test',
33
- schedule: '0/1 * * * * *', // Every 1s.
53
+ enabled: true,
54
+ spec: {
55
+ type: 'timer',
56
+ cron: '0/1 * * * * *', // Every 1s.
57
+ },
34
58
  },
35
59
  ],
36
60
  };
37
61
 
38
62
  let count = 0;
39
63
  const done = new Trigger();
40
- const scheduler = new Scheduler(client, manifest, {
41
- callback: async () => {
42
- if (++count === 3) {
43
- done.wake();
44
- }
64
+ const scheduler = createScheduler(async () => {
65
+ if (++count === 3) {
66
+ done.wake();
67
+ }
68
+ });
69
+ await scheduler.register(client.spaces.default, manifest);
70
+ await scheduler.start();
71
+
72
+ await done.wait({ timeout: 5_000 });
73
+ expect(count).to.equal(3);
74
+ });
45
75
 
46
- return 200;
47
- },
76
+ test('webhook', async () => {
77
+ const manifest: FunctionManifest = {
78
+ functions: [
79
+ {
80
+ uri: 'example.com/function/test',
81
+ route: '/test',
82
+ handler: 'test',
83
+ },
84
+ ],
85
+ triggers: [
86
+ {
87
+ function: 'example.com/function/test',
88
+ enabled: true,
89
+ spec: {
90
+ type: 'webhook',
91
+ method: 'GET',
92
+ },
93
+ },
94
+ ],
95
+ };
96
+
97
+ const done = new Trigger();
98
+ const scheduler = createScheduler(async () => {
99
+ done.wake();
48
100
  });
101
+ const space = await client.spaces.create();
102
+ await scheduler.register(space, manifest);
103
+ await scheduler.start();
104
+
105
+ setTimeout(async () => triggerWebhook(space, manifest.functions![0].uri));
49
106
 
107
+ await done.wait();
108
+ });
109
+
110
+ test('websocket', async () => {
111
+ const manifest: FunctionManifest = {
112
+ functions: [
113
+ {
114
+ uri: 'example.com/function/test',
115
+ route: '/test',
116
+ handler: 'test',
117
+ },
118
+ ],
119
+ triggers: [
120
+ {
121
+ function: 'example.com/function/test',
122
+ enabled: true,
123
+ spec: {
124
+ type: 'websocket',
125
+ // url: 'https://hub.dxos.network/api/mailbox/test',
126
+ url: 'http://localhost:8081',
127
+ init: {
128
+ type: 'sync',
129
+ },
130
+ },
131
+ },
132
+ ],
133
+ };
134
+
135
+ const done = new Trigger();
136
+ const scheduler = createScheduler(async () => {
137
+ done.wake();
138
+ });
139
+ await scheduler.register(client.spaces.default, manifest);
50
140
  await scheduler.start();
51
141
 
142
+ // Test server.
143
+ setTimeout(() => {
144
+ const wss = new WebSocket.Server({ port: 8081 });
145
+ wss.on('connection', (ws: WebSocket) => {
146
+ ws.on('message', (data) => {
147
+ const info = JSON.parse(new TextDecoder().decode(data as ArrayBuffer));
148
+ expect(info.type).to.equal('sync');
149
+ done.wake();
150
+ });
151
+ });
152
+ }, 500);
153
+
52
154
  await done.wait();
53
- expect(count).to.equal(3);
155
+ });
156
+
157
+ test('subscription', async () => {
158
+ const manifest: FunctionManifest = {
159
+ functions: [
160
+ {
161
+ uri: 'example.com/function/test',
162
+ route: '/test',
163
+ handler: 'test',
164
+ },
165
+ ],
166
+ triggers: [
167
+ {
168
+ function: 'example.com/function/test',
169
+ enabled: true,
170
+ spec: {
171
+ type: 'subscription',
172
+ filter: [{ type: TestType.typename }],
173
+ },
174
+ },
175
+ ],
176
+ };
177
+
178
+ let count = 0;
179
+ const done = new Trigger();
180
+ const scheduler = createScheduler(async () => {
181
+ if (++count === 1) {
182
+ done.wake();
183
+ }
184
+ });
185
+ await scheduler.register(client.spaces.default, manifest);
186
+ await scheduler.start();
54
187
 
55
- await scheduler.stop();
56
- await client.destroy();
188
+ setTimeout(() => {
189
+ const space = client.spaces.default;
190
+ const object = create(TestType, { title: 'Hello world!' });
191
+ space.db.add(object);
192
+ }, 100);
193
+
194
+ await done.wait();
57
195
  });
58
196
  });