Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

middleware not executing after request is complete #277

Closed
iscekic opened this issue Jun 14, 2024 · 2 comments
Closed

middleware not executing after request is complete #277

iscekic opened this issue Jun 14, 2024 · 2 comments

Comments

@iscekic
Copy link

iscekic commented Jun 14, 2024

I'm facing 2 issues:

  1. After a response is sent, no other middleware afterwards is executed, even if next is called
// index.ts
router
  .get("/live", live, logRequest);

await server
  .use(router)
  .listen(env.port, () => {
    logger.info(`server listening on port ${env.port.toString()}`);
  });

// live.ts
import { MiddlewareNext, Request, Response } from "hyper-express";

const bootTime = performance.now();

export const live = (req: Request, res: Response, next: MiddlewareNext) => {
  const uptime = Math.round((performance.now() - bootTime) / 1000);

  res.json({ uptime });

  next();
};

// logRequest.ts
export const logRequest = (
  req: Request,
  res: Response,
  next: MiddlewareNext,
) => {
  console.log("this line is never reached!");

  next();
};
  1. How do I inspect the current state of the response object? e.g
const middleware1 = (req, res, next) => {
  res.setHeader("foo", "bar");
}

const middleware2 = (req, res, next) => {
  console.log(res); // headers are not inspectable anywhere
  // console.log(res.getHeaders()); // getHeaders is not a function
}
@iscekic
Copy link
Author

iscekic commented Jun 14, 2024

I've worked around the issues:

diff --git a/src/components/router/Route.js b/src/components/router/Route.js
index 97c7e12a1f52484d7dcedba9b3599126420997c2..087d86fb24c5b78842bcea6fc83880372efccb49 100644
--- a/src/components/router/Route.js
+++ b/src/components/router/Route.js
@@ -67,9 +67,6 @@ class Route {
      * @param {Number=} cursor The middleware cursor.
      */
     handle(request, response, cursor = 0) {
-        // Do not handle the request if the response has been sent aka. the request is no longer active
-        if (response.completed) return;
-
         // Retrieve the middleware for the current cursor, track the cursor if there is a valid middleware
         let iterator;
         const middleware = this.options.middlewares[cursor];

This patch makes it so the middleware chain doesn't stop when the request is completed. If this behavior is desirable in general (I don't see why not), it's probably a very easy change to make.

  1. I set the headers in res.locals - a bit cumbersome, but it did the trick

@kartikk221
Copy link
Owner

kartikk221 commented Jun 14, 2024

Hey, HyperExpress stops executing requests / middlewares If the request is disconnected or closed during traversal. This is a decision primarily made due to the expected behavior of request always being connected while execution of middlewares and also to prevent accidental bugs from calling Response methods which can no longer interact with the underlying uWS.HttpResponse object.

With that said, the patch on that line should do the trick to allow for the behavior your want. Just be careful that most Request/Response methods become no-ops under the hood once a response is completed and some may still cause an error If they somehow end up trying to call the underlying uWS.HttpResponse instance.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants