Unified · Split
| @@ -28,8 +28,10 @@ const isAllowedHost = (request: Request): boolean => { · 27 unmodified lines | ||
| 28 | 28 | // Static files |
| 29 | 29 | // --------------------------------------------------------------------------- |
| 30 | 30 | |
| 31 | −function collectStaticRoutes(dir: string, prefix = ""): Record<string, ReturnType<typeof Bun.file>> { | |
| 32 | − const routes: Record<string, ReturnType<typeof Bun.file>> = {}; | |
| 31 | +type StaticHandler = () => Response | Promise<Response>; | |
| 32 | + | |
| 33 | +function collectStaticRoutes(dir: string, prefix = ""): Record<string, StaticHandler> { | |
| 34 | + const routes: Record<string, StaticHandler> = {}; | |
| 33 | 35 | try { |
| 34 | 36 | for (const entry of readdirSync(dir, { withFileTypes: true })) { |
| 35 | 37 | const fullPath = join(dir, entry.name); |
| @@ -37,7 +39,10 @@ function collectStaticRoutes(dir: string, prefix = ""): Record<string, ReturnTyp · 1 unmodified line | ||
| 37 | 39 | if (entry.isDirectory()) { |
| 38 | 40 | Object.assign(routes, collectStaticRoutes(fullPath, routePath)); |
| 39 | 41 | } else { |
| 40 | − routes[routePath] = Bun.file(fullPath); | |
| 42 | + const file = Bun.file(fullPath); | |
| 43 | + routes[routePath] = () => new Response(file, { | |
| 44 | + headers: { "content-type": file.type || "application/octet-stream" }, | |
| 45 | + }); | |
| 41 | 46 | } |
| 42 | 47 | } |
| 43 | 48 | } catch {} |
| @@ -50,10 +55,13 @@ function collectStaticRoutes(dir: string, prefix = ""): Record<string, ReturnTyp · 6 unmodified lines | ||
| 50 | 55 | */ |
| 51 | 56 | function embeddedToStaticRoutes( |
| 52 | 57 | embedded: Record<string, string>, |
| 53 | −): Record<string, ReturnType<typeof Bun.file>> { | |
| 54 | − const routes: Record<string, ReturnType<typeof Bun.file>> = {}; | |
| 58 | +): Record<string, StaticHandler> { | |
| 59 | + const routes: Record<string, StaticHandler> = {}; | |
| 55 | 60 | for (const [key, bunfsPath] of Object.entries(embedded)) { |
| 56 | − routes[`/${key}`] = Bun.file(bunfsPath); | |
| 61 | + const file = Bun.file(bunfsPath); | |
| 62 | + routes[`/${key}`] = () => new Response(file, { | |
| 63 | + headers: { "content-type": file.type || "application/octet-stream" }, | |
| 64 | + }); | |
| 57 | 65 | } |
| 58 | 66 | return routes; |
| 59 | 67 | } |
| @@ -82,15 +90,17 @@ export async function startServer(opts: StartServerOptions = {}): Promise<Server · 22 unmodified lines | ||
| 82 | 90 | const handlers = await getServerHandlers(); |
| 83 | 91 | |
| 84 | 92 | // Build static routes from either embedded assets or disk |
| 85 | − let staticRoutes: Record<string, ReturnType<typeof Bun.file>>; | |
| 86 | − let indexHtml: ReturnType<typeof Bun.file>; | |
| 93 | + let staticRoutes: Record<string, StaticHandler>; | |
| 94 | + let serveIndex: StaticHandler; | |
| 87 | 95 | |
| 88 | 96 | if (opts.embeddedWebUI) { |
| 89 | 97 | staticRoutes = embeddedToStaticRoutes(opts.embeddedWebUI); |
| 90 | − indexHtml = staticRoutes["/index.html"] ?? Bun.file(join(clientDir, "index.html")); | |
| 98 | + const indexFile = Bun.file(opts.embeddedWebUI["index.html"] ?? join(clientDir, "index.html")); | |
| 99 | + serveIndex = () => new Response(indexFile, { headers: { "content-type": "text/html" } }); | |
| 91 | 100 | } else { |
| 92 | 101 | staticRoutes = collectStaticRoutes(clientDir); |
| 93 | − indexHtml = Bun.file(join(clientDir, "index.html")); | |
| 102 | + const indexFile = Bun.file(join(clientDir, "index.html")); | |
| 103 | + serveIndex = () => new Response(indexFile, { headers: { "content-type": "text/html" } }); | |
| 94 | 104 | } |
| 95 | 105 | |
| 96 | 106 | const server = Bun.serve({ |
| @@ -114,7 +124,7 @@ export async function startServer(opts: StartServerOptions = {}): Promise<Server · 17 unmodified lines | ||
| 114 | 124 | } |
| 115 | 125 | |
| 116 | 126 | // SPA fallback |
| 117 | − return new Response(indexHtml, { headers: { "content-type": "text/html" } }); | |
| 127 | + return serveIndex(); | |
| 118 | 128 | }, |
| 119 | 129 | error(error) { |
| 120 | 130 | console.error("Server error:", error); |