Use node-fetch instead of request (#6228)
* requestをnode-fetchになど * format * fix error * t * Fix test
This commit is contained in:
parent
bb7edfee04
commit
d3c0f3c251
12 changed files with 140 additions and 218 deletions
|
|
@ -1,5 +1,6 @@
|
|||
import * as fs from 'fs';
|
||||
import * as request from 'request';
|
||||
import fetch from 'node-fetch';
|
||||
import { httpAgent, httpsAgent } from './fetch';
|
||||
import config from '../config';
|
||||
import * as chalk from 'chalk';
|
||||
import Logger from '../services/logger';
|
||||
|
|
@ -7,11 +8,35 @@ import Logger from '../services/logger';
|
|||
export async function downloadUrl(url: string, path: string) {
|
||||
const logger = new Logger('download');
|
||||
|
||||
await new Promise((res, rej) => {
|
||||
logger.info(`Downloading ${chalk.cyan(url)} ...`);
|
||||
logger.info(`Downloading ${chalk.cyan(url)} ...`);
|
||||
|
||||
const response = await fetch(new URL(url).href, {
|
||||
headers: {
|
||||
'User-Agent': config.userAgent
|
||||
},
|
||||
timeout: 10 * 1000,
|
||||
agent: u => u.protocol == 'http:' ? httpAgent : httpsAgent,
|
||||
}).then(response => {
|
||||
if (!response.ok) {
|
||||
logger.error(`Got ${response.status} (${url})`);
|
||||
throw response.status;
|
||||
} else {
|
||||
return response;
|
||||
}
|
||||
});
|
||||
|
||||
await new Promise((res, rej) => {
|
||||
const writable = fs.createWriteStream(path);
|
||||
|
||||
response.body.on('error', (error: any) => {
|
||||
logger.error(`Failed to start download: ${chalk.cyan(url)}: ${error}`, {
|
||||
url: url,
|
||||
e: error
|
||||
});
|
||||
writable.close();
|
||||
rej(error);
|
||||
});
|
||||
|
||||
writable.on('finish', () => {
|
||||
logger.succ(`Download finished: ${chalk.cyan(url)}`);
|
||||
res();
|
||||
|
|
@ -25,35 +50,6 @@ export async function downloadUrl(url: string, path: string) {
|
|||
rej(error);
|
||||
});
|
||||
|
||||
const req = request({
|
||||
url: new URL(url).href, // https://github.com/syuilo/misskey/issues/2637
|
||||
proxy: config.proxy,
|
||||
timeout: 10 * 1000,
|
||||
forever: true,
|
||||
headers: {
|
||||
'User-Agent': config.userAgent
|
||||
}
|
||||
});
|
||||
|
||||
req.pipe(writable);
|
||||
|
||||
req.on('response', response => {
|
||||
if (response.statusCode !== 200) {
|
||||
logger.error(`Got ${response.statusCode} (${url})`);
|
||||
writable.close();
|
||||
rej(response.statusCode);
|
||||
}
|
||||
});
|
||||
|
||||
req.on('error', error => {
|
||||
logger.error(`Failed to start download: ${chalk.cyan(url)}: ${error}`, {
|
||||
url: url,
|
||||
e: error
|
||||
});
|
||||
writable.close();
|
||||
rej(error);
|
||||
});
|
||||
|
||||
logger.succ(`Downloaded to: ${path}`);
|
||||
response.body.pipe(writable);
|
||||
});
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue