Skip to content

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

License

Notifications You must be signed in to change notification settings

jsdom/whatwg-mimetype

Repository files navigation

Parse, serialize, and manipulate MIME types

This package will parse MIME types into a structured format, which can then be manipulated and serialized:

const MIMEType = require("content-type-parser");

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

mimeType.parameters.set("charset", "windows-1252");
console.assert(mimeType.parameters.get("charset") === "windows-1252");
console.assert(mimeType.toString() === "text/html;charset=windows-1252");

console.assert(mimeType.isHTML() === true);
console.assert(mimeType.isXML() === false);

Parsing is a fairly complex process; see the specification for details (and similarly for serialization).

If the passed string cannot be parsed as a MIME type, the MIMEType constructor will throw.

This package's algorithms conform to those of the WHATWG MIME Sniffing Standard, and is aligned up to commit cc81ec4.

MIMEType API

This package's main module's default export is a class, MIMEType. Its constructor takes a string which it will attempt to parse into a MIME type; if parsing fails, an Error will be thrown.

Properties

  • type: the MIME type's type, e.g. "text"
  • subtype: the MIME type's subtype, e.g. "html"
  • essence: the MIME type's essence, e.g. "text/html"
  • parameters: an instance of MIMETypeParameters, containing this MIME type's parameters

type and subtype can be changed. They will be validated to be non-empty and only contain HTTP token code points.

essence is only a getter, and cannot be changed.

parameters is also a getter, but the contents of the MIMETypeParameters object are mutable, as described below.

Methods

  • toString() serializes the MIME type to a string
  • isHTML(): returns true if this instance represents a HTML MIME type
  • isXML(): returns true if this instance represents an XML MIME type

Note: the isHTML() and isXML() methods are speculative, and may be removed or changed in future major versions. See whatwg/mimesniff#48 for brainstorming in this area. Currently we implement these mainly because they are useful in jsdom.

MIMETypeParameters API

The MIMETypeParameters class, instances of which are returned by mimeType.parameters, has equivalent surface API to a JavaScript Map.

However, MIMETypeParameters methods will always interpret their arguments as appropriate for MIME types, so e.g. parameter names will be lowercased, and attempting to set invalid characters will throw.

Some examples:

const mimeType = new MIMEType(`x/x;a=b;c=D;E="F"`);

// Logs:
// a b
// c D
// e F
for (const [name, value] of mimeType.parameters) {
  console.log(name, value);
}

console.assert(mimeType.parameters.has("a"));
console.assert(mimeType.parameters.has("A"));
console.assert(mimeType.parameters.get("A") === "b");

mimeType.parameters.set("Q", "X");
console.assert(mimeType.parameters.get("q") === "X");
console.assert(mimeType.toString() === "x/x;a=b;c=d;e=F;q=X");

// Throws:
mimeType.parameters.set("@", "x");

Raw parsing/serialization APIs

If you want primitives on which to build your own API, you can get direct access to the parsing and serialization algorithms as follows:

const parse = require("content-type-parser/parser");
const serialize = require("content-type-parser/serialize");

parse(string) returns an object containing the type and subtype strings, plus parameters, which is a Map. This is roughly our equivalent of the spec's MIME type record. If parsing fails, it instead returns null.

serialize(record) operates on the such an object, giving back a string according to the serialization algorithm.

About

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

Resources

License

Security policy

Stars

Watchers

Forks

Contributors 4

  •  
  •  
  •  
  •