-
-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathserve.ts
More file actions
43 lines (35 loc) · 1.17 KB
/
Copy pathserve.ts
File metadata and controls
43 lines (35 loc) · 1.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import { join, resolve } from "node:path";
const DIST = resolve(import.meta.dirname, "dist");
const SOCK = "/var/run/cabotage/cabotage.sock";
async function serveFile(filePath: string): Promise<Response | null> {
const resolved = resolve(filePath);
if (!resolved.startsWith(DIST)) return null;
const file = Bun.file(resolved);
if (await file.exists()) return new Response(file);
return null;
}
Bun.serve({
unix: SOCK,
async fetch(req) {
const url = new URL(req.url);
const pathname = decodeURIComponent(url.pathname);
// Try exact file
let resp = await serveFile(join(DIST, pathname));
if (resp) return resp;
// Try as directory with index.html
resp = await serveFile(join(DIST, pathname, "index.html"));
if (resp) return resp;
// Try with .html extension
resp = await serveFile(join(DIST, pathname + ".html"));
if (resp) return resp;
// 404
const notFound = await serveFile(join(DIST, "404.html"));
if (notFound)
return new Response(notFound.body, {
status: 404,
headers: notFound.headers,
});
return new Response("Not Found", { status: 404 });
},
});
console.log(`Listening on ${SOCK}`);