Skip to content

Commit

Permalink
feat: use get-url-proxy for better proxy environment detection
Browse files Browse the repository at this point in the history
use get-url-proxy for better proxy environment detection
  • Loading branch information
gajus committed Nov 20, 2017
2 parents 22dfd27 + b8f93c8 commit 63dca9d
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 5 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ node_modules
!.gitignore
!.npmignore
!.travis.yml
package-lock.json
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,12 @@ type fetch = (url: string, configuration?: UserConfigurationType) => Promise<Res

### HTTP proxy

Uses `HTTP_PROXY` (`http://host:port`) environment variable value to configure HTTP proxy.
Uses `PROTOCOL_PROXY` environment variable value to configure HTTP(S) proxy and supports `NO_PROXY` exclusions.

```
export NO_PROXY=".localdomain,192.168.1."
export HTTP_PROXY="http://host:port"
```

> Note: You must also configure `NODE_TLS_REJECT_UNAUTHORIZED=0`.
> This is a lazy workaround to enable the proxy to work with TLS.
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"bluefeather": "^2.7.0",
"es6-error": "^4.0.2",
"form-data": "^2.3.1",
"get-url-proxy": "^1.1.2",
"http-proxy-agent": "^2.0.0",
"https-proxy-agent": "^2.1.0",
"node-fetch": "^2.0.0-alpha.5",
Expand Down Expand Up @@ -72,5 +73,5 @@
"precommit": "npm run lint && npm run test && npm run build",
"test": "NODE_ENV=development nyc --reporter=text ava --verbose --serial"
},
"version": "1.0.0"
"version": "1.0.1"
}
8 changes: 5 additions & 3 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import {
import FormData from 'form-data';
import HttpProxyAgent from 'http-proxy-agent';
import HttpsProxyAgent from 'https-proxy-agent';
import getProxy from 'get-url-proxy/cached';
import Logger from './Logger';
import type {
ConfigurationType,
Expand Down Expand Up @@ -100,17 +101,18 @@ const createConfiguration = async (url: string, userConfiguration: UserConfigura
}

let agent;
const proxy = getProxy(url);

if (process.env.HTTP_PROXY) {
log.debug('using proxy %s', process.env.HTTP_PROXY);
if (proxy) {
log.debug('using proxy %s', proxy);

if (process.env.NODE_TLS_REJECT_UNAUTHORIZED !== '0') {
throw new Error('Must configure NODE_TLS_REJECT_UNAUTHORIZED.');
}

const AgentConstructor = url.toLowerCase().startsWith('https://') ? HttpsProxyAgent : HttpProxyAgent;

agent = new AgentConstructor(process.env.HTTP_PROXY);
agent = new AgentConstructor(proxy);
}

const host = getHost(url);
Expand Down

0 comments on commit 63dca9d

Please sign in to comment.