http-proxy-middleware icon indicating copy to clipboard operation
http-proxy-middleware copied to clipboard

Server Crashed after [HPM] Upgrading to WebSocket socket error

Open whatwewant opened this issue 3 years ago • 7 comments
trafficstars

Checks

Describe the bug (be clear and concise)

10531652080005_ pic

Step-by-step reproduction instructions

1. Proxy Websocket Broken

Expected behavior (be clear and concise)

Server not crashed, just log errors

How is http-proxy-middleware used in your project?

const { createProxyMiddleware } = require('http-proxy-middleware');

...
createProxyMiddleware({
  target: `http://127.0.0.1:${this.port}/`,
  changeOrigin: true,
  ws: true,
  router: (req) => {
    const url = req.url;
    if (url.startsWith('/xxx')) {
      return null;
    }
    const result = this.router(url, this.url, this.port);
    if (result) {
      return result;
    }
    if (url.startsWith('/v1')) {
      return this.url;
    }
    return `http://127.0.0.1:${this.port}/`;
  },
})
...


### What http-proxy-middleware configuration are you using?

```typescript
{
  target: `http://127.0.0.1:${this.port}/`,
  changeOrigin: true,
  ws: true,
  router: (req) => {
    const url = req.url;
    if (url.startsWith('/xxx')) {
      return null;
    }
    const result = this.router(url, this.url, this.port);
    if (result) {
      return result;
    }
    if (url.startsWith('/v1')) {
      return this.url;
    }
    return `http://127.0.0.1:${this.port}/`;
  },
}


### What OS/version and node/version are you seeing the problem?

```shell
Platform: darwin
Architecture: arm64
Kernal: 21.3.0
Node: v16.14.2
V8: 9.4.146.24-node.20
NPM: 6.14.16
Yarn: 1.22.18

Additional context (optional)

No response

whatwewant avatar May 09 '22 07:05 whatwewant

related: https://github.com/chimurai/http-proxy-middleware/issues/733

whatwewant avatar May 09 '22 07:05 whatwewant

https://github.com/chimurai/http-proxy-middleware/blob/3b9730826187c708fbd16cb0baa588f6aad73a00/src/http-proxy-middleware.ts#L74

@chimurai node-http-proxy socket will throw error if no error listener for default eventemitter

  1. https://github.com/http-party/node-http-proxy/blob/9b96cd725127a024dabebec6c7ea8c807272223d/lib/http-proxy/passes/ws-incoming.js#L121
  2. https://github.com/http-party/node-http-proxy/blob/9b96cd725127a024dabebec6c7ea8c807272223d/lib/http-proxy/passes/ws-incoming.js#L153
  3. https://github.com/http-party/node-http-proxy/blob/9b96cd725127a024dabebec6c7ea8c807272223d/lib/http-proxy/index.js#L38

whatwewant avatar May 09 '22 07:05 whatwewant

how to: just

this.proxy.ws(req, socket, head, activeProxyOptions, this.logError); 

whatwewant avatar May 09 '22 07:05 whatwewant

found latest source code, should change this.logError

whatwewant avatar May 09 '22 07:05 whatwewant

Hi. Thanks for reporting.

Could you try with http-proxy-middleware@beta? (v3.0.0-beta.0)

V3 registered some additional error handlers to catch the uncaught errors. (https://github.com/chimurai/http-proxy-middleware/blob/master/src/plugins/default/debug-proxy-errors-plugin.ts)

Hopefully it's already fixed in V3

chimurai avatar May 09 '22 18:05 chimurai

Hi. Thanks for reporting.

Could you try with http-proxy-middleware@beta? (v3.0.0-beta.0)

V3 registered some additional error handlers to catch the uncaught errors. (https://github.com/chimurai/http-proxy-middleware/blob/master/src/plugins/default/debug-proxy-errors-plugin.ts)

Hopefully it's already fixed in V3

thanks for reply, i will try it later

whatwewant avatar May 16 '22 05:05 whatwewant

It is coming in beta too

shekharramola avatar Nov 08 '22 10:11 shekharramola