import { readFile } from "fs/promises"; import path from "path"; import { auth } from "@/lib/auth"; const PIPELINE_ROOT = process.env.PIPELINE_ROOT || path.join(process.cwd(), "pipeline"); function markdownToHtml(md: string): string { let html = md // Headers .replace(/^#{6}\s+(.+)$/gm, "
$2")
// Inline code
.replace(/`([^`]+)`/g, "$1")
// Unordered lists
.replace(/^[-*]\s+(.+)$/gm, "")
// Single newlines in context
.replace(/\n/g, "
");
// Wrap consecutive
${html}
`; } export async function GET( _request: Request, { params }: { params: Promise<{ path: string[] }> } ) { const session = await auth(); if (!session) return new Response("Unauthorized", { status: 401 }); const { path: segments } = await params; const filePath = path.join(PIPELINE_ROOT, ...segments); const resolved = path.resolve(filePath); if (!resolved.startsWith(path.resolve(PIPELINE_ROOT))) { return new Response("Forbidden", { status: 403 }); } try { const content = await readFile(resolved, "utf-8"); const ext = path.extname(resolved).toLowerCase(); if (ext === ".md") { return new Response(markdownToHtml(content), { headers: { "Content-Type": "text/html; charset=utf-8" }, }); } if (ext === ".json") { const pretty = JSON.stringify(JSON.parse(content), null, 2); return new Response( `${pretty.replace(/`,
{ headers: { "Content-Type": "text/html; charset=utf-8" } }
);
}
// HTML files served as-is
if (ext === ".html") {
return new Response(content, {
headers: { "Content-Type": "text/html; charset=utf-8" },
});
}
// Fallback: plain text
return new Response(content, {
headers: { "Content-Type": "text/plain; charset=utf-8" },
});
} catch {
return new Response("Not found", { status: 404 });
}
}