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
28// Static files28// Static files
29// ---------------------------------------------------------------------------29// ---------------------------------------------------------------------------
30 30
31function collectStaticRoutes(dir: string, prefix = ""): Record<string, ReturnType<typeof Bun.file>> {31type StaticHandler = () => Response | Promise<Response>;
32 const routes: Record<string, ReturnType<typeof Bun.file>> = {};32
33function collectStaticRoutes(dir: string, prefix = ""): Record<string, StaticHandler> {
34 const routes: Record<string, StaticHandler> = {};
33 try {35 try {
34 for (const entry of readdirSync(dir, { withFileTypes: true })) {36 for (const entry of readdirSync(dir, { withFileTypes: true })) {
35 const fullPath = join(dir, entry.name);37 const fullPath = join(dir, entry.name);
@@ -37,7 +39,10 @@ function collectStaticRoutes(dir: string, prefix = ""): Record<string, ReturnTyp · 1 unmodified line
37 if (entry.isDirectory()) {39 if (entry.isDirectory()) {
38 Object.assign(routes, collectStaticRoutes(fullPath, routePath));40 Object.assign(routes, collectStaticRoutes(fullPath, routePath));
39 } else {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 } catch {}48 } catch {}
@@ -50,10 +55,13 @@ function collectStaticRoutes(dir: string, prefix = ""): Record<string, ReturnTyp · 6 unmodified lines
50 */55 */
51function embeddedToStaticRoutes(56function embeddedToStaticRoutes(
52 embedded: Record<string, string>,57 embedded: Record<string, string>,
53): Record<string, ReturnType<typeof Bun.file>> {58): Record<string, StaticHandler> {
54 const routes: Record<string, ReturnType<typeof Bun.file>> = {};59 const routes: Record<string, StaticHandler> = {};
55 for (const [key, bunfsPath] of Object.entries(embedded)) {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 return routes;66 return routes;
59}67}
@@ -82,15 +90,17 @@ export async function startServer(opts: StartServerOptions = {}): Promise<Server · 22 unmodified lines
82 const handlers = await getServerHandlers();90 const handlers = await getServerHandlers();
83 91
84 // Build static routes from either embedded assets or disk92 // Build static routes from either embedded assets or disk
85 let staticRoutes: Record<string, ReturnType<typeof Bun.file>>;93 let staticRoutes: Record<string, StaticHandler>;
86 let indexHtml: ReturnType<typeof Bun.file>;94 let serveIndex: StaticHandler;
87 95
88 if (opts.embeddedWebUI) {96 if (opts.embeddedWebUI) {
89 staticRoutes = embeddedToStaticRoutes(opts.embeddedWebUI);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 } else {100 } else {
92 staticRoutes = collectStaticRoutes(clientDir);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 const server = Bun.serve({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 // SPA fallback126 // SPA fallback
117 return new Response(indexHtml, { headers: { "content-type": "text/html" } });127 return serveIndex();
118 },128 },
119 error(error) {129 error(error) {
120 console.error("Server error:", error);130 console.error("Server error:", error);