app.ts

3.8 KB · raw

1import type { Database } from "bun:sqlite";2import { join } from "node:path";3import { Hono } from "hono";4import { bodyLimit } from "hono/body-limit";5import { secureHeaders } from "hono/secure-headers";6import { registerStatus } from "../admin/console.ts";7import { registerIssues } from "../admin/issues.ts";8import { registerReleases } from "../admin/releases.ts";9import { registerRepos } from "../admin/repos.ts";10import { openBucket } from "../bucket/reader.ts";11import { isSafePath } from "../bucket/repo.ts";12import { registerIssuePages } from "../issues/pages.ts";13import { registerArchivesPage } from "../pages/archives.ts";14import { registerDocsPages } from "../pages/docs.ts";15import { registerFileRoutes } from "../pages/files.ts";16import { registerFrontPages } from "../pages/front.ts";17import { registerHousekeeping } from "../pages/housekeeping.ts";18import { registerProfilePage } from "../pages/profile.ts";19import { registerReleasesPage } from "../pages/releases.ts";20import { registerRepoPage } from "../pages/repo.ts";21import { registerSavePage } from "../pages/save.ts";22import { registerReleaseRoutes } from "../releases/routes.ts";23import { registerTarballRoutes } from "../tarball/archive.ts";24import type { Config } from "./config.ts";25import { registerLogin } from "./login.ts";2627const MAX_BODY_BYTES = 64 * 1024;28const PUBLIC_DIR = join(import.meta.dir, "../../public");29// §18 allows Bunny Fonts: its stylesheet and the font files it points at.30const FONTS = "https://fonts.bunny.net";3132/** The Hono app with §25's headers and body cap, and every route built so far. */33export function createApp(config: Config, kepr: Database): Hono {34	const app = new Hono();35	// §25 allows images from the presigned host, which is the bucket endpoint's origin.36	const imgSrc =37		config.bucket.kind === "s3" ? ["'self'", new URL(config.bucket.endpoint).origin] : ["'self'"];38	app.use(39		secureHeaders({40			contentSecurityPolicy: {41				defaultSrc: ["'self'"],42				scriptSrc: ["'self'"],43				styleSrc: ["'self'", FONTS],44				fontSrc: [FONTS],45				imgSrc,46				objectSrc: ["'none'"],47				baseUri: ["'none'"],48				formAction: ["'self'"],49				frameAncestors: ["'none'"],50			},51			xFrameOptions: "DENY",52		}),53	);54	app.use(55		bodyLimit({ maxSize: MAX_BODY_BYTES, onError: (c) => c.text("Request body too large", 413) }),56	);57	// `/static/assets/` is the operator's uploads in `data/assets/` (§19), such as the signature.58	const assetsDir = join(config.dataDir, "assets");59	app.get("/static/:path{.+}", async (c) => {60		const path = c.req.param("path");61		const file = Bun.file(62			path.startsWith("assets/") ? join(assetsDir, path.slice(7)) : join(PUBLIC_DIR, path),63		);64		if (!isSafePath(path) || !(await file.exists())) return c.notFound();65		c.header("Cache-Control", "public, max-age=3600");66		return c.body(await file.bytes(), 200, { "Content-Type": file.type });67	});68	const ctx = { domain: config.domain, kepr, mirrorDir: join(config.dataDir, "mirror") };69	const bucket = openBucket(config.bucket);70	// Before every other route, so the admin guard runs ahead of each `/admin` handler.71	registerLogin(app, ctx, config.adminPasswordHash);72	const admin = {73		...ctx,74		dataDir: config.dataDir,75		sync: { bucket, kepr, mirrorDir: ctx.mirrorDir, tarballDir: join(config.dataDir, "tarballs") },76	};77	registerStatus(app, admin);78	registerRepos(app, admin);79	registerIssues(app, admin);80	registerReleases(app, admin);81	registerHousekeeping(app, ctx);82	registerFrontPages(app, ctx);83	registerDocsPages(app, ctx);84	registerProfilePage(app, ctx);85	registerRepoPage(app, ctx);86	registerFileRoutes(app, ctx);87	registerSavePage(app, ctx);88	registerArchivesPage(app, ctx);89	registerReleasesPage(app, ctx);90	registerIssuePages(app, ctx);91	registerTarballRoutes(app, ctx, join(config.dataDir, "tarballs"));92	registerReleaseRoutes(app, ctx, bucket);93	return app;94}