@dxos/functions 0.5.3-main.83788ff → 0.5.3-main.8ffbbae

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 (79) hide show
  1. package/dist/lib/browser/index.mjs +479 -744
  2. package/dist/lib/browser/index.mjs.map +4 -4
  3. package/dist/lib/browser/meta.json +1 -1
  4. package/dist/lib/node/index.cjs +474 -725
  5. package/dist/lib/node/index.cjs.map +4 -4
  6. package/dist/lib/node/meta.json +1 -1
  7. package/dist/types/src/index.d.ts +0 -2
  8. package/dist/types/src/index.d.ts.map +1 -1
  9. package/dist/types/src/runtime/dev-server.d.ts +10 -7
  10. package/dist/types/src/runtime/dev-server.d.ts.map +1 -1
  11. package/dist/types/src/runtime/scheduler.d.ts +59 -11
  12. package/dist/types/src/runtime/scheduler.d.ts.map +1 -1
  13. package/dist/types/src/testing/test/handler.d.ts +0 -1
  14. package/dist/types/src/testing/test/handler.d.ts.map +1 -1
  15. package/dist/types/src/types.d.ts +111 -131
  16. package/dist/types/src/types.d.ts.map +1 -1
  17. package/package.json +12 -14
  18. package/schema/functions.json +116 -139
  19. package/src/index.ts +0 -2
  20. package/src/runtime/dev-server.test.ts +35 -15
  21. package/src/runtime/dev-server.ts +19 -36
  22. package/src/runtime/scheduler.test.ts +75 -54
  23. package/src/runtime/scheduler.ts +298 -66
  24. package/src/testing/test/handler.ts +2 -8
  25. package/src/types.ts +42 -58
  26. package/dist/types/src/function/function-registry.d.ts +0 -24
  27. package/dist/types/src/function/function-registry.d.ts.map +0 -1
  28. package/dist/types/src/function/function-registry.test.d.ts +0 -2
  29. package/dist/types/src/function/function-registry.test.d.ts.map +0 -1
  30. package/dist/types/src/function/index.d.ts +0 -2
  31. package/dist/types/src/function/index.d.ts.map +0 -1
  32. package/dist/types/src/testing/functions-integration.test.d.ts +0 -2
  33. package/dist/types/src/testing/functions-integration.test.d.ts.map +0 -1
  34. package/dist/types/src/testing/index.d.ts +0 -4
  35. package/dist/types/src/testing/index.d.ts.map +0 -1
  36. package/dist/types/src/testing/setup.d.ts +0 -5
  37. package/dist/types/src/testing/setup.d.ts.map +0 -1
  38. package/dist/types/src/testing/types.d.ts +0 -9
  39. package/dist/types/src/testing/types.d.ts.map +0 -1
  40. package/dist/types/src/testing/util.d.ts +0 -3
  41. package/dist/types/src/testing/util.d.ts.map +0 -1
  42. package/dist/types/src/trigger/index.d.ts +0 -2
  43. package/dist/types/src/trigger/index.d.ts.map +0 -1
  44. package/dist/types/src/trigger/trigger-registry.d.ts +0 -40
  45. package/dist/types/src/trigger/trigger-registry.d.ts.map +0 -1
  46. package/dist/types/src/trigger/trigger-registry.test.d.ts +0 -2
  47. package/dist/types/src/trigger/trigger-registry.test.d.ts.map +0 -1
  48. package/dist/types/src/trigger/type/index.d.ts +0 -5
  49. package/dist/types/src/trigger/type/index.d.ts.map +0 -1
  50. package/dist/types/src/trigger/type/subscription-trigger.d.ts +0 -4
  51. package/dist/types/src/trigger/type/subscription-trigger.d.ts.map +0 -1
  52. package/dist/types/src/trigger/type/timer-trigger.d.ts +0 -4
  53. package/dist/types/src/trigger/type/timer-trigger.d.ts.map +0 -1
  54. package/dist/types/src/trigger/type/webhook-trigger.d.ts +0 -4
  55. package/dist/types/src/trigger/type/webhook-trigger.d.ts.map +0 -1
  56. package/dist/types/src/trigger/type/websocket-trigger.d.ts +0 -13
  57. package/dist/types/src/trigger/type/websocket-trigger.d.ts.map +0 -1
  58. package/dist/types/src/util.d.ts +0 -15
  59. package/dist/types/src/util.d.ts.map +0 -1
  60. package/dist/types/src/util.test.d.ts +0 -2
  61. package/dist/types/src/util.test.d.ts.map +0 -1
  62. package/src/function/function-registry.test.ts +0 -105
  63. package/src/function/function-registry.ts +0 -90
  64. package/src/function/index.ts +0 -5
  65. package/src/testing/functions-integration.test.ts +0 -99
  66. package/src/testing/index.ts +0 -7
  67. package/src/testing/setup.ts +0 -45
  68. package/src/testing/types.ts +0 -9
  69. package/src/testing/util.ts +0 -16
  70. package/src/trigger/index.ts +0 -5
  71. package/src/trigger/trigger-registry.test.ts +0 -255
  72. package/src/trigger/trigger-registry.ts +0 -189
  73. package/src/trigger/type/index.ts +0 -8
  74. package/src/trigger/type/subscription-trigger.ts +0 -80
  75. package/src/trigger/type/timer-trigger.ts +0 -44
  76. package/src/trigger/type/webhook-trigger.ts +0 -47
  77. package/src/trigger/type/websocket-trigger.ts +0 -91
  78. package/src/util.test.ts +0 -43
  79. package/src/util.ts +0 -48
