detect_proxy_agent.js 786 B

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