Skip to content

Commit

Permalink
fix: support async afterEach hook
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
lhokktyn committed Oct 5, 2024
1 parent 1a3815e commit 891566a
Showing 1 changed file with 10 additions and 12 deletions.
22 changes: 10 additions & 12 deletions src/dsl/verifier/proxy/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down

0 comments on commit 891566a

Please sign in to comment.