You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
23 lines
632 B
JavaScript
23 lines
632 B
JavaScript
5 years ago
|
const axios = require('axios');
|
||
|
const fs = require('fs');
|
||
|
|
||
|
async function download(url, path, retry = 3) {
|
||
|
let r;
|
||
|
try {
|
||
|
const res = await axios.get(url, { responseType: 'stream' });
|
||
|
if (path) {
|
||
|
const w = await fs.createWriteStream(path);
|
||
|
res.data.pipe(w);
|
||
|
await new Promise((resolve, reject) => {
|
||
|
w.on('finish', resolve);
|
||
|
w.on('error', reject);
|
||
|
});
|
||
|
} else r = res.data;
|
||
|
} catch (e) {
|
||
|
if (retry) r = await download(url, path, retry - 1);
|
||
|
else throw e;
|
||
|
}
|
||
|
return r;
|
||
|
}
|
||
|
module.exports = download;
|