Skip to content

HttpRouter.mountApp prefix matching fixed #5111

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jul 1, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/clear-items-send.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@effect/platform": patch
---

`HttpRouter.mountApp` prefix matching fixed
8 changes: 8 additions & 0 deletions packages/platform-node/test/HttpServer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,14 @@ describe("HttpServer", () => {
expect(todo).toEqual("/1")
const root = yield* client.get("/child").pipe(Effect.flatMap((_) => _.text))
expect(root).toEqual("/")
const rootSearch = yield* client.get("/child?foo=bar").pipe(Effect.flatMap((_) => _.text))
expect(rootSearch).toEqual("?foo=bar")
const rootSlash = yield* client.get("/child/").pipe(Effect.flatMap((_) => _.text))
expect(rootSlash).toEqual("/")
const invalid = yield* client.get("/child1/", {
urlParams: { foo: "bar" }
}).pipe(Effect.map((_) => _.status))
expect(invalid).toEqual(404)
}).pipe(Effect.provide(NodeHttpServer.layerTest)))

it.scoped("mountApp/includePrefix", () =>
Expand Down
5 changes: 4 additions & 1 deletion packages/platform/src/internal/httpRouter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -243,9 +243,12 @@ const toHttpApp = <E, R>(
const context = Context.unsafeMake(new Map(fiber.getFiberRef(FiberRef.currentContext).unsafeMap))
const request = Context.unsafeGet(context, ServerRequest.HttpServerRequest)
if (mountsLen > 0) {
const searchIndex = request.url.indexOf("?")
const pathname = searchIndex === -1 ? request.url : request.url.slice(0, searchIndex)

for (let i = 0; i < mountsLen; i++) {
const [path, routeContext, options] = mounts[i]
if (request.url.startsWith(path)) {
if (pathname === path || pathname.startsWith(path + "/")) {
context.unsafeMap.set(RouteContext.key, routeContext)
if (options?.includePrefix !== true) {
context.unsafeMap.set(ServerRequest.HttpServerRequest.key, sliceRequestUrl(request, path))
Expand Down