npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

mime-detector

v1.0.8

Published

A TypeScript module to detect file MIME types

Readme

mime-detector

A TypeScript module that accurately detects file MIME types by examining both file contents (magic bytes) and extensions.

Features

  • Accurate file type detection using magic bytes (file signatures)
  • Fallback to extension-based detection when magic bytes aren't conclusive
  • Support for common document, image, audio, and video formats
  • Written in TypeScript with full type definitions
  • Zero runtime dependencies

Installation

npm install mime-detector

Usage

import { getMimeType, getMimeExtension, isDocument, isImage, isAudio, isVideo } from 'mime-detector';

// Get MIME type
console.log(await getMimeType('https://example.com/document.pdf')); // 'application/pdf'
console.log(await getMimeType('https://example.com/image.jpg')); // 'image/jpeg'

// Get file extension from MIME type
console.log(getMimeExtension('image/jpeg')); // '.jpg'
console.log(getMimeExtension('application/pdf')); // '.pdf'

// Check file type
console.log(await isDocument('https://example.com/document.pdf')); // true
console.log(await isImage('https://example.com/image.jpg')); // true
console.log(await isAudio('https://example.com/song.mp3')); // true
console.log(await isVideo('https://example.com/movie.mp4')); // true

API

getMimeType(filePath: string): Promise

Returns the MIME type for the given file path. First attempts to detect the type by reading the file's magic bytes, then falls back to extension-based detection if necessary.

getMimeExtension(mimeType: string): string | null

Returns the corresponding file extension (including the dot) for a given MIME type. Returns null if no matching extension is found.

Helper Functions

  • isDocument(filePath: string): Promise<boolean> - Checks if the file is a document
  • isImage(filePath: string): Promise<boolean> - Checks if the file is an image
  • isAudio(filePath: string): Promise<boolean> - Checks if the file is an audio file
  • isVideo(filePath: string): Promise<boolean> - Checks if the file is a video file

Supported Formats

Documents

  • PDF (.pdf)
  • Microsoft Word (.doc, .docx)
  • Text (.txt)
  • Rich Text Format (.rtf)

Images

  • JPEG (.jpg, .jpeg)
  • PNG (.png)
  • GIF (.gif)
  • WebP (.webp)
  • SVG (.svg)

Audio

  • MP3 (.mp3)
  • WAV (.wav)
  • OGG (.ogg)
  • M4A (.m4a)

Video

  • MP4 (.mp4)
  • WebM (.webm)
  • AVI (.avi)
  • QuickTime (.mov)
  • Matroska (.mkv)

Magic Bytes Detection

The module first attempts to detect file types by examining their magic bytes (file signatures). This provides more accurate detection than extension-based methods alone, as it looks at the actual file contents. Currently supports magic bytes detection for:

  • PDF files
  • JPEG images
  • PNG images
  • GIF images
  • WebP images
  • SVG images
  • MP3 audio
  • MP4 video
  • WebM video

For other formats, or when magic bytes detection fails, the module falls back to extension-based detection.

Error Handling

If the file cannot be read (e.g., due to permissions or if it doesn't exist), the module falls back to extension-based detection. If the file type cannot be determined, it returns 'application/octet-stream'.

License

MIT

Contributing

Contributions are welcome! Feel free to submit issues and pull requests.

Some areas where you could help:

  • Adding support for more file formats
  • Adding more magic byte signatures
  • Improving detection accuracy
  • Adding tests
  • Documentation improvements