-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support proxy from npmrc or env
Support npmrc https-proxy and proxy. Support env HTTPS_PROXY, https_proxy, HTTP_PROXY, http_proxy. closes #6
- Loading branch information
Showing
4 changed files
with
124 additions
and
10 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
// Only support https proxy right now | ||
const config = require('libnpmconfig'); | ||
const HttpsProxyAgent = require('https-proxy-agent'); | ||
const {info, warn} = require('./log'); | ||
|
||
module.exports = function ({ | ||
_npmrc, | ||
_env = process.env | ||
} = {}) { | ||
if (!_npmrc) _npmrc = config.read().toJSON(); | ||
const rcHttpsProxy = _npmrc['https-proxy']; | ||
const rcProxy = _npmrc['proxy']; | ||
const envHttpsProxy = _env.HTTPS_PROXY; | ||
const envHttpsProxyL = _env.https_proxy; | ||
const envHttpProxy = _env.HTTP_PROXY; | ||
const envHttpProxyL = _env.http_proxy; | ||
|
||
let proxy; | ||
if (rcHttpsProxy) { | ||
info(`Got npmrc https-proxy: ${rcHttpsProxy}`); | ||
proxy = rcHttpsProxy; | ||
} else if (rcProxy) { | ||
info(`Got npmrc proxy: ${rcProxy}`); | ||
proxy = rcProxy; | ||
} else if (envHttpsProxy) { | ||
info(`Got env HTTPS_PROXY: ${envHttpsProxy}`); | ||
proxy = envHttpsProxy; | ||
} else if (envHttpsProxyL) { | ||
info(`Got env https_proxy: ${envHttpsProxyL}`); | ||
proxy = envHttpsProxyL; | ||
} else if (envHttpProxy) { | ||
info(`Got env HTTP_PROXY: ${envHttpProxy}`); | ||
proxy = envHttpProxy; | ||
} else if (envHttpProxyL) { | ||
info(`Got env http_proxy: ${envHttpProxyL}`); | ||
proxy = envHttpProxyL; | ||
} | ||
|
||
if (!proxy) return; | ||
|
||
// Start with http or https | ||
if (proxy.startsWith('http')) { | ||
return new HttpsProxyAgent(proxy); | ||
} | ||
|
||
warn('Unsupported (not a http(s) proxy), you may experience failure.'); | ||
}; |
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,59 @@ | ||
const test = require('ava'); | ||
const getAgent = require('../lib/get-https-proxy-agent'); | ||
|
||
test.serial('getHttpsProxyAgent gets https-proxy from npmrc', async t => { | ||
|
||
const agent = getAgent({ | ||
_npmrc: { 'https-proxy' : 'https://domain1.com'}, | ||
_env: {} | ||
}); | ||
t.is(agent.proxy.href, 'https://domain1.com/'); | ||
}); | ||
|
||
test.serial('getHttpsProxyAgent gets proxy from npmrc', async t => { | ||
const agent = getAgent({ | ||
_npmrc: { 'proxy' : 'https://domain2.com:80443'}, | ||
_env: {} | ||
}); | ||
t.is(agent.proxy.href, 'https://domain2.com:80443/'); | ||
}); | ||
|
||
test.serial('getHttpsProxyAgent gets HTTPS_PROXY from env', async t => { | ||
const agent = getAgent({ | ||
_npmrc: {}, | ||
_env: {HTTPS_PROXY: 'https://domain3.com'} | ||
}); | ||
t.is(agent.proxy.href, 'https://domain3.com/'); | ||
}); | ||
|
||
test.serial('getHttpsProxyAgent gets https_proxy from env', async t => { | ||
const agent = getAgent({ | ||
_npmrc: {}, | ||
_env: {https_proxy: 'https://domain4.com'} | ||
}); | ||
t.is(agent.proxy.href, 'https://domain4.com/'); | ||
}); | ||
|
||
test.serial('getHttpsProxyAgent gets HTTP_PROXY from env', async t => { | ||
const agent = getAgent({ | ||
_npmrc: {}, | ||
_env: {HTTP_PROXY: 'http://domain5.com'} | ||
}); | ||
t.is(agent.proxy.href, 'http://domain5.com/'); | ||
}); | ||
|
||
test.serial('getHttpsProxyAgent gets http_proxy from env', async t => { | ||
const agent = getAgent({ | ||
_npmrc: {}, | ||
_env: {HTTP_PROXY: 'http://domain6.com'} | ||
}); | ||
t.is(agent.proxy.href, 'http://domain6.com/'); | ||
}); | ||
|
||
test.serial('getHttpsProxyAgent returns nothing if no proxy set', async t => { | ||
const agent = getAgent({ | ||
_npmrc: {}, | ||
_env: {} | ||
}); | ||
t.is(agent, undefined); | ||
}); |