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

feat!: proxy bypass with WebSocket #18070

Merged
merged 2 commits into from
Oct 30, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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