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

v0.0.8

Published

A library to determine the file type, MIME type, and media type of a given file.

Downloads

3,139

Readme

Mimetics

Know thy files

npm pipeline license downloads

Gitlab Github Twitter Discord

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. I've included all of the file types that GPT could think of, but if I'm missing any please submit a pull request.

Features

  • Detects file type using magic numbers
  • Detects file type using text content analysis
  • Extracts the MIME and media types
  • Supports wide variety of file types.
  • Extendable with user-defined magic numbers, MIME types and file types

Installation

npm install mimetics

Usage

Basic Usage

Read a file and pass the resulting buffer to Mimetics, which analyzes it to determine its file type, MIME type, and media type:

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

const buffer = fs.readFileSync('example.jpg')

// call it as a function:
const fileInfo = mimetics(buffer)

// or, you can call the `parse` method:
const fileInfo = mimetics.parse(buffer)

console.log(fileInfo) // Logs { ext: 'jpg', mime: 'image/jpeg', media: 'image' }

In the Browser

const fileInput = document.getElementById('myFileInput')
fileInput.addEventListener('change', (event) => {
  const file = event.target.files[0]
  const reader = new FileReader()
  reader.onload = (event) => {
    const arrayBuffer = event.target.result
    const fileInfo = mimetics(arrayBuffer)
    console.log(fileInfo)
  }
  reader.readAsArrayBuffer(file)
})

Adding Custom Magic Numbers

Here, we're adding a custom magic number for a hypothetical file type and then using Mimetics to analyze a file of that type:

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

const buffer = fs.readFileSync('example.custom')
const mm = new mimetics({
  magic: { custom: [0x43, 0x55, 0x53, 0x54] }
})
const fileInfo = mm.parse(buffer)

console.log(fileInfo) // Logs { ext: 'custom', mime: 'application/octet-stream', media: 'application' }

Adding Custom MIME Types

Here, we're adding a custom MIME type for a specific file extension:

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

const buffer = fs.readFileSync('example.custom')
const mm = new Mimetics()
mm.addMimeType('custom', 'application/x-custom')

const fileInfo = mm.parse(buffer)

console.log(fileInfo) // Logs { ext: 'custom', mime: 'application/x-custom', media: 'application' }

API

Mimetics exports a single class with the following methods:

  • parse(buffer: Buffer): Object - Takes in a Buffer and returns an object with the file type (ext), MIME type (mime), and media type (media).
  • getFileType(buffer: Buffer): string - Determines the file type from the provided buffer.
  • getMimeType(extension: string): string - Returns the MIME type for the provided file extension.
  • getMediaType(extension: string): string - Returns the media type for the provided file extension.

Mimetics also provides a comprehensive API to customize and extend the default behavior:

  • setOptions(opts: Object): Mimetics - Sets options for the instance, enabling the addition of custom magic numbers, MIME types, file types and edge cases.
  • addMagicNumber(ext: string, magicNumber: Array<number>): void - Adds a new magic number to the instance's magicNumbers map.
  • addMimeType(ext: string, mimeType: string): void - Adds a new MIME type to the instance's mimeTypeMap.
  • addFileType(ext: string, regex: RegExp): void - Adds a new file type to the instance's fileTypeMap.
  • addEdgeCase(specialExt: string, extList: Array<string>): void - Adds a new edge case to the instance's edgeCases map.

Mimetics.parse(buffer)

Takes a buffer as input, identifies the file type, mime type, and media type, and returns an object containing these three properties: ext, mime, media.

Parameters

  • buffer - Buffer - The input buffer to be parsed.

Returns

An object containing the determined file type, mime type, and media type.


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.