From 8c7cef6cb73633dbc68996bf4ba4b0e7613682b6 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 1/2] 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. Fixes #1241 --- 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(); From d368c802129494b8a5895f91926d8c875f183326 Mon Sep 17 00:00:00 2001 From: lhokktyn <810615+lhokktyn@users.noreply.github.com> Date: Sat, 5 Oct 2024 10:10:04 +0100 Subject: [PATCH 2/2] refactor: align before/afterEach function design --- src/dsl/verifier/proxy/hooks.ts | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/src/dsl/verifier/proxy/hooks.ts b/src/dsl/verifier/proxy/hooks.ts index dd4a1024c..6a4d81c59 100644 --- a/src/dsl/verifier/proxy/hooks.ts +++ b/src/dsl/verifier/proxy/hooks.ts @@ -8,21 +8,21 @@ export const registerBeforeHook = ( config: ProxyOptions, stateSetupPath: string ): void => { + if (config.beforeEach) logger.trace("registered 'beforeEach' hook"); app.use(async (req, res, next) => { - if (config.beforeEach !== undefined) { - logger.trace("registered 'beforeEach' hook"); - if (req.path === stateSetupPath) { - logger.debug("executing 'beforeEach' hook"); - try { - await config.beforeEach(); - } catch (e) { - logger.error(`error executing 'beforeEach' hook: ${e.message}`); - logger.debug(`Stack trace was: ${e.stack}`); - next(new Error(`error executing 'beforeEach' hook: ${e.message}`)); - } + if (req.path === stateSetupPath && config.beforeEach) { + logger.debug("executing 'beforeEach' hook"); + try { + await config.beforeEach(); + next(); + } catch (e) { + logger.error(`error executing 'beforeEach' hook: ${e.message}`); + logger.debug(`Stack trace was: ${e.stack}`); + next(new Error(`error executing 'beforeEach' hook: ${e.message}`)); } + } else { + next(); } - next(); }); }; @@ -31,7 +31,7 @@ export const registerAfterHook = ( config: ProxyOptions, stateSetupPath: string ): void => { - logger.trace("registered 'afterEach' hook"); + if (config.afterEach) logger.trace("registered 'afterEach' hook"); app.use(async (req, res, next) => { if (req.path !== stateSetupPath && config.afterEach) { logger.debug("executing 'afterEach' hook");