2020-04-09 23:42:23 +09:00
|
|
|
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,
|
2020-04-12 20:32:34 +09:00
|
|
|
agent: getAgentByUrl,
|
2020-04-09 23:42:23 +09:00
|
|
|
});
|
|
|
|
|
|
|
|
if (!res.ok) {
|
|
|
|
throw {
|
|
|
|
name: `StatusError`,
|
|
|
|
statusCode: res.status,
|
|
|
|
message: `${res.status} ${res.statusText}`,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
return await res.json();
|
|
|
|
}
|
|
|
|
|
2020-04-12 20:32:34 +09:00
|
|
|
/**
|
|
|
|
* Get http non-proxy agent
|
|
|
|
*/
|
|
|
|
const _http = new http.Agent({
|
|
|
|
keepAlive: true,
|
|
|
|
keepAliveMsecs: 30 * 1000,
|
|
|
|
});
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get https non-proxy agent
|
|
|
|
*/
|
|
|
|
const _https = new https.Agent({
|
|
|
|
keepAlive: true,
|
|
|
|
keepAliveMsecs: 30 * 1000,
|
|
|
|
lookup: cache.lookup,
|
|
|
|
});
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get http proxy or non-proxy agent
|
|
|
|
*/
|
2020-04-09 23:42:23 +09:00
|
|
|
export const httpAgent = config.proxy
|
|
|
|
? new HttpProxyAgent(config.proxy)
|
2020-04-12 20:32:34 +09:00
|
|
|
: _http;
|
2020-04-09 23:42:23 +09:00
|
|
|
|
2020-04-12 20:32:34 +09:00
|
|
|
/**
|
|
|
|
* Get https proxy or non-proxy agent
|
|
|
|
*/
|
2020-04-09 23:42:23 +09:00
|
|
|
export const httpsAgent = config.proxy
|
|
|
|
? new HttpsProxyAgent(config.proxy)
|
2020-04-12 20:32:34 +09:00
|
|
|
: _https;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get agent by URL
|
|
|
|
* @param url URL
|
|
|
|
* @param bypassProxy Allways bypass proxy
|
|
|
|
*/
|
|
|
|
export function getAgentByUrl(url: URL, bypassProxy = false) {
|
|
|
|
if (bypassProxy || (config.proxyBypassHosts || []).includes(url.hostname)) {
|
|
|
|
return url.protocol == 'http:' ? _http : _https;
|
|
|
|
} else {
|
|
|
|
return url.protocol == 'http:' ? httpAgent : httpsAgent;
|
|
|
|
}
|
|
|
|
}
|