• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1const url = require('url')
2
3module.exports = setWarning
4
5function setWarning (reqOrRes, code, message, replace) {
6  //   Warning    = "Warning" ":" 1#warning-value
7  // warning-value = warn-code SP warn-agent SP warn-text [SP warn-date]
8  // warn-code  = 3DIGIT
9  // warn-agent = ( host [ ":" port ] ) | pseudonym
10  //                 ; the name or pseudonym of the server adding
11  //                 ; the Warning header, for use in debugging
12  // warn-text  = quoted-string
13  // warn-date  = <"> HTTP-date <">
14  // (https://tools.ietf.org/html/rfc2616#section-14.46)
15  const host = url.parse(reqOrRes.url).host
16  const jsonMessage = JSON.stringify(message)
17  const jsonDate = JSON.stringify(new Date().toUTCString())
18  const header = replace ? 'set' : 'append'
19
20  reqOrRes.headers[header](
21    'Warning',
22    `${code} ${host} ${jsonMessage} ${jsonDate}`
23  )
24}
25