react / wstein / node_modules / jest-cli / node_modules / jsdom / node_modules / request / lib / getProxyFromURI.js
81141 views'use strict'12function formatHostname(hostname) {3// canonicalize the hostname, so that 'oogle.com' won't match 'google.com'4return hostname.replace(/^\.*/, '.').toLowerCase()5}67function parseNoProxyZone(zone) {8zone = zone.trim().toLowerCase()910var zoneParts = zone.split(':', 2)11, zoneHost = formatHostname(zoneParts[0])12, zonePort = zoneParts[1]13, hasPort = zone.indexOf(':') > -11415return {hostname: zoneHost, port: zonePort, hasPort: hasPort}16}1718function uriInNoProxy(uri, noProxy) {19var port = uri.port || (uri.protocol === 'https:' ? '443' : '80')20, hostname = formatHostname(uri.hostname)21, noProxyList = noProxy.split(',')2223// iterate through the noProxyList until it finds a match.24return noProxyList.map(parseNoProxyZone).some(function(noProxyZone) {25var isMatchedAt = hostname.indexOf(noProxyZone.hostname)26, hostnameMatched = (27isMatchedAt > -1 &&28(isMatchedAt === hostname.length - noProxyZone.hostname.length)29)3031if (noProxyZone.hasPort) {32return (port === noProxyZone.port) && hostnameMatched33}3435return hostnameMatched36})37}3839function getProxyFromURI(uri) {40// Decide the proper request proxy to use based on the request URI object and the41// environmental variables (NO_PROXY, HTTP_PROXY, etc.)42// respect NO_PROXY environment variables (see: http://lynx.isc.org/current/breakout/lynx_help/keystrokes/environments.html)4344var noProxy = process.env.NO_PROXY || process.env.no_proxy || ''4546// if the noProxy is a wildcard then return null4748if (noProxy === '*') {49return null50}5152// if the noProxy is not empty and the uri is found return null5354if (noProxy !== '' && uriInNoProxy(uri, noProxy)) {55return null56}5758// Check for HTTP or HTTPS Proxy in environment Else default to null5960if (uri.protocol === 'http:') {61return process.env.HTTP_PROXY ||62process.env.http_proxy || null63}6465if (uri.protocol === 'https:') {66return process.env.HTTPS_PROXY ||67process.env.https_proxy ||68process.env.HTTP_PROXY ||69process.env.http_proxy || null70}7172// if none of that works, return null73// (What uri protocol are you using then?)7475return null76}7778module.exports = getProxyFromURI798081