import { getSettings } from "yt-dlp-transcript-common/lib/settings"; import { NetworkRequestError, applyNetworkRequest, networkSettingsProblems, sanitizeNetwork, type NetworkSettings, } from "yt-dlp-transcript-common/lib/networkExits"; import { saveSettingsHeld, withSettingsLock } from "./saveSettings"; // THE NETWORK EXITS' ONE WRITER (lane network-exits): `pnpm ops network` and // /settings → Network exits both send a request here. Applied to the stored // block (a masked proxy keeps its stored credentials), refused whole with // every problem named when the result is outside the trial // (networkSettingsProblems), then saved. The block holds no secret (review 1, // finding 1: credentials live in network.credentialsFile), so the answer is // the saved block as it is. // // Not a "use server" module: the ops route and networkActions.ts call it. export class NetworkSaveError extends Error {} export async function saveNetworkRequest(body: Record): Promise { return withSettingsLock(async () => { let next: Record; try { next = applyNetworkRequest(getSettings().network, body); } catch (e) { if (e instanceof NetworkRequestError) throw new NetworkSaveError(e.message); throw e; } const problems = networkSettingsProblems(next); if (problems.length) throw new NetworkSaveError(`not saved — ${problems.join("; ")}`); await saveSettingsHeld({ network: sanitizeNetwork(next) }); return getSettings().network; }); }