Gathering detailed insights and metrics for mimetics
Gathering detailed insights and metrics for mimetics
Mimetics determines the file type, MIME type, and media type of a given file using magic numbers and content analysis to detect the most likely file type and then maps that to the appropriate MIME and media type.
npm install mimetics
Typescript
Module System
Node Version
NPM Version
71.7
Supply Chain
96.6
Quality
75.6
Maintenance
100
Vulnerability
99.3
License
JavaScript (100%)
Total Downloads
45,417
Last Day
193
Last Week
827
Last Month
3,241
Last Year
37,703
5 Stars
28 Commits
2 Forks
1 Watching
1 Branches
1 Contributors
Minified
Minified + Gzipped
Latest Version
1.0.0
Package Id
mimetics@1.0.0
Unpacked Size
20.78 MB
Size
20.68 MB
File Count
27
NPM Version
8.19.4
Node Version
16.20.0
Publised On
01 Nov 2024
Cumulative downloads
Total Downloads
Last day
25.3%
193
Compared to previous day
Last week
-4.7%
827
Compared to previous week
Last month
-8.9%
3,241
Compared to previous month
Last year
388.8%
37,703
Compared to previous year
1
3
Know thy files
Mimetics identifies file types based on magic bytes, patterns, and other unique file attributes. It provides an intuitive API for both synchronous and asynchronous file type detection, supporting various file formats including text, image, audio, video, and archive types.
To add Mimetics to your project, use:
1npm install mimetics
Mimetics allows you to detect file types from file buffers, file names, and even File objects in browser environments. You can also extend its functionality by adding custom definitions.
1const Mimetics = require('mimetics') 2const fs = require('fs') 3 4// Load a buffer from a file (e.g., sample.txt) 5const buffer = fs.readFileSync('sample.txt') 6const mimetics = new Mimetics() 7 8// Synchronous file type detection 9const fileType = mimetics.parse(buffer) 10console.log(fileType) // Output: { tag: 'text', type: 'text', ext: 'txt', mime: 'text/plain' } 11 12// Asynchronous file type detection (for ZIP files, etc.) 13mimetics.parseAsync(buffer).then(fileType => { 14 console.log(fileType) 15})
You can extend Mimetics with custom definitions for additional file types.
1 2const Mimetics = require('mimetics') 3 4const customDefinitions = [ 5 { 6 tag: 'custom', 7 type: 'myfiletype', 8 ext: 'myft', 9 mime: 'application/x-myfiletype', 10 magic: [0x12, 0x34, 0x56], 11 pattern: /^MYFILE/i, 12 } 13] 14 15const mimetics = new Mimetics() 16mimetics.addDefinitions(customDefinitions) 17 18const buffer = /* load a buffer for a custom file type */ 19const fileType = mimetics.parse(buffer) 20console.log(fileType) // Output should match custom definition
1const Mimetics = require('mimetics') 2 3const fileInput = document.querySelector('#fileInput') 4fileInput.addEventListener('change', async (event) => { 5 const file = event.target.files[0] 6 const mimetics = new Mimetics() 7 8 const fileType = await mimetics.fromFile(file) 9 console.log(fileType) // Output: file type information 10})
Mimetics currently supports a variety of formats, including but not limited to:
txt
), Markdown (md
), LaTeX (tex
), RTF (rtf
)jpg
, jpeg
), PNG (png
), GIF (gif
), BMP (bmp
), ICON (ico
), WebP (webp
), PDF (pdf
), SVG (svg
)mp3
), OGG (ogg
), WAV (wav
)mp4
), QuickTime (mov
), AVI (avi
), MKV (mkv
), WebM (webm
), FLV (flv
)zip
), RAR (rar
), GZIP (gz
), 7ZIP (7z
)epub
)parse(buffer, name)
Synchronously parses a buffer to identify the file type.
Parameters:
buffer
(Uint8Array | ArrayBuffer): The file buffer to parse.name
(string, optional): The file name, which can help in detection.Returns: File type object or null
if no match is found.
parseAsync(buffer, name)
Asynchronously parses a buffer to identify the file type, with support for ZIP archive analysis.
Parameters:
buffer
(Uint8Array | ArrayBuffer): The file buffer to parse.name
(string, optional): The file name, which can assist in detection.Returns: A promise resolving to a file type object or null
if no match is found.
fromName(filePath)
Determines file type based on the file name extension.
Parameters:
filePath
(string): The file path or name.Returns: File type object based on the file extension or null
if no match is found.
fromFile(file)
Asynchronously determines the file type from a File object (for browser use).
Parameters:
file
(File): File object to analyze.Returns: A promise resolving to a file type object.
addDefinitions(definitions)
Adds custom file definitions to the existing set.
definitions
(ArrayNo vulnerabilities found.
No security vulnerabilities found.