(dir: string, prefix = "")
| 56 | ); |
| 57 | |
| 58 | function collectStaticRoutes(dir: string, prefix = ""): Record<string, StaticHandler> { |
| 59 | const routes: Record<string, StaticHandler> = {}; |
| 60 | // oxlint-disable-next-line executor/no-try-catch-or-throw -- boundary: filesystem route discovery is best-effort for optional built assets |
| 61 | try { |
| 62 | for (const entry of readdirSync(dir, { withFileTypes: true })) { |
| 63 | const fullPath = join(dir, entry.name); |
| 64 | const routePath = `${prefix}/${entry.name}`; |
| 65 | if (entry.isDirectory()) { |
| 66 | Object.assign(routes, collectStaticRoutes(fullPath, routePath)); |
| 67 | } else { |
| 68 | const file = Bun.file(fullPath); |
| 69 | routes[routePath] = () => |
| 70 | routePath === "/index.html" |
| 71 | ? htmlResponse(file) |
| 72 | : new Response(file, { |
| 73 | headers: { |
| 74 | "content-type": file.type || "application/octet-stream", |
| 75 | }, |
| 76 | }); |
| 77 | } |
| 78 | } |
| 79 | } catch {} |
| 80 | return routes; |
| 81 | } |
| 82 | |
| 83 | /** |
| 84 | * Convert an embedded web UI map (path → bunfs path) into Bun.serve routes. |
no test coverage detected