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
99.4
Supply Chain
99.5
Quality
81.7
Maintenance
100
Vulnerability
100
License
Module System
Min. Node Version
Typescript Support
Node Version
NPM Version
82 Stars
34 Commits
18 Forks
8 Watching
1 Branches
8 Contributors
Updated on 13 Sept 2024
Minified
Minified + Gzipped
JavaScript (100%)
Cumulative downloads
Total Downloads
Last day
-8.2%
5,330,793
Compared to previous day
Last week
2.4%
31,666,546
Compared to previous week
Last month
19.5%
125,494,583
Compared to previous month
Last year
17.6%
1,132,732,297
Compared to previous year
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
Found 1/27 approved changesets -- score normalized to 0
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
detected GitHub workflow tokens with excessive permissions
Details
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 2024-11-18
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 More