Skip to content

fix: use jsdelivr's content-type header to detect binary file#2036

Open
shuuji3 wants to merge 4 commits intomainfrom
shuuji3/fix/improve-binary-file-detection-with-content-type
Open

fix: use jsdelivr's content-type header to detect binary file#2036
shuuji3 wants to merge 4 commits intomainfrom
shuuji3/fix/improve-binary-file-detection-with-content-type

Conversation

@shuuji3
Copy link
Member

@shuuji3 shuuji3 commented Mar 11, 2026

🔗 Linked issue

resolve #2034

🧭 Context

#1959 introduced a new feature to detect the binary file to prevent showing binary content in the text content viewer. But the current detection method is only checking the filename, resulting in the possible false positive detection.

This switches the logic to use the content-type header from the upstream CDN response.

📚 Description

  • Currently, the server API endpoint to fetch a requested file from jsdelivr CDN, only returns the CDN file content to the client, now it also returns the content-type header value from CDN.
  • Update the binary detection logic on the client side to use content-type. For example, image/* will be estimated as a binary file, instead of checking if the file path ends with .png, .jpg, etc.

Example:

screenshot of binary warning, saying 'Binary file File type 'application/wasm' is not supported for preview. View raw file'

@vercel
Copy link

vercel bot commented Mar 11, 2026

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
docs.npmx.dev Ready Ready Preview, Comment Mar 14, 2026 8:37am
npmx.dev Ready Ready Preview, Comment Mar 14, 2026 8:37am
1 Skipped Deployment
Project Deployment Actions Updated (UTC)
npmx-lunaria Ignored Ignored Mar 14, 2026 8:37am

Request Review

@codecov
Copy link

codecov bot commented Mar 11, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ All tests successful. No failed tests found.

📢 Thoughts on this report? Let us know!

@shuuji3 shuuji3 closed this Mar 12, 2026
@shuuji3 shuuji3 force-pushed the shuuji3/fix/improve-binary-file-detection-with-content-type branch from 46a7626 to d3cfce5 Compare March 12, 2026 02:38
@github-actions
Copy link

github-actions bot commented Mar 13, 2026

Lunaria Status Overview

🌕 This pull request will trigger status changes.

Learn more

By default, every PR changing files present in the Lunaria configuration's files property will be considered and trigger status changes accordingly.

You can change this by adding one of the keywords present in the ignoreKeywords property in your Lunaria configuration file in the PR's title (ignoring all files) or by including a tracker directive in the merged commit's description.

Tracked Files

File Note
i18n/locales/en.json Source changed, localizations will be marked as outdated.
Warnings reference
Icon Description
🔄️ The source for this localization has been updated since the creation of this pull request, make sure all changes in the source have been applied.

@shuuji3 shuuji3 changed the title fix: add contentType key to /api/registry/file/... response fix: use jsdlvr's content-type header to detect binary file Mar 14, 2026
@shuuji3 shuuji3 changed the title fix: use jsdlvr's content-type header to detect binary file fix: use jsdelivr's content-type header to detect binary file Mar 14, 2026
@coderabbitai
Copy link
Contributor

coderabbitai bot commented Mar 14, 2026

📝 Walkthrough

Walkthrough

Refactors binary-file detection from path-extension based logic to MIME type-based checks by replacing isBinaryFilePath with isBinaryContentType. The server file fetcher now returns { content, contentType } and the API response exposes a new contentType field. The shared PackageFileContentResponse type gains an optional contentType: string | null. Frontend guards that previously excluded binary files by path were removed; UI warnings for unsupported previews now include the actual MIME content type.

Possibly related PRs

  • feat: add binary file warning #1959: Introduced extension-based binary detection and related UI warning; this PR replaces that detection with MIME-based logic and threads contentType from the server.

Suggested reviewers

  • 43081j
🚥 Pre-merge checks | ✅ 3
✅ Passed checks (3 passed)
Check name Status Explanation
Linked Issues check ✅ Passed The PR addresses issue #2034 by implementing content-type-based binary detection via CDN headers instead of solely relying on file extensions.
Out of Scope Changes check ✅ Passed All changes are scoped to resolving #2034: adding contentType to the API response, updating binary detection logic, and displaying the content type in warnings.
Description check ✅ Passed The pull request description is directly related to the changeset, providing context about the shift from path-based to content-type-based binary file detection.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch shuuji3/fix/improve-binary-file-detection-with-content-type
📝 Coding Plan
  • Generate coding plan for human review comments

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

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.

Improve binary file detection logic

1 participant