@dxos/functions 0.3.9-main.ee9a520 → 0.3.9-main.ef334cd
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 +130 -62
- package/dist/lib/browser/index.mjs.map +4 -4
- package/dist/lib/browser/meta.json +1 -1
- package/dist/lib/node/index.cjs +131 -64
- package/dist/lib/node/index.cjs.map +4 -4
- package/dist/lib/node/meta.json +1 -1
- package/dist/types/src/handler.d.ts +10 -2
- package/dist/types/src/handler.d.ts.map +1 -1
- package/dist/types/src/manifest.d.ts +3 -2
- package/dist/types/src/manifest.d.ts.map +1 -1
- package/dist/types/src/runtime/dev-server.d.ts +1 -0
- package/dist/types/src/runtime/dev-server.d.ts.map +1 -1
- package/dist/types/src/runtime/scheduler.d.ts +8 -3
- package/dist/types/src/runtime/scheduler.d.ts.map +1 -1
- package/dist/types/src/runtime/scheduler.test.d.ts +2 -0
- package/dist/types/src/runtime/scheduler.test.d.ts.map +1 -0
- package/package.json +11 -9
- package/src/handler.ts +29 -3
- package/src/manifest.ts +4 -2
- package/src/runtime/dev-server.ts +4 -2
- package/src/runtime/scheduler.test.ts +57 -0
- package/src/runtime/scheduler.ts +98 -58
- package/dist/types/src/function.test.d.ts +0 -2
- package/dist/types/src/function.test.d.ts.map +0 -1
- package/src/function.test.ts +0 -7
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@dxos/functions",
|
|
3
|
-
"version": "0.3.9-main.
|
|
3
|
+
"version": "0.3.9-main.ef334cd",
|
|
4
4
|
"description": "Functions SDK and runtime.",
|
|
5
5
|
"homepage": "https://dxos.org",
|
|
6
6
|
"bugs": "https://github.com/dxos/dxos/issues",
|
|
@@ -19,17 +19,19 @@
|
|
|
19
19
|
"src"
|
|
20
20
|
],
|
|
21
21
|
"dependencies": {
|
|
22
|
+
"@preact/signals-core": "^1.5.0",
|
|
22
23
|
"cron": "^3.1.6",
|
|
24
|
+
"deepsignal": "1.4.0-shallow.0",
|
|
23
25
|
"express": "^4.17.1",
|
|
24
26
|
"get-port-please": "^3.1.1",
|
|
25
|
-
"@dxos/
|
|
26
|
-
"@dxos/
|
|
27
|
-
"@dxos/
|
|
28
|
-
"@dxos/
|
|
29
|
-
"@dxos/
|
|
30
|
-
"@dxos/
|
|
31
|
-
"@dxos/
|
|
32
|
-
"@dxos/util": "0.3.9-main.
|
|
27
|
+
"@dxos/async": "0.3.9-main.ef334cd",
|
|
28
|
+
"@dxos/context": "0.3.9-main.ef334cd",
|
|
29
|
+
"@dxos/client": "0.3.9-main.ef334cd",
|
|
30
|
+
"@dxos/echo-schema": "0.3.9-main.ef334cd",
|
|
31
|
+
"@dxos/log": "0.3.9-main.ef334cd",
|
|
32
|
+
"@dxos/invariant": "0.3.9-main.ef334cd",
|
|
33
|
+
"@dxos/node-std": "0.3.9-main.ef334cd",
|
|
34
|
+
"@dxos/util": "0.3.9-main.ef334cd"
|
|
33
35
|
},
|
|
34
36
|
"devDependencies": {
|
|
35
37
|
"@types/express": "^4.17.17"
|
package/src/handler.ts
CHANGED
|
@@ -2,7 +2,10 @@
|
|
|
2
2
|
// Copyright 2023 DXOS.org
|
|
3
3
|
//
|
|
4
4
|
|
|
5
|
-
import { type Client } from '@dxos/client';
|
|
5
|
+
import { type Client, PublicKey } from '@dxos/client';
|
|
6
|
+
import { type Space } from '@dxos/client/echo';
|
|
7
|
+
import { isTypedObject, type TypedObject } from '@dxos/echo-schema';
|
|
8
|
+
import { nonNullable } from '@dxos/util';
|
|
6
9
|
|
|
7
10
|
// TODO(burdon): No response?
|
|
8
11
|
export interface Response {
|
|
@@ -12,6 +15,7 @@ export interface Response {
|
|
|
12
15
|
// TODO(burdon): Limit access to individual space?
|
|
13
16
|
export interface FunctionContext {
|
|
14
17
|
client: Client;
|
|
18
|
+
dataDir?: string;
|
|
15
19
|
}
|
|
16
20
|
|
|
17
21
|
// TODO(burdon): Model after http request. Ref Lambda/OpenFaaS.
|
|
@@ -23,6 +27,28 @@ export type FunctionHandler<T extends {}> = (params: {
|
|
|
23
27
|
}) => Promise<Response | void>;
|
|
24
28
|
|
|
25
29
|
export type FunctionSubscriptionEvent = {
|
|
26
|
-
space
|
|
27
|
-
objects
|
|
30
|
+
space?: string; // TODO(burdon): Convert to PublicKey.
|
|
31
|
+
objects?: string[];
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
export type FunctionSubscriptionEvent2 = {
|
|
35
|
+
space?: Space;
|
|
36
|
+
objects?: TypedObject[];
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
export const subscriptionHandler = (
|
|
40
|
+
handler: FunctionHandler<FunctionSubscriptionEvent2>,
|
|
41
|
+
): FunctionHandler<FunctionSubscriptionEvent> => {
|
|
42
|
+
return ({ event, context, ...rest }) => {
|
|
43
|
+
const { client } = context;
|
|
44
|
+
const space = event.space ? client.spaces.get(PublicKey.from(event.space)) : undefined;
|
|
45
|
+
const objects =
|
|
46
|
+
space &&
|
|
47
|
+
event.objects
|
|
48
|
+
?.map<TypedObject | undefined>((id) => space!.db.getObjectById(id))
|
|
49
|
+
.filter(nonNullable)
|
|
50
|
+
.filter(isTypedObject);
|
|
51
|
+
|
|
52
|
+
return handler({ event: { space, objects }, context, ...rest });
|
|
53
|
+
};
|
|
28
54
|
};
|
package/src/manifest.ts
CHANGED
|
@@ -15,11 +15,13 @@ export type FunctionDef = {
|
|
|
15
15
|
description?: string;
|
|
16
16
|
};
|
|
17
17
|
|
|
18
|
+
// TODO(burdon): Query DSL.
|
|
18
19
|
export type TriggerSubscription = {
|
|
19
20
|
type: string;
|
|
20
21
|
spaceKey: string;
|
|
21
22
|
props?: Record<string, any>;
|
|
22
|
-
|
|
23
|
+
deep?: boolean; // Watch changes to object (not just creation).
|
|
24
|
+
delay?: number;
|
|
23
25
|
};
|
|
24
26
|
|
|
25
27
|
// TODO(burdon): Generalize binding.
|
|
@@ -28,7 +30,7 @@ export type TriggerSubscription = {
|
|
|
28
30
|
export type FunctionTrigger = {
|
|
29
31
|
function: string;
|
|
30
32
|
schedule?: string;
|
|
31
|
-
|
|
33
|
+
subscriptions?: TriggerSubscription[];
|
|
32
34
|
};
|
|
33
35
|
|
|
34
36
|
/**
|
|
@@ -20,6 +20,7 @@ export type DevServerOptions = {
|
|
|
20
20
|
directory: string;
|
|
21
21
|
manifest: FunctionManifest;
|
|
22
22
|
reload?: boolean;
|
|
23
|
+
dataDir?: string;
|
|
23
24
|
};
|
|
24
25
|
|
|
25
26
|
/**
|
|
@@ -79,13 +80,13 @@ export class DevServer {
|
|
|
79
80
|
res.statusCode = await this._invoke(name, req.body);
|
|
80
81
|
res.end();
|
|
81
82
|
} catch (err: any) {
|
|
82
|
-
log.
|
|
83
|
+
log.catch(err);
|
|
83
84
|
res.statusCode = 500;
|
|
84
85
|
res.end();
|
|
85
86
|
}
|
|
86
87
|
});
|
|
87
88
|
|
|
88
|
-
this._port = await getPort({ port: 7200, portRange: [7200, 7299] });
|
|
89
|
+
this._port = await getPort({ host: 'localhost', port: 7200, portRange: [7200, 7299] });
|
|
89
90
|
this._server = app.listen(this._port);
|
|
90
91
|
|
|
91
92
|
try {
|
|
@@ -161,6 +162,7 @@ export class DevServer {
|
|
|
161
162
|
|
|
162
163
|
const context: FunctionContext = {
|
|
163
164
|
client: this._client,
|
|
165
|
+
dataDir: this._options.dataDir,
|
|
164
166
|
};
|
|
165
167
|
|
|
166
168
|
let statusCode = 200;
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2023 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import { expect } from 'chai';
|
|
6
|
+
|
|
7
|
+
import { Trigger } from '@dxos/async';
|
|
8
|
+
import { Client } from '@dxos/client';
|
|
9
|
+
import { TestBuilder } from '@dxos/client/testing';
|
|
10
|
+
import { describe, test } from '@dxos/test';
|
|
11
|
+
|
|
12
|
+
import { Scheduler } from './scheduler';
|
|
13
|
+
import { type FunctionManifest } from '../manifest';
|
|
14
|
+
|
|
15
|
+
describe('scheduler', () => {
|
|
16
|
+
test('basic', async () => {
|
|
17
|
+
const testBuilder = new TestBuilder();
|
|
18
|
+
const client = new Client({ services: testBuilder.createLocal() });
|
|
19
|
+
await client.initialize();
|
|
20
|
+
await client.halo.createIdentity();
|
|
21
|
+
|
|
22
|
+
const manifest: FunctionManifest = {
|
|
23
|
+
functions: [
|
|
24
|
+
{
|
|
25
|
+
id: 'example.com/function/test',
|
|
26
|
+
name: 'test',
|
|
27
|
+
handler: 'test',
|
|
28
|
+
},
|
|
29
|
+
],
|
|
30
|
+
triggers: [
|
|
31
|
+
{
|
|
32
|
+
function: 'example.com/function/test',
|
|
33
|
+
schedule: '* * * * * *', // Every second.
|
|
34
|
+
},
|
|
35
|
+
],
|
|
36
|
+
};
|
|
37
|
+
|
|
38
|
+
let count = 0;
|
|
39
|
+
const done = new Trigger();
|
|
40
|
+
const scheduler = new Scheduler(client, manifest, {
|
|
41
|
+
callback: async () => {
|
|
42
|
+
if (++count === 3) {
|
|
43
|
+
done.wake();
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
return 200;
|
|
47
|
+
},
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
await scheduler.start();
|
|
51
|
+
await done.wait();
|
|
52
|
+
expect(count).to.equal(3);
|
|
53
|
+
|
|
54
|
+
await scheduler.stop();
|
|
55
|
+
await client.destroy();
|
|
56
|
+
});
|
|
57
|
+
});
|
package/src/runtime/scheduler.ts
CHANGED
|
@@ -4,19 +4,23 @@
|
|
|
4
4
|
|
|
5
5
|
import { CronJob } from 'cron';
|
|
6
6
|
|
|
7
|
-
import { DeferredTask } from '@dxos/async';
|
|
7
|
+
import { debounce, DeferredTask } from '@dxos/async';
|
|
8
8
|
import { type Client, type PublicKey } from '@dxos/client';
|
|
9
|
-
import { type Space } from '@dxos/client/echo';
|
|
9
|
+
import { type Space, TextObject } from '@dxos/client/echo';
|
|
10
10
|
import { Context } from '@dxos/context';
|
|
11
|
-
import { Filter, createSubscription } from '@dxos/echo-schema';
|
|
11
|
+
import { Filter, createSubscription, type Query, subscribe } from '@dxos/echo-schema';
|
|
12
12
|
import { invariant } from '@dxos/invariant';
|
|
13
13
|
import { log } from '@dxos/log';
|
|
14
14
|
import { ComplexMap } from '@dxos/util';
|
|
15
15
|
|
|
16
|
-
import { type
|
|
16
|
+
import { type FunctionSubscriptionEvent } from '../handler';
|
|
17
|
+
import { type FunctionDef, type FunctionManifest, type FunctionTrigger, type TriggerSubscription } from '../manifest';
|
|
18
|
+
|
|
19
|
+
type Callback = (data: FunctionSubscriptionEvent) => Promise<number>;
|
|
17
20
|
|
|
18
21
|
type SchedulerOptions = {
|
|
19
|
-
endpoint
|
|
22
|
+
endpoint?: string;
|
|
23
|
+
callback?: Callback;
|
|
20
24
|
};
|
|
21
25
|
|
|
22
26
|
/**
|
|
@@ -33,7 +37,7 @@ export class Scheduler {
|
|
|
33
37
|
constructor(
|
|
34
38
|
private readonly _client: Client,
|
|
35
39
|
private readonly _manifest: FunctionManifest,
|
|
36
|
-
private readonly _options: SchedulerOptions,
|
|
40
|
+
private readonly _options: SchedulerOptions = {},
|
|
37
41
|
) {}
|
|
38
42
|
|
|
39
43
|
async start() {
|
|
@@ -58,6 +62,7 @@ export class Scheduler {
|
|
|
58
62
|
const def = this._manifest.functions.find((config) => config.id === trigger.function);
|
|
59
63
|
invariant(def, `Function not found: ${trigger.function}`);
|
|
60
64
|
|
|
65
|
+
// Currently supports only one trigger declaration per function.
|
|
61
66
|
const exists = this._mounts.get(key);
|
|
62
67
|
if (!exists) {
|
|
63
68
|
this._mounts.set(key, { ctx, trigger });
|
|
@@ -66,52 +71,14 @@ export class Scheduler {
|
|
|
66
71
|
return;
|
|
67
72
|
}
|
|
68
73
|
|
|
69
|
-
//
|
|
74
|
+
// Timer.
|
|
70
75
|
if (trigger.schedule) {
|
|
71
|
-
|
|
72
|
-
await this.execFunction(def, {
|
|
73
|
-
space: space.key,
|
|
74
|
-
});
|
|
75
|
-
});
|
|
76
|
-
|
|
77
|
-
// TODO(burdon): Check greater than 30s min (use cron-parser).
|
|
78
|
-
const job = new CronJob(trigger.schedule, () => task.schedule());
|
|
79
|
-
|
|
80
|
-
job.start();
|
|
81
|
-
ctx.onDispose(() => job.stop());
|
|
76
|
+
this._createTimer(ctx, space, def, trigger);
|
|
82
77
|
}
|
|
83
78
|
|
|
84
|
-
//
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
const task = new DeferredTask(ctx, async () => {
|
|
88
|
-
await this.execFunction(def, {
|
|
89
|
-
space: space.key,
|
|
90
|
-
objects: Array.from(objectIds),
|
|
91
|
-
});
|
|
92
|
-
});
|
|
93
|
-
|
|
94
|
-
const subscription = createSubscription(({ added, updated }) => {
|
|
95
|
-
for (const object of added) {
|
|
96
|
-
objectIds.add(object.id);
|
|
97
|
-
}
|
|
98
|
-
for (const object of updated) {
|
|
99
|
-
objectIds.add(object.id);
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
task.schedule();
|
|
103
|
-
});
|
|
104
|
-
|
|
105
|
-
const { type, props } = trigger.subscription;
|
|
106
|
-
const query = space.db.query(Filter.typename(type, props));
|
|
107
|
-
const unsubscribe = query.subscribe(({ objects }) => {
|
|
108
|
-
subscription.update(objects);
|
|
109
|
-
}, true);
|
|
110
|
-
|
|
111
|
-
ctx.onDispose(() => {
|
|
112
|
-
subscription.unsubscribe();
|
|
113
|
-
unsubscribe();
|
|
114
|
-
});
|
|
79
|
+
// Subscription.
|
|
80
|
+
for (const triggerSubscription of trigger.subscriptions ?? []) {
|
|
81
|
+
this._createSubscription(ctx, space, def, triggerSubscription);
|
|
115
82
|
}
|
|
116
83
|
}
|
|
117
84
|
}
|
|
@@ -125,19 +92,92 @@ export class Scheduler {
|
|
|
125
92
|
}
|
|
126
93
|
}
|
|
127
94
|
|
|
128
|
-
private
|
|
95
|
+
private _createTimer(ctx: Context, space: Space, def: FunctionDef, trigger: FunctionTrigger) {
|
|
96
|
+
const task = new DeferredTask(ctx, async () => {
|
|
97
|
+
await this._execFunction(def, {
|
|
98
|
+
space: space.key,
|
|
99
|
+
});
|
|
100
|
+
});
|
|
101
|
+
|
|
102
|
+
// TODO(burdon): Check greater than 30s min (use cron-parser).
|
|
103
|
+
invariant(trigger.schedule);
|
|
104
|
+
const job = new CronJob(trigger.schedule, () => task.schedule());
|
|
105
|
+
|
|
106
|
+
job.start();
|
|
107
|
+
ctx.onDispose(() => job.stop());
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
private _createSubscription(ctx: Context, space: Space, def: FunctionDef, triggerSubscription: TriggerSubscription) {
|
|
111
|
+
const objectIds = new Set<string>();
|
|
112
|
+
const task = new DeferredTask(ctx, async () => {
|
|
113
|
+
await this._execFunction(def, {
|
|
114
|
+
space: space.key,
|
|
115
|
+
objects: Array.from(objectIds),
|
|
116
|
+
});
|
|
117
|
+
});
|
|
118
|
+
|
|
119
|
+
// TODO(burdon): Standardize subscription handles.
|
|
120
|
+
const subscriptions: (() => void)[] = [];
|
|
121
|
+
const subscription = createSubscription(({ added, updated }) => {
|
|
122
|
+
for (const object of added) {
|
|
123
|
+
objectIds.add(object.id);
|
|
124
|
+
}
|
|
125
|
+
for (const object of updated) {
|
|
126
|
+
objectIds.add(object.id);
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
task.schedule();
|
|
130
|
+
});
|
|
131
|
+
subscriptions.push(() => subscription.unsubscribe());
|
|
132
|
+
|
|
133
|
+
const { type, props, deep, delay } = triggerSubscription;
|
|
134
|
+
const update = ({ objects }: Query) => {
|
|
135
|
+
subscription.update(objects);
|
|
136
|
+
|
|
137
|
+
// TODO(burdon): Hack to monitor changes to Document's text object.
|
|
138
|
+
if (deep) {
|
|
139
|
+
log.info('update', { type, deep, objects: objects.length });
|
|
140
|
+
for (const object of objects) {
|
|
141
|
+
const content = object.content;
|
|
142
|
+
if (content instanceof TextObject) {
|
|
143
|
+
subscriptions.push(content[subscribe](debounce(() => subscription.update([object]), 1_000)));
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
};
|
|
148
|
+
|
|
149
|
+
// TODO(burdon): [Bug]: all callbacks are fired on the first mutation.
|
|
150
|
+
// TODO(burdon): [Bug]: not updated when document is deleted (either top or hierarchically).
|
|
151
|
+
const query = space.db.query(Filter.typename(type, props));
|
|
152
|
+
subscriptions.push(query.subscribe(delay ? debounce(update, delay * 1_000) : update));
|
|
153
|
+
|
|
154
|
+
ctx.onDispose(() => {
|
|
155
|
+
subscriptions.forEach((unsubscribe) => unsubscribe());
|
|
156
|
+
});
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
private async _execFunction(def: FunctionDef, data: any) {
|
|
129
160
|
try {
|
|
130
161
|
log('request', { function: def.id });
|
|
131
|
-
const
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
}
|
|
136
|
-
|
|
137
|
-
|
|
162
|
+
const { endpoint, callback } = this._options;
|
|
163
|
+
let status = 0;
|
|
164
|
+
if (endpoint) {
|
|
165
|
+
// TODO(burdon): Move out of scheduler (generalize as callback).
|
|
166
|
+
const response = await fetch(`${this._options.endpoint}/${def.name}`, {
|
|
167
|
+
method: 'POST',
|
|
168
|
+
headers: {
|
|
169
|
+
'Content-Type': 'application/json',
|
|
170
|
+
},
|
|
171
|
+
body: JSON.stringify(data),
|
|
172
|
+
});
|
|
173
|
+
|
|
174
|
+
status = response.status;
|
|
175
|
+
} else if (callback) {
|
|
176
|
+
status = await callback(data);
|
|
177
|
+
}
|
|
138
178
|
|
|
139
179
|
// const result = await response.json();
|
|
140
|
-
log('result', { function: def.id, result:
|
|
180
|
+
log('result', { function: def.id, result: status });
|
|
141
181
|
} catch (err: any) {
|
|
142
182
|
log.error('error', { function: def.id, error: err.message });
|
|
143
183
|
}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"function.test.d.ts","sourceRoot":"","sources":["../../../src/function.test.ts"],"names":[],"mappings":""}
|