Skip to content

Commit 3c9836d

Browse files
authored
feat!: proxy bypass with WebSocket (#18070)
1 parent 437795d commit 3c9836d

File tree

1 file changed

+15
-1
lines changed
  • packages/vite/src/node/server/middlewares

1 file changed

+15
-1
lines changed

packages/vite/src/node/server/middlewares/proxy.ts

+15-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ export interface ProxyOptions extends HttpProxy.ServerOptions {
2424
*/
2525
bypass?: (
2626
req: http.IncomingMessage,
27-
res: http.ServerResponse,
27+
/** undefined for WebSocket upgrade requests */
28+
res: http.ServerResponse | undefined,
2829
options: ProxyOptions,
2930
) => void | null | undefined | false | string
3031
/**
@@ -167,6 +168,19 @@ export function proxyMiddleware(
167168
opts.target?.toString().startsWith('ws:') ||
168169
opts.target?.toString().startsWith('wss:')
169170
) {
171+
if (opts.bypass) {
172+
const bypassResult = opts.bypass(req, undefined, opts)
173+
if (typeof bypassResult === 'string') {
174+
req.url = bypassResult
175+
debug?.(`bypass: ${req.url} -> ${bypassResult}`)
176+
return
177+
} else if (bypassResult === false) {
178+
debug?.(`bypass: ${req.url} -> 404`)
179+
socket.end('HTTP/1.1 404 Not Found\r\n\r\n', '')
180+
return
181+
}
182+
}
183+
170184
if (opts.rewrite) {
171185
req.url = opts.rewrite(url)
172186
}

0 commit comments

Comments
 (0)