Skip to content

Commit e84e16b

Browse files
committed
🔧 fix: JIT route cache
1 parent 589481d commit e84e16b

File tree

3 files changed

+25
-21
lines changed

3 files changed

+25
-21
lines changed

example/a.ts

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

3-
const app = new Elysia()
3+
const app = new Elysia({ precompile: true })
44
.get('/', Bun.file('test/kyuukurarin.mp4'))
55
.listen(3000)
6+
7+
console.log(app.routes[0].composed?.toString())

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "elysia",
33
"description": "Ergonomic Framework for Human",
4-
"version": "1.0.0-rc.17",
4+
"version": "1.0.0-rc.19",
55
"author": {
66
"name": "saltyAom",
77
"url": "https://github.com/SaltyAom",

src/compose.ts

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -340,18 +340,18 @@ export const composeHandler = ({
340340
trace: Sucrose.TraceInference
341341
}
342342
}): ComposedHandler => {
343+
const isHandleFn = typeof handler === 'function'
344+
if (!isHandleFn) handler = mapCompactResponse(handler)
345+
343346
const hasErrorHandler =
344-
app.config.forceErrorEncapsulation ||
347+
(app.config.forceErrorEncapsulation && isHandleFn) ||
345348
hooks.error.length > 0 ||
346349
app.event.error.length > 0 ||
347350
typeof Bun === 'undefined' ||
348-
app.onResponse.length > 0 ||
351+
hooks.onResponse.length > 0 ||
349352
hooks.onResponse.length > 0 ||
350353
!!hooks.trace.length
351354

352-
const isHandleFn = typeof handler === 'function'
353-
if (!isHandleFn) handler = mapCompactResponse(handler)
354-
355355
const handle = isHandleFn ? `handler(c)` : `handler`
356356
const handleResponse = hooks.onResponse.length
357357
? `\n;(async () => {${hooks.onResponse
@@ -1440,6 +1440,16 @@ export const composeGeneralHandler = (
14401440

14411441
const maybeAsync = app.event.request.some(isAsync)
14421442

1443+
const init = `\n
1444+
const url = request.url
1445+
const s = url.indexOf('/', 11)
1446+
const qi = url.indexOf('?', s + 1)
1447+
let path
1448+
if(qi === -1)
1449+
path = url.substring(s)
1450+
else
1451+
path = url.substring(s, qi)\n`
1452+
14431453
fnLiteral += `const {
14441454
app,
14451455
mapEarlyResponse,
@@ -1544,18 +1554,11 @@ export const composeGeneralHandler = (
15441554

15451555
endReport()
15461556

1547-
fnLiteral += `
1548-
const url = request.url
1549-
const s = url.indexOf('/', 11)
1550-
const qi = ctx.qi = url.indexOf('?', s + 1)
1551-
const path = ctx.path = url.substring(s, qi === -1 ? undefined : qi)`
1557+
fnLiteral += init
1558+
fnLiteral += `\nctx.qi = qi\n ctx.path = path\n`
15521559
} else {
1553-
fnLiteral += `
1554-
const url = request.url
1555-
const s = url.indexOf('/', 11)
1556-
const qi = url.indexOf('?', s + 1)
1557-
const path = url.substring(s, qi === -1 ? undefined : qi)
1558-
${hasTrace ? 'const id = +requestId.value++' : ''}
1560+
fnLiteral += init
1561+
fnLiteral += `${hasTrace ? 'const id = +requestId.value++' : ''}
15591562
const ctx = {
15601563
request,
15611564
store,
@@ -1737,8 +1740,7 @@ export const composeErrorHandler = (
17371740

17381741
export const jitRoute = (
17391742
index: number
1740-
) => `if(stc${index}) return stc${index}(ctx)\n
1741-
1742-
if(st${index}.compose) return (st${index} = st${index}?.compose())(ctx)
1743+
) => `if(stc${index}) return stc${index}(ctx)
1744+
if(st${index}.compose) return (stc${index} = st${index}.compose())(ctx)
17431745
17441746
return st${index}(ctx)`

0 commit comments

Comments
 (0)