Repository navigation
Low level MIME sniffing #49843
Description
Activity
- addedfeature requestIssues requesting new Node.js features.Issues requesting new Node.js features.
on Sep 24, 2023 whether through extension lookup or by inspecting "magic numbers"
Both have problems. Just looking at the extension is prone to false positives (e.g. GIF file with a .jpg extension), while sniffing the first few bytes is problematic with non-files like fifos (can't seek, can't rewind.)
Even determining if a file is text/plain is challenging if you don't know the encoding. A file consisting of the octets
00 00 00 20is text/plain only when you know it's UTF32-BE, otherwise it's application/octet-stream.https://mimesniff.spec.whatwg.org/ seems to be aimed at HTTP, not file systems, so I don't think it's of much use. I feel node shouldn't be in the business of making educated guesses, not when everyone else is making different educated guesses.
Reacted by Roman ShabanovFor future reference, we have a fast mimesniff parser at Ada - https://github.com/ada-url/mimesniff
Reacted by Jimmy Wärting and Johnwook ChoiA million years ago i programmed this
Is this something we should put in a transform stream?
github-actions commented
on Apr 14, 2024 on Apr 14, 2024 – with GitHub ActionsContributorMore actionsThere has been no activity on this feature request for 5 months. To help maintain relevant open issues, please add the never-stale
Issues and PRs exempt from automated stale handling. label or close this issue if it should be closed. If not, the issue will be automatically closed 6 months after the last non-automated comment.
For more information on how the project manages feature requests, please consult the feature request management document.- addedstaleIssues and PRs marked stale due to inactivity and scheduled for automatic closure.Issues and PRs marked stale due to inactivity and scheduled for automatic closure.
on Apr 14, 2024 github-actions commented
on May 15, 2024 on May 15, 2024 – with GitHub ActionsContributorMore actionsThere has been no activity on this feature request and it is being closed. If you feel closing this issue is not the right thing to do, please leave a comment.
For more information on how the project manages feature requests, please consult the feature request management document.
What is the problem this feature will solve?
This issue came up during a discussion related to
fs.openAsBlob, whereblob.typecurrently returns an empty string. I must agree that it would be nice if Node.js could automatically determine the MIME type of a given entity, whether through extension lookup or by inspecting "magic numbers".Of course, there might be some cases where certain file extensions collide, but I believe it would be best to adhere to the IANA standards. After all, all browsers are capable of deducing certain information. Here's a straightforward example:
I'm not sure about the exact mechanism behind it, but it simply works somehow.
I've come across some useful resources that might help address this issue:
These resources could provide valuable insights and potential solutions for improving the MIME type detection in Node.js.
What is the feature you are proposing to solve the problem?
I'm unsure about the API's potential appearance, but it would be beneficial to explore various methods for detecting it. I'm also uncertain whether it should return an array of potential matches along with their respective scores and probabilities.
For instance:
Perhaps when searching for a path, it could consider both the file name extension and perform MIME sniffing.
Lastly:
Please note that the exact method names and functionality would depend on the implementation details of the API.
I think this would serve the community to have this built right in as it's something that is very commonly used.