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: support async for proxy.bypass #18940

Merged
merged 7 commits into from
Jan 23, 2025
Merged
Changes from 1 commit
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
30 changes: 19 additions & 11 deletions packages/vite/src/node/server/middlewares/proxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,13 @@ export interface ProxyOptions extends HttpProxy.ServerOptions {
/** undefined for WebSocket upgrade requests */
res: http.ServerResponse | undefined,
options: ProxyOptions,
) => void | null | undefined | false | string
) =>
| void
| null
| undefined
| false
| string
| Promise<void | null | undefined | false | string>
/**
* rewrite the Origin header of a WebSocket request to match the target
*
Expand Down Expand Up @@ -158,7 +164,7 @@ export function proxyMiddleware(
})

if (httpServer) {
httpServer.on('upgrade', (req, socket, head) => {
httpServer.on('upgrade', async (req, socket, head) => {
const url = req.url!
for (const context in proxies) {
if (doesProxyContextMatchUrl(context, url)) {
Expand All @@ -169,16 +175,16 @@ export function proxyMiddleware(
opts.target?.toString().startsWith('wss:')
) {
if (opts.bypass) {
const bypassResult = opts.bypass(req, undefined, opts)
const bypassResult = await opts.bypass(req, undefined, opts)
if (typeof bypassResult === 'string') {
req.url = bypassResult
debug?.(`bypass: ${req.url} -> ${bypassResult}`)
shulaoda marked this conversation as resolved.
Show resolved Hide resolved
return
} else if (bypassResult === false) {
}
if (bypassResult === false) {
debug?.(`bypass: ${req.url} -> 404`)
socket.end('HTTP/1.1 404 Not Found\r\n\r\n', '')
return
}
return
shulaoda marked this conversation as resolved.
Show resolved Hide resolved
}

if (opts.rewrite) {
Expand All @@ -194,24 +200,26 @@ export function proxyMiddleware(
}

// Keep the named function. The name is visible in debug logs via `DEBUG=connect:dispatcher ...`
return function viteProxyMiddleware(req, res, next) {
return async function viteProxyMiddleware(req, res, next) {
shulaoda marked this conversation as resolved.
Show resolved Hide resolved
const url = req.url!
for (const context in proxies) {
if (doesProxyContextMatchUrl(context, url)) {
const [proxy, opts] = proxies[context]
const options: HttpProxy.ServerOptions = {}

if (opts.bypass) {
const bypassResult = opts.bypass(req, res, opts)
const bypassResult = await opts.bypass(req, res, opts)
shulaoda marked this conversation as resolved.
Show resolved Hide resolved
if (typeof bypassResult === 'string') {
req.url = bypassResult
debug?.(`bypass: ${req.url} -> ${bypassResult}`)
shulaoda marked this conversation as resolved.
Show resolved Hide resolved
return next()
} else if (bypassResult === false) {
next()
}
if (bypassResult === false) {
debug?.(`bypass: ${req.url} -> 404`)
res.statusCode = 404
return res.end()
res.end()
}
return
}

debug?.(`${req.url} -> ${opts.target || opts.forward}`)
Expand Down
Loading