@dxos/functions 0.3.8-main.152ec6f → 0.3.8-main.1b6c849
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/index.mjs +159 -141
- package/dist/lib/browser/index.mjs.map +4 -4
- package/dist/lib/browser/meta.json +1 -1
- package/dist/lib/node/index.cjs +159 -141
- package/dist/lib/node/index.cjs.map +4 -4
- package/dist/lib/node/meta.json +1 -1
- package/dist/types/src/handler.d.ts +2 -6
- package/dist/types/src/handler.d.ts.map +1 -1
- package/dist/types/src/manifest.d.ts +3 -2
- package/dist/types/src/manifest.d.ts.map +1 -1
- package/dist/types/src/runtime/dev-server.d.ts +10 -0
- 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/{trigger-manager.d.ts → scheduler.d.ts} +9 -6
- package/dist/types/src/runtime/scheduler.d.ts.map +1 -0
- package/package.json +10 -9
- package/src/handler.ts +7 -8
- package/src/manifest.ts +7 -3
- package/src/runtime/dev-server.ts +69 -45
- package/src/runtime/index.ts +1 -1
- package/src/runtime/scheduler.ts +145 -0
- package/dist/types/src/runtime/trigger-manager.d.ts.map +0 -1
- package/src/runtime/trigger-manager.ts +0 -138
package/dist/lib/node/index.cjs
CHANGED
|
@@ -29,7 +29,7 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
|
|
|
29
29
|
var node_exports = {};
|
|
30
30
|
__export(node_exports, {
|
|
31
31
|
DevServer: () => DevServer,
|
|
32
|
-
|
|
32
|
+
Scheduler: () => Scheduler
|
|
33
33
|
});
|
|
34
34
|
module.exports = __toCommonJS(node_exports);
|
|
35
35
|
var import_express = __toESM(require("express"));
|
|
@@ -38,6 +38,7 @@ var import_node_path = require("node:path");
|
|
|
38
38
|
var import_async = require("@dxos/async");
|
|
39
39
|
var import_invariant = require("@dxos/invariant");
|
|
40
40
|
var import_log = require("@dxos/log");
|
|
41
|
+
var import_cron = require("cron");
|
|
41
42
|
var import_async2 = require("@dxos/async");
|
|
42
43
|
var import_context = require("@dxos/context");
|
|
43
44
|
var import_echo_schema = require("@dxos/echo-schema");
|
|
@@ -58,11 +59,12 @@ var DevServer = class {
|
|
|
58
59
|
this._client = _client;
|
|
59
60
|
this._options = _options;
|
|
60
61
|
this._handlers = {};
|
|
62
|
+
this._seq = 0;
|
|
61
63
|
}
|
|
62
64
|
get endpoint() {
|
|
63
65
|
(0, import_invariant.invariant)(this._port, void 0, {
|
|
64
66
|
F: __dxlog_file,
|
|
65
|
-
L:
|
|
67
|
+
L: 45,
|
|
66
68
|
S: this,
|
|
67
69
|
A: [
|
|
68
70
|
"this._port",
|
|
@@ -79,29 +81,12 @@ var DevServer = class {
|
|
|
79
81
|
}
|
|
80
82
|
async initialize() {
|
|
81
83
|
for (const def of this._options.manifest.functions) {
|
|
82
|
-
const { id, path, handler: dir } = def;
|
|
83
84
|
try {
|
|
84
|
-
|
|
85
|
-
const handler = module2.default;
|
|
86
|
-
if (typeof handler !== "function") {
|
|
87
|
-
throw new Error(`Handler must export default function: ${id}`);
|
|
88
|
-
}
|
|
89
|
-
if (this._handlers[path]) {
|
|
90
|
-
import_log.log.warn(`Function already registered: ${id}`, void 0, {
|
|
91
|
-
F: __dxlog_file,
|
|
92
|
-
L: 67,
|
|
93
|
-
S: this,
|
|
94
|
-
C: (f, a) => f(...a)
|
|
95
|
-
});
|
|
96
|
-
}
|
|
97
|
-
this._handlers[path] = {
|
|
98
|
-
def,
|
|
99
|
-
handler
|
|
100
|
-
};
|
|
85
|
+
await this._load(def);
|
|
101
86
|
} catch (err) {
|
|
102
87
|
import_log.log.error("parsing function (check manifest)", err, {
|
|
103
88
|
F: __dxlog_file,
|
|
104
|
-
L:
|
|
89
|
+
L: 62,
|
|
105
90
|
S: this,
|
|
106
91
|
C: (f, a) => f(...a)
|
|
107
92
|
});
|
|
@@ -113,46 +98,23 @@ var DevServer = class {
|
|
|
113
98
|
app.use(import_express.default.json());
|
|
114
99
|
app.post("/:name", async (req, res) => {
|
|
115
100
|
const { name } = req.params;
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
},
|
|
121
|
-
succeed: (result = {}) => {
|
|
122
|
-
res.end(JSON.stringify(result));
|
|
123
|
-
return response;
|
|
124
|
-
}
|
|
125
|
-
};
|
|
126
|
-
const context = {
|
|
127
|
-
client: this._client,
|
|
128
|
-
status: response.status.bind(response)
|
|
129
|
-
};
|
|
130
|
-
void (async () => {
|
|
131
|
-
try {
|
|
132
|
-
(0, import_log.log)(`invoking: ${name}`, void 0, {
|
|
133
|
-
F: __dxlog_file,
|
|
134
|
-
L: 103,
|
|
135
|
-
S: this,
|
|
136
|
-
C: (f, a) => f(...a)
|
|
137
|
-
});
|
|
138
|
-
const { handler } = this._handlers[name];
|
|
139
|
-
const response2 = await handler({
|
|
140
|
-
context,
|
|
141
|
-
event: req.body
|
|
142
|
-
});
|
|
143
|
-
(0, import_log.log)("done", {
|
|
144
|
-
response: response2
|
|
145
|
-
}, {
|
|
146
|
-
F: __dxlog_file,
|
|
147
|
-
L: 106,
|
|
148
|
-
S: this,
|
|
149
|
-
C: (f, a) => f(...a)
|
|
150
|
-
});
|
|
151
|
-
} catch (err) {
|
|
152
|
-
res.statusCode = 500;
|
|
153
|
-
res.end(err.message);
|
|
101
|
+
try {
|
|
102
|
+
if (this._options.reload) {
|
|
103
|
+
const { def } = this._handlers[name];
|
|
104
|
+
await this._load(def, true);
|
|
154
105
|
}
|
|
155
|
-
|
|
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
|
+
}
|
|
156
118
|
});
|
|
157
119
|
this._port = await (0, import_get_port_please.getPort)({
|
|
158
120
|
port: 7200,
|
|
@@ -165,8 +127,8 @@ var DevServer = class {
|
|
|
165
127
|
try {
|
|
166
128
|
const { registrationId, endpoint } = await this._client.services.services.FunctionRegistryService.register({
|
|
167
129
|
endpoint: this.endpoint,
|
|
168
|
-
functions: this.functions.map(({ def: {
|
|
169
|
-
name
|
|
130
|
+
functions: this.functions.map(({ def: { name } }) => ({
|
|
131
|
+
name
|
|
170
132
|
}))
|
|
171
133
|
});
|
|
172
134
|
import_log.log.info("registered", {
|
|
@@ -174,7 +136,7 @@ var DevServer = class {
|
|
|
174
136
|
endpoint
|
|
175
137
|
}, {
|
|
176
138
|
F: __dxlog_file,
|
|
177
|
-
L:
|
|
139
|
+
L: 98,
|
|
178
140
|
S: this,
|
|
179
141
|
C: (f, a) => f(...a)
|
|
180
142
|
});
|
|
@@ -196,7 +158,7 @@ var DevServer = class {
|
|
|
196
158
|
registrationId: this._registrationId
|
|
197
159
|
}, {
|
|
198
160
|
F: __dxlog_file,
|
|
199
|
-
L:
|
|
161
|
+
L: 115,
|
|
200
162
|
S: this,
|
|
201
163
|
C: (f, a) => f(...a)
|
|
202
164
|
});
|
|
@@ -209,15 +171,84 @@ var DevServer = class {
|
|
|
209
171
|
this._port = void 0;
|
|
210
172
|
this._server = void 0;
|
|
211
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;
|
|
243
|
+
}
|
|
212
244
|
};
|
|
213
|
-
var __dxlog_file2 = "/home/runner/work/dxos/dxos/packages/core/functions/src/runtime/
|
|
214
|
-
var
|
|
215
|
-
constructor(_client, _manifest,
|
|
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) {
|
|
216
248
|
this._client = _client;
|
|
217
249
|
this._manifest = _manifest;
|
|
218
|
-
this.
|
|
219
|
-
this._mounts = new import_util.ComplexMap(({
|
|
220
|
-
this._queries = /* @__PURE__ */ new Set();
|
|
250
|
+
this._options = _options;
|
|
251
|
+
this._mounts = new import_util.ComplexMap(({ id, spaceKey }) => `${spaceKey.toHex()}:${id}`);
|
|
221
252
|
}
|
|
222
253
|
async start() {
|
|
223
254
|
this._client.spaces.subscribe(async (spaces) => {
|
|
@@ -230,22 +261,22 @@ var TriggerManager = class {
|
|
|
230
261
|
});
|
|
231
262
|
}
|
|
232
263
|
async stop() {
|
|
233
|
-
for (const {
|
|
234
|
-
await this.unmount(
|
|
264
|
+
for (const { id, spaceKey } of this._mounts.keys()) {
|
|
265
|
+
await this.unmount(id, spaceKey);
|
|
235
266
|
}
|
|
236
267
|
}
|
|
237
268
|
async mount(ctx, space, trigger) {
|
|
238
269
|
const key = {
|
|
239
|
-
|
|
270
|
+
id: trigger.function,
|
|
240
271
|
spaceKey: space.key
|
|
241
272
|
};
|
|
242
|
-
const
|
|
243
|
-
(0, import_invariant2.invariant)(
|
|
273
|
+
const def = this._manifest.functions.find((config) => config.id === trigger.function);
|
|
274
|
+
(0, import_invariant2.invariant)(def, `Function not found: ${trigger.function}`, {
|
|
244
275
|
F: __dxlog_file2,
|
|
245
|
-
L:
|
|
276
|
+
L: 59,
|
|
246
277
|
S: this,
|
|
247
278
|
A: [
|
|
248
|
-
"
|
|
279
|
+
"def",
|
|
249
280
|
"`Function not found: ${trigger.function}`"
|
|
250
281
|
]
|
|
251
282
|
});
|
|
@@ -260,57 +291,55 @@ var TriggerManager = class {
|
|
|
260
291
|
trigger
|
|
261
292
|
}, {
|
|
262
293
|
F: __dxlog_file2,
|
|
263
|
-
L:
|
|
294
|
+
L: 64,
|
|
264
295
|
S: this,
|
|
265
296
|
C: (f, a) => f(...a)
|
|
266
297
|
});
|
|
267
298
|
if (ctx.disposed) {
|
|
268
299
|
return;
|
|
269
300
|
}
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
301
|
+
if (trigger.schedule) {
|
|
302
|
+
const task = new import_async2.DeferredTask(ctx, async () => {
|
|
303
|
+
await this.execFunction(def, {
|
|
304
|
+
space: space.key
|
|
305
|
+
});
|
|
275
306
|
});
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
space: space.key,
|
|
288
|
-
objects: objectIds.size,
|
|
289
|
-
count
|
|
290
|
-
}, {
|
|
291
|
-
F: __dxlog_file2,
|
|
292
|
-
L: 84,
|
|
293
|
-
S: this,
|
|
294
|
-
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
|
+
});
|
|
295
318
|
});
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
subscription
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
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
|
+
}
|
|
309
338
|
}
|
|
310
339
|
}
|
|
311
|
-
async unmount(
|
|
340
|
+
async unmount(id, spaceKey) {
|
|
312
341
|
const key = {
|
|
313
|
-
|
|
342
|
+
id,
|
|
314
343
|
spaceKey
|
|
315
344
|
};
|
|
316
345
|
const { ctx } = this._mounts.get(key) ?? {};
|
|
@@ -319,50 +348,39 @@ var TriggerManager = class {
|
|
|
319
348
|
await ctx.dispose();
|
|
320
349
|
}
|
|
321
350
|
}
|
|
322
|
-
async execFunction(
|
|
323
|
-
const { endpoint } = options;
|
|
324
|
-
(0, import_invariant2.invariant)(endpoint, "Missing endpoint", {
|
|
325
|
-
F: __dxlog_file2,
|
|
326
|
-
L: 120,
|
|
327
|
-
S: this,
|
|
328
|
-
A: [
|
|
329
|
-
"endpoint",
|
|
330
|
-
"'Missing endpoint'"
|
|
331
|
-
]
|
|
332
|
-
});
|
|
351
|
+
async execFunction(def, data) {
|
|
333
352
|
try {
|
|
334
|
-
(0, import_log2.log)("
|
|
335
|
-
function:
|
|
353
|
+
(0, import_log2.log)("request", {
|
|
354
|
+
function: def.id
|
|
336
355
|
}, {
|
|
337
356
|
F: __dxlog_file2,
|
|
338
|
-
L:
|
|
357
|
+
L: 130,
|
|
339
358
|
S: this,
|
|
340
359
|
C: (f, a) => f(...a)
|
|
341
360
|
});
|
|
342
|
-
const
|
|
343
|
-
const res = await fetch(url, {
|
|
361
|
+
const response = await fetch(`${this._options.endpoint}/${def.name}`, {
|
|
344
362
|
method: "POST",
|
|
345
|
-
body: JSON.stringify(data),
|
|
346
363
|
headers: {
|
|
347
364
|
"Content-Type": "application/json"
|
|
348
|
-
}
|
|
365
|
+
},
|
|
366
|
+
body: JSON.stringify(data)
|
|
349
367
|
});
|
|
350
368
|
(0, import_log2.log)("result", {
|
|
351
|
-
function:
|
|
352
|
-
result:
|
|
369
|
+
function: def.id,
|
|
370
|
+
result: response.status
|
|
353
371
|
}, {
|
|
354
372
|
F: __dxlog_file2,
|
|
355
|
-
L:
|
|
373
|
+
L: 140,
|
|
356
374
|
S: this,
|
|
357
375
|
C: (f, a) => f(...a)
|
|
358
376
|
});
|
|
359
377
|
} catch (err) {
|
|
360
378
|
import_log2.log.error("error", {
|
|
361
|
-
function:
|
|
379
|
+
function: def.id,
|
|
362
380
|
error: err.message
|
|
363
381
|
}, {
|
|
364
382
|
F: __dxlog_file2,
|
|
365
|
-
L:
|
|
383
|
+
L: 142,
|
|
366
384
|
S: this,
|
|
367
385
|
C: (f, a) => f(...a)
|
|
368
386
|
});
|
|
@@ -372,6 +390,6 @@ var TriggerManager = class {
|
|
|
372
390
|
// Annotate the CommonJS export names for ESM import in node:
|
|
373
391
|
0 && (module.exports = {
|
|
374
392
|
DevServer,
|
|
375
|
-
|
|
393
|
+
Scheduler
|
|
376
394
|
});
|
|
377
395
|
//# sourceMappingURL=index.cjs.map
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
|
-
"sources": ["../../../src/runtime/dev-server.ts", "../../../src/runtime/
|
|
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};\n\n/**\n * Functions dev server provides a local HTTP server for testing functions.\n */\n//
|
|
5
|
-
"mappings": ";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAIA,qBAAoB;AACpB,6BAAwB;AAExB,uBAAqB;AAErB,mBAAwB;AAExB,uBAA0B;AAC1B,iBAAoB;ACRpB,IAAAA,gBAA6B;AAG7B,qBAAwB;AACxB,yBAA2C;AAC3C,IAAAC,oBAA0B;AAC1B,IAAAC,cAAoB;AACpB,kBAA2B;;;;;;;;;
|
|
6
|
-
"names": ["import_async", "import_invariant", "import_log", "DevServer", "constructor", "_client", "_options", "_handlers", "endpoint", "invariant", "_port", "proxy", "_proxy", "functions", "Object", "values", "initialize", "def", "manifest", "
|
|
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": ";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAIA,qBAAoB;AACpB,6BAAwB;AAExB,uBAAqB;AA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G,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
|
}
|
package/dist/lib/node/meta.json
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"inputs":{"packages/core/functions/src/handler.ts":{"bytes":
|
|
1
|
+
{"inputs":{"packages/core/functions/src/handler.ts":{"bytes":1313,"imports":[],"format":"esm"},"packages/core/functions/src/manifest.ts":{"bytes":1735,"imports":[],"format":"esm"},"packages/core/functions/src/runtime/dev-server.ts":{"bytes":18553,"imports":[{"path":"express","kind":"import-statement","external":true},{"path":"get-port-please","kind":"import-statement","external":true},{"path":"node:path","kind":"import-statement","external":true},{"path":"@dxos/async","kind":"import-statement","external":true},{"path":"@dxos/invariant","kind":"import-statement","external":true},{"path":"@dxos/log","kind":"import-statement","external":true},{"path":"<runtime>","kind":"import-statement","external":true}],"format":"esm"},"packages/core/functions/src/runtime/scheduler.ts":{"bytes":16641,"imports":[{"path":"cron","kind":"import-statement","external":true},{"path":"@dxos/async","kind":"import-statement","external":true},{"path":"@dxos/context","kind":"import-statement","external":true},{"path":"@dxos/echo-schema","kind":"import-statement","external":true},{"path":"@dxos/invariant","kind":"import-statement","external":true},{"path":"@dxos/log","kind":"import-statement","external":true},{"path":"@dxos/util","kind":"import-statement","external":true}],"format":"esm"},"packages/core/functions/src/runtime/index.ts":{"bytes":566,"imports":[{"path":"packages/core/functions/src/runtime/dev-server.ts","kind":"import-statement","original":"./dev-server"},{"path":"packages/core/functions/src/runtime/scheduler.ts","kind":"import-statement","original":"./scheduler"}],"format":"esm"},"packages/core/functions/src/index.ts":{"bytes":637,"imports":[{"path":"packages/core/functions/src/handler.ts","kind":"import-statement","original":"./handler"},{"path":"packages/core/functions/src/manifest.ts","kind":"import-statement","original":"./manifest"},{"path":"packages/core/functions/src/runtime/index.ts","kind":"import-statement","original":"./runtime"}],"format":"esm"}},"outputs":{"packages/core/functions/dist/lib/node/index.cjs.map":{"imports":[],"exports":[],"inputs":{},"bytes":16608},"packages/core/functions/dist/lib/node/index.cjs":{"imports":[{"path":"express","kind":"import-statement","external":true},{"path":"get-port-please","kind":"import-statement","external":true},{"path":"node:path","kind":"import-statement","external":true},{"path":"@dxos/async","kind":"import-statement","external":true},{"path":"@dxos/invariant","kind":"import-statement","external":true},{"path":"@dxos/log","kind":"import-statement","external":true},{"path":"cron","kind":"import-statement","external":true},{"path":"@dxos/async","kind":"import-statement","external":true},{"path":"@dxos/context","kind":"import-statement","external":true},{"path":"@dxos/echo-schema","kind":"import-statement","external":true},{"path":"@dxos/invariant","kind":"import-statement","external":true},{"path":"@dxos/log","kind":"import-statement","external":true},{"path":"@dxos/util","kind":"import-statement","external":true}],"exports":["DevServer","Scheduler"],"entryPoint":"packages/core/functions/src/index.ts","inputs":{"packages/core/functions/src/index.ts":{"bytesInOutput":0},"packages/core/functions/src/runtime/dev-server.ts":{"bytesInOutput":4729},"packages/core/functions/src/runtime/index.ts":{"bytesInOutput":0},"packages/core/functions/src/runtime/scheduler.ts":{"bytesInOutput":4189}},"bytes":9466}}}
|
|
@@ -1,18 +1,14 @@
|
|
|
1
1
|
import { type Client } from '@dxos/client';
|
|
2
2
|
export interface Response {
|
|
3
3
|
status(code: number): Response;
|
|
4
|
-
succeed(data?: object): Response;
|
|
5
4
|
}
|
|
6
5
|
export interface FunctionContext {
|
|
7
6
|
client: Client;
|
|
8
|
-
status(code: number): Response;
|
|
9
7
|
}
|
|
10
|
-
/**
|
|
11
|
-
* Function handler.
|
|
12
|
-
*/
|
|
13
8
|
export type FunctionHandler<T extends {}> = (params: {
|
|
14
|
-
context: FunctionContext;
|
|
15
9
|
event: T;
|
|
10
|
+
context: FunctionContext;
|
|
11
|
+
response: Response;
|
|
16
12
|
}) => Promise<Response | void>;
|
|
17
13
|
export type FunctionSubscriptionEvent = {
|
|
18
14
|
space: string;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"handler.d.ts","sourceRoot":"","sources":["../../../src/handler.ts"],"names":[],"mappings":"AAIA,OAAO,EAAE,KAAK,MAAM,EAAE,MAAM,cAAc,CAAC;
|
|
1
|
+
{"version":3,"file":"handler.d.ts","sourceRoot":"","sources":["../../../src/handler.ts"],"names":[],"mappings":"AAIA,OAAO,EAAE,KAAK,MAAM,EAAE,MAAM,cAAc,CAAC;AAG3C,MAAM,WAAW,QAAQ;IACvB,MAAM,CAAC,IAAI,EAAE,MAAM,GAAG,QAAQ,CAAC;CAChC;AAGD,MAAM,WAAW,eAAe;IAC9B,MAAM,EAAE,MAAM,CAAC;CAChB;AAID,MAAM,MAAM,eAAe,CAAC,CAAC,SAAS,EAAE,IAAI,CAAC,MAAM,EAAE;IACnD,KAAK,EAAE,CAAC,CAAC;IACT,OAAO,EAAE,eAAe,CAAC;IACzB,QAAQ,EAAE,QAAQ,CAAC;CACpB,KAAK,OAAO,CAAC,QAAQ,GAAG,IAAI,CAAC,CAAC;AAE/B,MAAM,MAAM,yBAAyB,GAAG;IACtC,KAAK,EAAE,MAAM,CAAC;IACd,OAAO,EAAE,MAAM,EAAE,CAAC;CACnB,CAAC"}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
export type FunctionDef = {
|
|
2
2
|
id: string;
|
|
3
|
-
|
|
3
|
+
name: string;
|
|
4
4
|
handler: string;
|
|
5
5
|
description?: string;
|
|
6
6
|
};
|
|
@@ -12,7 +12,8 @@ export type TriggerSubscription = {
|
|
|
12
12
|
};
|
|
13
13
|
export type FunctionTrigger = {
|
|
14
14
|
function: string;
|
|
15
|
-
|
|
15
|
+
schedule?: string;
|
|
16
|
+
subscription?: TriggerSubscription;
|
|
16
17
|
};
|
|
17
18
|
/**
|
|
18
19
|
* Function manifest file.
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"manifest.d.ts","sourceRoot":"","sources":["../../../src/manifest.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"manifest.d.ts","sourceRoot":"","sources":["../../../src/manifest.ts"],"names":[],"mappings":"AAOA,MAAM,MAAM,WAAW,GAAG;IAExB,EAAE,EAAE,MAAM,CAAC;IAEX,IAAI,EAAE,MAAM,CAAC;IAEb,OAAO,EAAE,MAAM,CAAC;IAChB,WAAW,CAAC,EAAE,MAAM,CAAC;CACtB,CAAC;AAEF,MAAM,MAAM,mBAAmB,GAAG;IAChC,IAAI,EAAE,MAAM,CAAC;IACb,QAAQ,EAAE,MAAM,CAAC;IACjB,KAAK,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,CAAC;IAC5B,MAAM,CAAC,EAAE,MAAM,EAAE,CAAC;CACnB,CAAC;AAKF,MAAM,MAAM,eAAe,GAAG;IAC5B,QAAQ,EAAE,MAAM,CAAC;IACjB,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,YAAY,CAAC,EAAE,mBAAmB,CAAC;CACpC,CAAC;AAEF;;GAEG;AACH,MAAM,MAAM,gBAAgB,GAAG;IAC7B,SAAS,EAAE,WAAW,EAAE,CAAC;IACzB,QAAQ,EAAE,eAAe,EAAE,CAAC;CAC7B,CAAC"}
|