style: format gateway server

This commit is contained in:
Peter Steinberger
2025-12-24 17:45:39 +01:00
parent f4fb80e523
commit bd223606b1

View File

@@ -175,10 +175,7 @@ function resolveHooksConfig(cfg: ClawdisConfig): HooksConfigResolved | null {
}; };
} }
function extractHookToken( function extractHookToken(req: IncomingMessage, url: URL): string | undefined {
req: IncomingMessage,
url: URL,
): string | undefined {
const auth = const auth =
typeof req.headers.authorization === "string" typeof req.headers.authorization === "string"
? req.headers.authorization.trim() ? req.headers.authorization.trim()
@@ -1299,7 +1296,6 @@ export async function startGatewayServer(
); );
} }
let canvasHost: CanvasHostHandler | null = null; let canvasHost: CanvasHostHandler | null = null;
let canvasHostServer: CanvasHostServer | null = null; let canvasHostServer: CanvasHostServer | null = null;
if (canvasHostEnabled) { if (canvasHostEnabled) {
@@ -1328,10 +1324,7 @@ export async function startGatewayServer(
if (!hooksConfig) return false; if (!hooksConfig) return false;
const url = new URL(req.url ?? "/", `http://${bindHost}:${port}`); const url = new URL(req.url ?? "/", `http://${bindHost}:${port}`);
const basePath = hooksConfig.basePath; const basePath = hooksConfig.basePath;
if ( if (url.pathname !== basePath && !url.pathname.startsWith(`${basePath}/`)) {
url.pathname !== basePath &&
!url.pathname.startsWith(`${basePath}/`)
) {
return false; return false;
} }
@@ -1351,9 +1344,7 @@ export async function startGatewayServer(
return true; return true;
} }
const subPath = url.pathname const subPath = url.pathname.slice(basePath.length).replace(/^\/+/, "");
.slice(basePath.length)
.replace(/^\/+/, "");
if (!subPath) { if (!subPath) {
res.statusCode = 404; res.statusCode = 404;
res.setHeader("Content-Type", "text/plain; charset=utf-8"); res.setHeader("Content-Type", "text/plain; charset=utf-8");
@@ -1381,8 +1372,7 @@ export async function startGatewayServer(
return true; return true;
} }
const modeRaw = (payload as { mode?: unknown }).mode; const modeRaw = (payload as { mode?: unknown }).mode;
const mode = const mode = modeRaw === "next-heartbeat" ? "next-heartbeat" : "now";
modeRaw === "next-heartbeat" ? "next-heartbeat" : "now";
enqueueSystemEvent(text); enqueueSystemEvent(text);
if (mode === "now") { if (mode === "now") {
requestReplyHeartbeatNow({ reason: "hook:wake" }); requestReplyHeartbeatNow({ reason: "hook:wake" });
@@ -1439,9 +1429,12 @@ export async function startGatewayServer(
typeof thinkingRaw === "string" && thinkingRaw.trim() typeof thinkingRaw === "string" && thinkingRaw.trim()
? thinkingRaw.trim() ? thinkingRaw.trim()
: undefined; : undefined;
const timeoutRaw = (payload as { timeoutSeconds?: unknown }).timeoutSeconds; const timeoutRaw = (payload as { timeoutSeconds?: unknown })
.timeoutSeconds;
const timeoutSeconds = const timeoutSeconds =
typeof timeoutRaw === "number" && Number.isFinite(timeoutRaw) && timeoutRaw > 0 typeof timeoutRaw === "number" &&
Number.isFinite(timeoutRaw) &&
timeoutRaw > 0
? Math.floor(timeoutRaw) ? Math.floor(timeoutRaw)
: undefined; : undefined;
@@ -1483,11 +1476,11 @@ export async function startGatewayServer(
lane: "cron", lane: "cron",
}); });
const summary = const summary =
result.summary?.trim() || result.summary?.trim() || result.error?.trim() || result.status;
result.error?.trim() ||
result.status;
const prefix = const prefix =
result.status === "ok" ? `Hook ${name}` : `Hook ${name} (${result.status})`; result.status === "ok"
? `Hook ${name}`
: `Hook ${name} (${result.status})`;
enqueueSystemEvent(`${prefix}: ${summary}`.trim()); enqueueSystemEvent(`${prefix}: ${summary}`.trim());
if (wakeMode === "now") { if (wakeMode === "now") {
requestReplyHeartbeatNow({ reason: `hook:${jobId}` }); requestReplyHeartbeatNow({ reason: `hook:${jobId}` });