chore: Enable "curly" rule to avoid single-statement if confusion/errors.

This commit is contained in:
cpojer
2026-01-31 16:19:20 +09:00
parent 009b16fab8
commit 5ceff756e1
1266 changed files with 27871 additions and 9393 deletions

View File

@@ -99,11 +99,21 @@ function isLoopbackHost(host: string) {
}
function isLoopbackAddress(ip: string | undefined): boolean {
if (!ip) return false;
if (ip === "127.0.0.1") return true;
if (ip.startsWith("127.")) return true;
if (ip === "::1") return true;
if (ip.startsWith("::ffff:127.")) return true;
if (!ip) {
return false;
}
if (ip === "127.0.0.1") {
return true;
}
if (ip.startsWith("127.")) {
return true;
}
if (ip === "::1") {
return true;
}
if (ip.startsWith("::ffff:127.")) {
return true;
}
return false;
}
@@ -158,7 +168,9 @@ export async function ensureChromeExtensionRelayServer(opts: {
}
const existing = serversByPort.get(info.port);
if (existing) return existing;
if (existing) {
return existing;
}
let extensionWs: WebSocket | null = null;
const cdpClients = new Set<WebSocket>();
@@ -192,13 +204,17 @@ export async function ensureChromeExtensionRelayServer(opts: {
const broadcastToCdpClients = (evt: CdpEvent) => {
const msg = JSON.stringify(evt);
for (const ws of cdpClients) {
if (ws.readyState !== WebSocket.OPEN) continue;
if (ws.readyState !== WebSocket.OPEN) {
continue;
}
ws.send(msg);
}
};
const sendResponseToCdp = (ws: WebSocket, res: CdpResponse) => {
if (ws.readyState !== WebSocket.OPEN) return;
if (ws.readyState !== WebSocket.OPEN) {
return;
}
ws.send(JSON.stringify(res));
};
@@ -253,12 +269,16 @@ export async function ensureChromeExtensionRelayServer(opts: {
const targetId = typeof params.targetId === "string" ? params.targetId : undefined;
if (targetId) {
for (const t of connectedTargets.values()) {
if (t.targetId === targetId) return { targetInfo: t.targetInfo };
if (t.targetId === targetId) {
return { targetInfo: t.targetInfo };
}
}
}
if (cmd.sessionId && connectedTargets.has(cmd.sessionId)) {
const t = connectedTargets.get(cmd.sessionId);
if (t) return { targetInfo: t.targetInfo };
if (t) {
return { targetInfo: t.targetInfo };
}
}
const first = Array.from(connectedTargets.values())[0];
return { targetInfo: first?.targetInfo };
@@ -266,9 +286,13 @@ export async function ensureChromeExtensionRelayServer(opts: {
case "Target.attachToTarget": {
const params = (cmd.params ?? {}) as { targetId?: string };
const targetId = typeof params.targetId === "string" ? params.targetId : undefined;
if (!targetId) throw new Error("targetId required");
if (!targetId) {
throw new Error("targetId required");
}
for (const t of connectedTargets.values()) {
if (t.targetId === targetId) return { sessionId: t.sessionId };
if (t.targetId === targetId) {
return { sessionId: t.sessionId };
}
}
throw new Error("target not found");
}
@@ -322,7 +346,9 @@ export async function ensureChromeExtensionRelayServer(opts: {
"Protocol-Version": "1.3",
};
// Only advertise the WS URL if a real extension is connected.
if (extensionWs) payload.webSocketDebuggerUrl = cdpWsUrl;
if (extensionWs) {
payload.webSocketDebuggerUrl = cdpWsUrl;
}
res.writeHead(200, { "Content-Type": "application/json" });
res.end(JSON.stringify(payload));
return;
@@ -438,7 +464,9 @@ export async function ensureChromeExtensionRelayServer(opts: {
extensionWs = ws;
const ping = setInterval(() => {
if (ws.readyState !== WebSocket.OPEN) return;
if (ws.readyState !== WebSocket.OPEN) {
return;
}
ws.send(JSON.stringify({ method: "ping" } satisfies ExtensionPingMessage));
}, 5000);
@@ -452,7 +480,9 @@ export async function ensureChromeExtensionRelayServer(opts: {
if (parsed && typeof parsed === "object" && "id" in parsed && typeof parsed.id === "number") {
const pending = pendingExtension.get(parsed.id);
if (!pending) return;
if (!pending) {
return;
}
pendingExtension.delete(parsed.id);
clearTimeout(pending.timer);
if ("error" in parsed && typeof parsed.error === "string" && parsed.error.trim()) {
@@ -464,18 +494,26 @@ export async function ensureChromeExtensionRelayServer(opts: {
}
if (parsed && typeof parsed === "object" && "method" in parsed) {
if ((parsed as ExtensionPongMessage).method === "pong") return;
if ((parsed as ExtensionForwardEventMessage).method !== "forwardCDPEvent") return;
if ((parsed as ExtensionPongMessage).method === "pong") {
return;
}
if ((parsed as ExtensionForwardEventMessage).method !== "forwardCDPEvent") {
return;
}
const evt = parsed as ExtensionForwardEventMessage;
const method = evt.params?.method;
const params = evt.params?.params;
const sessionId = evt.params?.sessionId;
if (!method || typeof method !== "string") return;
if (!method || typeof method !== "string") {
return;
}
if (method === "Target.attachedToTarget") {
const attached = (params ?? {}) as AttachedToTargetEvent;
const targetType = attached?.targetInfo?.type ?? "page";
if (targetType !== "page") return;
if (targetType !== "page") {
return;
}
if (attached?.sessionId && attached?.targetInfo?.targetId) {
const prev = connectedTargets.get(attached.sessionId);
const nextTargetId = attached.targetInfo.targetId;
@@ -502,7 +540,9 @@ export async function ensureChromeExtensionRelayServer(opts: {
if (method === "Target.detachedFromTarget") {
const detached = (params ?? {}) as DetachedFromTargetEvent;
if (detached?.sessionId) connectedTargets.delete(detached.sessionId);
if (detached?.sessionId) {
connectedTargets.delete(detached.sessionId);
}
broadcastToCdpClients({ method, params, sessionId });
return;
}
@@ -515,7 +555,9 @@ export async function ensureChromeExtensionRelayServer(opts: {
const targetId = targetInfo?.targetId;
if (targetId && (targetInfo?.type ?? "page") === "page") {
for (const [sid, target] of connectedTargets) {
if (target.targetId !== targetId) continue;
if (target.targetId !== targetId) {
continue;
}
connectedTargets.set(sid, {
...target,
targetInfo: { ...target.targetInfo, ...(targetInfo as object) },
@@ -559,8 +601,12 @@ export async function ensureChromeExtensionRelayServer(opts: {
} catch {
return;
}
if (!cmd || typeof cmd !== "object") return;
if (typeof cmd.id !== "number" || typeof cmd.method !== "string") return;
if (!cmd || typeof cmd !== "object") {
return;
}
if (typeof cmd.id !== "number" || typeof cmd.method !== "string") {
return;
}
if (!extensionWs) {
sendResponseToCdp(ws, {
@@ -665,7 +711,9 @@ export async function ensureChromeExtensionRelayServer(opts: {
export async function stopChromeExtensionRelayServer(opts: { cdpUrl: string }): Promise<boolean> {
const info = parseBaseUrl(opts.cdpUrl);
const existing = serversByPort.get(info.port);
if (!existing) return false;
if (!existing) {
return false;
}
await existing.stop();
return true;
}