hono icon indicating copy to clipboard operation
hono copied to clipboard

feat(serve-static): "206 Partial Content" support

Open exoego opened this issue 1 year ago • 3 comments

Closes #3324

This implements 206 Partial Content(also called as range requests) support in serve-static middleware. It will be useful specially for seeking a video file.

serve-static base implementation now accepts an optional function:

partialContentSupport?: (path: string) => Promise<{
      size: number
      getPartialContent: (start: number, end: number) => PartialContent
      close: () => void
}>

If partialContentSupport is provided and HTTP header includes valid Range, serve-static returns subsets of content. getPartialContent function encapsulates a file handle to avoid opening and closing file multiple times. close function is invoked when all ranges are read, to teardown the file handle.

416 Range Not Satisfiable

The implementation also returns 416 Range Not Satisfiable if Range headers contains many range requests (more than 10), that is potentially DoS (Denial of Service) attack. This limitation (10) is just a clueless number. The number may be changed when we understand it is too small/too big.

Not included

  • Cache-control via If-Range, ETag and so on. I think this feature is useful without having cache-control, which needs a lot work.
  • Node.js implementation. Because Node.js does not share serverStatic implementation 😭 It is solely implemented here: https://github.com/honojs/node-server/blob/74e86a28f375e5acd52e342519ff2b1110a95c16/src/serve-static.ts#L59
  • Deno coverage is 0%: #3456 will fix it

The author should do the following, if applicable

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

exoego avatar Sep 28 '24 10:09 exoego

Codecov Report

Attention: Patch coverage is 82.65896% with 30 lines in your changes missing coverage. Please review.

Project coverage is 95.36%. Comparing base (f1ec415) to head (07b6334). Report is 217 commits behind head on main.

Files with missing lines Patch % Lines
src/adapter/deno/serve-static.ts 0.00% 24 Missing :warning:
src/adapter/bun/serve-static.ts 81.81% 6 Missing :warning:
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #3461      +/-   ##
==========================================
- Coverage   95.58%   95.36%   -0.22%     
==========================================
  Files         155      155              
  Lines        9357     9524     +167     
  Branches     2746     2776      +30     
==========================================
+ Hits         8944     9083     +139     
- Misses        413      441      +28     

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

:rocket: New features to boost your workflow:
  • :snowflake: Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • :package: JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

codecov[bot] avatar Sep 29 '24 00:09 codecov[bot]

Hi @exoego

I'm sorry I can't review it yet. Still a little bit busy; please wait.

yusukebe avatar Oct 08 '24 10:10 yusukebe

@exoego

I'm sorry for being super late. I have an idea, and I'll share it with you soon.

yusukebe avatar Oct 12 '24 11:10 yusukebe