@ende/media-types

ende add jsr:@ende/media-types

Version 2026.10.15 on JSR.

Utility functions for media types (MIME types).

This API is inspired by the GoLang mime package and jshttp/mime-types, and is designed to integrate and improve the APIs from x/media_types.

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

import { contentType, allExtensions, getCharset } from "@ende/media-types";
import { assertEquals } from "@ende/assert";

assertEquals(allExtensions("application/json"), ["json", "map"]);

assertEquals(contentType(".json"), "application/json; charset=UTF-8");

assertEquals(getCharset("text/plain"), "UTF-8");

Modules

Exports

  • allExtensions — Returns all the extensions known to be associated with the media type `type`, or `undefined` if no extensions are found.
  • contentType — Returns the full `Content-Type` or `Content-Disposition` header value for the given extension or media type.
  • ContentTypeToExtension — Maps content types to their corresponding file extensions.
  • DB — MIME-types database.
  • extension — Returns the most relevant extension for the given media type, or `undefined` if no extension can be found.
  • 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`.
  • KnownExtensionOrType — Known extension or type. Used in contentType.
  • parseMediaType — Parses the media type and any optional parameters, per RFC 1521.
  • typeByExtension — Returns the media type associated with the file extension, or `undefined` if no media type is found.