Skip to content

Commit a555850

Browse files
committed
🔧 fix: #1671 mount produce incorrect url
1 parent c047fb4 commit a555850

File tree

6 files changed

+63
-6
lines changed

6 files changed

+63
-6
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# 1.4.22
22
Bug fix:
3+
- [#1671](https://github.com/elysiajs/elysia/issues/1671) mount() produces incorrect URL path when Elysia instance has prefix option
34
- ValueError with summary missing types
45
- Elysia not using Bun.routes
56

example/a.ts

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,18 @@
11
import { Elysia } from '../src'
22

3-
const app = new Elysia().get('/', () => 'a').listen(3000)
3+
const sdkApp = new Elysia({ prefix: '/sdk' }).mount(
4+
'/problems-domain',
5+
(request) => {
6+
console.log(request.url)
7+
8+
return Response.json({ path: new URL(request.url).pathname })
9+
}
10+
)
11+
12+
const app = new Elysia().use(sdkApp)
13+
14+
const response = await app
15+
.handle(new Request('http://localhost/sdk/problems-domain/problems'))
16+
.then((x) => x.text())
17+
18+
console.log(response)

src/adapter/bun/compose.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ const createContext = (
3333
const getQi =
3434
`const u=request.url,` +
3535
`s=u.indexOf('/',${standardHostname ? 11 : 7}),` +
36-
`qi=u.indexOf('?', s + 1)\n`
36+
`qi=u.indexOf('?',s+1)\n`
3737

3838
const needsQuery =
3939
inference.query ||

src/index.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5613,8 +5613,12 @@ export default class Elysia<
56135613
throw new Error('Invalid handler')
56145614
})()
56155615

5616-
const length = path.length - (path.endsWith('*') ? 1 : 0)
5616+
const fullPath =
5617+
typeof path === 'string' && this.config.prefix
5618+
? this.config.prefix + path
5619+
: path
56175620

5621+
const length = fullPath.length - (path.endsWith('*') ? 1 : 0)
56185622
const handler: Handler = ({ request, path }) =>
56195623
handle(
56205624
new Request(

src/utils.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,13 @@ import { ElysiaFile } from './universal/file'
2828
export const hasHeaderShorthand = 'toJSON' in new Headers()
2929

3030
export const replaceUrlPath = (url: string, pathname: string) => {
31-
const urlObject = new URL(url)
32-
urlObject.pathname = pathname
33-
return urlObject.toString()
31+
const pathStartIndex = url.indexOf('/', 11)
32+
const queryIndex = url.indexOf('?', pathStartIndex)
33+
34+
if (queryIndex === -1)
35+
return `${url.slice(0, pathStartIndex)}${pathname.charCodeAt(0) === 47 ? '' : '/'}${pathname}`
36+
37+
return `${url.slice(0, pathStartIndex)}${pathname.charCodeAt(0) === 47 ? '' : '/'}${pathname}${url.slice(queryIndex)}`
3438
}
3539

3640
export const isClass = (v: Object) =>

test/core/mount.test.ts

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,4 +137,37 @@ describe('Mount', () => {
137137
'x-test': 'test'
138138
})
139139
})
140+
141+
it('mount without prefix - strips mount path', async () => {
142+
const app = new Elysia().mount('/sdk/problems-domain', (request) => {
143+
return Response.json({ path: new URL(request.url).pathname })
144+
})
145+
146+
const response = await app
147+
.handle(
148+
new Request('http://localhost/sdk/problems-domain/problems')
149+
)
150+
.then((x) => x.json() as Promise<{ path: string }>)
151+
152+
expect(response.path).toBe('/problems')
153+
})
154+
155+
it('mount with prefix - should strip both prefix and mount path', async () => {
156+
const sdkApp = new Elysia({ prefix: '/sdk' }).mount(
157+
'/problems-domain',
158+
(request) => {
159+
return Response.json({ path: new URL(request.url).pathname })
160+
}
161+
)
162+
163+
const app = new Elysia().use(sdkApp)
164+
165+
const response = await app
166+
.handle(
167+
new Request('http://localhost/sdk/problems-domain/problems')
168+
)
169+
.then((x) => x.json() as Promise<{ path: string }>)
170+
171+
expect(response.path).toBe('/problems')
172+
})
140173
})

0 commit comments

Comments
 (0)