/** * Backend registry -- creates and caches backend instances by type. */ import type { BackendType, CredentialBackend, VaultConfig } from "../types.js"; import { AgeBackend } from "./age-backend.js"; import { KeychainBackend } from "./keychain-backend.js"; import { PassthroughBackend } from "./passthrough-backend.js"; const instances = new Map(); export function createBackend( type: BackendType, config: VaultConfig, ): CredentialBackend { const cacheKey = type; const cached = instances.get(cacheKey); if (cached) { return cached; } let backend: CredentialBackend; switch (type) { case "age": backend = new AgeBackend(config.age); break; case "keychain": backend = new KeychainBackend(config.keychain); break; case "passthrough": backend = new PassthroughBackend(); break; default: { const exhaustive: never = type; throw new Error(`Unknown backend type: ${String(exhaustive)}`); } } instances.set(cacheKey, backend); return backend; } export function getPassthroughBackend(): CredentialBackend { return createBackend("passthrough", { backend: "passthrough", managedProviders: "all", }); } /** * Clear cached backend instances (used on config change or reload). */ export function clearBackendCache(): void { instances.clear(); }