Skip to content

Commit e596dab

Browse files
committed
🔧 fix: #1747 parsing request from mount hang
1 parent 6561d61 commit e596dab

File tree

3 files changed

+37
-78
lines changed

3 files changed

+37
-78
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ Bug fix:
33
- [#1755](https://github.com/elysiajs/elysia/issues/1755) deduplicate local handler from global event
44
- [#1752](https://github.com/elysiajs/elysia/issues/1752) system router with trailing path doesn't match with non-trailing
55
- url format redos
6+
- [#1747](https://github.com/elysiajs/elysia/issues/1747) parsing request from mount hang
67

78
# 1.4.25 - 12 Feb 2025
89
Feature:

example/a.ts

Lines changed: 34 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,37 @@
11
import { Elysia } from '../src'
22

3-
// ❌ BROKEN: Using separate instances with .use()
4-
const typesRoutes = new Elysia({ prefix: '/types' })
5-
.get('/', () => 'List all types')
6-
.get('/:id', ({ params }) => `Get type: ${params.id}`)
7-
8-
const itemsRoutes = new Elysia()
9-
.get('/', () => 'List all items')
10-
.get('/:id', ({ params }) => `Get item: ${params.id}`)
11-
12-
const brokenModule = new Elysia({ prefix: '/items' })
13-
.use(typesRoutes) // Static prefix - should match first
14-
.use(itemsRoutes) // Has /:id catch-all
15-
16-
const typesRoutesNoPrefix = new Elysia()
17-
.get('/', () => 'List all types')
18-
.get('/:id', ({ params }) => `Get type: ${params.id}`)
19-
20-
const brokenModuleWithGroup = new Elysia({ prefix: '/items' })
21-
.group('/types', (app) => app.use(typesRoutesNoPrefix))
22-
.use(itemsRoutes)
23-
24-
// ✅ WORKS: All routes in single instance
25-
const workingModule = new Elysia({ prefix: '/items' })
26-
.get('/:id', ({ params }) => `Get item: ${params.id}`)
27-
.get('/', () => 'List all items')
28-
.get('/types', () => 'List all types')
29-
.get('/types/:id', ({ params }) => `Get type: ${params.id}`)
30-
31-
const work = new Elysia({ systemRouter: true })
32-
.use(workingModule) // GET /items/types returns "List all types" ✅
33-
// .listen(3000)
34-
35-
const notWork = new Elysia({ systemRouter: true })
36-
.use(brokenModule) // GET /items/types returns "Get item: types" ❌
37-
.use(brokenModuleWithGroup) // GET /items/types returns "Get item: types" ❌
38-
.listen(3001)
39-
40-
Bun.serve({
41-
port: 3002,
42-
routes: {
43-
'/items/types/': {
44-
GET: () => new Response('/items/types/')
45-
},
46-
'/items/types/:id': {
47-
GET: () => new Response('/items/types/:id')
3+
const useMount = true // true => hangs, false => works
4+
5+
const handler = async (request: Request) => {
6+
console.log("A")
7+
// Rebuild request with new headers (problematic in mount mode)
8+
const headers = new Headers(request.headers)
9+
headers.set('x-request-id', 'req_test')
10+
const patched = new Request(request, { headers })
11+
12+
// This is where it hangs when mounted
13+
const body = await patched.text()
14+
15+
return Response.json({
16+
ok: true,
17+
body,
18+
requestId: patched.headers.get('x-request-id')
19+
})
20+
}
21+
22+
const app = useMount
23+
? new Elysia().mount('/v1/', handler)
24+
: new Elysia().all('/v1/*', ({ request }) => handler(request))
25+
26+
app.listen(3000)
27+
28+
app.handle(
29+
new Request('http://localhost:3000/v1/test', {
30+
headers: {
31+
'content-type': 'hello'
4832
},
49-
'/items/:id': {
50-
GET: () => new Response('/items/:id')
51-
}
52-
}
53-
})
33+
body: 'hello'
34+
})
35+
)
36+
.then((x) => x.status)
37+
.then(console.log)

src/index.ts

Lines changed: 2 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -5582,21 +5582,7 @@ export default class Elysia<
55825582
})()
55835583

55845584
const handler: Handler = ({ request, path }) =>
5585-
run(
5586-
new Request(replaceUrlPath(request.url, path), {
5587-
method: request.method,
5588-
headers: request.headers,
5589-
signal: request.signal,
5590-
credentials: request.credentials,
5591-
referrerPolicy: request.referrerPolicy as any,
5592-
duplex: request.duplex,
5593-
redirect: request.redirect,
5594-
mode: request.mode,
5595-
keepalive: request.keepalive,
5596-
integrity: request.integrity,
5597-
body: request.body
5598-
})
5599-
)
5585+
run(new Request(replaceUrlPath(request.url, path), request))
56005586

56015587
this.route('ALL', '/*', handler as any, {
56025588
parse: 'none',
@@ -5632,19 +5618,7 @@ export default class Elysia<
56325618
handle(
56335619
new Request(
56345620
replaceUrlPath(request.url, path.slice(length) || '/'),
5635-
{
5636-
method: request.method,
5637-
headers: request.headers,
5638-
signal: request.signal,
5639-
credentials: request.credentials,
5640-
referrerPolicy: request.referrerPolicy as any,
5641-
duplex: request.duplex,
5642-
redirect: request.redirect,
5643-
mode: request.mode,
5644-
keepalive: request.keepalive,
5645-
integrity: request.integrity,
5646-
body: request.body
5647-
}
5621+
request
56485622
)
56495623
)
56505624

0 commit comments

Comments
 (0)