Gathering detailed insights and metrics for whatwg-mimetype
Gathering detailed insights and metrics for whatwg-mimetype
Gathering detailed insights and metrics for whatwg-mimetype
Gathering detailed insights and metrics for whatwg-mimetype
Parses, serializes, and manipulates MIME types, according to the WHATWG MIME Sniffing Standard
npm install whatwg-mimetype
Typescript
Module System
Min. Node Version
Node Version
NPM Version
JavaScript (100%)
Total Downloads
0
Last Day
0
Last Week
0
Last Month
0
Last Year
0
MIT License
87 Stars
35 Commits
19 Forks
7 Watchers
1 Branches
9 Contributors
Updated on May 30, 2025
Latest Version
4.0.0
Package Id
whatwg-mimetype@4.0.0
Unpacked Size
16.33 kB
Size
5.14 kB
File Count
8
NPM Version
10.2.0
Node Version
21.1.0
Published on
Nov 11, 2023
Cumulative downloads
Total Downloads
5
This package will parse MIME types into a structured format, which can then be manipulated and serialized:
1const MIMEType = require("whatwg-mimetype"); 2 3const mimeType = new MIMEType(`Text/HTML;Charset="utf-8"`); 4 5console.assert(mimeType.toString() === "text/html;charset=utf-8"); 6 7console.assert(mimeType.type === "text"); 8console.assert(mimeType.subtype === "html"); 9console.assert(mimeType.essence === "text/html"); 10console.assert(mimeType.parameters.get("charset") === "utf-8"); 11 12mimeType.parameters.set("charset", "windows-1252"); 13console.assert(mimeType.parameters.get("charset") === "windows-1252"); 14console.assert(mimeType.toString() === "text/html;charset=windows-1252"); 15 16console.assert(mimeType.isHTML() === true); 17console.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:
1const mimeType = new MIMEType(`x/x;a=b;c=D;E="F"`); 2 3// Logs: 4// a b 5// c D 6// e F 7for (const [name, value] of mimeType.parameters) { 8 console.log(name, value); 9} 10 11console.assert(mimeType.parameters.has("a")); 12console.assert(mimeType.parameters.has("A")); 13console.assert(mimeType.parameters.get("A") === "b"); 14 15mimeType.parameters.set("Q", "X"); 16console.assert(mimeType.parameters.get("q") === "X"); 17console.assert(mimeType.toString() === "x/x;a=b;c=d;e=F;q=X"); 18 19// Throws: 20mimeType.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:
1const parse = require("whatwg-mimetype/parser"); 2const 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.
No vulnerabilities found.
Reason
no dangerous workflow patterns detected
Reason
no binaries found in the repo
Reason
license file detected
Details
Reason
security policy file detected
Details
Reason
1 existing vulnerabilities detected
Details
Reason
dependency not pinned by hash detected -- score normalized to 3
Details
Reason
1 commit(s) and 1 issue activity found in the last 90 days -- score normalized to 1
Reason
detected GitHub workflow tokens with excessive permissions
Details
Reason
Found 2/27 approved changesets -- score normalized to 0
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
project is not fuzzed
Details
Reason
branch protection not enabled on development/release branches
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Score
Last Scanned on 2025-05-26
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn MoreLast Day
0%
NaN
Compared to previous day
Last Week
0%
NaN
Compared to previous week
Last Month
0%
NaN
Compared to previous month
Last Year
0%
NaN
Compared to previous year