Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
var download = function(url, dest, cb) {
var file = fs.createWriteStream(dest);
http.get(url, function(response) {
response.pipe(file);
file.on("finish", function() {
file.close(cb); // close() is async, call cb after close completes.
});
}).on("error", function(err) { // Handle errors
fs.unlink(dest); // Delete the file async. (But we don't check the result)
if (cb) cb(err.message);
});
};
function getHttpAndHttps(url, cb) {
if(/^https/.test(url)) {
https.get(url, cb).on('error', function(e) {
util.puts(JSON.stringify(e));
util.puts(url);
cb(null);
});
} else {
http.get(url, cb).on('error', function(e) {
util.puts(JSON.stringify(e));
util.puts(url);
cb(null);
});
}
}
function queryURL(url, cb) {
if (cache[url]) return cb(null, null, cache[url]);
http.get(url, function(response) {
parseBody(response, function(body){
cache[url] = body;
cb(null, response, body);
});
})
.on('error', function(err) {
cb(err);
});
}
request(url, length) {
const options = typeof url === 'string' ? URL.parse(url) : url;
if (!options.headers) options.headers = {};
if (length > 0) {
options.headers.Range = `bytes=${length}-`;
}
if (options.protocol === 'https:') {
let req = https.get(options, (res) => {
this.processRes(req, res);
});
} else {
let req = http.get(options, (res) => {
this.processRes(req, res);
});
}
}
function fetch(uri, callback){
uri = url.parse(uri);
if(uri.protocol === "https:"){
return https.get(uri, callback);
}else{
return http.get(uri, callback);
}
}