@dxos/functions 0.5.4-main.962dfc7 → 0.5.4-main.a7f191d
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-ERL6PHMU.mjs +1084 -0
- package/dist/lib/browser/chunk-ERL6PHMU.mjs.map +7 -0
- package/dist/lib/browser/index.mjs +39 -1074
- package/dist/lib/browser/index.mjs.map +4 -4
- package/dist/lib/browser/meta.json +1 -1
- package/dist/lib/browser/testing/index.mjs +148 -0
- package/dist/lib/browser/testing/index.mjs.map +7 -0
- package/dist/lib/node/chunk-INM6XAL7.cjs +1097 -0
- package/dist/lib/node/chunk-INM6XAL7.cjs.map +7 -0
- package/dist/lib/node/index.cjs +36 -1067
- package/dist/lib/node/index.cjs.map +4 -4
- package/dist/lib/node/meta.json +1 -1
- package/dist/lib/node/testing/index.cjs +169 -0
- package/dist/lib/node/testing/index.cjs.map +7 -0
- package/dist/types/src/function/function-registry.d.ts +1 -0
- package/dist/types/src/function/function-registry.d.ts.map +1 -1
- package/dist/types/src/handler.d.ts +2 -2
- package/dist/types/src/handler.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/testing/index.d.ts +1 -0
- package/dist/types/src/testing/index.d.ts.map +1 -1
- package/dist/types/src/testing/manifest.d.ts +3 -0
- package/dist/types/src/testing/manifest.d.ts.map +1 -0
- package/dist/types/src/testing/plugin-init.d.ts +6 -0
- package/dist/types/src/testing/plugin-init.d.ts.map +1 -0
- package/dist/types/src/testing/setup.d.ts +11 -1
- package/dist/types/src/testing/setup.d.ts.map +1 -1
- package/dist/types/src/testing/util.d.ts +2 -0
- package/dist/types/src/testing/util.d.ts.map +1 -1
- package/package.json +23 -14
- package/src/function/function-registry.test.ts +14 -1
- package/src/function/function-registry.ts +10 -0
- package/src/handler.ts +22 -3
- package/src/runtime/dev-server.test.ts +42 -24
- package/src/runtime/dev-server.ts +17 -13
- package/src/testing/functions-integration.test.ts +8 -45
- package/src/testing/index.ts +1 -0
- package/src/testing/manifest.ts +15 -0
- package/src/testing/plugin-init.ts +20 -0
- package/src/testing/setup.ts +63 -6
- package/src/testing/util.ts +10 -0
|
@@ -1,123 +1,54 @@
|
|
|
1
1
|
import "@dxos/node-std/globals";
|
|
2
|
+
import {
|
|
3
|
+
DevServer,
|
|
4
|
+
FunctionRegistry,
|
|
5
|
+
Scheduler,
|
|
6
|
+
TriggerRegistry
|
|
7
|
+
} from "./chunk-ERL6PHMU.mjs";
|
|
2
8
|
import {
|
|
3
9
|
FUNCTION_SCHEMA,
|
|
4
10
|
FunctionDef,
|
|
5
11
|
FunctionManifestSchema,
|
|
6
|
-
FunctionTrigger
|
|
7
|
-
__require
|
|
12
|
+
FunctionTrigger
|
|
8
13
|
} from "./chunk-4D4I3YMJ.mjs";
|
|
9
14
|
|
|
10
|
-
// packages/core/functions/src/function/function-registry.ts
|
|
11
|
-
import { Event } from "@dxos/async";
|
|
12
|
-
import { create, Filter } from "@dxos/client/echo";
|
|
13
|
-
import { Resource } from "@dxos/context";
|
|
14
|
-
import { PublicKey } from "@dxos/keys";
|
|
15
|
-
import { log } from "@dxos/log";
|
|
16
|
-
import { ComplexMap, diff } from "@dxos/util";
|
|
17
|
-
var __dxlog_file = "/home/runner/work/dxos/dxos/packages/core/functions/src/function/function-registry.ts";
|
|
18
|
-
var FunctionRegistry = class extends Resource {
|
|
19
|
-
constructor(_client) {
|
|
20
|
-
super();
|
|
21
|
-
this._client = _client;
|
|
22
|
-
this._functionBySpaceKey = new ComplexMap(PublicKey.hash);
|
|
23
|
-
this.registered = new Event();
|
|
24
|
-
}
|
|
25
|
-
getFunctions(space) {
|
|
26
|
-
return this._functionBySpaceKey.get(space.key) ?? [];
|
|
27
|
-
}
|
|
28
|
-
/**
|
|
29
|
-
* Loads function definitions from the manifest into the space.
|
|
30
|
-
* We first load all the definitions from the space to deduplicate by functionId.
|
|
31
|
-
*/
|
|
32
|
-
async register(space, functions) {
|
|
33
|
-
log("register", {
|
|
34
|
-
space: space.key,
|
|
35
|
-
functions: functions?.length ?? 0
|
|
36
|
-
}, {
|
|
37
|
-
F: __dxlog_file,
|
|
38
|
-
L: 38,
|
|
39
|
-
S: this,
|
|
40
|
-
C: (f, a) => f(...a)
|
|
41
|
-
});
|
|
42
|
-
if (!functions?.length) {
|
|
43
|
-
return;
|
|
44
|
-
}
|
|
45
|
-
if (!space.db.graph.runtimeSchemaRegistry.hasSchema(FunctionDef)) {
|
|
46
|
-
space.db.graph.runtimeSchemaRegistry.registerSchema(FunctionDef);
|
|
47
|
-
}
|
|
48
|
-
const { objects: existing } = await space.db.query(Filter.schema(FunctionDef)).run();
|
|
49
|
-
const { added } = diff(existing, functions, (a, b) => a.uri === b.uri);
|
|
50
|
-
added.forEach((def) => space.db.add(create(FunctionDef, def)));
|
|
51
|
-
}
|
|
52
|
-
async _open() {
|
|
53
|
-
log.info("opening...", void 0, {
|
|
54
|
-
F: __dxlog_file,
|
|
55
|
-
L: 54,
|
|
56
|
-
S: this,
|
|
57
|
-
C: (f, a) => f(...a)
|
|
58
|
-
});
|
|
59
|
-
const spacesSubscription = this._client.spaces.subscribe(async (spaces) => {
|
|
60
|
-
for (const space of spaces) {
|
|
61
|
-
if (this._functionBySpaceKey.has(space.key)) {
|
|
62
|
-
continue;
|
|
63
|
-
}
|
|
64
|
-
const registered = [];
|
|
65
|
-
this._functionBySpaceKey.set(space.key, registered);
|
|
66
|
-
await space.waitUntilReady();
|
|
67
|
-
if (this._ctx.disposed) {
|
|
68
|
-
break;
|
|
69
|
-
}
|
|
70
|
-
this._ctx.onDispose(space.db.query(Filter.schema(FunctionDef)).subscribe(({ objects }) => {
|
|
71
|
-
const { added } = diff(registered, objects, (a, b) => a.uri === b.uri);
|
|
72
|
-
if (added.length > 0) {
|
|
73
|
-
registered.push(...added);
|
|
74
|
-
this.registered.emit({
|
|
75
|
-
space,
|
|
76
|
-
added
|
|
77
|
-
});
|
|
78
|
-
}
|
|
79
|
-
}));
|
|
80
|
-
}
|
|
81
|
-
});
|
|
82
|
-
this._ctx.onDispose(() => spacesSubscription.unsubscribe());
|
|
83
|
-
}
|
|
84
|
-
async _close(_) {
|
|
85
|
-
log.info("closing...", void 0, {
|
|
86
|
-
F: __dxlog_file,
|
|
87
|
-
L: 87,
|
|
88
|
-
S: this,
|
|
89
|
-
C: (f, a) => f(...a)
|
|
90
|
-
});
|
|
91
|
-
this._functionBySpaceKey.clear();
|
|
92
|
-
}
|
|
93
|
-
};
|
|
94
|
-
|
|
95
15
|
// packages/core/functions/src/handler.ts
|
|
96
|
-
import {
|
|
97
|
-
import {
|
|
16
|
+
import { TextV0Type } from "@braneframe/types";
|
|
17
|
+
import { PublicKey } from "@dxos/client";
|
|
18
|
+
import { log } from "@dxos/log";
|
|
98
19
|
import { nonNullable } from "@dxos/util";
|
|
99
|
-
var
|
|
100
|
-
var subscriptionHandler = (handler) => {
|
|
101
|
-
return ({ event: { data }, context, ...rest }) => {
|
|
20
|
+
var __dxlog_file = "/home/runner/work/dxos/dxos/packages/core/functions/src/handler.ts";
|
|
21
|
+
var subscriptionHandler = (handler, types) => {
|
|
22
|
+
return async ({ event: { data }, context, response, ...rest }) => {
|
|
102
23
|
const { client } = context;
|
|
103
|
-
const space = data.spaceKey ? client.spaces.get(
|
|
24
|
+
const space = data.spaceKey ? client.spaces.get(PublicKey.from(data.spaceKey)) : void 0;
|
|
25
|
+
if (!space) {
|
|
26
|
+
log.error("Invalid space", void 0, {
|
|
27
|
+
F: __dxlog_file,
|
|
28
|
+
L: 90,
|
|
29
|
+
S: void 0,
|
|
30
|
+
C: (f, a) => f(...a)
|
|
31
|
+
});
|
|
32
|
+
return response.status(500);
|
|
33
|
+
}
|
|
34
|
+
registerTypes(space, types);
|
|
104
35
|
const objects = space ? data.objects?.map((id) => space.db.getObjectById(id)).filter(nonNullable) : [];
|
|
105
36
|
if (!!data.spaceKey && !space) {
|
|
106
|
-
|
|
37
|
+
log.warn("invalid space", {
|
|
107
38
|
data
|
|
108
39
|
}, {
|
|
109
|
-
F:
|
|
110
|
-
L:
|
|
40
|
+
F: __dxlog_file,
|
|
41
|
+
L: 100,
|
|
111
42
|
S: void 0,
|
|
112
43
|
C: (f, a) => f(...a)
|
|
113
44
|
});
|
|
114
45
|
} else {
|
|
115
|
-
|
|
46
|
+
log.info("handler", {
|
|
116
47
|
space: space?.key.truncate(),
|
|
117
48
|
objects: objects?.length
|
|
118
49
|
}, {
|
|
119
|
-
F:
|
|
120
|
-
L:
|
|
50
|
+
F: __dxlog_file,
|
|
51
|
+
L: 102,
|
|
121
52
|
S: void 0,
|
|
122
53
|
C: (f, a) => f(...a)
|
|
123
54
|
});
|
|
@@ -131,986 +62,20 @@ var subscriptionHandler = (handler) => {
|
|
|
131
62
|
}
|
|
132
63
|
},
|
|
133
64
|
context,
|
|
65
|
+
response,
|
|
134
66
|
...rest
|
|
135
67
|
});
|
|
136
68
|
};
|
|
137
69
|
};
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
import { log as log3 } from "@dxos/log";
|
|
147
|
-
var __dxlog_file3 = "/home/runner/work/dxos/dxos/packages/core/functions/src/runtime/dev-server.ts";
|
|
148
|
-
var DevServer = class {
|
|
149
|
-
constructor(_client, _functionsRegistry, _options) {
|
|
150
|
-
this._client = _client;
|
|
151
|
-
this._functionsRegistry = _functionsRegistry;
|
|
152
|
-
this._options = _options;
|
|
153
|
-
this._ctx = createContext();
|
|
154
|
-
this._handlers = {};
|
|
155
|
-
this._seq = 0;
|
|
156
|
-
this.update = new Event2();
|
|
157
|
-
this._functionsRegistry.registered.on(async ({ added }) => {
|
|
158
|
-
added.forEach((def) => this._load(def));
|
|
159
|
-
await this._safeUpdateRegistration();
|
|
160
|
-
log3("new functions loaded", {
|
|
161
|
-
added
|
|
162
|
-
}, {
|
|
163
|
-
F: __dxlog_file3,
|
|
164
|
-
L: 53,
|
|
165
|
-
S: this,
|
|
166
|
-
C: (f, a) => f(...a)
|
|
167
|
-
});
|
|
168
|
-
});
|
|
169
|
-
}
|
|
170
|
-
get stats() {
|
|
171
|
-
return {
|
|
172
|
-
seq: this._seq
|
|
173
|
-
};
|
|
174
|
-
}
|
|
175
|
-
get endpoint() {
|
|
176
|
-
invariant(this._port, void 0, {
|
|
177
|
-
F: __dxlog_file3,
|
|
178
|
-
L: 64,
|
|
179
|
-
S: this,
|
|
180
|
-
A: [
|
|
181
|
-
"this._port",
|
|
182
|
-
""
|
|
183
|
-
]
|
|
184
|
-
});
|
|
185
|
-
return `http://localhost:${this._port}`;
|
|
186
|
-
}
|
|
187
|
-
get proxy() {
|
|
188
|
-
return this._proxy;
|
|
189
|
-
}
|
|
190
|
-
get functions() {
|
|
191
|
-
return Object.values(this._handlers);
|
|
192
|
-
}
|
|
193
|
-
async start() {
|
|
194
|
-
invariant(!this._server, void 0, {
|
|
195
|
-
F: __dxlog_file3,
|
|
196
|
-
L: 77,
|
|
197
|
-
S: this,
|
|
198
|
-
A: [
|
|
199
|
-
"!this._server",
|
|
200
|
-
""
|
|
201
|
-
]
|
|
202
|
-
});
|
|
203
|
-
log3.info("starting...", void 0, {
|
|
204
|
-
F: __dxlog_file3,
|
|
205
|
-
L: 78,
|
|
206
|
-
S: this,
|
|
207
|
-
C: (f, a) => f(...a)
|
|
208
|
-
});
|
|
209
|
-
this._ctx = createContext();
|
|
210
|
-
const app = express();
|
|
211
|
-
app.use(express.json());
|
|
212
|
-
app.post("/:path", async (req, res) => {
|
|
213
|
-
const { path: path2 } = req.params;
|
|
214
|
-
try {
|
|
215
|
-
log3.info("calling", {
|
|
216
|
-
path: path2
|
|
217
|
-
}, {
|
|
218
|
-
F: __dxlog_file3,
|
|
219
|
-
L: 88,
|
|
220
|
-
S: this,
|
|
221
|
-
C: (f, a) => f(...a)
|
|
222
|
-
});
|
|
223
|
-
if (this._options.reload) {
|
|
224
|
-
const { def } = this._handlers["/" + path2];
|
|
225
|
-
await this._load(def, true);
|
|
226
|
-
}
|
|
227
|
-
res.statusCode = await this.invoke("/" + path2, req.body);
|
|
228
|
-
res.end();
|
|
229
|
-
} catch (err) {
|
|
230
|
-
log3.catch(err, void 0, {
|
|
231
|
-
F: __dxlog_file3,
|
|
232
|
-
L: 98,
|
|
233
|
-
S: this,
|
|
234
|
-
C: (f, a) => f(...a)
|
|
235
|
-
});
|
|
236
|
-
res.statusCode = 500;
|
|
237
|
-
res.end();
|
|
238
|
-
}
|
|
239
|
-
});
|
|
240
|
-
this._port = await getPort({
|
|
241
|
-
host: "localhost",
|
|
242
|
-
port: 7200,
|
|
243
|
-
portRange: [
|
|
244
|
-
7200,
|
|
245
|
-
7299
|
|
246
|
-
]
|
|
247
|
-
});
|
|
248
|
-
this._server = app.listen(this._port);
|
|
249
|
-
try {
|
|
250
|
-
const { registrationId, endpoint } = await this._client.services.services.FunctionRegistryService.register({
|
|
251
|
-
endpoint: this.endpoint
|
|
252
|
-
});
|
|
253
|
-
log3.info("registered", {
|
|
254
|
-
endpoint
|
|
255
|
-
}, {
|
|
256
|
-
F: __dxlog_file3,
|
|
257
|
-
L: 113,
|
|
258
|
-
S: this,
|
|
259
|
-
C: (f, a) => f(...a)
|
|
260
|
-
});
|
|
261
|
-
this._proxy = endpoint;
|
|
262
|
-
this._functionServiceRegistration = registrationId;
|
|
263
|
-
await this._functionsRegistry.open(this._ctx);
|
|
264
|
-
} catch (err) {
|
|
265
|
-
await this.stop();
|
|
266
|
-
throw new Error("FunctionRegistryService not available (check plugin is configured).");
|
|
70
|
+
var registerTypes = (space, types = []) => {
|
|
71
|
+
const registry = space.db.graph.runtimeSchemaRegistry;
|
|
72
|
+
for (const type of [
|
|
73
|
+
...types,
|
|
74
|
+
TextV0Type
|
|
75
|
+
]) {
|
|
76
|
+
if (!registry.hasSchema(type)) {
|
|
77
|
+
registry.registerSchema(type);
|
|
267
78
|
}
|
|
268
|
-
log3.info("started", {
|
|
269
|
-
port: this._port
|
|
270
|
-
}, {
|
|
271
|
-
F: __dxlog_file3,
|
|
272
|
-
L: 124,
|
|
273
|
-
S: this,
|
|
274
|
-
C: (f, a) => f(...a)
|
|
275
|
-
});
|
|
276
|
-
}
|
|
277
|
-
async stop() {
|
|
278
|
-
invariant(this._server, void 0, {
|
|
279
|
-
F: __dxlog_file3,
|
|
280
|
-
L: 128,
|
|
281
|
-
S: this,
|
|
282
|
-
A: [
|
|
283
|
-
"this._server",
|
|
284
|
-
""
|
|
285
|
-
]
|
|
286
|
-
});
|
|
287
|
-
log3.info("stopping...", void 0, {
|
|
288
|
-
F: __dxlog_file3,
|
|
289
|
-
L: 129,
|
|
290
|
-
S: this,
|
|
291
|
-
C: (f, a) => f(...a)
|
|
292
|
-
});
|
|
293
|
-
const trigger = new Trigger();
|
|
294
|
-
this._server.close(async () => {
|
|
295
|
-
log3.info("server stopped", void 0, {
|
|
296
|
-
F: __dxlog_file3,
|
|
297
|
-
L: 133,
|
|
298
|
-
S: this,
|
|
299
|
-
C: (f, a) => f(...a)
|
|
300
|
-
});
|
|
301
|
-
try {
|
|
302
|
-
if (this._functionServiceRegistration) {
|
|
303
|
-
invariant(this._client.services.services.FunctionRegistryService, void 0, {
|
|
304
|
-
F: __dxlog_file3,
|
|
305
|
-
L: 136,
|
|
306
|
-
S: this,
|
|
307
|
-
A: [
|
|
308
|
-
"this._client.services.services.FunctionRegistryService",
|
|
309
|
-
""
|
|
310
|
-
]
|
|
311
|
-
});
|
|
312
|
-
await this._client.services.services.FunctionRegistryService.unregister({
|
|
313
|
-
registrationId: this._functionServiceRegistration
|
|
314
|
-
});
|
|
315
|
-
log3.info("unregistered", {
|
|
316
|
-
registrationId: this._functionServiceRegistration
|
|
317
|
-
}, {
|
|
318
|
-
F: __dxlog_file3,
|
|
319
|
-
L: 141,
|
|
320
|
-
S: this,
|
|
321
|
-
C: (f, a) => f(...a)
|
|
322
|
-
});
|
|
323
|
-
this._functionServiceRegistration = void 0;
|
|
324
|
-
this._proxy = void 0;
|
|
325
|
-
}
|
|
326
|
-
trigger.wake();
|
|
327
|
-
} catch (err) {
|
|
328
|
-
trigger.throw(err);
|
|
329
|
-
}
|
|
330
|
-
});
|
|
331
|
-
await trigger.wait();
|
|
332
|
-
this._port = void 0;
|
|
333
|
-
this._server = void 0;
|
|
334
|
-
log3.info("stopped", void 0, {
|
|
335
|
-
F: __dxlog_file3,
|
|
336
|
-
L: 155,
|
|
337
|
-
S: this,
|
|
338
|
-
C: (f, a) => f(...a)
|
|
339
|
-
});
|
|
340
|
-
}
|
|
341
|
-
/**
|
|
342
|
-
* Load function.
|
|
343
|
-
*/
|
|
344
|
-
async _load(def, force) {
|
|
345
|
-
const { uri, route, handler } = def;
|
|
346
|
-
const filePath = join(this._options.baseDir, handler);
|
|
347
|
-
log3.info("loading", {
|
|
348
|
-
uri,
|
|
349
|
-
force
|
|
350
|
-
}, {
|
|
351
|
-
F: __dxlog_file3,
|
|
352
|
-
L: 164,
|
|
353
|
-
S: this,
|
|
354
|
-
C: (f, a) => f(...a)
|
|
355
|
-
});
|
|
356
|
-
if (force) {
|
|
357
|
-
Object.keys(__require.cache).filter((key) => key.startsWith(filePath)).forEach((key) => {
|
|
358
|
-
delete __require.cache[key];
|
|
359
|
-
});
|
|
360
|
-
}
|
|
361
|
-
const module = __require(filePath);
|
|
362
|
-
if (typeof module.default !== "function") {
|
|
363
|
-
throw new Error(`Handler must export default function: ${uri}`);
|
|
364
|
-
}
|
|
365
|
-
this._handlers[route] = {
|
|
366
|
-
def,
|
|
367
|
-
handler: module.default
|
|
368
|
-
};
|
|
369
|
-
}
|
|
370
|
-
async _safeUpdateRegistration() {
|
|
371
|
-
invariant(this._functionServiceRegistration, void 0, {
|
|
372
|
-
F: __dxlog_file3,
|
|
373
|
-
L: 186,
|
|
374
|
-
S: this,
|
|
375
|
-
A: [
|
|
376
|
-
"this._functionServiceRegistration",
|
|
377
|
-
""
|
|
378
|
-
]
|
|
379
|
-
});
|
|
380
|
-
try {
|
|
381
|
-
await this._client.services.services.FunctionRegistryService.updateRegistration({
|
|
382
|
-
registrationId: this._functionServiceRegistration,
|
|
383
|
-
functions: this.functions.map(({ def: { id, route } }) => ({
|
|
384
|
-
id,
|
|
385
|
-
route
|
|
386
|
-
}))
|
|
387
|
-
});
|
|
388
|
-
} catch (err) {
|
|
389
|
-
log3.catch(err, void 0, {
|
|
390
|
-
F: __dxlog_file3,
|
|
391
|
-
L: 193,
|
|
392
|
-
S: this,
|
|
393
|
-
C: (f, a) => f(...a)
|
|
394
|
-
});
|
|
395
|
-
}
|
|
396
|
-
}
|
|
397
|
-
/**
|
|
398
|
-
* Invoke function.
|
|
399
|
-
*/
|
|
400
|
-
async invoke(path2, data) {
|
|
401
|
-
const seq = ++this._seq;
|
|
402
|
-
const now = Date.now();
|
|
403
|
-
log3.info("req", {
|
|
404
|
-
seq,
|
|
405
|
-
path: path2
|
|
406
|
-
}, {
|
|
407
|
-
F: __dxlog_file3,
|
|
408
|
-
L: 204,
|
|
409
|
-
S: this,
|
|
410
|
-
C: (f, a) => f(...a)
|
|
411
|
-
});
|
|
412
|
-
const statusCode = await this._invoke(path2, {
|
|
413
|
-
data
|
|
414
|
-
});
|
|
415
|
-
log3.info("res", {
|
|
416
|
-
seq,
|
|
417
|
-
path: path2,
|
|
418
|
-
statusCode,
|
|
419
|
-
duration: Date.now() - now
|
|
420
|
-
}, {
|
|
421
|
-
F: __dxlog_file3,
|
|
422
|
-
L: 207,
|
|
423
|
-
S: this,
|
|
424
|
-
C: (f, a) => f(...a)
|
|
425
|
-
});
|
|
426
|
-
this.update.emit(statusCode);
|
|
427
|
-
return statusCode;
|
|
428
|
-
}
|
|
429
|
-
async _invoke(path2, event) {
|
|
430
|
-
const { handler } = this._handlers[path2] ?? {};
|
|
431
|
-
invariant(handler, `invalid path: ${path2}`, {
|
|
432
|
-
F: __dxlog_file3,
|
|
433
|
-
L: 214,
|
|
434
|
-
S: this,
|
|
435
|
-
A: [
|
|
436
|
-
"handler",
|
|
437
|
-
"`invalid path: ${path}`"
|
|
438
|
-
]
|
|
439
|
-
});
|
|
440
|
-
const context = {
|
|
441
|
-
client: this._client,
|
|
442
|
-
dataDir: this._options.dataDir
|
|
443
|
-
};
|
|
444
|
-
let statusCode = 200;
|
|
445
|
-
const response = {
|
|
446
|
-
status: (code) => {
|
|
447
|
-
statusCode = code;
|
|
448
|
-
return response;
|
|
449
|
-
}
|
|
450
|
-
};
|
|
451
|
-
await handler({
|
|
452
|
-
context,
|
|
453
|
-
event,
|
|
454
|
-
response
|
|
455
|
-
});
|
|
456
|
-
return statusCode;
|
|
457
|
-
}
|
|
458
|
-
};
|
|
459
|
-
var createContext = () => new Context({
|
|
460
|
-
name: "DevServer"
|
|
461
|
-
});
|
|
462
|
-
|
|
463
|
-
// packages/core/functions/src/runtime/scheduler.ts
|
|
464
|
-
import path from "@dxos/node-std/path";
|
|
465
|
-
import { Mutex } from "@dxos/async";
|
|
466
|
-
import { Context as Context2 } from "@dxos/context";
|
|
467
|
-
import { log as log4 } from "@dxos/log";
|
|
468
|
-
var __dxlog_file4 = "/home/runner/work/dxos/dxos/packages/core/functions/src/runtime/scheduler.ts";
|
|
469
|
-
var Scheduler = class {
|
|
470
|
-
constructor(functions, triggers, _options = {}) {
|
|
471
|
-
this.functions = functions;
|
|
472
|
-
this.triggers = triggers;
|
|
473
|
-
this._options = _options;
|
|
474
|
-
this._ctx = createContext2();
|
|
475
|
-
this._functionUriToCallMutex = /* @__PURE__ */ new Map();
|
|
476
|
-
this.functions.registered.on(async ({ space, added }) => {
|
|
477
|
-
await this._safeActivateTriggers(space, this.triggers.getInactiveTriggers(space), added);
|
|
478
|
-
});
|
|
479
|
-
this.triggers.registered.on(async ({ space, triggers: triggers2 }) => {
|
|
480
|
-
await this._safeActivateTriggers(space, triggers2, this.functions.getFunctions(space));
|
|
481
|
-
});
|
|
482
|
-
}
|
|
483
|
-
async start() {
|
|
484
|
-
await this._ctx.dispose();
|
|
485
|
-
this._ctx = createContext2();
|
|
486
|
-
await this.functions.open(this._ctx);
|
|
487
|
-
await this.triggers.open(this._ctx);
|
|
488
|
-
}
|
|
489
|
-
async stop() {
|
|
490
|
-
await this._ctx.dispose();
|
|
491
|
-
await this.functions.close();
|
|
492
|
-
await this.triggers.close();
|
|
493
|
-
}
|
|
494
|
-
// TODO(burdon): Remove and update registries directly.
|
|
495
|
-
async register(space, manifest) {
|
|
496
|
-
await this.functions.register(space, manifest.functions);
|
|
497
|
-
await this.triggers.register(space, manifest);
|
|
498
|
-
}
|
|
499
|
-
async _safeActivateTriggers(space, triggers, functions) {
|
|
500
|
-
const mountTasks = triggers.map((trigger) => {
|
|
501
|
-
return this.activate(space, functions, trigger);
|
|
502
|
-
});
|
|
503
|
-
await Promise.all(mountTasks).catch(log4.catch);
|
|
504
|
-
}
|
|
505
|
-
async activate(space, functions, trigger) {
|
|
506
|
-
const definition = functions.find((def) => def.uri === trigger.function);
|
|
507
|
-
if (!definition) {
|
|
508
|
-
log4.info("function is not found for trigger", {
|
|
509
|
-
trigger
|
|
510
|
-
}, {
|
|
511
|
-
F: __dxlog_file4,
|
|
512
|
-
L: 78,
|
|
513
|
-
S: this,
|
|
514
|
-
C: (f, a) => f(...a)
|
|
515
|
-
});
|
|
516
|
-
return;
|
|
517
|
-
}
|
|
518
|
-
await this.triggers.activate(space, trigger, async (args) => {
|
|
519
|
-
const mutex = this._functionUriToCallMutex.get(definition.uri) ?? new Mutex();
|
|
520
|
-
this._functionUriToCallMutex.set(definition.uri, mutex);
|
|
521
|
-
log4.info("function triggered, waiting for mutex", {
|
|
522
|
-
uri: definition.uri
|
|
523
|
-
}, {
|
|
524
|
-
F: __dxlog_file4,
|
|
525
|
-
L: 86,
|
|
526
|
-
S: this,
|
|
527
|
-
C: (f, a) => f(...a)
|
|
528
|
-
});
|
|
529
|
-
return mutex.executeSynchronized(() => {
|
|
530
|
-
log4.info("mutex acquired", {
|
|
531
|
-
uri: definition.uri
|
|
532
|
-
}, {
|
|
533
|
-
F: __dxlog_file4,
|
|
534
|
-
L: 88,
|
|
535
|
-
S: this,
|
|
536
|
-
C: (f, a) => f(...a)
|
|
537
|
-
});
|
|
538
|
-
return this._execFunction(definition, trigger, {
|
|
539
|
-
meta: trigger.meta ?? {},
|
|
540
|
-
data: {
|
|
541
|
-
...args,
|
|
542
|
-
spaceKey: space.key
|
|
543
|
-
}
|
|
544
|
-
});
|
|
545
|
-
});
|
|
546
|
-
});
|
|
547
|
-
log4("activated trigger", {
|
|
548
|
-
space: space.key,
|
|
549
|
-
trigger
|
|
550
|
-
}, {
|
|
551
|
-
F: __dxlog_file4,
|
|
552
|
-
L: 96,
|
|
553
|
-
S: this,
|
|
554
|
-
C: (f, a) => f(...a)
|
|
555
|
-
});
|
|
556
|
-
}
|
|
557
|
-
async _execFunction(def, trigger, { data, meta }) {
|
|
558
|
-
let status = 0;
|
|
559
|
-
try {
|
|
560
|
-
const payload = Object.assign({}, meta && {
|
|
561
|
-
meta
|
|
562
|
-
}, data);
|
|
563
|
-
const { endpoint, callback } = this._options;
|
|
564
|
-
if (endpoint) {
|
|
565
|
-
const url = path.join(endpoint, def.route);
|
|
566
|
-
log4.info("exec", {
|
|
567
|
-
function: def.uri,
|
|
568
|
-
url,
|
|
569
|
-
triggerType: trigger.spec.type
|
|
570
|
-
}, {
|
|
571
|
-
F: __dxlog_file4,
|
|
572
|
-
L: 113,
|
|
573
|
-
S: this,
|
|
574
|
-
C: (f, a) => f(...a)
|
|
575
|
-
});
|
|
576
|
-
const response = await fetch(url, {
|
|
577
|
-
method: "POST",
|
|
578
|
-
headers: {
|
|
579
|
-
"Content-Type": "application/json"
|
|
580
|
-
},
|
|
581
|
-
body: JSON.stringify(payload)
|
|
582
|
-
});
|
|
583
|
-
status = response.status;
|
|
584
|
-
} else if (callback) {
|
|
585
|
-
log4.info("exec", {
|
|
586
|
-
function: def.uri
|
|
587
|
-
}, {
|
|
588
|
-
F: __dxlog_file4,
|
|
589
|
-
L: 124,
|
|
590
|
-
S: this,
|
|
591
|
-
C: (f, a) => f(...a)
|
|
592
|
-
});
|
|
593
|
-
status = await callback(payload) ?? 200;
|
|
594
|
-
}
|
|
595
|
-
if (status && status >= 400) {
|
|
596
|
-
throw new Error(`Response: ${status}`);
|
|
597
|
-
}
|
|
598
|
-
log4.info("done", {
|
|
599
|
-
function: def.uri,
|
|
600
|
-
status
|
|
601
|
-
}, {
|
|
602
|
-
F: __dxlog_file4,
|
|
603
|
-
L: 134,
|
|
604
|
-
S: this,
|
|
605
|
-
C: (f, a) => f(...a)
|
|
606
|
-
});
|
|
607
|
-
} catch (err) {
|
|
608
|
-
log4.error("error", {
|
|
609
|
-
function: def.uri,
|
|
610
|
-
error: err.message
|
|
611
|
-
}, {
|
|
612
|
-
F: __dxlog_file4,
|
|
613
|
-
L: 136,
|
|
614
|
-
S: this,
|
|
615
|
-
C: (f, a) => f(...a)
|
|
616
|
-
});
|
|
617
|
-
status = 500;
|
|
618
|
-
}
|
|
619
|
-
return status;
|
|
620
|
-
}
|
|
621
|
-
};
|
|
622
|
-
var createContext2 = () => new Context2({
|
|
623
|
-
name: "FunctionScheduler"
|
|
624
|
-
});
|
|
625
|
-
|
|
626
|
-
// packages/core/functions/src/trigger/trigger-registry.ts
|
|
627
|
-
import { Event as Event3 } from "@dxos/async";
|
|
628
|
-
import { create as create2, Filter as Filter3, getMeta } from "@dxos/client/echo";
|
|
629
|
-
import { Context as Context3, Resource as Resource2 } from "@dxos/context";
|
|
630
|
-
import { compareForeignKeys, ECHO_ATTR_META, foreignKey } from "@dxos/echo-schema";
|
|
631
|
-
import { invariant as invariant2 } from "@dxos/invariant";
|
|
632
|
-
import { PublicKey as PublicKey3 } from "@dxos/keys";
|
|
633
|
-
import { log as log9 } from "@dxos/log";
|
|
634
|
-
import { ComplexMap as ComplexMap2, diff as diff2 } from "@dxos/util";
|
|
635
|
-
|
|
636
|
-
// packages/core/functions/src/trigger/type/subscription-trigger.ts
|
|
637
|
-
import { TextV0Type } from "@braneframe/types";
|
|
638
|
-
import { debounce, UpdateScheduler } from "@dxos/async";
|
|
639
|
-
import { Filter as Filter2 } from "@dxos/client/echo";
|
|
640
|
-
import { createSubscription, getAutomergeObjectCore } from "@dxos/echo-db";
|
|
641
|
-
import { log as log5 } from "@dxos/log";
|
|
642
|
-
var __dxlog_file5 = "/home/runner/work/dxos/dxos/packages/core/functions/src/trigger/type/subscription-trigger.ts";
|
|
643
|
-
var createSubscriptionTrigger = async (ctx, space, spec, callback) => {
|
|
644
|
-
const objectIds = /* @__PURE__ */ new Set();
|
|
645
|
-
const task = new UpdateScheduler(ctx, async () => {
|
|
646
|
-
if (objectIds.size > 0) {
|
|
647
|
-
const objects = Array.from(objectIds);
|
|
648
|
-
objectIds.clear();
|
|
649
|
-
await callback({
|
|
650
|
-
objects
|
|
651
|
-
});
|
|
652
|
-
}
|
|
653
|
-
}, {
|
|
654
|
-
maxFrequency: 4
|
|
655
|
-
});
|
|
656
|
-
const subscriptions = [];
|
|
657
|
-
const subscription = createSubscription(({ added, updated }) => {
|
|
658
|
-
const sizeBefore = objectIds.size;
|
|
659
|
-
for (const object of added) {
|
|
660
|
-
objectIds.add(object.id);
|
|
661
|
-
}
|
|
662
|
-
for (const object of updated) {
|
|
663
|
-
objectIds.add(object.id);
|
|
664
|
-
}
|
|
665
|
-
if (objectIds.size > sizeBefore) {
|
|
666
|
-
log5.info("updated", {
|
|
667
|
-
added: added.length,
|
|
668
|
-
updated: updated.length
|
|
669
|
-
}, {
|
|
670
|
-
F: __dxlog_file5,
|
|
671
|
-
L: 47,
|
|
672
|
-
S: void 0,
|
|
673
|
-
C: (f, a) => f(...a)
|
|
674
|
-
});
|
|
675
|
-
task.trigger();
|
|
676
|
-
}
|
|
677
|
-
});
|
|
678
|
-
subscriptions.push(() => subscription.unsubscribe());
|
|
679
|
-
const { filter, options: { deep, delay } = {} } = spec;
|
|
680
|
-
const update = ({ objects }) => {
|
|
681
|
-
log5.info("update", {
|
|
682
|
-
objects: objects.length
|
|
683
|
-
}, {
|
|
684
|
-
F: __dxlog_file5,
|
|
685
|
-
L: 57,
|
|
686
|
-
S: void 0,
|
|
687
|
-
C: (f, a) => f(...a)
|
|
688
|
-
});
|
|
689
|
-
subscription.update(objects);
|
|
690
|
-
if (deep) {
|
|
691
|
-
for (const object of objects) {
|
|
692
|
-
const content = object.content;
|
|
693
|
-
if (content instanceof TextV0Type) {
|
|
694
|
-
subscriptions.push(getAutomergeObjectCore(content).updates.on(debounce(() => subscription.update([
|
|
695
|
-
object
|
|
696
|
-
]), 1e3)));
|
|
697
|
-
}
|
|
698
|
-
}
|
|
699
|
-
}
|
|
700
|
-
};
|
|
701
|
-
log5.info("subscription", {
|
|
702
|
-
filter
|
|
703
|
-
}, {
|
|
704
|
-
F: __dxlog_file5,
|
|
705
|
-
L: 76,
|
|
706
|
-
S: void 0,
|
|
707
|
-
C: (f, a) => f(...a)
|
|
708
|
-
});
|
|
709
|
-
if (filter) {
|
|
710
|
-
const query = space.db.query(Filter2.typename(filter[0].type, filter[0].props));
|
|
711
|
-
subscriptions.push(query.subscribe(delay ? debounce(update, delay) : update));
|
|
712
|
-
}
|
|
713
|
-
ctx.onDispose(() => {
|
|
714
|
-
subscriptions.forEach((unsubscribe) => unsubscribe());
|
|
715
|
-
});
|
|
716
|
-
};
|
|
717
|
-
|
|
718
|
-
// packages/core/functions/src/trigger/type/timer-trigger.ts
|
|
719
|
-
import { CronJob } from "cron";
|
|
720
|
-
import { DeferredTask } from "@dxos/async";
|
|
721
|
-
import { log as log6 } from "@dxos/log";
|
|
722
|
-
var __dxlog_file6 = "/home/runner/work/dxos/dxos/packages/core/functions/src/trigger/type/timer-trigger.ts";
|
|
723
|
-
var createTimerTrigger = async (ctx, space, spec, callback) => {
|
|
724
|
-
const task = new DeferredTask(ctx, async () => {
|
|
725
|
-
await callback({});
|
|
726
|
-
});
|
|
727
|
-
let last = 0;
|
|
728
|
-
let run = 0;
|
|
729
|
-
const job = CronJob.from({
|
|
730
|
-
cronTime: spec.cron,
|
|
731
|
-
runOnInit: false,
|
|
732
|
-
onTick: () => {
|
|
733
|
-
const now = Date.now();
|
|
734
|
-
const delta = last ? now - last : 0;
|
|
735
|
-
last = now;
|
|
736
|
-
run++;
|
|
737
|
-
log6.info("tick", {
|
|
738
|
-
space: space.key.truncate(),
|
|
739
|
-
count: run,
|
|
740
|
-
delta
|
|
741
|
-
}, {
|
|
742
|
-
F: __dxlog_file6,
|
|
743
|
-
L: 38,
|
|
744
|
-
S: void 0,
|
|
745
|
-
C: (f, a) => f(...a)
|
|
746
|
-
});
|
|
747
|
-
task.schedule();
|
|
748
|
-
}
|
|
749
|
-
});
|
|
750
|
-
job.start();
|
|
751
|
-
ctx.onDispose(() => job.stop());
|
|
752
|
-
};
|
|
753
|
-
|
|
754
|
-
// packages/core/functions/src/trigger/type/webhook-trigger.ts
|
|
755
|
-
import { getPort as getPort2 } from "get-port-please";
|
|
756
|
-
import http from "@dxos/node-std/http";
|
|
757
|
-
import { log as log7 } from "@dxos/log";
|
|
758
|
-
var __dxlog_file7 = "/home/runner/work/dxos/dxos/packages/core/functions/src/trigger/type/webhook-trigger.ts";
|
|
759
|
-
var createWebhookTrigger = async (ctx, space, spec, callback) => {
|
|
760
|
-
const server = http.createServer(async (req, res) => {
|
|
761
|
-
if (req.method !== spec.method) {
|
|
762
|
-
res.statusCode = 405;
|
|
763
|
-
return res.end();
|
|
764
|
-
}
|
|
765
|
-
res.statusCode = await callback({});
|
|
766
|
-
res.end();
|
|
767
|
-
});
|
|
768
|
-
const port = await getPort2({
|
|
769
|
-
random: true
|
|
770
|
-
});
|
|
771
|
-
server.listen(port, () => {
|
|
772
|
-
log7.info("started webhook", {
|
|
773
|
-
port
|
|
774
|
-
}, {
|
|
775
|
-
F: __dxlog_file7,
|
|
776
|
-
L: 41,
|
|
777
|
-
S: void 0,
|
|
778
|
-
C: (f, a) => f(...a)
|
|
779
|
-
});
|
|
780
|
-
spec.port = port;
|
|
781
|
-
});
|
|
782
|
-
ctx.onDispose(() => {
|
|
783
|
-
server.close();
|
|
784
|
-
});
|
|
785
|
-
};
|
|
786
|
-
|
|
787
|
-
// packages/core/functions/src/trigger/type/websocket-trigger.ts
|
|
788
|
-
import WebSocket from "ws";
|
|
789
|
-
import { sleep, Trigger as Trigger2 } from "@dxos/async";
|
|
790
|
-
import { log as log8 } from "@dxos/log";
|
|
791
|
-
var __dxlog_file8 = "/home/runner/work/dxos/dxos/packages/core/functions/src/trigger/type/websocket-trigger.ts";
|
|
792
|
-
var createWebsocketTrigger = async (ctx, space, spec, callback, options = {
|
|
793
|
-
retryDelay: 2,
|
|
794
|
-
maxAttempts: 5
|
|
795
|
-
}) => {
|
|
796
|
-
const { url, init } = spec;
|
|
797
|
-
let ws;
|
|
798
|
-
for (let attempt = 1; attempt <= options.maxAttempts; attempt++) {
|
|
799
|
-
const open = new Trigger2();
|
|
800
|
-
ws = new WebSocket(url);
|
|
801
|
-
Object.assign(ws, {
|
|
802
|
-
onopen: () => {
|
|
803
|
-
log8.info("opened", {
|
|
804
|
-
url
|
|
805
|
-
}, {
|
|
806
|
-
F: __dxlog_file8,
|
|
807
|
-
L: 40,
|
|
808
|
-
S: void 0,
|
|
809
|
-
C: (f, a) => f(...a)
|
|
810
|
-
});
|
|
811
|
-
if (spec.init) {
|
|
812
|
-
ws.send(new TextEncoder().encode(JSON.stringify(init)));
|
|
813
|
-
}
|
|
814
|
-
open.wake(true);
|
|
815
|
-
},
|
|
816
|
-
onclose: (event) => {
|
|
817
|
-
log8.info("closed", {
|
|
818
|
-
url,
|
|
819
|
-
code: event.code
|
|
820
|
-
}, {
|
|
821
|
-
F: __dxlog_file8,
|
|
822
|
-
L: 49,
|
|
823
|
-
S: void 0,
|
|
824
|
-
C: (f, a) => f(...a)
|
|
825
|
-
});
|
|
826
|
-
if (event.code === 1006) {
|
|
827
|
-
setTimeout(async () => {
|
|
828
|
-
log8.info(`reconnecting in ${options.retryDelay}s...`, {
|
|
829
|
-
url
|
|
830
|
-
}, {
|
|
831
|
-
F: __dxlog_file8,
|
|
832
|
-
L: 54,
|
|
833
|
-
S: void 0,
|
|
834
|
-
C: (f, a) => f(...a)
|
|
835
|
-
});
|
|
836
|
-
await createWebsocketTrigger(ctx, space, spec, callback, options);
|
|
837
|
-
}, options.retryDelay * 1e3);
|
|
838
|
-
}
|
|
839
|
-
open.wake(false);
|
|
840
|
-
},
|
|
841
|
-
onerror: (event) => {
|
|
842
|
-
log8.catch(event.error, {
|
|
843
|
-
url
|
|
844
|
-
}, {
|
|
845
|
-
F: __dxlog_file8,
|
|
846
|
-
L: 63,
|
|
847
|
-
S: void 0,
|
|
848
|
-
C: (f, a) => f(...a)
|
|
849
|
-
});
|
|
850
|
-
},
|
|
851
|
-
onmessage: async (event) => {
|
|
852
|
-
try {
|
|
853
|
-
log8.info("message", void 0, {
|
|
854
|
-
F: __dxlog_file8,
|
|
855
|
-
L: 68,
|
|
856
|
-
S: void 0,
|
|
857
|
-
C: (f, a) => f(...a)
|
|
858
|
-
});
|
|
859
|
-
const data = JSON.parse(new TextDecoder().decode(event.data));
|
|
860
|
-
await callback({
|
|
861
|
-
data
|
|
862
|
-
});
|
|
863
|
-
} catch (err) {
|
|
864
|
-
log8.catch(err, {
|
|
865
|
-
url
|
|
866
|
-
}, {
|
|
867
|
-
F: __dxlog_file8,
|
|
868
|
-
L: 72,
|
|
869
|
-
S: void 0,
|
|
870
|
-
C: (f, a) => f(...a)
|
|
871
|
-
});
|
|
872
|
-
}
|
|
873
|
-
}
|
|
874
|
-
});
|
|
875
|
-
const isOpen = await open.wait();
|
|
876
|
-
if (isOpen) {
|
|
877
|
-
break;
|
|
878
|
-
} else {
|
|
879
|
-
const wait = Math.pow(attempt, 2) * options.retryDelay;
|
|
880
|
-
if (attempt < options.maxAttempts) {
|
|
881
|
-
log8.warn(`failed to connect; trying again in ${wait}s`, {
|
|
882
|
-
attempt
|
|
883
|
-
}, {
|
|
884
|
-
F: __dxlog_file8,
|
|
885
|
-
L: 83,
|
|
886
|
-
S: void 0,
|
|
887
|
-
C: (f, a) => f(...a)
|
|
888
|
-
});
|
|
889
|
-
await sleep(wait * 1e3);
|
|
890
|
-
}
|
|
891
|
-
}
|
|
892
|
-
}
|
|
893
|
-
ctx.onDispose(() => {
|
|
894
|
-
ws?.close();
|
|
895
|
-
});
|
|
896
|
-
};
|
|
897
|
-
|
|
898
|
-
// packages/core/functions/src/trigger/trigger-registry.ts
|
|
899
|
-
var __dxlog_file9 = "/home/runner/work/dxos/dxos/packages/core/functions/src/trigger/trigger-registry.ts";
|
|
900
|
-
var triggerHandlers = {
|
|
901
|
-
subscription: createSubscriptionTrigger,
|
|
902
|
-
timer: createTimerTrigger,
|
|
903
|
-
webhook: createWebhookTrigger,
|
|
904
|
-
websocket: createWebsocketTrigger
|
|
905
|
-
};
|
|
906
|
-
var TriggerRegistry = class extends Resource2 {
|
|
907
|
-
constructor(_client, _options) {
|
|
908
|
-
super();
|
|
909
|
-
this._client = _client;
|
|
910
|
-
this._options = _options;
|
|
911
|
-
this._triggersBySpaceKey = new ComplexMap2(PublicKey3.hash);
|
|
912
|
-
this.registered = new Event3();
|
|
913
|
-
this.removed = new Event3();
|
|
914
|
-
}
|
|
915
|
-
getActiveTriggers(space) {
|
|
916
|
-
return this._getTriggers(space, (t) => t.activationCtx != null);
|
|
917
|
-
}
|
|
918
|
-
getInactiveTriggers(space) {
|
|
919
|
-
return this._getTriggers(space, (t) => t.activationCtx == null);
|
|
920
|
-
}
|
|
921
|
-
async activate(space, trigger, callback) {
|
|
922
|
-
log9("activate", {
|
|
923
|
-
space: space.key,
|
|
924
|
-
trigger
|
|
925
|
-
}, {
|
|
926
|
-
F: __dxlog_file9,
|
|
927
|
-
L: 72,
|
|
928
|
-
S: this,
|
|
929
|
-
C: (f, a) => f(...a)
|
|
930
|
-
});
|
|
931
|
-
const activationCtx = new Context3({
|
|
932
|
-
name: `FunctionTrigger-${trigger.function}`
|
|
933
|
-
});
|
|
934
|
-
this._ctx.onDispose(() => activationCtx.dispose());
|
|
935
|
-
const registeredTrigger = this._triggersBySpaceKey.get(space.key)?.find((reg) => reg.trigger.id === trigger.id);
|
|
936
|
-
invariant2(registeredTrigger, `Trigger is not registered: ${trigger.function}`, {
|
|
937
|
-
F: __dxlog_file9,
|
|
938
|
-
L: 77,
|
|
939
|
-
S: this,
|
|
940
|
-
A: [
|
|
941
|
-
"registeredTrigger",
|
|
942
|
-
"`Trigger is not registered: ${trigger.function}`"
|
|
943
|
-
]
|
|
944
|
-
});
|
|
945
|
-
registeredTrigger.activationCtx = activationCtx;
|
|
946
|
-
try {
|
|
947
|
-
const options = this._options?.[trigger.spec.type];
|
|
948
|
-
await triggerHandlers[trigger.spec.type](activationCtx, space, trigger.spec, callback, options);
|
|
949
|
-
} catch (err) {
|
|
950
|
-
delete registeredTrigger.activationCtx;
|
|
951
|
-
throw err;
|
|
952
|
-
}
|
|
953
|
-
}
|
|
954
|
-
/**
|
|
955
|
-
* Loads triggers from the manifest into the space.
|
|
956
|
-
*/
|
|
957
|
-
async register(space, manifest) {
|
|
958
|
-
log9("register", {
|
|
959
|
-
space: space.key
|
|
960
|
-
}, {
|
|
961
|
-
F: __dxlog_file9,
|
|
962
|
-
L: 93,
|
|
963
|
-
S: this,
|
|
964
|
-
C: (f, a) => f(...a)
|
|
965
|
-
});
|
|
966
|
-
if (!manifest.triggers?.length) {
|
|
967
|
-
return;
|
|
968
|
-
}
|
|
969
|
-
if (!space.db.graph.runtimeSchemaRegistry.hasSchema(FunctionTrigger)) {
|
|
970
|
-
space.db.graph.runtimeSchemaRegistry.registerSchema(FunctionTrigger);
|
|
971
|
-
}
|
|
972
|
-
const manifestTriggers = manifest.triggers.map((trigger) => {
|
|
973
|
-
let keys = trigger[ECHO_ATTR_META]?.keys;
|
|
974
|
-
delete trigger[ECHO_ATTR_META];
|
|
975
|
-
if (!keys?.length) {
|
|
976
|
-
keys = [
|
|
977
|
-
foreignKey("manifest", [
|
|
978
|
-
trigger.function,
|
|
979
|
-
trigger.spec.type
|
|
980
|
-
].join(":"))
|
|
981
|
-
];
|
|
982
|
-
}
|
|
983
|
-
return create2(FunctionTrigger, trigger, {
|
|
984
|
-
keys
|
|
985
|
-
});
|
|
986
|
-
});
|
|
987
|
-
const { objects: existing } = await space.db.query(Filter3.schema(FunctionTrigger)).run();
|
|
988
|
-
const { added } = diff2(existing, manifestTriggers, compareForeignKeys);
|
|
989
|
-
added.forEach((trigger) => {
|
|
990
|
-
space.db.add(trigger);
|
|
991
|
-
log9.info("added", {
|
|
992
|
-
meta: getMeta(trigger)
|
|
993
|
-
}, {
|
|
994
|
-
F: __dxlog_file9,
|
|
995
|
-
L: 120,
|
|
996
|
-
S: this,
|
|
997
|
-
C: (f, a) => f(...a)
|
|
998
|
-
});
|
|
999
|
-
});
|
|
1000
|
-
if (added.length > 0) {
|
|
1001
|
-
await space.db.flush();
|
|
1002
|
-
}
|
|
1003
|
-
}
|
|
1004
|
-
async _open() {
|
|
1005
|
-
log9.info("open...", void 0, {
|
|
1006
|
-
F: __dxlog_file9,
|
|
1007
|
-
L: 129,
|
|
1008
|
-
S: this,
|
|
1009
|
-
C: (f, a) => f(...a)
|
|
1010
|
-
});
|
|
1011
|
-
const spaceListSubscription = this._client.spaces.subscribe(async (spaces) => {
|
|
1012
|
-
for (const space of spaces) {
|
|
1013
|
-
if (this._triggersBySpaceKey.has(space.key)) {
|
|
1014
|
-
continue;
|
|
1015
|
-
}
|
|
1016
|
-
const registered = [];
|
|
1017
|
-
this._triggersBySpaceKey.set(space.key, registered);
|
|
1018
|
-
await space.waitUntilReady();
|
|
1019
|
-
if (this._ctx.disposed) {
|
|
1020
|
-
break;
|
|
1021
|
-
}
|
|
1022
|
-
this._ctx.onDispose(space.db.query(Filter3.schema(FunctionTrigger)).subscribe(async ({ objects: current }) => {
|
|
1023
|
-
log9.info("update", {
|
|
1024
|
-
space: space.key,
|
|
1025
|
-
registered: registered.length,
|
|
1026
|
-
current: current.length
|
|
1027
|
-
}, {
|
|
1028
|
-
F: __dxlog_file9,
|
|
1029
|
-
L: 146,
|
|
1030
|
-
S: this,
|
|
1031
|
-
C: (f, a) => f(...a)
|
|
1032
|
-
});
|
|
1033
|
-
await this._handleRemovedTriggers(space, current, registered);
|
|
1034
|
-
this._handleNewTriggers(space, current, registered);
|
|
1035
|
-
}));
|
|
1036
|
-
}
|
|
1037
|
-
});
|
|
1038
|
-
this._ctx.onDispose(() => spaceListSubscription.unsubscribe());
|
|
1039
|
-
log9.info("opened", void 0, {
|
|
1040
|
-
F: __dxlog_file9,
|
|
1041
|
-
L: 155,
|
|
1042
|
-
S: this,
|
|
1043
|
-
C: (f, a) => f(...a)
|
|
1044
|
-
});
|
|
1045
|
-
}
|
|
1046
|
-
async _close(_) {
|
|
1047
|
-
log9.info("close...", void 0, {
|
|
1048
|
-
F: __dxlog_file9,
|
|
1049
|
-
L: 159,
|
|
1050
|
-
S: this,
|
|
1051
|
-
C: (f, a) => f(...a)
|
|
1052
|
-
});
|
|
1053
|
-
this._triggersBySpaceKey.clear();
|
|
1054
|
-
log9.info("closed", void 0, {
|
|
1055
|
-
F: __dxlog_file9,
|
|
1056
|
-
L: 161,
|
|
1057
|
-
S: this,
|
|
1058
|
-
C: (f, a) => f(...a)
|
|
1059
|
-
});
|
|
1060
|
-
}
|
|
1061
|
-
_handleNewTriggers(space, current, registered) {
|
|
1062
|
-
const added = current.filter((candidate) => {
|
|
1063
|
-
return candidate.enabled && registered.find((reg) => reg.trigger.id === candidate.id) == null;
|
|
1064
|
-
});
|
|
1065
|
-
if (added.length > 0) {
|
|
1066
|
-
const newRegisteredTriggers = added.map((trigger) => ({
|
|
1067
|
-
trigger
|
|
1068
|
-
}));
|
|
1069
|
-
registered.push(...newRegisteredTriggers);
|
|
1070
|
-
log9.info("added", () => ({
|
|
1071
|
-
spaceKey: space.key,
|
|
1072
|
-
triggers: added.map((trigger) => trigger.function)
|
|
1073
|
-
}), {
|
|
1074
|
-
F: __dxlog_file9,
|
|
1075
|
-
L: 172,
|
|
1076
|
-
S: this,
|
|
1077
|
-
C: (f, a) => f(...a)
|
|
1078
|
-
});
|
|
1079
|
-
this.registered.emit({
|
|
1080
|
-
space,
|
|
1081
|
-
triggers: added
|
|
1082
|
-
});
|
|
1083
|
-
}
|
|
1084
|
-
}
|
|
1085
|
-
async _handleRemovedTriggers(space, current, registered) {
|
|
1086
|
-
const removed = [];
|
|
1087
|
-
for (let i = registered.length - 1; i >= 0; i--) {
|
|
1088
|
-
const wasRemoved = current.filter((trigger) => trigger.enabled).find((trigger) => trigger.id === registered[i].trigger.id) == null;
|
|
1089
|
-
if (wasRemoved) {
|
|
1090
|
-
const unregistered = registered.splice(i, 1)[0];
|
|
1091
|
-
await unregistered.activationCtx?.dispose();
|
|
1092
|
-
removed.push(unregistered.trigger);
|
|
1093
|
-
}
|
|
1094
|
-
}
|
|
1095
|
-
if (removed.length > 0) {
|
|
1096
|
-
log9.info("removed", () => ({
|
|
1097
|
-
spaceKey: space.key,
|
|
1098
|
-
triggers: removed.map((trigger) => trigger.function)
|
|
1099
|
-
}), {
|
|
1100
|
-
F: __dxlog_file9,
|
|
1101
|
-
L: 198,
|
|
1102
|
-
S: this,
|
|
1103
|
-
C: (f, a) => f(...a)
|
|
1104
|
-
});
|
|
1105
|
-
this.removed.emit({
|
|
1106
|
-
space,
|
|
1107
|
-
triggers: removed
|
|
1108
|
-
});
|
|
1109
|
-
}
|
|
1110
|
-
}
|
|
1111
|
-
_getTriggers(space, predicate) {
|
|
1112
|
-
const allSpaceTriggers = this._triggersBySpaceKey.get(space.key) ?? [];
|
|
1113
|
-
return allSpaceTriggers.filter(predicate).map((trigger) => trigger.trigger);
|
|
1114
79
|
}
|
|
1115
80
|
};
|
|
1116
81
|
export {
|