Security News
tea.xyz Spam Plagues npm and RubyGems Package Registries
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.
negotiator
Advanced tools
Package description
The negotiator npm package is a content negotiation library used to parse and match HTTP headers for various content types. It allows servers to select the best representation of a resource based on the client's capabilities and preferences, as expressed through HTTP headers.
Content-Type Negotiation
This feature allows the server to determine the best media type to respond with, based on the client's 'Accept' HTTP header.
const Negotiator = require('negotiator');
const availableMediaTypes = ['text/html', 'application/json'];
const negotiator = new Negotiator(request);
const bestMediaType = negotiator.mediaType(availableMediaTypes);
Language Negotiation
This feature enables the server to select the best language for the response content based on the client's 'Accept-Language' HTTP header.
const Negotiator = require('negotiator');
const availableLanguages = ['en', 'es', 'fr'];
const negotiator = new Negotiator(request);
const bestLanguage = negotiator.language(availableLanguages);
Encoding Negotiation
This feature allows the server to choose the best encoding for the response based on the client's 'Accept-Encoding' HTTP header.
const Negotiator = require('negotiator');
const availableEncodings = ['gzip', 'deflate'];
const negotiator = new Negotiator(request);
const bestEncoding = negotiator.encoding(availableEncodings);
Charset Negotiation
This feature allows the server to determine the best charset for the response content based on the client's 'Accept-Charset' HTTP header.
const Negotiator = require('negotiator');
const availableCharsets = ['utf-8', 'iso-8859-1'];
const negotiator = new Negotiator(request);
const bestCharset = negotiator.charset(availableCharsets);
The 'accepts' npm package is similar to 'negotiator' in that it also provides content negotiation capabilities. It is built on top of 'negotiator' and provides a higher-level API for handling 'Accept', 'Accept-Encoding', 'Accept-Language', and 'Accept-Charset' headers.
The 'content-negotiation' package is another alternative that offers similar functionality for negotiating HTTP content. It differs in API design and may have different performance characteristics or additional features compared to 'negotiator'.
Readme
An HTTP content negotiator for Node.js
$ npm install negotiator
var Negotiator = require('negotiator')
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
.
Returns the most preferred media type from the client.
Returns the most preferred media type from a list of available media types.
Returns an array of preferred media types ordered by the client preference.
Returns an array of preferred media types ordered by priority from a list of available media types.
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
.
Returns the most preferred language from the client.
Returns the most preferred language from a list of available languages.
Returns an array of preferred languages ordered by the client preference.
Returns an array of preferred languages ordered by priority from a list of available languages.
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
.
Returns the most preferred charset from the client.
Returns the most preferred charset from a list of available charsets.
Returns an array of preferred charsets ordered by the client preference.
Returns an array of preferred charsets ordered by priority from a list of available charsets.
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
.
Returns the most preferred encoding from the client.
Returns the most preferred encoding from a list of available encodings.
Returns an array of preferred encodings ordered by the client preference.
Returns an array of preferred encodings ordered by priority from a list of available encodings.
The accepts module builds on this module and provides an alternative interface, mime type validation, and more.
FAQs
HTTP content negotiation
The npm package negotiator receives a total of 26,000,808 weekly downloads. As such, negotiator popularity was classified as popular.
We found that negotiator demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 2 open source maintainers 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
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.
Security News
UnitedHealth Group disclosed that the ransomware attack on Change Healthcare compromised protected health information for millions in the U.S., with estimated costs to the company expected to reach $1 billion.