Skip to main content
On this page

@std/media-types

Overview Jump to heading

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 "@std/media-types";
import { assertEquals } from "@std/assert";

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

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

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

Add to your project Jump to heading

deno add jsr:@std/media-types

See all symbols in @std/media-types on

What are media types? Jump to heading

Media types, also known as MIME types, are standardized identifiers used to indicate the nature and format of a file or piece of data. They are commonly used in HTTP headers to specify the type of content being sent or received.

Why use @std/media-types? Jump to heading

You may need to determine the correct MIME type for a file based on its extension, or vice versa, when handling file uploads, downloads, or serving web content. This package provides utility functions to easily map between extensions and MIME types and obtain charset hints.

Examples Jump to heading

Quick lookups Jump to heading

import { allExtensions, extension, typeByExtension } from "@std/media-types";

console.log(typeByExtension(".png")); // "image/png"
console.log(extension("text/html")); // "html"
console.log(allExtensions("application/json")); // ["json", "map"]

Build headers for responses Jump to heading

import { contentType } from "@std/media-types";

// Produces a full header value with charset when appropriate
const ct = contentType(".css"); // "text/css; charset=UTF-8"

return new Response("body", {
  headers: {
    "Content-Type": ct ?? "application/octet-stream",
  },
});

Parse and normalize a Content-Type header Jump to heading

import { formatMediaType, parseMediaType } from "@std/media-types";

const header = "text/HTML; charset=UTF-8";
const [type, params] = parseMediaType(header)!; // ["text/html", { charset: "UTF-8" }]

// Re-serialize with normalized type and params (lowercased keys)
const normalized = formatMediaType(type, params);
// "text/html; charset=UTF-8"

Extract multipart/form-data boundary from a request Jump to heading

import { parseMediaType } from "@std/media-types";

function getBoundary(headers: Headers): string | undefined {
  const value = headers.get("content-type");
  if (!value) return undefined;
  const parsed = parseMediaType(value);
  return parsed?.[1]?.boundary;
}

Detect charset from an incoming request Jump to heading

import { getCharset } from "@std/media-types";

async function readText(req: Request): Promise<string> {
  const charset = getCharset(req.headers.get("content-type") ?? "") ?? "UTF-8";
  const bytes = new Uint8Array(await req.arrayBuffer());
  const decoder = new TextDecoder(charset);
  return decoder.decode(bytes);
}

Tips Jump to heading

  • Prefer contentType(extOrType) when constructing HTTP responses.
  • Use allExtensions(type) to support multiple possible extensions.
  • parseMediaType throws on invalid input; wrap in try/catch if parsing user input.
  • When contentType() returns undefined for an unknown extension/type, fall back to application/octet-stream.

Did you find what you needed?

Privacy policy