refactor(pairing): share allowFrom normalization
This commit is contained in:
@@ -204,6 +204,15 @@ function normalizeAllowEntry(channel: PairingChannel, entry: string): string {
|
||||
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(
|
||||
channel: PairingChannel,
|
||||
env: NodeJS.ProcessEnv = process.env,
|
||||
@@ -213,8 +222,7 @@ export async function readChannelAllowFromStore(
|
||||
version: 1,
|
||||
allowFrom: [],
|
||||
});
|
||||
const list = Array.isArray(value.allowFrom) ? value.allowFrom : [];
|
||||
return list.map((v) => normalizeAllowEntry(channel, String(v))).filter(Boolean);
|
||||
return normalizeAllowFromList(channel, value);
|
||||
}
|
||||
|
||||
export async function addChannelAllowFromStoreEntry(params: {
|
||||
@@ -232,10 +240,8 @@ export async function addChannelAllowFromStoreEntry(params: {
|
||||
version: 1,
|
||||
allowFrom: [],
|
||||
});
|
||||
const current = (Array.isArray(value.allowFrom) ? value.allowFrom : [])
|
||||
.map((v) => normalizeAllowEntry(params.channel, String(v)))
|
||||
.filter(Boolean);
|
||||
const normalized = normalizeAllowEntry(params.channel, normalizeId(params.entry));
|
||||
const current = normalizeAllowFromList(params.channel, value);
|
||||
const normalized = normalizeAllowFromInput(params.channel, params.entry);
|
||||
if (!normalized) {
|
||||
return { changed: false, allowFrom: current };
|
||||
}
|
||||
@@ -267,10 +273,8 @@ export async function removeChannelAllowFromStoreEntry(params: {
|
||||
version: 1,
|
||||
allowFrom: [],
|
||||
});
|
||||
const current = (Array.isArray(value.allowFrom) ? value.allowFrom : [])
|
||||
.map((v) => normalizeAllowEntry(params.channel, String(v)))
|
||||
.filter(Boolean);
|
||||
const normalized = normalizeAllowEntry(params.channel, normalizeId(params.entry));
|
||||
const current = normalizeAllowFromList(params.channel, value);
|
||||
const normalized = normalizeAllowFromInput(params.channel, params.entry);
|
||||
if (!normalized) {
|
||||
return { changed: false, allowFrom: current };
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user