app.ts

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