Skip to content
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

fix(combine/every): make every middleware work with short-circuiting middlewares #3441

Merged
merged 1 commit into from
Sep 24, 2024

Conversation

paolostyle
Copy link
Contributor

@paolostyle paolostyle commented Sep 22, 2024

When using every middleware to compose middlewares that short-circuits the chain (i.e. return a Response object, like e.g. zod-validator, but I had also issues with e.g. cors with OPTIONS requests) and, as a result, don't call next(), it was throwing an error Error: Context is not finalized. Did you forget to return a Response object or await next()?. This fixes the issue and makes it possible to use it with (probably?) any middleware.

As a side note, I attempted to simplify the compose's middleware signature (and I did) as it could easily accept an array of functions/handlers and simplify the code. But I think the signature is so complicated because of the usage in dispatch method of the Hono class and that would require mapping to an array of handlers which would probably slightly worsen the overall performance (not sure how to measure that but presumably 0 Array.prototype.maps is better than 1) and that method seems to be rather critical. If you think it's worth considering I'll submit another PR, but it's probably not worth it.

The author should do the following, if applicable

  • Add tests
  • Run tests
  • bun run format:fix && bun run lint:fix to format the code
  • Add TSDoc/JSDoc to document the code

Copy link

codecov bot commented Sep 23, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 95.77%. Comparing base (66df4e1) to head (bfe2a56).
Report is 6 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #3441   +/-   ##
=======================================
  Coverage   95.77%   95.77%           
=======================================
  Files         155      155           
  Lines        9310     9311    +1     
  Branches     2759     2802   +43     
=======================================
+ Hits         8917     8918    +1     
  Misses        393      393           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@usualoma
Copy link
Member

Hi @paolostyle
Thanks for the fix.
I think it looks good!

Copy link
Member

@yusukebe yusukebe left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

@yusukebe
Copy link
Member

@paolostyle

Thank you! I'll merge this now.

@yusukebe yusukebe merged commit fe0a82a into honojs:main Sep 24, 2024
16 checks passed
@paolostyle paolostyle deleted the combine-every-fix branch September 24, 2024 09:01
TinsFox pushed a commit to TinsFox/hono that referenced this pull request Oct 28, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants