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

Async middleware not working correctly #36

Open
filipduch opened this issue Nov 9, 2023 · 2 comments
Open

Async middleware not working correctly #36

filipduch opened this issue Nov 9, 2023 · 2 comments

Comments

@filipduch
Copy link

When using async middleware it starts the execution and jumps right back to the next one before it finishes.

import server from "bunrest";
const app = server();
const port = 3000;

const asyncMiddleware = async (req, res, next) => {
  console.log(new Date(), "start");
  await Bun.sleep(2000);
  console.log(new Date(), "end");
  res.status(429).send("Too many requests");
};

app.get("/user", asyncMiddleware, (req, res) => {
  console.log(new Date(), "user endpoint");
  res.status(200).json({ message: "ok" });
});

app.listen(port, () => {
  console.log(`App is listening on port ${port}`);
});`

Output:

App is listening on port 3000
2023-11-09T18:54:44.563Z start
2023-11-09T18:54:44.563Z user endpoint
2023-11-09T18:54:46.563Z end
284 |         if (
285 |           typeof target[prop] === "function" &&
286 |           (prop === "json" || prop === "send") &&
287 |           target.isReady()
288 |         ) {
289 |           throw new Error("You cannot send response twice");
                    ^
error: You cannot send response twice
      at get (.../node_modules/bunrest/src/server/server.ts:289:16)

Expected output (processing should end when middleware sends 429):

App is listening on port 3000
2023-11-09T18:54:44.563Z start
2023-11-09T18:54:46.563Z end

Other Relevant Info:
Bun version: v1.0.2
bunrest version: v1.3.7
platform: ubuntu 20.04
node installed: yes
node version: v18.12.0

@Benjythebee
Copy link

Benjythebee commented Nov 22, 2023

A similar problem occurs with app.use and synchronous methods;

In Express, the order of middlewares is respected, but it isn't in Bunrest.
See this code:

import server from "bunrest";
const app = server();
const port = 3000;

app.get("/notuser", (req, res) => {
    console.log(new Date(), "notuser endpoint");
    res.status(200).json({ message: "ok" });
  });
  

app.use((req:any, res:any, next:any) => {
    console.log(new Date(), "This is a /user route - middleware");
    next();
})

app.get("/user", (req, res) => {
  console.log(new Date(), "user endpoint");
  res.status(200).json({ message: "ok" });
});

app.listen(port, () => {
  console.log(`App is listening on port ${port}`);
});

In express, if I query /notuser, the console log of the app.use will not run; but in bunrest I get
image

Which means that somewhere in the code there is a logic that probably says if app.use, bump execution to the top which is invalid;
EDIT: Seems like these lines of code are causing the issue I'm talking about

        this.middlewares.push({
          path: "*",
          middlewareFunc: arg1 as Handler,
        });

The middleware will be applied to all routes, when it should be applied to all routes after all X middlewares before it

@nielsvantslot
Copy link
Contributor

@filipduch @Benjythebee I think the issue lies in the way middleware is executed in:
chain.ts

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

3 participants