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
77.8
Maintenance
100
Vulnerability
100
License
TypeScript (87.25%)
HTML (6.97%)
CSS (3.48%)
JavaScript (2.3%)
Total Downloads
11,677,162
Last Day
23,151
Last Week
193,049
Last Month
830,267
Last Year
8,485,621
136 Stars
213 Commits
26 Forks
1 Watching
7 Branches
14 Contributors
Minified
Minified + Gzipped
Latest Version
1.10.0
Package Id
magic-bytes.js@1.10.0
Unpacked Size
47.97 kB
Size
12.40 kB
File Count
26
NPM Version
10.1.0
Node Version
18.19.1
Publised On
29 Feb 2024
Cumulative downloads
Total Downloads
Last day
-1.6%
23,151
Compared to previous day
Last week
-9.6%
193,049
Compared to previous week
Last month
1.5%
830,267
Compared to previous month
Last year
202.7%
8,485,621
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 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:
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
no binaries found in the repo
Reason
no dangerous workflow patterns detected
Reason
license file detected
Details
Reason
detected GitHub workflow tokens with excessive permissions
Details
Reason
dependency not pinned by hash detected -- score normalized to 2
Details
Reason
Found 5/27 approved changesets -- score normalized to 1
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
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
24 existing vulnerabilities detected
Details
Score
Last Scanned on 2025-01-20
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