Parses, serializes, and manipulates MIME types, according to the WHATWG MIME Sniffing Standard
—
Low-level parsing and serialization functions providing direct access to the WHATWG MIME Sniffing Standard algorithms. These APIs are useful for building custom MIME type handling implementations.
Raw parsing function that converts MIME type strings into structured objects.
/**
* Parses a MIME type string into a structured object
* @param {string} input - MIME type string to parse
* @returns {Object|null} MIME type record object or null if parsing fails
* @returns {string} returns.type - The parsed type component
* @returns {string} returns.subtype - The parsed subtype component
* @returns {Map<string, string>} returns.parameters - Map of parameter name-value pairs
*/
function parse(input);Import:
const parse = require("whatwg-mimetype/lib/parser");Usage Examples:
const parse = require("whatwg-mimetype/lib/parser");
// Successful parsing
const result = parse("text/html;charset=utf-8");
console.log(result);
// {
// type: "text",
// subtype: "html",
// parameters: Map { "charset" => "utf-8" }
// }
// Access parsed components
console.log(result.type); // "text"
console.log(result.subtype); // "html"
console.log(result.parameters.get("charset")); // "utf-8"
// Failed parsing returns null
const invalid = parse("invalid mime type");
console.log(invalid); // null
// Complex parameter parsing
const complex = parse('application/json; charset="utf-8"; boundary=something');
console.log(complex.parameters.size); // 2
console.log(complex.parameters.get("charset")); // "utf-8"
console.log(complex.parameters.get("boundary")); // "something"Raw serialization function that converts MIME type record objects back to strings.
/**
* Serializes a MIME type record object to a string
* @param {Object} mimeType - MIME type record object to serialize
* @param {string} mimeType.type - The type component
* @param {string} mimeType.subtype - The subtype component
* @param {Map<string, string>} mimeType.parameters - Parameter name-value pairs
* @returns {string} Serialized MIME type string
*/
function serialize(mimeType);Import:
const serialize = require("whatwg-mimetype/lib/serializer");Usage Examples:
const serialize = require("whatwg-mimetype/lib/serializer");
// Serialize a MIME type record
const mimeType = {
type: "text",
subtype: "html",
parameters: new Map([
["charset", "utf-8"],
["boundary", "something"]
])
};
const serialized = serialize(mimeType);
console.log(serialized); // "text/html;charset=utf-8;boundary=something"
// Serialize with no parameters
const simple = {
type: "application",
subtype: "json",
parameters: new Map()
};
console.log(serialize(simple)); // "application/json"
// Parameters with special characters are quoted automatically
const special = {
type: "multipart",
subtype: "form-data",
parameters: new Map([
["boundary", "----boundary with spaces----"]
])
};
console.log(serialize(special));
// 'multipart/form-data;boundary="----boundary with spaces----"'The parse and serialize functions work together to provide low-level MIME type manipulation:
const parse = require("whatwg-mimetype/lib/parser");
const serialize = require("whatwg-mimetype/lib/serializer");
// Parse, modify, and serialize
const original = "text/html;charset=iso-8859-1";
const parsed = parse(original);
if (parsed) {
// Modify the parsed object
parsed.parameters.set("charset", "utf-8");
parsed.parameters.set("lang", "en");
// Serialize back to string
const modified = serialize(parsed);
console.log(modified); // "text/html;charset=utf-8;lang=en"
}
// Round-trip compatibility
const input = "APPLICATION/XML; charset=UTF-8";
const roundTrip = serialize(parse(input));
console.log(roundTrip); // "application/xml;charset=UTF-8"
// Note: type/subtype normalized to lowercase, parameter values preservedThe serialize function can also work with MIMEType class instances since they have the same interface:
const MIMEType = require("whatwg-mimetype");
const serialize = require("whatwg-mimetype/lib/serializer");
const mimeType = new MIMEType("text/html;charset=utf-8");
// serialize() works with both raw objects and MIMEType instances
const serialized = serialize(mimeType);
console.log(serialized); // "text/html;charset=utf-8"
// This is equivalent to calling toString() on the MIMEType instance
console.log(mimeType.toString()); // "text/html;charset=utf-8"
console.log(serialized === mimeType.toString()); // trueconst parse = require("whatwg-mimetype/lib/parser");
const serialize = require("whatwg-mimetype/lib/serializer");
function processContentType(contentTypeHeader) {
const parsed = parse(contentTypeHeader);
if (!parsed) {
return null; // Invalid MIME type
}
// Normalize charset parameter
if (parsed.parameters.has("charset")) {
const charset = parsed.parameters.get("charset").toLowerCase();
parsed.parameters.set("charset", charset);
}
// Add default charset for text types
if (parsed.type === "text" && !parsed.parameters.has("charset")) {
parsed.parameters.set("charset", "utf-8");
}
return serialize(parsed);
}
console.log(processContentType("text/html")); // "text/html;charset=utf-8"
console.log(processContentType("text/plain;charset=ISO-8859-1")); // "text/plain;charset=iso-8859-1"const parse = require("whatwg-mimetype/lib/parser");
function analyzeContentTypes(mimeTypeStrings) {
const results = {
valid: 0,
invalid: 0,
types: new Map()
};
for (const mimeTypeString of mimeTypeStrings) {
const parsed = parse(mimeTypeString);
if (parsed) {
results.valid++;
const count = results.types.get(parsed.type) || 0;
results.types.set(parsed.type, count + 1);
} else {
results.invalid++;
}
}
return results;
}
const analysis = analyzeContentTypes([
"text/html",
"application/json",
"invalid-mime-type",
"image/png",
"text/plain"
]);
console.log(analysis);
// {
// valid: 4,
// invalid: 1,
// types: Map { "text" => 2, "application" => 1, "image" => 1 }
// }The raw APIs provide optimal performance for scenarios requiring:
const parse = require("whatwg-mimetype/lib/parser");
// More efficient than creating MIMEType instances for validation only
function isValidMimeType(mimeTypeString) {
return parse(mimeTypeString) !== null;
}
// Efficient batch validation
function validateMimeTypes(mimeTypeStrings) {
return mimeTypeStrings.filter(isValidMimeType);
}Install with Tessl CLI
npx tessl i tessl/npm-whatwg-mimetype