oven-sh / bun

Incredibly fast JavaScript runtime, bundler, test runner, and package manager – all in one
https://bun.sh
Other
73.28k stars 2.69k forks source link

Proxying WebSockets with node-http-proxy (from Vite) doesn't work, works with Node #10441

Open OlegWock opened 5 months ago

OlegWock commented 5 months ago

What version of Bun is running?

1.1.4+fbe2fe0c3

What platform is your computer?

Darwin 23.4.0 arm64 arm

What steps can reproduce the bug?

  1. Create new project with Vite
  2. Setup proxying in Vite config
export default defineConfig({
  root: import.meta.dir,
  server: {
    port: 8441,
    proxy: {
      '/mirror': {
        target: 'ws://ws.vi-server.org',
        changeOrigin: true,
        ws: true
      },
    }
  }
});
  1. Run Vite using bun bunx --bun vite
  2. Try connecting to the endpoint (for example with websocat):
websocat ws://localhost:8441/mirror

What is the expected behavior?

WebSocket proxy connection is established, you can send messages (enter text and press enter in websocat) and get response. This is how it works with node.

What do you see instead?

WebSocket between client and proxy is established, but connection between proxy and server fails.

Additional information

There seem to be some differencies how http works in Node and Bun. I was able to pinpoint the issue likely to these lines:

https://github.com/http-party/node-http-proxy/blob/9b96cd725127a024dabebec6c7ea8c807272223d/lib/http-proxy/passes/ws-incoming.js#L112-L120

On Node:

On Bun:

Package node-http-proxy isn't maintained, but used by Vite. Also, res.upgrade seem to be error as there is no such property on Node docs. But in the end it's difference in events order that breaks proxying

lukeed commented 1 month ago

This is still an issue. Any server.proxy configuration within Vite config does not work in bun. Currently have:

server: {
    proxy: {
      "/api": {
        ws: true,
        target: `http://localhost:${PORT}`,
      },
    },
  },

Adding changeOrigin or rewriteWsOrigin doesnt help, nor does a complex configure() method help since the response is already sent.

The problem is likely within node-http-proxy but IMO this issue should exist here for bun's node compat.

mrchoke commented 3 weeks ago

This issue persists, preventing me from migrating to the Bun environment because I still need to use a dev proxy that enables WebSocket.