Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

contenttype

Package Overview
Dependencies
Maintainers
1
Versions
3
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

contenttype

Content-Type, Accept, and media/MIME-type parsing

  • 1.0.2
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
0
decreased by-100%
Maintainers
1
Weekly downloads
 
Created
Source

Content-Type parsing

new MediaType(type, [parameters])

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"';

parseMedia(type)

Returns a new instance of MediaType.

splitQuotedString(str, delimiter, quote)

Splits a string by a delimiter character (default: semicolon), ignoring quoted sections (default: double quote).

splitContentTypes(str)

Splits an Accept (or similar) header into an Array of strings of content-types.

splitContentType('application/json, text/html').map(parseMedia)

select(reps, accept)

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.

mediaCmp(a, b)

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

Keywords

FAQs

Package last updated on 20 Jul 2015

Did you know?

Socket

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.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc