Skip to content

Commit dbdd2ed

Browse files
committed
Fix proxy error on web sockets
Fixes #6088.
1 parent a9d61da commit dbdd2ed

File tree

1 file changed

+12
-2
lines changed

1 file changed

+12
-2
lines changed

src/node/proxy.ts

+12-2
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,19 @@ import { HttpCode } from "../common/http"
33

44
export const proxy = proxyServer.createProxyServer({})
55

6+
// The error handler catches when the proxy fails to connect (for example when
7+
// there is nothing running on the target port).
68
proxy.on("error", (error, _, res) => {
7-
res.writeHead(HttpCode.ServerError)
8-
res.end(error.message)
9+
// This could be for either a web socket or a regular request. Despite what
10+
// the types say, writeHead() will not exist on web socket requests (nor will
11+
// status() from Express). But writing out the code manually does not work
12+
// for regular requests thus the branching behavior.
13+
if (typeof res.writeHead !== "undefined") {
14+
res.writeHead(HttpCode.ServerError)
15+
res.end(error.message)
16+
} else {
17+
res.end(`HTTP/1.1 ${HttpCode.ServerError} ${error.message}\r\n\r\n`)
18+
}
919
})
1020

1121
// Intercept the response to rewrite absolute redirects against the base path.

0 commit comments

Comments
 (0)