<commit_before>var express = require('express'),
    cors = require('cors'),
    webcommand = require('webcommand'),
    parseUrl = require('webcommand').parseUrl,
    generateUrl = require('webcommand').generateUrl,
    stream = require('event-stream');
var request = require('request');

module.exports = createServer;

function createServer(cmdList){
    var webCommand = webcommand(cmdList);
    var app = express();
    app.use(cors());
    app.get('/getCommands', cors(), function(req,res) {
        res.json(webCommand.getCommandList());
    });

    app.post('/*', function(req,res){
        var wc = parseUrl(req.url),
            cStream= stream.through();
        cStream.on('error', function(err) {
            var ret = { error : err.name, message : err.message, stack : err.stack };
            if (err.name == 'COMMAND_NOT_ALLOWED') res.status(403, ret);
            else if (err.name == 'COMMAND_NOT_FOUND') res.status(501, ret);
            else if (err.name == 'COMMAND_EXITED_ABNORMALLY') res.status(400, ret);
            else res.status(500, ret);
        });
        if(wc.pipes){
            var curPipe = wc.pipes.shift(),
                purl = generateUrl({
                base : curPipe.base,
                cmd : curPipe.cmd,
                args : curPipe.args,
                pipes : wc.pipes
            });
            var iStream= stream.through();
            iStream.pipe(request.post(purl)).pipe(res);
            webCommand.webCommand(wc.cmd,wc.args, req, iStream, cStream);
        }
        else{
            webCommand.webCommand(wc.cmd,wc.args, req, res, cStream);
        }
    });
    return app;
}<commit_msg>added better test ls, and fixed how we behave on end<commit_after>var express = require('express'),
    cors = require('cors'),
    webcommand = require('webcommand'),
    parseUrl = require('webcommand').parseUrl,
    generateUrl = require('webcommand').generateUrl,
    stream = require('event-stream');
var request = require('request');

module.exports = createServer;

function createServer(cmdList){
    var webCommand = webcommand(cmdList);
    var app = express();
    app.use(cors());
    app.get('/getCommands', cors(), function(req,res) {
        res.json(webCommand.getCommandList());
    });

    app.post('/*', function(req,res){
        var wc = parseUrl(req.url),
            cStream= stream.through();
        cStream.on('error', function(err) {
            var ret = { error : err.name, message : err.message, stack : err.stack };
            if (err.name == 'COMMAND_NOT_ALLOWED') res.status(403, ret);
            else if (err.name == 'COMMAND_NOT_FOUND') res.status(501, ret);
            else if (err.name == 'COMMAND_EXITED_ABNORMALLY') res.status(400, ret);
            else res.status(500, ret);
        });
        if(wc.pipes){
            var curPipe = wc.pipes.shift(),
                purl = generateUrl({
                base : curPipe.base,
                cmd : curPipe.cmd,
                args : curPipe.args,
                pipes : wc.pipes
            });
            var iStream= stream.through();
            iStream.pipe(require('through-logged')()).pipe(request.post(purl)).pipe(res);
            webCommand.webCommand(wc.cmd,wc.args, req, iStream, cStream);
        }
        else{
            webCommand.webCommand(wc.cmd,wc.args, req, res, cStream);
        }
    });
    return app;
}
{'fraction_non_alphanumeric': 0.14859675036927622, 'fraction_numerical': 0.0070901033973412115, 'mean_word_length': 2.2464046021093003, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2778652', 'n_tokens_mistral': 1045, 'n_tokens_neox': 1018, 'n_words': 204}