Parses, serializes, and manipulates MIME types, according to the WHATWG MIME Sniffing Standard
npx @tessl/cli install tessl/npm-whatwg-mimetype@4.0.0WHATWG MIME Type is a JavaScript library that parses, serializes, and manipulates MIME types according to the WHATWG MIME Sniffing Standard specification. It provides a complete object-oriented API for working with MIME type strings, with proper validation, parameter handling, and utility methods for detecting common MIME types.
npm install whatwg-mimetypeconst MIMEType = require("whatwg-mimetype");For raw parsing/serialization APIs:
const parse = require("whatwg-mimetype/lib/parser");
const serialize = require("whatwg-mimetype/lib/serializer");const MIMEType = require("whatwg-mimetype");
// Parse a MIME type string
const mimeType = new MIMEType(`Text/HTML;Charset="utf-8"`);
console.assert(mimeType.toString() === "text/html;charset=utf-8");
console.assert(mimeType.type === "text");
console.assert(mimeType.subtype === "html");
console.assert(mimeType.essence === "text/html");
console.assert(mimeType.parameters.get("charset") === "utf-8");
// Modify parameters
mimeType.parameters.set("charset", "windows-1252");
console.assert(mimeType.parameters.get("charset") === "windows-1252");
console.assert(mimeType.toString() === "text/html;charset=windows-1252");
// Check MIME type categories
console.assert(mimeType.isHTML() === true);
console.assert(mimeType.isXML() === false);WHATWG MIME Type is built around several key components:
Core MIME type functionality for parsing strings into structured objects and manipulating their components. Ideal for HTTP header processing, content-type analysis, and web standards implementations.
class MIMEType {
constructor(string: string);
static parse(string: string): MIMEType | null;
}Map-like interface for accessing and modifying MIME type parameters with automatic validation and case-insensitive handling. Perfect for managing charset, boundary, and other MIME type parameters.
class MIMETypeParameters {
get(name: string): string | undefined;
set(name: string, value: string): Map;
has(name: string): boolean;
delete(name: string): boolean;
clear(): void;
}Low-level parsing and serialization functions for building custom MIME type handling implementations. Provides direct access to the WHATWG parsing algorithms.
function parse(input: string): {
type: string;
subtype: string;
parameters: Map<string, string>;
} | null;
function serialize(mimeType: {
type: string;
subtype: string;
parameters: Map<string, string>;
}): string;