Skip to content

Commit

Permalink
fix: bypass libnpmconfig bug on win32
Browse files Browse the repository at this point in the history
Removed libnpmconfig, use direct "npm config command".
  • Loading branch information
3cp committed Jan 17, 2021
1 parent b15a932 commit 47947da
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 39 deletions.
28 changes: 15 additions & 13 deletions lib/get-https-proxy-agent.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
// Only support https proxy right now
const config = require('libnpmconfig');
const {execSync} = require('child_process');
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;
function getNpmrcKey(key) {
const value = execSync(`npm config get ${key}`).toString().trim();
if (value === 'undefined' || value === 'null') return;
return value;
}

module.exports = function () {
const rcHttpsProxy = getNpmrcKey('https-proxy');
const rcProxy = getNpmrcKey('proxy');
const envHttpsProxy = process.env.HTTPS_PROXY;
const envHttpsProxyL = process.env.https_proxy;
const envHttpProxy = process.env.HTTP_PROXY;
const envHttpProxyL = process.env.http_proxy;

let proxy;
if (rcHttpsProxy) {
Expand Down Expand Up @@ -43,5 +45,5 @@ module.exports = function ({
return new HttpsProxyAgent(proxy);
}

warn('Unsupported (not a http(s) proxy), you may experience failure.');
warn(`Unsupported proxy ${proxy}, you may experience failure.`);
};
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@
"hosted-git-info": "^3.0.5",
"https-proxy-agent": "^5.0.0",
"isutf8": "^3.1.1",
"libnpmconfig": "^1.2.1",
"lodash.camelcase": "^4.3.0",
"lodash.mergewith": "^4.6.2",
"minimist": "^1.2.5",
Expand All @@ -61,5 +60,8 @@
"tar-fs": "^2.1.0",
"tmp": "^0.2.1",
"vinyl": "^2.2.1"
},
"ava": {
"concurrency": 1
}
}
70 changes: 45 additions & 25 deletions test/get-https-proxy-agent.spec.js
Original file line number Diff line number Diff line change
@@ -1,67 +1,87 @@
const test = require('ava');
const {execSync} = require('child_process');
const getAgent = require('../lib/get-https-proxy-agent');

function getAgentWith(mock) {
for (let key in mock.npmrc) {
execSync(`npm set ${key} ${mock.npmrc[key]}`);
}
for (let key in mock.env) {
process.env[key] = mock.env[key];
}
try {
return getAgent();
} finally {
for (let key in mock.npmrc) {
execSync(`npm config delete ${key}`);
}
for (let key in mock.env) {
delete process.env[key];
}
}
}

test.serial('getHttpsProxyAgent gets https-proxy from npmrc', async t => {

const agent = getAgent({
_npmrc: { 'https-proxy' : 'https://domain1.com'},
_env: {}
const agent = getAgentWith({
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: {}
const agent = getAgentWith({
npmrc: { 'proxy' : 'https://domain2.com:80443'},
env: {}
});
t.is(agent.proxy.href, 'https://domain2.com:80443/');
});

test.serial('getHttpsProxyAgent gets proxy from npmrc, but reject non-http proxy', async t => {
const agent = getAgent({
_npmrc: { 'proxy' : 'what.ever.proxy'},
_env: {}
const agent = getAgentWith({
npmrc: { 'proxy' : 'what.ever.proxy'},
env: {}
});
t.is(agent, undefined);
});

test.serial('getHttpsProxyAgent gets HTTPS_PROXY from env', async t => {
const agent = getAgent({
_npmrc: {},
_env: {HTTPS_PROXY: 'https://domain3.com'}
const agent = getAgentWith({
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'}
const agent = getAgentWith({
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'}
const agent = getAgentWith({
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'}
const agent = getAgentWith({
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: {}
const agent = getAgentWith({
npmrc: {},
env: {}
});
t.is(agent, undefined);
});
});

0 comments on commit 47947da

Please sign in to comment.