
Product
A New Overview in our Dashboard
We redesigned Socket's first logged-in page to display rich and insightful visualizations about your repositories protected against supply chain threats.
whatwg-mimetype
Advanced tools
Parses, serializes, and manipulates MIME types, according to the WHATWG MIME Sniffing Standard
The whatwg-mimetype npm package is a JavaScript implementation of the WHATWG MIME type standard. It allows for parsing, serializing, and manipulating MIME types according to the rules defined by the WHATWG specification.
Parsing MIME types
This feature allows for the parsing of MIME type strings to create MIMEType objects that can be easily manipulated and queried.
const MIMEType = require('whatwg-mimetype');
const mimeType = new MIMEType('text/html');
console.log(mimeType.toString()); // 'text/html'
Serializing MIME types
This feature enables the conversion of MIMEType objects back into their string representation.
const MIMEType = require('whatwg-mimetype');
const mimeType = new MIMEType('text/html');
console.log(mimeType.toString()); // 'text/html'
Extracting MIME type components
This feature allows for the extraction of the type, subtype, and parameters of a MIME type for further processing or inspection.
const MIMEType = require('whatwg-mimetype');
const mimeType = new MIMEType('text/html; charset=UTF-8');
console.log(mimeType.type); // 'text'
console.log(mimeType.subtype); // 'html'
console.log(mimeType.parameters.get('charset')); // 'UTF-8'
Checking for essence
This feature provides the essence of the MIME type, which is the combination of the type and subtype, excluding any parameters.
const MIMEType = require('whatwg-mimetype');
const mimeType = new MIMEType('text/html; charset=UTF-8');
console.log(mimeType.essence); // 'text/html'
The mime-types package is a simple utility for looking up MIME types based on file extensions and vice versa. It is a more lightweight option compared to whatwg-mimetype, focusing on MIME type mapping rather than parsing and manipulation.
The mime package is another popular library for MIME type mapping. It offers functionality to define custom MIME type mappings and provides lookup methods. Unlike whatwg-mimetype, it does not strictly adhere to the WHATWG MIME type standard for parsing and serialization.
The content-type package is designed to parse and format HTTP Content-Type header fields. It's similar to whatwg-mimetype in that it deals with MIME types, but it is specifically tailored for HTTP header parsing and does not provide general MIME type manipulation features.
This package will parse MIME types into a structured format, which can then be manipulated and serialized:
const MIMEType = require("whatwg-mimetype");
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).
This package's algorithms conform to those of the WHATWG MIME Sniffing Standard, and is aligned up to commit 8e9a7dd.
MIMEType
APIThis 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.
parse()
static factory methodAs an alternative to the constructor, you can use MIMEType.parse(string)
. The only difference is that parse()
will return null
on failed parsing, whereas the constructor will throw. It thus makes the most sense to use the constructor in cases where unparseable MIME types would be exceptional, and use parse()
when dealing with input from some unconstrained source.
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 parameterstype
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.
toString()
serializes the MIME type to a stringisHTML()
: returns true if this instance represents a HTML MIME typeisXML()
: returns true if this instance represents an XML MIME typeisJavaScript({ prohibitParameters })
: returns true if this instance represents a JavaScript MIME type. prohibitParameters
can be set to true to disallow any parameters, i.e. to test if the MIME type's serialization is a JavaScript MIME type essence match.Note: the isHTML()
, isXML()
, and isJavaScript()
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
APIThe 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");
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("whatwg-mimetype/parser");
const serialize = require("whatwg-mimetype/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.
FAQs
Parses, serializes, and manipulates MIME types, according to the WHATWG MIME Sniffing Standard
The npm package whatwg-mimetype receives a total of 26,842,499 weekly downloads. As such, whatwg-mimetype popularity was classified as popular.
We found that whatwg-mimetype demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 6 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Product
We redesigned Socket's first logged-in page to display rich and insightful visualizations about your repositories protected against supply chain threats.
Product
Automatically fix and test dependency updates with socket fix—a new CLI tool that turns CVE alerts into safe, automated upgrades.
Security News
CISA denies CVE funding issues amid backlash over a new CVE foundation formed by board members, raising concerns about transparency and program governance.