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

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

exoego
Copy link
Contributor

@exoego exoego commented Sep 28, 2024

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

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 29, 2024

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 2 commits behind head on main.

Files with missing lines Patch % Lines
src/adapter/deno/serve-static.ts 0.00% 24 Missing ⚠️
src/adapter/bun/serve-static.ts 81.81% 6 Missing ⚠️
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     

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

@exoego exoego force-pushed the partial-contents branch 4 times, most recently from d29d1e9 to 5802d0d Compare October 1, 2024 01:47
@exoego exoego changed the title feat(serve-static): Partial contents support feat(serve-static): "206 Partial Content" support Oct 1, 2024
@exoego exoego marked this pull request as ready for review October 1, 2024 02:54
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Note) ? is added because when serve-static checks Range header, headers is undefined on some existing test cases.

@yusukebe
Copy link
Member

yusukebe commented Oct 8, 2024

Hi @exoego

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

@yusukebe
Copy link
Member

@exoego

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

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.

Support returning 206 Partial Content with serveStatic
2 participants