-
Notifications
You must be signed in to change notification settings - Fork 92
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1067 from farawaysouthwest/mitch/add-proxy-npm-in…
…staller Add HTTP Proxy support to NPM Binary Installer
- Loading branch information
1 parent
8170f75
commit f4f8277
Showing
2 changed files
with
105 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
/** | ||
* | ||
* NoProxy logic derived from Request project: | ||
* https://github.com/request/request/blob/3c0cddc7c8eb60b470e9519da85896ed7ee0081e/lib/getProxyFromURI.js | ||
* | ||
*/ | ||
|
||
const formatHostName = (hostname) => | ||
hostname.replace(/^\.*/, ".").toLowerCase(); | ||
|
||
const parseNoProxyZone = (zone) => { | ||
zone = zone.trim(); | ||
|
||
const zoneParts = zone.split(":", 2); | ||
const zoneHost = formatHostName(zoneParts[0]); | ||
const zonePort = zoneParts[1]; | ||
const hasPort = zone.indexOf(":") > -1; | ||
|
||
return { hostname: zoneHost, port: zonePort, hasPort: hasPort }; | ||
}; | ||
|
||
const urlInNoProxy = (requestURL, noProxy) => { | ||
const port = | ||
requestURL.port || (requestURL.protocol === "https:" ? "443" : "80"); | ||
|
||
// clean hostname | ||
const hostname = formatHostName(requestURL.hostname); | ||
|
||
// convert to array | ||
const noProxyList = noProxy.split(","); | ||
|
||
// iterate over noProxyList and find match with RequestURL | ||
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; | ||
}); | ||
}; | ||
|
||
const getProxyEnv = (requestURL) => { | ||
const noProxy = process.env.NO_PROXY || process.env.no_proxy || ""; | ||
|
||
// if the noProxy is a wildcard then return null | ||
if (noProxy === "*") { | ||
return null; | ||
} | ||
|
||
// if the noProxy is not empty and the uri is found, return null | ||
if (noProxy !== "" && urlInNoProxy(requestURL, noProxy)) { | ||
return null; | ||
} | ||
|
||
// get proxy based on request url's protocol | ||
if (requestURL.protocol == "http:") { | ||
return process.env.HTTP_PROXY || process.env.http_proxy || null; | ||
} | ||
|
||
if (requestURL.protocol == "https:") { | ||
return process.env.HTTPS_PROXY || process.env.https_proxy || null; | ||
} | ||
|
||
// not a supported protocol... | ||
return null; | ||
}; | ||
|
||
const configureProxy = (requestURL) => { | ||
const url = new URL(requestURL); | ||
const env = getProxyEnv(url); | ||
|
||
// short circuit if null | ||
if (!env) return null; | ||
|
||
// parse proxy url | ||
const { hostname, port, protocol, username, password } = new URL(env); | ||
|
||
// return proxy object for axios request | ||
return { | ||
proxy: { | ||
protocol, | ||
hostname, | ||
port, | ||
auth: { | ||
username, | ||
password, | ||
}, | ||
}, | ||
}; | ||
}; | ||
|
||
module.exports = { | ||
configureProxy, | ||
}; |