1 | 'use strict';
|
2 |
|
3 | var debug = require('debug')('urllib:detect_proxy_agent');
|
4 | var getProxyFromURI = require('./get_proxy_from_uri');
|
5 |
|
6 | var proxyAgents = {};
|
7 |
|
8 | function detectProxyAgent(uri, args) {
|
9 | if (!args.enableProxy && !process.env.URLLIB_ENABLE_PROXY) {
|
10 | return null;
|
11 | }
|
12 | var proxy = args.proxy || process.env.URLLIB_PROXY;
|
13 | if (!proxy) {
|
14 | proxy = getProxyFromURI(uri);
|
15 | if (!proxy) {
|
16 | return null;
|
17 | }
|
18 | }
|
19 |
|
20 | var proxyAgent = proxyAgents[proxy];
|
21 | if (!proxyAgent) {
|
22 | debug('create new proxy %s', proxy);
|
23 |
|
24 | proxyAgent = proxyAgents[proxy] = new (require('proxy-agent'))(proxy);
|
25 | }
|
26 | debug('get proxy: %s', proxy);
|
27 | return proxyAgent;
|
28 | }
|
29 |
|
30 | module.exports = detectProxyAgent;
|
31 | module.exports.proxyAgents = proxyAgents;
|