index.cjs 3.1 KB

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