or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

index.mdmime-type-core.mdparameter-management.mdraw-apis.md
tile.json

tessl/npm-whatwg-mimetype

Parses, serializes, and manipulates MIME types, according to the WHATWG MIME Sniffing Standard

Workspace
tessl
Visibility
Public
Created
Last updated
Describes
npmpkg:npm/whatwg-mimetype@4.0.x

To install, run

npx @tessl/cli install tessl/npm-whatwg-mimetype@4.0.0

index.mddocs/

WHATWG MIME Type

WHATWG 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.

Package Information

  • Package Name: whatwg-mimetype
  • Package Type: npm
  • Language: JavaScript
  • Installation: npm install whatwg-mimetype

Core Imports

const MIMEType = require("whatwg-mimetype");

For raw parsing/serialization APIs:

const parse = require("whatwg-mimetype/lib/parser");
const serialize = require("whatwg-mimetype/lib/serializer");

Basic Usage

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);

Architecture

WHATWG MIME Type is built around several key components:

  • MIMEType Class: Primary interface for parsing and manipulating MIME types with full WHATWG spec compliance
  • Parameter Management: Map-like interface for handling MIME type parameters with automatic validation
  • Type Detection: Built-in methods for identifying HTML, XML, and JavaScript MIME types
  • Raw Parsing/Serialization: Low-level APIs for custom implementations
  • Validation Engine: Comprehensive validation of type names, parameter names, and values

Capabilities

MIME Type Parsing and Manipulation

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;
}

MIME Type Core

Parameter Management

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;
}

Parameter Management

Raw Parsing and Serialization

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;

Raw APIs