@goa/vary

1.0.0 • Public • Published

@goa/vary

npm version

@goa/vary is a fork of Manipulate the HTTP Vary header rewritten in ES6 Modules.

yarn add @goa/vary

The original module has been updated to be used in @goa/koa: Koa web server compiled with Google Closure Compiler using Depack into a single file library (0 dependencies).

Table Of Contents

API

The package is available by importing its default and named functions:

import vary, { append } from '@goa/vary'

vary(
  res: http.ServerResponse,
  field: string|Array<string>,
): void

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.

/* alanode example/ */
import vary from '@goa/vary'
import aqt from '@rqt/aqt'
import { createServer } from 'http'

const server = createServer((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')
})

server.listen(async () => {
  const url = `http://localhost:${server.address().port}`
  const { headers } = await aqt(url)
  console.log(headers)
  server.close()
})
{ vary: 'User-Agent',
  'content-type': 'text/html',
  date: 'Sat, 20 Apr 2019 11:57:51 GMT',
  connection: 'close',
  'content-length': '36' }

append(
  header: string,
  field: string|Array<string>,
): string

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.

append('Accept, User-Agent', 'Origin')
import { append } from '@goa/vary'

// Get header string appending "Origin" to "Accept, User-Agent"
console.log(append('Accept, User-Agent', 'Origin'))
console.log(append('Accept-Encoding, Accept', '*'))
console.log(append('Accept-Encoding, Accept, *', 'Origin'))
Accept, User-Agent, Origin
*
*

Copyright

Original Author: Douglas Wilson


Art Deco © Art Deco for Idio 2019 Idio Tech Nation Visa Tech Nation Visa Sucks

Versions

Current Tags

  • Version
    Downloads (Last 7 Days)
    • Tag
  • 1.0.0
    1
    • latest

Version History

  • Version
    Downloads (Last 7 Days)
    • Published
  • 1.0.0
    1

Package Sidebar

Install

npm i @goa/vary

Weekly Downloads

1

Version

1.0.0

License

MIT

Unpacked Size

13 kB

Total Files

10

Last publish

Collaborators

  • zvr