Skip to content

Commit 620d96b

Browse files
committed
🔧 fix: #1596 handle context pass to function with sub context
1 parent 2d30e61 commit 620d96b

File tree

4 files changed

+69
-15
lines changed

4 files changed

+69
-15
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ Improvement
77
Bug fix:
88
- [#1591](https://github.com/elysiajs/elysia/pull/1591), [#1590](https://github.com/elysiajs/elysia/pull/1591) merge set.headers without duplicating Response
99
- add set immediate fallback
10+
- [#1596](https://github.com/elysiajs/elysia/issues/1596) handle context pass to function with sub context
1011
- [#1597](https://github.com/elysiajs/elysia/issues/1597) safeParse, parse to static type
1112

1213
# 1.4.18 - 4 Dec 2025

example/a.ts

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1-
import { Elysia, t } from '../src'
1+
import { t, getSchemaValidator } from '../src'
2+
import { sucrose } from '../src/sucrose'
23

3-
new Elysia({ aot: false })
4-
.guard(
5-
{
6-
afterResponse: () => {
7-
console.log('afterResponse')
8-
}
9-
},
10-
(app) => app.get('/test', () => 'afterResponse')
11-
)
12-
.get('/', () => 'hi')
13-
.listen(3000)
4+
const v = sucrose({
5+
handler: (context) => {
6+
console.log('path >>> ', context.path)
7+
console.log(context)
8+
9+
return { id: 'test' }
10+
}
11+
})
12+
13+
console.log(v)

src/sucrose.ts

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -551,8 +551,36 @@ export const isContextPassToFunction = (
551551
) => {
552552
// ! Function is passed to another function, assume as all is accessed
553553
try {
554-
const captureFunction = new RegExp(`\\w\\((.*?)?${context}`, 'gs')
555-
captureFunction.test(body)
554+
const captureFunction = new RegExp(
555+
`\\w\\((?:.*?)?${context}(?:.*?)?\\)`,
556+
'gs'
557+
)
558+
const exactParameter = new RegExp(`${context}(,|\\))`, 'gs')
559+
560+
const length = body.length
561+
let fn
562+
563+
fn = captureFunction.exec(body) + ''
564+
while (
565+
captureFunction.lastIndex !== 0 &&
566+
captureFunction.lastIndex < length + (fn ? fn.length : 0)
567+
) {
568+
if (fn && exactParameter.test(fn)) {
569+
inference.query = true
570+
inference.headers = true
571+
inference.body = true
572+
inference.cookie = true
573+
inference.set = true
574+
inference.server = true
575+
inference.url = true
576+
inference.route = true
577+
inference.path = true
578+
579+
return true
580+
}
581+
582+
fn = captureFunction.exec(body) + ''
583+
}
556584

557585
/*
558586
Since JavaScript engine already format the code (removing whitespace, newline, etc.),
@@ -700,7 +728,11 @@ export const sucrose = (
700728
code.charCodeAt(0) === 123 &&
701729
code.charCodeAt(body.length - 1) === 125
702730
)
703-
code = code.slice(1, -1)
731+
code = code.slice(1, -1).trim()
732+
733+
console.log(
734+
isContextPassToFunction(mainParameter, code, fnInference)
735+
)
704736

705737
if (!isContextPassToFunction(mainParameter, code, fnInference))
706738
inferBodyReference(code, aliases, fnInference)

test/sucrose/sucrose.test.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -307,4 +307,25 @@ describe('sucrose', () => {
307307
route: true
308308
})
309309
})
310+
311+
it('handle context pass to function with sub context', () => {
312+
expect(
313+
sucrose({
314+
handler: (context) => {
315+
console.log('path >>> ', context.path)
316+
console.log(context)
317+
}
318+
})
319+
).toEqual({
320+
query: true,
321+
headers: true,
322+
body: true,
323+
cookie: true,
324+
set: true,
325+
server: true,
326+
path: true,
327+
url: true,
328+
route: true
329+
})
330+
})
310331
})

0 commit comments

Comments
 (0)