app.ts

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