Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

negotiator

jshttp202.8mMIT1.0.0TypeScript support: definitely-typed

HTTP content negotiation

http, content negotiation, accept, accept-language, accept-encoding, accept-charset

readme

negotiator

NPM Version NPM Downloads Node.js Version Build Status Test Coverage

An HTTP content negotiator for Node.js

Installation

$ npm install negotiator

API

var Negotiator = require('negotiator')

Accept Negotiation

availableMediaTypes = ['text/html', 'text/plain', 'application/json']

// The negotiator constructor receives a request object
negotiator = new Negotiator(request)

// Let's say Accept header is 'text/html, application/*;q=0.2, image/jpeg;q=0.8'

negotiator.mediaTypes()
// -> ['text/html', 'image/jpeg', 'application/*']

negotiator.mediaTypes(availableMediaTypes)
// -> ['text/html', 'application/json']

negotiator.mediaType(availableMediaTypes)
// -> 'text/html'

You can check a working example at examples/accept.js.

Methods

mediaType()

Returns the most preferred media type from the client.

mediaType(availableMediaType)

Returns the most preferred media type from a list of available media types.

mediaTypes()

Returns an array of preferred media types ordered by the client preference.

mediaTypes(availableMediaTypes)

Returns an array of preferred media types ordered by priority from a list of available media types.

Accept-Language Negotiation

negotiator = new Negotiator(request)

availableLanguages = ['en', 'es', 'fr']

// Let's say Accept-Language header is 'en;q=0.8, es, pt'

negotiator.languages()
// -> ['es', 'pt', 'en']

negotiator.languages(availableLanguages)
// -> ['es', 'en']

language = negotiator.language(availableLanguages)
// -> 'es'

You can check a working example at examples/language.js.

Methods

language()

Returns the most preferred language from the client.

language(availableLanguages)

Returns the most preferred language from a list of available languages.

languages()

Returns an array of preferred languages ordered by the client preference.

languages(availableLanguages)

Returns an array of preferred languages ordered by priority from a list of available languages.

Accept-Charset Negotiation

availableCharsets = ['utf-8', 'iso-8859-1', 'iso-8859-5']

negotiator = new Negotiator(request)

// Let's say Accept-Charset header is 'utf-8, iso-8859-1;q=0.8, utf-7;q=0.2'

negotiator.charsets()
// -> ['utf-8', 'iso-8859-1', 'utf-7']

negotiator.charsets(availableCharsets)
// -> ['utf-8', 'iso-8859-1']

negotiator.charset(availableCharsets)
// -> 'utf-8'

You can check a working example at examples/charset.js.

Methods

charset()

Returns the most preferred charset from the client.

charset(availableCharsets)

Returns the most preferred charset from a list of available charsets.

charsets()

Returns an array of preferred charsets ordered by the client preference.

charsets(availableCharsets)

Returns an array of preferred charsets ordered by priority from a list of available charsets.

Accept-Encoding Negotiation

availableEncodings = ['identity', 'gzip']

negotiator = new Negotiator(request)

// Let's say Accept-Encoding header is 'gzip, compress;q=0.2, identity;q=0.5'

negotiator.encodings()
// -> ['gzip', 'identity', 'compress']

negotiator.encodings(availableEncodings)
// -> ['gzip', 'identity']

negotiator.encoding(availableEncodings)
// -> 'gzip'

You can check a working example at examples/encoding.js.

Methods

encoding()

Returns the most preferred encoding from the client.

encoding(availableEncodings)

Returns the most preferred encoding from a list of available encodings.

encoding(availableEncodings, { preferred })

Returns the most preferred encoding from a list of available encodings, while prioritizing based on preferred array between same-quality encodings.

encodings()

Returns an array of preferred encodings ordered by the client preference.

encodings(availableEncodings)

Returns an array of preferred encodings ordered by priority from a list of available encodings.

encodings(availableEncodings, { preferred })

Returns an array of preferred encodings ordered by priority from a list of available encodings, while prioritizing based on preferred array between same-quality encodings.

See Also

The accepts module builds on this module and provides an alternative interface, mime type validation, and more.

License

MIT

changelog

1.0.0 / 2024-08-31

  • Drop support for node <18
  • Added an option preferred encodings array #59

0.6.3 / 2022-01-22

  • Revert "Lazy-load modules from main entry point"

0.6.2 / 2019-04-29

  • Fix sorting charset, encoding, and language with extra parameters

0.6.1 / 2016-05-02

  • perf: improve Accept parsing speed
  • perf: improve Accept-Charset parsing speed
  • perf: improve Accept-Encoding parsing speed
  • perf: improve Accept-Language parsing speed

0.6.0 / 2015-09-29

  • Fix including type extensions in parameters in Accept parsing
  • Fix parsing Accept parameters with quoted equals
  • Fix parsing Accept parameters with quoted semicolons
  • Lazy-load modules from main entry point
  • perf: delay type concatenation until needed
  • perf: enable strict mode
  • perf: hoist regular expressions
  • perf: remove closures getting spec properties
  • perf: remove a closure from media type parsing
  • perf: remove property delete from media type parsing

0.5.3 / 2015-05-10

  • Fix media type parameter matching to be case-insensitive

0.5.2 / 2015-05-06

  • Fix comparing media types with quoted values
  • Fix splitting media types with quoted commas

0.5.1 / 2015-02-14

  • Fix preference sorting to be stable for long acceptable lists

0.5.0 / 2014-12-18

  • Fix list return order when large accepted list
  • Fix missing identity encoding when q=0 exists
  • Remove dynamic building of Negotiator class

0.4.9 / 2014-10-14

  • Fix error when media type has invalid parameter

0.4.8 / 2014-09-28

  • Fix all negotiations to be case-insensitive
  • Stable sort preferences of same quality according to client order
  • Support Node.js 0.6

0.4.7 / 2014-06-24

  • Handle invalid provided languages
  • Handle invalid provided media types

0.4.6 / 2014-06-11

  • Order by specificity when quality is the same

0.4.5 / 2014-05-29

  • Fix regression in empty header handling

0.4.4 / 2014-05-29

  • Fix behaviors when headers are not present

0.4.3 / 2014-04-16

  • Handle slashes on media params correctly

0.4.2 / 2014-02-28

  • Fix media type sorting
  • Handle media types params strictly

0.4.1 / 2014-01-16

  • Use most specific matches

0.4.0 / 2014-01-09

  • Remove preferred prefix from methods