All files / lib utils.js

100% Statements 22/22
100% Branches 6/6
100% Functions 3/3
100% Lines 22/22
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 453x   3x 7x 7x   7x 7x 6x       1x     3x 24x 1x     23x 23x 23x 23x   23x 37x 22x       1x     3x   2x     3x          
const URL = require('url-parse')
 
const determineTransport = function (transports, url) {
  const parsedUrl = new URL(url)
  const scheme = parsedUrl.protocol.replace(':', '')
 
  for (let transport of transports) {
    if (transport.schemes.includes(scheme)) {
      return transport
    }
  }
 
  throw Error(`Unsupported scheme in URL: ${url}`)
}
 
const negotiateDecoder = function (decoders, contentType) {
  if (contentType === undefined) {
    return decoders[0]
  }
 
  const fullType = contentType.toLowerCase().split(';')[0].trim()
  const mainType = fullType.split('/')[0] + '/*'
  const wildcardType = '*/*'
  const acceptableTypes = [fullType, mainType, wildcardType]
 
  for (let decoder of decoders) {
    if (acceptableTypes.includes(decoder.mediaType)) {
      return decoder
    }
  }
 
  throw Error(`Unsupported media in Content-Type header: ${contentType}`)
}
 
const csrfSafeMethod = function (method) {
  // these HTTP methods do not require CSRF protection
  return (/^(GET|HEAD|OPTIONS|TRACE)$/.test(method))
}
 
module.exports = {
  determineTransport: determineTransport,
  negotiateDecoder: negotiateDecoder,
  csrfSafeMethod: csrfSafeMethod
}