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 { registerArchivesPage } from "../pages/archives.ts";9import { registerFileRoutes } from "../pages/files.ts";10import { registerReleasesPage } from "../pages/releases.ts";11import { registerRepoPage } from "../pages/repo.ts";12import { registerSavePage } from "../pages/save.ts";13import { registerReleaseRoutes } from "../releases/routes.ts";14import { registerTarballRoutes } from "../tarball/archive.ts";15import type { Config } from "./config.ts";1617const MAX_BODY_BYTES = 64 * 1024;18const PUBLIC_DIR = join(import.meta.dir, "../../public");1920const FONTS = "https://fonts.bunny.net";212223export function createApp(config: Config, kepr: Database): Hono {24 const app = new Hono();25 26 const imgSrc =27 config.bucket.kind === "s3" ? ["'self'", new URL(config.bucket.endpoint).origin] : ["'self'"];28 app.use(29 secureHeaders({30 contentSecurityPolicy: {31 defaultSrc: ["'self'"],32 scriptSrc: ["'self'"],33 styleSrc: ["'self'", FONTS],34 fontSrc: [FONTS],35 imgSrc,36 objectSrc: ["'none'"],37 baseUri: ["'none'"],38 formAction: ["'self'"],39 frameAncestors: ["'none'"],40 },41 xFrameOptions: "DENY",42 }),43 );44 app.use(45 bodyLimit({ maxSize: MAX_BODY_BYTES, onError: (c) => c.text("Request body too large", 413) }),46 );47 app.get("/static/:path{.+}", async (c) => {48 const path = c.req.param("path");49 const file = Bun.file(join(PUBLIC_DIR, path));50 if (!isSafePath(path) || !(await file.exists())) return c.notFound();51 c.header("Cache-Control", "public, max-age=3600");52 return c.body(await file.bytes(), 200, { "Content-Type": file.type });53 });54 const ctx = { domain: config.domain, kepr, mirrorDir: join(config.dataDir, "mirror") };55 registerRepoPage(app, ctx);56 registerFileRoutes(app, ctx);57 registerSavePage(app, ctx);58 registerArchivesPage(app, ctx);59 registerReleasesPage(app, ctx);60 registerTarballRoutes(app, ctx, join(config.dataDir, "tarballs"));61 registerReleaseRoutes(app, ctx, openBucket(config.bucket));62 return app;63}