2026-03-04 23:24:48 +00:00
|
|
|
import { type Got, HTTPError, RequestError } from "got";
|
2026-03-20 11:29:44 -07:00
|
|
|
import { IAdvancedMatcher } from "@/service/infrastructure/network/AdvancedMatcher.js";
|
2026-03-04 23:24:48 +00:00
|
|
|
import { IStatusProvider } from "@/service/infrastructure/network/IStatusProvider.js";
|
|
|
|
|
import { HttpStatusPayload } from "@/types/network.js";
|
|
|
|
|
import { MonitorStatusResponse } from "@/types/network.js";
|
|
|
|
|
import { Agent as HttpsAgent } from "https";
|
2026-06-03 11:48:59 -07:00
|
|
|
import { Agent as HttpAgent } from "http";
|
2026-03-05 17:54:16 +00:00
|
|
|
import { Monitor, MonitorType } from "@/types/monitor.js";
|
2026-06-04 22:43:43 +05:30
|
|
|
import { NETWORK_ERROR, isStatusUp } from "@/service/infrastructure/network/utils.js";
|
2026-03-05 19:34:57 +00:00
|
|
|
import CacheableLookup from "cacheable-lookup";
|
2026-03-04 23:24:48 +00:00
|
|
|
|
|
|
|
|
export class HttpProvider implements IStatusProvider<HttpStatusPayload> {
|
|
|
|
|
readonly type = "http";
|
|
|
|
|
|
2026-06-03 11:48:59 -07:00
|
|
|
// Shared, pooled agents reused across every check
|
|
|
|
|
private readonly httpAgent: HttpAgent;
|
|
|
|
|
private readonly httpsAgent: HttpsAgent;
|
|
|
|
|
private readonly httpsAgentInsecure: HttpsAgent;
|
|
|
|
|
|
2026-03-04 23:24:48 +00:00
|
|
|
constructor(
|
|
|
|
|
private got: Got,
|
2026-03-20 11:29:44 -07:00
|
|
|
private advancedMatcher: IAdvancedMatcher
|
2026-03-05 19:34:57 +00:00
|
|
|
) {
|
2026-03-10 19:04:04 -04:00
|
|
|
const cacheable = new CacheableLookup({ maxTtl: 300, errorTtl: 30 });
|
2026-03-05 19:34:57 +00:00
|
|
|
this.got = got.extend({
|
|
|
|
|
dnsCache: cacheable,
|
|
|
|
|
timeout: {
|
|
|
|
|
request: 30000,
|
|
|
|
|
},
|
|
|
|
|
retry: { limit: 1 },
|
|
|
|
|
});
|
2026-06-03 11:48:59 -07:00
|
|
|
|
|
|
|
|
const agentOptions = { keepAlive: true, maxSockets: 256, maxFreeSockets: 256 };
|
|
|
|
|
this.httpAgent = new HttpAgent(agentOptions);
|
|
|
|
|
this.httpsAgent = new HttpsAgent({ ...agentOptions, rejectUnauthorized: true });
|
|
|
|
|
this.httpsAgentInsecure = new HttpsAgent({ ...agentOptions, rejectUnauthorized: false });
|
2026-03-05 19:34:57 +00:00
|
|
|
}
|
2026-03-04 23:24:48 +00:00
|
|
|
|
2026-03-05 17:54:16 +00:00
|
|
|
supports(type: MonitorType) {
|
2026-03-04 23:24:48 +00:00
|
|
|
return type === "http";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private handleHttpError<T>(error: unknown, monitor: Monitor): MonitorStatusResponse<T> {
|
|
|
|
|
if (error instanceof HTTPError || error instanceof RequestError) {
|
2026-06-03 23:27:10 +05:30
|
|
|
const statusCode = error.response?.statusCode;
|
2026-06-04 22:43:43 +05:30
|
|
|
const statusUp = isStatusUp(statusCode, monitor.customUpCodes);
|
2026-06-05 00:58:35 +05:30
|
|
|
const responseTime = error.timings?.phases?.firstByte ?? error.timings?.phases?.total ?? 0;
|
2026-06-05 00:35:30 +05:30
|
|
|
|
|
|
|
|
if (!statusUp) {
|
|
|
|
|
return {
|
|
|
|
|
monitorId: monitor.id,
|
|
|
|
|
teamId: monitor.teamId,
|
|
|
|
|
type: monitor.type,
|
|
|
|
|
status: false,
|
|
|
|
|
code: statusCode ?? NETWORK_ERROR,
|
|
|
|
|
message: error.message,
|
|
|
|
|
responseTime,
|
|
|
|
|
timings: error.timings,
|
|
|
|
|
payload: null as T,
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const { jsonPath } = monitor;
|
|
|
|
|
const body = error.response?.body ?? "";
|
|
|
|
|
const contentType = error.response?.headers?.["content-type"] || "";
|
|
|
|
|
const isJson = contentType.includes("application/json");
|
|
|
|
|
|
|
|
|
|
if (jsonPath && !isJson) {
|
|
|
|
|
return {
|
|
|
|
|
monitorId: monitor.id,
|
|
|
|
|
teamId: monitor.teamId,
|
|
|
|
|
type: monitor.type,
|
|
|
|
|
status: false,
|
|
|
|
|
code: statusCode ?? NETWORK_ERROR,
|
|
|
|
|
message: "Response is not JSON",
|
|
|
|
|
responseTime,
|
|
|
|
|
timings: error.timings,
|
|
|
|
|
payload: body as unknown as T,
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let payload: T;
|
|
|
|
|
if (isJson) {
|
|
|
|
|
try {
|
|
|
|
|
payload = JSON.parse(body) as T;
|
|
|
|
|
} catch {
|
|
|
|
|
payload = body as unknown as T;
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
payload = body as unknown as T;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const matchResult = this.advancedMatcher.validate<T>(payload, monitor);
|
2026-06-01 00:03:03 +05:30
|
|
|
|
2026-03-04 23:24:48 +00:00
|
|
|
return {
|
|
|
|
|
monitorId: monitor.id,
|
|
|
|
|
teamId: monitor.teamId,
|
|
|
|
|
type: monitor.type,
|
2026-06-05 00:35:30 +05:30
|
|
|
status: statusUp && matchResult.ok,
|
2026-06-03 23:27:10 +05:30
|
|
|
code: statusCode ?? NETWORK_ERROR,
|
2026-06-05 00:35:30 +05:30
|
|
|
message: matchResult.ok ? error.message : matchResult.message,
|
|
|
|
|
responseTime,
|
2026-03-04 23:24:48 +00:00
|
|
|
timings: error.timings,
|
2026-06-05 00:35:30 +05:30
|
|
|
payload,
|
|
|
|
|
extracted: matchResult.extracted,
|
2026-03-04 23:24:48 +00:00
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
monitorId: monitor.id,
|
|
|
|
|
teamId: monitor.teamId,
|
|
|
|
|
type: monitor.type,
|
|
|
|
|
status: false,
|
|
|
|
|
code: NETWORK_ERROR,
|
|
|
|
|
message: error instanceof Error ? error.message : String(error),
|
|
|
|
|
responseTime: 0,
|
|
|
|
|
payload: null as T,
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async handle<T>(monitor: Monitor): Promise<MonitorStatusResponse<T>> {
|
2026-03-04 23:36:36 +00:00
|
|
|
const { url, secret, jsonPath, ignoreTlsErrors } = monitor;
|
|
|
|
|
|
|
|
|
|
if (!url) {
|
|
|
|
|
throw new Error("URL is required for HTTP monitor");
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-04 23:24:48 +00:00
|
|
|
const options: Record<string, unknown> = {
|
|
|
|
|
headers: monitor.secret ? { Authorization: `Bearer ${secret}` } : undefined,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
options.agent = {
|
2026-06-03 11:48:59 -07:00
|
|
|
http: this.httpAgent,
|
|
|
|
|
https: ignoreTlsErrors ? this.httpsAgentInsecure : this.httpsAgent,
|
2026-03-04 23:24:48 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
const response = await this.got<string>(url, options);
|
2026-03-04 23:36:36 +00:00
|
|
|
const contentType = response.headers["content-type"] || "";
|
|
|
|
|
const isJson = contentType.includes("application/json");
|
2026-03-04 23:24:48 +00:00
|
|
|
|
2026-03-04 23:36:36 +00:00
|
|
|
if (jsonPath && !isJson) {
|
|
|
|
|
return {
|
|
|
|
|
monitorId: monitor.id,
|
|
|
|
|
teamId: monitor.teamId,
|
|
|
|
|
type: monitor.type,
|
|
|
|
|
status: false,
|
|
|
|
|
code: response.statusCode,
|
|
|
|
|
message: "Response is not JSON",
|
2026-06-03 11:48:59 -07:00
|
|
|
responseTime: response.timings.phases.firstByte ?? 0,
|
2026-03-04 23:36:36 +00:00
|
|
|
timings: response.timings,
|
|
|
|
|
payload: response.body as unknown as T,
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let payload: T;
|
2026-03-04 23:24:48 +00:00
|
|
|
if (isJson) {
|
|
|
|
|
try {
|
|
|
|
|
payload = JSON.parse(response.body) as T;
|
|
|
|
|
} catch {
|
|
|
|
|
payload = response.body as unknown as T;
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
payload = response.body as unknown as T;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const matchResult = this.advancedMatcher.validate<T>(payload, monitor);
|
2026-06-04 22:43:43 +05:30
|
|
|
const statusUp = isStatusUp(response.statusCode, monitor.customUpCodes);
|
2026-03-04 23:24:48 +00:00
|
|
|
return {
|
|
|
|
|
monitorId: monitor.id,
|
|
|
|
|
teamId: monitor.teamId,
|
|
|
|
|
type: monitor.type,
|
2026-06-03 23:27:10 +05:30
|
|
|
status: statusUp && matchResult.ok,
|
2026-03-04 23:24:48 +00:00
|
|
|
code: response.statusCode,
|
|
|
|
|
message: matchResult.ok ? (response.statusMessage ?? "OK") : matchResult.message,
|
2026-06-03 11:48:59 -07:00
|
|
|
responseTime: response.timings.phases.firstByte ?? 0,
|
2026-03-04 23:24:48 +00:00
|
|
|
timings: response.timings,
|
|
|
|
|
payload,
|
|
|
|
|
extracted: matchResult.extracted,
|
|
|
|
|
};
|
|
|
|
|
} catch (error: unknown) {
|
|
|
|
|
return this.handleHttpError(error, monitor);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|