feat: support async handlers

This commit is contained in:
HRG @ SCExC 2025-01-12 10:16:51 -05:00
parent 9d625b0fd5
commit 66911677f0

View file

@ -730,7 +730,7 @@ function embedHTML(url) {
return `<div hx-get="${url}" hx-trigger="load"></div>`;
}
app.all("*", (req, res) => {
app.all("*", async (req, res) => {
const req_path = req.path;
const verb = req.method;
@ -779,10 +779,11 @@ app.all("*", (req, res) => {
),
);
};
return vm.runInContext(
(route.handler += `\n\nhandler(req, res)`),
context,
);
const handlerScript = new vm.Script(route.handler, { filename: `handler_${route.id}.js` });
// Prepare the execution code that invokes `handler(req, res)` and awaits it
const executionScript = new vm.Script(`handler(req, res); `, { filename: `execution_${route.id}.js` });
await handlerScript.runInContext(context);
const result = await executionScript.runInContext(context);
} catch (e) {
const error = e.stack ? `${e}\n\n${e.stack}` : `${e}`;
model.createLog(db, structure.id, route.id, error, true);