fix(crestodian): cancel gateway probe bodies

This commit is contained in:
Vincent Koc
2026-06-19 09:32:43 +02:00
parent 7cc66b5175
commit dfc5bd5fcc
2 changed files with 26 additions and 1 deletions
+23
View File
@@ -6,6 +6,7 @@ import { probeGatewayUrl, probeLocalCommand } from "./probes.js";
describe("crestodian probes", () => {
afterEach(() => {
vi.restoreAllMocks();
vi.unstubAllGlobals();
});
it("bounds noisy local command probe output", async () => {
@@ -54,4 +55,26 @@ describe("crestodian probes", () => {
expect(timeoutSpy).toHaveBeenCalledWith(expect.any(Function), MAX_TIMER_TIMEOUT_MS);
});
it("cancels gateway health response bodies", async () => {
const cancel = vi.fn(async () => undefined);
vi.stubGlobal(
"fetch",
vi.fn(
async () =>
({
ok: false,
statusText: "Service Unavailable",
body: { cancel },
}) as unknown as Response,
),
);
await expect(probeGatewayUrl("ws://127.0.0.1:1234")).resolves.toEqual({
reachable: false,
url: "ws://127.0.0.1:1234",
error: "Service Unavailable",
});
expect(cancel).toHaveBeenCalledTimes(1);
});
});
+3 -1
View File
@@ -116,8 +116,9 @@ export async function probeGatewayUrl(
const timeoutMs = resolveTimerTimeoutMs(opts.timeoutMs, 900);
const controller = new AbortController();
const timeout = setTimeout(() => controller.abort(), timeoutMs);
let response: Response | undefined;
try {
const response = await fetch(healthUrl, {
response = await fetch(healthUrl, {
method: "GET",
signal: controller.signal,
});
@@ -130,5 +131,6 @@ export async function probeGatewayUrl(
};
} finally {
clearTimeout(timeout);
await response?.body?.cancel().catch(() => undefined);
}
}