Product
Introducing SSO
Streamline your login process and enhance security by enabling Single Sign-On (SSO) on the Socket platform, now available for all customers on the Enterprise plan, supporting 20+ identity providers.
magic-bytes.js
Advanced tools
Readme
Magic Bytes is a javascript library analyzing the first bytes of a file to tell you its type. Use it inside your browser or serversided using nodejs.
The procedure is based on https://en.wikipedia.org/wiki/List_of_file_signatures.
[!NOTE]
A small note on versioning. Strictly speaking, each new filetype which is supported by this library can break someones' API. Please note that this library adds new filetypes with minor release. This means files, which validate to "null" in some versions, may find a result in a new version.Or in some cases the library will find more results, than before. So don't depend on the found-array size in any shape or form. Filetypes will not be remoevd tho
Run npm install magic-bytes.js
There is an interactive example present at https://larskoelpin.github.io/magic-bytes/.
The following functions are available:
filetypeinfo(bytes: number[])
Contains typeinformation like name, extension and mime type: [{typename: "zip"}, {typename: "jar"}]
filetypename(bytes: number[])
: Contains type names only: ["zip", "jar"]
filetypemime(bytes: number[])
: Contains type mime types only: ["application/zip", "application/jar"]
filetypeextension(bytes: number[])
: Contains type extensions only: ["zip", "jar"]
Both function return an empty array []
otherwise, which means it could not detect the file signature. Keep in mind that
txt files for example fall in this category.
You don't have to load the whole file in memory. For validating a file uploaded to S3 using Lambda for example, it may be
enough to load the files first 100 bytes and validate against them. This is especially useful for big files.
see examples for practical usage.
On server:
import filetype from 'magic-bytes.js'
filetype(fs.readFileSync("myimage.png")) // ["png"]
To run an HTML-Example checkout the project and run
npm install; npm run example
This opens an HTML example using magic bytes as a window variable. It kinda looks like that.
<input type="file" id="file" />
<script src="node_modules/magic-bytes.js/dist/browser.js" type="application/javascript"></script>
<script>
document.getElementById("file").addEventListener('change', (event, x) => {
const fileReader = new FileReader();
fileReader.onloadend = (f) => {
const bytes = new Uint8Array(f.target.result);
console.log("Possible filetypes: " + filetypeinfo(bytes))
}
fileReader.readAsArrayBuffer(event.target.files[0])
})
</script>
Run npm test
See examples/
The create-snapshot.js
creates a new tree. The tree has a similar shape to the following
{
"0x47": {
"0x49": {
"0x46": {
"0x38": {
"0x37": {
"0x61": {
"matches": [
{
"typename": "gif",
"mime": "image/gif",
"extension": "gif"
}
]
}
},
}
}
}
}
}
It acts as a giant lookup map for the given byte signatures.
FAQs
Detect Filetype by bytes
The npm package magic-bytes.js receives a total of 105,262 weekly downloads. As such, magic-bytes.js popularity was classified as popular.
We found that magic-bytes.js demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Product
Streamline your login process and enhance security by enabling Single Sign-On (SSO) on the Socket platform, now available for all customers on the Enterprise plan, supporting 20+ identity providers.
Security News
Tea.xyz, a crypto project aimed at rewarding open source contributions, is once again facing backlash due to an influx of spam packages flooding public package registries.
Security News
As cyber threats become more autonomous, AI-powered defenses are crucial for businesses to stay ahead of attackers who can exploit software vulnerabilities at scale.