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

Handle async pathRewrite function #374

Closed
WestonThayer opened this issue Nov 4, 2019 · 4 comments
Closed

Handle async pathRewrite function #374

WestonThayer opened this issue Nov 4, 2019 · 4 comments

Comments

@WestonThayer
Copy link

Is this a feature request?

Yes. I would like a pathRewrite function that returns a Promise to be handled. Currently it can only return a string.

My use case is: when a request for /api/foo?auth_token=xxx arrives, I need to:

  1. Grab auth_token and check its validity. This is an async operation
  2. If it is valid, use the token to look up some additional information that token has permission to access in a database (also an async operation)
  3. Assuming all goes well, return a rewritten path, like /other-api/bar?db-field-1=abc&db-field-2=abc. If it did not go well, the path will be rewritten to /other-api/404

Similar to #318, but this covers the case where more than just the response headers/status needs to be modified.

I have this working and tested in a local fork, please let me know if you would accept a PR.

@sysoft
Copy link

sysoft commented Nov 11, 2019

same requestion!

@WestonThayer
Copy link
Author

Workaround: I ended up just using the underlying http-proxy lib.

const http = require("http");
const httpProxy = require("http-proxy");
const url = require("url");

const proxy = httpProxy.createProxyServer({ target: "http://example.com" });
const server = http.createServer( /* handle HTTP requests */ );

server.on("upgrade", async (req, socket, head) => {
  const parsedUrl = url.parse(req.url, true /* also parse query params */);

  if (parsedUrl.pathname === "/api/foo") {
    // this should be in a try/catch for any situation in which the Promise could
    // reject
    const usr = await someAuthService.validate(parsedUrl.query.auth_token);
    const someVar = await someDbService.getField1(usr);
    const otherVar = await someDbService.getField2(usr);

    // Now do the path rewrite
    req.url = `/other-api/bar?db-field-1=${someVar}&db-field2=${otherVar}`;

    // let request through
    proxy.ws(req, socket, head);
  } else {
    // Invalid path, close the WebSocket
    socket.destroy();
    // Alternatively, could choose some other path rewrite, like:
    // req.url = "/other-api/404";
    // proxy.ws(req, socket, head);
  }
});

server.listen(3000);

@rsethc
Copy link
Contributor

rsethc commented Feb 13, 2020

I also had a need for this (in my case, the path rewriting behavior is dependent on its own HTTP request, so async is needed). As such I have created a pull request with the fix.

@chimurai
Copy link
Owner

published http-proxy-middleware@0.21.0 with async pathRewrite

Thanks @rsethc !

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

4 participants