Skip to content

Commit

Permalink
feat!: proxy bypass with WebSocket (#18070)
Browse files Browse the repository at this point in the history
  • Loading branch information
sapphi-red authored Oct 30, 2024
1 parent 437795d commit 3c9836d
Showing 1 changed file with 15 additions and 1 deletion.
16 changes: 15 additions & 1 deletion packages/vite/src/node/server/middlewares/proxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ export interface ProxyOptions extends HttpProxy.ServerOptions {
*/
bypass?: (
req: http.IncomingMessage,
res: http.ServerResponse,
/** undefined for WebSocket upgrade requests */
res: http.ServerResponse | undefined,
options: ProxyOptions,
) => void | null | undefined | false | string
/**
Expand Down Expand Up @@ -167,6 +168,19 @@ export function proxyMiddleware(
opts.target?.toString().startsWith('ws:') ||
opts.target?.toString().startsWith('wss:')
) {
if (opts.bypass) {
const bypassResult = opts.bypass(req, undefined, opts)
if (typeof bypassResult === 'string') {
req.url = bypassResult
debug?.(`bypass: ${req.url} -> ${bypassResult}`)
return
} else if (bypassResult === false) {
debug?.(`bypass: ${req.url} -> 404`)
socket.end('HTTP/1.1 404 Not Found\r\n\r\n', '')
return
}
}

if (opts.rewrite) {
req.url = opts.rewrite(url)
}
Expand Down

0 comments on commit 3c9836d

Please # to comment.