proxy-from-env
Advanced tools
Comparing version 0.0.1 to 0.1.0
43
index.js
@@ -6,8 +6,8 @@ 'use strict'; | ||
var DEFAULT_PORTS = { | ||
'ftp:': 21, | ||
'gopher:': 70, | ||
'http:': 80, | ||
'https:': 443, | ||
'ws:': 80, | ||
'wss:': 443, | ||
ftp: 21, | ||
gopher: 70, | ||
http: 80, | ||
https: 443, | ||
ws: 80, | ||
wss: 443, | ||
}; | ||
@@ -21,3 +21,3 @@ | ||
/** | ||
* @param {string} url - The URL | ||
* @param {string|object} url - The URL, or the result from url.parse. | ||
* @return {string} The URL of the proxy that should handle the request to the | ||
@@ -27,8 +27,19 @@ * given URL. If no proxy is set, this will be an empty string. | ||
function getProxyForUrl(url) { | ||
var parsedUrl = parseUrl(url); | ||
if (!parsedUrl.host || !shouldProxy(parsedUrl)) { | ||
return ''; // Don't proxy invalid URLs or URLs that match NO_PROXY. | ||
var parsedUrl = typeof url === 'string' ? parseUrl(url) : url || {}; | ||
var proto = parsedUrl.protocol; | ||
var hostname = parsedUrl.host; | ||
var port = parsedUrl.port; | ||
if (typeof hostname !== 'string' || !hostname || typeof proto !== 'string') { | ||
return ''; // Don't proxy URLs without a valid scheme or host. | ||
} | ||
var proto = url.split(':', 1)[0]; | ||
proto = proto.split(':', 1)[0]; | ||
// Stripping ports in this way instead of using parsedUrl.hostname to make | ||
// sure that the brackets around IPv6 addresses are kept. | ||
hostname = hostname.replace(/:\d*$/, ''); | ||
port = parseInt(port) || DEFAULT_PORTS[proto] || 0; | ||
if (!shouldProxy(hostname, port)) { | ||
return ''; // Don't proxy URLs that match NO_PROXY. | ||
} | ||
return getEnv(proto + '_proxy') || getEnv('all_proxy'); | ||
@@ -40,7 +51,8 @@ } | ||
* | ||
* @param {object} parsedUrl - The result of url.parse | ||
* @param {string} hostname - The host name of the URL. | ||
* @param {number} port - The effective port of the URL. | ||
* @returns {boolean} Whether the given URL should be proxied. | ||
* @private | ||
*/ | ||
function shouldProxy(parsedUrl) { | ||
function shouldProxy(hostname, port) { | ||
var NO_PROXY = getEnv('no_proxy').toLowerCase(); | ||
@@ -54,7 +66,2 @@ if (!NO_PROXY) { | ||
var port = parseInt(parsedUrl.port) || DEFAULT_PORTS[parsedUrl.protocol] || 0; | ||
// Stripping ports in this way instead of using parsedUrl.hostname to make | ||
// sure that the brackets around IPv6 addresses are kept. | ||
var hostname = parsedUrl.host.replace(/:\d*$/, ''); | ||
return NO_PROXY.split(/[,\s]/).every(function(proxy) { | ||
@@ -61,0 +68,0 @@ if (!proxy) { |
{ | ||
"name": "proxy-from-env", | ||
"version": "0.0.1", | ||
"version": "0.1.0", | ||
"description": "Offers getProxyForUrl to get the proxy URL for a URL, respecting the *_PROXY (e.g. HTTP_PROXY) and NO_PROXY environment variables.", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
# proxy-from-env | ||
`proxy-from-env` is a Node.js package that exports a function (`getProxyForUrl`) | ||
that takes an input URL (a string) and returns the desired proxy URL (also a | ||
string) based on standard proxy environment variables. If no proxy is set, an | ||
empty string is returned. | ||
that takes an input URL (a string or | ||
[`url.parse`](https://nodejs.org/docs/latest/api/url.html#url_url_parsing)'s | ||
return value) and returns the desired proxy URL (also a string) based on | ||
standard proxy environment variables. If no proxy is set, an empty string is | ||
returned. | ||
@@ -8,0 +10,0 @@ It is your responsibility to actually proxy the request using the given URL. |
35
test.js
@@ -5,2 +5,3 @@ /* jshint mocha:true */ | ||
var assert = require('assert'); | ||
var parseUrl = require('url').parse; | ||
@@ -29,12 +30,16 @@ var getProxyForUrl = require('./').getProxyForUrl; | ||
assert.strictEqual('', getProxyForUrl('//example.com')); | ||
assert.strictEqual('', getProxyForUrl('://example.com')); | ||
assert.strictEqual('', getProxyForUrl('://')); | ||
assert.strictEqual('', getProxyForUrl('/path')); | ||
assert.strictEqual('', getProxyForUrl('')); | ||
assert.throws(function() { | ||
getProxyForUrl(); | ||
}, TypeError); // "Parameter 'url' must be a string, not undefined" | ||
assert.throws(function() { | ||
getProxyForUrl({}); | ||
}, TypeError); // "Parameter 'url' must be a string, not object" | ||
assert.strictEqual('', getProxyForUrl('http:')); | ||
assert.strictEqual('', getProxyForUrl('http:/')); | ||
assert.strictEqual('', getProxyForUrl('http://')); | ||
assert.strictEqual('', getProxyForUrl('prototype://')); | ||
assert.strictEqual('', getProxyForUrl('hasOwnProperty://')); | ||
assert.strictEqual('', getProxyForUrl('__proto__://')); | ||
assert.strictEqual('', getProxyForUrl()); | ||
assert.strictEqual('', getProxyForUrl({})); | ||
assert.strictEqual('', getProxyForUrl({host: 'x', protocol: 1})); | ||
assert.strictEqual('', getProxyForUrl({host: 1, protocol: 'x'})); | ||
}); | ||
@@ -47,2 +52,4 @@ | ||
assert.strictEqual('http://http-proxy', getProxyForUrl('http://example')); | ||
assert.strictEqual('http://http-proxy', | ||
getProxyForUrl(parseUrl('http://example'))); | ||
@@ -283,2 +290,14 @@ process.env.http_proxy = 'http://priority'; | ||
}); | ||
it('no_proxy should not be case-sensitive', function() { | ||
process.env.ALL_PROXY = 'http://proxy'; | ||
process.env.NO_PROXY = 'XXX,YYY,ZzZ'; | ||
assert.strictEqual('', getProxyForUrl('http://xxx')); | ||
assert.strictEqual('', getProxyForUrl('http://XXX')); | ||
assert.strictEqual('', getProxyForUrl('http://yyy')); | ||
assert.strictEqual('', getProxyForUrl('http://YYY')); | ||
assert.strictEqual('', getProxyForUrl('http://ZzZ')); | ||
assert.strictEqual('', getProxyForUrl('http://zZz')); | ||
}); | ||
}); |
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
Found 1 instance in 1 package
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
Found 13 instances in 1 package
22063
331
119
97