@dxos/functions 0.5.3-main.f752aaa → 0.5.3-main.fffc127
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 +884 -457
- 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 +868 -447
- 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 +2 -0
- package/dist/types/src/index.d.ts.map +1 -1
- package/dist/types/src/runtime/dev-server.d.ts +16 -13
- 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 +13 -27
- 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 +162 -107
- package/dist/types/src/types.d.ts.map +1 -1
- package/package.json +33 -15
- package/schema/functions.json +144 -103
- 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 +54 -31
- package/src/index.ts +2 -0
- package/src/runtime/dev-server.test.ts +60 -0
- package/src/runtime/dev-server.ts +104 -53
- package/src/runtime/scheduler.test.ts +61 -73
- package/src/runtime/scheduler.ts +91 -270
- 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 +87 -48
|
@@ -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-4D4I3YMJ.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: 53,
|
|
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: 64,
|
|
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: 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();
|
|
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: 88,
|
|
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: 98,
|
|
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: 113,
|
|
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: 124,
|
|
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: 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
|
+
});
|
|
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: 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);
|
|
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: 155,
|
|
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: 164,
|
|
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: 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
|
+
}
|
|
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: 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,
|
|
231
423
|
S: this,
|
|
232
424
|
C: (f, a) => f(...a)
|
|
233
425
|
});
|
|
234
|
-
|
|
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
|
+
});
|
|
235
440
|
const context = {
|
|
236
441
|
client: this._client,
|
|
237
442
|
dataDir: this._options.dataDir
|
|
@@ -248,453 +453,675 @@ 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 { createSubscription, Filter, getAutomergeObjectCore } from "@dxos/client/echo";
|
|
273
|
-
import { Context } from "@dxos/context";
|
|
274
|
-
import { invariant as invariant2 } from "@dxos/invariant";
|
|
275
|
-
import { log as log3 } from "@dxos/log";
|
|
276
|
-
import { ComplexMap } from "@dxos/util";
|
|
277
|
-
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";
|
|
278
469
|
var Scheduler = class {
|
|
279
|
-
constructor(
|
|
280
|
-
this.
|
|
281
|
-
this.
|
|
470
|
+
constructor(functions, triggers, _options = {}) {
|
|
471
|
+
this.functions = functions;
|
|
472
|
+
this.triggers = triggers;
|
|
282
473
|
this._options = _options;
|
|
283
|
-
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
|
+
});
|
|
284
482
|
}
|
|
285
483
|
async start() {
|
|
286
|
-
this.
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
await this.mount(new Context(), space, trigger);
|
|
291
|
-
}
|
|
292
|
-
}
|
|
293
|
-
});
|
|
484
|
+
await this._ctx.dispose();
|
|
485
|
+
this._ctx = createContext2();
|
|
486
|
+
await this.functions.open(this._ctx);
|
|
487
|
+
await this.triggers.open(this._ctx);
|
|
294
488
|
}
|
|
295
489
|
async stop() {
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
490
|
+
await this._ctx.dispose();
|
|
491
|
+
await this.functions.close();
|
|
492
|
+
await this.triggers.close();
|
|
299
493
|
}
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
L: 72,
|
|
309
|
-
S: this,
|
|
310
|
-
A: [
|
|
311
|
-
"def",
|
|
312
|
-
"`Function not found: ${trigger.function}`"
|
|
313
|
-
]
|
|
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);
|
|
314
502
|
});
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
log3("mount", {
|
|
322
|
-
space: space.key,
|
|
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", {
|
|
323
509
|
trigger
|
|
324
510
|
}, {
|
|
325
|
-
F:
|
|
511
|
+
F: __dxlog_file4,
|
|
326
512
|
L: 78,
|
|
327
513
|
S: this,
|
|
328
514
|
C: (f, a) => f(...a)
|
|
329
515
|
});
|
|
330
|
-
|
|
331
|
-
return;
|
|
332
|
-
}
|
|
333
|
-
if (trigger.timer) {
|
|
334
|
-
await this._createTimer(ctx, space, def, trigger.timer);
|
|
335
|
-
}
|
|
336
|
-
if (trigger.webhook) {
|
|
337
|
-
await this._createWebhook(ctx, space, def, trigger.webhook);
|
|
338
|
-
}
|
|
339
|
-
if (trigger.websocket) {
|
|
340
|
-
await this._createWebsocket(ctx, space, def, trigger.websocket);
|
|
341
|
-
}
|
|
342
|
-
if (trigger.subscription) {
|
|
343
|
-
await this._createSubscription(ctx, space, def, trigger.subscription);
|
|
344
|
-
}
|
|
345
|
-
}
|
|
346
|
-
}
|
|
347
|
-
async unmount(id, spaceKey) {
|
|
348
|
-
const key = {
|
|
349
|
-
id,
|
|
350
|
-
spaceKey
|
|
351
|
-
};
|
|
352
|
-
const { ctx } = this._mounts.get(key) ?? {};
|
|
353
|
-
if (ctx) {
|
|
354
|
-
this._mounts.delete(key);
|
|
355
|
-
await ctx.dispose();
|
|
516
|
+
return;
|
|
356
517
|
}
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
function: def.id
|
|
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
|
|
363
523
|
}, {
|
|
364
|
-
F:
|
|
365
|
-
L:
|
|
524
|
+
F: __dxlog_file4,
|
|
525
|
+
L: 86,
|
|
366
526
|
S: this,
|
|
367
527
|
C: (f, a) => f(...a)
|
|
368
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);
|
|
369
563
|
const { endpoint, callback } = this._options;
|
|
370
564
|
if (endpoint) {
|
|
371
|
-
|
|
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, {
|
|
372
577
|
method: "POST",
|
|
373
578
|
headers: {
|
|
374
579
|
"Content-Type": "application/json"
|
|
375
580
|
},
|
|
376
|
-
body: JSON.stringify(
|
|
581
|
+
body: JSON.stringify(payload)
|
|
377
582
|
});
|
|
583
|
+
status = response.status;
|
|
378
584
|
} else if (callback) {
|
|
379
|
-
|
|
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}`);
|
|
380
597
|
}
|
|
381
|
-
|
|
382
|
-
function: def.
|
|
598
|
+
log4.info("done", {
|
|
599
|
+
function: def.uri,
|
|
600
|
+
status
|
|
383
601
|
}, {
|
|
384
|
-
F:
|
|
385
|
-
L:
|
|
602
|
+
F: __dxlog_file4,
|
|
603
|
+
L: 134,
|
|
386
604
|
S: this,
|
|
387
605
|
C: (f, a) => f(...a)
|
|
388
606
|
});
|
|
389
607
|
} catch (err) {
|
|
390
|
-
|
|
391
|
-
function: def.
|
|
608
|
+
log4.error("error", {
|
|
609
|
+
function: def.uri,
|
|
392
610
|
error: err.message
|
|
393
611
|
}, {
|
|
394
|
-
F:
|
|
395
|
-
L:
|
|
612
|
+
F: __dxlog_file4,
|
|
613
|
+
L: 136,
|
|
396
614
|
S: this,
|
|
397
615
|
C: (f, a) => f(...a)
|
|
398
616
|
});
|
|
617
|
+
status = 500;
|
|
399
618
|
}
|
|
619
|
+
return status;
|
|
400
620
|
}
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
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
|
|
411
683
|
}, {
|
|
412
|
-
F:
|
|
413
|
-
L:
|
|
414
|
-
S:
|
|
684
|
+
F: __dxlog_file5,
|
|
685
|
+
L: 57,
|
|
686
|
+
S: void 0,
|
|
415
687
|
C: (f, a) => f(...a)
|
|
416
688
|
});
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
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)
|
|
421
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)
|
|
422
779
|
});
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
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
|
|
437
805
|
}, {
|
|
438
|
-
F:
|
|
439
|
-
L:
|
|
440
|
-
S:
|
|
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,
|
|
441
824
|
C: (f, a) => f(...a)
|
|
442
825
|
});
|
|
443
|
-
|
|
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
|
+
}
|
|
444
873
|
}
|
|
445
874
|
});
|
|
446
|
-
|
|
447
|
-
|
|
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
|
+
}
|
|
448
892
|
}
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
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", {
|
|
454
923
|
space: space.key,
|
|
455
924
|
trigger
|
|
456
925
|
}, {
|
|
457
|
-
F:
|
|
458
|
-
L:
|
|
926
|
+
F: __dxlog_file9,
|
|
927
|
+
L: 72,
|
|
459
928
|
S: this,
|
|
460
929
|
C: (f, a) => f(...a)
|
|
461
930
|
});
|
|
462
|
-
const
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
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
|
|
466
985
|
});
|
|
467
986
|
});
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
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)
|
|
471
993
|
}, {
|
|
472
|
-
F:
|
|
473
|
-
L:
|
|
994
|
+
F: __dxlog_file9,
|
|
995
|
+
L: 120,
|
|
474
996
|
S: this,
|
|
475
997
|
C: (f, a) => f(...a)
|
|
476
998
|
});
|
|
477
999
|
});
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
}
|
|
1000
|
+
if (added.length > 0) {
|
|
1001
|
+
await space.db.flush();
|
|
1002
|
+
}
|
|
481
1003
|
}
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
retryDelay: 2,
|
|
487
|
-
maxAttempts: 5
|
|
488
|
-
}) {
|
|
489
|
-
log3.info("websocket", {
|
|
490
|
-
space: space.key,
|
|
491
|
-
trigger
|
|
492
|
-
}, {
|
|
493
|
-
F: __dxlog_file3,
|
|
494
|
-
L: 213,
|
|
1004
|
+
async _open() {
|
|
1005
|
+
log9.info("open...", void 0, {
|
|
1006
|
+
F: __dxlog_file9,
|
|
1007
|
+
L: 129,
|
|
495
1008
|
S: this,
|
|
496
1009
|
C: (f, a) => f(...a)
|
|
497
1010
|
});
|
|
498
|
-
const
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
ws = new WebSocket(url);
|
|
503
|
-
Object.assign(ws, {
|
|
504
|
-
onopen: () => {
|
|
505
|
-
log3.info("opened", {
|
|
506
|
-
url
|
|
507
|
-
}, {
|
|
508
|
-
F: __dxlog_file3,
|
|
509
|
-
L: 223,
|
|
510
|
-
S: this,
|
|
511
|
-
C: (f, a) => f(...a)
|
|
512
|
-
});
|
|
513
|
-
if (trigger.init) {
|
|
514
|
-
ws.send(new TextEncoder().encode(JSON.stringify(trigger.init)));
|
|
515
|
-
}
|
|
516
|
-
open.wake(true);
|
|
517
|
-
},
|
|
518
|
-
onclose: () => {
|
|
519
|
-
log3.info("closed", {
|
|
520
|
-
url
|
|
521
|
-
}, {
|
|
522
|
-
F: __dxlog_file3,
|
|
523
|
-
L: 232,
|
|
524
|
-
S: this,
|
|
525
|
-
C: (f, a) => f(...a)
|
|
526
|
-
});
|
|
527
|
-
open.wake(false);
|
|
528
|
-
},
|
|
529
|
-
onerror: (event) => {
|
|
530
|
-
log3.catch(event.error, {
|
|
531
|
-
url
|
|
532
|
-
}, {
|
|
533
|
-
F: __dxlog_file3,
|
|
534
|
-
L: 237,
|
|
535
|
-
S: this,
|
|
536
|
-
C: (f, a) => f(...a)
|
|
537
|
-
});
|
|
538
|
-
},
|
|
539
|
-
onmessage: async (event) => {
|
|
540
|
-
try {
|
|
541
|
-
const data = JSON.parse(new TextDecoder().decode(event.data));
|
|
542
|
-
await this._execFunction(def, {
|
|
543
|
-
space: space.key,
|
|
544
|
-
data
|
|
545
|
-
});
|
|
546
|
-
} catch (err) {
|
|
547
|
-
log3.catch(err, {
|
|
548
|
-
url
|
|
549
|
-
}, {
|
|
550
|
-
F: __dxlog_file3,
|
|
551
|
-
L: 245,
|
|
552
|
-
S: this,
|
|
553
|
-
C: (f, a) => f(...a)
|
|
554
|
-
});
|
|
555
|
-
}
|
|
1011
|
+
const spaceListSubscription = this._client.spaces.subscribe(async (spaces) => {
|
|
1012
|
+
for (const space of spaces) {
|
|
1013
|
+
if (this._triggersBySpaceKey.has(space.key)) {
|
|
1014
|
+
continue;
|
|
556
1015
|
}
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
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
|
|
566
1027
|
}, {
|
|
567
|
-
F:
|
|
568
|
-
L:
|
|
1028
|
+
F: __dxlog_file9,
|
|
1029
|
+
L: 146,
|
|
569
1030
|
S: this,
|
|
570
1031
|
C: (f, a) => f(...a)
|
|
571
1032
|
});
|
|
572
|
-
await
|
|
573
|
-
|
|
1033
|
+
await this._handleRemovedTriggers(space, current, registered);
|
|
1034
|
+
this._handleNewTriggers(space, current, registered);
|
|
1035
|
+
}));
|
|
574
1036
|
}
|
|
575
|
-
}
|
|
576
|
-
|
|
577
|
-
|
|
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)
|
|
578
1044
|
});
|
|
579
1045
|
}
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
log3.info("subscription", {
|
|
585
|
-
space: space.key,
|
|
586
|
-
trigger
|
|
587
|
-
}, {
|
|
588
|
-
F: __dxlog_file3,
|
|
589
|
-
L: 271,
|
|
1046
|
+
async _close(_) {
|
|
1047
|
+
log9.info("close...", void 0, {
|
|
1048
|
+
F: __dxlog_file9,
|
|
1049
|
+
L: 159,
|
|
590
1050
|
S: this,
|
|
591
1051
|
C: (f, a) => f(...a)
|
|
592
1052
|
});
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
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)
|
|
599
1059
|
});
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
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,
|
|
608
1076
|
S: this,
|
|
609
1077
|
C: (f, a) => f(...a)
|
|
610
1078
|
});
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
objects: objects.length
|
|
626
|
-
}, {
|
|
627
|
-
F: __dxlog_file3,
|
|
628
|
-
L: 301,
|
|
629
|
-
S: this,
|
|
630
|
-
C: (f, a) => f(...a)
|
|
631
|
-
});
|
|
632
|
-
for (const object of objects) {
|
|
633
|
-
const content = object.content;
|
|
634
|
-
if (content instanceof TextV0Type) {
|
|
635
|
-
subscriptions.push(getAutomergeObjectCore(content).updates.on(debounce(() => subscription.update([
|
|
636
|
-
object
|
|
637
|
-
]), 1e3)));
|
|
638
|
-
}
|
|
639
|
-
}
|
|
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);
|
|
640
1093
|
}
|
|
641
|
-
}
|
|
642
|
-
|
|
643
|
-
|
|
644
|
-
|
|
645
|
-
|
|
646
|
-
|
|
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);
|
|
647
1114
|
}
|
|
648
1115
|
};
|
|
649
|
-
|
|
650
|
-
// packages/core/functions/src/types.ts
|
|
651
|
-
import * as S from "@effect/schema/Schema";
|
|
652
|
-
var TimerTriggerSchema = S.struct({
|
|
653
|
-
cron: S.string
|
|
654
|
-
});
|
|
655
|
-
var WebhookTriggerSchema = S.struct({
|
|
656
|
-
port: S.number
|
|
657
|
-
});
|
|
658
|
-
var WebsocketTriggerSchema = S.struct({
|
|
659
|
-
url: S.string,
|
|
660
|
-
init: S.optional(S.record(S.string, S.any))
|
|
661
|
-
});
|
|
662
|
-
var SubscriptionTriggerSchema = S.struct({
|
|
663
|
-
spaceKey: S.optional(S.string),
|
|
664
|
-
// TODO(burdon): Define query DSL.
|
|
665
|
-
filter: S.array(S.struct({
|
|
666
|
-
type: S.string,
|
|
667
|
-
props: S.optional(S.record(S.string, S.any))
|
|
668
|
-
})),
|
|
669
|
-
options: S.optional(S.struct({
|
|
670
|
-
// Watch changes to object (not just creation).
|
|
671
|
-
deep: S.optional(S.boolean),
|
|
672
|
-
// Debounce changes (delay in ms).
|
|
673
|
-
delay: S.optional(S.number)
|
|
674
|
-
}))
|
|
675
|
-
});
|
|
676
|
-
var FunctionTriggerSchema = S.struct({
|
|
677
|
-
function: S.string.pipe(S.description("Function ID/URI.")),
|
|
678
|
-
timer: S.optional(TimerTriggerSchema),
|
|
679
|
-
webhook: S.optional(WebhookTriggerSchema),
|
|
680
|
-
websocket: S.optional(WebsocketTriggerSchema),
|
|
681
|
-
subscription: S.optional(SubscriptionTriggerSchema)
|
|
682
|
-
});
|
|
683
|
-
var FunctionDefSchema = S.struct({
|
|
684
|
-
id: S.string,
|
|
685
|
-
description: S.optional(S.string),
|
|
686
|
-
name: S.string,
|
|
687
|
-
// TODO(burdon): NPM/GitHub URL?
|
|
688
|
-
handler: S.string
|
|
689
|
-
});
|
|
690
|
-
var FunctionManifestSchema = S.struct({
|
|
691
|
-
functions: S.mutable(S.array(FunctionDefSchema)),
|
|
692
|
-
triggers: S.mutable(S.array(FunctionTriggerSchema))
|
|
693
|
-
});
|
|
694
1116
|
export {
|
|
695
1117
|
DevServer,
|
|
1118
|
+
FUNCTION_SCHEMA,
|
|
1119
|
+
FunctionDef,
|
|
696
1120
|
FunctionManifestSchema,
|
|
1121
|
+
FunctionRegistry,
|
|
1122
|
+
FunctionTrigger,
|
|
697
1123
|
Scheduler,
|
|
1124
|
+
TriggerRegistry,
|
|
698
1125
|
subscriptionHandler
|
|
699
1126
|
};
|
|
700
1127
|
//# sourceMappingURL=index.mjs.map
|