all files / lib/spdy-transport/protocol/base/ utils.js

89.47% Statements 34/38
76% Branches 19/25
100% Functions 7/7
89.47% Lines 34/38
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95        28× 28×     28×       30×             304×     307×       3571× 3571× 1324× 1324×     2247×                                                           10×     10×       2236×   2232×        
'use strict'
 
var utils = exports
 
var util = require('util')
 
function ProtocolError (code, message) {
  this.code = code
  this.message = message
}
util.inherits(ProtocolError, Error)
utils.ProtocolError = ProtocolError
 
utils.error = function error (code, message) {
  return new ProtocolError(code, message)
}
 
utils.reverse = function reverse (object) {
  var result = []
 
  Object.keys(object).forEach(function (key) {
    result[object[key] | 0] = key
  })
 
  return result
}
 
// weight [1, 36] <=> priority [0, 7]
// This way weight=16 is preserved and has priority=3
utils.weightToPriority = function weightToPriority (weight) {
  return ((Math.min(35, (weight - 1)) / 35) * 7) | 0
}
 
utils.priorityToWeight = function priorityToWeight (priority) {
  return (((priority / 7) * 35) | 0) + 1
}
 
// Copy-Paste from node
exports.addHeaderLine = function addHeaderLine (field, value, dest) {
  field = field.toLowerCase()
  if (/^:/.test(field)) {
    dest[field] = value
    return
  }
 
  switch (field) {
    // Array headers:
    case 'set-cookie':
      if (dest[field] !== undefined) {
        dest[field].push(value)
      } else {
        dest[field] = [ value ]
      }
      break
 
    /* eslint-disable max-len */
    // list is taken from:
    /* eslint-enable max-len */
    case 'content-type':
    case 'content-length':
    case 'user-agent':
    case 'referer':
    case 'host':
    case 'authorization':
    case 'proxy-authorization':
    case 'if-modified-since':
    case 'if-unmodified-since':
    case 'from':
    case 'location':
    case 'max-forwards':
      // drop duplicates
      Eif (dest[field] === undefined) {
        dest[field] = value
      }
      break
 
    case 'cookie':
      // make semicolon-separated list
      if (dest[field] !== undefined) {
        dest[field] += '; ' + value
      } else {
        dest[field] = value
      }
      break
 
    default:
      // make comma-separated list
      if (dest[field] !== undefined) {
        dest[field] += ', ' + value
      } else {
        dest[field] = value
      }
  }
}