hono icon indicating copy to clipboard operation
hono copied to clipboard

[PoC] refactor: implement serve-static middleware with Node.js API

Open usualoma opened this issue 1 year ago • 5 comments

This PR also fixes #3324. Although it is less thorough than #3461 or #3516, it implements the minimum necessary processing of the Renge request header. (And I think the performance is not bad.)

What is this?

This also has something to do with the organization of serve-static and its integration with node-serve, but as for serve-static, I think that by importing “node:*”, we can use node-serve's serve-static.ts as-is in deno and bun. (I haven't checked whether it works with older versions of Deno and Bun, but I think it should work with the current versions.) https://github.com/honojs/node-server/blob/main/src/serve-static.ts

Currently, the serve-static is abstracting to bridge the differences in runtime. Still, I think eliminating the abstraction would be simpler, and I would stop trying to absorb the differences with Hono. https://github.com/honojs/hono/blob/main/src/middleware/serve-static/index.ts#L36-L40

If serve-static in cloudflare-workers is deprecated, I think it would be a good idea to consolidate middleware/serve-static when a major version is released and unify it into a version that uses “node:*”.

Alternatively, I think it might be a good idea to have an “adapter for serve-static that proxies to cloud storage such as R2, S3 and GCP Cloud Storage”. However, if you were to implement this, I think it would be better to have independent code without the current abstraction layer. I think the “Worse is Better” approach would be appropriate here.

The author should do the following, if applicable

  • [ ] Add tests
  • [x] Run tests
  • [x] bun run format:fix && bun run lint:fix to format the code

usualoma avatar Oct 17 '24 03:10 usualoma

Codecov Report

Attention: Patch coverage is 63.76812% with 50 lines in your changes missing coverage. Please review.

Project coverage is 94.22%. Comparing base (90833d2) to head (01817e4). Report is 117 commits behind head on main.

Files with missing lines Patch % Lines
src/adapter/node/serve-static.ts 63.97% 49 Missing :warning:
src/adapter/deno/serve-static.ts 0.00% 1 Missing :warning:

:x: Your patch check has failed because the patch coverage (63.76%) is below the target coverage (80.00%). You can increase the patch coverage or adjust the target coverage.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #3523      +/-   ##
==========================================
- Coverage   94.29%   94.22%   -0.08%     
==========================================
  Files         157      158       +1     
  Lines        9488     9561      +73     
  Branches     2763     2786      +23     
==========================================
+ Hits         8947     9009      +62     
- Misses        541      552      +11     

:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.

codecov[bot] avatar Oct 17 '24 03:10 codecov[bot]

According to this PR, we could say that the following policies apply.

  • Web requests and responses follow web standards
  • Access to the file system follows the Node.js API

usualoma avatar Oct 17 '24 04:10 usualoma

This is a draft of one approach to the following comments.

https://github.com/honojs/hono/pull/3516#issuecomment-2415983393

usualoma avatar Oct 17 '24 04:10 usualoma

Hi @usualoma !

Thank you for the PR. Using the Node.js API, node:*, for Deno and Bun is a good approach!

However, though I think you already noticed it, this hono repo still has runtime-specific APIs, including for WebSockets and ConnInfo. In my thoughts, the ideal solution may be to remove runtime-specific APIs from this hono repo and use only WebStandards APIs. If so, we must create another namespace for modules like this serve-static middleware.

We should consider where to put runtime-specific things.

yusukebe avatar Oct 17 '24 05:10 yusukebe

In addition, if we merge this, this issue can be resolved: https://github.com/honojs/hono/issues/3483

yusukebe avatar Oct 17 '24 05:10 yusukebe