staging-tester / ocean-landing-serve / src/ lib/ explorer.ts

import { error, redirect } from '@sveltejs/kit';
import { invalidate } from '$app/navigation';
import { HttpError, paths, type BlobView, type Commit, type FileList, type RepoInfo, type TreeView } from '@sailfork/client';
import { api, highlighter, sync } from '#lib/sailfork.js';
import { repoStore } from '#lib/live/repo-store.js';

const seg = (p: string) => p.split('/').filter(Boolean).map(encodeURIComponent).join('/');

export const view = async <T>(path: string, notFound: string): Promise<T> => {
	try {
		return await api.load<T>(path);
	} catch (err) {
		if (err instanceof HttpError) error(err.status === 404 ? 404 : 502, err.status === 404 ? notFound : 'The server could not load this.');
		throw err;
	}
};

export const headDependency = 'app:repo-head';

// Per repository: the head and branch list on screen, the latest RepoInfo seen, and whether its head is watched.
type Seen = { rendered: string | undefined; branches: string; latest: RepoInfo | undefined; watching: boolean };
const seen = new Map<string, Seen>();
const seenFor = (key: string) => {
	let s = seen.get(key);
	if (!s) seen.set(key, (s = { rendered: undefined, branches: '', latest: undefined, watching: false }));
	return s;
};

const idle = (fn: () => void) => ('requestIdleCallback' in window ? window.requestIdleCallback(fn) : setTimeout(fn, 50));

// Highlighting a file is a worker round trip (the first one also loads the grammars), so the likeliest files are highlighted while the page is idle.
let warmedSha = '';
const preHighlight = (owner: string, name: string) => {
	const local = repoStore(owner, name).current();
	if (!local || local.sha === warmedSha) return;
	warmedSha = local.sha;
	const depth = (p: string) => p.split('/').length;
	const todo = [...local.blobs.values()]
		.filter((b) => b.text !== null && !local.lazy.has(b.path) && b.size < 60_000)
		.sort((a, b) => depth(a.path) - depth(b.path) || a.size - b.size)
		.slice(0, 60);
	const next = () => {
		const file = todo.shift();
		if (!file?.text) return;
		void highlighter.highlight(file.path, file.text, file.version).then(() => idle(next), () => idle(next));
	};
	idle(next);
};

const watchHead = (owner: string, name: string, branch: string) => {
	const state = seenFor(`${owner}/${name}`);
	if (state.watching) return;
	state.watching = true;
	const store = repoStore(owner, name);
	let offRepo: (() => void) | undefined;
	// Subscribing to a resource that is already open revalidates it, so re-subscribing is how a refresh is asked for.
	const refresh = () => {
		offRepo?.();
		offRepo = api.resource<RepoInfo>(paths.repo(owner, name)).subscribe((r) => {
			if (!r.data) return;
			state.latest = r.data;
			if (r.data.branches.join() !== state.branches) void invalidate(headDependency);
		});
		store.sync(branch).then(
			() => {
				const sha = store.current()?.sha;
				if (sha && sha !== state.rendered) void invalidate(headDependency);
				preHighlight(owner, name);
			},
			() => undefined
		);
	};
	// Every push changes the row's `head`, and a new branch changes its branch list.
	let first = true;
	sync.repo(`${owner}/${name}`).subscribe(() => {
		if (!first) refresh();
		first = false;
	});
	let last = 0;
	const onReturn = () => {
		if (document.visibilityState !== 'visible' || Date.now() - last < 1000) return;
		last = Date.now();
		refresh();
	};
	document.addEventListener('visibilitychange', onReturn);
	window.addEventListener('focus', onReturn);
	refresh();
};

export type Visit = { owner: string; name: string; base: string; branch: string; commits: Commit[] };

// An empty repository (nothing pushed yet) has no commits to list, so pages show how to push the first one.
export const loadRepo = async (owner: string, name: string): Promise<Visit & { repo: RepoInfo }> => {
	const state = seenFor(`${owner}/${name}`);
	const store = repoStore(owner, name);
	await store.ready();
	const repo = state.latest ?? (await view<RepoInfo>(paths.repo(owner, name), 'No such repository.'));
	const empty = repo.head === null && repo.branches.length === 0;
	const local = store.current();
	// Until the snapshot has downloaded, pages fall back to the per-request API.
	const commits = empty ? [] : (local?.commits ?? (await view<Commit[]>(paths.commits(owner, name, { ref: repo.defaultBranch, limit: 1000 }), 'No such branch.')));
	state.rendered = commits[0]?.sha;
	state.branches = repo.branches.join();
	watchHead(owner, name, repo.defaultBranch);
	if (!empty) preHighlight(owner, name);
	return { owner, name, base: `/${owner}/${name}`, branch: repo.defaultBranch, commits, repo };
};

