std@0.144.0/media_types

Deno standard library
GitHub Stars
2196
Go to latest
import * as mod from "https://deno.land/std@0.144.0/media_types/mod.ts";

Provides utility functions for media types.

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

Variables

extensions

A map of extensions for a given media type.

types

A map of the media type for a given extension

Functions

contentType

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

extension

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

extensionsByType

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.

formatMediaType

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

getCharset

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

parseMediaType

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.

typeByExtension

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

std/media_types

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 deno.land/x/media_types.

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

contentType()

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 "https://deno.land/std@0.144.0/media_types/mod.ts";

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

extension()

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

import { extension } from "https://deno.land/std@0.144.0/media_types/mod.ts";

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

extensionsByType()

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 "https://deno.land/std@0.144.0/media_types/mod.ts";

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

formatMediaType()

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 "https://deno.land/std@0.144.0/media_types/mod.ts";

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

getCharset()

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

import { getCharset } from "https://deno.land/std@0.144.0/media_types/mod.ts";

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

parseMediaType()

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 "https://deno.land/std@0.144.0/media_types/mod.ts";
import { assertEquals } from "https://deno.land/std@0.144.0/testing/asserts.ts";

assertEquals(
  parseMediaType("application/JSON"),
  [
    "application/json",
    undefined,
  ],
);

assertEquals(
  parseMediaType("text/html; charset=UTF-8"),
  [
    "application/json",
    { charset: "UTF-8" },
  ],
);

typeByExtension()

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

import { typeByExtension } from "https://deno.land/std@0.144.0/media_types/mod.ts";

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