skip commit pages outside the commit budget before rendering them
Signed-off-by: dawn <90008@klbr.net>
34 unmodified lines3536373839404142434445464647484950515253142 unmodified linesExpand all19619719819920020120220320420520620720820921021121218 unmodified lines23123223323423523622323723823924024124224324417 unmodified lines2622632642652662672682692708 unmodified lines2792802812822832842852862872882892902912922932942952962972982993003013023033043053063073081 unmodified line31031131231328028128228331431531631731866 unmodified lines34 unmodified linesconst deps = new AsyncLocalStorage();const reads = { count: 0, ms: 0 };// file sizes from the folder listings this run read, so a file over the budget never rendersconst sizes = new Map();const files = new Set();const sizeKey = (ref, file) => `${ref}\0${file}`;
const learnSizes = (url, body) => { const ref = url.searchParams.get("ref") ?? ""; const dir = url.searchParams.get("path") ?? ""; try { for (const entry of JSON.parse(body).entries ?? []) { sizes.set(sizeKey(ref, dir ? `${dir}/${entry.name}` : entry.name), entry.size ?? 0); const k = sizeKey(ref, dir ? `${dir}/${entry.name}` : entry.name); sizes.set(k, entry.size ?? 0); if (entry.mode && entry.mode !== "0040000" && entry.mode !== "040000") files.add(k); } } catch {}};
142 unmodified linesExpand all const rankOfRoute = (route) => { const parsed = routeOf(prefix, route, repo); return parsed ? rankOf(parsed.view, parsed.depth) : 0; }; const commitSources = [prefix, `${prefix}/commits/${repo.default_branch}`, `${prefix}/branches`, `${prefix}/tags`]; const commitSourceSet = new Set(commitSources); const commitsRoute = `${prefix}/commits/${repo.default_branch}`; const currentAllowedCommits = () => { const order = commitSources .flatMap((route) => pages[route]?.links ?? []) .filter((route) => route.startsWith(`${prefix}/commit/`)); return new Set([...new Set(order)].slice(0, budget.commits)); };
while (todo.length > 0) { checkCurrent(); const batch = todo.sort((a, b) => rankOfRoute(a) - rankOfRoute(b) || (a < b ? -1 : a > b ? 1 : 0));18 unmodified lines if (!accepted(page)) return void results.set(route, { deps }); const gz = await gzip(canonicalHtml(page.html), { level: 9, memLevel: 9 }); // commit pages link their parents and trees at their own commit: only the log // and the front page decide which commits get pages const rawLinks = route.startsWith(`${prefix}/commit/`) ? [] const links = route.startsWith(`${prefix}/commit/`) ? [] : [...linksOf(page.html, origin, (pathname) => routeOf(prefix, pathname, repo))]; : [...linksOf(page.html, origin, (pathname) => routeOf(prefix, pathname, repo))]; const links = commitSourceSet.has(route) ? rawLinks : rawLinks.filter((next) => !next.startsWith(`${prefix}/commit/`)); results.set(route, { deps, gz, links }); }); const writes = []; for (const route of wanted) {17 unmodified lines rendered.add(route); stats.rendered++; writes.push(route); for (const next of kept) { if (next.startsWith(`${prefix}/commit/`)) continue; if (!seen.has(next)) { seen.add(next); todo.push(next); }8 unmodified lines await mkdir(path.dirname(file), { recursive: true }); await link(empty, file); await writeFile(file + ".gz", gz); }); if (commitsRoute in pages || commitsRoute in left || !seen.has(commitsRoute)) { for (const next of currentAllowedCommits()) { if (!seen.has(next)) { seen.add(next); todo.push(next); } } } else if (prefix in pages) { for (const next of pages[prefix].links.filter((r) => r.startsWith(`${prefix}/commit/`)).slice(0, budget.commits)) { if (!seen.has(next)) { seen.add(next); todo.push(next); } } } }
// a link the budget rules out is never rendered, so its url stays the SPA's function keep(next) { const parsed = routeOf(prefix, next, repo); if (over.has(next)) return false; if (parsed.view === "tree" && files.has(sizeKey(parsed.ref, parsed.file))) return false; if (parsed.view === "blob" && (sizes.get(sizeKey(parsed.ref, parsed.file)) ?? 0) > budget.blobBytes) { overBudget.add(next); return false; }1 unmodified line }
// commit pages: the newest the front page and the log link, then branch and tag tips. // the rest go, and come back if a page links them again const commitOrder = [prefix, `${prefix}/commits/${repo.default_branch}`, `${prefix}/branches`, `${prefix}/tags`] .flatMap((route) => pages[route]?.links ?? []) .filter((route) => route.startsWith(`${prefix}/commit/`)); const commits = new Set([...new Set(commitOrder)].slice(0, budget.commits)); const commits = currentAllowedCommits(); const inBudget = (route) => !route.startsWith(`${prefix}/commit/`) || commits.has(route);
const live = reachable(prefix, pages, inBudget); for (const route of Object.keys(pages)) {66 unmodified lines