Gathering detailed insights and metrics for magic-bytes.js
Gathering detailed insights and metrics for magic-bytes.js
Gathering detailed insights and metrics for magic-bytes.js
Gathering detailed insights and metrics for magic-bytes.js
npm install magic-bytes.js
Typescript
Module System
Node Version
NPM Version
99.5
Supply Chain
99.6
Quality
78.5
Maintenance
100
Vulnerability
100
License
TypeScript (87.64%)
HTML (6.7%)
CSS (3.34%)
JavaScript (2.32%)
Total Downloads
16,112,014
Last Day
25,595
Last Week
269,794
Last Month
1,138,860
Last Year
10,580,015
MIT License
145 Stars
226 Commits
29 Forks
1 Watchers
7 Branches
16 Contributors
Updated on Jun 24, 2025
Minified
Minified + Gzipped
Latest Version
1.12.1
Package Id
magic-bytes.js@1.12.1
Unpacked Size
55.16 kB
Size
11.42 kB
File Count
21
NPM Version
10.1.0
Node Version
18.20.8
Published on
Apr 26, 2025
Cumulative downloads
Total Downloads
Last Day
-1.4%
25,595
Compared to previous day
Last Week
-0.9%
269,794
Compared to previous week
Last Month
5%
1,138,860
Compared to previous month
Last Year
119.3%
10,580,015
Compared to previous year
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 removed though.
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"]
register(fileType: string, string[])
: registers a custom signatureBoth 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:
1import filetype from 'magic-bytes.js' 2 3filetype(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.
1<input type="file" id="file" />
2
3 <script src="node_modules/magic-bytes.js/dist/browser.js" type="application/javascript"></script>
4<script>
5 document.getElementById("file").addEventListener('change', (event, x) => {
6 const fileReader = new FileReader();
7 fileReader.onloadend = (f) => {
8 const bytes = new Uint8Array(f.target.result);
9 console.log("Possible filetypes: " + filetypeinfo(bytes))
10 }
11 fileReader.readAsArrayBuffer(event.target.files[0])
12 })
13</script>
Run npm test
See examples/
The create-snapshot.js
creates a new tree. The tree has a similar shape to the following
1{ 2 "0x47": { 3 "0x49": { 4 "0x46": { 5 "0x38": { 6 "0x37": { 7 "0x61": { 8 "matches": [ 9 { 10 "typename": "gif", 11 "mime": "image/gif", 12 "extension": "gif" 13 } 14 ] 15 } 16 }, 17 } 18 } 19 } 20 } 21}
It acts as a giant lookup map for the given byte signatures.
No vulnerabilities found.
Reason
13 commit(s) and 1 issue activity found in the last 90 days -- score normalized to 10
Reason
no dangerous workflow patterns detected
Reason
no binaries found in the repo
Reason
license file detected
Details
Reason
detected GitHub workflow tokens with excessive permissions
Details
Reason
Found 9/27 approved changesets -- score normalized to 3
Reason
dependency not pinned by hash detected -- score normalized to 2
Details
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
Reason
project is not fuzzed
Details
Reason
branch protection not enabled on development/release branches
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Reason
35 existing vulnerabilities detected
Details
Score
Last Scanned on 2025-06-30
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More