mirror of
https://github.com/louislam/uptime-kuma.git
synced 2024-11-30 18:24:03 +00:00
50 lines
1.3 KiB
JavaScript
50 lines
1.3 KiB
JavaScript
const https = require("https");
|
|
const http = require("http");
|
|
const CacheableLookup = require("cacheable-lookup");
|
|
|
|
class CacheableDnsHttpAgent {
|
|
|
|
static cacheable = new CacheableLookup();
|
|
|
|
static httpAgentList = {};
|
|
static httpsAgentList = {};
|
|
|
|
/**
|
|
* Register cacheable to global agents
|
|
*/
|
|
static registerGlobalAgent() {
|
|
this.cacheable.install(http.globalAgent);
|
|
this.cacheable.install(https.globalAgent);
|
|
}
|
|
|
|
/**
|
|
* @var {https.AgentOptions} agentOptions
|
|
* @return {https.Agent}
|
|
*/
|
|
static getHttpsAgent(agentOptions) {
|
|
let key = JSON.stringify(agentOptions);
|
|
if (!(key in this.httpsAgentList)) {
|
|
this.httpsAgentList[key] = new https.Agent(agentOptions);
|
|
this.cacheable.install(this.httpsAgentList[key]);
|
|
}
|
|
return this.httpsAgentList[key];
|
|
}
|
|
|
|
/**
|
|
* @var {http.AgentOptions} agentOptions
|
|
* @return {https.Agents}
|
|
*/
|
|
static getHttpAgent(agentOptions) {
|
|
let key = JSON.stringify(agentOptions);
|
|
if (!(key in this.httpAgentList)) {
|
|
this.httpAgentList[key] = new http.Agent(agentOptions);
|
|
this.cacheable.install(this.httpAgentList[key]);
|
|
}
|
|
return this.httpAgentList[key];
|
|
}
|
|
|
|
}
|
|
|
|
module.exports = {
|
|
CacheableDnsHttpAgent,
|
|
};
|