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

@whykhamist/mime-types

v1.0.2

Published

The ultimate javascript content-type utility.

Downloads

26

Readme

mime-types

NPM Version NPM Downloads

The ultimate javascript content-type utility.

Forked from mime-types

  • Written in typescript

  • Created MimeTypes Class

  • Changed function names

    | old | new | | ----------- | --------------------------------- | | lookup | getMime | | contentType | getContentType | | extension | getExtension | | charset | getCharset |

  • New Function

Adding Types

All mime types are based on mime-db, so open a PR there if you'd like to add mime types.

API

Getting Started

Installation

$ npm i @whykhamist/mime-types

or

$ yarn add @whykhamist/mime-types

CJS

const mime = require("@whykhamist/mime-types").default;
mime.getMime(...)

OR

// const MimeTypes = require("@whykhamist/mime-types").MimeTypes;
// const MimeDb = require("@whykhamist/mime-types").MimeDb;
const { MimeTypes, MimeDb } = require("@whykhamist/mime-types");
const mime = new MimeTypes(MimeDb);
mime.getMime(...)

OR

const { types, extensions, typeSets, getMime, getMimes, getContentType, getExtension, getCharset } = require("@whykhamist/mime-types");

ESM

import mime from "@whykhamist/mime-types";
mime.getMime(...)

OR

import { MimeTypes, MimeDb } from "@whykhamist/mime-types";
const mime = new MimeTypes(MimeDb);
mime.getMime(...)

OR

const { types, extensions, typeSets, getMime, getMimes, getContentType, getExtension, getCharset } from "@whykhamist/mime-types";

Others

import db from "mime-db/db.json";
import { MimeTypes } from "@whykhamist/mime-types";
import { MimeDatabase } from "mime-db";

const mime = new MimeTypes(db as MimeDatabase);

extname

By default, the class uses regex to extract the file extension from a path string. If you want to use other function for the extraction (ex. Node.js path.extname), then create a new MimeTypes instance and add the function as a second paramenter

import { extname } from "path";
const mime = new MimeTypes(db, extname);

Make sure that the function takes a string (path) and returns the file extension. Must return any falsy value if no extension could be identified.

Regex used

/^.+\.([^.]+)$/

Properties

types

A map of content-types by extension.

const type = types["mp3"];
// "audio/mpeg"

typeSets

A map of array of content types by extension.

const types = typeSets["mp3"];
// [ 'audio/mp3', 'audio/mpeg' ]

extensions

A map of extensions by content-type.

const exts = extensions["text/x-c"];
// [ "c", "cc", "cxx", "cpp", "h", "hh", "dic" ]

Methods

All functions return false if input is invalid or not found.

getMime

Lookup the content-type associated with a file.

getMime("json"); // 'application/json'
getMime(".md"); // 'text/markdown'
getMime("file.html"); // 'text/html'
getMime("folder/file.js"); // 'application/javascript'
getMime("folder/.htaccess"); // false

getMime("cats"); // false

getMimes

Find all content-types that are associated with a file.

getMimes("mp3"); // [ "audio/mpeg", "audio/mp3" ]
getMimes("path/to/file.rtf"); // [ "application/rtf", "text/rtf" ]
getMimes("c:\\path\\to\\file.bmp"); // [ "image/x-ms-bmp", "image/bmp", ]

getContentType

Create a full content-type header given a content-type or extension. When given an extension, mime.lookup is used to get the matching content-type, otherwise the given content-type is used. Then if the content-type does not already have a charset parameter, mime.charset is used to get the default charset and add to the returned content-type.

getContentType("markdown"); // 'text/x-markdown; charset=utf-8'
getContentType("file.json"); // 'application/json; charset=utf-8'
getContentType("text/html"); // 'text/html; charset=utf-8'
getContentType("text/html; charset=iso-8859-1"); // 'text/html; charset=iso-8859-1'

// from a full path
getContentType(path.extname("/path/to/file.json")); // 'application/json; charset=utf-8'

getExtension

Get the default extension for a content-type.

getExtension("application/octet-stream"); // 'bin'

getCharset

Lookup the implied default charset of a content-type.

getCharset("text/markdown"); // 'UTF-8'

License

MIT