Skip to content

Support async proxy rewriting #7534

@snoozbuster

Description

@snoozbuster

Clear and concise description of the problem

In vite's ProxyOptions, rewrite is defined as a simple string => string transformer. However, http-proxy-middleware (which is what I am assuming inspired these options) added the ability for its pathRewrite function to be async in 0.21.0.

In my application, I use this method to test when a local backend service is running (via tcp-port-used, which is async) and automatically transform the path into a shape which is agreeable to the local service. (another piece of code which is implemented on top of HPM's router option performs the host rewrite).

Suggested solution

Since vite passes the entire URL to this function, I believe that both pieces (host + path rewrite) could be implemented on top of this one function if it were made async. Additionally, given that the two places this is called (1, 2) look like they are express middlewares, I see no obvious reason why these couldn't be easily made async.

Happy to open a quick PR for this if the feature is agreeable. It should be backwards compatible as the signature can be changed to rewrite: (string) => string | Promise<string> which is an awaitable signature. This blocks me from upgrading my applications to vite as our full stack developers make heavy use of this automatic proxying functionality and I'm not keen to take it away from them (everything else we have otherwise implemented on top of HPM appears to be implementable directly on top of http-proxy other than this piece).

Alternative

No response

Additional context

No response

Validations

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions