summaryrefslogtreecommitdiff
path: root/src/misc
diff options
context:
space:
mode:
authorMeiMei <30769358+mei23@users.noreply.github.com>2020-04-09 23:42:23 +0900
committerGitHub <noreply@github.com>2020-04-09 23:42:23 +0900
commitd3c0f3c251e8371d78d953f32f7311a38f4a1bdb (patch)
treeef746ec79b4cc53ad15da2680b2e5d6280d39867 /src/misc
parentCreate aiscript.ja-JP.md (diff)
downloadmisskey-d3c0f3c251e8371d78d953f32f7311a38f4a1bdb.tar.gz
misskey-d3c0f3c251e8371d78d953f32f7311a38f4a1bdb.tar.bz2
misskey-d3c0f3c251e8371d78d953f32f7311a38f4a1bdb.zip
Use node-fetch instead of request (#6228)
* requestをnode-fetchになど * format * fix error * t * Fix test
Diffstat (limited to 'src/misc')
-rw-r--r--src/misc/donwload-url.ts60
-rw-r--r--src/misc/fetch.ts43
2 files changed, 71 insertions, 32 deletions
diff --git a/src/misc/donwload-url.ts b/src/misc/donwload-url.ts
index 939e6e9803..cd15bbd731 100644
--- a/src/misc/donwload-url.ts
+++ b/src/misc/donwload-url.ts
@@ -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,53 +8,48 @@ 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 writable = fs.createWriteStream(path);
+ 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;
+ }
+ });
- writable.on('finish', () => {
- logger.succ(`Download finished: ${chalk.cyan(url)}`);
- res();
- });
+ await new Promise((res, rej) => {
+ const writable = fs.createWriteStream(path);
- writable.on('error', error => {
- logger.error(`Download failed: ${chalk.cyan(url)}: ${error}`, {
+ response.body.on('error', (error: any) => {
+ logger.error(`Failed to start download: ${chalk.cyan(url)}: ${error}`, {
url: url,
e: error
});
+ writable.close();
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);
- }
+ writable.on('finish', () => {
+ logger.succ(`Download finished: ${chalk.cyan(url)}`);
+ res();
});
- req.on('error', error => {
- logger.error(`Failed to start download: ${chalk.cyan(url)}: ${error}`, {
+ writable.on('error', error => {
+ logger.error(`Download failed: ${chalk.cyan(url)}: ${error}`, {
url: url,
e: error
});
- writable.close();
rej(error);
});
- logger.succ(`Downloaded to: ${path}`);
+ response.body.pipe(writable);
});
}
diff --git a/src/misc/fetch.ts b/src/misc/fetch.ts
new file mode 100644
index 0000000000..887aae1659
--- /dev/null
+++ b/src/misc/fetch.ts
@@ -0,0 +1,43 @@
+import * as http from 'http';
+import * as https from 'https';
+import * as cache from 'lookup-dns-cache';
+import fetch, { HeadersInit } from 'node-fetch';
+import { HttpProxyAgent } from 'http-proxy-agent';
+import { HttpsProxyAgent } from 'https-proxy-agent';
+import config from '../config';
+
+export async function getJson(url: string, accept = 'application/json, */*', timeout = 10000, headers?: HeadersInit) {
+ const res = await fetch(url, {
+ headers: Object.assign({
+ 'User-Agent': config.userAgent,
+ Accept: accept
+ }, headers || {}),
+ timeout,
+ agent: u => u.protocol == 'http:' ? httpAgent : httpsAgent,
+ });
+
+ if (!res.ok) {
+ throw {
+ name: `StatusError`,
+ statusCode: res.status,
+ message: `${res.status} ${res.statusText}`,
+ };
+ }
+
+ return await res.json();
+}
+
+export const httpAgent = config.proxy
+ ? new HttpProxyAgent(config.proxy)
+ : new http.Agent({
+ keepAlive: true,
+ keepAliveMsecs: 30 * 1000,
+ });
+
+export const httpsAgent = config.proxy
+ ? new HttpsProxyAgent(config.proxy)
+ : new https.Agent({
+ keepAlive: true,
+ keepAliveMsecs: 30 * 1000,
+ lookup: cache.lookup,
+ });