rudeshark.net/src/misc/download-url.ts
syuilo b9cb6d1c10 refactor: refactoring imports
将来ESMに移行しやすいように
Related: #7658

なんかmochaが起動しなくなってるけど理由不明
すぐ直したい
2021-08-19 18:33:41 +09:00

41 lines
1.0 KiB
TypeScript

import * as fs from 'fs';
import * as stream from 'stream';
import * as util from 'util';
import { URL } from 'url';
import fetch from 'node-fetch';
import { getAgentByUrl } from './fetch.js';
import { AbortController } from 'abort-controller';
import config from '@/config/index.js';
import * as chalk from 'chalk';
import Logger from '@/services/logger.js';
const pipeline = util.promisify(stream.pipeline);
export async function downloadUrl(url: string, path: string) {
const logger = new Logger('download');
logger.info(`Downloading ${chalk.cyan(url)} ...`);
const controller = new AbortController();
setTimeout(() => {
controller.abort();
}, 60 * 1000);
const response = await fetch(new URL(url).href, {
headers: {
'User-Agent': config.userAgent
},
timeout: 10 * 1000,
signal: controller.signal,
agent: getAgentByUrl,
});
if (!response.ok) {
logger.error(`Got ${response.status} (${url})`);
throw response.status;
}
await pipeline(response.body, fs.createWriteStream(path));
logger.succ(`Download finished: ${chalk.cyan(url)}`);
}