Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: visibly warn about proxy settings
- Loading branch information
1 parent
a3d103c
commit 88d3cde
Showing
6 changed files
with
99 additions
and
189 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,23 @@ | ||
const PROXY_ENV_VARIABLES = ['HTTP_PROXY', 'HTTPS_PROXY', 'NO_PROXY']; | ||
|
||
|
||
/** | ||
* Expects an environment variables object (typically process.env) | ||
* and returns an array of strings representing HTTP proxy settings, | ||
* such as ['HTTPS_PROXY=https://proxy.example.com:8080', ...] | ||
* | ||
* Supports both upper and lower case names and skips env vars set as empty | ||
* strings (other falsy values are not taken care of as env vars can only | ||
* be strings). | ||
* | ||
* Note: The settings are later only printed to the user. Applying the settings | ||
* is handled directly by the 'request' library, see | ||
* https://github.com/request/request#user-content-proxies | ||
*/ | ||
module.exports = function getProxySettings(env) { | ||
// can't use Object.entries() yet (Node 6 support) | ||
return Object.keys(env).map(name => [name, env[name]]) | ||
.filter(entry => PROXY_ENV_VARIABLES.includes(entry[0].toUpperCase())) | ||
.filter(entry => entry[1] !== '') | ||
.map(entry => `${entry[0]}=${entry[1]}`); | ||
}; |
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 { assert } = require('chai'); | ||
|
||
const getProxySettings = require('../../lib/getProxySettings'); | ||
|
||
|
||
describe('getProxySettings()', () => { | ||
it('detects HTTP_PROXY', () => { | ||
assert.deepEqual(getProxySettings({ | ||
SHELL: '/bin/bash', | ||
USER: 'honza', | ||
HTTP_PROXY: 'http://proxy.example.com:8080', | ||
}), [ | ||
'HTTP_PROXY=http://proxy.example.com:8080', | ||
]); | ||
}); | ||
|
||
it('detects HTTPS_PROXY', () => { | ||
assert.deepEqual(getProxySettings({ | ||
SHELL: '/bin/bash', | ||
USER: 'honza', | ||
HTTPS_PROXY: 'https://proxy.example.com:8080', | ||
}), [ | ||
'HTTPS_PROXY=https://proxy.example.com:8080', | ||
]); | ||
}); | ||
|
||
it('detects NO_PROXY', () => { | ||
assert.deepEqual(getProxySettings({ | ||
SHELL: '/bin/bash', | ||
USER: 'honza', | ||
NO_PROXY: '*', | ||
}), [ | ||
'NO_PROXY=*', | ||
]); | ||
}); | ||
|
||
it('detects both lower and upper case', () => { | ||
assert.deepEqual(getProxySettings({ | ||
SHELL: '/bin/bash', | ||
USER: 'honza', | ||
http_proxy: 'http://proxy.example.com:8080', | ||
NO_PROXY: '*', | ||
}), [ | ||
'http_proxy=http://proxy.example.com:8080', | ||
'NO_PROXY=*', | ||
]); | ||
}); | ||
|
||
it('skips environment variables set to empty strings', () => { | ||
assert.deepEqual(getProxySettings({ | ||
SHELL: '/bin/bash', | ||
USER: 'honza', | ||
http_proxy: 'http://proxy.example.com:8080', | ||
NO_PROXY: '', | ||
}), [ | ||
'http_proxy=http://proxy.example.com:8080', | ||
]); | ||
}); | ||
}); |