Skip to content

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

Closed
exoego wants to merge 2 commits into
honojs:mainfrom
exoego:partial-contents
Closed

exoego wants to merge 2 commits into
honojs:mainfrom
exoego:partial-contents

Conversation

@exoego

@exoego exoego commented Sep 28, 2024 •

Copy link
Copy Markdown
Contributor

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

@codecov

codecov Bot commented Sep 29, 2024 •

Copy link
Copy Markdown

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 ⚠️
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.

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

@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
Comment thread src/request.ts

Copy link
Copy Markdown
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

yusukebe commented Oct 8, 2024

Copy link
Copy Markdown
Member

Hi @exoego

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

@yusukebe

Copy link
Copy Markdown
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