From 891566ae4fe1ae2455590f55d2d945378dc79b2a Mon Sep 17 00:00:00 2001 From: lhokktyn <810615+lhokktyn@users.noreply.github.com> Date: Sat, 5 Oct 2024 09:57:50 +0100 Subject: [PATCH] fix: support async afterEach hook Currently a asynchronous afterEach hook will execute concurrently with the underlying express request lifecycle. This fix allows async hooks to complete before allowing the express request to continue through its middleware stack. --- src/dsl/verifier/proxy/hooks.ts | 22 ++++++++++------------ 1 file changed, 10 insertions(+), 12 deletions(-) diff --git a/src/dsl/verifier/proxy/hooks.ts b/src/dsl/verifier/proxy/hooks.ts index 8c434cf46..dd4a1024c 100644 --- a/src/dsl/verifier/proxy/hooks.ts +++ b/src/dsl/verifier/proxy/hooks.ts @@ -31,19 +31,17 @@ export const registerAfterHook = ( config: ProxyOptions, stateSetupPath: string ): void => { + logger.trace("registered 'afterEach' hook"); app.use(async (req, res, next) => { - if (config.afterEach !== undefined) { - logger.trace("registered 'afterEach' hook"); - next(); - if (req.path !== stateSetupPath) { - logger.debug("executing 'afterEach' hook"); - try { - await config.afterEach(); - } catch (e) { - logger.error(`error executing 'afterEach' hook: ${e.message}`); - logger.debug(`Stack trace was: ${e.stack}`); - next(new Error(`error executing 'afterEach' hook: ${e.message}`)); - } + if (req.path !== stateSetupPath && config.afterEach) { + logger.debug("executing 'afterEach' hook"); + try { + await config.afterEach(); + next(); + } catch (e) { + logger.error(`error executing 'afterEach' hook: ${e.message}`); + logger.debug(`Stack trace was: ${e.stack}`); + next(new Error(`error executing 'afterEach' hook: ${e.message}`)); } } else { next();