Skip to content

Commit 960e029

Browse files
committed
add rudimentary noproxy support
1 parent 8f35b40 commit 960e029

File tree

2 files changed

+41
-1
lines changed

2 files changed

+41
-1
lines changed

lib/install.js

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ const makeDir = require('make-dir');
1414
// for fetching binaries
1515
const fetch = require('node-fetch');
1616
const tar = require('tar');
17+
const url = require('url');
1718

1819
let npgVersion = 'unknown';
1920
try {
@@ -24,6 +25,34 @@ try {
2425
// do nothing
2526
}
2627

28+
function should_proxy(uri, noProxyList) {
29+
const { hostname, port } = url.parse(uri);
30+
31+
/* TODO: Default ports as defined by the protocol should still
32+
match, they currently will not if the noProxyList entry
33+
explicitly specifies the default port. */
34+
35+
// eslint-disable-next-line eqeqeq
36+
const portMatch = (entry) => entry.port === undefined || entry.port == port;
37+
38+
for (let i = 0; i < noProxyList && noProxyList.length; i++) {
39+
const entry = noProxyList[i];
40+
if (!portMatch(entry)) {
41+
continue;
42+
}
43+
// TODO: Deal with 'partial wildcards': *.foo.com
44+
if (entry.hostSuffix === '*') {
45+
return false;
46+
}
47+
const rightSideMatch = hostname.indexOf(entry.hostSuffix, hostname.length, entry.hostSuffix.length);
48+
if (rightSideMatch > -1) {
49+
return false;
50+
}
51+
}
52+
53+
return true;
54+
}
55+
2756
function place_binary(uri, targetDir, opts, callback) {
2857
log.http('GET', uri);
2958

@@ -54,8 +83,18 @@ function place_binary(uri, targetDir, opts, callback) {
5483
process.env.http_proxy ||
5584
process.env.HTTP_PROXY ||
5685
process.env.npm_config_proxy;
86+
const noProxy = opts.noproxy ||
87+
process.env.no_proxy ||
88+
process.env.NO_PROXY ||
89+
process.env.npm_config_noproxy;
90+
// TODO: IPv6 address + port support.
91+
const noProxyList = noProxy && noProxy.split(',').map((e) => {
92+
const [hostSuffix, port] = e.split(':');
93+
return { hostSuffix, port };
94+
});
95+
5796
let agent;
58-
if (proxyUrl) {
97+
if (proxyUrl && should_proxy(sanitized, noProxyList)) {
5998
const ProxyAgent = require('https-proxy-agent');
6099
agent = new ProxyAgent(proxyUrl);
61100
log.http('download', 'proxy agent configured using: "%s"', proxyUrl);

lib/node-pre-gyp.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@ proto.configDefs = {
113113
debug: Boolean, // 'build'
114114
directory: String, // bin
115115
proxy: String, // 'install'
116+
noproxy: String, // 'install'
116117
loglevel: String // everywhere
117118
};
118119

0 commit comments

Comments
 (0)