// STORED TEXT — a big, rarely read text file kept on disk as one zstd frame, // under its own name plus `.zst` (`transcript.live_chat.json.zst`), and read // back as if it were the plain file. Pure: names only, no fs (the read path is // lib/storedText-server.ts, the compressor and the one deleter // lib/storedText-write-server.ts). // // DECLARED NAMES ONLY. A name is stored compressed only when it is declared // (`isStoredTextName`); for every other name `.zst` means nothing here. // Two kinds are declared: the raw live-chat replay (STORED_TEXT_NAMES — lane // text-compress, slices 1–2: the media drive's tens of GB of chat) and EVERY // CAPTION TRACK, `transcript..vtt` (lane text-compress-captions, slices // 4–5: about 77 GB on the corpus disk; `isStoredCaptionName`). // `metadata.info.json` is not declared (a design note only). // // THE RULE: PLAIN, WHEN PRESENT, IS THE TRUTH. Every writer keeps writing the // plain name (yt-dlp, the chat fetch); none has to know about the stored form. // When both are present the plain one is either the latest write or a // compression that has not finished — reading it is right in both cases. // // TWO KINDS OF LISTING. One used to MATCH names (is the chat on disk, which // tracks are there, what to fetch) is LOGICAL: `logicalEntries` reports each // declared `X.zst` as `X`. One used to OPERATE on every entry as a file (the // files card, the movers, the byte walk) stays PHYSICAL: there the `.zst` is a // file with its own name and size. import { LIVE_CHAT_MEDIA_FILENAME } from "./mediaTier"; export const STORED_TEXT_SUFFIX = ".zst"; // The FIXED names stored compressed. One state per name, whatever its size: // simpler to test than "compressed when big". export const STORED_TEXT_NAMES: readonly string[] = Object.freeze([LIVE_CHAT_MEDIA_FILENAME]); const DECLARED = new Set(STORED_TEXT_NAMES); // EVERY CAPTION TRACK: `transcript..vtt`, any language code (videoStatus // `isTranscriptVtt`'s pattern, less the temp below — storedText.test.ts holds // them together; this module cannot import videoStatus, which imports it). The en / en-orig pair, // the regional and auto-translated tracks, the foreign-language subtitles. // Not a transcriber's temp output (`transcript.tmp-.vtt`, // controller/transcribeOne.ts): somebody's in-flight bytes, never stored. const CAPTION_RE = /^transcript\.(?!tmp-\d+\.vtt$)[^.]+\.vtt$/; export function isStoredCaptionName(name: string): boolean { return CAPTION_RE.test(name); } export function isStoredTextName(name: string): boolean { return DECLARED.has(name) || CAPTION_RE.test(name); } export function storedNameOf(name: string): string { return `${name}${STORED_TEXT_SUFFIX}`; } // The plain name a stored file stands for, or null when `name` is not the // stored form of a declared name. export function plainNameOf(name: string): string | null { if (!name.endsWith(STORED_TEXT_SUFFIX)) return null; const plain = name.slice(0, -STORED_TEXT_SUFFIX.length); return isStoredTextName(plain) ? plain : null; } // The name a listing that MATCHES reports for this entry. export function logicalNameOf(name: string): string { return plainNameOf(name) ?? name; } // A readdir's names as a matching listing: each declared `X.zst` reported as // `X`, deduped (both forms present are one file), first-seen order kept. export function logicalEntries(entries: Iterable): string[] { const out: string[] = []; const seen = new Set(); for (const e of entries) { const n = logicalNameOf(e); if (seen.has(n)) continue; seen.add(n); out.push(n); } return out; } // Does this PHYSICAL listing hold `name` in either form? export function listingHas(entries: readonly string[], name: string): boolean { if (entries.includes(name)) return true; return isStoredTextName(name) && entries.includes(storedNameOf(name)); } export type StoredForms = "plain" | "zst" | "both"; // Which forms of a declared `name` a physical listing holds, or null for none. export function storedFormsIn(entries: readonly string[], name: string): StoredForms | null { const plain = entries.includes(name); const zst = isStoredTextName(name) && entries.includes(storedNameOf(name)); if (plain && zst) return "both"; if (plain) return "plain"; if (zst) return "zst"; return null; } // THE CAPTION TRACKS ON DISK IN THE STORED FORM ONLY, by their track code // (`transcript.en-orig.vtt.zst` with no plain `transcript.en-orig.vtt` → // "en-orig"), from a PHYSICAL listing. These are what a yt-dlp subtitle pass // must not be asked for: yt-dlp's own "already present" skip looks for the // plain name, so it would fetch the track again and land a second copy beside // the stored one (a request against the subtitle 429 limit, and two forms). export function storedOnlyCaptionTracks(entries: readonly string[]): string[] { const have = new Set(entries); const out: string[] = []; for (const e of entries) { const plain = plainNameOf(e); if (plain === null || !CAPTION_RE.test(plain) || have.has(plain)) continue; out.push(plain.slice("transcript.".length, -".vtt".length)); } return out.sort(); } // `--sub-langs` with these tracks EXCLUDED: each appended as `-`, its // regex characters escaped (yt-dlp matches a code as a regex anchored at both // ends, and applies the exclusions after the requests before them, so // `en.*,-en` asks every English track but `en`). Unchanged when there are none. export function excludeSubTracks(subLangs: string, tracks: readonly string[]): string { if (tracks.length === 0) return subLangs; const esc = (t: string) => t.replace(/[.*+?^${}()|[\]\\]/g, "\\$&"); return [subLangs, ...tracks.map((t) => `-${esc(t)}`)].join(","); }