mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-09 07:07:39 +00:00
chore: Enable "curly" rule to avoid single-statement if confusion/errors.
This commit is contained in:
@@ -40,7 +40,9 @@ export type {
|
||||
* Normalize a CDP WebSocket URL to use the correct base URL.
|
||||
*/
|
||||
function normalizeWsUrl(raw: string | undefined, cdpBaseUrl: string): string | undefined {
|
||||
if (!raw) return undefined;
|
||||
if (!raw) {
|
||||
return undefined;
|
||||
}
|
||||
try {
|
||||
return normalizeCdpWsUrl(raw, cdpBaseUrl);
|
||||
} catch {
|
||||
@@ -54,7 +56,9 @@ async function fetchJson<T>(url: string, timeoutMs = 1500, init?: RequestInit):
|
||||
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}`);
|
||||
if (!res.ok) {
|
||||
throw new Error(`HTTP ${res.status}`);
|
||||
}
|
||||
return (await res.json()) as T;
|
||||
} finally {
|
||||
clearTimeout(t);
|
||||
@@ -67,7 +71,9 @@ async function fetchOk(url: string, timeoutMs = 1500, init?: RequestInit): Promi
|
||||
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}`);
|
||||
if (!res.ok) {
|
||||
throw new Error(`HTTP ${res.status}`);
|
||||
}
|
||||
} finally {
|
||||
clearTimeout(t);
|
||||
}
|
||||
@@ -82,7 +88,9 @@ function createProfileContext(
|
||||
): ProfileContext {
|
||||
const state = () => {
|
||||
const current = opts.getState();
|
||||
if (!current) throw new Error("Browser server not started");
|
||||
if (!current) {
|
||||
throw new Error("Browser server not started");
|
||||
}
|
||||
return current;
|
||||
};
|
||||
|
||||
@@ -170,7 +178,9 @@ function createProfileContext(
|
||||
while (Date.now() < deadline) {
|
||||
const tabs = await listTabs().catch(() => [] as BrowserTab[]);
|
||||
const found = tabs.find((t) => t.targetId === createdViaCdp);
|
||||
if (found) return found;
|
||||
if (found) {
|
||||
return found;
|
||||
}
|
||||
await new Promise((r) => setTimeout(r, 100));
|
||||
}
|
||||
return { targetId: createdViaCdp, title: "", url, type: "page" };
|
||||
@@ -201,7 +211,9 @@ function createProfileContext(
|
||||
throw err;
|
||||
});
|
||||
|
||||
if (!created.id) throw new Error("Failed to open tab (missing id)");
|
||||
if (!created.id) {
|
||||
throw new Error("Failed to open tab (missing id)");
|
||||
}
|
||||
const profileState = getProfileState();
|
||||
profileState.lastTargetId = created.id;
|
||||
return {
|
||||
@@ -214,7 +226,9 @@ function createProfileContext(
|
||||
};
|
||||
|
||||
const resolveRemoteHttpTimeout = (timeoutMs: number | undefined) => {
|
||||
if (profile.cdpIsLoopback) return timeoutMs ?? 300;
|
||||
if (profile.cdpIsLoopback) {
|
||||
return timeoutMs ?? 300;
|
||||
}
|
||||
const resolved = state().resolved;
|
||||
if (typeof timeoutMs === "number" && Number.isFinite(timeoutMs)) {
|
||||
return Math.max(Math.floor(timeoutMs), resolved.remoteCdpTimeoutMs);
|
||||
@@ -249,7 +263,9 @@ function createProfileContext(
|
||||
setProfileRunning(running);
|
||||
running.proc.on("exit", () => {
|
||||
// Guard against server teardown (e.g., SIGUSR1 restart)
|
||||
if (!opts.getState()) return;
|
||||
if (!opts.getState()) {
|
||||
return;
|
||||
}
|
||||
const profileState = getProfileState();
|
||||
if (profileState.running?.pid === running.pid) {
|
||||
setProfileRunning(null);
|
||||
@@ -282,7 +298,9 @@ function createProfileContext(
|
||||
}
|
||||
}
|
||||
|
||||
if (await isReachable(600)) return;
|
||||
if (await isReachable(600)) {
|
||||
return;
|
||||
}
|
||||
// Relay server is up, but no attached tab yet. Prompt user to attach.
|
||||
throw new Error(
|
||||
`Chrome extension relay is running, but no tab is connected. Click the OpenClaw Chrome extension icon on a tab to attach it (profile "${profile.name}").`,
|
||||
@@ -292,7 +310,9 @@ function createProfileContext(
|
||||
if (!httpReachable) {
|
||||
if ((current.resolved.attachOnly || remoteCdp) && opts.onEnsureAttachTarget) {
|
||||
await opts.onEnsureAttachTarget(profile);
|
||||
if (await isHttpReachable(1200)) return;
|
||||
if (await isHttpReachable(1200)) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
if (current.resolved.attachOnly || remoteCdp) {
|
||||
throw new Error(
|
||||
@@ -307,7 +327,9 @@ function createProfileContext(
|
||||
}
|
||||
|
||||
// Port is reachable - check if we own it
|
||||
if (await isReachable()) return;
|
||||
if (await isReachable()) {
|
||||
return;
|
||||
}
|
||||
|
||||
// HTTP responds but WebSocket fails - port in use by something else
|
||||
if (!profileState.running) {
|
||||
@@ -321,7 +343,9 @@ function createProfileContext(
|
||||
if (current.resolved.attachOnly || remoteCdp) {
|
||||
if (opts.onEnsureAttachTarget) {
|
||||
await opts.onEnsureAttachTarget(profile);
|
||||
if (await isReachable(1200)) return;
|
||||
if (await isReachable(1200)) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
throw new Error(
|
||||
remoteCdp
|
||||
@@ -368,7 +392,9 @@ function createProfileContext(
|
||||
const resolveById = (raw: string) => {
|
||||
const resolved = resolveTargetIdFromTabs(raw, candidates);
|
||||
if (!resolved.ok) {
|
||||
if (resolved.reason === "ambiguous") return "AMBIGUOUS" as const;
|
||||
if (resolved.reason === "ambiguous") {
|
||||
return "AMBIGUOUS" as const;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
return candidates.find((t) => t.targetId === resolved.targetId) ?? null;
|
||||
@@ -377,7 +403,9 @@ function createProfileContext(
|
||||
const pickDefault = () => {
|
||||
const last = profileState.lastTargetId?.trim() || "";
|
||||
const lastResolved = last ? resolveById(last) : null;
|
||||
if (lastResolved && lastResolved !== "AMBIGUOUS") return lastResolved;
|
||||
if (lastResolved && lastResolved !== "AMBIGUOUS") {
|
||||
return lastResolved;
|
||||
}
|
||||
// Prefer a real page tab first (avoid service workers/background targets).
|
||||
const page = candidates.find((t) => (t.type ?? "page") === "page");
|
||||
return page ?? candidates.at(0) ?? null;
|
||||
@@ -393,7 +421,9 @@ function createProfileContext(
|
||||
if (chosen === "AMBIGUOUS") {
|
||||
throw new Error("ambiguous target id prefix");
|
||||
}
|
||||
if (!chosen) throw new Error("tab not found");
|
||||
if (!chosen) {
|
||||
throw new Error("tab not found");
|
||||
}
|
||||
profileState.lastTargetId = chosen.targetId;
|
||||
return chosen;
|
||||
};
|
||||
@@ -463,7 +493,9 @@ function createProfileContext(
|
||||
return { stopped };
|
||||
}
|
||||
const profileState = getProfileState();
|
||||
if (!profileState.running) return { stopped: false };
|
||||
if (!profileState.running) {
|
||||
return { stopped: false };
|
||||
}
|
||||
await stopOpenClawChrome(profileState.running);
|
||||
setProfileRunning(null);
|
||||
return { stopped: true };
|
||||
@@ -530,7 +562,9 @@ function createProfileContext(
|
||||
export function createBrowserRouteContext(opts: ContextOptions): BrowserRouteContext {
|
||||
const state = () => {
|
||||
const current = opts.getState();
|
||||
if (!current) throw new Error("Browser server not started");
|
||||
if (!current) {
|
||||
throw new Error("Browser server not started");
|
||||
}
|
||||
return current;
|
||||
};
|
||||
|
||||
@@ -552,7 +586,9 @@ export function createBrowserRouteContext(opts: ContextOptions): BrowserRouteCon
|
||||
for (const name of Object.keys(current.resolved.profiles)) {
|
||||
const profileState = current.profiles.get(name);
|
||||
const profile = resolveProfile(current.resolved, name);
|
||||
if (!profile) continue;
|
||||
if (!profile) {
|
||||
continue;
|
||||
}
|
||||
|
||||
let tabCount = 0;
|
||||
let running = false;
|
||||
|
||||
Reference in New Issue
Block a user