@dxos/functions 0.3.7 → 0.3.8-main.0ab6c73
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/README.md +2 -2
- package/dist/lib/browser/index.mjs +213 -144
- package/dist/lib/browser/index.mjs.map +4 -4
- package/dist/lib/browser/meta.json +1 -1
- package/dist/lib/node/index.cjs +228 -158
- package/dist/lib/node/index.cjs.map +4 -4
- package/dist/lib/node/meta.json +1 -1
- package/dist/types/src/handler.d.ts +17 -0
- package/dist/types/src/handler.d.ts.map +1 -0
- package/dist/types/src/index.d.ts +2 -1
- package/dist/types/src/index.d.ts.map +1 -1
- package/dist/types/src/manifest.d.ts +25 -0
- package/dist/types/src/manifest.d.ts.map +1 -0
- package/dist/types/src/runtime/dev-server.d.ts +21 -5
- package/dist/types/src/runtime/dev-server.d.ts.map +1 -1
- package/dist/types/src/runtime/index.d.ts +1 -1
- package/dist/types/src/runtime/index.d.ts.map +1 -1
- package/dist/types/src/runtime/scheduler.d.ts +22 -0
- package/dist/types/src/runtime/scheduler.d.ts.map +1 -0
- package/package.json +11 -10
- package/src/handler.ts +28 -0
- package/src/index.ts +2 -1
- package/src/manifest.ts +40 -0
- package/src/runtime/dev-server.ts +99 -57
- package/src/runtime/index.ts +1 -1
- package/src/runtime/scheduler.ts +145 -0
- package/dist/types/src/function.d.ts +0 -36
- package/dist/types/src/function.d.ts.map +0 -1
- package/dist/types/src/runtime/trigger-manager.d.ts +0 -20
- package/dist/types/src/runtime/trigger-manager.d.ts.map +0 -1
- package/src/function.ts +0 -51
- package/src/runtime/trigger-manager.ts +0 -139
package/dist/lib/node/index.cjs
CHANGED
|
@@ -26,52 +26,67 @@ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__ge
|
|
|
26
26
|
mod
|
|
27
27
|
));
|
|
28
28
|
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
var src_exports = {};
|
|
32
|
-
__export(src_exports, {
|
|
29
|
+
var node_exports = {};
|
|
30
|
+
__export(node_exports, {
|
|
33
31
|
DevServer: () => DevServer,
|
|
34
|
-
|
|
32
|
+
Scheduler: () => Scheduler
|
|
35
33
|
});
|
|
36
|
-
module.exports = __toCommonJS(
|
|
37
|
-
|
|
38
|
-
// packages/core/functions/src/runtime/dev-server.ts
|
|
34
|
+
module.exports = __toCommonJS(node_exports);
|
|
39
35
|
var import_express = __toESM(require("express"));
|
|
36
|
+
var import_get_port_please = require("get-port-please");
|
|
40
37
|
var import_node_path = require("node:path");
|
|
41
|
-
var import_portfinder = require("portfinder");
|
|
42
38
|
var import_async = require("@dxos/async");
|
|
39
|
+
var import_invariant = require("@dxos/invariant");
|
|
43
40
|
var import_log = require("@dxos/log");
|
|
44
|
-
var
|
|
45
|
-
var
|
|
41
|
+
var import_cron = require("cron");
|
|
42
|
+
var import_async2 = require("@dxos/async");
|
|
43
|
+
var import_context = require("@dxos/context");
|
|
44
|
+
var import_echo_schema = require("@dxos/echo-schema");
|
|
45
|
+
var import_invariant2 = require("@dxos/invariant");
|
|
46
|
+
var import_log2 = require("@dxos/log");
|
|
47
|
+
var import_util = require("@dxos/util");
|
|
48
|
+
var __require = /* @__PURE__ */ ((x) => typeof require !== "undefined" ? require : typeof Proxy !== "undefined" ? new Proxy(x, {
|
|
49
|
+
get: (a, b) => (typeof require !== "undefined" ? require : a)[b]
|
|
50
|
+
}) : x)(function(x) {
|
|
51
|
+
if (typeof require !== "undefined")
|
|
52
|
+
return require.apply(this, arguments);
|
|
53
|
+
throw Error('Dynamic require of "' + x + '" is not supported');
|
|
54
|
+
});
|
|
55
|
+
var __dxlog_file = "/home/runner/work/dxos/dxos/packages/core/functions/src/runtime/dev-server.ts";
|
|
46
56
|
var DevServer = class {
|
|
47
57
|
// prettier-ignore
|
|
48
58
|
constructor(_client, _options) {
|
|
49
59
|
this._client = _client;
|
|
50
60
|
this._options = _options;
|
|
51
61
|
this._handlers = {};
|
|
52
|
-
|
|
53
|
-
get port() {
|
|
54
|
-
return this._port;
|
|
62
|
+
this._seq = 0;
|
|
55
63
|
}
|
|
56
64
|
get endpoint() {
|
|
57
|
-
|
|
65
|
+
(0, import_invariant.invariant)(this._port, void 0, {
|
|
66
|
+
F: __dxlog_file,
|
|
67
|
+
L: 45,
|
|
68
|
+
S: this,
|
|
69
|
+
A: [
|
|
70
|
+
"this._port",
|
|
71
|
+
""
|
|
72
|
+
]
|
|
73
|
+
});
|
|
74
|
+
return `http://localhost:${this._port}`;
|
|
75
|
+
}
|
|
76
|
+
get proxy() {
|
|
77
|
+
return this._proxy;
|
|
58
78
|
}
|
|
59
79
|
get functions() {
|
|
60
|
-
return Object.
|
|
80
|
+
return Object.values(this._handlers);
|
|
61
81
|
}
|
|
62
82
|
async initialize() {
|
|
63
|
-
for (const
|
|
83
|
+
for (const def of this._options.manifest.functions) {
|
|
64
84
|
try {
|
|
65
|
-
|
|
66
|
-
const handler = module2.default;
|
|
67
|
-
if (typeof handler !== "function") {
|
|
68
|
-
throw new Error(`Handler must export default function: ${id}`);
|
|
69
|
-
}
|
|
70
|
-
this._handlers[id] = handler;
|
|
85
|
+
await this._load(def);
|
|
71
86
|
} catch (err) {
|
|
72
|
-
import_log.log.error("parsing function (check
|
|
87
|
+
import_log.log.error("parsing function (check manifest)", err, {
|
|
73
88
|
F: __dxlog_file,
|
|
74
|
-
L:
|
|
89
|
+
L: 62,
|
|
75
90
|
S: this,
|
|
76
91
|
C: (f, a) => f(...a)
|
|
77
92
|
});
|
|
@@ -81,58 +96,55 @@ var DevServer = class {
|
|
|
81
96
|
async start() {
|
|
82
97
|
const app = (0, import_express.default)();
|
|
83
98
|
app.use(import_express.default.json());
|
|
84
|
-
app.post("/:
|
|
85
|
-
const
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
F: __dxlog_file,
|
|
91
|
-
L: 74,
|
|
92
|
-
S: this,
|
|
93
|
-
C: (f, a) => f(...a)
|
|
94
|
-
});
|
|
95
|
-
const builder = {
|
|
96
|
-
status: (code) => {
|
|
97
|
-
res.statusCode = code;
|
|
98
|
-
return builder;
|
|
99
|
-
},
|
|
100
|
-
succeed: (result = {}) => {
|
|
101
|
-
res.end(JSON.stringify(result));
|
|
102
|
-
return builder;
|
|
103
|
-
}
|
|
104
|
-
};
|
|
105
|
-
const context = {
|
|
106
|
-
client: this._client,
|
|
107
|
-
status: builder.status.bind(builder)
|
|
108
|
-
};
|
|
109
|
-
void (async () => {
|
|
110
|
-
try {
|
|
111
|
-
await this._handlers[functionName]({
|
|
112
|
-
event: req.body,
|
|
113
|
-
context
|
|
114
|
-
});
|
|
115
|
-
} catch (err) {
|
|
116
|
-
res.statusCode = 500;
|
|
117
|
-
res.end(err.message);
|
|
99
|
+
app.post("/:name", async (req, res) => {
|
|
100
|
+
const { name } = req.params;
|
|
101
|
+
try {
|
|
102
|
+
if (this._options.reload) {
|
|
103
|
+
const { def } = this._handlers[name];
|
|
104
|
+
await this._load(def, true);
|
|
118
105
|
}
|
|
119
|
-
|
|
106
|
+
res.statusCode = await this._invoke(name, req.body);
|
|
107
|
+
res.end();
|
|
108
|
+
} catch (err) {
|
|
109
|
+
import_log.log.error(err, void 0, {
|
|
110
|
+
F: __dxlog_file,
|
|
111
|
+
L: 82,
|
|
112
|
+
S: this,
|
|
113
|
+
C: (f, a) => f(...a)
|
|
114
|
+
});
|
|
115
|
+
res.statusCode = 500;
|
|
116
|
+
res.end();
|
|
117
|
+
}
|
|
120
118
|
});
|
|
121
|
-
this._port = await (0,
|
|
122
|
-
|
|
119
|
+
this._port = await (0, import_get_port_please.getPort)({
|
|
120
|
+
port: 7200,
|
|
121
|
+
portRange: [
|
|
122
|
+
7200,
|
|
123
|
+
7299
|
|
124
|
+
]
|
|
123
125
|
});
|
|
124
126
|
this._server = app.listen(this._port);
|
|
125
127
|
try {
|
|
126
|
-
const { registrationId } = await this._client.services.services.FunctionRegistryService.register({
|
|
128
|
+
const { registrationId, endpoint } = await this._client.services.services.FunctionRegistryService.register({
|
|
127
129
|
endpoint: this.endpoint,
|
|
128
|
-
functions: this.functions.map((name) => ({
|
|
130
|
+
functions: this.functions.map(({ def: { name } }) => ({
|
|
129
131
|
name
|
|
130
132
|
}))
|
|
131
133
|
});
|
|
134
|
+
import_log.log.info("registered", {
|
|
135
|
+
registrationId,
|
|
136
|
+
endpoint
|
|
137
|
+
}, {
|
|
138
|
+
F: __dxlog_file,
|
|
139
|
+
L: 98,
|
|
140
|
+
S: this,
|
|
141
|
+
C: (f, a) => f(...a)
|
|
142
|
+
});
|
|
132
143
|
this._registrationId = registrationId;
|
|
144
|
+
this._proxy = endpoint;
|
|
133
145
|
} catch (err) {
|
|
134
146
|
await this.stop();
|
|
135
|
-
throw new Error("FunctionRegistryService not available
|
|
147
|
+
throw new Error("FunctionRegistryService not available (check plugin is configured).");
|
|
136
148
|
}
|
|
137
149
|
}
|
|
138
150
|
async stop() {
|
|
@@ -142,52 +154,132 @@ var DevServer = class {
|
|
|
142
154
|
await this._client.services.services.FunctionRegistryService.unregister({
|
|
143
155
|
registrationId: this._registrationId
|
|
144
156
|
});
|
|
157
|
+
import_log.log.info("unregistered", {
|
|
158
|
+
registrationId: this._registrationId
|
|
159
|
+
}, {
|
|
160
|
+
F: __dxlog_file,
|
|
161
|
+
L: 115,
|
|
162
|
+
S: this,
|
|
163
|
+
C: (f, a) => f(...a)
|
|
164
|
+
});
|
|
145
165
|
this._registrationId = void 0;
|
|
166
|
+
this._proxy = void 0;
|
|
146
167
|
}
|
|
147
168
|
trigger.wake();
|
|
148
169
|
});
|
|
149
170
|
await trigger.wait();
|
|
150
|
-
this._server = void 0;
|
|
151
171
|
this._port = void 0;
|
|
172
|
+
this._server = void 0;
|
|
173
|
+
}
|
|
174
|
+
/**
|
|
175
|
+
* Load function.
|
|
176
|
+
*/
|
|
177
|
+
async _load(def, flush = false) {
|
|
178
|
+
const { id, name, handler } = def;
|
|
179
|
+
const path = (0, import_node_path.join)(this._options.directory, handler);
|
|
180
|
+
import_log.log.info("loading", {
|
|
181
|
+
id
|
|
182
|
+
}, {
|
|
183
|
+
F: __dxlog_file,
|
|
184
|
+
L: 134,
|
|
185
|
+
S: this,
|
|
186
|
+
C: (f, a) => f(...a)
|
|
187
|
+
});
|
|
188
|
+
if (flush) {
|
|
189
|
+
Object.keys(__require.cache).filter((key) => key.startsWith(path)).forEach((key) => delete __require.cache[key]);
|
|
190
|
+
}
|
|
191
|
+
const module2 = __require(path);
|
|
192
|
+
if (typeof module2.default !== "function") {
|
|
193
|
+
throw new Error(`Handler must export default function: ${id}`);
|
|
194
|
+
}
|
|
195
|
+
this._handlers[name] = {
|
|
196
|
+
def,
|
|
197
|
+
handler: module2.default
|
|
198
|
+
};
|
|
199
|
+
}
|
|
200
|
+
/**
|
|
201
|
+
* Invoke function handler.
|
|
202
|
+
*/
|
|
203
|
+
async _invoke(name, event) {
|
|
204
|
+
const seq = ++this._seq;
|
|
205
|
+
const now = Date.now();
|
|
206
|
+
import_log.log.info("req", {
|
|
207
|
+
seq,
|
|
208
|
+
name
|
|
209
|
+
}, {
|
|
210
|
+
F: __dxlog_file,
|
|
211
|
+
L: 159,
|
|
212
|
+
S: this,
|
|
213
|
+
C: (f, a) => f(...a)
|
|
214
|
+
});
|
|
215
|
+
const { handler } = this._handlers[name];
|
|
216
|
+
const context = {
|
|
217
|
+
client: this._client
|
|
218
|
+
};
|
|
219
|
+
let statusCode = 200;
|
|
220
|
+
const response = {
|
|
221
|
+
status: (code) => {
|
|
222
|
+
statusCode = code;
|
|
223
|
+
return response;
|
|
224
|
+
}
|
|
225
|
+
};
|
|
226
|
+
await handler({
|
|
227
|
+
context,
|
|
228
|
+
event,
|
|
229
|
+
response
|
|
230
|
+
});
|
|
231
|
+
import_log.log.info("res", {
|
|
232
|
+
seq,
|
|
233
|
+
name,
|
|
234
|
+
statusCode,
|
|
235
|
+
duration: Date.now() - now
|
|
236
|
+
}, {
|
|
237
|
+
F: __dxlog_file,
|
|
238
|
+
L: 175,
|
|
239
|
+
S: this,
|
|
240
|
+
C: (f, a) => f(...a)
|
|
241
|
+
});
|
|
242
|
+
return statusCode;
|
|
152
243
|
}
|
|
153
244
|
};
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
var import_context = require("@dxos/context");
|
|
158
|
-
var import_echo_schema = require("@dxos/echo-schema");
|
|
159
|
-
var import_invariant = require("@dxos/invariant");
|
|
160
|
-
var import_log2 = require("@dxos/log");
|
|
161
|
-
var import_util = require("@dxos/util");
|
|
162
|
-
var __dxlog_file2 = "/home/circleci/project/packages/core/functions/src/runtime/trigger-manager.ts";
|
|
163
|
-
var TriggerManager = class {
|
|
164
|
-
constructor(_client, _triggers, _invokeOptions) {
|
|
245
|
+
var __dxlog_file2 = "/home/runner/work/dxos/dxos/packages/core/functions/src/runtime/scheduler.ts";
|
|
246
|
+
var Scheduler = class {
|
|
247
|
+
constructor(_client, _manifest, _options) {
|
|
165
248
|
this._client = _client;
|
|
166
|
-
this.
|
|
167
|
-
this.
|
|
168
|
-
this._mounts = new import_util.ComplexMap(({
|
|
169
|
-
this._queries = /* @__PURE__ */ new Set();
|
|
249
|
+
this._manifest = _manifest;
|
|
250
|
+
this._options = _options;
|
|
251
|
+
this._mounts = new import_util.ComplexMap(({ id, spaceKey }) => `${spaceKey.toHex()}:${id}`);
|
|
170
252
|
}
|
|
171
253
|
async start() {
|
|
172
254
|
this._client.spaces.subscribe(async (spaces) => {
|
|
173
255
|
for (const space of spaces) {
|
|
174
256
|
await space.waitUntilReady();
|
|
175
|
-
for (const trigger of this.
|
|
257
|
+
for (const trigger of this._manifest.triggers ?? []) {
|
|
176
258
|
await this.mount(new import_context.Context(), space, trigger);
|
|
177
259
|
}
|
|
178
260
|
}
|
|
179
261
|
});
|
|
180
262
|
}
|
|
181
263
|
async stop() {
|
|
182
|
-
for (const {
|
|
183
|
-
await this.unmount(
|
|
264
|
+
for (const { id, spaceKey } of this._mounts.keys()) {
|
|
265
|
+
await this.unmount(id, spaceKey);
|
|
184
266
|
}
|
|
185
267
|
}
|
|
186
268
|
async mount(ctx, space, trigger) {
|
|
187
269
|
const key = {
|
|
188
|
-
|
|
270
|
+
id: trigger.function,
|
|
189
271
|
spaceKey: space.key
|
|
190
272
|
};
|
|
273
|
+
const def = this._manifest.functions.find((config) => config.id === trigger.function);
|
|
274
|
+
(0, import_invariant2.invariant)(def, `Function not found: ${trigger.function}`, {
|
|
275
|
+
F: __dxlog_file2,
|
|
276
|
+
L: 59,
|
|
277
|
+
S: this,
|
|
278
|
+
A: [
|
|
279
|
+
"def",
|
|
280
|
+
"`Function not found: ${trigger.function}`"
|
|
281
|
+
]
|
|
282
|
+
});
|
|
191
283
|
const exists = this._mounts.get(key);
|
|
192
284
|
if (!exists) {
|
|
193
285
|
this._mounts.set(key, {
|
|
@@ -199,57 +291,55 @@ var TriggerManager = class {
|
|
|
199
291
|
trigger
|
|
200
292
|
}, {
|
|
201
293
|
F: __dxlog_file2,
|
|
202
|
-
L:
|
|
294
|
+
L: 64,
|
|
203
295
|
S: this,
|
|
204
296
|
C: (f, a) => f(...a)
|
|
205
297
|
});
|
|
206
298
|
if (ctx.disposed) {
|
|
207
299
|
return;
|
|
208
300
|
}
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
301
|
+
if (trigger.schedule) {
|
|
302
|
+
const task = new import_async2.DeferredTask(ctx, async () => {
|
|
303
|
+
await this.execFunction(def, {
|
|
304
|
+
space: space.key
|
|
305
|
+
});
|
|
214
306
|
});
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
space: space.key,
|
|
227
|
-
objects: objectIds.size,
|
|
228
|
-
count
|
|
229
|
-
}, {
|
|
230
|
-
F: __dxlog_file2,
|
|
231
|
-
L: 84,
|
|
232
|
-
S: this,
|
|
233
|
-
C: (f, a) => f(...a)
|
|
307
|
+
const job = new import_cron.CronJob(trigger.schedule, () => task.schedule());
|
|
308
|
+
job.start();
|
|
309
|
+
ctx.onDispose(() => job.stop());
|
|
310
|
+
}
|
|
311
|
+
if (trigger.subscription) {
|
|
312
|
+
const objectIds = /* @__PURE__ */ new Set();
|
|
313
|
+
const task = new import_async2.DeferredTask(ctx, async () => {
|
|
314
|
+
await this.execFunction(def, {
|
|
315
|
+
space: space.key,
|
|
316
|
+
objects: Array.from(objectIds)
|
|
317
|
+
});
|
|
234
318
|
});
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
subscription
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
319
|
+
const subscription = (0, import_echo_schema.createSubscription)(({ added, updated }) => {
|
|
320
|
+
for (const object of added) {
|
|
321
|
+
objectIds.add(object.id);
|
|
322
|
+
}
|
|
323
|
+
for (const object of updated) {
|
|
324
|
+
objectIds.add(object.id);
|
|
325
|
+
}
|
|
326
|
+
task.schedule();
|
|
327
|
+
});
|
|
328
|
+
const { type, props } = trigger.subscription;
|
|
329
|
+
const query = space.db.query(import_echo_schema.Filter.typename(type, props));
|
|
330
|
+
const unsubscribe = query.subscribe(({ objects }) => {
|
|
331
|
+
subscription.update(objects);
|
|
332
|
+
}, true);
|
|
333
|
+
ctx.onDispose(() => {
|
|
334
|
+
subscription.unsubscribe();
|
|
335
|
+
unsubscribe();
|
|
336
|
+
});
|
|
337
|
+
}
|
|
248
338
|
}
|
|
249
339
|
}
|
|
250
|
-
async unmount(
|
|
340
|
+
async unmount(id, spaceKey) {
|
|
251
341
|
const key = {
|
|
252
|
-
|
|
342
|
+
id,
|
|
253
343
|
spaceKey
|
|
254
344
|
};
|
|
255
345
|
const { ctx } = this._mounts.get(key) ?? {};
|
|
@@ -258,59 +348,39 @@ var TriggerManager = class {
|
|
|
258
348
|
await ctx.dispose();
|
|
259
349
|
}
|
|
260
350
|
}
|
|
261
|
-
async
|
|
262
|
-
const { endpoint, runtime } = options;
|
|
263
|
-
(0, import_invariant.invariant)(endpoint, "Missing endpoint", {
|
|
264
|
-
F: __dxlog_file2,
|
|
265
|
-
L: 120,
|
|
266
|
-
S: this,
|
|
267
|
-
A: [
|
|
268
|
-
"endpoint",
|
|
269
|
-
"'Missing endpoint'"
|
|
270
|
-
]
|
|
271
|
-
});
|
|
272
|
-
(0, import_invariant.invariant)(runtime, "Missing runtime", {
|
|
273
|
-
F: __dxlog_file2,
|
|
274
|
-
L: 121,
|
|
275
|
-
S: this,
|
|
276
|
-
A: [
|
|
277
|
-
"runtime",
|
|
278
|
-
"'Missing runtime'"
|
|
279
|
-
]
|
|
280
|
-
});
|
|
351
|
+
async execFunction(def, data) {
|
|
281
352
|
try {
|
|
282
|
-
(0, import_log2.log)("
|
|
283
|
-
function:
|
|
353
|
+
(0, import_log2.log)("request", {
|
|
354
|
+
function: def.id
|
|
284
355
|
}, {
|
|
285
356
|
F: __dxlog_file2,
|
|
286
|
-
L:
|
|
357
|
+
L: 130,
|
|
287
358
|
S: this,
|
|
288
359
|
C: (f, a) => f(...a)
|
|
289
360
|
});
|
|
290
|
-
const
|
|
291
|
-
const res = await fetch(url, {
|
|
361
|
+
const response = await fetch(`${this._options.endpoint}/${def.name}`, {
|
|
292
362
|
method: "POST",
|
|
293
|
-
body: JSON.stringify(data),
|
|
294
363
|
headers: {
|
|
295
364
|
"Content-Type": "application/json"
|
|
296
|
-
}
|
|
365
|
+
},
|
|
366
|
+
body: JSON.stringify(data)
|
|
297
367
|
});
|
|
298
368
|
(0, import_log2.log)("result", {
|
|
299
|
-
function:
|
|
300
|
-
result:
|
|
369
|
+
function: def.id,
|
|
370
|
+
result: response.status
|
|
301
371
|
}, {
|
|
302
372
|
F: __dxlog_file2,
|
|
303
|
-
L:
|
|
373
|
+
L: 140,
|
|
304
374
|
S: this,
|
|
305
375
|
C: (f, a) => f(...a)
|
|
306
376
|
});
|
|
307
377
|
} catch (err) {
|
|
308
378
|
import_log2.log.error("error", {
|
|
309
|
-
function:
|
|
379
|
+
function: def.id,
|
|
310
380
|
error: err.message
|
|
311
381
|
}, {
|
|
312
382
|
F: __dxlog_file2,
|
|
313
|
-
L:
|
|
383
|
+
L: 142,
|
|
314
384
|
S: this,
|
|
315
385
|
C: (f, a) => f(...a)
|
|
316
386
|
});
|
|
@@ -320,6 +390,6 @@ var TriggerManager = class {
|
|
|
320
390
|
// Annotate the CommonJS export names for ESM import in node:
|
|
321
391
|
0 && (module.exports = {
|
|
322
392
|
DevServer,
|
|
323
|
-
|
|
393
|
+
Scheduler
|
|
324
394
|
});
|
|
325
395
|
//# sourceMappingURL=index.cjs.map
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
|
-
"sources": ["../../../src/
|
|
4
|
-
"sourcesContent": ["//\n// Copyright 2023 DXOS.org\n//\n\
|
|
5
|
-
"mappings": "
|
|
6
|
-
"names": ["
|
|
3
|
+
"sources": ["../../../src/runtime/dev-server.ts", "../../../src/runtime/scheduler.ts"],
|
|
4
|
+
"sourcesContent": ["//\n// Copyright 2023 DXOS.org\n//\n\nimport express from 'express';\nimport { getPort } from 'get-port-please';\nimport type http from 'http';\nimport { join } from 'node:path';\n\nimport { Trigger } from '@dxos/async';\nimport { type Client } from '@dxos/client';\nimport { invariant } from '@dxos/invariant';\nimport { log } from '@dxos/log';\n\nimport { type FunctionContext, type FunctionHandler, type Response } from '../handler';\nimport { type FunctionDef, type FunctionManifest } from '../manifest';\n\nexport type DevServerOptions = {\n port?: number;\n directory: string;\n manifest: FunctionManifest;\n reload?: boolean;\n};\n\n/**\n * Functions dev server provides a local HTTP server for testing functions.\n */\nexport class DevServer {\n // Function handlers indexed by name (URL path).\n private readonly _handlers: Record<string, { def: FunctionDef; handler: FunctionHandler<any> }> = {};\n\n private _server?: http.Server;\n private _port?: number;\n private _registrationId?: string;\n private _proxy?: string;\n private _seq = 0;\n\n // prettier-ignore\n constructor(\n private readonly _client: Client,\n private readonly _options: DevServerOptions,\n ) {}\n\n get endpoint() {\n invariant(this._port);\n return `http://localhost:${this._port}`;\n }\n\n get proxy() {\n return this._proxy;\n }\n\n get functions() {\n return Object.values(this._handlers);\n }\n\n async initialize() {\n for (const def of this._options.manifest.functions) {\n try {\n await this._load(def);\n } catch (err) {\n log.error('parsing function (check manifest)', err);\n }\n }\n }\n\n async start() {\n const app = express();\n app.use(express.json());\n\n app.post('/:name', async (req, res) => {\n const { name } = req.params;\n try {\n if (this._options.reload) {\n const { def } = this._handlers[name];\n await this._load(def, true);\n }\n\n res.statusCode = await this._invoke(name, req.body);\n res.end();\n } catch (err: any) {\n log.error(err);\n res.statusCode = 500;\n res.end();\n }\n });\n\n this._port = await getPort({ port: 7200, portRange: [7200, 7299] });\n this._server = app.listen(this._port);\n\n try {\n // Register functions.\n const { registrationId, endpoint } = await this._client.services.services.FunctionRegistryService!.register({\n endpoint: this.endpoint,\n functions: this.functions.map(({ def: { name } }) => ({ name })),\n });\n\n log.info('registered', { registrationId, endpoint });\n this._registrationId = registrationId;\n this._proxy = endpoint;\n } catch (err: any) {\n await this.stop();\n throw new Error('FunctionRegistryService not available (check plugin is configured).');\n }\n }\n\n async stop() {\n const trigger = new Trigger();\n this._server?.close(async () => {\n if (this._registrationId) {\n await this._client.services.services.FunctionRegistryService!.unregister({\n registrationId: this._registrationId,\n });\n\n log.info('unregistered', { registrationId: this._registrationId });\n this._registrationId = undefined;\n this._proxy = undefined;\n }\n\n trigger.wake();\n });\n\n await trigger.wait();\n this._port = undefined;\n this._server = undefined;\n }\n\n /**\n * Load function.\n */\n private async _load(def: FunctionDef, flush = false) {\n const { id, name, handler } = def;\n const path = join(this._options.directory, handler);\n log.info('loading', { id });\n\n // Remove from cache.\n if (flush) {\n Object.keys(require.cache)\n .filter((key) => key.startsWith(path))\n .forEach((key) => delete require.cache[key]);\n }\n\n // eslint-disable-next-line @typescript-eslint/no-var-requires\n const module = require(path);\n if (typeof module.default !== 'function') {\n throw new Error(`Handler must export default function: ${id}`);\n }\n\n this._handlers[name] = { def, handler: module.default };\n }\n\n /**\n * Invoke function handler.\n */\n private async _invoke(name: string, event: any) {\n const seq = ++this._seq;\n const now = Date.now();\n\n log.info('req', { seq, name });\n const { handler } = this._handlers[name];\n\n const context: FunctionContext = {\n client: this._client,\n };\n\n let statusCode = 200;\n const response: Response = {\n status: (code: number) => {\n statusCode = code;\n return response;\n },\n };\n\n await handler({ context, event, response });\n log.info('res', { seq, name, statusCode, duration: Date.now() - now });\n\n return statusCode;\n }\n}\n", "//\n// Copyright 2023 DXOS.org\n//\n\nimport { CronJob } from 'cron';\n\nimport { DeferredTask } from '@dxos/async';\nimport { type Client, type PublicKey } from '@dxos/client';\nimport { type Space } from '@dxos/client/echo';\nimport { Context } from '@dxos/context';\nimport { Filter, createSubscription } from '@dxos/echo-schema';\nimport { invariant } from '@dxos/invariant';\nimport { log } from '@dxos/log';\nimport { ComplexMap } from '@dxos/util';\n\nimport { type FunctionDef, type FunctionManifest, type FunctionTrigger } from '../manifest';\n\ntype SchedulerOptions = {\n endpoint: string;\n};\n\n/**\n * Functions scheduler.\n */\n// TODO(burdon): Create tests.\nexport class Scheduler {\n // Map of mounted functions.\n private readonly _mounts = new ComplexMap<\n { id: string; spaceKey: PublicKey },\n { ctx: Context; trigger: FunctionTrigger }\n >(({ id, spaceKey }) => `${spaceKey.toHex()}:${id}`);\n\n constructor(\n private readonly _client: Client,\n private readonly _manifest: FunctionManifest,\n private readonly _options: SchedulerOptions,\n ) {}\n\n async start() {\n this._client.spaces.subscribe(async (spaces) => {\n for (const space of spaces) {\n await space.waitUntilReady();\n for (const trigger of this._manifest.triggers ?? []) {\n await this.mount(new Context(), space, trigger);\n }\n }\n });\n }\n\n async stop() {\n for (const { id, spaceKey } of this._mounts.keys()) {\n await this.unmount(id, spaceKey);\n }\n }\n\n private async mount(ctx: Context, space: Space, trigger: FunctionTrigger) {\n const key = { id: trigger.function, spaceKey: space.key };\n const def = this._manifest.functions.find((config) => config.id === trigger.function);\n invariant(def, `Function not found: ${trigger.function}`);\n\n const exists = this._mounts.get(key);\n if (!exists) {\n this._mounts.set(key, { ctx, trigger });\n log('mount', { space: space.key, trigger });\n if (ctx.disposed) {\n return;\n }\n\n // Cron schedule.\n if (trigger.schedule) {\n const task = new DeferredTask(ctx, async () => {\n await this.execFunction(def, {\n space: space.key,\n });\n });\n\n // TODO(burdon): Check greater than 30s min (use cron-parser).\n const job = new CronJob(trigger.schedule, () => task.schedule());\n\n job.start();\n ctx.onDispose(() => job.stop());\n }\n\n // ECHO subscription.\n if (trigger.subscription) {\n const objectIds = new Set<string>();\n const task = new DeferredTask(ctx, async () => {\n await this.execFunction(def, {\n space: space.key,\n objects: Array.from(objectIds),\n });\n });\n\n const subscription = createSubscription(({ added, updated }) => {\n for (const object of added) {\n objectIds.add(object.id);\n }\n for (const object of updated) {\n objectIds.add(object.id);\n }\n\n task.schedule();\n });\n\n const { type, props } = trigger.subscription;\n const query = space.db.query(Filter.typename(type, props));\n const unsubscribe = query.subscribe(({ objects }) => {\n subscription.update(objects);\n }, true);\n\n ctx.onDispose(() => {\n subscription.unsubscribe();\n unsubscribe();\n });\n }\n }\n }\n\n private async unmount(id: string, spaceKey: PublicKey) {\n const key = { id, spaceKey };\n const { ctx } = this._mounts.get(key) ?? {};\n if (ctx) {\n this._mounts.delete(key);\n await ctx.dispose();\n }\n }\n\n private async execFunction(def: FunctionDef, data: any) {\n try {\n log('request', { function: def.id });\n const response = await fetch(`${this._options.endpoint}/${def.name}`, {\n method: 'POST',\n headers: {\n 'Content-Type': 'application/json',\n },\n body: JSON.stringify(data),\n });\n\n // const result = await response.json();\n log('result', { function: def.id, result: response.status });\n } catch (err: any) {\n log.error('error', { function: def.id, error: err.message });\n }\n }\n}\n"],
|
|
5
|
+
"mappings": ";;;;;;;;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AI2C;MAAG,GAAA;;;;;;AAClC,YAAMoB,WAAW,MAAMuD,MAAM,GAAG,KAAKlI,SAASG,QAAQ,IAAIS,IAAIc,IAAI,IAAI;QACpEyG,QAAQ;QACRC,SAAS;UACP,gBAAgB;QAClB;QACArG,MAAMsG,KAAKC,UAAUL,IAAAA;MACvB,CAAA;AAGAjH,sBAAAA,KAAI,UAAU;QAAE8E,UAAUlF,IAAI2C;QAAIgF,QAAQ5D,SAASC;MAAO,GAAA;;;;;;IAC5D,SAAS7D,KAAU;AACjBC,kBAAAA,IAAIC,MAAM,SAAS;QAAE6E,UAAUlF,IAAI2C;QAAItC,OAAOF,IAAIyH;MAAQ,GAAA;;;;;;IAC5D;EACF;AACF;",
|
|
6
|
+
"names": ["import_async", "import_invariant", "import_log", "DevServer", "constructor", "_client", "_options", "_handlers", "_seq", "endpoint", "invariant", "_port", "proxy", "_proxy", "functions", "Object", "values", "initialize", "def", "manifest", "_load", "err", "log", "error", "start", "app", "express", "use", "json", "post", "req", "res", "name", "params", "reload", "statusCode", "_invoke", "body", "end", "getPort", "port", "portRange", "_server", "listen", "registrationId", "services", "FunctionRegistryService", "register", "map", "info", "_registrationId", "stop", "Error", "trigger", "Trigger", "close", "unregister", "undefined", "wake", "wait", "flush", "id", "handler", "path", "join", "directory", "keys", "require", "cache", "filter", "key", "startsWith", "forEach", "module", "default", "event", "seq", "now", "Date", "context", "client", "response", "status", "code", "duration", "Scheduler", "_manifest", "_mounts", "ComplexMap", "spaceKey", "toHex", "spaces", "subscribe", "space", "waitUntilReady", "triggers", "mount", "Context", "unmount", "ctx", "function", "find", "config", "exists", "get", "set", "disposed", "schedule", "task", "DeferredTask", "execFunction", "job", "CronJob", "onDispose", "subscription", "objectIds", "Set", "objects", "Array", "from", "createSubscription", "added", "updated", "object", "add", "type", "props", "query", "db", "Filter", "typename", "unsubscribe", "update", "delete", "dispose", "data", "fetch", "method", "headers", "JSON", "stringify", "result", "message"]
|
|
7
7
|
}
|