Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: server.proxy ws error causes crash #9123

Merged
merged 1 commit into from Jul 14, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
32 changes: 23 additions & 9 deletions packages/vite/src/node/server/middlewares/proxy.ts
@@ -1,4 +1,5 @@
import type * as http from 'node:http'
import type * as net from 'node:net'
import httpProxy from 'http-proxy'
import type { Connect } from 'types/connect'
import type { HttpProxy } from 'types/http-proxy'
Expand Down Expand Up @@ -43,16 +44,29 @@ export function proxyMiddleware(
}
const proxy = httpProxy.createProxyServer(opts) as HttpProxy.Server

proxy.on('error', (err, req, res) => {
config.logger.error(`${colors.red(`http proxy error:`)}\n${err.stack}`, {
timestamp: true,
error: err
})
res
.writeHead(500, {
'Content-Type': 'text/plain'
proxy.on('error', (err, req, originalRes) => {
// When it is ws proxy, res is net.Socket
const res = originalRes as http.ServerResponse | net.Socket
if ('req' in res) {
config.logger.error(
`${colors.red(`http proxy error:`)}\n${err.stack}`,
{
timestamp: true,
error: err
}
)
res
.writeHead(500, {
'Content-Type': 'text/plain'
})
.end()
} else {
config.logger.error(`${colors.red(`ws proxy error:`)}\n${err.stack}`, {
timestamp: true,
error: err
})
.end()
res.end()
}
})

if (opts.configure) {
Expand Down