// THE /cookies SCREEN'S READS (plans/cookie-screen.md) — only what the rows // show, so the page loads fast where /settings does not (/settings ships the // whole parsed settings.json, ~120 KB of it the lanes' state, into one big // client form): // // - `.service-cookies.json` and each alert job's status (loadCookieAlerts — // the dashboard's own payload, which carries X's jar age); // - settings.json parsed once here for cookiesFromBrowser and social.x — // nothing of it is sent to the browser but the rows; // - a stat and a read of X's exported jar, archive.today's session.json, a // readdir of `.forum-session/` and each host's session.json; // - one channel config per yt-dlp service (the channel of its last clean // request: its own cookiesFromBrowser may be the one yt-dlp uses); // - one name-only read of that browser's cookie store, cached 5 min // (lib/browserCookieAge.ts) — `fresh` reads it again; // - the forum-thread channels (their host and thread): one config read per // channel, never their data/, remembered for a minute while channels/'s // own mtime holds (lib/forumChannels.ts). // // No channel listing, no index, no corpus walk. A read that fails leaves its // row "unknown"; it never fails the page. import { readdir } from "node:fs/promises"; import path from "node:path"; import { getPaths, type Paths } from "yt-dlp-transcript-common/lib/paths"; import { getSettings, type SiteSettings } from "yt-dlp-transcript-common/lib/settings"; import { displayPath, displayText } from "yt-dlp-transcript-common/lib/privacy/privacy-server"; import { readServiceCookieState } from "yt-dlp-transcript-common/lib/serviceCookies-server"; import { buildCookieStatus, type CookieStatusInput, type CookieStatusPayload, type YtdlpCookieInput } from "yt-dlp-transcript-common/lib/cookieStatus"; import { BROWSER_COOKIE_SERVICES, readBrowserCookieAges } from "yt-dlp-transcript-common/lib/browserCookieAge"; import { resolveCookiePolicy } from "yt-dlp-transcript-common/lib/cookiePolicy"; import { isValidChannelSlug, readChannelConfig } from "yt-dlp-transcript-common/controller/channels"; import { readXSessionStatus, xProfileDir } from "yt-dlp-transcript-common/social/xSessionBroker"; import { resolveXCookieSourceFor } from "yt-dlp-transcript-common/social/xBrowserLogin"; import { archiveSessionDir, readArchiveSessionStatus } from "yt-dlp-transcript-common/lib/archives/archiveTodaySession"; import { forumSessionDir, readForumSessionStatus } from "yt-dlp-transcript-common/social/forumSession"; import { loadCookieAlerts } from "./cookieAlerts"; import { listForumChannels } from "./forumChannels"; import { runningCookieActions } from "./cookieRefreshGuard"; import { xSessionReport } from "yt-dlp-transcript-common/jobs/networkGuard"; import type { XSittingView } from "yt-dlp-transcript-common/lib/cookieStatus"; async function safe(fn: () => Promise, fallback: T): Promise { try { return await fn(); } catch { return fallback; } } export async function loadCookieStatus( paths: Paths = getPaths(), opts: { now?: number; fresh?: boolean } = {}, ): Promise { const now = opts.now ?? Date.now(); let settings: SiteSettings | null = null; try { settings = getSettings(); } catch { settings = null; } const [alerts, state] = await Promise.all([ safe(() => loadCookieAlerts(paths, now), { alerts: [], pending: [] }), readServiceCookieState(paths), ]); // X: the source in use and the profile. const session = await safe(() => readXSessionStatus(paths), { hasProfile: false, hasCookies: false, looksAuthenticated: false }); const resolved = settings ? await safe(() => resolveXCookieSourceFor(paths, settings!), null) : null; const x: CookieStatusInput["x"] = { source: resolved?.source ?? "profile", ...(resolved?.browserSpec ? { browserSpec: resolved.browserSpec } : {}), profileDir: xProfileDir(paths), hasProfile: session.hasProfile, loggedIn: session.looksAuthenticated, ...(alerts.x ? { jar: alerts.x } : {}), }; // The yt-dlp services: the spec yt-dlp uses (the last clean request's // channel's own, else settings'), and its store's ages — one read per spec. const ytdlp = {} as CookieStatusInput["ytdlp"]; for (const svc of BROWSER_COOKIE_SERVICES) { const channel = state.lastGood[svc]?.channel ?? state.services[svc]?.evidence.channel; // A slug from the state file, checked before it names a path. const config = channel && isValidChannelSlug(channel) ? await safe(() => readChannelConfig(paths, channel), null) : null; const own = config?.cookiesFromBrowser?.trim(); const spec = settings ? resolveCookiePolicy(settings, config ?? undefined).cookies : own || undefined; const age = await readBrowserCookieAges(spec, { now, ...(opts.fresh ? { fresh: true } : {}) }); const entry: YtdlpCookieInput = { ...(spec ? { spec } : {}), specFrom: own ? "channel" : "settings", ...(own && channel ? { channel } : {}), age: age.services[svc], }; ytdlp[svc] = entry; } const archiveStatus = await safe(() => readArchiveSessionStatus(paths), null); const archive: CookieStatusInput["archive"] = { dir: archiveSessionDir(paths), hasProfile: archiveStatus?.hasProfile ?? false, ...(archiveStatus?.lastConnect ? { connectedAt: archiveStatus.lastConnect.connectedAt, cleared: archiveStatus.lastConnect.cleared } : {}), }; const hosts = await safe( async () => (await readdir(path.join(paths.transcriptsDir, ".forum-session"), { withFileTypes: true })) .filter((d) => d.isDirectory()) .map((d) => d.name), [] as string[], ); const forums: CookieStatusInput["forums"] = []; for (const host of hosts) { const st = await safe(() => readForumSessionStatus(paths, host), null); if (!st) continue; forums.push({ host, dir: forumSessionDir(paths, host), hasProfile: st.hasProfile, ...(st.lastConnect ? { connectedAt: st.lastConnect.connectedAt, cleared: st.lastConnect.cleared } : {}), }); } const forumChannels = await safe(() => listForumChannels(paths, { fresh: opts.fresh === true }), []); // X's sitting and account (lane x-sittings), read from disk: nothing asks X. const report = await safe(() => xSessionReport({ paths, now }), null); const xSitting: XSittingView | undefined = report ? { ...report.sitting, account: report.account, ...(report.cooldown ? { cooldownUntil: report.cooldown.until } : {}), } : undefined; const payload = buildCookieStatus({ now, alerts, state, x, ytdlp, archive, forums, forumChannels, running: runningCookieActions(), display: { path: displayPath, text: displayText }, }); return xSitting ? { ...payload, xSitting } : payload; }