mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-08 21:28:26 +00:00
chore: Enable "curly" rule to avoid single-statement if confusion/errors.
This commit is contained in:
@@ -17,7 +17,9 @@ export type HooksConfigResolved = {
|
||||
};
|
||||
|
||||
export function resolveHooksConfig(cfg: OpenClawConfig): HooksConfigResolved | null {
|
||||
if (cfg.hooks?.enabled !== true) return null;
|
||||
if (cfg.hooks?.enabled !== true) {
|
||||
return null;
|
||||
}
|
||||
const token = cfg.hooks?.token?.trim();
|
||||
if (!token) {
|
||||
throw new Error("hooks.enabled requires hooks.token");
|
||||
@@ -51,15 +53,21 @@ export function extractHookToken(req: IncomingMessage, url: URL): HookTokenResul
|
||||
typeof req.headers.authorization === "string" ? req.headers.authorization.trim() : "";
|
||||
if (auth.toLowerCase().startsWith("bearer ")) {
|
||||
const token = auth.slice(7).trim();
|
||||
if (token) return { token, fromQuery: false };
|
||||
if (token) {
|
||||
return { token, fromQuery: false };
|
||||
}
|
||||
}
|
||||
const headerToken =
|
||||
typeof req.headers["x-openclaw-token"] === "string"
|
||||
? req.headers["x-openclaw-token"].trim()
|
||||
: "";
|
||||
if (headerToken) return { token: headerToken, fromQuery: false };
|
||||
if (headerToken) {
|
||||
return { token: headerToken, fromQuery: false };
|
||||
}
|
||||
const queryToken = url.searchParams.get("token");
|
||||
if (queryToken) return { token: queryToken.trim(), fromQuery: true };
|
||||
if (queryToken) {
|
||||
return { token: queryToken.trim(), fromQuery: true };
|
||||
}
|
||||
return { token: undefined, fromQuery: false };
|
||||
}
|
||||
|
||||
@@ -72,7 +80,9 @@ export async function readJsonBody(
|
||||
let total = 0;
|
||||
const chunks: Buffer[] = [];
|
||||
req.on("data", (chunk: Buffer) => {
|
||||
if (done) return;
|
||||
if (done) {
|
||||
return;
|
||||
}
|
||||
total += chunk.length;
|
||||
if (total > maxBytes) {
|
||||
done = true;
|
||||
@@ -83,7 +93,9 @@ export async function readJsonBody(
|
||||
chunks.push(chunk);
|
||||
});
|
||||
req.on("end", () => {
|
||||
if (done) return;
|
||||
if (done) {
|
||||
return;
|
||||
}
|
||||
done = true;
|
||||
const raw = Buffer.concat(chunks).toString("utf-8").trim();
|
||||
if (!raw) {
|
||||
@@ -98,7 +110,9 @@ export async function readJsonBody(
|
||||
}
|
||||
});
|
||||
req.on("error", (err) => {
|
||||
if (done) return;
|
||||
if (done) {
|
||||
return;
|
||||
}
|
||||
done = true;
|
||||
resolve({ ok: false, error: String(err) });
|
||||
});
|
||||
@@ -123,7 +137,9 @@ export function normalizeWakePayload(
|
||||
| { ok: true; value: { text: string; mode: "now" | "next-heartbeat" } }
|
||||
| { ok: false; error: string } {
|
||||
const text = typeof payload.text === "string" ? payload.text.trim() : "";
|
||||
if (!text) return { ok: false, error: "text required" };
|
||||
if (!text) {
|
||||
return { ok: false, error: "text required" };
|
||||
}
|
||||
const mode = payload.mode === "next-heartbeat" ? "next-heartbeat" : "now";
|
||||
return { ok: true, value: { text, mode } };
|
||||
}
|
||||
@@ -149,10 +165,16 @@ const getHookChannelSet = () => new Set<string>(listHookChannelValues());
|
||||
export const getHookChannelError = () => `channel must be ${listHookChannelValues().join("|")}`;
|
||||
|
||||
export function resolveHookChannel(raw: unknown): HookMessageChannel | null {
|
||||
if (raw === undefined) return "last";
|
||||
if (typeof raw !== "string") return null;
|
||||
if (raw === undefined) {
|
||||
return "last";
|
||||
}
|
||||
if (typeof raw !== "string") {
|
||||
return null;
|
||||
}
|
||||
const normalized = normalizeMessageChannel(raw);
|
||||
if (!normalized || !getHookChannelSet().has(normalized)) return null;
|
||||
if (!normalized || !getHookChannelSet().has(normalized)) {
|
||||
return null;
|
||||
}
|
||||
return normalized as HookMessageChannel;
|
||||
}
|
||||
|
||||
@@ -170,7 +192,9 @@ export function normalizeAgentPayload(
|
||||
}
|
||||
| { ok: false; error: string } {
|
||||
const message = typeof payload.message === "string" ? payload.message.trim() : "";
|
||||
if (!message) return { ok: false, error: "message required" };
|
||||
if (!message) {
|
||||
return { ok: false, error: "message required" };
|
||||
}
|
||||
const nameRaw = payload.name;
|
||||
const name = typeof nameRaw === "string" && nameRaw.trim() ? nameRaw.trim() : "Hook";
|
||||
const wakeMode = payload.wakeMode === "next-heartbeat" ? "next-heartbeat" : "now";
|
||||
@@ -181,7 +205,9 @@ export function normalizeAgentPayload(
|
||||
? sessionKeyRaw.trim()
|
||||
: `hook:${idFactory()}`;
|
||||
const channel = resolveHookChannel(payload.channel);
|
||||
if (!channel) return { ok: false, error: getHookChannelError() };
|
||||
if (!channel) {
|
||||
return { ok: false, error: getHookChannelError() };
|
||||
}
|
||||
const toRaw = payload.to;
|
||||
const to = typeof toRaw === "string" && toRaw.trim() ? toRaw.trim() : undefined;
|
||||
const modelRaw = payload.model;
|
||||
|
||||
Reference in New Issue
Block a user