Product
Introducing SSO
Streamline your login process and enhance security by enabling Single Sign-On (SSO) on the Socket platform, now available for all customers on the Enterprise plan, supporting 20+ identity providers.
content-type-parser
Advanced tools
Readme
Content-Type
Header StringsThis package will parse the Content-Type
header field into an introspectable data structure, whose parameters can be manipulated:
const contentTypeParser = require("content-type-parser");
const contentType = contentTypeParser(`Text/HTML;Charset="utf-8"`);
console.assert(contentType.toString() === "text/html;charset=utf-8");
console.assert(contentType.type === "text");
console.assert(contentType.subtype === "html");
console.assert(contentType.get("charset") === "utf-8");
contentType.set("charset", "windows-1252");
console.assert(contentType.get("charset") === "windows-1252");
console.assert(contentType.toString() === "text/html;charset=windows-1252");
console.assert(contentType.isHTML() === true);
console.assert(contentType.isXML() === false);
console.assert(contentType.isText() === true);
Note how parsing will lowercase the type, subtype, and parameter name tokens (but not parameter values).
If the passed string cannot be parsed as a content-type, contentTypeParser
will return null
.
ContentType
instance APIThis package's main module's default export will return an instance of the ContentType
class, which has the following public APIs:
type
: the top-level media type, e.g. "text"
subtype
: the subtype, e.g. "html"
parameterList
: an array of { separator, key, value }
pairs representing the parameters. The separator
field contains any whitespace, not just the ;
character.In general you should not directly manipulate parameterList
. Instead, use the following APIs:
get("key")
: returns the value of the parameter with the given key, or undefined
if no such parameter is presentset("key", "value")
: adds the given key/value pair to the parameter list, or overwrites the existing value if an entry already existedBoth of these will lowercase the keys.
isHTML()
: returns true if this instance's MIME type is the HTML MIME type, "text/html"
isXML()
: returns true if this instance's MIME type is an XML MIME typeisText()
: returns true if this instance's top-level media type is "text"
toString()
will return a canonicalized representation of the content-type, re-built from the parsed componentsThis package was originally based on the excellent work of @nicolashenry, in jsdom. It has since been pulled out into this separate package.
FAQs
Parse the value of the Content-Type header
The npm package content-type-parser receives a total of 240,806 weekly downloads. As such, content-type-parser popularity was classified as popular.
We found that content-type-parser demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer 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
Streamline your login process and enhance security by enabling Single Sign-On (SSO) on the Socket platform, now available for all customers on the Enterprise plan, supporting 20+ identity providers.
Security News
Tea.xyz, a crypto project aimed at rewarding open source contributions, is once again facing backlash due to an influx of spam packages flooding public package registries.
Security News
As cyber threats become more autonomous, AI-powered defenses are crucial for businesses to stay ahead of attackers who can exploit software vulnerabilities at scale.