1import type { Hono } from "hono";2import { headId, saveByPrefix } from "../mirror/queries.ts";3import { notFound, openPublicRepo, type PageContext } from "../pages/layout.ts";4import { countInstall } from "../releases/releases.ts";5import { cachedTarball } from "./cache.ts";6789const TARBALL_CACHE = "private, max-age=31536000";1011export function registerTarballRoutes(app: Hono, ctx: PageContext, tarballDir: string): void {12 app.get("/:repo/archive/HEAD.tar.gz", (c) => {13 const repo = openPublicRepo(ctx, c.req.param("repo"));14 const head = repo && headId(repo.mirror);15 if (!repo || !head) return notFound(c, ctx);16 const path = `/${repo.settings.name}/archive/${head}.tar.gz`;17 18 c.header("Link", `<https://${ctx.domain}${path}>; rel="immutable"`);19 c.header("Cache-Control", "no-cache");20 return c.redirect(path, 302);21 });2223 app.get("/:repo/archive/:id{[0-9a-f]+\\.tar\\.gz}", (c) => {24 const repo = openPublicRepo(ctx, c.req.param("repo"));25 const id = c.req.param("id").replace(/\.tar\.gz$/, "");26 const save = repo && saveByPrefix(repo.mirror, id);27 if (!repo || !save) return notFound(c, ctx);28 const name = repo.settings.name;29 if (save.id !== id) {30 c.header("Cache-Control", "public, max-age=60");31 return c.redirect(`/${name}/archive/${save.id}.tar.gz`, 302);32 }33 const bytes = cachedTarball(tarballDir, repo.mirror, name, save.id);34 35 if (c.req.header("User-Agent")?.includes("nix/")) countInstall(ctx.kepr, name);36 c.header("Content-Type", "application/gzip");37 c.header("Content-Disposition", `attachment; filename="${name}-${save.id.slice(0, 8)}.tar.gz"`);38 c.header("Cache-Control", TARBALL_CACHE);39 return c.body(bytes);40 });41}