Deno standard library
GitHub Stars
Go to latest
import * as mod from "";

Provides utility functions for media types.

This API is inspired by the GoLang mime package and jshttp/mime-types.



A map of extensions for a given media type.


A map of the media type for a given extension



Given an extension or media type, return a full Content-Type or Content-Disposition header value.


For a given media type, return the most relevant extension, or undefined if no extension can be found.


Returns the extensions known to be associated with the media type type. The returned extensions will each begin with a leading dot, as in .html.


Serializes the media type and the optional parameters as a media type conforming to RFC 2045 and RFC 2616.


Given a media type or header value, identify the encoding charset. If the charset cannot be determined, the function returns undefined.


Parses the media type and any optional parameters, per RFC 1521. Media types are the values in Content-Type and Content-Disposition headers. On success the function returns a tuple where the first element is the media type and the second element is the optional parameters or undefined if there are none.


Returns the media type associated with the file extension. Values are normalized to lower case and matched irrespective of a leading ..


Provides an API for handling media (MIME) types.

These APIs are inspired by the GoLang mime package and jshttp/mime-types, and is designed to integrate and improve the APIs from

The vendor folder contains copy of the jshttp/mime-db db.json file along with its license.


Given a extension or media type, return a fully qualified header value for setting a Content-Type or Content-Disposition header. The function will process the value passed as a media type if it contains a /, otherwise will attempt to match as an extension, with or without the leading ..

Note: a side effect of deno/x/media_types was that you could pass a file name (e.g. file.json) and it would return the content type. This behavior is intentionally not supported here. If you want to get an extension for a file name, use extname() from std/path/mod.ts to determine the extension and pass it here.

import { contentType } from "";

contentType(".json"); // `application/json; charset=UTF-8`
contentType("text/html"); // `text/html; charset=UTF-8`
contentType("txt"); // `text/plain; charset=UTF-8`
contentType("foo"); // undefined
contentType("file.json"); // undefined


Given a media type, return the most relevant extension. If no extension can be determined undefined is returned.

import { extension } from "";

extension("text/plain"); // `txt`
extension("application/json"); // `json`
extension("text/html; charset=UTF-8"); // `html`
extension("application/foo"); // undefined


Given a media type, return an array of extensions that can be applied. If no extension can be determined undefined is returned.

import { extensionsByType } from "";

extensionsByType("application/json"); // ["js", "mjs"]
extensionsByType("text/html; charset=UTF-8"); // ["html", "htm", "shtml"]
extensionsByType("application/foo"); // undefined


Given a media type and optional parameters, return a spec compliant value. If the parameters result in a non-compliant value, an empty string ("") is returned.

import { formatMediaType } from "";

formatMediaType("text/plain", { charset: "UTF-8" }); // `text/plain; charset=UTF-8`


Given a media type, return the charset encoding for the value.

import { getCharset } from "";

getCharset("text/plain"); // `UTF-8`
getCharset("application/foo"); // undefined
getCharset("application/news-checkgroups"); // `US-ASCII`
getCharset("application/news-checkgroups; charset=UTF-8"); // `UTF-8`


Given a header value string, parse a value into a media type and any optional parameters. If the supplied value is invalid, the function will throw.

import { parseMediaType } from "";
import { assertEquals } from "";


  parseMediaType("text/html; charset=UTF-8"),
    { charset: "UTF-8" },


Given an extension, return a media type. The extension can have a leading . or not.

import { typeByExtension } from "";

typeByExtension("js"); // `application/json`
typeByExtension(".HTML"); // `text/html`
typeByExtension("foo"); // undefined
typeByExtension("file.json"); // undefined