2020-04-09 08:42:23 -06:00
|
|
|
import * as http from 'http';
|
|
|
|
import * as https from 'https';
|
2021-08-16 02:44:43 -06:00
|
|
|
import CacheableLookup from 'cacheable-lookup';
|
2020-04-09 08:42:23 -06:00
|
|
|
import fetch, { HeadersInit } from 'node-fetch';
|
2021-08-16 02:44:43 -06:00
|
|
|
import { HttpProxyAgent, HttpsProxyAgent } from 'hpagent';
|
2021-08-19 06:55:45 -06:00
|
|
|
import config from '@/config/index';
|
2020-10-17 10:46:40 -06:00
|
|
|
import { URL } from 'url';
|
2020-04-09 08:42:23 -06:00
|
|
|
|
|
|
|
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 05:32:34 -06:00
|
|
|
agent: getAgentByUrl,
|
2020-04-09 08:42:23 -06:00
|
|
|
});
|
|
|
|
|
|
|
|
if (!res.ok) {
|
|
|
|
throw {
|
|
|
|
name: `StatusError`,
|
|
|
|
statusCode: res.status,
|
|
|
|
message: `${res.status} ${res.statusText}`,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
return await res.json();
|
|
|
|
}
|
|
|
|
|
2020-07-25 20:04:07 -06:00
|
|
|
export async function getHtml(url: string, accept = 'text/html, */*', timeout = 10000, headers?: HeadersInit) {
|
|
|
|
const res = await fetch(url, {
|
|
|
|
headers: Object.assign({
|
|
|
|
'User-Agent': config.userAgent,
|
|
|
|
Accept: accept
|
|
|
|
}, headers || {}),
|
|
|
|
timeout,
|
|
|
|
agent: getAgentByUrl,
|
|
|
|
});
|
|
|
|
|
|
|
|
if (!res.ok) {
|
|
|
|
throw {
|
|
|
|
name: `StatusError`,
|
|
|
|
statusCode: res.status,
|
|
|
|
message: `${res.status} ${res.statusText}`,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
return await res.text();
|
|
|
|
}
|
|
|
|
|
2021-08-16 02:44:43 -06:00
|
|
|
const cache = new CacheableLookup({
|
|
|
|
maxTtl: 3600, // 1hours
|
|
|
|
errorTtl: 30, // 30secs
|
|
|
|
lookup: false, // nativeのdns.lookupにfallbackしない
|
|
|
|
});
|
|
|
|
|
2020-04-12 05:32:34 -06:00
|
|
|
/**
|
|
|
|
* Get http non-proxy agent
|
|
|
|
*/
|
|
|
|
const _http = new http.Agent({
|
|
|
|
keepAlive: true,
|
|
|
|
keepAliveMsecs: 30 * 1000,
|
2021-05-19 01:11:47 -06:00
|
|
|
lookup: cache.lookup,
|
|
|
|
} as http.AgentOptions);
|
2020-04-12 05:32:34 -06:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get https non-proxy agent
|
|
|
|
*/
|
|
|
|
const _https = new https.Agent({
|
|
|
|
keepAlive: true,
|
|
|
|
keepAliveMsecs: 30 * 1000,
|
|
|
|
lookup: cache.lookup,
|
2021-08-16 02:44:43 -06:00
|
|
|
} as https.AgentOptions);
|
|
|
|
|
|
|
|
const maxSockets = Math.max(256, config.deliverJobConcurrency || 128);
|
2020-04-12 05:32:34 -06:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get http proxy or non-proxy agent
|
|
|
|
*/
|
2020-04-09 08:42:23 -06:00
|
|
|
export const httpAgent = config.proxy
|
2021-08-16 02:44:43 -06:00
|
|
|
? new HttpProxyAgent({
|
|
|
|
keepAlive: true,
|
|
|
|
keepAliveMsecs: 30 * 1000,
|
|
|
|
maxSockets,
|
|
|
|
maxFreeSockets: 256,
|
|
|
|
scheduling: 'lifo',
|
|
|
|
proxy: config.proxy
|
|
|
|
})
|
2020-04-12 05:32:34 -06:00
|
|
|
: _http;
|
2020-04-09 08:42:23 -06:00
|
|
|
|
2020-04-12 05:32:34 -06:00
|
|
|
/**
|
|
|
|
* Get https proxy or non-proxy agent
|
|
|
|
*/
|
2020-04-09 08:42:23 -06:00
|
|
|
export const httpsAgent = config.proxy
|
2021-08-16 02:44:43 -06:00
|
|
|
? new HttpsProxyAgent({
|
|
|
|
keepAlive: true,
|
|
|
|
keepAliveMsecs: 30 * 1000,
|
|
|
|
maxSockets,
|
|
|
|
maxFreeSockets: 256,
|
|
|
|
scheduling: 'lifo',
|
|
|
|
proxy: config.proxy
|
|
|
|
})
|
2020-04-12 05:32:34 -06: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;
|
|
|
|
}
|
|
|
|
}
|