Parses, serializes, and manipulates MIME types, according to the WHATWG MIME Sniffing Standard
npx @tessl/cli install tessl/npm-whatwg-mimetype@4.0.00
# WHATWG MIME Type
1
2
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.
3
4
## Package Information
5
6
- **Package Name**: whatwg-mimetype
7
- **Package Type**: npm
8
- **Language**: JavaScript
9
- **Installation**: `npm install whatwg-mimetype`
10
11
## Core Imports
12
13
```javascript
14
const MIMEType = require("whatwg-mimetype");
15
```
16
17
For raw parsing/serialization APIs:
18
19
```javascript
20
const parse = require("whatwg-mimetype/lib/parser");
21
const serialize = require("whatwg-mimetype/lib/serializer");
22
```
23
24
## Basic Usage
25
26
```javascript
27
const MIMEType = require("whatwg-mimetype");
28
29
// Parse a MIME type string
30
const mimeType = new MIMEType(`Text/HTML;Charset="utf-8"`);
31
32
console.assert(mimeType.toString() === "text/html;charset=utf-8");
33
console.assert(mimeType.type === "text");
34
console.assert(mimeType.subtype === "html");
35
console.assert(mimeType.essence === "text/html");
36
console.assert(mimeType.parameters.get("charset") === "utf-8");
37
38
// Modify parameters
39
mimeType.parameters.set("charset", "windows-1252");
40
console.assert(mimeType.parameters.get("charset") === "windows-1252");
41
console.assert(mimeType.toString() === "text/html;charset=windows-1252");
42
43
// Check MIME type categories
44
console.assert(mimeType.isHTML() === true);
45
console.assert(mimeType.isXML() === false);
46
```
47
48
## Architecture
49
50
WHATWG MIME Type is built around several key components:
51
52
- **MIMEType Class**: Primary interface for parsing and manipulating MIME types with full WHATWG spec compliance
53
- **Parameter Management**: Map-like interface for handling MIME type parameters with automatic validation
54
- **Type Detection**: Built-in methods for identifying HTML, XML, and JavaScript MIME types
55
- **Raw Parsing/Serialization**: Low-level APIs for custom implementations
56
- **Validation Engine**: Comprehensive validation of type names, parameter names, and values
57
58
## Capabilities
59
60
### MIME Type Parsing and Manipulation
61
62
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.
63
64
```javascript { .api }
65
class MIMEType {
66
constructor(string: string);
67
static parse(string: string): MIMEType | null;
68
}
69
```
70
71
[MIME Type Core](./mime-type-core.md)
72
73
### Parameter Management
74
75
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.
76
77
```javascript { .api }
78
class MIMETypeParameters {
79
get(name: string): string | undefined;
80
set(name: string, value: string): Map;
81
has(name: string): boolean;
82
delete(name: string): boolean;
83
clear(): void;
84
}
85
```
86
87
[Parameter Management](./parameter-management.md)
88
89
### Raw Parsing and Serialization
90
91
Low-level parsing and serialization functions for building custom MIME type handling implementations. Provides direct access to the WHATWG parsing algorithms.
92
93
```javascript { .api }
94
function parse(input: string): {
95
type: string;
96
subtype: string;
97
parameters: Map<string, string>;
98
} | null;
99
100
function serialize(mimeType: {
101
type: string;
102
subtype: string;
103
parameters: Map<string, string>;
104
}): string;
105
```
106
107
[Raw APIs](./raw-apis.md)