-
-
Notifications
You must be signed in to change notification settings - Fork 427
fix: handle minified arrow functions in hasReturn detection #1623
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
tt-a1i
wants to merge
2
commits into
elysiajs:main
Choose a base branch
from
tt-a1i:fix/hasreturn-minified-arrow
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,143 @@ | ||
| import { describe, expect, it } from 'bun:test' | ||
| import { Elysia } from '../../src' | ||
|
|
||
| describe('beforeHandle with arrow functions', () => { | ||
| it('should execute beforeHandle with arrow function expression', async () => { | ||
| let beforeHandleCalled = false | ||
|
|
||
| const app = new Elysia() | ||
| .get('/test', () => 'ok', { | ||
| // Arrow function expression (no braces) - this was broken with minified code | ||
| beforeHandle: () => { | ||
| beforeHandleCalled = true | ||
| } | ||
| }) | ||
|
|
||
| const response = await app.handle(new Request('http://localhost/test')) | ||
| expect(response.status).toBe(200) | ||
| expect(beforeHandleCalled).toBe(true) | ||
| }) | ||
|
|
||
| it('should execute beforeHandle with arrow function returning value', async () => { | ||
| const app = new Elysia() | ||
| .get('/test', () => 'should not reach', { | ||
| // Arrow function expression that returns early | ||
| beforeHandle: () => 'intercepted' | ||
| }) | ||
|
|
||
| const response = await app.handle(new Request('http://localhost/test')) | ||
| expect(await response.text()).toBe('intercepted') | ||
| }) | ||
|
|
||
| it('should execute async beforeHandle with arrow function expression', async () => { | ||
| let beforeHandleCalled = false | ||
|
|
||
| const app = new Elysia() | ||
| .get('/test', () => 'ok', { | ||
| // Async arrow function expression | ||
| beforeHandle: async () => { | ||
| beforeHandleCalled = true | ||
| } | ||
| }) | ||
|
|
||
| const response = await app.handle(new Request('http://localhost/test')) | ||
| expect(response.status).toBe(200) | ||
| expect(beforeHandleCalled).toBe(true) | ||
| }) | ||
|
|
||
| it('should execute beforeHandle with complex arrow expression', async () => { | ||
| let validatorCalled = false | ||
| const validator = () => { | ||
| validatorCalled = true | ||
| } | ||
|
|
||
| const app = new Elysia() | ||
| .get('/test', () => 'ok', { | ||
| // Complex arrow expression like: async ({status}) => requireSignature()({status}) | ||
| // This pattern was broken when code is minified | ||
| beforeHandle: () => validator() | ||
| }) | ||
|
|
||
| const response = await app.handle(new Request('http://localhost/test')) | ||
| expect(response.status).toBe(200) | ||
| expect(validatorCalled).toBe(true) | ||
| }) | ||
|
|
||
| it('should execute beforeHandle with arrow function block', async () => { | ||
| let beforeHandleCalled = false | ||
|
|
||
| const app = new Elysia() | ||
| .get('/test', () => 'ok', { | ||
| // Arrow function with block (this always worked) | ||
| beforeHandle: () => { | ||
| beforeHandleCalled = true | ||
| return | ||
| } | ||
| }) | ||
|
|
||
| const response = await app.handle(new Request('http://localhost/test')) | ||
| expect(response.status).toBe(200) | ||
| expect(beforeHandleCalled).toBe(true) | ||
| }) | ||
|
|
||
| it('should handle multiple beforeHandle hooks with arrow expressions', async () => { | ||
| const callOrder: number[] = [] | ||
|
|
||
| const app = new Elysia() | ||
| .get('/test', () => 'ok', { | ||
| beforeHandle: [ | ||
| () => { | ||
| callOrder.push(1) | ||
| }, | ||
| () => { | ||
| callOrder.push(2) | ||
| }, | ||
| () => { | ||
| callOrder.push(3) | ||
| } | ||
| ] | ||
| }) | ||
|
|
||
| const response = await app.handle(new Request('http://localhost/test')) | ||
| expect(response.status).toBe(200) | ||
| expect(callOrder).toEqual([1, 2, 3]) | ||
| }) | ||
|
|
||
| // Test with truly minified code (no spaces around arrow) | ||
| // This simulates what happens when code is bundled and minified | ||
| it('should execute beforeHandle with truly minified arrow function (no spaces)', async () => { | ||
| // Construct a function with no spaces: async()=>'intercepted' | ||
| // This is what real minifiers produce | ||
| const minifiedHandler = Function("return async()=>'intercepted'")() | ||
|
|
||
| const app = new Elysia().get('/test', () => 'should not reach', { | ||
| beforeHandle: minifiedHandler | ||
| }) | ||
|
|
||
| const response = await app.handle(new Request('http://localhost/test')) | ||
| expect(await response.text()).toBe('intercepted') | ||
| }) | ||
|
|
||
| // Test with actual HTTP server (not just app.handle) | ||
| it('should work with live server', async () => { | ||
| let beforeHandleCalled = false | ||
|
|
||
| const app = new Elysia() | ||
| .get('/test', () => 'ok', { | ||
| beforeHandle: () => { | ||
| beforeHandleCalled = true | ||
| } | ||
| }) | ||
| .listen(0) | ||
|
|
||
| try { | ||
| const response = await fetch( | ||
| `http://localhost:${app.server?.port}/test` | ||
| ) | ||
| expect(response.status).toBe(200) | ||
| expect(beforeHandleCalled).toBe(true) | ||
| } finally { | ||
| await app.stop() | ||
| } | ||
| }) | ||
| }) | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.