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

perf(hono-base): don't import HTTPException #2885

Closed
wants to merge 1 commit into from

Conversation

yusukebe
Copy link
Member

@yusukebe yusukebe commented Jun 2, 2024

This is like a refactor but is made a perf since reducing the bundle size for the performance.

With this PR, the hono-base.ts does not need to import the HTTPException class directly. So, the bundle size for the app that does not use HTTPException will be small. To enable it, it does not use instanceOf; it will detect by checking the property named a symbol of IS_HTTP_EXCEPTION.

Result:

CleanShot 2024-06-02 at 18 40 35@2x

It was reduced by 260 bytes. It seems small, but reducing size is important for edge environments such as Cloudflare Workers.

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 Jun 2, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 87.88%. Comparing base (320b945) to head (afb729e).

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #2885   +/-   ##
=======================================
  Coverage   87.88%   87.88%           
=======================================
  Files         139      139           
  Lines       14208    14215    +7     
  Branches     2314     2338   +24     
=======================================
+ Hits        12486    12493    +7     
  Misses       1722     1722           

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

@yusukebe
Copy link
Member Author

yusukebe commented Jun 2, 2024

Hey @usualoma. I want to know your thoughts on this.

@usualoma
Copy link
Member

usualoma commented Jun 2, 2024

@yusukebe
Ah I see, this is very nice!

Alternatively, how about the following. It simplifies the definition and opens up the possibility for users themselves to use "errors implementing getResponse()", other than hono’s HTTPException. The behaviour would change if an existing application used "errors implementing getResponse()", but, well, there would be no such application.

diff --git a/src/hono-base.ts b/src/hono-base.ts
index f3e9ad03..858d65db 100644
--- a/src/hono-base.ts
+++ b/src/hono-base.ts
@@ -7,7 +7,6 @@
 import { compose } from './compose'
 import { Context } from './context'
 import type { ExecutionContext } from './context'
-import { HTTPException } from './http-exception'
 import { HonoRequest } from './request'
 import type { Router } from './router'
 import { METHODS, METHOD_NAME_ALL, METHOD_NAME_ALL_LOWERCASE } from './router'
@@ -21,6 +20,7 @@ import type {
   MergeSchemaPath,
   MiddlewareHandler,
   MiddlewareHandlerInterface,
+  HTTPResponseError,
   Next,
   NotFoundHandler,
   OnHandlerInterface,
@@ -38,8 +38,8 @@ const notFoundHandler = (c: Context) => {
   return c.text('404 Not Found', 404)
 }
 
-const errorHandler = (err: Error, c: Context) => {
-  if (err instanceof HTTPException) {
+const errorHandler = (err: Error | HTTPResponseError, c: Context) => {
+  if ('getResponse' in err) {
     return err.getResponse()
   }
   console.error(err)
diff --git a/src/types.ts b/src/types.ts
index 2178e5c1..87691b01 100644
--- a/src/types.ts
+++ b/src/types.ts
@@ -91,8 +91,12 @@ export type H<
 > = Handler<E, P, I, R> | MiddlewareHandler<E, P, I>
 
 export type NotFoundHandler<E extends Env = any> = (c: Context<E>) => Response | Promise<Response>
+
+export interface HTTPResponseError extends Error {
+  getResponse: () => Response
+}
 export type ErrorHandler<E extends Env = any> = (
-  err: Error,
+  err: Error | HTTPResponseError,
   c: Context<E>
 ) => Response | Promise<Response>

@yusukebe
Copy link
Member Author

yusukebe commented Jun 3, 2024

@usualoma Thank you for the suggestion!

I also thought it was okay to check getResponse only first. As you said, we can ignore other applications that have "errors implementing getResponse()" since they are very rare or do not exist. So, I'll follow your idea.

@yusukebe
Copy link
Member Author

yusukebe commented Jun 3, 2024

@usualoma

Can you create the new PR with your patch?

usualoma added a commit to usualoma/hono that referenced this pull request Jun 3, 2024
This change is intended to make error handling more flexible and reduce bundle size.
This idea is originated from honojs#2885

Co-authored-by: Yusuke Wada <[email protected]>
usualoma added a commit to usualoma/hono that referenced this pull request Jun 3, 2024
This change is intended to make error handling more flexible and reduce bundle size.
This idea is originated from honojs#2885

Co-authored-by: Yusuke Wada <[email protected]>
@usualoma
Copy link
Member

usualoma commented Jun 3, 2024

@yusukebe Created! #2898

yusukebe added a commit that referenced this pull request Jun 3, 2024
…#2898)

This change is intended to make error handling more flexible and reduce bundle size.
This idea is originated from #2885

Co-authored-by: Yusuke Wada <[email protected]>
@yusukebe
Copy link
Member Author

yusukebe commented Jun 3, 2024

This is not necessary now since #2898 has been merged.

@yusukebe yusukebe closed this Jun 3, 2024
@usualoma usualoma mentioned this pull request Jun 21, 2024
4 tasks
@yusukebe yusukebe deleted the perf/dont-import-http-exception-to-hono-base branch June 27, 2024 01:20
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.

2 participants