Skip to content

Commit

Permalink
refactor: descriptive name & arg order for testing proxy ctx against url
Browse files Browse the repository at this point in the history
  • Loading branch information
mikeplus64 committed Sep 10, 2021
1 parent 707fb9d commit 437457d
Showing 1 changed file with 5 additions and 5 deletions.
10 changes: 5 additions & 5 deletions packages/vite/src/node/server/middlewares/proxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ export function proxyMiddleware(
httpServer.on('upgrade', (req, socket, head) => {
const url = req.url!
for (const context in proxies) {
if (testUrl(url, context)) {
if (doesProxyContextMatchUrl(context, url)) {
const [proxy, opts] = proxies[context]
if (
(opts.ws || opts.target?.toString().startsWith('ws:')) &&
Expand All @@ -84,7 +84,7 @@ export function proxyMiddleware(
return function viteProxyMiddleware(req, res, next) {
const url = req.url!
for (const context in proxies) {
if (testUrl(url, context)) {
if (doesProxyContextMatchUrl(context, url)) {
const [proxy, opts] = proxies[context]
const options: HttpProxy.ServerOptions = {}

Expand Down Expand Up @@ -116,9 +116,9 @@ export function proxyMiddleware(
}
}

function testUrl(url: string, target: string) {
function doesProxyContextMatchUrl(context: string, url: string): boolean {
return (
(target.startsWith('^') && new RegExp(target).test(url)) ||
url.startsWith(target)
(context.startsWith('^') && new RegExp(context).test(url)) ||
url.startsWith(context)
)
}

0 comments on commit 437457d

Please sign in to comment.