Gathering detailed insights and metrics for mimetics
Gathering detailed insights and metrics for mimetics
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
78.8
Supply Chain
97.2
Quality
78.4
Maintenance
100
Vulnerability
98.6
License
JavaScript (100%)
Total Downloads
71,395
Last Day
27
Last Week
1,503
Last Month
6,430
Last Year
53,167
MIT License
8 Stars
40 Commits
4 Forks
1 Watchers
1 Branches
2 Contributors
Updated on Mar 06, 2025
Minified
Minified + Gzipped
Latest Version
1.0.4
Package Id
mimetics@1.0.4
Unpacked Size
35.01 kB
Size
9.45 kB
File Count
7
NPM Version
8.19.4
Node Version
16.20.0
Published on
Feb 13, 2025
Cumulative downloads
Total Downloads
Last Day
-25%
27
Compared to previous day
Last Week
7.9%
1,503
Compared to previous week
Last Month
-8.9%
6,430
Compared to previous month
Last Year
191.7%
53,167
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.