app.ts

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