-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: attempt to use smaller function set of fetch
Make-fetch-happen uses minipass-fetch, disregarding caching, this goes back to supporting the proxy ourselves.
- Loading branch information
Showing
4 changed files
with
120 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
'use strict' | ||
// Taken from https://github.com/request/request/blob/212570b/lib/getProxyFromURI.js | ||
|
||
const url = require('url') | ||
const HttpProxyAgent = require('http-proxy-agent') | ||
const HttpsProxyAgent = require('https-proxy-agent') | ||
|
||
function formatHostname (hostname) { | ||
// canonicalize the hostname, so that 'oogle.com' won't match 'google.com' | ||
return hostname.replace(/^\.*/, '.').toLowerCase() | ||
} | ||
|
||
function parseNoProxyZone (zone) { | ||
zone = zone.trim().toLowerCase() | ||
|
||
const zoneParts = zone.split(':', 2) | ||
const zoneHost = formatHostname(zoneParts[0]) | ||
const zonePort = zoneParts[1] | ||
const hasPort = zone.indexOf(':') > -1 | ||
|
||
return { hostname: zoneHost, port: zonePort, hasPort: hasPort } | ||
} | ||
|
||
function uriInNoProxy (uri, noProxy) { | ||
const port = uri.port || (uri.protocol === 'https:' ? '443' : '80') | ||
const hostname = formatHostname(uri.hostname) | ||
const noProxyList = noProxy.split(',') | ||
|
||
// iterate through the noProxyList until it finds a match. | ||
return noProxyList.map(parseNoProxyZone).some(function (noProxyZone) { | ||
const isMatchedAt = hostname.indexOf(noProxyZone.hostname) | ||
const hostnameMatched = ( | ||
isMatchedAt > -1 && | ||
(isMatchedAt === hostname.length - noProxyZone.hostname.length) | ||
) | ||
|
||
if (noProxyZone.hasPort) { | ||
return (port === noProxyZone.port) && hostnameMatched | ||
} | ||
|
||
return hostnameMatched | ||
}) | ||
} | ||
|
||
function getProxyFromURI (gyp, uri, ca) { | ||
// If a string URI/URL was given, parse it into a URL object | ||
if (typeof uri === 'string') { | ||
// eslint-disable-next-line | ||
uri = new url.URL(uri) | ||
} | ||
|
||
// Decide the proper request proxy to use based on the request URI object and the | ||
// environmental variables (NO_PROXY, HTTP_PROXY, etc.) | ||
// respect NO_PROXY environment variables (see: https://lynx.invisible-island.net/lynx2.8.7/breakout/lynx_help/keystrokes/environments.html) | ||
|
||
const noProxy = gyp.opts.noproxy || | ||
process.env.NO_PROXY || | ||
process.env.no_proxy || '' | ||
|
||
// if the noProxy is a wildcard then return null | ||
|
||
if (noProxy === '*') { | ||
return null | ||
} | ||
|
||
// if the noProxy is not empty and the uri is found return null | ||
|
||
if (noProxy !== '' && uriInNoProxy(uri, noProxy)) { | ||
return null | ||
} | ||
|
||
// Check for HTTP or HTTPS Proxy in environment Else default to null | ||
|
||
if (uri.protocol === 'http:') { | ||
let pxuri = gyp.opts.proxy || | ||
process.env.HTTP_PROXY || | ||
process.env.http_proxy || null | ||
|
||
if (pxuri) { | ||
pxuri = new url.URL(pxuri) | ||
return new HttpProxyAgent({ host: pxuri.hostname, port: pxuri.port, ca }) | ||
} | ||
} | ||
|
||
if (uri.protocol === 'https:') { | ||
let pxuri = gyp.opts.proxy || | ||
process.env.HTTPS_PROXY || | ||
process.env.https_proxy || | ||
process.env.HTTP_PROXY || | ||
process.env.http_proxy || null | ||
|
||
if (pxuri) { | ||
pxuri = new url.URL(pxuri) | ||
return new HttpsProxyAgent({ host: pxuri.host, port: pxuri.port, ca }) | ||
} | ||
} | ||
|
||
// if none of that works, return null | ||
// (What uri protocol are you using then?) | ||
|
||
return null | ||
} | ||
|
||
module.exports = getProxyFromURI |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters