History

fix: use handler functions for Bun.serve routes compatibility

Rhys Sullivan committed 7 months agov1.4.3-beta.0

Commit 73e2cfe1 parent ce52c6d1 file · +21 −11Browse files

Unified · Split

apps/local/src/serve.ts+21−11
@@ -28,8 +28,10 @@ const isAllowedHost = (request: Request): boolean => { · 27 unmodified lines
2828 // Static files
2929 // ---------------------------------------------------------------------------
3030
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> = {};
3335 try {
3436 for (const entry of readdirSync(dir, { withFileTypes: true })) {
3537 const fullPath = join(dir, entry.name);
@@ -37,7 +39,10 @@ function collectStaticRoutes(dir: string, prefix = ""): Record<string, ReturnTyp · 1 unmodified line
3739 if (entry.isDirectory()) {
3840 Object.assign(routes, collectStaticRoutes(fullPath, routePath));
3941 } 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+ });
4146 }
4247 }
4348 } catch {}
@@ -50,10 +55,13 @@ function collectStaticRoutes(dir: string, prefix = ""): Record<string, ReturnTyp · 6 unmodified lines
5055 */
5156 function embeddedToStaticRoutes(
5257 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> = {};
5560 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+ });
5765 }
5866 return routes;
5967 }
@@ -82,15 +90,17 @@ export async function startServer(opts: StartServerOptions = {}): Promise<Server · 22 unmodified lines
8290 const handlers = await getServerHandlers();
8391
8492 // 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;
8795
8896 if (opts.embeddedWebUI) {
8997 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" } });
91100 } else {
92101 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" } });
94104 }
95105
96106 const server = Bun.serve({
@@ -114,7 +124,7 @@ export async function startServer(opts: StartServerOptions = {}): Promise<Server · 17 unmodified lines
114124 }
115125
116126 // SPA fallback
117− return new Response(indexHtml, { headers: { "content-type": "text/html" } });
127+ return serveIndex();
118128 },
119129 error(error) {
120130 console.error("Server error:", error);