From 496821f00a58c11be3d90d05c1a14cd320989358 Mon Sep 17 00:00:00 2001 From: Philip H <47042125+pheiduck@users.noreply.github.com> Date: Fri, 26 Jan 2024 17:40:37 +0100 Subject: [PATCH] fixup: koa --- src/lib/Server.js | 3 +-- src/lib/Util.js | 35 +++++++++++++++++++++++++++++++++++ 2 files changed, 36 insertions(+), 2 deletions(-) diff --git a/src/lib/Server.js b/src/lib/Server.js index 39198532..8f46de8f 100644 --- a/src/lib/Server.js +++ b/src/lib/Server.js @@ -70,8 +70,7 @@ module.exports = class Server { } ctx.session.authenticated = true; - - ctx.status = 204; + ctx.session.save(); debug(`New Session: ${ctx.session.id}`); }) diff --git a/src/lib/Util.js b/src/lib/Util.js index a39d7769..cc6e89c2 100644 --- a/src/lib/Util.js +++ b/src/lib/Util.js @@ -17,6 +17,41 @@ module.exports = class Util { return true; } + static promisify(fn) { + // eslint-disable-next-line func-names + return function(req, res) { + Promise.resolve().then(async () => fn(req, res)) + .then((result) => { + if (res.headersSent) return; + + if (typeof result === 'undefined') { + return res + .status(204) + .end(); + } + + return res + .status(200) + .json(result); + }) + .catch((error) => { + if (typeof error === 'string') { + error = new Error(error); + } + + // eslint-disable-next-line no-console + console.error(error); + + return res + .status(error.statusCode || 500) + .json({ + error: error.message || error.toString(), + stack: error.stack, + }); + }); + }; + } + static async exec(cmd, { log = true, } = {}) {