Socket
Socket
Sign inDemoInstall

content-type

Package Overview
Dependencies
0
Maintainers
1
Versions
7
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    content-type

Javascript/ECMAScript library for parsing Content-Type and Media/MIME type strings


Version published
Weekly downloads
30M
decreased by-1.35%
Maintainers
1
Install size
11.0 kB
Created
Weekly downloads
 

Package description

What is content-type?

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.

What are content-type's main functionalities?

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' } });

Other packages similar to content-type

Readme

Source

Content-Type parsing

note: forked from here

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

Last updated on 08 Oct 2013

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.

Install

Related posts

SocketSocket SOC 2 Logo

Product

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

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc