UNPKG

786 BJavaScriptView Raw
1'use strict';
2
3var debug = require('debug')('urllib:detect_proxy_agent');
4var getProxyFromURI = require('./get_proxy_from_uri');
5
6var proxyAgents = {};
7
8function 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 // lazy require, only support node >= 4
24 proxyAgent = proxyAgents[proxy] = new (require('proxy-agent'))(proxy);
25 }
26 debug('get proxy: %s', proxy);
27 return proxyAgent;
28}
29
30module.exports = detectProxyAgent;
31module.exports.proxyAgents = proxyAgents;