mirror of
https://github.com/louislam/uptime-kuma.git
synced 2024-12-23 23:50:35 -06:00
51 lines
1.3 KiB
JavaScript
51 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,
|
|
};
|