Code coverage report for beefy/lib/handlers/static.js

Statements: 100% (21 / 21)      Branches: 91.67% (11 / 12)      Functions: 100% (4 / 4)      Lines: 100% (21 / 21)      Ignored: none     

All files » beefy/lib/handlers/ » static.js
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 461   1       1 16   16   1 24         24   24 20 20     24   1 72 14     58   58 58 48     10 10            
module.exports = handleStatic
 
var path = require('path')
  , mime = require('mime')
  , fs = require('fs')
 
function handleStatic(opts, io, nextHandler) {
  var cwd = opts.cwd || process.cwd()
 
  return handle
 
  function handle(server, req, resp, parsed) {
    var filepath = path.join.apply(
        path
      , [cwd].concat(parsed.pathname.split('/'))
    )
 
    var check = [filepath]
 
    if(/html/.test(req.headers.accept || '')) {
      check.push(path.join(filepath, 'index.html'))
      check.push(filepath + '.html')
    }
 
    return iter()
 
    function iter() {
      if(!check.length) {
        return nextHandler(server, req, resp, parsed)
      }
 
      var currentPath = check.shift()
 
      fs.lstat(currentPath, function(err, stat) {
        if(err || stat.isDirectory()) {
          return iter()
        }
 
        resp.setHeader('content-type', mime.lookup(currentPath))
        fs.createReadStream(currentPath)
          .pipe(resp)
      })
    }
  }
}