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