import type { Database } from "bun:sqlite"; import type { Context } from "hono"; import { openMirror } from "../mirror/store.ts"; import { publicRepo, type RepoSettings, setting } from "../server/db.ts"; import { type Html, html } from "./html.ts"; export type PageContext = { domain: string; kepr: Database; mirrorDir: string }; export type PublicRepo = { settings: RepoSettings; mirror: Database }; // Static files are cached for an hour; the release in their URLs makes a new one load at once. const { version } = (await Bun.file(`${import.meta.dir}/../../package.json`).json()) as { version: string; }; const HEAD_CACHE = "public, max-age=60"; // Content at a save never changes, but strip can remove it, so it is long-lived rather than // immutable, and `private`, so no shared cache keeps it after the repo turns private (§20). const SAVE_CACHE = "private, max-age=31536000"; const FONTS = "https://fonts.bunny.net/css?family=bellota:700|comfortaa:700|martian-mono:400|quicksand:400,600|sacramento:400&display=swap"; /** * The gate of §20: a private repo and an unknown one both come back null, and so both 404 alike. */ export function openPublicRepo(ctx: PageContext, name: string): PublicRepo | null { const settings = publicRepo(ctx.kepr, name); return settings ? { settings, mirror: openMirror(ctx.mirrorDir, name) } : null; } export function cacheFor(c: Context, atSave: boolean): void { c.header("Cache-Control", atSave ? SAVE_CACHE : HEAD_CACHE); } export function notFound(c: Context, ctx: PageContext): Response { c.header("Cache-Control", "no-store"); const body = html`
Nothing lives at this address.
`; return c.html(page(ctx, { title: "Not found", description: "", path: c.req.path, body }), 404); } type PageOptions = { title: string; description: string; path: string; body: Html }; export function page(ctx: PageContext, options: PageOptions): string { const site = setting(ctx.kepr, "site_name") ?? "kepr"; const branding = setting(ctx.kepr, "branding"); // The front page is the site itself, so its title is the bare site name. const title = options.title ? `${options.title} — ${site}` : site; const url = `https://${ctx.domain}${options.path}`; return html`