// CONNECT AN ARCHIVE (demo epic D1, item 1): the one place // settings.remoteArchives is changed. The editor's ops action // (`pnpm ops archives`) and its /sites form call these; umtool's Source chip // asks the editor's action — so every connect is probed the same way and // written by the editor's one settings writer. // // connect probe `/corpus.json` (lib/remoteArchives-server.ts) and, // unless `dryRun`, add { id, origin, title, addedAt }. An origin // already connected answers what it is and writes nothing. // remove drop the entry by id (or origin). The TRANSCRIPT_SITE_URL entry // is not stored, so it cannot be removed here: it goes with the // variable. import { connectedArchives, findConnectedArchive, freeArchiveId, sanitizeRemoteArchives, archiveOriginOf, type ConnectedArchive, type RemoteArchive } from "../lib/remoteArchives"; import { ArchiveProbeError, probeArchive, type ArchiveProbe, type FetchLike } from "../lib/remoteArchives-server"; import { privateHostRefusal, type LookupAll } from "../lib/archiveHosts-server"; export type RemoteArchiveDeps = { // The stored list (settings.remoteArchives). stored: () => unknown; // Write the whole list (the editor's saveSettings({ remoteArchives })). save: (next: RemoteArchive[]) => Promise; fetch?: FetchLike; now?: () => Date; env?: Record; // The host check's name lookup (archiveHosts-server.ts); a test's fake. lookup?: LookupAll; }; export type ConnectResult = { probe: ArchiveProbe; archive: ConnectedArchive; // true: it was connected already (nothing written); false: added now; null: a dry run. already: boolean | null; }; export async function connectRemoteArchive(o: { origin: string; dryRun?: boolean }, deps: RemoteArchiveDeps): Promise { // This machine or its network: refused before anything is asked of it, // unless decided here (archiveHosts-server.ts). const origin = archiveOriginOf(o.origin); const env = deps.env ?? process.env; if (origin && env.ARCHILYZER_ALLOW_PRIVATE_ARCHIVES !== "1" && !findConnectedArchive(connectedArchives(deps.stored(), env), origin)) { const refused = await privateHostRefusal(origin, { lookup: deps.lookup }); if (refused) throw new ArchiveProbeError(refused); } const probe = await probeArchive(o.origin, { fetch: deps.fetch }); const list = connectedArchives(deps.stored(), deps.env); const have = findConnectedArchive(list, probe.origin); if (have && !have.fromEnv) return { probe, archive: have, already: true }; const stored = sanitizeRemoteArchives(deps.stored()); const archive: RemoteArchive = { id: freeArchiveId(probe.origin, stored.map((a) => a.id), probe.siteId), origin: probe.origin, title: probe.title, addedAt: (deps.now?.() ?? new Date()).toISOString(), }; if (o.dryRun) return { probe, archive, already: null }; await deps.save([...stored, archive]); return { probe, archive, already: false }; } export class RemoteArchiveError extends Error {} /** Remove a stored archive by id or origin; answers the removed entry. */ export async function removeRemoteArchive(which: string, deps: RemoteArchiveDeps): Promise { const stored = sanitizeRemoteArchives(deps.stored()); const origin = archiveOriginOf(which); const hit = stored.find((a) => a.id === which || (origin !== null && a.origin === origin)); if (!hit) { const env = connectedArchives(deps.stored(), deps.env).find((a) => a.fromEnv && (a.id === which || a.origin === origin)); if (env) throw new RemoteArchiveError(`${env.origin} comes from TRANSCRIPT_SITE_URL, not settings.json — unset the variable to stop offering it`); throw new RemoteArchiveError(`no connected archive "${which}" — connected: ${stored.map((a) => a.id).join(", ") || "none"}`); } await deps.save(stored.filter((a) => a !== hit)); return hit; }