|
1 | 1 | // HTTP probe for OpenWebUI E2E scenarios. |
| 2 | +import { pathToFileURL } from "node:url"; |
2 | 3 | import { readPositiveIntEnv } from "../env-limits.mjs"; |
3 | 4 | |
4 | | -const [url, expectedRaw = "200"] = process.argv.slice(2); |
5 | | -if (!url) { |
6 | | -throw new Error("usage: http-probe.mjs <url> [status|lt500]"); |
7 | | -} |
8 | | - |
9 | 5 | function parseExpectedStatus(raw) { |
10 | 6 | if (!/^[1-5]\d\d$/u.test(raw)) { |
11 | 7 | throw new Error(`expected status must be lt500 or a decimal HTTP status. Got: ${raw}`); |
12 | 8 | } |
13 | 9 | return Number(raw); |
14 | 10 | } |
15 | 11 | |
16 | | -const timeoutMs = readPositiveIntEnv("OPENCLAW_HTTP_PROBE_TIMEOUT_MS", 30_000); |
17 | | -const expectedStatus = expectedRaw === "lt500" ? undefined : parseExpectedStatus(expectedRaw); |
18 | | -const controller = new AbortController(); |
19 | | -const timer = setTimeout(() => controller.abort(), timeoutMs); |
20 | | - |
21 | | -try { |
| 12 | +export async function probeHttpStatus({ |
| 13 | + url, |
| 14 | + expectedRaw = "200", |
| 15 | + timeoutMs = 30_000, |
| 16 | + bearer = "", |
| 17 | + fetchImpl = fetch, |
| 18 | +}) { |
| 19 | +if (!url) { |
| 20 | +throw new Error("usage: http-probe.mjs <url> [status|lt500]"); |
| 21 | +} |
| 22 | +const expectedStatus = expectedRaw === "lt500" ? undefined : parseExpectedStatus(expectedRaw); |
| 23 | +const controller = new AbortController(); |
| 24 | +const timer = setTimeout(() => controller.abort(), timeoutMs); |
| 25 | +let res; |
22 | 26 | const headers = {}; |
23 | | -if (process.env.OPENCLAW_HTTP_PROBE_BEARER) { |
24 | | -headers.authorization = `Bearer ${process.env.OPENCLAW_HTTP_PROBE_BEARER}`; |
| 27 | +if (bearer) { |
| 28 | +headers.authorization = `Bearer ${bearer}`; |
| 29 | +} |
| 30 | + |
| 31 | +try { |
| 32 | +res = await fetchImpl(url, { headers, signal: controller.signal }).catch(() => null); |
| 33 | +return expectedRaw === "lt500" |
| 34 | + ? Boolean(res && res.status < 500) |
| 35 | + : res?.status === expectedStatus; |
| 36 | +} finally { |
| 37 | +clearTimeout(timer); |
| 38 | +await res?.body?.cancel?.().catch(() => undefined); |
25 | 39 | } |
26 | | -const res = await fetch(url, { headers, signal: controller.signal }).catch(() => null); |
27 | | -const ok = |
28 | | -expectedRaw === "lt500" ? Boolean(res && res.status < 500) : res?.status === expectedStatus; |
| 40 | +} |
| 41 | + |
| 42 | +async function main() { |
| 43 | +const [url, expectedRaw = "200"] = process.argv.slice(2); |
| 44 | +const ok = await probeHttpStatus({ |
| 45 | + url, |
| 46 | + expectedRaw, |
| 47 | +timeoutMs: readPositiveIntEnv("OPENCLAW_HTTP_PROBE_TIMEOUT_MS", 30_000), |
| 48 | +bearer: process.env.OPENCLAW_HTTP_PROBE_BEARER, |
| 49 | +}); |
29 | 50 | process.exit(ok ? 0 : 1); |
30 | | -} finally { |
31 | | -clearTimeout(timer); |
| 51 | +} |
| 52 | + |
| 53 | +if (process.argv[1] && import.meta.url === pathToFileURL(process.argv[1]).href) { |
| 54 | +await main(); |
32 | 55 | } |