mirror of
https://gitcode.com/gh_mirrors/hy/Hydro.git
synced 2026-10-17 17:36:08 +08:00
20 lines
563 B
TypeScript
20 lines
563 B
TypeScript
import superagent from 'superagent';
|
|
import fs from 'fs';
|
|
|
|
async function _download(url: string, path: string, retry: number) {
|
|
const w = fs.createWriteStream(path);
|
|
superagent.get(url).retry(retry).pipe(w);
|
|
await new Promise((resolve, reject) => {
|
|
w.on('finish', resolve);
|
|
w.on('error', reject);
|
|
});
|
|
return path;
|
|
}
|
|
|
|
export default function download(url: string, path?: string, retry = 3) {
|
|
if (path) return _download(url, path, retry);
|
|
return superagent.get(url).retry(retry);
|
|
}
|
|
|
|
global.Hydro.lib.download = download;
|