Skip to content

Commit

Permalink
fix: server.proxy ws error causes crash (#9123)
Browse files Browse the repository at this point in the history
  • Loading branch information
sapphi-red authored Jul 14, 2022
1 parent d12d469 commit c2426d1
Showing 1 changed file with 23 additions and 9 deletions.
32 changes: 23 additions & 9 deletions packages/vite/src/node/server/middlewares/proxy.ts
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit c2426d1

Please sign in to comment.