@dxos/functions 0.5.3-main.bc67fdb → 0.5.3-main.c8ad1bb
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.
- package/dist/lib/browser/index.mjs +642 -424
- package/dist/lib/browser/index.mjs.map +4 -4
- package/dist/lib/browser/meta.json +1 -1
- package/dist/lib/node/index.cjs +627 -421
- package/dist/lib/node/index.cjs.map +4 -4
- package/dist/lib/node/meta.json +1 -1
- package/dist/types/src/index.d.ts +2 -0
- package/dist/types/src/index.d.ts.map +1 -1
- package/dist/types/src/registry/function-registry.d.ts +24 -0
- package/dist/types/src/registry/function-registry.d.ts.map +1 -0
- package/dist/types/src/registry/function-registry.test.d.ts +2 -0
- package/dist/types/src/registry/function-registry.test.d.ts.map +1 -0
- package/dist/types/src/registry/index.d.ts +2 -0
- package/dist/types/src/registry/index.d.ts.map +1 -0
- package/dist/types/src/runtime/dev-server.d.ts +7 -10
- package/dist/types/src/runtime/dev-server.d.ts.map +1 -1
- package/dist/types/src/runtime/scheduler.d.ts +10 -59
- package/dist/types/src/runtime/scheduler.d.ts.map +1 -1
- package/dist/types/src/testing/functions-integration.test.d.ts +2 -0
- package/dist/types/src/testing/functions-integration.test.d.ts.map +1 -0
- package/dist/types/src/testing/index.d.ts +4 -0
- package/dist/types/src/testing/index.d.ts.map +1 -0
- package/dist/types/src/testing/setup.d.ts +5 -0
- package/dist/types/src/testing/setup.d.ts.map +1 -0
- package/dist/types/src/testing/test/handler.d.ts +1 -0
- package/dist/types/src/testing/test/handler.d.ts.map +1 -1
- package/dist/types/src/testing/types.d.ts +9 -0
- package/dist/types/src/testing/types.d.ts.map +1 -0
- package/dist/types/src/testing/util.d.ts +3 -0
- package/dist/types/src/testing/util.d.ts.map +1 -0
- package/dist/types/src/trigger/index.d.ts +2 -0
- package/dist/types/src/trigger/index.d.ts.map +1 -0
- package/dist/types/src/trigger/trigger-registry.d.ts +40 -0
- package/dist/types/src/trigger/trigger-registry.d.ts.map +1 -0
- package/dist/types/src/trigger/trigger-registry.test.d.ts +2 -0
- package/dist/types/src/trigger/trigger-registry.test.d.ts.map +1 -0
- package/dist/types/src/trigger/type/index.d.ts +5 -0
- package/dist/types/src/trigger/type/index.d.ts.map +1 -0
- package/dist/types/src/trigger/type/subscription-trigger.d.ts +4 -0
- package/dist/types/src/trigger/type/subscription-trigger.d.ts.map +1 -0
- package/dist/types/src/trigger/type/timer-trigger.d.ts +4 -0
- package/dist/types/src/trigger/type/timer-trigger.d.ts.map +1 -0
- package/dist/types/src/trigger/type/webhook-trigger.d.ts +4 -0
- package/dist/types/src/trigger/type/webhook-trigger.d.ts.map +1 -0
- package/dist/types/src/trigger/type/websocket-trigger.d.ts +13 -0
- package/dist/types/src/trigger/type/websocket-trigger.d.ts.map +1 -0
- package/dist/types/src/types.d.ts +118 -112
- package/dist/types/src/types.d.ts.map +1 -1
- package/package.json +16 -13
- package/schema/functions.json +121 -107
- package/src/index.ts +2 -0
- package/src/registry/function-registry.test.ts +105 -0
- package/src/registry/function-registry.ts +84 -0
- package/src/registry/index.ts +5 -0
- package/src/runtime/dev-server.test.ts +15 -35
- package/src/runtime/dev-server.ts +36 -18
- package/src/runtime/scheduler.test.ts +54 -75
- package/src/runtime/scheduler.ts +58 -298
- package/src/testing/functions-integration.test.ts +99 -0
- package/src/testing/index.ts +7 -0
- package/src/testing/setup.ts +45 -0
- package/src/testing/test/handler.ts +8 -2
- package/src/testing/types.ts +9 -0
- package/src/testing/util.ts +16 -0
- package/src/trigger/index.ts +5 -0
- package/src/trigger/trigger-registry.test.ts +229 -0
- package/src/trigger/trigger-registry.ts +176 -0
- package/src/trigger/type/index.ts +8 -0
- package/src/trigger/type/subscription-trigger.ts +73 -0
- package/src/trigger/type/timer-trigger.ts +44 -0
- package/src/trigger/type/webhook-trigger.ts +47 -0
- package/src/trigger/type/websocket-trigger.ts +91 -0
- package/src/types.ts +56 -42
package/src/runtime/scheduler.ts
CHANGED
|
@@ -2,22 +2,15 @@
|
|
|
2
2
|
// Copyright 2023 DXOS.org
|
|
3
3
|
//
|
|
4
4
|
|
|
5
|
-
import { CronJob } from 'cron';
|
|
6
|
-
import { getPort } from 'get-port-please';
|
|
7
|
-
import http from 'node:http';
|
|
8
5
|
import path from 'node:path';
|
|
9
|
-
import WebSocket from 'ws';
|
|
10
6
|
|
|
11
|
-
import {
|
|
12
|
-
import { debounce, DeferredTask, sleep, Trigger } from '@dxos/async';
|
|
13
|
-
import { type Client, type PublicKey } from '@dxos/client';
|
|
14
|
-
import { createSubscription, Filter, getAutomergeObjectCore, type Query, type Space } from '@dxos/client/echo';
|
|
7
|
+
import { type Space } from '@dxos/client/echo';
|
|
15
8
|
import { Context } from '@dxos/context';
|
|
16
|
-
import { invariant } from '@dxos/invariant';
|
|
17
9
|
import { log } from '@dxos/log';
|
|
18
|
-
import { ComplexMap } from '@dxos/util';
|
|
19
10
|
|
|
20
11
|
import { type FunctionEventMeta } from '../handler';
|
|
12
|
+
import { type FunctionRegistry } from '../registry';
|
|
13
|
+
import { type TriggerRegistry } from '../trigger';
|
|
21
14
|
import { type FunctionDef, type FunctionManifest, type FunctionTrigger } from '../types';
|
|
22
15
|
|
|
23
16
|
export type Callback = (data: any) => Promise<void | number>;
|
|
@@ -31,101 +24,80 @@ export type SchedulerOptions = {
|
|
|
31
24
|
* The scheduler triggers function execution based on various triggers.
|
|
32
25
|
*/
|
|
33
26
|
export class Scheduler {
|
|
34
|
-
|
|
35
|
-
private readonly _mounts = new ComplexMap<
|
|
36
|
-
{ spaceKey: PublicKey; id: string },
|
|
37
|
-
{ ctx: Context; trigger: FunctionTrigger }
|
|
38
|
-
>(({ spaceKey, id }) => `${spaceKey.toHex()}:${id}`);
|
|
27
|
+
private _ctx = createContext();
|
|
39
28
|
|
|
40
29
|
constructor(
|
|
41
|
-
|
|
42
|
-
|
|
30
|
+
public readonly functions: FunctionRegistry,
|
|
31
|
+
public readonly triggers: TriggerRegistry,
|
|
43
32
|
private readonly _options: SchedulerOptions = {},
|
|
44
|
-
) {
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
}
|
|
33
|
+
) {
|
|
34
|
+
this.functions.onFunctionsRegistered.on(async ({ space, newFunctions }) => {
|
|
35
|
+
await this._safeActivateTriggers(space, this.triggers.getInactiveTriggers(space), newFunctions);
|
|
36
|
+
});
|
|
37
|
+
this.triggers.registered.on(async ({ space, triggers }) => {
|
|
38
|
+
await this._safeActivateTriggers(space, triggers, this.functions.getFunctions(space));
|
|
39
|
+
});
|
|
51
40
|
}
|
|
52
41
|
|
|
53
42
|
async start() {
|
|
54
|
-
this.
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
await this.mount(new Context(), space, trigger);
|
|
59
|
-
}
|
|
60
|
-
}
|
|
61
|
-
});
|
|
43
|
+
await this._ctx.dispose();
|
|
44
|
+
this._ctx = createContext();
|
|
45
|
+
await this.functions.open(this._ctx);
|
|
46
|
+
await this.triggers.open(this._ctx);
|
|
62
47
|
}
|
|
63
48
|
|
|
64
49
|
async stop() {
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
50
|
+
await this._ctx.dispose();
|
|
51
|
+
await this.functions.close();
|
|
52
|
+
await this.triggers.close();
|
|
68
53
|
}
|
|
69
54
|
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
const key = { spaceKey: space.key, id: trigger.function };
|
|
75
|
-
const def = this._manifest.functions.find((config) => config.id === trigger.function);
|
|
76
|
-
invariant(def, `Function not found: ${trigger.function}`);
|
|
77
|
-
|
|
78
|
-
// TODO(burdon): Currently supports only one trigger declaration per function.
|
|
79
|
-
const exists = this._mounts.get(key);
|
|
80
|
-
if (!exists) {
|
|
81
|
-
this._mounts.set(key, { ctx, trigger });
|
|
82
|
-
log('mount', { space: space.key, trigger });
|
|
83
|
-
if (ctx.disposed) {
|
|
84
|
-
return;
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
//
|
|
88
|
-
// Triggers types.
|
|
89
|
-
//
|
|
90
|
-
|
|
91
|
-
if (trigger.timer) {
|
|
92
|
-
await this._createTimer(ctx, space, def, trigger);
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
if (trigger.webhook) {
|
|
96
|
-
await this._createWebhook(ctx, space, def, trigger);
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
if (trigger.websocket) {
|
|
100
|
-
await this._createWebsocket(ctx, space, def, trigger);
|
|
101
|
-
}
|
|
55
|
+
public async register(space: Space, manifest: FunctionManifest) {
|
|
56
|
+
await this.functions.register(space, manifest);
|
|
57
|
+
await this.triggers.register(space, manifest);
|
|
58
|
+
}
|
|
102
59
|
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
60
|
+
private async _safeActivateTriggers(
|
|
61
|
+
space: Space,
|
|
62
|
+
triggers: FunctionTrigger[],
|
|
63
|
+
functions: FunctionDef[],
|
|
64
|
+
): Promise<void> {
|
|
65
|
+
const mountTasks = triggers.map((trigger) => {
|
|
66
|
+
return this.activate(space, functions, trigger);
|
|
67
|
+
});
|
|
68
|
+
await Promise.all(mountTasks).catch(log.catch);
|
|
107
69
|
}
|
|
108
70
|
|
|
109
|
-
private async
|
|
110
|
-
const
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
await ctx.dispose();
|
|
71
|
+
private async activate(space: Space, functions: FunctionDef[], fnTrigger: FunctionTrigger) {
|
|
72
|
+
const definition = functions.find((def) => def.uri === fnTrigger.function);
|
|
73
|
+
if (!definition) {
|
|
74
|
+
log.info('function is not found for trigger', { fnTrigger });
|
|
75
|
+
return;
|
|
115
76
|
}
|
|
77
|
+
|
|
78
|
+
await this.triggers.activate({ space }, fnTrigger, async (args) => {
|
|
79
|
+
return this._execFunction(definition, {
|
|
80
|
+
meta: fnTrigger.meta,
|
|
81
|
+
data: { ...args, spaceKey: space.key },
|
|
82
|
+
});
|
|
83
|
+
});
|
|
84
|
+
log('activated trigger', { space: space.key, trigger: fnTrigger });
|
|
116
85
|
}
|
|
117
86
|
|
|
118
|
-
private async _execFunction<TData, TMeta>(
|
|
87
|
+
private async _execFunction<TData, TMeta>(
|
|
88
|
+
def: FunctionDef,
|
|
89
|
+
{ data, meta }: { data: TData; meta?: TMeta },
|
|
90
|
+
): Promise<number> {
|
|
119
91
|
let status = 0;
|
|
120
92
|
try {
|
|
121
93
|
// TODO(burdon): Pass in Space key (common context)?
|
|
122
|
-
const payload = Object.assign({},
|
|
94
|
+
const payload = Object.assign({}, meta && ({ meta } satisfies FunctionEventMeta<TMeta>), data);
|
|
123
95
|
|
|
124
96
|
const { endpoint, callback } = this._options;
|
|
125
97
|
if (endpoint) {
|
|
126
98
|
// TODO(burdon): Move out of scheduler (generalize as callback).
|
|
127
|
-
const url = path.join(endpoint, def.
|
|
128
|
-
log.info('exec', { function: def.
|
|
99
|
+
const url = path.join(endpoint, def.route);
|
|
100
|
+
log.info('exec', { function: def.uri, url });
|
|
129
101
|
const response = await fetch(url, {
|
|
130
102
|
method: 'POST',
|
|
131
103
|
headers: {
|
|
@@ -136,7 +108,7 @@ export class Scheduler {
|
|
|
136
108
|
|
|
137
109
|
status = response.status;
|
|
138
110
|
} else if (callback) {
|
|
139
|
-
log.info('exec', { function: def.
|
|
111
|
+
log.info('exec', { function: def.uri });
|
|
140
112
|
status = (await callback(payload)) ?? 200;
|
|
141
113
|
}
|
|
142
114
|
|
|
@@ -146,226 +118,14 @@ export class Scheduler {
|
|
|
146
118
|
}
|
|
147
119
|
|
|
148
120
|
// const result = await response.json();
|
|
149
|
-
log.info('done', { function: def.
|
|
121
|
+
log.info('done', { function: def.uri, status });
|
|
150
122
|
} catch (err: any) {
|
|
151
|
-
log.error('error', { function: def.
|
|
123
|
+
log.error('error', { function: def.uri, error: err.message });
|
|
152
124
|
status = 500;
|
|
153
125
|
}
|
|
154
126
|
|
|
155
127
|
return status;
|
|
156
128
|
}
|
|
157
|
-
|
|
158
|
-
//
|
|
159
|
-
// Triggers
|
|
160
|
-
//
|
|
161
|
-
|
|
162
|
-
/**
|
|
163
|
-
* Cron timer.
|
|
164
|
-
*/
|
|
165
|
-
private async _createTimer(ctx: Context, space: Space, def: FunctionDef, trigger: FunctionTrigger) {
|
|
166
|
-
log.info('timer', { space: space.key, trigger });
|
|
167
|
-
const spec = trigger.timer!;
|
|
168
|
-
|
|
169
|
-
const task = new DeferredTask(ctx, async () => {
|
|
170
|
-
await this._execFunction(def, trigger, { spaceKey: space.key });
|
|
171
|
-
});
|
|
172
|
-
|
|
173
|
-
let last = 0;
|
|
174
|
-
let run = 0;
|
|
175
|
-
// https://www.npmjs.com/package/cron#constructor
|
|
176
|
-
const job = CronJob.from({
|
|
177
|
-
cronTime: spec.cron,
|
|
178
|
-
runOnInit: false,
|
|
179
|
-
onTick: () => {
|
|
180
|
-
// TODO(burdon): Check greater than 30s (use cron-parser).
|
|
181
|
-
const now = Date.now();
|
|
182
|
-
const delta = last ? now - last : 0;
|
|
183
|
-
last = now;
|
|
184
|
-
|
|
185
|
-
run++;
|
|
186
|
-
log.info('tick', { space: space.key.truncate(), count: run, delta });
|
|
187
|
-
task.schedule();
|
|
188
|
-
},
|
|
189
|
-
});
|
|
190
|
-
|
|
191
|
-
job.start();
|
|
192
|
-
ctx.onDispose(() => job.stop());
|
|
193
|
-
}
|
|
194
|
-
|
|
195
|
-
/**
|
|
196
|
-
* Webhook.
|
|
197
|
-
*/
|
|
198
|
-
private async _createWebhook(ctx: Context, space: Space, def: FunctionDef, trigger: FunctionTrigger) {
|
|
199
|
-
log.info('webhook', { space: space.key, trigger });
|
|
200
|
-
const spec = trigger.webhook!;
|
|
201
|
-
|
|
202
|
-
// TODO(burdon): Enable POST hook with payload.
|
|
203
|
-
const server = http.createServer(async (req, res) => {
|
|
204
|
-
if (req.method !== spec.method) {
|
|
205
|
-
res.statusCode = 405;
|
|
206
|
-
return res.end();
|
|
207
|
-
}
|
|
208
|
-
|
|
209
|
-
res.statusCode = await this._execFunction(def, trigger, { spaceKey: space.key });
|
|
210
|
-
res.end();
|
|
211
|
-
});
|
|
212
|
-
|
|
213
|
-
// TODO(burdon): Not used.
|
|
214
|
-
// const DEF_PORT_RANGE = { min: 7500, max: 7599 };
|
|
215
|
-
// const portRange = Object.assign({}, trigger.port, DEF_PORT_RANGE) as WebhookTrigger['port'];
|
|
216
|
-
const port = await getPort({
|
|
217
|
-
random: true,
|
|
218
|
-
// portRange: [portRange!.min, portRange!.max],
|
|
219
|
-
});
|
|
220
|
-
|
|
221
|
-
// TODO(burdon): Update trigger object with actual port.
|
|
222
|
-
server.listen(port, () => {
|
|
223
|
-
log.info('started webhook', { port });
|
|
224
|
-
spec.port = port;
|
|
225
|
-
});
|
|
226
|
-
|
|
227
|
-
ctx.onDispose(() => {
|
|
228
|
-
server.close();
|
|
229
|
-
});
|
|
230
|
-
}
|
|
231
|
-
|
|
232
|
-
/**
|
|
233
|
-
* Websocket.
|
|
234
|
-
* NOTE: The port must be unique, so the same hook cannot be used for multiple spaces.
|
|
235
|
-
*/
|
|
236
|
-
private async _createWebsocket(
|
|
237
|
-
ctx: Context,
|
|
238
|
-
space: Space,
|
|
239
|
-
def: FunctionDef,
|
|
240
|
-
trigger: FunctionTrigger,
|
|
241
|
-
options: {
|
|
242
|
-
retryDelay: number;
|
|
243
|
-
maxAttempts: number;
|
|
244
|
-
} = {
|
|
245
|
-
retryDelay: 2,
|
|
246
|
-
maxAttempts: 5,
|
|
247
|
-
},
|
|
248
|
-
) {
|
|
249
|
-
log.info('websocket', { space: space.key, trigger });
|
|
250
|
-
const spec = trigger.websocket!;
|
|
251
|
-
const { url, init } = spec;
|
|
252
|
-
|
|
253
|
-
let ws: WebSocket;
|
|
254
|
-
for (let attempt = 1; attempt <= options.maxAttempts; attempt++) {
|
|
255
|
-
const open = new Trigger<boolean>();
|
|
256
|
-
|
|
257
|
-
ws = new WebSocket(url);
|
|
258
|
-
Object.assign(ws, {
|
|
259
|
-
onopen: () => {
|
|
260
|
-
log.info('opened', { url });
|
|
261
|
-
if (spec.init) {
|
|
262
|
-
ws.send(new TextEncoder().encode(JSON.stringify(init)));
|
|
263
|
-
}
|
|
264
|
-
|
|
265
|
-
open.wake(true);
|
|
266
|
-
},
|
|
267
|
-
|
|
268
|
-
onclose: (event) => {
|
|
269
|
-
log.info('closed', { url, code: event.code });
|
|
270
|
-
// Reconnect if server closes (e.g., CF restart).
|
|
271
|
-
// https://developer.mozilla.org/en-US/docs/Web/API/CloseEvent/code
|
|
272
|
-
if (event.code === 1006) {
|
|
273
|
-
setTimeout(async () => {
|
|
274
|
-
log.info(`reconnecting in ${options.retryDelay}s...`, { url });
|
|
275
|
-
await this._createWebsocket(ctx, space, def, trigger, options);
|
|
276
|
-
}, options.retryDelay * 1_000);
|
|
277
|
-
}
|
|
278
|
-
|
|
279
|
-
open.wake(false);
|
|
280
|
-
},
|
|
281
|
-
|
|
282
|
-
onerror: (event) => {
|
|
283
|
-
log.catch(event.error, { url });
|
|
284
|
-
},
|
|
285
|
-
|
|
286
|
-
onmessage: async (event) => {
|
|
287
|
-
try {
|
|
288
|
-
const data = JSON.parse(new TextDecoder().decode(event.data as Uint8Array));
|
|
289
|
-
await this._execFunction(def, trigger, { spaceKey: space.key, data });
|
|
290
|
-
} catch (err) {
|
|
291
|
-
log.catch(err, { url });
|
|
292
|
-
}
|
|
293
|
-
},
|
|
294
|
-
} satisfies Partial<WebSocket>);
|
|
295
|
-
|
|
296
|
-
const isOpen = await open.wait();
|
|
297
|
-
if (isOpen) {
|
|
298
|
-
break;
|
|
299
|
-
} else {
|
|
300
|
-
const wait = Math.pow(attempt, 2) * options.retryDelay;
|
|
301
|
-
if (attempt < options.maxAttempts) {
|
|
302
|
-
log.warn(`failed to connect; trying again in ${wait}s`, { attempt });
|
|
303
|
-
await sleep(wait * 1_000);
|
|
304
|
-
}
|
|
305
|
-
}
|
|
306
|
-
}
|
|
307
|
-
|
|
308
|
-
ctx.onDispose(() => {
|
|
309
|
-
ws?.close();
|
|
310
|
-
});
|
|
311
|
-
}
|
|
312
|
-
|
|
313
|
-
/**
|
|
314
|
-
* ECHO subscription.
|
|
315
|
-
*/
|
|
316
|
-
private async _createSubscription(ctx: Context, space: Space, def: FunctionDef, trigger: FunctionTrigger) {
|
|
317
|
-
log.info('subscription', { space: space.key, trigger });
|
|
318
|
-
const spec = trigger.subscription!;
|
|
319
|
-
|
|
320
|
-
const objectIds = new Set<string>();
|
|
321
|
-
const task = new DeferredTask(ctx, async () => {
|
|
322
|
-
await this._execFunction(def, trigger, { spaceKey: space.key, objects: Array.from(objectIds) });
|
|
323
|
-
});
|
|
324
|
-
|
|
325
|
-
// TODO(burdon): Don't fire initially?
|
|
326
|
-
// TODO(burdon): Create queue. Only allow one invocation per trigger at a time?
|
|
327
|
-
const subscriptions: (() => void)[] = [];
|
|
328
|
-
const subscription = createSubscription(({ added, updated }) => {
|
|
329
|
-
log.info('updated', { added: added.length, updated: updated.length });
|
|
330
|
-
for (const object of added) {
|
|
331
|
-
objectIds.add(object.id);
|
|
332
|
-
}
|
|
333
|
-
for (const object of updated) {
|
|
334
|
-
objectIds.add(object.id);
|
|
335
|
-
}
|
|
336
|
-
|
|
337
|
-
task.schedule();
|
|
338
|
-
});
|
|
339
|
-
|
|
340
|
-
subscriptions.push(() => subscription.unsubscribe());
|
|
341
|
-
|
|
342
|
-
// TODO(burdon): Disable trigger if keeps failing.
|
|
343
|
-
const { filter, options: { deep, delay } = {} } = spec;
|
|
344
|
-
const update = ({ objects }: Query) => {
|
|
345
|
-
subscription.update(objects);
|
|
346
|
-
|
|
347
|
-
// TODO(burdon): Hack to monitor changes to Document's text object.
|
|
348
|
-
if (deep) {
|
|
349
|
-
log.info('update', { objects: objects.length });
|
|
350
|
-
for (const object of objects) {
|
|
351
|
-
const content = object.content;
|
|
352
|
-
if (content instanceof TextV0Type) {
|
|
353
|
-
subscriptions.push(
|
|
354
|
-
getAutomergeObjectCore(content).updates.on(debounce(() => subscription.update([object]), 1_000)),
|
|
355
|
-
);
|
|
356
|
-
}
|
|
357
|
-
}
|
|
358
|
-
}
|
|
359
|
-
};
|
|
360
|
-
|
|
361
|
-
// TODO(burdon): Is Filter.or implemented?
|
|
362
|
-
// TODO(burdon): [Bug]: all callbacks are fired on the first mutation.
|
|
363
|
-
// TODO(burdon): [Bug]: not updated when document is deleted (either top or hierarchically).
|
|
364
|
-
const query = space.db.query(Filter.or(filter.map(({ type, props }) => Filter.typename(type, props))));
|
|
365
|
-
subscriptions.push(query.subscribe(delay ? debounce(update, delay) : update));
|
|
366
|
-
|
|
367
|
-
ctx.onDispose(() => {
|
|
368
|
-
subscriptions.forEach((unsubscribe) => unsubscribe());
|
|
369
|
-
});
|
|
370
|
-
}
|
|
371
129
|
}
|
|
130
|
+
|
|
131
|
+
const createContext = () => new Context({ name: 'FunctionScheduler' });
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2024 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import { expect } from 'chai';
|
|
6
|
+
import path from 'path';
|
|
7
|
+
|
|
8
|
+
import { Trigger, waitForCondition } from '@dxos/async';
|
|
9
|
+
import { type Client } from '@dxos/client';
|
|
10
|
+
import { create, type Space } from '@dxos/client/echo';
|
|
11
|
+
import { performInvitation, TestBuilder } from '@dxos/client/testing';
|
|
12
|
+
import { Invitation } from '@dxos/protocols/proto/dxos/client/services';
|
|
13
|
+
import { describe, test } from '@dxos/test';
|
|
14
|
+
|
|
15
|
+
import { setTestCallHandler } from './test/handler';
|
|
16
|
+
import { FunctionRegistry } from '../registry';
|
|
17
|
+
import { DevServer, Scheduler } from '../runtime';
|
|
18
|
+
import { createFunctionRuntime, createInitializedClients, TestType } from '../testing';
|
|
19
|
+
import { TriggerRegistry } from '../trigger';
|
|
20
|
+
import { FunctionDef, FunctionTrigger } from '../types';
|
|
21
|
+
|
|
22
|
+
describe('functions e2e', () => {
|
|
23
|
+
let testBuilder: TestBuilder;
|
|
24
|
+
before(async () => {
|
|
25
|
+
testBuilder = new TestBuilder();
|
|
26
|
+
});
|
|
27
|
+
after(async () => {
|
|
28
|
+
await testBuilder.destroy();
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
test('a function gets triggered in response to another peer object creations', async () => {
|
|
32
|
+
// TODO(burdon): Create builder pattern.
|
|
33
|
+
const functionRuntime = await createFunctionRuntime(testBuilder);
|
|
34
|
+
const devServer = await startDevServer(functionRuntime);
|
|
35
|
+
const scheduler = await startScheduler(functionRuntime, devServer);
|
|
36
|
+
|
|
37
|
+
const app = (await createInitializedClients(testBuilder, 1))[0];
|
|
38
|
+
const space = await app.spaces.create();
|
|
39
|
+
await inviteMember(space, functionRuntime);
|
|
40
|
+
|
|
41
|
+
const uri = 'example.com/function/test';
|
|
42
|
+
space.db.add(create(FunctionDef, { uri, route: '/test', handler: 'test' }));
|
|
43
|
+
const triggerMeta: FunctionTrigger['meta'] = { name: 'DXOS' };
|
|
44
|
+
space.db.add(
|
|
45
|
+
create(FunctionTrigger, {
|
|
46
|
+
function: uri,
|
|
47
|
+
meta: triggerMeta,
|
|
48
|
+
spec: {
|
|
49
|
+
type: 'subscription',
|
|
50
|
+
filter: [{ type: TestType.typename }],
|
|
51
|
+
},
|
|
52
|
+
}),
|
|
53
|
+
);
|
|
54
|
+
|
|
55
|
+
const called = new Trigger<any>();
|
|
56
|
+
setTestCallHandler(async (args) => {
|
|
57
|
+
called.wake(args.event.data);
|
|
58
|
+
return args.response.status(200);
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
await waitTriggersReplicated(space, scheduler);
|
|
62
|
+
const addedObject = space.db.add(create(TestType, { title: '42' }));
|
|
63
|
+
|
|
64
|
+
const callArgs = await called.wait();
|
|
65
|
+
expect(callArgs.meta).to.deep.eq(triggerMeta);
|
|
66
|
+
expect(callArgs.objects).to.deep.eq([addedObject.id]);
|
|
67
|
+
expect(callArgs.spaceKey).to.eq(space.key.toHex());
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
const waitTriggersReplicated = async (space: Space, scheduler: Scheduler) => {
|
|
71
|
+
await waitForCondition({ condition: () => scheduler.triggers.getActiveTriggers(space).length > 0 });
|
|
72
|
+
};
|
|
73
|
+
|
|
74
|
+
// TODO(burdon): Factor out utils to builder pattern.
|
|
75
|
+
|
|
76
|
+
const startScheduler = async (client: Client, devServer: DevServer) => {
|
|
77
|
+
const functionRegistry = new FunctionRegistry(client);
|
|
78
|
+
const triggerRegistry = new TriggerRegistry(client);
|
|
79
|
+
const scheduler = new Scheduler(functionRegistry, triggerRegistry, { endpoint: devServer.endpoint });
|
|
80
|
+
await scheduler.start();
|
|
81
|
+
testBuilder.ctx.onDispose(() => scheduler.stop());
|
|
82
|
+
return scheduler;
|
|
83
|
+
};
|
|
84
|
+
|
|
85
|
+
const startDevServer = async (client: Client) => {
|
|
86
|
+
const functionRegistry = new FunctionRegistry(client);
|
|
87
|
+
const server = new DevServer(client, functionRegistry, {
|
|
88
|
+
baseDir: path.join(__dirname, '../testing'),
|
|
89
|
+
});
|
|
90
|
+
await server.start();
|
|
91
|
+
testBuilder.ctx.onDispose(() => server.stop());
|
|
92
|
+
return server;
|
|
93
|
+
};
|
|
94
|
+
|
|
95
|
+
const inviteMember = async (host: Space, guest: Client) => {
|
|
96
|
+
const [{ invitation: hostInvitation }] = await Promise.all(performInvitation({ host, guest: guest.spaces }));
|
|
97
|
+
expect(hostInvitation?.state).to.eq(Invitation.State.SUCCESS);
|
|
98
|
+
};
|
|
99
|
+
});
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2024 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import { FunctionsPlugin } from '@dxos/agent';
|
|
6
|
+
import { Client, Config } from '@dxos/client';
|
|
7
|
+
import { type TestBuilder } from '@dxos/client/testing';
|
|
8
|
+
import { range } from '@dxos/util';
|
|
9
|
+
|
|
10
|
+
import { TestType } from './types';
|
|
11
|
+
import { FunctionDef, FunctionTrigger } from '../types';
|
|
12
|
+
|
|
13
|
+
// TODO(burdon): Create TestBuilder.
|
|
14
|
+
|
|
15
|
+
export const createInitializedClients = async (testBuilder: TestBuilder, count: number = 1, config?: Config) => {
|
|
16
|
+
const clients = range(count).map(() => new Client({ config, services: testBuilder.createLocalClientServices() }));
|
|
17
|
+
testBuilder.ctx.onDispose(() => Promise.all(clients.map((c) => c.destroy())));
|
|
18
|
+
return Promise.all(
|
|
19
|
+
clients.map(async (client, index) => {
|
|
20
|
+
await client.initialize();
|
|
21
|
+
await client.halo.createIdentity({ displayName: `Peer ${index}` });
|
|
22
|
+
client.addSchema(TestType, FunctionDef, FunctionTrigger);
|
|
23
|
+
return client;
|
|
24
|
+
}),
|
|
25
|
+
);
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
export const createFunctionRuntime = async (testBuilder: TestBuilder): Promise<Client> => {
|
|
29
|
+
const config = new Config({
|
|
30
|
+
runtime: {
|
|
31
|
+
agent: {
|
|
32
|
+
plugins: [{ id: 'dxos.org/agent/plugin/functions', config: { port: 8080 } }],
|
|
33
|
+
},
|
|
34
|
+
},
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
const client = (await createInitializedClients(testBuilder, 1, config))[0];
|
|
38
|
+
|
|
39
|
+
// TODO(burdon): Better way to configure plugin? (Rationalize chess.test).
|
|
40
|
+
const functionsPlugin = new FunctionsPlugin();
|
|
41
|
+
await functionsPlugin.initialize({ client, clientServices: client.services });
|
|
42
|
+
await functionsPlugin.open();
|
|
43
|
+
testBuilder.ctx.onDispose(() => functionsPlugin.close());
|
|
44
|
+
return client;
|
|
45
|
+
};
|
|
@@ -4,6 +4,12 @@
|
|
|
4
4
|
|
|
5
5
|
import { type FunctionHandler } from '../../handler';
|
|
6
6
|
|
|
7
|
-
|
|
8
|
-
|
|
7
|
+
let callHandler: FunctionHandler<any> = async ({ response }) => response.status(200);
|
|
8
|
+
|
|
9
|
+
export const setTestCallHandler = (handler: FunctionHandler<any>) => {
|
|
10
|
+
callHandler = handler;
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
export const handler: FunctionHandler<any> = async (args) => {
|
|
14
|
+
return callHandler(args);
|
|
9
15
|
};
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2024 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import { Filter, type Space } from '@dxos/client/echo';
|
|
6
|
+
import { invariant } from '@dxos/invariant';
|
|
7
|
+
|
|
8
|
+
import { FunctionTrigger } from '../types';
|
|
9
|
+
|
|
10
|
+
export const triggerWebhook = async (space: Space, uri: string) => {
|
|
11
|
+
const trigger = (
|
|
12
|
+
await space.db.query(Filter.schema(FunctionTrigger, (t: FunctionTrigger) => t.function === uri)).run()
|
|
13
|
+
).objects[0];
|
|
14
|
+
invariant(trigger.spec.type === 'webhook');
|
|
15
|
+
void fetch(`http://localhost:${trigger.spec.port}`);
|
|
16
|
+
};
|