refactor(pairing): share allowFrom normalization

This commit is contained in:
Peter Steinberger
2026-02-15 05:43:35 +00:00
parent 21df9ebd92
commit 91c041e5da

View File

@@ -204,6 +204,15 @@ function normalizeAllowEntry(channel: PairingChannel, entry: string): string {
return String(normalized).trim(); return String(normalized).trim();
} }
function normalizeAllowFromList(channel: PairingChannel, store: AllowFromStore): string[] {
const list = Array.isArray(store.allowFrom) ? store.allowFrom : [];
return list.map((v) => normalizeAllowEntry(channel, String(v))).filter(Boolean);
}
function normalizeAllowFromInput(channel: PairingChannel, entry: string | number): string {
return normalizeAllowEntry(channel, normalizeId(entry));
}
export async function readChannelAllowFromStore( export async function readChannelAllowFromStore(
channel: PairingChannel, channel: PairingChannel,
env: NodeJS.ProcessEnv = process.env, env: NodeJS.ProcessEnv = process.env,
@@ -213,8 +222,7 @@ export async function readChannelAllowFromStore(
version: 1, version: 1,
allowFrom: [], allowFrom: [],
}); });
const list = Array.isArray(value.allowFrom) ? value.allowFrom : []; return normalizeAllowFromList(channel, value);
return list.map((v) => normalizeAllowEntry(channel, String(v))).filter(Boolean);
} }
export async function addChannelAllowFromStoreEntry(params: { export async function addChannelAllowFromStoreEntry(params: {
@@ -232,10 +240,8 @@ export async function addChannelAllowFromStoreEntry(params: {
version: 1, version: 1,
allowFrom: [], allowFrom: [],
}); });
const current = (Array.isArray(value.allowFrom) ? value.allowFrom : []) const current = normalizeAllowFromList(params.channel, value);
.map((v) => normalizeAllowEntry(params.channel, String(v))) const normalized = normalizeAllowFromInput(params.channel, params.entry);
.filter(Boolean);
const normalized = normalizeAllowEntry(params.channel, normalizeId(params.entry));
if (!normalized) { if (!normalized) {
return { changed: false, allowFrom: current }; return { changed: false, allowFrom: current };
} }
@@ -267,10 +273,8 @@ export async function removeChannelAllowFromStoreEntry(params: {
version: 1, version: 1,
allowFrom: [], allowFrom: [],
}); });
const current = (Array.isArray(value.allowFrom) ? value.allowFrom : []) const current = normalizeAllowFromList(params.channel, value);
.map((v) => normalizeAllowEntry(params.channel, String(v))) const normalized = normalizeAllowFromInput(params.channel, params.entry);
.filter(Boolean);
const normalized = normalizeAllowEntry(params.channel, normalizeId(params.entry));
if (!normalized) { if (!normalized) {
return { changed: false, allowFrom: current }; return { changed: false, allowFrom: current };
} }