Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
/*! @license MIT ©2016 Ruben Verborgh, Ghent University - imec */
/* Single-function HTTP(S) request module */
var EventEmitter = require('events').EventEmitter,
_ = require('lodash'),
url = require('url'),
http = require('follow-redirects').http,
https = require('follow-redirects').https,
zlib = require('zlib');
// Try to keep connections open, and set a maximum number of connections per server
var AGENT_SETTINGS = { keepAlive: true, maxSockets: 5 };
var AGENTS = {
http: new http.Agent(AGENT_SETTINGS),
https: new https.Agent(AGENT_SETTINGS),
};
// Decode encoded streams with these decoders
var DECODERS = { gzip: zlib.createGunzip, deflate: zlib.createInflate };
// Creates an HTTP request with the given settings
function createRequest(settings) {
// Parse the request URL
if (settings.url)
_.assign(settings, url.parse(settings.url));
// Emit the response through a proxy
var request, requestProxy = new EventEmitter(),
requester = settings.protocol === 'http:' ? http : https;
settings.agents = AGENTS;
request = requester.request(settings, function (response) {
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
'Accept-Encoding': 'gzip,deflate,sdch',
'Accept-Language': 'en-US,en;q=0.8',
'Cache-Control': 'no-cache',
'Connection': 'keep-alive',
'User-Agent': 'cleverstack'
},
rejectUnauthorized: false,
secureProtocol: require('constants').SSL_OP_NO_TLSv1_2
};
var proxy = process.env.HTTPS_PROXY || process.env.HTTP_PROXY || null;
if (proxy !== null) {
options.agent = new proxyAgent(proxy);
} else {
options.agent = new https.Agent(options);
}
return download(options, dir);
};