@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.
- package/dist/lib/browser/chunk-4D4I3YMJ.mjs +86 -0
- package/dist/lib/browser/chunk-4D4I3YMJ.mjs.map +7 -0
- package/dist/lib/browser/index.mjs +915 -297
- package/dist/lib/browser/index.mjs.map +4 -4
- package/dist/lib/browser/meta.json +1 -1
- package/dist/lib/browser/types.mjs +14 -0
- package/dist/lib/browser/types.mjs.map +7 -0
- package/dist/lib/node/chunk-3UYUR5N5.cjs +103 -0
- package/dist/lib/node/chunk-3UYUR5N5.cjs.map +7 -0
- package/dist/lib/node/index.cjs +901 -290
- package/dist/lib/node/index.cjs.map +4 -4
- package/dist/lib/node/meta.json +1 -1
- package/dist/lib/node/types.cjs +35 -0
- package/dist/lib/node/types.cjs.map +7 -0
- package/dist/types/src/browser/index.d.ts +2 -0
- package/dist/types/src/browser/index.d.ts.map +1 -0
- package/dist/types/src/function/function-registry.d.ts +24 -0
- package/dist/types/src/function/function-registry.d.ts.map +1 -0
- package/dist/types/src/function/function-registry.test.d.ts +2 -0
- package/dist/types/src/function/function-registry.test.d.ts.map +1 -0
- package/dist/types/src/function/index.d.ts +2 -0
- package/dist/types/src/function/index.d.ts.map +1 -0
- package/dist/types/src/handler.d.ts +33 -12
- package/dist/types/src/handler.d.ts.map +1 -1
- package/dist/types/src/index.d.ts +3 -1
- package/dist/types/src/index.d.ts.map +1 -1
- package/dist/types/src/runtime/dev-server.d.ts +15 -7
- package/dist/types/src/runtime/dev-server.d.ts.map +1 -1
- package/dist/types/src/runtime/dev-server.test.d.ts +2 -0
- package/dist/types/src/runtime/dev-server.test.d.ts.map +1 -0
- package/dist/types/src/runtime/scheduler.d.ts +15 -15
- 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 +4 -0
- package/dist/types/src/testing/test/handler.d.ts.map +1 -0
- package/dist/types/src/testing/test/index.d.ts +3 -0
- package/dist/types/src/testing/test/index.d.ts.map +1 -0
- 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 +37 -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 +215 -0
- package/dist/types/src/types.d.ts.map +1 -0
- package/dist/types/tools/schema.d.ts +2 -0
- package/dist/types/tools/schema.d.ts.map +1 -0
- package/package.json +38 -13
- package/schema/functions.json +211 -0
- package/src/browser/index.ts +5 -0
- package/src/function/function-registry.test.ts +105 -0
- package/src/function/function-registry.ts +90 -0
- package/src/function/index.ts +5 -0
- package/src/handler.ts +56 -26
- package/src/index.ts +3 -1
- package/src/runtime/dev-server.test.ts +60 -0
- package/src/runtime/dev-server.ts +104 -53
- package/src/runtime/scheduler.test.ts +159 -21
- package/src/runtime/scheduler.ts +87 -150
- package/src/testing/functions-integration.test.ts +100 -0
- package/src/testing/index.ts +7 -0
- package/src/testing/setup.ts +43 -0
- package/src/testing/test/handler.ts +15 -0
- package/src/testing/test/index.ts +7 -0
- 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 +272 -0
- package/src/trigger/trigger-registry.ts +211 -0
- package/src/trigger/type/index.ts +8 -0
- package/src/trigger/type/subscription-trigger.ts +86 -0
- package/src/trigger/type/timer-trigger.ts +45 -0
- package/src/trigger/type/webhook-trigger.ts +48 -0
- package/src/trigger/type/websocket-trigger.ts +92 -0
- package/src/types.ts +115 -0
- package/dist/types/src/manifest.d.ts +0 -26
- package/dist/types/src/manifest.d.ts.map +0 -1
- package/src/manifest.ts +0 -42
|
@@ -0,0 +1,211 @@
|
|
|
1
|
+
{
|
|
2
|
+
"$schema": "http://json-schema.org/draft-07/schema#",
|
|
3
|
+
"type": "object",
|
|
4
|
+
"required": [],
|
|
5
|
+
"properties": {
|
|
6
|
+
"functions": {
|
|
7
|
+
"type": "array",
|
|
8
|
+
"items": {
|
|
9
|
+
"type": "object",
|
|
10
|
+
"required": [
|
|
11
|
+
"uri",
|
|
12
|
+
"route",
|
|
13
|
+
"handler"
|
|
14
|
+
],
|
|
15
|
+
"properties": {
|
|
16
|
+
"uri": {
|
|
17
|
+
"type": "string",
|
|
18
|
+
"description": "a string",
|
|
19
|
+
"title": "string"
|
|
20
|
+
},
|
|
21
|
+
"route": {
|
|
22
|
+
"type": "string",
|
|
23
|
+
"description": "a string",
|
|
24
|
+
"title": "string"
|
|
25
|
+
},
|
|
26
|
+
"handler": {
|
|
27
|
+
"type": "string",
|
|
28
|
+
"description": "a string",
|
|
29
|
+
"title": "string"
|
|
30
|
+
},
|
|
31
|
+
"description": {
|
|
32
|
+
"type": "string",
|
|
33
|
+
"description": "a string",
|
|
34
|
+
"title": "string"
|
|
35
|
+
}
|
|
36
|
+
},
|
|
37
|
+
"additionalProperties": false
|
|
38
|
+
}
|
|
39
|
+
},
|
|
40
|
+
"triggers": {
|
|
41
|
+
"type": "array",
|
|
42
|
+
"items": {
|
|
43
|
+
"type": "object",
|
|
44
|
+
"required": [
|
|
45
|
+
"function",
|
|
46
|
+
"spec"
|
|
47
|
+
],
|
|
48
|
+
"properties": {
|
|
49
|
+
"function": {
|
|
50
|
+
"type": "string",
|
|
51
|
+
"description": "Function URI.",
|
|
52
|
+
"title": "string"
|
|
53
|
+
},
|
|
54
|
+
"enabled": {
|
|
55
|
+
"type": "boolean",
|
|
56
|
+
"description": "a boolean",
|
|
57
|
+
"title": "boolean"
|
|
58
|
+
},
|
|
59
|
+
"meta": {
|
|
60
|
+
"$ref": "#/$defs/object"
|
|
61
|
+
},
|
|
62
|
+
"spec": {
|
|
63
|
+
"anyOf": [
|
|
64
|
+
{
|
|
65
|
+
"type": "object",
|
|
66
|
+
"required": [
|
|
67
|
+
"type",
|
|
68
|
+
"cron"
|
|
69
|
+
],
|
|
70
|
+
"properties": {
|
|
71
|
+
"type": {
|
|
72
|
+
"const": "timer"
|
|
73
|
+
},
|
|
74
|
+
"cron": {
|
|
75
|
+
"type": "string",
|
|
76
|
+
"description": "a string",
|
|
77
|
+
"title": "string"
|
|
78
|
+
}
|
|
79
|
+
},
|
|
80
|
+
"additionalProperties": false
|
|
81
|
+
},
|
|
82
|
+
{
|
|
83
|
+
"type": "object",
|
|
84
|
+
"required": [
|
|
85
|
+
"type",
|
|
86
|
+
"method"
|
|
87
|
+
],
|
|
88
|
+
"properties": {
|
|
89
|
+
"type": {
|
|
90
|
+
"const": "webhook"
|
|
91
|
+
},
|
|
92
|
+
"method": {
|
|
93
|
+
"type": "string",
|
|
94
|
+
"description": "a string",
|
|
95
|
+
"title": "string"
|
|
96
|
+
},
|
|
97
|
+
"port": {
|
|
98
|
+
"type": "number",
|
|
99
|
+
"description": "a number",
|
|
100
|
+
"title": "number"
|
|
101
|
+
}
|
|
102
|
+
},
|
|
103
|
+
"additionalProperties": false
|
|
104
|
+
},
|
|
105
|
+
{
|
|
106
|
+
"type": "object",
|
|
107
|
+
"required": [
|
|
108
|
+
"type",
|
|
109
|
+
"url"
|
|
110
|
+
],
|
|
111
|
+
"properties": {
|
|
112
|
+
"type": {
|
|
113
|
+
"const": "websocket"
|
|
114
|
+
},
|
|
115
|
+
"url": {
|
|
116
|
+
"type": "string",
|
|
117
|
+
"description": "a string",
|
|
118
|
+
"title": "string"
|
|
119
|
+
},
|
|
120
|
+
"init": {
|
|
121
|
+
"type": "object",
|
|
122
|
+
"required": [],
|
|
123
|
+
"properties": {},
|
|
124
|
+
"additionalProperties": {
|
|
125
|
+
"$id": "/schemas/any",
|
|
126
|
+
"title": "any"
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
},
|
|
130
|
+
"additionalProperties": false
|
|
131
|
+
},
|
|
132
|
+
{
|
|
133
|
+
"type": "object",
|
|
134
|
+
"required": [
|
|
135
|
+
"type",
|
|
136
|
+
"filter"
|
|
137
|
+
],
|
|
138
|
+
"properties": {
|
|
139
|
+
"type": {
|
|
140
|
+
"const": "subscription"
|
|
141
|
+
},
|
|
142
|
+
"filter": {
|
|
143
|
+
"type": "array",
|
|
144
|
+
"items": {
|
|
145
|
+
"type": "object",
|
|
146
|
+
"required": [
|
|
147
|
+
"type"
|
|
148
|
+
],
|
|
149
|
+
"properties": {
|
|
150
|
+
"type": {
|
|
151
|
+
"type": "string",
|
|
152
|
+
"description": "a string",
|
|
153
|
+
"title": "string"
|
|
154
|
+
},
|
|
155
|
+
"props": {
|
|
156
|
+
"type": "object",
|
|
157
|
+
"required": [],
|
|
158
|
+
"properties": {},
|
|
159
|
+
"additionalProperties": {
|
|
160
|
+
"$id": "/schemas/any",
|
|
161
|
+
"title": "any"
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
},
|
|
165
|
+
"additionalProperties": false
|
|
166
|
+
}
|
|
167
|
+
},
|
|
168
|
+
"options": {
|
|
169
|
+
"type": "object",
|
|
170
|
+
"required": [],
|
|
171
|
+
"properties": {
|
|
172
|
+
"deep": {
|
|
173
|
+
"type": "boolean",
|
|
174
|
+
"description": "a boolean",
|
|
175
|
+
"title": "boolean"
|
|
176
|
+
},
|
|
177
|
+
"delay": {
|
|
178
|
+
"type": "number",
|
|
179
|
+
"description": "a number",
|
|
180
|
+
"title": "number"
|
|
181
|
+
}
|
|
182
|
+
},
|
|
183
|
+
"additionalProperties": false
|
|
184
|
+
}
|
|
185
|
+
},
|
|
186
|
+
"additionalProperties": false
|
|
187
|
+
}
|
|
188
|
+
]
|
|
189
|
+
}
|
|
190
|
+
},
|
|
191
|
+
"additionalProperties": false
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
},
|
|
195
|
+
"additionalProperties": false,
|
|
196
|
+
"$defs": {
|
|
197
|
+
"object": {
|
|
198
|
+
"$id": "/schemas/object",
|
|
199
|
+
"oneOf": [
|
|
200
|
+
{
|
|
201
|
+
"type": "object"
|
|
202
|
+
},
|
|
203
|
+
{
|
|
204
|
+
"type": "array"
|
|
205
|
+
}
|
|
206
|
+
],
|
|
207
|
+
"description": "an object in the TypeScript meaning, i.e. the `object` type",
|
|
208
|
+
"title": "object"
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
}
|
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2023 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import { expect } from 'chai';
|
|
6
|
+
|
|
7
|
+
import { Trigger } from '@dxos/async';
|
|
8
|
+
import { type Client } from '@dxos/client';
|
|
9
|
+
import { TestBuilder } from '@dxos/client/testing';
|
|
10
|
+
import { Context } from '@dxos/context';
|
|
11
|
+
import { Filter } from '@dxos/echo-db';
|
|
12
|
+
import { create } from '@dxos/echo-schema';
|
|
13
|
+
import { describe, test } from '@dxos/test';
|
|
14
|
+
import { range } from '@dxos/util';
|
|
15
|
+
|
|
16
|
+
import { FunctionRegistry } from './function-registry';
|
|
17
|
+
import { createInitializedClients } from '../testing';
|
|
18
|
+
import { FunctionDef, type FunctionManifest } from '../types';
|
|
19
|
+
|
|
20
|
+
const testManifest: FunctionManifest = {
|
|
21
|
+
functions: [
|
|
22
|
+
{
|
|
23
|
+
uri: 'dxos.functions.test/hello',
|
|
24
|
+
route: '/hello',
|
|
25
|
+
handler: 'test',
|
|
26
|
+
},
|
|
27
|
+
],
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
describe('function registry', () => {
|
|
31
|
+
let ctx: Context;
|
|
32
|
+
let testBuilder: TestBuilder;
|
|
33
|
+
beforeEach(async () => {
|
|
34
|
+
ctx = new Context();
|
|
35
|
+
testBuilder = new TestBuilder();
|
|
36
|
+
});
|
|
37
|
+
afterEach(async () => {
|
|
38
|
+
await ctx.dispose();
|
|
39
|
+
await testBuilder.destroy();
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
describe('register', () => {
|
|
43
|
+
test('creates new functions', async () => {
|
|
44
|
+
const client = (await createInitializedClients(testBuilder))[0];
|
|
45
|
+
const registry = createRegistry(client);
|
|
46
|
+
const space = await client.spaces.create();
|
|
47
|
+
await registry.register(space, testManifest.functions);
|
|
48
|
+
const { objects: definitions } = await space.db.query(Filter.schema(FunctionDef)).run();
|
|
49
|
+
expect(definitions.length).to.eq(1);
|
|
50
|
+
expect(definitions[0].uri).to.eq(testManifest.functions?.[0]?.uri);
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
test('de-duplicates by function URI', async () => {
|
|
54
|
+
const client = (await createInitializedClients(testBuilder))[0];
|
|
55
|
+
const registry = createRegistry(client);
|
|
56
|
+
const space = await client.spaces.create();
|
|
57
|
+
const existing = space.db.add(create(FunctionDef, testManifest.functions![0]));
|
|
58
|
+
await registry.register(space, testManifest.functions);
|
|
59
|
+
const { objects: definitions } = await space.db.query(Filter.schema(FunctionDef)).run();
|
|
60
|
+
expect(definitions.length).to.eq(1);
|
|
61
|
+
expect(definitions[0].uri).to.eq(existing.uri);
|
|
62
|
+
});
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
describe('onFunctionsRegistered', () => {
|
|
66
|
+
test('called with all registered when opened', async () => {
|
|
67
|
+
const client = (await createInitializedClients(testBuilder))[0];
|
|
68
|
+
const registry = createRegistry(client);
|
|
69
|
+
const space = await client.spaces.create();
|
|
70
|
+
const definitions = range(3, () => create(FunctionDef, testManifest.functions![0]));
|
|
71
|
+
definitions.forEach((def) => space.db.add(def));
|
|
72
|
+
|
|
73
|
+
const functionRegistered = new Trigger<FunctionDef[]>();
|
|
74
|
+
registry.registered.on((fn) => {
|
|
75
|
+
functionRegistered.wake(fn.added);
|
|
76
|
+
});
|
|
77
|
+
void registry.open(ctx);
|
|
78
|
+
const functions = await functionRegistered.wait();
|
|
79
|
+
const expected = definitions.map((def) => def.uri).sort();
|
|
80
|
+
expect(functions.map((fn) => fn.uri).sort()).to.deep.eq(expected);
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
test('called when a new functions is added', async () => {
|
|
84
|
+
const client = (await createInitializedClients(testBuilder))[0];
|
|
85
|
+
const registry = createRegistry(client);
|
|
86
|
+
const space = await client.spaces.create();
|
|
87
|
+
|
|
88
|
+
const functionRegistered = new Trigger<FunctionDef>();
|
|
89
|
+
registry.registered.on((fn) => {
|
|
90
|
+
expect(fn.added.length).to.eq(1);
|
|
91
|
+
functionRegistered.wake(fn.added[0]);
|
|
92
|
+
});
|
|
93
|
+
await registry.open(ctx);
|
|
94
|
+
await registry.register(space, testManifest.functions);
|
|
95
|
+
const registered = await functionRegistered.wait();
|
|
96
|
+
expect(registered.uri).to.eq(testManifest.functions![0].uri);
|
|
97
|
+
});
|
|
98
|
+
});
|
|
99
|
+
|
|
100
|
+
const createRegistry = (client: Client) => {
|
|
101
|
+
const registry = new FunctionRegistry(client);
|
|
102
|
+
ctx.onDispose(() => registry.close());
|
|
103
|
+
return registry;
|
|
104
|
+
};
|
|
105
|
+
});
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2024 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import { Event } from '@dxos/async';
|
|
6
|
+
import { type Client } from '@dxos/client';
|
|
7
|
+
import { create, Filter, type Space } from '@dxos/client/echo';
|
|
8
|
+
import { type Context, Resource } from '@dxos/context';
|
|
9
|
+
import { PublicKey } from '@dxos/keys';
|
|
10
|
+
import { log } from '@dxos/log';
|
|
11
|
+
import { ComplexMap, diff } from '@dxos/util';
|
|
12
|
+
|
|
13
|
+
import { FunctionDef, type FunctionManifest } from '../types';
|
|
14
|
+
|
|
15
|
+
export type FunctionsRegisteredEvent = {
|
|
16
|
+
space: Space;
|
|
17
|
+
added: FunctionDef[];
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
export class FunctionRegistry extends Resource {
|
|
21
|
+
private readonly _functionBySpaceKey = new ComplexMap<PublicKey, FunctionDef[]>(PublicKey.hash);
|
|
22
|
+
|
|
23
|
+
public readonly registered = new Event<FunctionsRegisteredEvent>();
|
|
24
|
+
|
|
25
|
+
constructor(private readonly _client: Client) {
|
|
26
|
+
super();
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
public getFunctions(space: Space): FunctionDef[] {
|
|
30
|
+
return this._functionBySpaceKey.get(space.key) ?? [];
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Loads function definitions from the manifest into the space.
|
|
35
|
+
* We first load all the definitions from the space to deduplicate by functionId.
|
|
36
|
+
*/
|
|
37
|
+
public async register(space: Space, functions: FunctionManifest['functions']): Promise<void> {
|
|
38
|
+
log('register', { space: space.key, functions: functions?.length ?? 0 });
|
|
39
|
+
if (!functions?.length) {
|
|
40
|
+
return;
|
|
41
|
+
}
|
|
42
|
+
if (!space.db.graph.runtimeSchemaRegistry.hasSchema(FunctionDef)) {
|
|
43
|
+
space.db.graph.runtimeSchemaRegistry.registerSchema(FunctionDef);
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
// Sync definitions.
|
|
47
|
+
const { objects: existing } = await space.db.query(Filter.schema(FunctionDef)).run();
|
|
48
|
+
const { added } = diff(existing, functions, (a, b) => a.uri === b.uri);
|
|
49
|
+
// TODO(burdon): Update existing templates.
|
|
50
|
+
added.forEach((def) => space.db.add(create(FunctionDef, def)));
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
protected override async _open(): Promise<void> {
|
|
54
|
+
log.info('opening...');
|
|
55
|
+
const spacesSubscription = this._client.spaces.subscribe(async (spaces) => {
|
|
56
|
+
for (const space of spaces) {
|
|
57
|
+
if (this._functionBySpaceKey.has(space.key)) {
|
|
58
|
+
continue;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
const registered: FunctionDef[] = [];
|
|
62
|
+
this._functionBySpaceKey.set(space.key, registered);
|
|
63
|
+
await space.waitUntilReady();
|
|
64
|
+
if (this._ctx.disposed) {
|
|
65
|
+
break;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
// Subscribe to updates.
|
|
69
|
+
this._ctx.onDispose(
|
|
70
|
+
space.db.query(Filter.schema(FunctionDef)).subscribe(({ objects }) => {
|
|
71
|
+
const { added } = diff(registered, objects, (a, b) => a.uri === b.uri);
|
|
72
|
+
// TODO(burdon): Update and remove.
|
|
73
|
+
if (added.length > 0) {
|
|
74
|
+
registered.push(...added);
|
|
75
|
+
this.registered.emit({ space, added });
|
|
76
|
+
}
|
|
77
|
+
}),
|
|
78
|
+
);
|
|
79
|
+
}
|
|
80
|
+
});
|
|
81
|
+
|
|
82
|
+
// TODO(burdon): API: Normalize unsubscribe methods.
|
|
83
|
+
this._ctx.onDispose(() => spacesSubscription.unsubscribe());
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
protected override async _close(_: Context): Promise<void> {
|
|
87
|
+
log.info('closing...');
|
|
88
|
+
this._functionBySpaceKey.clear();
|
|
89
|
+
}
|
|
90
|
+
}
|
package/src/handler.ts
CHANGED
|
@@ -8,31 +8,61 @@ import { type EchoReactiveObject } from '@dxos/echo-schema';
|
|
|
8
8
|
import { log } from '@dxos/log';
|
|
9
9
|
import { nonNullable } from '@dxos/util';
|
|
10
10
|
|
|
11
|
-
// TODO(burdon):
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
11
|
+
// TODO(burdon): Model after http request. Ref Lambda/OpenFaaS.
|
|
12
|
+
// https://docs.aws.amazon.com/lambda/latest/dg/typescript-handler.html
|
|
13
|
+
// https://www.serverless.com/framework/docs/providers/aws/guide/serverless.yml/#functions
|
|
14
|
+
// https://www.npmjs.com/package/aws-lambda
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Function handler.
|
|
18
|
+
*/
|
|
19
|
+
export type FunctionHandler<TData = {}, TMeta = {}> = (params: {
|
|
20
|
+
context: FunctionContext;
|
|
21
|
+
event: FunctionEvent<TData, TMeta>;
|
|
22
|
+
response: FunctionResponse;
|
|
23
|
+
}) => Promise<FunctionResponse | void>;
|
|
15
24
|
|
|
16
|
-
|
|
25
|
+
/**
|
|
26
|
+
* Function context.
|
|
27
|
+
*/
|
|
17
28
|
export interface FunctionContext {
|
|
29
|
+
// TODO(burdon): Limit access to individual space.
|
|
18
30
|
client: Client;
|
|
31
|
+
// TODO(burdon): Replace with storage service abstraction.
|
|
19
32
|
dataDir?: string;
|
|
20
33
|
}
|
|
21
34
|
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
35
|
+
/**
|
|
36
|
+
* Event payload.
|
|
37
|
+
*/
|
|
38
|
+
export type FunctionEvent<TData = {}, TMeta = {}> = {
|
|
39
|
+
data: FunctionEventMeta<TMeta> & TData;
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* Metadata from trigger.
|
|
44
|
+
*/
|
|
45
|
+
export type FunctionEventMeta<TMeta = {}> = {
|
|
46
|
+
meta: TMeta;
|
|
47
|
+
};
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* Function response.
|
|
51
|
+
*/
|
|
52
|
+
export interface FunctionResponse {
|
|
53
|
+
status(code: number): FunctionResponse;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
//
|
|
57
|
+
// Subscription utils.
|
|
58
|
+
//
|
|
29
59
|
|
|
30
|
-
export type
|
|
31
|
-
|
|
60
|
+
export type RawSubscriptionData = {
|
|
61
|
+
spaceKey?: string;
|
|
32
62
|
objects?: string[];
|
|
33
63
|
};
|
|
34
64
|
|
|
35
|
-
export type
|
|
65
|
+
export type SubscriptionData = {
|
|
36
66
|
space?: Space;
|
|
37
67
|
objects?: EchoReactiveObject<any>[];
|
|
38
68
|
};
|
|
@@ -47,22 +77,22 @@ export type FunctionSubscriptionEvent2 = {
|
|
|
47
77
|
*
|
|
48
78
|
* NOTE: Get space key from devtools or `dx space list --json`
|
|
49
79
|
*/
|
|
50
|
-
export const subscriptionHandler = (
|
|
51
|
-
handler: FunctionHandler<
|
|
52
|
-
): FunctionHandler<
|
|
53
|
-
return ({ event, context, ...rest }) => {
|
|
80
|
+
export const subscriptionHandler = <TMeta>(
|
|
81
|
+
handler: FunctionHandler<SubscriptionData, TMeta>,
|
|
82
|
+
): FunctionHandler<RawSubscriptionData, TMeta> => {
|
|
83
|
+
return ({ event: { data }, context, ...rest }) => {
|
|
54
84
|
const { client } = context;
|
|
55
|
-
const space =
|
|
56
|
-
const objects =
|
|
57
|
-
space
|
|
58
|
-
|
|
85
|
+
const space = data.spaceKey ? client.spaces.get(PublicKey.from(data.spaceKey)) : undefined;
|
|
86
|
+
const objects = space
|
|
87
|
+
? data.objects?.map<EchoReactiveObject<any> | undefined>((id) => space!.db.getObjectById(id)).filter(nonNullable)
|
|
88
|
+
: [];
|
|
59
89
|
|
|
60
|
-
if (!!
|
|
61
|
-
log.warn('invalid space', {
|
|
90
|
+
if (!!data.spaceKey && !space) {
|
|
91
|
+
log.warn('invalid space', { data });
|
|
62
92
|
} else {
|
|
63
93
|
log.info('handler', { space: space?.key.truncate(), objects: objects?.length });
|
|
64
94
|
}
|
|
65
95
|
|
|
66
|
-
return handler({ event: { space, objects }, context, ...rest });
|
|
96
|
+
return handler({ event: { data: { ...data, space, objects } }, context, ...rest });
|
|
67
97
|
};
|
|
68
98
|
};
|
package/src/index.ts
CHANGED
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2023 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import { expect } from 'chai';
|
|
6
|
+
import path from 'path';
|
|
7
|
+
|
|
8
|
+
import { waitForCondition } from '@dxos/async';
|
|
9
|
+
import { type Client } from '@dxos/client';
|
|
10
|
+
import { TestBuilder } from '@dxos/client/testing';
|
|
11
|
+
import { describe, test } from '@dxos/test';
|
|
12
|
+
|
|
13
|
+
import { DevServer } from './dev-server';
|
|
14
|
+
import { FunctionRegistry } from '../function';
|
|
15
|
+
import { createFunctionRuntime } from '../testing';
|
|
16
|
+
import { type FunctionManifest } from '../types';
|
|
17
|
+
|
|
18
|
+
describe('dev server', () => {
|
|
19
|
+
let client: Client;
|
|
20
|
+
let testBuilder: TestBuilder;
|
|
21
|
+
before(async () => {
|
|
22
|
+
testBuilder = new TestBuilder();
|
|
23
|
+
client = await createFunctionRuntime(testBuilder);
|
|
24
|
+
expect(client.services.services.FunctionRegistryService).to.exist;
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
after(async () => {
|
|
28
|
+
await testBuilder.destroy();
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
test('start/stop', async () => {
|
|
32
|
+
const manifest: FunctionManifest = {
|
|
33
|
+
functions: [
|
|
34
|
+
{
|
|
35
|
+
uri: 'example.com/function/test',
|
|
36
|
+
route: 'test',
|
|
37
|
+
handler: 'test',
|
|
38
|
+
},
|
|
39
|
+
],
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
const registry = new FunctionRegistry(client);
|
|
43
|
+
const server = new DevServer(client, registry, {
|
|
44
|
+
baseDir: path.join(__dirname, '../testing'),
|
|
45
|
+
});
|
|
46
|
+
const space = await client.spaces.create();
|
|
47
|
+
await registry.register(space, manifest.functions);
|
|
48
|
+
await server.start();
|
|
49
|
+
|
|
50
|
+
// TODO(burdon): Doesn't shut down cleanly.
|
|
51
|
+
// Error: invariant violation [this._client.services.services.FunctionRegistryService]
|
|
52
|
+
testBuilder.ctx.onDispose(() => server.stop());
|
|
53
|
+
expect(server).to.exist;
|
|
54
|
+
|
|
55
|
+
await waitForCondition({ condition: () => server.functions.length > 0 });
|
|
56
|
+
|
|
57
|
+
await server.invoke('test', {});
|
|
58
|
+
expect(server.stats.seq).to.eq(1);
|
|
59
|
+
});
|
|
60
|
+
});
|