<commit_before>// Load modules

var Ws = require('ws');
var Hoek = require('hoek');


// Declare internals

var internals = {
    webSockets: []
};


internals.defaults = {
    endpoint: '/debug/terminal'
};


exports.register = function (plugin, options, next) {

    var settings = Hoek.applyToDefaults(internals.defaults, options);
    plugin.route({
        method: 'GET',
        path: settings.endpoint,
        handler: internals.handler
    });

    for (var i = 0, il = plugin.servers.length; i < il; ++i) {
        registerServer(plugin.servers[i]);
    }

    var oldStdout = process.stdout.write.bind(process.stdout);
    process.stdout.write = function (chunk, encoding) {

        oldStdout(chunk, encoding);
        transmit(chunk);
    };

    var oldStderr = process.stderr.write.bind(process.stderr);
    process.stderr.write = function (chunk, encoding) {

        oldStderr(chunk, encoding);
        transmit(chunk);
    };

    return next();
};


var registerServer = function (server) {

    var ws = new Ws.Server({ server: server.listener, path: '/reflektor' });
    ws.on('connection', function (socket) {

        socket.send('Welcome');
    });

    internals.webSockets.push(ws);
};


var transmit = function (data) {

    try {
        internals.webSockets.forEach(function (ws) {

            for (var i = 0, il = ws.clients.length; i < il; ++i) {
                ws.clients[i].send(data.toString());
            }
        });
    }
    catch (err) {}
};


internals.handler = function (request, reply) {
<commit_msg>Works with newer hapi<commit_after>// Load modules

var Ws = require('ws');
var Hoek = require('hoek');


// Declare internals

var internals = {
    webSockets: []
};


internals.defaults = {
    endpoint: '/debug/terminal'
};


exports.register = function (plugin, options, next) {

    var settings = Hoek.applyToDefaults(internals.defaults, options);
    plugin.route({
        method: 'GET',
        path: settings.endpoint,
        handler: internals.handler
    });

    for (var i = 0, il = plugin.servers.length; i < il; ++i) {
        registerServer(plugin.servers[i]);
    }

    var oldStdout = process.stdout.write.bind(process.stdout);
    process.stdout.write = function (chunk, encoding) {

        oldStdout(chunk, encoding);
        transmit(chunk);
    };

    var oldStderr = process.stderr.write.bind(process.stderr);
    process.stderr.write = function (chunk, encoding) {

        oldStderr(chunk, encoding);
        transmit(chunk);
    };

    return next();
};


exports.register.attributes = {

    pkg: require('../package.json')
};


var registerServer = function (server) {

    var ws = new Ws.Server({ server: server.listener, path: '/reflektor' });
    ws.on('connection', function (socket) {

        socket.send('Welcome');
    });

    internals.webSockets.push(ws);
};


var transmit = function (data) {

    try {
        internals.webSockets.forEach(function (ws) {

            for (var i = 0, il = ws.clients.length; i < il; ++i) {
                ws.clients[i].send(data.toString());
            }
        });
    }
    catch (err) {}
};


internals.handler = function (request, reply) {

{'fraction_non_alphanumeric': 0.15009439899307742, 'fraction_numerical': 0.0012586532410320957, 'mean_word_length': 2.55195530726257, 'pattern_counts': {'":': 0, '<': 7, '<?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': '1717836', 'n_tokens_mistral': 1027, 'n_tokens_neox': 976, 'n_words': 216}