var fs = require('fs');
var path = require('path');
var send = require('send');
var isFile = /\/[^.]+\.[a-z0-9]+$/gi;
var url = require('url');

// your custom headers
function headers(res, path, headers, dataHeaders) {

    headers = dataHeaders ? Object.assign(headers, dataHeaders) : headers;
    if (headers) {
        Object.keys(headers).forEach(key => res.setHeader(key, headers[key]));
    }

    // add http headers 
    if (path.substr(-3, 3) === '.js') {
        res.setHeader('Content-Encoding', 'gzip');
    }
}

exports.static = function (scope, inst, args, data, next) {

    if (!data.req || !data.res) {
        return next(new Error('Flow-static.static: No request or response stream found.'));
    }

    var file = args.file || data.file || data.params.name || url.parse(data.req.url).pathname;

    // normalize public path
    file = path.normalize(file);

    // add index.html to path
    /*if (file[file.length-1] === '/') {
        file = 'index.html';
    }*/

    // push the engine client directly
    //if (args.push && res.push) {

        /*push.args: {
            request: {accept: '*'+'/'+'*'},
            response: {
                'Content-Type': 'application/javascript',
                //'Content-Encoding': 'gzip',
                'Content-Length': clientFile.length
            }
        }*/
        //send(args.req, file, {root: path.join(this._env.workDir, args.wd || '')})
        //.on('error', onError)
        //.on('headers', headers)
        //.pipe(res.push(args.push.url, args.push.args || {}));
    //}
    //console.log('Static file:', scope.env._appDir, args.wd, path.join(scope.env._appDir, args.wd || ''), file);
    send(data.req, file, {root: path.join(scope.env._appDir, args.wd || '')})
    .on('error', function (err) {
        data.res.statusCode = err.status || 500;
        data.res.end(err.stack);
    })
    .on('headers', (res, path) => headers(res, path, args.headers, data.headers))
    .pipe(data.res);

    next(null, data);
};

@@ -721,18 +721,117 @@
     
-var file =
+// resolve file path%0A    const file = path.resolve(%0A        scope.env._appDir,%0A        args.wd %7C%7C '',%0A       
  arg
@@ -909,70 +909,23 @@
 name
-;%0A%0A    // normalize public path%0A    file = path.normalize(file
+.substr(1)%0A    
 );%0A%0A
@@ -1641,45 +1641,20 @@
 :', 
-scope.env._appDir, args.wd, path.join
+path.resolve
 (sco
@@ -1686,16 +1686,16 @@
 %7C ''
-)
 , file)
+)
 ;%0A  
@@ -1719,61 +1719,8 @@
 file
-, %7Broot: path.join(scope.env._appDir, args.wd %7C%7C '')%7D
 )%0A  

remove slash from url path and resolve path with appDir