Security News
JSR Working Group Kicks Off with Ambitious Roadmap and Plans for Open Governance
At its inaugural meeting, the JSR Working Group outlined plans for an open governance model and a roadmap to enhance JavaScript package management.
content-type
Advanced tools
Javascript/ECMAScript library for parsing Content-Type and Media/MIME type strings
The content-type npm package is used to create and parse HTTP Content-Type header according to RFC 7231. It allows you to parse content-type strings to objects and format content-type objects to strings.
Parsing Content-Type
This feature allows you to parse a Content-Type string, returning an object with the media type and parameters like charset.
const contentType = require('content-type');
const parsed = contentType.parse('text/html; charset=utf-8');
Formatting Content-Type
This feature enables you to format an object representing a Content-Type into a string that can be used in HTTP headers.
const contentType = require('content-type');
const format = contentType.format({ type: 'text/html', parameters: { charset: 'utf-8' } });
The mime-types package is similar to content-type as it provides the ability to look up the content-type based on a file extension and vice versa. It is more focused on the association between file extensions and mime types rather than parsing and formatting Content-Type headers.
The mime package is another alternative that allows you to map between MIME types and file extensions. It provides functionality to define custom mime type mappings and is more comprehensive in terms of the number of file types it supports compared to content-type.
Negotiator is an HTTP content negotiation library that goes beyond just handling Content-Type headers. It can be used to negotiate language, charset, encoding, etc., based on the Accept-* headers from the client. It is more complex and offers broader functionality for handling HTTP negotiations.
note: forked from here
The MediaType represents a parsed Media Type. For use in HTTP, the first (but only the first) q
parameter will be parsed as a float.
Other parameters are available through the params
object.
The first argument is the full media type, the second argument, if provided, is strictly a list of parameters.
The toString
method converts the object back into a Media type.
var p = new MediaType('text/html;level=1;q=0.5');
p.q === 0.5;
p.params.level === "1"
var q = new MediaType('application/json', {profile: 'http://example.com/schema.json'});
q.type === "application/json";
q.params.profile === "http://example.com/schema.json";
q.q = 1;
q.toString() === 'application/json;q=1;profile="http://example.com/schema.json"';
Returns a new instance of MediaType.
Splits a string by a delimiter character (default: semicolon), ignoring quoted sections (default: double quote).
Splits an Accept (or similar) header into an Array of strings of content-types.
splitContentType('application/json, text/html').map(parseMedia)
Pick an ideal representation to send, given an Array of representations to choose from, and the client-preferred list as an Array.
See example.js for an example.
Accepts two MediaType instances and tests them for being a subset/superset.
If a is a superset of b (b is smaller than a), return 1. If b is a superset of a, return -1. If they are the exact same, return 0. If they are disjoint, return null.
The q-value, if any, is ignored.
mediaCmp(parseMedia('text/html'), parseMedia('text/html')) === 0
mediaCmp(parseMedia('*/*'), parseMedia('text/html')) === 1
mediaCmp(parseMedia('text/html;level=1'), parseMedia('text/html')) === -1
mediaCmp(parseMedia('application/json;profile="v1.json"'), parseMedia('application/json;profile="v2.json"')) === null
FAQs
Create and parse HTTP Content-Type header
The npm package content-type receives a total of 24,707,621 weekly downloads. As such, content-type popularity was classified as popular.
We found that content-type 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.
Security News
At its inaugural meeting, the JSR Working Group outlined plans for an open governance model and a roadmap to enhance JavaScript package management.
Security News
Research
An advanced npm supply chain attack is leveraging Ethereum smart contracts for decentralized, persistent malware control, evading traditional defenses.
Security News
Research
Attackers are impersonating Sindre Sorhus on npm with a fake 'chalk-node' package containing a malicious backdoor to compromise developers' projects.