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.
Hydro/hydro/lib/download.js

24 lines
661 B
JavaScript

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;
}
global.Hydro.lib.download = module.exports = download;