Fix proxy error on web sockets

Fixes #6088.
This commit is contained in:
Asher 2023-03-21 11:10:58 -08:00
parent a9d61daa91
commit dbdd2edb62
No known key found for this signature in database
GPG Key ID: D63C1EF81242354A
1 changed files with 12 additions and 2 deletions

View File

@ -3,9 +3,19 @@ import { HttpCode } from "../common/http"
export const proxy = proxyServer.createProxyServer({}) export const proxy = proxyServer.createProxyServer({})
// The error handler catches when the proxy fails to connect (for example when
// there is nothing running on the target port).
proxy.on("error", (error, _, res) => { proxy.on("error", (error, _, res) => {
res.writeHead(HttpCode.ServerError) // This could be for either a web socket or a regular request. Despite what
res.end(error.message) // the types say, writeHead() will not exist on web socket requests (nor will
// status() from Express). But writing out the code manually does not work
// for regular requests thus the branching behavior.
if (typeof res.writeHead !== "undefined") {
res.writeHead(HttpCode.ServerError)
res.end(error.message)
} else {
res.end(`HTTP/1.1 ${HttpCode.ServerError} ${error.message}\r\n\r\n`)
}
}) })
// Intercept the response to rewrite absolute redirects against the base path. // Intercept the response to rewrite absolute redirects against the base path.