diff --git a/README.md b/README.md index 4078be2..7160fa8 100644 --- a/README.md +++ b/README.md @@ -23,6 +23,7 @@ pooling, proxies, retries, [and more](#features)! * [`opts.cacheManager`](#opts-cache-manager) * [`opts.cache`](#opts-cache) * [`opts.proxy`](#opts-proxy) + * [`opts.noProxy`](#opts-no-proxy) * [`opts.ca, opts.cert, opts.key`](#https-opts) * [`opts.maxSockets`](#opts-max-sockets) * [`opts.retry`](#opts-retry) @@ -137,6 +138,7 @@ make-fetch-happen augments the `node-fetch` API with additional features availab * [`opts.cacheManager`](#opts-cache-manager) - Cache target to read/write * [`opts.cache`](#opts-cache) - `fetch` cache mode. Controls cache *behavior*. * [`opts.proxy`](#opts-proxy) - Proxy agent +* [`opts.noProxy`](#opts-no-proxy) - Domain segments to disable proxying for. * [`opts.ca, opts.cert, opts.key, opts.strictSSL`](#https-opts) * [`opts.localAddress`](#opts-local-address) * [`opts.maxSockets`](#opts-max-sockets) @@ -286,6 +288,13 @@ fetch('https://registry.npmjs.org/make-fetch-happen', { }) ``` +#### `> opts.noProxy` + +If present, should be a comma-separated string or an array of domain extensions +that a proxy should _not_ be used for. + +This option may also be provided through `process.env.NO_PROXY`. + #### `> opts.ca, opts.cert, opts.key, opts.strictSSL` These values are passed in directly to the HTTPS agent and will be used for both diff --git a/agent.js b/agent.js index e19e5cf..fb97208 100644 --- a/agent.js +++ b/agent.js @@ -59,9 +59,22 @@ function getAgent (uri, opts) { return agent } -function checkNoProxy (uri) { - // TODO - return false +function checkNoProxy (uri, opts) { + const host = url.parse(uri).hostname.split('.').reverse() + let noproxy = (opts.noProxy || getProcessEnv('no_proxy')) + if (typeof noproxy === 'string') { + noproxy = noproxy.split(/\s*,\s*/g) + } + return noproxy && noproxy.some(no => { + const noParts = no.split('.').filter(x => x).reverse() + if (!noParts.length) { return false } + for (let i = 0; i < noParts.length; i++) { + if (host[i] !== noParts[i]) { + return false + } + } + return true + }) } module.exports.getProcessEnv = getProcessEnv @@ -97,10 +110,11 @@ function getProxyUri (uri, opts) { ) || ( protocol === 'http:' && getProcessEnv(['https_proxy', 'http_proxy', 'proxy']) ) + if (!proxy) { return null } const parsedProxy = (typeof proxy === 'string') ? url.parse(proxy) : proxy - return !checkNoProxy(uri) && parsedProxy + return !checkNoProxy(uri, opts) && parsedProxy } let HttpProxyAgent