forked from yarnpkg/yarn
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(request): add noProxy setting for configuration (yarnpkg#5048)
- Loading branch information
1 parent
63c56c7
commit 202f1f5
Showing
4 changed files
with
236 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
/* @flow */ | ||
|
||
import {URL} from 'url'; | ||
|
||
type Zone = { | ||
hostname: string, | ||
port: number, | ||
hasPort: boolean, | ||
}; | ||
|
||
function formatHostname(hostname: string): string { | ||
// canonicalize the hostname, so that 'oogle.com' won't match 'google.com' | ||
return hostname.replace(/^\.*/, '.').toLowerCase(); | ||
} | ||
|
||
function parseNoProxyZone(zone: string): Zone { | ||
zone = zone.trim().toLowerCase(); | ||
|
||
const zoneParts = zone.split(':', 2); | ||
const hostname = formatHostname(zoneParts[0]); | ||
const port: number = (zoneParts[1]: any); | ||
const hasPort = zone.indexOf(':') > -1; | ||
|
||
return { | ||
hostname, | ||
port, | ||
hasPort, | ||
}; | ||
} | ||
|
||
export default function isExcludedFromProxy(url: string, noProxy: string): boolean { | ||
if (!noProxy) { | ||
return false; | ||
} | ||
|
||
if (noProxy === '*') { | ||
return true; | ||
} | ||
|
||
const uri = new URL(url); | ||
|
||
const port = uri.port || (uri.protocol === 'https:' ? '443' : '80'); | ||
const hostname = formatHostname(uri.hostname); | ||
const noProxyList = noProxy.split(','); | ||
|
||
// iterate through the noProxyList until it finds a match. | ||
return noProxyList.map(parseNoProxyZone).some(noProxyZone => { | ||
const isMatchedAt = hostname.indexOf(noProxyZone.hostname); | ||
const hostnameMatched = isMatchedAt > -1 && isMatchedAt === hostname.length - noProxyZone.hostname.length; | ||
|
||
if (noProxyZone.hasPort) { | ||
return port === noProxyZone.port && hostnameMatched; | ||
} | ||
|
||
return hostnameMatched; | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters