app.ts

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