Skip to content

Commit

Permalink
🔧 fix: JIT route cache
Browse files Browse the repository at this point in the history
  • Loading branch information
SaltyAom committed Mar 15, 2024
1 parent 589481d commit e84e16b
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 21 deletions.
4 changes: 3 additions & 1 deletion example/a.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { Elysia } from '../src'

const app = new Elysia()
const app = new Elysia({ precompile: true })
.get('/', Bun.file('test/kyuukurarin.mp4'))
.listen(3000)

console.log(app.routes[0].composed?.toString())
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "elysia",
"description": "Ergonomic Framework for Human",
"version": "1.0.0-rc.17",
"version": "1.0.0-rc.19",
"author": {
"name": "saltyAom",
"url": "https://github.com/SaltyAom",
Expand Down
40 changes: 21 additions & 19 deletions src/compose.ts
Original file line number Diff line number Diff line change
Expand Up @@ -340,18 +340,18 @@ export const composeHandler = ({
trace: Sucrose.TraceInference
}
}): ComposedHandler => {
const isHandleFn = typeof handler === 'function'
if (!isHandleFn) handler = mapCompactResponse(handler)

const hasErrorHandler =
app.config.forceErrorEncapsulation ||
(app.config.forceErrorEncapsulation && isHandleFn) ||
hooks.error.length > 0 ||
app.event.error.length > 0 ||
typeof Bun === 'undefined' ||
app.onResponse.length > 0 ||
hooks.onResponse.length > 0 ||
hooks.onResponse.length > 0 ||
!!hooks.trace.length

const isHandleFn = typeof handler === 'function'
if (!isHandleFn) handler = mapCompactResponse(handler)

const handle = isHandleFn ? `handler(c)` : `handler`
const handleResponse = hooks.onResponse.length
? `\n;(async () => {${hooks.onResponse
Expand Down Expand Up @@ -1440,6 +1440,16 @@ export const composeGeneralHandler = (

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

const init = `\n
const url = request.url
const s = url.indexOf('/', 11)
const qi = url.indexOf('?', s + 1)
let path
if(qi === -1)
path = url.substring(s)
else
path = url.substring(s, qi)\n`

fnLiteral += `const {
app,
mapEarlyResponse,
Expand Down Expand Up @@ -1544,18 +1554,11 @@ export const composeGeneralHandler = (

endReport()

fnLiteral += `
const url = request.url
const s = url.indexOf('/', 11)
const qi = ctx.qi = url.indexOf('?', s + 1)
const path = ctx.path = url.substring(s, qi === -1 ? undefined : qi)`
fnLiteral += init
fnLiteral += `\nctx.qi = qi\n ctx.path = path\n`
} else {
fnLiteral += `
const url = request.url
const s = url.indexOf('/', 11)
const qi = url.indexOf('?', s + 1)
const path = url.substring(s, qi === -1 ? undefined : qi)
${hasTrace ? 'const id = +requestId.value++' : ''}
fnLiteral += init
fnLiteral += `${hasTrace ? 'const id = +requestId.value++' : ''}
const ctx = {
request,
store,
Expand Down Expand Up @@ -1737,8 +1740,7 @@ export const composeErrorHandler = (

export const jitRoute = (
index: number
) => `if(stc${index}) return stc${index}(ctx)\n
if(st${index}.compose) return (st${index} = st${index}?.compose())(ctx)
) => `if(stc${index}) return stc${index}(ctx)
if(st${index}.compose) return (stc${index} = st${index}.compose())(ctx)
return st${index}(ctx)`

0 comments on commit e84e16b

Please sign in to comment.