// bazarr's own API key, found where bazarr keeps it. Shared by the client (tools, events) and the // Servarr step, and kept apart from client.ts so the step and its test load it without the client. import { readFileSync } from "node:fs"; /** * bazarr's own API key, read from where bazarr keeps it: `auth.apikey` in `config/config.yaml` under * its config directory. bazarr makes this key itself on first start and nothing lets the mesh set it, * so a key the mesh minted could never work; reading bazarr's own file needs nothing accepted and * stays right if the operator regenerates the key in bazarr's settings screen. The file is read, never * written. Undefined when the file or the key is not there. */ export function apiKeyFromConfigDir(configDir?: string): string | undefined { if (!configDir) return undefined; let text: string; try { text = readFileSync(`${configDir.replace(/\/$/, "")}/config/config.yaml`, "utf8"); } catch { return undefined; } return apiKeyFromConfigYaml(text); } /** `auth.apikey` from the text of bazarr's config.yaml — a top-level `auth:` mapping, one level deep. */ export function apiKeyFromConfigYaml(text: string): string | undefined { let inAuth = false; for (const line of text.split(/\r?\n/)) { if (/^\S/.test(line)) { inAuth = /^auth:\s*$/.test(line); continue; } if (!inAuth) continue; const m = line.match(/^\s+apikey:\s*(.*?)\s*$/); if (m) { const v = m[1].replace(/^(['"])(.*)\1$/, "$2").trim(); return v || undefined; } } return undefined; }