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 🙏

© 2024 – Pkg Stats / Ryan Hefner

mimetics

v1.0.0

Published

Identifies file types based on magic bytes, patterns, and other unique file attributes.

Downloads

3,492

Readme

Mimetics

Know thy files

npm pipeline license downloads

Gitlab Github Twitter Discord

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.

Installation

To add Mimetics to your project, use:

npm install mimetics

Usage

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.

Basic Example

const Mimetics = require('mimetics')
const fs = require('fs')

// Load a buffer from a file (e.g., sample.txt)
const buffer = fs.readFileSync('sample.txt')
const mimetics = new Mimetics()

// Synchronous file type detection
const fileType = mimetics.parse(buffer)
console.log(fileType) // Output: { tag: 'text', type: 'text', ext: 'txt', mime: 'text/plain' }

// Asynchronous file type detection (for ZIP files, etc.)
mimetics.parseAsync(buffer).then(fileType => {
  console.log(fileType)
})

Adding Custom Definitions

You can extend Mimetics with custom definitions for additional file types.


const Mimetics = require('mimetics')

const customDefinitions = [
  {
    tag: 'custom',
    type: 'myfiletype',
    ext: 'myft',
    mime: 'application/x-myfiletype',
    magic: [0x12, 0x34, 0x56],
    pattern: /^MYFILE/i,
  }
]

const mimetics = new Mimetics()
mimetics.addDefinitions(customDefinitions)

const buffer = /* load a buffer for a custom file type */
const fileType = mimetics.parse(buffer)
console.log(fileType) // Output should match custom definition

Browser Example

const Mimetics = require('mimetics')

const fileInput = document.querySelector('#fileInput')
fileInput.addEventListener('change', async (event) => {
  const file = event.target.files[0]
  const mimetics = new Mimetics()
  
  const fileType = await mimetics.fromFile(file)
  console.log(fileType) // Output: file type information
})

Supported File Types

Mimetics currently supports a variety of formats, including but not limited to:

  • Text: Plain text (txt), Markdown (md), LaTeX (tex), RTF (rtf)
  • Image: JPEG (jpg, jpeg), PNG (png), GIF (gif), BMP (bmp), ICON (ico), WebP (webp), PDF (pdf), SVG (svg)
  • Audio: MP3 (mp3), OGG (ogg), WAV (wav)
  • Video: MP4 (mp4), QuickTime (mov), AVI (avi), MKV (mkv), WebM (webm), FLV (flv)
  • Archive: ZIP (zip), RAR (rar), GZIP (gz), 7ZIP (7z)
  • Ebook: EPUB (epub)

API Reference

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.

  • Parameters:
    • definitions (Array): Array of custom file definitions to add, with each object containing properties like tag, type, ext, mime, magic, and pattern.

For more detailed API documentation, see the API reference and the comments in the code.

Contributing

Contributions are welcome. Submit a Pull Request or open an Issue to discuss any changes. Please read contributing.md for details on our code of conduct, and the process for submitting merge requests to us.

Testing

Mimetics includes a test suite built with testr.

To run the test, first clone the respository:

git clone https://github.com/basedwon/mimetics.git

Install the dependencies, then run npm test:

npm install
npm test

Donations

If you find this project useful and want to help support further development, please send us some coin. We greatly appreciate any and all contributions. Thank you!

Bitcoin (BTC):

1JUb1yNFH6wjGekRUW6Dfgyg4J4h6wKKdF

Monero (XMR):

46uV2fMZT3EWkBrGUgszJCcbqFqEvqrB4bZBJwsbx7yA8e2WBakXzJSUK8aqT4GoqERzbg4oKT2SiPeCgjzVH6VpSQ5y7KQ

License

Mimetics is MIT licensed.