package/src/index.ts CHANGED
@@ -2,8 +2,6 @@
2
2
  // Copyright 2023 DXOS.org
3
3
  //
4
4
 
5
- export * from './function';
6
5
  export * from './handler';
7
6
  export * from './runtime';
8
- export * from './trigger';
9
7
  export * from './types';
@@ -5,14 +5,12 @@
5
5
  import { expect } from 'chai';
6
6
  import path from 'path';
7
7
 
8
- import { waitForCondition } from '@dxos/async';
9
- import { type Client } from '@dxos/client';
8
+ import { FunctionsPlugin } from '@dxos/agent';
9
+ import { Client, Config } from '@dxos/client';
10
10
  import { TestBuilder } from '@dxos/client/testing';
11
- import { describe, test } from '@dxos/test';
11
+ import { describe, openAndClose, test } from '@dxos/test';
12
12
 
13
13
  import { DevServer } from './dev-server';
14
- import { FunctionRegistry } from '../function';
15
- import { createFunctionRuntime } from '../testing';
16
14
  import { type FunctionManifest } from '../types';
17
15
 
18
16
  describe('dev server', () => {
@@ -20,10 +18,35 @@ describe('dev server', () => {
20
18
  let testBuilder: TestBuilder;
21
19
  before(async () => {
22
20
  testBuilder = new TestBuilder();
23
- client = await createFunctionRuntime(testBuilder);
21
+ const config = new Config({
22
+ runtime: {
23
+ agent: {
24
+ plugins: [
25
+ {
26
+ id: 'dxos.org/agent/plugin/functions',
27
+ config: {
28
+ port: 8080,
29
+ },
30
+ },
31
+ ],
32
+ },
33
+ },
34
+ });
35
+
36
+ const services = testBuilder.createLocalClientServices();
37
+ client = new Client({ config, services });
38
+
39
+ await client.initialize();
40
+ await client.halo.createIdentity();
41
+ testBuilder.ctx.onDispose(() => client.destroy());
42
+
43
+ // TODO(burdon): Better way to configure plugin? (Rationalize chess.test).
44
+ const functionsPlugin = new FunctionsPlugin();
45
+ await functionsPlugin.initialize({ client, clientServices: services });
46
+ await openAndClose(functionsPlugin);
47
+
24
48
  expect(client.services.services.FunctionRegistryService).to.exist;
25
49
  });
26
-
27
50
  after(async () => {
28
51
  await testBuilder.destroy();
29
52
  });
@@ -32,19 +55,18 @@ describe('dev server', () => {
32
55
  const manifest: FunctionManifest = {
33
56
  functions: [
34
57
  {
35
- uri: 'example.com/function/test',
36
- route: 'test',
58
+ id: 'example.com/function/test',
59
+ path: 'test',
37
60
  handler: 'test',
38
61
  },
39
62
  ],
40
63
  };
41
64
 
42
- const registry = new FunctionRegistry(client);
43
- const server = new DevServer(client, registry, {
65
+ const server = new DevServer(client, {
66
+ manifest,
44
67
  baseDir: path.join(__dirname, '../testing'),
45
68
  });
46
- const space = await client.spaces.create();
47
- await registry.register(space, manifest.functions);
69
+ await server.initialize();
48
70
  await server.start();
49
71
 
50
72
  // TODO(burdon): Doesn't shut down cleanly.
@@ -52,8 +74,6 @@ describe('dev server', () => {
52
74
  testBuilder.ctx.onDispose(() => server.stop());
53
75
  expect(server).to.exist;
54
76
 
55
- await waitForCondition({ condition: () => server.functions.length > 0 });
56
-
57
77
  await server.invoke('test', {});
58
78
  expect(server.stats.seq).to.eq(1);
59
79
  });
@@ -9,15 +9,14 @@ import { join } from 'node:path';
9
9
 
10
10
  import { Event, Trigger } from '@dxos/async';
11
11
  import { type Client } from '@dxos/client';
12
- import { Context } from '@dxos/context';
13
12
  import { invariant } from '@dxos/invariant';
14
13
  import { log } from '@dxos/log';
15
14
 
16
- import { type FunctionRegistry } from '../function';
17
15
  import { type FunctionContext, type FunctionEvent, type FunctionHandler, type FunctionResponse } from '../handler';
18
- import { type FunctionDef } from '../types';
16
+ import { type FunctionDef, type FunctionManifest } from '../types';
19
17
 
20
18
  export type DevServerOptions = {
19
+ manifest: FunctionManifest;
21
20
  baseDir: string;
22
21
  port?: number;
23
22
  reload?: boolean;
@@ -28,8 +27,6 @@ export type DevServerOptions = {
28
27
  * Functions dev server provides a local HTTP server for testing functions.
29
28
  */
30
29
  export class DevServer {
31
- private _ctx = createContext();
32
-
33
30
  // Function handlers indexed by name (URL path).
34
31
  private readonly _handlers: Record<string, { def: FunctionDef; handler: FunctionHandler<any> }> = {};
35
32
 
@@ -41,17 +38,11 @@ export class DevServer {
41
38
 
42
39
  public readonly update = new Event<number>();
43
40
 
41
+ // prettier-ignore
44
42
  constructor(
45
43
  private readonly _client: Client,
46
- private readonly _functionsRegistry: FunctionRegistry,
47
44
  private readonly _options: DevServerOptions,
48
- ) {
49
- this._functionsRegistry.registered.on(async ({ added }) => {
50
- added.forEach((def) => this._load(def));
51
- await this._safeUpdateRegistration();
52
- log('new functions loaded', { added });
53
- });
54
- }
45
+ ) {}
55
46
 
56
47
  get stats() {
57
48
  return {
@@ -72,10 +63,19 @@ export class DevServer {
72
63
  return Object.values(this._handlers);
73
64
  }
74
65
 
66
+ async initialize() {
67
+ for (const def of this._options.manifest.functions) {
68
+ try {
69
+ await this._load(def);
70
+ } catch (err) {
71
+ log.error('parsing function (check manifest)', err);
72
+ }
73
+ }
74
+ }
75
+
75
76
  async start() {
76
77
  invariant(!this._server);
77
78
  log.info('starting...');
78
- this._ctx = createContext();
79
79
 
80
80
  // TODO(burdon): Move to hono.
81
81
  const app = express();
@@ -107,14 +107,12 @@ export class DevServer {
107
107
  // Register functions.
108
108
  const { registrationId, endpoint } = await this._client.services.services.FunctionRegistryService!.register({
109
109
  endpoint: this.endpoint,
110
+ functions: this.functions.map(({ def: { id, path } }) => ({ id, path })),
110
111
  });
111
112
 
112
113
  log.info('registered', { endpoint });
113
114
  this._proxy = endpoint;
114
115
  this._functionServiceRegistration = registrationId;
115
-
116
- // Open after registration, so that it can be updated with the list of function definitions.
117
- await this._functionsRegistry.open(this._ctx);
118
116
  } catch (err: any) {
119
117
  await this.stop();
120
118
  throw new Error('FunctionRegistryService not available (check plugin is configured).');
@@ -158,9 +156,9 @@ export class DevServer {
158
156
  * Load function.
159
157
  */
160
158
  private async _load(def: FunctionDef, force = false) {
161
- const { uri, route, handler } = def;
159
+ const { id, path, handler } = def;
162
160
  const filePath = join(this._options.baseDir, handler);
163
- log.info('loading', { uri, force });
161
+ log.info('loading', { id, force });
164
162
 
165
163
  // Remove from cache.
166
164
  if (force) {
@@ -171,26 +169,13 @@ export class DevServer {
171
169
  });
172
170
  }
173
171
 
174
- // TODO(burdon): Import types.
175
172
  // eslint-disable-next-line @typescript-eslint/no-var-requires
176
173
  const module = require(filePath);
177
174
  if (typeof module.default !== 'function') {
178
- throw new Error(`Handler must export default function: ${uri}`);
175
+ throw new Error(`Handler must export default function: ${id}`);
179
176
  }
180
177
 
181
- this._handlers[route] = { def, handler: module.default };
182
- }
183
-
184
- private async _safeUpdateRegistration(): Promise<void> {
185
- invariant(this._functionServiceRegistration);
186
- try {
187
- await this._client.services.services.FunctionRegistryService!.updateRegistration({
188
- registrationId: this._functionServiceRegistration,
189
- functions: this.functions.map(({ def: { id, route } }) => ({ id, route })),
190
- });
191
- } catch (e) {
192
- log.catch(e);
193
- }
178
+ this._handlers[path] = { def, handler: module.default };
194
179
  }
195
180
 
196
181
  /**
@@ -229,5 +214,3 @@ export class DevServer {
229
214
  return statusCode;
230
215
  }
231
216
  }
232
-
233
- const createContext = () => new Context({ name: 'DevServer' });
@@ -6,43 +6,40 @@ import { expect } from 'chai';
6
6
  import WebSocket from 'ws';
7
7
 
8
8
  import { Trigger } from '@dxos/async';
9
- import { type Client } from '@dxos/client';
9
+ import { Client } from '@dxos/client';
10
10
  import { TestBuilder } from '@dxos/client/testing';
11
- import { create } from '@dxos/echo-schema';
11
+ import { create, S, TypedObject } from '@dxos/echo-schema';
12
12
  import { describe, test } from '@dxos/test';
13
13
 
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
+ import { Scheduler } from './scheduler';
15
+ import { type FunctionManifest, type WebhookTrigger } from '../types';
19
16
 
20
17
  // TODO(burdon): Test we can add and remove triggers.
21
18
  describe('scheduler', () => {
22
- let testBuilder: TestBuilder;
23
19
  let client: Client;
24
20
  before(async () => {
25
- testBuilder = new TestBuilder();
26
- client = (await createInitializedClients(testBuilder, 1))[0];
21
+ const testBuilder = new TestBuilder();
22
+ client = new Client({ services: testBuilder.createLocalClientServices() });
23
+ await client.initialize();
24
+ await client.halo.createIdentity();
27
25
  });
28
26
  after(async () => {
29
- await testBuilder.destroy();
27
+ await client.destroy();
30
28
  });
31
29
 
32
30
  test('timer', async () => {
33
31
  const manifest: FunctionManifest = {
34
32
  functions: [
35
33
  {
36
- uri: 'example.com/function/test',
37
- route: '/test',
34
+ id: 'example.com/function/test',
35
+ path: '/test',
38
36
  handler: 'test',
39
37
  },
40
38
  ],
41
39
  triggers: [
42
40
  {
43
41
  function: 'example.com/function/test',
44
- spec: {
45
- type: 'timer',
42
+ timer: {
46
43
  cron: '0/1 * * * * *', // Every 1s.
47
44
  },
48
45
  },
@@ -51,13 +48,18 @@ describe('scheduler', () => {
51
48
 
52
49
  let count = 0;
53
50
  const done = new Trigger();
54
- const scheduler = createScheduler(async () => {
55
- if (++count === 3) {
56
- done.wake();
57
- }
51
+ const scheduler = new Scheduler(client, manifest, {
52
+ callback: async () => {
53
+ if (++count === 3) {
54
+ done.wake();
55
+ }
56
+ },
58
57
  });
59
- await scheduler.register(client.spaces.default, manifest);
58
+
60
59
  await scheduler.start();
60
+ after(async () => {
61
+ await scheduler.stop();
62
+ });
61
63
 
62
64
  await done.wait({ timeout: 5_000 });
63
65
  expect(count).to.equal(3);
@@ -67,16 +69,15 @@ describe('scheduler', () => {
67
69
  const manifest: FunctionManifest = {
68
70
  functions: [
69
71
  {
70
- uri: 'example.com/function/test',
71
- route: '/test',
72
+ id: 'example.com/function/test',
73
+ path: '/test',
72
74
  handler: 'test',
73
75
  },
74
76
  ],
75
77
  triggers: [
76
78
  {
77
79
  function: 'example.com/function/test',
78
- spec: {
79
- type: 'webhook',
80
+ webhook: {
80
81
  method: 'GET',
81
82
  },
82
83
  },
@@ -84,14 +85,23 @@ describe('scheduler', () => {
84
85
  };
85
86
 
86
87
  const done = new Trigger();
87
- const scheduler = createScheduler(async () => {
88
- done.wake();
88
+ const scheduler = new Scheduler(client, manifest, {
89
+ callback: async () => {
90
+ done.wake();
91
+ },
89
92
  });
90
- const space = await client.spaces.create();
91
- await scheduler.register(space, manifest);
93
+
92
94
  await scheduler.start();
95
+ after(async () => {
96
+ await scheduler.stop();
97
+ });
93
98
 
94
- setTimeout(async () => triggerWebhook(space, manifest.functions![0].uri));
99
+ setTimeout(() => {
100
+ const mount: WebhookTrigger = scheduler.mounts.find(
101
+ (mount) => mount.function === 'example.com/function/test',
102
+ )!.webhook!;
103
+ void fetch(`http://localhost:${mount.port}`);
104
+ });
95
105
 
96
106
  await done.wait();
97
107
  });
@@ -100,16 +110,15 @@ describe('scheduler', () => {
100
110
  const manifest: FunctionManifest = {
101
111
  functions: [
102
112
  {
103
- uri: 'example.com/function/test',
104
- route: '/test',
113
+ id: 'example.com/function/test',
114
+ path: '/test',
105
115
  handler: 'test',
106
116
  },
107
117
  ],
108
118
  triggers: [
109
119
  {
110
120
  function: 'example.com/function/test',
111
- spec: {
112
- type: 'websocket',
121
+ websocket: {
113
122
  // url: 'https://hub.dxos.network/api/mailbox/test',
114
123
  url: 'http://localhost:8081',
115
124
  init: {
@@ -121,11 +130,16 @@ describe('scheduler', () => {
121
130
  };
122
131
 
123
132
  const done = new Trigger();
124
- const scheduler = createScheduler(async () => {
125
- done.wake();
133
+ const scheduler = new Scheduler(client, manifest, {
134
+ callback: async (data) => {
135
+ done.wake();
136
+ },
126
137
  });
127
- await scheduler.register(client.spaces.default, manifest);
138
+
128
139
  await scheduler.start();
140
+ after(async () => {
141
+ await scheduler.stop();
142
+ });
129
143
 
130
144
  // Test server.
131
145
  setTimeout(() => {
@@ -143,20 +157,29 @@ describe('scheduler', () => {
143
157
  });
144
158
 
145
159
  test('subscription', async () => {
160
+ class TestType extends TypedObject({ typename: 'example.com/type/Test', version: '0.1.0' })({
161
+ title: S.string,
162
+ }) {}
163
+ client.addSchema(TestType);
164
+
146
165
  const manifest: FunctionManifest = {
147
166
  functions: [
148
167
  {
149
- uri: 'example.com/function/test',
150
- route: '/test',
168
+ id: 'example.com/function/test',
169
+ path: '/test',
151
170
  handler: 'test',
152
171
  },
153
172
  ],
154
173
  triggers: [
155
174
  {
156
175
  function: 'example.com/function/test',
157
- spec: {
158
- type: 'subscription',
159
- filter: [{ type: TestType.typename }],
176
+ subscription: {
177
+ spaceKey: client.spaces.default.key.toHex(),
178
+ filter: [
179
+ {
180
+ type: TestType.typename,
181
+ },
182
+ ],
160
183
  },
161
184
  },
162
185
  ],
@@ -164,14 +187,20 @@ describe('scheduler', () => {
164
187
 
165
188
  let count = 0;
166
189
  const done = new Trigger();
167
- const scheduler = createScheduler(async () => {
168
- if (++count === 2) {
169
- done.wake();
170
- }
190
+ const scheduler = new Scheduler(client, manifest, {
191
+ callback: async () => {
192
+ if (++count === 2) {
193
+ done.wake();
194
+ }
195
+ },
171
196
  });
172
- await scheduler.register(client.spaces.default, manifest);
197
+
173
198
  await scheduler.start();
199
+ after(async () => {
200
+ await scheduler.stop();
201
+ });
174
202
 
203
+ // TODO(burdon): Query for Expando?
175
204
  setTimeout(() => {
176
205
  const space = client.spaces.default;
177
206
  const object = create(TestType, { title: 'Hello world!' });
@@ -180,12 +209,4 @@ describe('scheduler', () => {
180
209
 
181
210
  await done.wait();
182
211
  });
183
-
184
- const createScheduler = (callback: SchedulerOptions['callback']) => {
185
- const scheduler = new Scheduler(new FunctionRegistry(client), new TriggerRegistry(client), { callback });
186
- after(async () => {
187
- await scheduler.stop();
188
- });
189
- return scheduler;
190
- };
191
212
  });