mirror of
https://github.com/farcasclaudiu/openclaw.git
synced 2026-06-28 23:02:02 +03:00
chore: Enable "curly" rule to avoid single-statement if confusion/errors.
This commit is contained in:
@@ -15,8 +15,11 @@ async function withTempStateDir<T>(fn: (stateDir: string) => Promise<T>) {
|
||||
try {
|
||||
return await fn(dir);
|
||||
} finally {
|
||||
if (previous === undefined) delete process.env.OPENCLAW_STATE_DIR;
|
||||
else process.env.OPENCLAW_STATE_DIR = previous;
|
||||
if (previous === undefined) {
|
||||
delete process.env.OPENCLAW_STATE_DIR;
|
||||
} else {
|
||||
process.env.OPENCLAW_STATE_DIR = previous;
|
||||
}
|
||||
await fs.rm(dir, { recursive: true, force: true });
|
||||
}
|
||||
}
|
||||
|
||||
@@ -51,9 +51,13 @@ function resolveCredentialsDir(env: NodeJS.ProcessEnv = process.env): string {
|
||||
/** Sanitize channel ID for use in filenames (prevent path traversal). */
|
||||
function safeChannelKey(channel: PairingChannel): string {
|
||||
const raw = String(channel).trim().toLowerCase();
|
||||
if (!raw) throw new Error("invalid pairing channel");
|
||||
if (!raw) {
|
||||
throw new Error("invalid pairing channel");
|
||||
}
|
||||
const safe = raw.replace(/[\\/:*?"<>|]/g, "_").replace(/\.\./g, "_");
|
||||
if (!safe || safe === "_") throw new Error("invalid pairing channel");
|
||||
if (!safe || safe === "_") {
|
||||
throw new Error("invalid pairing channel");
|
||||
}
|
||||
return safe;
|
||||
}
|
||||
|
||||
@@ -83,11 +87,15 @@ async function readJsonFile<T>(
|
||||
try {
|
||||
const raw = await fs.promises.readFile(filePath, "utf-8");
|
||||
const parsed = safeParseJson<T>(raw);
|
||||
if (parsed == null) return { value: fallback, exists: true };
|
||||
if (parsed == null) {
|
||||
return { value: fallback, exists: true };
|
||||
}
|
||||
return { value: parsed, exists: true };
|
||||
} catch (err) {
|
||||
const code = (err as { code?: string }).code;
|
||||
if (code === "ENOENT") return { value: fallback, exists: false };
|
||||
if (code === "ENOENT") {
|
||||
return { value: fallback, exists: false };
|
||||
}
|
||||
return { value: fallback, exists: false };
|
||||
}
|
||||
}
|
||||
@@ -133,15 +141,21 @@ async function withFileLock<T>(
|
||||
}
|
||||
|
||||
function parseTimestamp(value: string | undefined): number | null {
|
||||
if (!value) return null;
|
||||
if (!value) {
|
||||
return null;
|
||||
}
|
||||
const parsed = Date.parse(value);
|
||||
if (!Number.isFinite(parsed)) return null;
|
||||
if (!Number.isFinite(parsed)) {
|
||||
return null;
|
||||
}
|
||||
return parsed;
|
||||
}
|
||||
|
||||
function isExpired(entry: PairingRequest, nowMs: number): boolean {
|
||||
const createdAt = parseTimestamp(entry.createdAt);
|
||||
if (!createdAt) return true;
|
||||
if (!createdAt) {
|
||||
return true;
|
||||
}
|
||||
return nowMs - createdAt > PAIRING_PENDING_TTL_MS;
|
||||
}
|
||||
|
||||
@@ -183,7 +197,9 @@ function randomCode(): string {
|
||||
function generateUniqueCode(existing: Set<string>): string {
|
||||
for (let attempt = 0; attempt < 500; attempt += 1) {
|
||||
const code = randomCode();
|
||||
if (!existing.has(code)) return code;
|
||||
if (!existing.has(code)) {
|
||||
return code;
|
||||
}
|
||||
}
|
||||
throw new Error("failed to generate unique pairing code");
|
||||
}
|
||||
@@ -194,8 +210,12 @@ function normalizeId(value: string | number): string {
|
||||
|
||||
function normalizeAllowEntry(channel: PairingChannel, entry: string): string {
|
||||
const trimmed = entry.trim();
|
||||
if (!trimmed) return "";
|
||||
if (trimmed === "*") return "";
|
||||
if (!trimmed) {
|
||||
return "";
|
||||
}
|
||||
if (trimmed === "*") {
|
||||
return "";
|
||||
}
|
||||
const adapter = getPairingAdapter(channel);
|
||||
const normalized = adapter?.normalizeAllowEntry ? adapter.normalizeAllowEntry(trimmed) : trimmed;
|
||||
return String(normalized).trim();
|
||||
@@ -233,8 +253,12 @@ export async function addChannelAllowFromStoreEntry(params: {
|
||||
.map((v) => normalizeAllowEntry(params.channel, String(v)))
|
||||
.filter(Boolean);
|
||||
const normalized = normalizeAllowEntry(params.channel, normalizeId(params.entry));
|
||||
if (!normalized) return { changed: false, allowFrom: current };
|
||||
if (current.includes(normalized)) return { changed: false, allowFrom: current };
|
||||
if (!normalized) {
|
||||
return { changed: false, allowFrom: current };
|
||||
}
|
||||
if (current.includes(normalized)) {
|
||||
return { changed: false, allowFrom: current };
|
||||
}
|
||||
const next = [...current, normalized];
|
||||
await writeJsonFile(filePath, {
|
||||
version: 1,
|
||||
@@ -264,9 +288,13 @@ export async function removeChannelAllowFromStoreEntry(params: {
|
||||
.map((v) => normalizeAllowEntry(params.channel, String(v)))
|
||||
.filter(Boolean);
|
||||
const normalized = normalizeAllowEntry(params.channel, normalizeId(params.entry));
|
||||
if (!normalized) return { changed: false, allowFrom: current };
|
||||
if (!normalized) {
|
||||
return { changed: false, allowFrom: current };
|
||||
}
|
||||
const next = current.filter((entry) => entry !== normalized);
|
||||
if (next.length === current.length) return { changed: false, allowFrom: current };
|
||||
if (next.length === current.length) {
|
||||
return { changed: false, allowFrom: current };
|
||||
}
|
||||
await writeJsonFile(filePath, {
|
||||
version: 1,
|
||||
allowFrom: next,
|
||||
@@ -423,7 +451,9 @@ export async function approveChannelPairingCode(params: {
|
||||
}): Promise<{ id: string; entry?: PairingRequest } | null> {
|
||||
const env = params.env ?? process.env;
|
||||
const code = params.code.trim().toUpperCase();
|
||||
if (!code) return null;
|
||||
if (!code) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const filePath = resolvePairingPath(params.channel, env);
|
||||
return await withFileLock(
|
||||
@@ -448,7 +478,9 @@ export async function approveChannelPairingCode(params: {
|
||||
return null;
|
||||
}
|
||||
const entry = pruned[idx];
|
||||
if (!entry) return null;
|
||||
if (!entry) {
|
||||
return null;
|
||||
}
|
||||
pruned.splice(idx, 1);
|
||||
await writeJsonFile(filePath, {
|
||||
version: 1,
|
||||
|
||||
Reference in New Issue
Block a user