<commit_before>/**
 * Headless shell container functions.
 *
 * Parts abducted from node-phantom's node-phantom.js.
 *
 * @author   <NAME> <<EMAIL>>
 * @license  MIT Licensed
 */

/*jshint
 strict:true, eqeqeq:true, newcap:false, multistr:true, expr:true,
 loopfunc:true, shadow:true, node:true, indent:4
*/

if (typeof process === 'undefined') {
    console.error("Headless shell needs to be executed from mothership");
}

var uname = require('uname'),
    utsname = uname.uname();

var Shell = function (container, user, list, index, load) {
    "use strict";
    this.container = container;
    try {
        this.list = JSON.parse(fs.readFileSync(list).toString());
    } catch (err) {
        var match = (new RegExp('\\('+shell.path+'\/(.*):(.*):(.*)\\)', 'i')).exec(err.stack);
        if (match) error = {
            path: list,
            line: 0,
            ch: 0,
            message: err.message,
            stack: err.stack
        };
    }
    this.callback = null;
    var self = this;
    switch (this.container) {
        case 'node':
            var node = cp.fork('shell.js', [list, index]);
            node.on('message', function (payload) {
                var message = payload.message,
                    data = payload.data;
                switch (message) {
                    case 'request':
                        self[data.command](user, data, function (args) {
                            node.send({
                                message: 'response',
                                data: {
                                    id: data.id,
                                    command: data.command,
                                    args: args
                                }
                            });
                        });
                        break;
                    case 'data':
                        var args = data.args;
                        if (user) {
                            send(user.socket, {
                                name: user.name,
                                message: 'data',
                                data: data
                            });
                        }
                        if (data.command === 'log' || data.command === 'box') {
                            node.send({
                                message: 'response',
                                data: {
                                    id: data.id,
                                    command: data.command,
                                    args: args
                                }
                            });
                        }
                        if (args.error) error = args.error;
                        break;
                    case 'error':
                        if (user) {
                            send(user.socket, {
                                name: user.name,
                                message: 'error',
                                data: data
                            });
                        }
                        util.error(data.stack);
                        break;
                }
            });
            node.on('exit', function (code, signal) {
                if (user) {
                    send(user.socket, {
                        name: user.name,
                        message: 'data',
                        data: {
                            args: {
                                error: error,
                                message: "Exited",
                                progress: 100
                            }
                        }
                    });
                }
                if (list.run === 'forever') {
                    send(user.socket, {
                        name: user.name,
                        message: 'data',
                        data: {
                            args: {
                                error: error,
                                message: "Reinitializing, running forever",
                                progress: 0
                            }
                        }
                    });
                    var run = new Shell(container, user, list, index, load);
                }
            });
            node.send({
                message: 'init',
                data: {
                    headlessVersion: version,
                    version: process.version.substring(1),
                    arch: os.platform()+"-"+utsname.machine,
                    hostname: os.hostname(),
                    payload: load
                }
            });
            self.node = node;
            break;
        case 'phantom':
            var server = http.createServer(function (req, res) {
                res.writeHead(200, {'Content-Type':'text/html'});
                res.end("<html><head><script>\n\
                    var socket = new WebSocket('ws://'+location.host);\n\
                    socket.onopen = function (){\n\
                        alert(JSON.stringify({\n\
                            message: 'init',\n\
                            data: {\n\
                                headlessVersion: '"+version+"',\n\
                                version: '"+process.version.substring(1)+"',\n\
                                arch: '"+os.platform()+"-"+utsname.machine+"',\n\
                                hostname: '"+os.hostname()+"',\n\
                                payload: JSON.parse("+JSON.stringify(load)+")\n\
                            }\n\
                        }));\n\
                    };\n\
                    socket.onmessage = function (event){\n\
                        alert(event.data);\n\
                    };\n\
                </script></head><body></body></html>");
            }).listen(function () {
                var phantom = cp.spawn(config.phantomPath ? config.phantomPath : require('phantomjs').path, ['--ignore-ssl-errors=true', 'shell.js', server.address().port, list, index]);
                phantom.stdout.on('data', function (data) {
                    util.log("Phantom stdout: "+data);
                });
                phantom.stderr.on('data', function (data) {
                    util.error("Phantom stderr: "+data);
                });
                phantom.on('exit', function (code, signal) {
                    if (user) {
                        send(user.socket, {
                            name: user.name,
                            message: 'data',
                            data: {
                                args: {
                                    error: error,
                                    message: "Exited",
                                    progress: 100
                                }
                            }
                        });
                    }
                    server.close(function () {
                        if (list.run === 'forever') {
                            if (user) {
                                send(user.socket, {
                                    name: user.name,
                                    message: 'data',
                                    data: {
                                        args: {
                                            error: error,
                                            message: "Reinitializing, running forever",
                                            progress: 0
                                        }
                                    }<commit_msg>respawn fix<commit_after>/**
 * Headless shell container functions.
 *
 * Parts abducted from node-phantom's node-phantom.js.
 *
 * @author   <NAME> <<EMAIL>>
 * @license  MIT Licensed
 */

/*jshint
 strict:true, eqeqeq:true, newcap:false, multistr:true, expr:true,
 loopfunc:true, shadow:true, node:true, indent:4
*/

if (typeof process === 'undefined') {
    console.error("Headless shell needs to be executed from mothership");
}

var uname = require('uname'),
    utsname = uname.uname();

var Shell = function (container, user, list, index, load) {
    "use strict";
    this.container = container;
    try {
        this.list = JSON.parse(fs.readFileSync(list).toString());
    } catch (err) {
        var match = (new RegExp('\\('+shell.path+'\/(.*):(.*):(.*)\\)', 'i')).exec(err.stack);
        if (match) error = {
            path: list,
            line: 0,
            ch: 0,
            message: err.message,
            stack: err.stack
        };
    }
    this.callback = null;
    var self = this;
    switch (this.container) {
        case 'node':
            var node = cp.fork('shell.js', [list, index]);
            node.on('message', function (payload) {
                var message = payload.message,
                    data = payload.data;
                switch (message) {
                    case 'request':
                        self[data.command](user, data, function (args) {
                            node.send({
                                message: 'response',
                                data: {
                                    id: data.id,
                                    command: data.command,
                                    args: args
                                }
                            });
                        });
                        break;
                    case 'data':
                        var args = data.args;
                        if (user) {
                            send(user.socket, {
                                name: user.name,
                                message: 'data',
                                data: data
                            });
                        }
                        if (data.command === 'log' || data.command === 'box') {
                            node.send({
                                message: 'response',
                                data: {
                                    id: data.id,
                                    command: data.command,
                                    args: args
                                }
                            });
                        }
                        if (args.error) error = args.error;
                        break;
                    case 'error':
                        if (user) {
                            send(user.socket, {
                                name: user.name,
                                message: 'error',
                                data: data
                            });
                        }
                        util.error(data.stack);
                        break;
                }
            });
            node.on('exit', function (code, signal) {
                if (user) {
                    send(user.socket, {
                        name: user.name,
                        message: 'data',
                        data: {
                            args: {
                                error: error,
                                message: "Exited",
                                progress: 100
                            }
                        }
                    });
                }
                if (self.list.run === 'forever') {
                    if (user) {
                        util.log("Respawning "+list);
                        send(user.socket, {
                            name: user.name,
                            message: 'data',
                            data: {
                                args: {
                                    error: error,
                                    message: "Respawning",
                                    progress: 0
                                }
                            }
                        });
                    }
                    var run = new Shell(container, user, list, index, load);
                }
            });
            node.send({
                message: 'init',
                data: {
                    headlessVersion: version,
                    version: process.version.substring(1),
                    arch: os.platform()+"-"+utsname.machine,
                    hostname: os.hostname(),
                    payload: load
                }
            });
            self.node = node;
            break;
        case 'phantom':
            var server = http.createServer(function (req, res) {
                res.writeHead(200, {'Content-Type':'text/html'});
                res.end("<html><head><script>\n\
                    var socket = new WebSocket('ws://'+location.host);\n\
                    socket.onopen = function (){\n\
                        alert(JSON.stringify({\n\
                            message: 'init',\n\
                            data: {\n\
                                headlessVersion: '"+version+"',\n\
                                version: '"+process.version.substring(1)+"',\n\
                                arch: '"+os.platform()+"-"+utsname.machine+"',\n\
                                hostname: '"+os.hostname()+"',\n\
                                payload: JSON.parse("+JSON.stringify(load)+")\n\
                            }\n\
                        }));\n\
                    };\n\
                    socket.onmessage = function (event){\n\
                        alert(event.data);\n\
                    };\n\
                </script></head><body></body></html>");
            }).listen(function () {
                var phantom = cp.spawn(config.phantomPath ? config.phantomPath : require('phantomjs').path, ['--ignore-ssl-errors=true', 'shell.js', server.address().port, list, index]);
                phantom.stdout.on('data', function (data) {
                    util.log("Phantom stdout: "+data);
                });
                phantom.stderr.on('data', function (data) {
                    util.error("Phantom stderr: "+data);
                });
                phantom.on('exit', function (code, signal) {
                    if (user) {
                        send(user.socket, {
                            name: user.name,
                            message: 'data',
                            data: {
                                args: {
                                    error: error,
                                    message: "Exited",
                                    progress: 100
                                }
                            }
                        });
                    }
                    server.close(function () {
                        if (self.list.run === 'forever') {
                            if (user) {
                                util.log("Respawning "+list);
                                send(user.socket, {
                                    name: user.name,
                                    message: 'data',
                                    data: {
                                        args: {
                                            error: error,
                                            message: "Respawning",
                                            progress: 0
                                        }
                                    }
{'fraction_non_alphanumeric': 0.10951008645533142, 'fraction_numerical': 0.002095886822111606, 'mean_word_length': 0.6976873471203024, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 45, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4458021', 'n_tokens_mistral': 3490, 'n_tokens_neox': 3188, 'n_words': 667}