Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
function download (url, dest, cb) {
const file = fs.createWriteStream(dest);
http.get(url, function(response) {
response.pipe(file);
file.on('finish', function() {
file.close(cb);
});
}).on('error', function(err) {
fs.unlink(dest);
if (cb) cb(err.message);
});
}
const download = function(url, cb) {
https
.get(url, resp => {
let data = '';
resp.on('data', chunk => {
data += chunk;
});
resp.on('end', () => {
cb(null, JSON.parse(data));
});
})
.on('error', err => {
cb(err);
});
};
const request = (url) => new Promise((resolve, reject) => {
https
.get(url, (resp) => {
let data = ''
resp.on('data', (chunk) => data += chunk)
resp.on('end', () => resolve(JSON.parse(data)))
})
.on("error", (err) => reject(err))
})
return new Promise((resolve, reject) => {
https.get(url, (resp) => {
let data = '';
resp.on('data', (chunk) => {
data += chunk;
});
resp.on('end', () => {
resolve(data);
});
}).on("error", (err) => reject(err));
});
}
file.on('open', () => {
https.get(url, (res) => {
res.pipe(file)
file.on('finish', () => {
file.close(resolve)
})
})
.on('error', (err) => {
return reject(new Error('Failed to download file at ' + url + ' (' + 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 download(url, callback) {
https.get(url, options, (res) => {
let data = '';
res.on('data', (chunk) => { data += chunk; });
res.on('end', (e) => {
if (e) throw e;
callback(data);
});
});
}
function downloadHttps(url, cb) {
var data = [], dataLen = 0;
https.get(url, function(res) {
res.on('data', function(chunk) {
data.push(chunk);
dataLen += chunk.length;
}).on('end', function() {
var buf = Buffer.alloc(dataLen);
for (var i=0, len = data.length, pos = 0; i < len; i++) {
data[i].copy(buf, pos);
pos += data[i].length;
}
cb(buf);
});
});
}
function getHttps(url, cb) {
https.get(url, (res) => {
let body = '';
res.on('data', (d) => {
body += d;
});
res.on('end', () => {
cb(body);
});
});
}