refactor(browser): reuse CDP fetch helpers

This commit is contained in:
Peter Steinberger
2026-02-15 18:27:02 +00:00
parent 394e69a2f8
commit a2c695126d

View File

@@ -10,7 +10,8 @@ import type {
ProfileRuntimeState,
ProfileStatus,
} from "./server-context.types.js";
import { appendCdpPath, createTargetViaCdp, getHeadersWithAuth, normalizeCdpWsUrl } from "./cdp.js";
import { fetchJson, fetchOk } from "./cdp.helpers.js";
import { appendCdpPath, createTargetViaCdp, normalizeCdpWsUrl } from "./cdp.js";
import {
isChromeCdpReady,
isChromeReachable,
@@ -62,35 +63,6 @@ function normalizeWsUrl(raw: string | undefined, cdpBaseUrl: string): string | u
}
}
async function fetchJson<T>(url: string, timeoutMs = 1500, init?: RequestInit): Promise<T> {
const ctrl = new AbortController();
const t = setTimeout(ctrl.abort.bind(ctrl), timeoutMs);
try {
const headers = getHeadersWithAuth(url, (init?.headers as Record<string, string>) || {});
const res = await fetch(url, { ...init, headers, signal: ctrl.signal });
if (!res.ok) {
throw new Error(`HTTP ${res.status}`);
}
return (await res.json()) as T;
} finally {
clearTimeout(t);
}
}
async function fetchOk(url: string, timeoutMs = 1500, init?: RequestInit): Promise<void> {
const ctrl = new AbortController();
const t = setTimeout(ctrl.abort.bind(ctrl), timeoutMs);
try {
const headers = getHeadersWithAuth(url, (init?.headers as Record<string, string>) || {});
const res = await fetch(url, { ...init, headers, signal: ctrl.signal });
if (!res.ok) {
throw new Error(`HTTP ${res.status}`);
}
} finally {
clearTimeout(t);
}
}
/**
* Create a profile-scoped context for browser operations.
*/