// The default branch's head, so its responses are addressed by commit sha: a sha never changes, so a cached copy never goes stale.
export const pin = (ref: string, { branch, commits }: Visit) => (ref === branch ? (commits[0]?.sha ?? ref) : ref);

// The local copy, when it is the commit the ref points at.
const copyOf = (ref: string, visit: Visit) => {
	const local = repoStore(visit.owner, visit.name).current();
	return local && local.sha === pin(ref, visit) ? local : undefined;
};

const clean = (path: string) => path.split('/').filter(Boolean).join('/');
const toBlob = (base: string, ref: string, path: string) => `${base}/blob/${encodeURIComponent(ref)}/${seg(path)}`;

const fetchTree = async (ref: string, path: string, visit: Visit): Promise<TreeView> => {
	const local = copyOf(ref, visit);
	const dir = clean(path);
	if (local) {
		const tree = local.trees.get(dir);
		if (tree) return tree;
		if (local.fileSet.has(dir)) redirect(307, toBlob(visit.base, ref, dir));
		error(404, `No folder at ${path} in this commit.`);
	}
	const found = await view<TreeView | BlobView>(paths.tree(visit.owner, visit.name, pin(ref, visit), path), `No folder at ${path} in this commit.`);
	// The API answers 307 to the blob URL when the path is a file and fetch follows it, so a blob comes back here.
	if (!('entries' in found)) redirect(307, toBlob(visit.base, ref, path));
	return found;
};

const blobView = async (ref: string, path: string, visit: Visit): Promise<BlobView> => {
	const local = copyOf(ref, visit);
	const file = clean(path);
	if (local) {
		const held = local.blobs.get(file);
		if (held && !local.lazy.has(file)) return held;
		if (!held && !local.fileSet.has(file)) error(404, `No file at ${path} in this commit.`);
	}
	return view<BlobView>(paths.blob(visit.owner, visit.name, pin(ref, visit), path), `No file at ${path} in this commit.`);
};

export const loadTree = async (ref: string, path: string, visit: Visit) => {
	const found = await fetchTree(ref, path, visit);
	const entry = found.entries.find((e) => !e.dir && e.name.toLowerCase() === 'readme.md');
	const file = entry ? await blobView(ref, entry.path, visit) : null;
	if (!copyOf(ref, visit)) warmFiles(visit, found.sha, found.entries.filter((e) => !e.dir).slice(0, 10).map((e) => e.path));
	return { ...found, ref, readme: entry ? { path: entry.path, text: file?.text ?? '' } : null };
};

export const loadBlob = async (ref: string, path: string, visit: Visit) => {
	const file = await blobView(ref, path, visit);
	return {
		ref,
		sha: file.sha,
		path: file.path,
		size: file.size,
		binary: file.binary,
		tooLarge: file.tooLarge,
		lines: file.text === null ? null : await highlighter.highlight(file.path, file.text, file.version),
		history: file.history,
		// The commit that last touched this file as of the ref, so the history marks the version on screen.
		version: file.history[0]?.sha ?? file.sha,
		commit: await commitAt(file.sha, visit)
	};
};

export const listFiles = async (ref: string, visit: Visit): Promise<{ files: string[]; sha: string }> => {
	const local = copyOf(ref, visit);
	if (local) return { files: local.files, sha: local.sha };
	const sha = pin(ref, visit);
	const list = await view<FileList>(paths.files(visit.owner, visit.name, sha), `No files at ${ref}.`);
	return { files: list.files.map((f) => f.path).sort(), sha };
};

// Folders the root lists are the likeliest next click, so their listings are fetched in the background once the file list is known.
export const warmFolders = (sha: string, files: string[], visit: Visit) => {
	if (repoStore(visit.owner, visit.name).current()?.sha === sha) return;
	const dirs = new Set(files.filter((f) => f.includes('/')).map((f) => f.slice(0, f.indexOf('/'))));
	for (const dir of dirs) void api.prefetch(paths.tree(visit.owner, visit.name, sha, dir));
};

// Files of the folder on screen are the likeliest next click: their contents are fetched and highlighted in the background so the click renders from memory.
const warmFiles = ({ owner, name }: Visit, sha: string, files: string[]) => {
	for (const path of files) {
		void api
			.load<BlobView>(paths.blob(owner, name, sha, path))
			.then((file) => (file.text === null ? null : highlighter.highlight(file.path, file.text, file.version)))
			.catch(() => undefined);
	}
};

export const warmCommits = ({ owner, name, commits }: Visit) => {
	for (const c of commits.slice(0, 10)) void api.prefetch(paths.commit(owner, name, c.sha));
};

export const commitAt = async (sha: string, { owner, name, commits }: Visit): Promise<Commit | null> =>
	commits.find((c) => c.sha === sha) ?? (await view<Commit[]>(paths.commits(owner, name, { ref: sha, limit: 1 }), 'No such commit.'))[0] ?? null;