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

@alshdavid/mime-types

v2.2.3

Published

The ultimate javascript content-type utility.

Downloads

1,551

Readme

mime-types

The ultimate javascript content-type utility, written entirely in TypeScript

Install

This package has no dependencies and uses no node.js specific APIs. This works in any JavaScript runtime, including Node.js and the Browser.

You can install this via NPM:

$ npm install @alshdavid/mime-types

Adding Types

All mime types are based on an object containing all the mime types (known as the mime-db), feel free to open a PR to expand the list of mime types.

API

This package offers imports for CommonJS, ES Modules. Node will automatically select the import type relevant for it. Bundlers like Parcel, Webpack, Rollup will likewise consume the package via ES Modules.

Importing

var mime = require('@alshdavid/mime-types')
import * as mime from '@alshdavid/mime-types'

Results

All functions return a tuple (JavaScript array)

type Result = [result: string, found: boolean]

e.g.

const [ result, found ] = mime.lookup('json')

Functions

mime.lookup(path)

Lookup the content-type associated with a file.

mime.lookup('json') // ['application/json', true]
mime.lookup('.md') // ['text/markdown', true]
mime.lookup('file.html') // ['text/html', true]
mime.lookup('folder/file.js') // ['application/javascript', true]
mime.lookup('folder/.htaccess') // ['', false]

mime.lookup('cats') // ['', false]

mime.contentType(type)

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.

mime.contentType('markdown') // ['text/x-markdown; charset=utf-8', true]
mime.contentType('file.json') // ['application/json; charset=utf-8', true]
mime.contentType('text/html') // ['text/html; charset=utf-8', true]
mime.contentType('text/html; charset=iso-8859-1') // ['text/html; charset=iso-8859-1', true]

// from a full path
mime.contentType(path.extname('/path/to/file.json')) // ['application/json; charset=utf-8', true]

mime.extension(type)

Get the default extension for a content-type.

mime.extension('application/octet-stream') // ['bin', true]

mime.charset(type)

Lookup the implied default charset of a content-type.

mime.charset('text/markdown') // ['UTF-8', true]

MimeTypesParser Class

If you wish to manually instantiate the mime-type parser, you can do so like this:

import { MimeTypesParser } from '@alshdavid/mime-types'

const mimeTypesParser = new MimeTypesParser()

// Methods are all identical to available functions
mimeTypesParser.lookup('file.html')
mimeTypesParser.contentType('text/html')
mimeTypesParser.extension('application/octet-stream')
mimeTypesParser.charset('text/markdown')

Expanding the database

import { MimeTypesParser, mimeDatabase, MimeDatabase } from '@alshdavid/mime-types'

const customMimeDatabase: MimeDatabase = {
    "application/my-custom-type": {}
}

const mimeTypesParser = new MimeTypesParser({
    database: { ...mimeDatabase, ...customMimeDatabase }
})

License

MIT