index.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. 'use strict';
  2. var parseUrl = require('url').parse;
  3. var DEFAULT_PORTS = {
  4. ftp: 21,
  5. gopher: 70,
  6. http: 80,
  7. https: 443,
  8. ws: 80,
  9. wss: 443,
  10. };
  11. var stringEndsWith = String.prototype.endsWith || function(s) {
  12. return s.length <= this.length &&
  13. this.indexOf(s, this.length - s.length) !== -1;
  14. };
  15. /**
  16. * @param {string|object} url - The URL, or the result from url.parse.
  17. * @return {string} The URL of the proxy that should handle the request to the
  18. * given URL. If no proxy is set, this will be an empty string.
  19. */
  20. function getProxyForUrl(url) {
  21. var parsedUrl = typeof url === 'string' ? parseUrl(url) : url || {};
  22. var proto = parsedUrl.protocol;
  23. var hostname = parsedUrl.host;
  24. var port = parsedUrl.port;
  25. if (typeof hostname !== 'string' || !hostname || typeof proto !== 'string') {
  26. return ''; // Don't proxy URLs without a valid scheme or host.
  27. }
  28. proto = proto.split(':', 1)[0];
  29. // Stripping ports in this way instead of using parsedUrl.hostname to make
  30. // sure that the brackets around IPv6 addresses are kept.
  31. hostname = hostname.replace(/:\d*$/, '');
  32. port = parseInt(port) || DEFAULT_PORTS[proto] || 0;
  33. if (!shouldProxy(hostname, port)) {
  34. return ''; // Don't proxy URLs that match NO_PROXY.
  35. }
  36. var proxy =
  37. getEnv('npm_config_' + proto + '_proxy') ||
  38. getEnv(proto + '_proxy') ||
  39. getEnv('npm_config_proxy') ||
  40. getEnv('all_proxy');
  41. if (proxy && proxy.indexOf('://') === -1) {
  42. // Missing scheme in proxy, default to the requested URL's scheme.
  43. proxy = proto + '://' + proxy;
  44. }
  45. return proxy;
  46. }
  47. /**
  48. * Determines whether a given URL should be proxied.
  49. *
  50. * @param {string} hostname - The host name of the URL.
  51. * @param {number} port - The effective port of the URL.
  52. * @returns {boolean} Whether the given URL should be proxied.
  53. * @private
  54. */
  55. function shouldProxy(hostname, port) {
  56. var NO_PROXY =
  57. (getEnv('npm_config_no_proxy') || getEnv('no_proxy')).toLowerCase();
  58. if (!NO_PROXY) {
  59. return true; // Always proxy if NO_PROXY is not set.
  60. }
  61. if (NO_PROXY === '*') {
  62. return false; // Never proxy if wildcard is set.
  63. }
  64. return NO_PROXY.split(/[,\s]/).every(function(proxy) {
  65. if (!proxy) {
  66. return true; // Skip zero-length hosts.
  67. }
  68. var parsedProxy = proxy.match(/^(.+):(\d+)$/);
  69. var parsedProxyHostname = parsedProxy ? parsedProxy[1] : proxy;
  70. var parsedProxyPort = parsedProxy ? parseInt(parsedProxy[2]) : 0;
  71. if (parsedProxyPort && parsedProxyPort !== port) {
  72. return true; // Skip if ports don't match.
  73. }
  74. if (!/^[.*]/.test(parsedProxyHostname)) {
  75. // No wildcards, so stop proxying if there is an exact match.
  76. return hostname !== parsedProxyHostname;
  77. }
  78. if (parsedProxyHostname.charAt(0) === '*') {
  79. // Remove leading wildcard.
  80. parsedProxyHostname = parsedProxyHostname.slice(1);
  81. }
  82. // Stop proxying if the hostname ends with the no_proxy host.
  83. return !stringEndsWith.call(hostname, parsedProxyHostname);
  84. });
  85. }
  86. /**
  87. * Get the value for an environment variable.
  88. *
  89. * @param {string} key - The name of the environment variable.
  90. * @return {string} The value of the environment variable.
  91. * @private
  92. */
  93. function getEnv(key) {
  94. return process.env[key.toLowerCase()] || process.env[key.toUpperCase()] || '';
  95. }
  96. exports.getProxyForUrl = getProxyForUrl;