Skip to content

refactor(timing/utils-body): a few minor edits #3557

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

Merged
merged 5 commits into from
Oct 31, 2024
Merged

Conversation

sharma-shray
Copy link
Contributor

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 Oct 24, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 94.71%. Comparing base (0a99bd3) to head (ee32903).
Report is 20 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #3557      +/-   ##
==========================================
+ Coverage   94.27%   94.71%   +0.44%     
==========================================
  Files         157      157              
  Lines        9501     9537      +36     
  Branches     2854     2783      -71     
==========================================
+ Hits         8957     9033      +76     
+ Misses        544      504      -40     

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

@usualoma
Copy link
Member

Hi @sharma-shray, thanks for the suggestion.

It may seem like a change that is obvious to you and doesn't need to be explained, but from the perspective of maintenance, it would be appreciated if the “why” was written in the commit message. Please use the following format as a reference for writing messages in future commits.

https://www.conventionalcommits.org/en/v1.0.0/

@@ -27,7 +27,7 @@ export const Suspense: FC<PropsWithChildren<{ fallback: any }>> = async ({
fallback,
}) => {
if (!children) {
return fallback.toString()
return await fallback.toString()
Copy link
Member

Choose a reason for hiding this comment

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

Please add cases where this change is necessary to the test.

@@ -30,7 +30,9 @@ interface TimingOptions {
const getTime = (): number => {
try {
return performance.now()
} catch {}
} catch (e) {
console.warn('performance.now() is not available. Falling back to Date.now()', e)
Copy link
Member

Choose a reason for hiding this comment

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

There are already several console.warn() in this file, but these are displayed when “middleware is not set up correctly”.

This fallback to Date.now() is displayed when running in an environment where performance.now() is not available, but I don't think users would be happy if they were shown a warning for every request.

If we display a warning when it is not available, it would be better to do it like this.

diff --git a/src/middleware/timing/timing.ts b/src/middleware/timing/timing.ts
index d387f43b..f341592f 100644
--- a/src/middleware/timing/timing.ts
+++ b/src/middleware/timing/timing.ts
@@ -27,12 +27,9 @@ interface TimingOptions {
   crossOrigin?: boolean | string | ((c: Context) => boolean | string)
 }
 
-const getTime = (): number => {
-  try {
-    return performance.now()
-  } catch {}
-  return Date.now()
-}
+const getTime: () => number =
+  performance?.now?.bind(performance) ??
+  (console.warn('performance.now is not supported, using Date.now'), Date.now.bind(Date))
 
 /**
  * Server-Timing Middleware for Hono.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hello! thanks for you response

Now I really think that was unnecessary, was planning to add some outputs about possible bugs, but thanks for your comments!

There are already several console.warn() in this file, but these are displayed when “middleware is not set up correctly”.

This fallback to Date.now() is displayed when running in an environment where performance.now() is not available, but I don't think users would be happy if they were shown a warning for every request.

If we display a warning when it is not available, it would be better to do it like this.

diff --git a/src/middleware/timing/timing.ts b/src/middleware/timing/timing.ts
index d387f43b..f341592f 100644
--- a/src/middleware/timing/timing.ts
+++ b/src/middleware/timing/timing.ts
@@ -27,12 +27,9 @@ interface TimingOptions {
   crossOrigin?: boolean | string | ((c: Context) => boolean | string)
 }
 
-const getTime = (): number => {
-  try {
-    return performance.now()
-  } catch {}
-  return Date.now()
-}
+const getTime: () => number =
+  performance?.now?.bind(performance) ??
+  (console.warn('performance.now is not supported, using Date.now'), Date.now.bind(Date))
 
 /**
  * Server-Timing Middleware for Hono.

@yusukebe yusukebe changed the title a few minor edits refactor(timing/utils-body): a few minor edits Oct 31, 2024
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

Hi @sharma-shray

Thank you for the PR. Looks good. But next time, please follow the @usualoma 's comment.

@yusukebe yusukebe merged commit fea5947 into honojs:main Oct 31, 2024
16 checks passed
TinsFox pushed a commit to TinsFox/hono that referenced this pull request Nov 11, 2024
* upd

* upd

* upd

* upd

* Add files via upload
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