import type { Database } from "bun:sqlite"; import type { Context } from "hono"; import { openMirror } from "../mirror/store.ts"; import { findRepo, type RepoSettings, setting } from "../server/db.ts"; import { signedIn } from "../server/login.ts"; import { type Html, html } from "./html.ts"; export type PageContext = { domain: string; kepr: Database; mirrorDir: string }; export type PublicRepo = { settings: RepoSettings; mirror: Database; private?: boolean }; // 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"; /** Callers must verify the operator session before opening a private repo. */ export function openRepo(ctx: PageContext, name: string, visibility: 0 | 1 = 1): PublicRepo | null { const settings = findRepo(ctx.kepr, name, visibility); if (!settings) return null; return { settings, mirror: openMirror(ctx.mirrorDir, name), private: visibility === 0 }; } export const openPublicRepo = (ctx: PageContext, name: string) => openRepo(ctx, name); export function openBrowserRepo(ctx: PageContext, c: Context, name: string): PublicRepo | null { const repo = openPublicRepo(ctx, name); if (repo || !signedIn(ctx, c)) return repo; c.header("Cache-Control", "no-store"); return openRepo(ctx, name, 0); } export function cacheFor(c: Context, atSave: boolean): void { if (c.res.headers.get("Cache-Control") === "no-store") return; 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`

Not found

Nothing lives at this address.

`; return c.html( page(ctx, { title: "Not found", description: "", path: c.req.path, body }, true), 404, ); } type PageOptions = { title: string; description: string; path: string; body: Html }; export function browserPage(c: Context, ctx: PageContext, options: PageOptions): Response { return c.html(page(ctx, options, c.res.headers.get("Cache-Control") === "no-store")); } export function page(ctx: PageContext, options: PageOptions, privateView = false): string { const site = setting(ctx.kepr, "site_name") ?? "kepr"; const branding = setting(ctx.kepr, "branding"); // Visitors are counted, never the operator's console or sign-in. const tracked = !privateView && !/^\/(admin|sign-in|connect)(\/|$)/.test(options.path); const umamiSrc = tracked && setting(ctx.kepr, "umami_src"); const umamiId = setting(ctx.kepr, "umami_id"); // 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` ${title} ${umamiSrc && umamiId && html``}
${options.body}
`.value; } const UNITS: [number, string][] = [ [365 * 86400, "year"], [30 * 86400, "month"], [7 * 86400, "week"], [86400, "day"], [3600, "hour"], [60, "minute"], ]; export function relativeTime(unixSeconds: number, now = Date.now() / 1000): string { const elapsed = now - unixSeconds; for (const [seconds, unit] of UNITS) { const n = Math.floor(elapsed / seconds); if (n >= 1) return `${n} ${unit}${n === 1 ? "" : "s"} ago`; } return "just now"; } export function formatDate(unixSeconds: number): string { return new Date(unixSeconds * 1000).toISOString().slice(0, 16).replace("T", " "); } export function plural(n: number, noun: string): string { return `${n.toLocaleString("en-US")} ${noun}${n === 1 ? "" : "s"}`; } export function formatSize(bytes: number): string { if (bytes < 1024) return `${bytes} B`; if (bytes < 1024 * 1024) return `${(bytes / 1024).toFixed(1)} KB`; return `${(bytes / 1024 / 1024).toFixed(1)} MB`; } /** Percent-encodes each segment of a repo path for use in a URL. */ export function pathUrl(path: string): string { return path.split("/").map(encodeURIComponent).join("/"); }