Socket
Socket
Sign inDemoInstall

vary

Package Overview
Dependencies
0
Maintainers
1
Versions
7
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    vary

Manipulate the HTTP Vary header


Version published
Maintainers
1
Install size
9.42 kB
Created

Package description

What is vary?

The vary npm package is a utility for manipulating the HTTP Vary header. It allows developers to programmatically append values to the Vary header of an HTTP response, ensuring that the correct header is constructed for proper handling of HTTP caching, content negotiation, and other mechanisms that might vary the response based on certain aspects of the request.

What are vary's main functionalities?

Appending to the Vary header

This feature allows you to append a field to the Vary header of an HTTP response. In the code sample, the 'User-Agent' field is added to the Vary header, indicating that the response may vary based on the 'User-Agent' header of the request.

"use strict";

const http = require('http');
const vary = require('vary');

http.createServer((req, res) => {
  vary(res, 'User-Agent');
  res.end('Response varies based on User-Agent header.');
}).listen(3000);

Appending multiple fields

This feature allows you to append multiple fields to the Vary header at once. In the code sample, both 'User-Agent' and 'Accept-Encoding' are added to the Vary header, indicating that the response may vary based on both of these request headers.

"use strict";

const http = require('http');
const vary = require('vary');

http.createServer((req, res) => {
  vary(res, 'User-Agent, Accept-Encoding');
  res.end('Response varies based on User-Agent and Accept-Encoding headers.');
}).listen(3000);

Other packages similar to vary

Readme

Source

vary

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

Manipulate the HTTP Vary header

Installation

This is a Node.js module available through the npm registry. Installation is done using the npm install command:

$ npm install vary

API

var vary = require('vary')

vary(res, field)

Adds the given header field to the Vary response header of res. This can be a string of a single field, a string of a valid Vary header, or an array of multiple fields.

This will append the header if not already listed, otherwise leaves it listed in the current location.

// Append "Origin" to the Vary header of the response
vary(res, 'Origin')

vary.append(header, field)

Adds the given header field to the Vary response header string header. This can be a string of a single field, a string of a valid Vary header, or an array of multiple fields.

This will append the header if not already listed, otherwise leaves it listed in the current location. The new header string is returned.

// Get header string appending "Origin" to "Accept, User-Agent"
vary.append('Accept, User-Agent', 'Origin')

Examples

Updating the Vary header when content is based on it

var http = require('http')
var vary = require('vary')

http.createServer(function onRequest (req, res) {
  // about to user-agent sniff
  vary(res, 'User-Agent')

  var ua = req.headers['user-agent'] || ''
  var isMobile = /mobi|android|touch|mini/i.test(ua)

  // serve site, depending on isMobile
  res.setHeader('Content-Type', 'text/html')
  res.end('You are (probably) ' + (isMobile ? '' : 'not ') + 'a mobile user')
})

Testing

$ npm test

License

MIT

Keywords

FAQs

Last updated on 24 Sep 2017

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