From 91c041e5da815285f7c774e29ec69a9609909718 Mon Sep 17 00:00:00 2001 From: Peter Steinberger Date: Sun, 15 Feb 2026 05:43:35 +0000 Subject: [PATCH] refactor(pairing): share allowFrom normalization --- src/pairing/pairing-store.ts | 24 ++++++++++++++---------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/src/pairing/pairing-store.ts b/src/pairing/pairing-store.ts index 69a1e8cab..d6a831337 100644 --- a/src/pairing/pairing-store.ts +++ b/src/pairing/pairing-store.ts @@ -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 }; }