Skip to content
This repository has been archived by the owner on Jun 12, 2022. It is now read-only.

Commit

Permalink
feat(proxy): Added opts.noProxy and NO_PROXY support
Browse files Browse the repository at this point in the history
Fixes: #17
  • Loading branch information
zkat committed Apr 27, 2017
1 parent c35015a commit f45c915
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 4 deletions.
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -286,6 +288,13 @@ fetch('https://registry.npmjs.org/make-fetch-happen', {
})
```

#### <a name="opts-no-proxy"></a> `> 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`.

#### <a name="https-opts"></a> `> opts.ca, opts.cert, opts.key, opts.strictSSL`

These values are passed in directly to the HTTPS agent and will be used for both
Expand Down
22 changes: 18 additions & 4 deletions agent.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit f45c915

Please sign in to comment.