From 66911677f0da32cc5455273a635b2bee21b05140 Mon Sep 17 00:00:00 2001 From: "HRG @ SCExC" Date: Sun, 12 Jan 2025 10:16:51 -0500 Subject: [PATCH] feat: support async handlers --- index.js | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/index.js b/index.js index 5058516..f71230e 100644 --- a/index.js +++ b/index.js @@ -730,7 +730,7 @@ function embedHTML(url) { return `
`; } -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);