2023-01-12 21:40:33 -07:00
|
|
|
import * as http from "node:http";
|
|
|
|
import * as https from "node:https";
|
|
|
|
import type { URL } from "node:url";
|
|
|
|
import CacheableLookup from "cacheable-lookup";
|
|
|
|
import fetch from "node-fetch";
|
|
|
|
import { HttpProxyAgent, HttpsProxyAgent } from "hpagent";
|
|
|
|
import config from "@/config/index.js";
|
|
|
|
|
|
|
|
export async function getJson(
|
|
|
|
url: string,
|
|
|
|
accept = "application/json, */*",
|
|
|
|
timeout = 10000,
|
|
|
|
headers?: Record<string, string>,
|
|
|
|
) {
|
2021-10-16 02:16:24 -06:00
|
|
|
const res = await getResponse({
|
|
|
|
url,
|
2023-01-12 21:40:33 -07:00
|
|
|
method: "GET",
|
|
|
|
headers: Object.assign(
|
|
|
|
{
|
|
|
|
"User-Agent": config.userAgent,
|
|
|
|
Accept: accept,
|
|
|
|
},
|
|
|
|
headers || {},
|
|
|
|
),
|
2021-12-09 07:58:30 -07:00
|
|
|
timeout,
|
2020-04-09 08:42:23 -06:00
|
|
|
});
|
|
|
|
|
|
|
|
return await res.json();
|
|
|
|
}
|
|
|
|
|
2023-01-12 21:40:33 -07:00
|
|
|
export async function getHtml(
|
|
|
|
url: string,
|
|
|
|
accept = "text/html, */*",
|
|
|
|
timeout = 10000,
|
|
|
|
headers?: Record<string, string>,
|
|
|
|
) {
|
2021-10-16 02:16:24 -06:00
|
|
|
const res = await getResponse({
|
|
|
|
url,
|
2023-01-12 21:40:33 -07:00
|
|
|
method: "GET",
|
|
|
|
headers: Object.assign(
|
|
|
|
{
|
|
|
|
"User-Agent": config.userAgent,
|
|
|
|
Accept: accept,
|
|
|
|
},
|
|
|
|
headers || {},
|
|
|
|
),
|
2021-12-09 07:58:30 -07:00
|
|
|
timeout,
|
2021-10-16 02:16:24 -06:00
|
|
|
});
|
|
|
|
|
|
|
|
return await res.text();
|
|
|
|
}
|
|
|
|
|
2023-01-12 21:40:33 -07:00
|
|
|
export async function getResponse(args: {
|
|
|
|
url: string;
|
|
|
|
method: string;
|
|
|
|
body?: string;
|
|
|
|
headers: Record<string, string>;
|
|
|
|
timeout?: number;
|
|
|
|
size?: number;
|
|
|
|
}) {
|
2022-04-16 02:19:30 -06:00
|
|
|
const timeout = args.timeout || 10 * 1000;
|
2021-10-16 02:16:24 -06:00
|
|
|
|
|
|
|
const controller = new AbortController();
|
|
|
|
setTimeout(() => {
|
|
|
|
controller.abort();
|
|
|
|
}, timeout * 6);
|
|
|
|
|
|
|
|
const res = await fetch(args.url, {
|
|
|
|
method: args.method,
|
|
|
|
headers: args.headers,
|
|
|
|
body: args.body,
|
2020-07-25 20:04:07 -06:00
|
|
|
timeout,
|
2022-04-16 02:19:30 -06:00
|
|
|
size: args.size || 10 * 1024 * 1024,
|
2020-07-25 20:04:07 -06:00
|
|
|
agent: getAgentByUrl,
|
2021-10-16 02:16:24 -06:00
|
|
|
signal: controller.signal,
|
2020-07-25 20:04:07 -06:00
|
|
|
});
|
|
|
|
|
|
|
|
if (!res.ok) {
|
2023-01-12 21:40:33 -07:00
|
|
|
throw new StatusError(
|
|
|
|
`${res.status} ${res.statusText}`,
|
|
|
|
res.status,
|
|
|
|
res.statusText,
|
|
|
|
);
|
2020-07-25 20:04:07 -06:00
|
|
|
}
|
|
|
|
|
2021-10-16 02:16:24 -06:00
|
|
|
return res;
|
2020-07-25 20:04:07 -06:00
|
|
|
}
|
|
|
|
|
2021-08-16 02:44:43 -06:00
|
|
|
const cache = new CacheableLookup({
|
2023-01-12 21:40:33 -07:00
|
|
|
maxTtl: 3600, // 1hours
|
|
|
|
errorTtl: 30, // 30secs
|
|
|
|
lookup: false, // nativeのdns.lookupにfallbackしない
|
2021-08-16 02:44:43 -06:00
|
|
|
});
|
|
|
|
|
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({
|
2023-01-12 21:40:33 -07:00
|
|
|
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({
|
2023-01-12 21:40:33 -07:00
|
|
|
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)) {
|
2023-01-12 21:40:33 -07:00
|
|
|
return url.protocol === "http:" ? _http : _https;
|
2020-04-12 05:32:34 -06:00
|
|
|
} else {
|
2023-01-12 21:40:33 -07:00
|
|
|
return url.protocol === "http:" ? httpAgent : httpsAgent;
|
2020-04-12 05:32:34 -06:00
|
|
|
}
|
|
|
|
}
|
2021-10-16 02:16:24 -06:00
|
|
|
|
|
|
|
export class StatusError extends Error {
|
|
|
|
public statusCode: number;
|
|
|
|
public statusMessage?: string;
|
|
|
|
public isClientError: boolean;
|
|
|
|
|
|
|
|
constructor(message: string, statusCode: number, statusMessage?: string) {
|
|
|
|
super(message);
|
2023-01-12 21:40:33 -07:00
|
|
|
this.name = "StatusError";
|
2021-10-16 02:16:24 -06:00
|
|
|
this.statusCode = statusCode;
|
|
|
|
this.statusMessage = statusMessage;
|
2023-01-12 21:40:33 -07:00
|
|
|
this.isClientError =
|
|
|
|
typeof this.statusCode === "number" &&
|
|
|
|
this.statusCode >= 400 &&
|
|
|
|
this.statusCode < 500;
|
2021-10-16 02:16:24 -06:00
|
|
|
}
|
|
|
|
}
|