Skip to content

Commit 703de2d

Browse files
committed
fix: use per-promise start index when registering guard dynamic import routes
1 parent d789956 commit 703de2d

File tree

2 files changed

+23
-2
lines changed

2 files changed

+23
-2
lines changed

src/index.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4687,7 +4687,7 @@ export default class Elysia<
46874687

46884688
// Handle dynamic imports (Promises) used inside guard callback
46894689
if (instance.promisedModules.size > 0) {
4690-
const syncCount = instance.router.history.length
4690+
let processedUntil = instance.router.history.length
46914691

46924692
for (const promise of instance.promisedModules.promises) {
46934693
this.promisedModules.add(
@@ -4705,8 +4705,11 @@ export default class Elysia<
47054705
const hasStandaloneSchema =
47064706
body || headers || query || params || cookie || response
47074707

4708+
const startIndex = processedUntil
4709+
processedUntil = instance.router.history.length
4710+
47084711
for (
4709-
let i = syncCount;
4712+
let i = startIndex;
47104713
i < instance.router.history.length;
47114714
i++
47124715
) {

test/core/modules.test.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,24 @@ describe('Modules', () => {
263263
expect(await res.text()).toBe('lazy-instance')
264264
})
265265

266+
it('register multiple dynamic import routes inside guard', async () => {
267+
const lazyA = Promise.resolve(new Elysia().get('/a', () => 'a'))
268+
const lazyB = Promise.resolve(new Elysia().get('/b', () => 'b'))
269+
270+
let hookCalls = 0
271+
272+
const app = new Elysia().guard(
273+
{ beforeHandle: () => { hookCalls++ } },
274+
(app) => app.use(lazyA).use(lazyB)
275+
)
276+
277+
await app.modules
278+
279+
expect((await app.handle(req('/a'))).status).toBe(200)
280+
expect((await app.handle(req('/b'))).status).toBe(200)
281+
expect(hookCalls).toBe(2)
282+
})
283+
266284
it('register dynamic import routes inside guard with hook', async () => {
267285
let called = false
268286

0 commit comments

Comments
 (0)