From fb9e1af73ddb3675606b2bbd19bb7db7e0a46344 Mon Sep 17 00:00:00 2001 From: Claude Bot Date: Sun, 20 Jul 2025 03:06:44 +0000 Subject: [PATCH] Fix DirectoryRoute path matching with wildcard pattern MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Register DirectoryRoute with /* pattern to match all sub-paths - This should fix 404 errors when accessing files in directory routes - For root path /, register as /* to catch all paths - For other paths, register as path/* to match sub-paths 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude --- src/bun.js/api/server.zig | 8 ++- .../js/bun/http/directory-route-debug.test.ts | 50 +++++++++++++++++++ 2 files changed, 57 insertions(+), 1 deletion(-) create mode 100644 test/js/bun/http/directory-route-debug.test.ts diff --git a/src/bun.js/api/server.zig b/src/bun.js/api/server.zig index 4f44e4f582..b68a024f58 100644 --- a/src/bun.js/api/server.zig +++ b/src/bun.js/api/server.zig @@ -2646,7 +2646,13 @@ pub fn NewServer(protocol_enum: enum { http, https }, development_kind: enum { d ServerConfig.applyStaticRoute(any_server, ssl_enabled, app, *FileRoute, file_route, entry.path, entry.method); }, .directory => |directory_route| { - ServerConfig.applyStaticRoute(any_server, ssl_enabled, app, *DirectoryRoute, directory_route, entry.path, entry.method); + // For directory routes, we need to handle all sub-paths, so append /* to the path + const dir_path = if (std.mem.eql(u8, entry.path, "/")) + std.fmt.allocPrint(bun.default_allocator, "/*", .{}) catch bun.outOfMemory() + else + std.fmt.allocPrint(bun.default_allocator, "{s}/*", .{entry.path}) catch bun.outOfMemory(); + defer bun.default_allocator.free(dir_path); + ServerConfig.applyStaticRoute(any_server, ssl_enabled, app, *DirectoryRoute, directory_route, dir_path, entry.method); }, .html => |html_bundle_route| { ServerConfig.applyStaticRoute(any_server, ssl_enabled, app, *HTMLBundle.Route, html_bundle_route.data, entry.path, entry.method); diff --git a/test/js/bun/http/directory-route-debug.test.ts b/test/js/bun/http/directory-route-debug.test.ts new file mode 100644 index 0000000000..5f788d6897 --- /dev/null +++ b/test/js/bun/http/directory-route-debug.test.ts @@ -0,0 +1,50 @@ +import { describe, it, expect, beforeAll, afterAll } from "bun:test"; +import { bunEnv, bunExe, tempDirWithFiles, isWindows } from "harness"; +import { join } from "path"; +import { readdirSync } from "fs"; + +describe("DirectoryRoute Debug", () => { + let testDir: string; + + beforeAll(() => { + testDir = tempDirWithFiles("directory-route-debug", { + "test.html": "Test Content", + }); + console.log("Debug Test directory:", testDir); + console.log("Files in directory:", readdirSync(testDir)); + }); + + it("should debug single file serving", async () => { + const server = Bun.serve({ + port: 0, + routes: { + "/": { dir: testDir }, + }, + }); + + console.log("Server started on port:", server.port); + console.log("Testing file at path: /test.html"); + + try { + const response = await fetch(`http://localhost:${server.port}/test.html`); + console.log("Response status:", response.status); + console.log("Response headers:", Object.fromEntries(response.headers.entries())); + + if (response.status !== 200) { + const text = await response.text(); + console.log("Response body:", text); + + // Also test if file exists directly + const testFilePath = join(testDir, "test.html"); + console.log("Direct file check:", Bun.file(testFilePath).size); + } else { + const text = await response.text(); + console.log("Success! Response body:", text); + } + + expect(response.status).toBe(200); + } finally { + server.stop(); + } + }); +}); \ No newline at end of file