租房小程序前端代码
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

31 lines
794 B

6 months ago
  1. 'use strict';
  2. var debug = require('util').debuglog('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;