npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

@prexjs/negotiator

v1.0.0

Published

HTTP content negotiator for Node.js to determine preferred media types, encodings, languages, and charsets.

Readme

@prexjs/negotiator

HTTP content negotiator for Node.js to determine preferred media types, encodings, languages, and charsets.

npm version npm downloads License: MIT TypeScript

@prexjs/negotiator is a lightweight HTTP content negotiation library. It parses request headers (Accept, Accept-Encoding, Accept-Language, and Accept-Charset) conforming to RFC standards, accurately computing quality factors (q) and specificity to resolve the best match against supported server formats.

Features

  • Media Type Negotiation: Parses Accept headers, respects wildcards (*/*, image/*), and sorts by specificity and quality factors.
  • Encoding Preference: Evaluates Accept-Encoding preferences including support for gzip, deflate, br, and identity matching.
  • Language & Charset Selection: Compares available locales and character sets against client preferences.
  • Configurable Candidates: Filter preferences against an array of available options or return all client preferences.
  • Zero External Dependencies: Pure TypeScript implementation with full CommonJS and ES Module builds.

Installation

Install using your preferred package manager:

# npm
npm install @prexjs/negotiator

# pnpm
pnpm add @prexjs/negotiator

# Yarn
yarn add @prexjs/negotiator

# Bun
bun add @prexjs/negotiator

Quick start

import http from 'node:http';
import Negotiator from '@prexjs/negotiator';

const server = http.createServer((req, res) => {
  const negotiator = new Negotiator(req);

  // Determine preferred media type
  const availableTypes = ['text/html', 'application/json', 'text/plain'];
  const preferredType = negotiator.mediaType(availableTypes);

  // Determine preferred language
  const availableLanguages = ['en-US', 'fr-FR', 'es-ES'];
  const preferredLanguage = negotiator.language(availableLanguages);

  // Determine preferred encoding
  const availableEncodings = ['gzip', 'deflate', 'identity'];
  const preferredEncoding = negotiator.encoding(availableEncodings);

  res.setHeader('Content-Type', preferredType || 'text/plain');
  res.setHeader('Content-Language', preferredLanguage || 'en-US');
  if (preferredEncoding) {
    res.setHeader('Content-Encoding', preferredEncoding);
  }

  res.end(`Preferred: ${preferredType}, ${preferredLanguage}, ${preferredEncoding}`);
});

server.listen(3000);

API

new Negotiator(request) / negotiator(request)

Creates a negotiator instance for an HTTP request object containing headers.

negotiator.mediaType(available?)

Returns the most preferred media type from available, or undefined if none match.

negotiator.mediaTypes(available?)

Returns an array of preferred media types sorted by quality factor and specificity.

negotiator.encoding(available?, options?)

Returns the most preferred encoding from available, or undefined.

negotiator.encodings(available?, options?)

Returns an array of preferred encodings sorted by preference.

negotiator.language(available?)

Returns the most preferred language code, or undefined.

negotiator.languages(available?)

Returns an array of preferred languages sorted by preference.

negotiator.charset(available?)

Returns the most preferred character set, or undefined.

negotiator.charsets(available?)

Returns an array of preferred charsets sorted by preference.

Compatibility

  • Node.js >= 18.0.0
  • Supports both ESM and CommonJS
  • TypeScript 5.0+

Related Packages

License

MIT