monkeeShark/packages/backend/src/core/HttpRequestService.ts

161 lines
3.7 KiB
TypeScript
Raw Normal View History

2022-09-17 18:27:08 +00:00
import * as http from 'node:http';
import * as https from 'node:https';
import CacheableLookup from 'cacheable-lookup';
import fetch from 'node-fetch';
import { HttpProxyAgent, HttpsProxyAgent } from 'hpagent';
import { Inject, Injectable } from '@nestjs/common';
import { DI } from '@/di-symbols.js';
2022-09-20 20:33:11 +00:00
import type { Config } from '@/config.js';
2022-09-17 18:27:08 +00:00
import { StatusError } from '@/misc/status-error.js';
import { bindThis } from '@/decorators.js';
2022-09-17 18:27:08 +00:00
import type { Response } from 'node-fetch';
import type { URL } from 'node:url';
@Injectable()
export class HttpRequestService {
/**
* Get http non-proxy agent
*/
2022-09-18 18:11:50 +00:00
private http: http.Agent;
2022-09-17 18:27:08 +00:00
/**
* Get https non-proxy agent
*/
2022-09-18 18:11:50 +00:00
private https: https.Agent;
2022-09-17 18:27:08 +00:00
/**
* Get http proxy or non-proxy agent
*/
public httpAgent: http.Agent;
/**
* Get https proxy or non-proxy agent
*/
public httpsAgent: https.Agent;
constructor(
@Inject(DI.config)
private config: Config,
) {
const cache = new CacheableLookup({
maxTtl: 3600, // 1hours
errorTtl: 30, // 30secs
lookup: false, // nativeのdns.lookupにfallbackしない
});
2022-09-18 18:11:50 +00:00
this.http = new http.Agent({
2022-09-17 18:27:08 +00:00
keepAlive: true,
keepAliveMsecs: 30 * 1000,
lookup: cache.lookup,
} as http.AgentOptions);
2022-09-18 18:11:50 +00:00
this.https = new https.Agent({
2022-09-17 18:27:08 +00:00
keepAlive: true,
keepAliveMsecs: 30 * 1000,
lookup: cache.lookup,
} as https.AgentOptions);
const maxSockets = Math.max(256, config.deliverJobConcurrency ?? 128);
this.httpAgent = config.proxy
? new HttpProxyAgent({
keepAlive: true,
keepAliveMsecs: 30 * 1000,
maxSockets,
maxFreeSockets: 256,
scheduling: 'lifo',
proxy: config.proxy,
})
2022-09-18 18:11:50 +00:00
: this.http;
2022-09-17 18:27:08 +00:00
this.httpsAgent = config.proxy
? new HttpsProxyAgent({
keepAlive: true,
keepAliveMsecs: 30 * 1000,
maxSockets,
maxFreeSockets: 256,
scheduling: 'lifo',
proxy: config.proxy,
})
2022-09-18 18:11:50 +00:00
: this.https;
2022-09-17 18:27:08 +00:00
}
/**
* Get agent by URL
* @param url URL
* @param bypassProxy Allways bypass proxy
*/
@bindThis
2022-09-17 18:27:08 +00:00
public getAgentByUrl(url: URL, bypassProxy = false): http.Agent | https.Agent {
if (bypassProxy || (this.config.proxyBypassHosts || []).includes(url.hostname)) {
2022-09-18 18:11:50 +00:00
return url.protocol === 'http:' ? this.http : this.https;
2022-09-17 18:27:08 +00:00
} else {
return url.protocol === 'http:' ? this.httpAgent : this.httpsAgent;
}
}
@bindThis
2022-09-17 18:27:08 +00:00
public async getJson(url: string, accept = 'application/json, */*', timeout = 10000, headers?: Record<string, string>): Promise<unknown> {
const res = await this.getResponse({
url,
method: 'GET',
headers: Object.assign({
'User-Agent': this.config.userAgent,
Accept: accept,
}, headers ?? {}),
timeout,
2022-12-25 06:02:47 +00:00
size: 1024 * 256,
2022-09-17 18:27:08 +00:00
});
return await res.json();
}
@bindThis
2022-09-17 18:27:08 +00:00
public async getHtml(url: string, accept = 'text/html, */*', timeout = 10000, headers?: Record<string, string>): Promise<string> {
const res = await this.getResponse({
url,
method: 'GET',
headers: Object.assign({
'User-Agent': this.config.userAgent,
Accept: accept,
}, headers ?? {}),
timeout,
});
return await res.text();
}
@bindThis
2022-09-17 18:27:08 +00:00
public async getResponse(args: {
url: string,
method: string,
body?: string,
headers: Record<string, string>,
timeout?: number,
size?: number,
}): Promise<Response> {
const timeout = args.timeout ?? 10 * 1000;
const controller = new AbortController();
setTimeout(() => {
controller.abort();
}, timeout * 6);
const res = await fetch(args.url, {
method: args.method,
headers: args.headers,
body: args.body,
timeout,
size: args.size ?? 10 * 1024 * 1024,
agent: (url) => this.getAgentByUrl(url),
signal: controller.signal,
});
if (!res.ok) {
throw new StatusError(`${res.status} ${res.statusText}`, res.status, res.statusText);
}
return res;
}
}