// A REMOTE-ONLY CHANNEL, registered on first need (demo epic D1 item 5). // // A sweep over a connected archive (settings.remoteArchives) cites moments of // channels this editor does not hold. Its clip windows are still fetched HERE — // the editor's paced queues, cookie policy and provenance — so the editor needs // a channel to put them under. The first time a window of such a channel is // asked for, with the archive named, the editor registers it: // // config only (`remoteOnly: true`, `remoteArchive`, `remoteUrl`, the // platform, `downloadMode: "manual"`), and NO `url` — nothing that syncs a // channel by its url can sync this one; no listing sweep is run. // // Every place a real channel is active skips it (lib/channelConfig.ts // isRemoteOnlyChannel): the lanes, the sync scheduler, Sync all and every // pipeline action, the metadata scan, cleanup, the storage watch, index and // stats builds, the site writer, a research run's channel list. The channel // list shows it as "remote only". // // A cited recording's page URL — what a window is fetched from — comes from // the archive's own record (archiveRecordOf), since the editor holds no // metadata for it; the ask may carry it too (umtool's chain sends it). // // A WHOLE-RECORDING PLATFORM (settings.clips.wholeRecordingPlatforms, Rumble): // a window there is cut from the saved whole recording — for a channel this // editor does not archive that is a whole download nobody planned. It is // refused with the recording's size (wholeRecordingRefusal) until the ask says // `allowWholeRecording: true` (one press: umtool's chain offers it). import type { Paths } from "../lib/paths"; import type { ChannelConfig } from "../lib/channelConfig"; import { PLATFORM_VALUES, type Platform } from "../lib/platform"; import { corpusUrl, manifestUrl, pageUrl } from "../lib/archive/contract"; import { WHOLE_RECORDING_REFUSED, archiveOriginOf, recordPage, sameOrigin, type ConnectedArchive } from "../lib/remoteArchives"; import { archiveChannelFacts, plainGetJson, type ArchiveChannelFacts, type GetJson } from "../lib/remoteArchives-server"; import { createChannel, readChannelConfig } from "./channels"; export type RemoteOnlyDeps = { // One JSON document over HTTP (the cached reader, item 6); a test's fake. getJson?: GetJson; facts?: (origin: string, slug: string, opts: { getJson?: GetJson }) => Promise; }; export class RemoteOnlyChannelError extends Error {} /** The config a remote-only channel is registered with, from its archive's facts. */ export function remoteOnlyConfigOf(archive: Pick, facts: ArchiveChannelFacts): ChannelConfig { const platform = facts.platform && (PLATFORM_VALUES as readonly string[]).includes(facts.platform) ? (facts.platform as Platform) : undefined; return { name: facts.name, handling: platform === "youtube" ? "youtube" : "transcribe", ...(platform ? { platform } : {}), remoteOnly: true, remoteArchive: archive.origin, ...(facts.url ? { remoteUrl: facts.url } : {}), downloadMode: "manual", } as ChannelConfig; } /** * The channel's config: the one on disk, or — when there is none — a * remote-only channel registered now from `archive`'s corpus.json. A channel * the archive does not list is refused. */ export async function ensureRemoteOnlyChannel( paths: Paths, slug: string, archive: Pick, deps: RemoteOnlyDeps = {}, ): Promise<{ config: ChannelConfig; registered: boolean }> { const have = await readChannelConfig(paths, slug); if (have) return { config: have, registered: false }; const facts = await (deps.facts ?? archiveChannelFacts)(archive.origin, slug, { getJson: deps.getJson }); const config = remoteOnlyConfigOf(archive, facts); try { await createChannel(paths, slug, config); } catch (e) { // Two asks at once: the other registered it first. const again = await readChannelConfig(paths, slug); if (again) return { config: again, registered: false }; throw e; } return { config: (await readChannelConfig(paths, slug)) ?? config, registered: true }; } export type ArchiveRecord = { webpageUrl?: string; duration?: number; platform?: string; title?: string }; /** * A recording's own record in its archive: the transcripts manifest's page for * the id, then the record whose id matches — its page URL and duration. Null * when the archive does not hold it. At most three documents, in order. */ export async function archiveRecordOf(origin: string, slug: string, id: string, deps: { getJson?: GetJson } = {}): Promise { const base = archiveOriginOf(origin); if (!base) return null; const getJson = deps.getJson ?? plainGetJson(); const corpus = (await getJson(corpusUrl(base)).catch(() => null)) as { channels?: { slug?: string; manifests?: { transcripts?: string } }[] } | null; const entry = corpus?.channels?.find((c) => c?.slug === slug); // Only the archive's own origin is read (an archive is untrusted input): a // manifest its corpus.json names elsewhere is not followed. const named = entry?.manifests?.transcripts; const at = typeof named === "string" && sameOrigin(named, base) ? named : manifestUrl("transcripts", slug, base); const manifest = (await getJson(at).catch(() => null)) as { slugToPage?: Record } | null; const page = manifest?.slugToPage?.[id] ?? manifest?.slugToPage?.[`${slug}/${id}`]; if (typeof page !== "number") return null; const records = (await getJson(pageUrl("transcripts", slug, page, base)).catch(() => null)) as Record[] | null; const r = (Array.isArray(records) ? records : []).find((x) => x?.id === id); if (!r) return null; const s = (k: string) => (typeof r[k] === "string" && (r[k] as string).trim() ? (r[k] as string).trim() : undefined); const duration = typeof r.duration === "number" && Number.isFinite(r.duration) ? r.duration : undefined; const webpageUrl = s("webpageUrl"); return { ...(webpageUrl && /^https?:\/\//i.test(webpageUrl) ? { webpageUrl } : {}), ...(duration !== undefined ? { duration } : {}), ...(s("platform") ? { platform: s("platform") } : {}), ...(s("title") ? { title: s("title") } : {}), }; } /** * The page URL an ask for a window carries for a recording of `origin`'s * channel: its archive record's, JUDGED AND NORMALISED (lib/remoteArchives.ts * recordPage) — the URL the check parsed, written back — or null when the * archive holds no record, names no page, or names one that is refused. Never * the record's own string (umtool's chain sends this to the editor, which * judges it again). */ export async function archiveRecordPageHref( origin: string, slug: string, id: string, detect: (u: string) => string | null, deps: { getJson?: GetJson } = {}, ): Promise { const record = await archiveRecordOf(origin, slug, id, deps).catch(() => null); if (!record?.webpageUrl) return null; const page = recordPage(record.webpageUrl, null, detect); return "refused" in page ? null : page.href; } // A whole recording's size, ESTIMATED from its length: what the saved-video // store's copy weighs at about this height. Approximate on purpose (no request // is made to learn it): video of this kind runs ~0.8 Mb/s at 360p up to ~5 Mb/s // at 1080p, audio included. const MBPS_BY_HEIGHT: [number, number][] = [ [360, 0.8], [480, 1.2], [720, 2.5], [1080, 5], ]; export function estimateRecordingBytes(durationSec: number, maxHeight = 720): number { const mbps = (MBPS_BY_HEIGHT.find(([h]) => maxHeight <= h) ?? MBPS_BY_HEIGHT[MBPS_BY_HEIGHT.length - 1])[1]; return Math.round((durationSec * mbps * 1e6) / 8); } const clock = (s: number) => { const h = Math.floor(s / 3600); const m = Math.floor((s % 3600) / 60); const sec = Math.floor(s % 60); return h > 0 ? `${h}:${String(m).padStart(2, "0")}:${String(sec).padStart(2, "0")}` : `${m}:${String(sec).padStart(2, "0")}`; }; const gb = (bytes: number) => (bytes >= 1e9 ? `${(bytes / 1e9).toFixed(1)} GB` : `${Math.max(1, Math.round(bytes / 1e6))} MB`); export { WHOLE_RECORDING_REFUSED }; /** * The refusal of a remote-only channel's window on a whole-recording platform: * the recording's length and estimated size, and the one key that fetches it. */ export function wholeRecordingRefusal(o: { platform: string; slug: string; id: string; duration?: number; maxHeight?: number }): string { const name = o.platform.charAt(0).toUpperCase() + o.platform.slice(1); const size = o.duration !== undefined ? `${o.slug}/${o.id} is ${clock(o.duration)} long, about ${gb(estimateRecordingBytes(o.duration, o.maxHeight))} (estimated)` : `${o.slug}/${o.id}'s length is not known to its archive`; return ( `${WHOLE_RECORDING_REFUSED} ${name} windows are cut from the WHOLE recording, and ${o.slug} is a remote-only channel: ` + `${size}. Send "allowWholeRecording": true to download it whole (once; its windows are cut from it).` ); }