'use strict'

/**
 * Serve static files from `rootDir`.
 *
 * Serves all files from a specified directory. Can be used for public and
 * asset folders.
 *
 * @param {String} root
 * @return {Function}
 * @api public
 */

var send = require('koa-send')
var path = require('path')
var fs = require('fs')
var util = require('util')

module.exports = function(rootName, opts) {
    return new function() {

        let finalFiles = []
        let options = opts || {}
        var root = rootName
        let self = this

        if (!root || (typeof root) != 'string')
            throw Error('rootDir must be a valid path.')

        if (!fs.statSync(root).isDirectory())
            throw Error('rootDir should be a directory.')

        finalFiles = walk(root)

        root = fs.realpathSync(root)

        options.root = process.cwd()
        options.index = options.index || "index.html"
        self.root = root
        self.rootDir = path.normalize(util.format("/%s/", path.normalize(rootName)))

        this.serveCached = function() {
            return function*(next) {
                var file = finalFiles[this.path]
                var trailingSlash = '/' == this.path[this.path.length - 1]

                // if file exists or this is directory
                // pass it to koa-send
                if (file || trailingSlash)
                    yield send(this, this.path, options)
                else
                    yield *next
            }
        }

        this.reloadCached = function() {
            finalFiles = walk(self.root)
        }

        // here we check if request path (eg: /doc/file.html) is
        // allowed (eg. in /doc/)
        this.serve = function() {
            return function*(next) {

                var allowed = this.path.indexOf(self.rootDir) === 0
                if (allowed)
                    yield send(this, this.path, options)
                else
                    yield *next

            }
        }

        function walk(directory) {
            let files = fs.readdirSync(directory)

            for(let file of files) {
                file = directory + '/' + file
                if ((fs.statSync(file)).isDirectory())
                    walk(file, finalFiles)
                else
                    finalFiles[path.normalize(file)] = true
            }
            return finalFiles;
        }
    }
}

@@ -12,17 +12,16 @@
 '%0A%0A/**%0A 
-*
  Serve s
@@ -52,21 +52,18 @@
 r%60.%0A
- *
 %0A 
-*
  Serves 
 all 
@@ -62,12 +62,8 @@
 ves 
-all 
 file
@@ -69,18 +69,16 @@
 es from 
-a 
 specifie
@@ -92,60 +92,68 @@
 tory
-. Can be used for public and%0A * asset folders.%0A *
+ at specified path or from root.%0A  Supports 'index' file.%0A
 %0A 
-*
  @pa
@@ -161,23 +161,25 @@
 am %7B
-String%7D root
+Object%7D options
 %0A 
-*
  @re
@@ -188,20 +188,46 @@
 rn %7B
-F
+Object%7D - has only 'server' f
 unction
-%7D
 %0A 
-*
  @ap
@@ -235,17 +235,16 @@
  public%0A
- 
 */%0A%0Avar 
@@ -301,536 +301,105 @@
 h')%0A
-var fs = require('fs')%0Avar util = require('util')%0A%0Amodule.exports = function(rootName, opts) %7B%0A    return new function() %7B%0A%0A        let finalFiles = %5B%5D%0A        let options = opts %7C%7C %7B%7D%0A        var root = rootName%0A        let self = this%0A%0A        if (!root %7C%7C (typeof root) != 'string')%0A            throw Error('rootDir must be a valid path.')%0A%0A        if (!fs.statSync(root).isDirectory())%0A            throw Error('rootDir should be a directory.')%0A%0A        finalFiles = walk(root)%0A%0A        root = fs.realpathSync(root)%0A%0A        
+%0Amodule.exports = function(opts) %7B%0A%0A    let options = opts %7C%7C %7B%7D%0A    options.root = path.resolve(
 opti
@@ -406,18 +406,22 @@
 ons.root
- =
+Dir %7C%7C
  process
@@ -426,21 +426,18 @@
 ss.cwd()
+)
 %0A
-    
     opti
@@ -482,167 +482,103 @@
 ml%22%0A
-   
+%0A
     
- self.root = root%0A        self.rootDir = path.normalize(util.format(%22/%25s/%22, path.normalize(rootName)))%0A%0A        this.serveCached = function() %7B%0A        
+if (typeof options.rootDir != 'string')%0A        throw Error('rootDir must be specified')%0A%0A
     
@@ -598,32 +598,33 @@
 on*(next) %7B%0A
+%0A
         
         var 
@@ -615,1296 +615,793 @@
     
-        var file = finalFiles%5Bthis.path%5D%0A                var trailingSlash = '/' == this.path%5Bthis.path.length - 1%5D%0A%0A                // if file exists or this is directory%0A                // pass it to koa-send%0A                if (file %7C%7C trailingSlash)%0A                    yield send(this, this.path, options)%0A                else%0A                    yield *next%0A            %7D%0A        %7D%0A%0A        this.reloadCached = function() %7B%0A            finalFiles = walk(self.root)%0A        %7D%0A%0A        // here we check if request path (eg: /doc/file.html) is%0A        // allowed (eg. in /doc/)%0A        this.serve = function() %7B%0A            return function*(next) %7B%0A%0A                var allowed = this.path.indexOf(self.rootDir) === 0%0A                if (allowed)%0A                    yield send(this, this.path, options)%0A                else%0A                    yield *next%0A%0A            %7D%0A        %7D%0A%0A        function walk(directory) %7B%0A            let files = fs.readdirSync(directory)%0A%0A            for(let file of files) %7B%0A                file = directory + '/' + file%0A                if ((fs.statSync(file)).isDirectory())%0A                    walk(file, finalFiles)%0A                else%0A                    finalFiles%5Bpath.normalize(file)%5D = true%0A            %7D%0A            return finalFiles;%0A        %7D
+/* Serve folder as root path - default%0A         eg. for options%0A            rootDir = 'web'%0A        'web/file.txt' will be served as 'http://host/file.txt'%0A        */%0A        if (!options.rootPath)%0A            return yield send(this, this.path, options)%0A%0A        // Check if request path (eg: /doc/file.html) is allowed (eg. in /doc)%0A        if (this.path.indexOf(options.rootPath) != 0)%0A            return yield * next%0A%0A        /* Serve folders as specified%0A         eg. for options:%0A            rootDir = 'web/static'%0A            rootPath = '/static'%0A%0A        'web/static/file.txt' will be served as 'http://server/static/file.txt'%0A        */%0A        if (options.rootPath)%0A            this.path = this.path.replace(options.rootPath, '')%0A%0A        yield send(this,  this.path, options)
 %0A   

streamline module