mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-09 17:04:32 +00:00
refactor(pairing): dedupe allow-from store updates
This commit is contained in:
@@ -213,6 +213,57 @@ function normalizeAllowFromInput(channel: PairingChannel, entry: string | number
|
|||||||
return normalizeAllowEntry(channel, normalizeId(entry));
|
return normalizeAllowEntry(channel, normalizeId(entry));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async function readAllowFromState(params: {
|
||||||
|
channel: PairingChannel;
|
||||||
|
entry: string | number;
|
||||||
|
filePath: string;
|
||||||
|
}): Promise<{ current: string[]; normalized: string | null }> {
|
||||||
|
const { value } = await readJsonFile<AllowFromStore>(params.filePath, {
|
||||||
|
version: 1,
|
||||||
|
allowFrom: [],
|
||||||
|
});
|
||||||
|
const current = normalizeAllowFromList(params.channel, value);
|
||||||
|
const normalized = normalizeAllowFromInput(params.channel, params.entry);
|
||||||
|
return { current, normalized: normalized || null };
|
||||||
|
}
|
||||||
|
|
||||||
|
async function writeAllowFromState(filePath: string, allowFrom: string[]): Promise<void> {
|
||||||
|
await writeJsonFile(filePath, {
|
||||||
|
version: 1,
|
||||||
|
allowFrom,
|
||||||
|
} satisfies AllowFromStore);
|
||||||
|
}
|
||||||
|
|
||||||
|
async function updateAllowFromStoreEntry(params: {
|
||||||
|
channel: PairingChannel;
|
||||||
|
entry: string | number;
|
||||||
|
env?: NodeJS.ProcessEnv;
|
||||||
|
apply: (current: string[], normalized: string) => string[] | null;
|
||||||
|
}): Promise<{ changed: boolean; allowFrom: string[] }> {
|
||||||
|
const env = params.env ?? process.env;
|
||||||
|
const filePath = resolveAllowFromPath(params.channel, env);
|
||||||
|
return await withFileLock(
|
||||||
|
filePath,
|
||||||
|
{ version: 1, allowFrom: [] } satisfies AllowFromStore,
|
||||||
|
async () => {
|
||||||
|
const { current, normalized } = await readAllowFromState({
|
||||||
|
channel: params.channel,
|
||||||
|
entry: params.entry,
|
||||||
|
filePath,
|
||||||
|
});
|
||||||
|
if (!normalized) {
|
||||||
|
return { changed: false, allowFrom: current };
|
||||||
|
}
|
||||||
|
const next = params.apply(current, normalized);
|
||||||
|
if (!next) {
|
||||||
|
return { changed: false, allowFrom: current };
|
||||||
|
}
|
||||||
|
await writeAllowFromState(filePath, next);
|
||||||
|
return { changed: true, allowFrom: next };
|
||||||
|
},
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
export async function readChannelAllowFromStore(
|
export async function readChannelAllowFromStore(
|
||||||
channel: PairingChannel,
|
channel: PairingChannel,
|
||||||
env: NodeJS.ProcessEnv = process.env,
|
env: NodeJS.ProcessEnv = process.env,
|
||||||
@@ -230,32 +281,17 @@ export async function addChannelAllowFromStoreEntry(params: {
|
|||||||
entry: string | number;
|
entry: string | number;
|
||||||
env?: NodeJS.ProcessEnv;
|
env?: NodeJS.ProcessEnv;
|
||||||
}): Promise<{ changed: boolean; allowFrom: string[] }> {
|
}): Promise<{ changed: boolean; allowFrom: string[] }> {
|
||||||
const env = params.env ?? process.env;
|
return await updateAllowFromStoreEntry({
|
||||||
const filePath = resolveAllowFromPath(params.channel, env);
|
channel: params.channel,
|
||||||
return await withFileLock(
|
entry: params.entry,
|
||||||
filePath,
|
env: params.env,
|
||||||
{ version: 1, allowFrom: [] } satisfies AllowFromStore,
|
apply: (current, normalized) => {
|
||||||
async () => {
|
|
||||||
const { value } = await readJsonFile<AllowFromStore>(filePath, {
|
|
||||||
version: 1,
|
|
||||||
allowFrom: [],
|
|
||||||
});
|
|
||||||
const current = normalizeAllowFromList(params.channel, value);
|
|
||||||
const normalized = normalizeAllowFromInput(params.channel, params.entry);
|
|
||||||
if (!normalized) {
|
|
||||||
return { changed: false, allowFrom: current };
|
|
||||||
}
|
|
||||||
if (current.includes(normalized)) {
|
if (current.includes(normalized)) {
|
||||||
return { changed: false, allowFrom: current };
|
return null;
|
||||||
}
|
}
|
||||||
const next = [...current, normalized];
|
return [...current, normalized];
|
||||||
await writeJsonFile(filePath, {
|
|
||||||
version: 1,
|
|
||||||
allowFrom: next,
|
|
||||||
} satisfies AllowFromStore);
|
|
||||||
return { changed: true, allowFrom: next };
|
|
||||||
},
|
},
|
||||||
);
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function removeChannelAllowFromStoreEntry(params: {
|
export async function removeChannelAllowFromStoreEntry(params: {
|
||||||
@@ -263,32 +299,18 @@ export async function removeChannelAllowFromStoreEntry(params: {
|
|||||||
entry: string | number;
|
entry: string | number;
|
||||||
env?: NodeJS.ProcessEnv;
|
env?: NodeJS.ProcessEnv;
|
||||||
}): Promise<{ changed: boolean; allowFrom: string[] }> {
|
}): Promise<{ changed: boolean; allowFrom: string[] }> {
|
||||||
const env = params.env ?? process.env;
|
return await updateAllowFromStoreEntry({
|
||||||
const filePath = resolveAllowFromPath(params.channel, env);
|
channel: params.channel,
|
||||||
return await withFileLock(
|
entry: params.entry,
|
||||||
filePath,
|
env: params.env,
|
||||||
{ version: 1, allowFrom: [] } satisfies AllowFromStore,
|
apply: (current, normalized) => {
|
||||||
async () => {
|
|
||||||
const { value } = await readJsonFile<AllowFromStore>(filePath, {
|
|
||||||
version: 1,
|
|
||||||
allowFrom: [],
|
|
||||||
});
|
|
||||||
const current = normalizeAllowFromList(params.channel, value);
|
|
||||||
const normalized = normalizeAllowFromInput(params.channel, params.entry);
|
|
||||||
if (!normalized) {
|
|
||||||
return { changed: false, allowFrom: current };
|
|
||||||
}
|
|
||||||
const next = current.filter((entry) => entry !== normalized);
|
const next = current.filter((entry) => entry !== normalized);
|
||||||
if (next.length === current.length) {
|
if (next.length === current.length) {
|
||||||
return { changed: false, allowFrom: current };
|
return null;
|
||||||
}
|
}
|
||||||
await writeJsonFile(filePath, {
|
return next;
|
||||||
version: 1,
|
|
||||||
allowFrom: next,
|
|
||||||
} satisfies AllowFromStore);
|
|
||||||
return { changed: true, allowFrom: next };
|
|
||||||
},
|
},
|
||||||
);
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function listChannelPairingRequests(
|
export async function listChannelPairingRequests(
|
||||||
|
|||||||
Reference in New Issue
Block a user