import type { ConcreteNetwork } from "@fetcha/core"; import type { ProviderHealth, ProxyProvider } from "./types"; /** Shared health probe: fetch the provider's own IP-echo endpoint through the network. */ export async function probeHealth(provider: ProxyProvider, network: ConcreteNetwork, probeUrl: string): Promise { const t0 = performance.now(); try { const res = await provider.fetch({ requestId: "probe", attemptId: "probe", url: probeUrl, method: "GET", headers: { accept: "application/json" }, timeoutMs: 15_000, network, geo: { country: null, region: null, city: null }, followRedirects: true, maxRedirects: 3, maxResponseBytes: 65_536, }); const latencyMs = Math.round(performance.now() - t0); if (res.status >= 200 && res.status < 400) { return { provider: provider.id, network, status: latencyMs > 6000 ? "degraded" : "healthy", latencyMs, checkedAt: new Date() }; } return { provider: provider.id, network, status: "degraded", latencyMs, checkedAt: new Date(), detail: `probe http ${res.status}` }; } catch (e) { return { provider: provider.id, network, status: "down", latencyMs: null, checkedAt: new Date(), detail: (e as Error).message }; } }