'use strict'

/**
 * Module dependencies
 */

const send = require('koa-send')
const normalize = require('path').normalize
const path = require('path')

module.exports = serve

/**
 * Serve static files from `rootDir`.
 *
 * Serves files from specified directory at specified path or from root.
 * Supports 'index' file.
 *
 * @param {Object} options
 * @return {Object} - {Function} serve
 * @api public
 */

function serve(opts) {

    let options = opts || {}
    options.root = path.resolve(options.rootDir || process.cwd())
    options.index = options.index || "index.html"
    let log = options.log || false

    if (typeof options.rootDir !== 'string')
        throw Error('rootDir must be specified')

    return function*(next) {
        /* Serve folder as root path - default
         eg. for options
            rootDir = 'web'
        'web/file.txt' will be served as 'http://host/file.txt'
        */
        let path = this.path
        if (!options.rootPath) {
            log && console.log(new Date().toISOString(), path)
            const sent = yield send(this, path, options)
            if (sent)
                return
            else
                return yield *next
        }

        // Check if request path (eg: /doc/file.html) is allowed (eg. in /doc)
        if (path.indexOf(options.rootPath) !== 0)
            return yield *next

        /* Serve folders as specified
         eg. for options:
            rootDir = 'web/static'
            rootPath = '/static'

        'web/static/file.txt' will be served as 'http://server/static/file.txt'
        */

        // Redirect non-slashed request to slashed
        // eg. /doc to /doc/

        if (path === options.rootPath)
            return this.redirect(normalize(options.rootPath + "/"))

        if (options.rootPath)
            path = normalize(path.replace(options.rootPath, "/"))

        log && console.log(new Date().toISOString(), path)
        const sent = yield send(this,  path, options)
        if (sent)
            return
        else
            return yield *next
    }
}

@@ -39,16 +39,49 @@
 es%0A */%0A%0A
+const assert = require('assert')%0A
 const se
@@ -436,16 +436,22 @@
 ic%0A */%0A%0A
+async 
 function
@@ -460,16 +460,17 @@
 erve
+ 
 (opts) %7B
 %0A%0A  
@@ -465,16 +465,107 @@
 (opts) %7B
+%0A    assert(typeof options.rootDir === 'string', 'rootDir must be specified (as a string)')
 %0A%0A    le
@@ -707,18 +707,20 @@
 ml%22%0A    
-le
+cons
 t log = 
@@ -749,125 +749,35 @@
     
-if (typeof options.rootDir !== 'string')%0A        throw Error('rootDir must be specified')%0A%0A    return function*(
+return async (ctx, 
 next)
+ =%3E
  %7B%0A 
@@ -950,16 +950,60 @@
      */%0A
+        assert(ctx, 'koa context requried')%0A
         
@@ -1144,31 +1144,30 @@
  sent = 
-yield
+await
  send(
-this
+ctx
 , path, 
@@ -1256,35 +1256,30 @@
      return 
-yield *
 next
+()
 %0A        %7D%0A%0A
@@ -1277,16 +1277,20 @@
       %7D%0A
+    
 %0A       
@@ -1430,28 +1430,27 @@
  return 
-yield *next%0A
+next()%0A    
 %0A       
@@ -1574,16 +1574,20 @@
 static'%0A
+    
 %0A       
@@ -1670,16 +1670,20 @@
      */%0A
+    
 %0A       
@@ -1755,16 +1755,20 @@
 o /doc/%0A
+    
 %0A       
@@ -1863,24 +1863,28 @@
 ath + %22/%22))%0A
+    
 %0A        if 
@@ -1968,16 +1968,20 @@
 , %22/%22))%0A
+    
 %0A       
@@ -2057,25 +2057,23 @@
 t = 
-yield
+await
  send(
-this, 
+ctx,
  pat
@@ -2157,19 +2157,14 @@
 urn 
-yield *
 next
+()
 %0A   

Convert to Koa v2.x middleware style