type KeyPair = { publicKey: string; privateKey: string } type Dependencies = { read: () => Promise generate: () => KeyPair installIfMissing: (keys: KeyPair) => Promise } /** A conditional database write selects the winner across processes; no existing pair is rotated. */ export async function ensureTrafficFlowHostKeys(deps: Dependencies): Promise<{ publicKey: string; created: boolean }> { const existing = await deps.read() if (existing.publicKey && existing.privateKey) return { publicKey: existing.publicKey, created: false } const created = await deps.installIfMissing(deps.generate()) const confirmed = await deps.read() if (!confirmed.publicKey || !confirmed.privateKey) throw new Error("Создание ключей NetFlow не подтверждено") return { publicKey: confirmed.publicKey, created } }