import { request } from './http.js';
import type { TreeFile } from '../repo-trees.js';
const seen = new Map<string, Promise<TreeFile[]>>();
const info = (repo: string) => request<{ defaultBranch: string; head: string | null }>('GET', `/api/repos/${repo}`);
// Every file of `owner/name` at a branch or commit (its default branch when none), with sizes; empty for a repository nothing was pushed to.
export const repoFiles = (owner: string, name: string, rev?: string): Promise<TreeFile[]> => {
const key = `${owner}/${name}@${rev ?? ''}`;
let found = seen.get(key);
if (!found) {
found = (async () => {
const at = rev ?? (await info(`${owner}/${name}`)).defaultBranch;
const list = await request<{ files: { path: string; size: number | null }[] }>('GET', `/api/repos/${owner}/${name}/files/${encodeURIComponent(at)}`);
return list.files.map((f) => ({ path: f.path, size: f.size ?? 0 }));
})();
seen.set(key, found);
found.catch(() => seen.delete(key));
}
return found;
};
// The repository's default branch and the commit it points at (null before the first push).
export const repoBranch = async (owner: string, name: string) => info(`${owner}/${name}`);