<commit_before>/*global Buffer, module, require, setTimeout*/

/*
con = require("./ClojureServer").clientConnectionState.connection
con.address()
con.end()

require("./ClojureServer").clientConnectionState.connection = null


con.eval("(+ 2 3)", function(err, result) { console.log(err || result); })
con.eval("", function(err, result) { console.log(err || result); })
Global.nreplConnection = con;

b=require("/home/lively/clojure-om/node-nrepl-client/node_modules/bencode")
b.
*/

var debug = true;
var exec = require("child_process").exec;
var nreplClient = require("/home/lively/clojure-om/node-nrepl-client/src/nrepl-client.js");
// delete require.cache["/home/lively/clojure-om/node-nrepl-client/src/nrepl-client.js"];

var clientConnectionState = module.exports.clientConnectionState || {
    connection: null
}

function findNreplServerPort(thenDo) {
    var cmdString = "lsof -i tcp | "
                  + "grep LISTEN | "
                  + "grep TCP | grep :7888 | "
                  + "sed -E 's/[^:]+:([0-9]+).*/\\1/' | "
                  + "head -n 1";
    // var cmdString = "lsof -i tcp | "
    //               + "grep LISTEN | "
    //               + "grep TCP | grep -v :9080 | grep -v :9081 | "
    //               + "sed -E 's/[^:]+:([0-9]+).*/\\1/' | "
    //               + "head -n 1";
    exec(cmdString, function(code, out, err) {
        var port = parseInt(out);
        thenDo(isNaN(port) ? new Error("no nREPL server listening") : null, port);
    });
}

function ensureNREPLConnection(thenDo) {
    if (clientConnectionState.connection) {
        thenDo(null, clientConnectionState.connection);
        return;
    }
    findNreplServerPort(function(err, port) {
        console.log("Found nREPL server on port %s", port);
        if (err) { thenDo(err, null); return; }
        clientConnectionState.connection = nreplClient.connect({
            host: "127.0.0.1",
            port: port,
            verbose: true});
        clientConnectionState.connection.once("connect", function() {
            thenDo(null, clientConnectionState.connection);
        });
        clientConnectionState.connection.once("close", function() {
            clientConnectionState.connection = null;
        });
    });
}

function doEval(code, thenDo) {
    debug && console.log("Clojure eval: ", code);
    ensureNREPLConnection(function(err, con) {
        if (err) { thenDo(err, null); return; }
        con.messageStream.on("error", function(err) { con.end(); thenDo(err, null); })
        con.messageStream.once("messageSequence", function(messages) { thenDo(null, messages); })
        con.eval(code, function(err, result) {/*currently ignore*/});
    });
}

// -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-

require("./LivelyServices").services.clojureEval = function (sessionServer, connection, msg) {
    function answer(data) {
        connection.send({action: msg.action + 'Result',
            inResponseTo: msg.messageId, data: data});
    }
    doEval(msg.data.code, function(err, result) {
        answer({result: err ? String(err) : result});
    });
}


module.exports = function(route, app) {

    app.post(route + "reset", function(req, res) {
        var con = module.exports.clientConnectionState.connection
        con && con.end();
        module.exports.clientConnectionState.connection = null;
        delete require.cache["/home/lively/clojure-om/node-nrepl-client/src/nrepl-client.js"];
        res.end("OK");
    });

    app.get(route, function(req, res) {
        res.end("ClojureServer is running!");
    });
}

module.exports.clientConnectionState = clientConnectionState;
<commit_msg>clojure: subserver requires / auto installs nrepl module lazily<commit_after>/*global Buffer, module, require, setTimeout*/

var debug = true;
var exec  = require("child_process").exec;
var async = require("async");
var path  = require("path");
var fs  = require("fs");

// -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-

/*
con = require("./ClojureServer").clientConnectionState.connection

con.interrupt("36B0531A-DDA0-4498-85A8-A7E414BD2B90", function() { console.log(arguments); })
con.interrupt(undefined, function() { console.log(arguments); })
con.address()
con.end()

require("./ClojureServer").clientConnectionState.connection = null


con.eval("(+ 2 3)", function(err, result) { console.log(err || result); })
con.eval("", function(err, result) { console.log(err || result); })
Global.nreplConnection = con;

b=require("/home/lively/clojure-om/node-nrepl-client/node_modules/bencode")
b.
*/

var clientConnectionState = module.exports.clientConnectionState || {
    connection: null
}

function ensureNreplModule(thenDo) {
    try {
        var nreplClient = require("nrepl-client");
        process.nextTick(function() { thenDo(null, nreplClient); });
    } catch (e) {
        console.log("Installing nodejs module nrepl-client");
        var path = require('path'),
            fs = require('fs'),
            node_modules = path.join(process.env.WORKSPACE_LK, 'node_modules');
        exec("git clone https://github.com/rksm/node-nrepl-client nrepl-client; cd nrepl-client; npm install;",
            {cwd: path.join(process.env.WORKSPACE_LK, 'node_modules')},
            function(code, out, err) {
                try { nreplClient = require("nrepl-client"); } catch (e) { thenDo(e, null); return; }
                console.log("nrepl-client installed");
                thenDo(null, nreplClient);
            });
    }
}


function findNreplServerPort(thenDo) {
    var cmdString = "lsof -i tcp | "
                  + "grep LISTEN | "
                  + "grep TCP | grep :7888 | "
                  + "sed -E 's/[^:]+:([0-9]+).*/\\1/' | "
                  + "head -n 1";
    // var cmdString = "lsof -i tcp | "
    //               + "grep LISTEN | "
    //               + "grep TCP | grep -v :9080 | grep -v :9081 | "
    //               + "sed -E 's/[^:]+:([0-9]+).*/\\1/' | "
    //               + "head -n 1";
    exec(cmdString, function(code, out, err) {
        var port = parseInt(out);
        thenDo(isNaN(port) ? new Error("no nREPL server listening") : null, port);
    });
}

function ensureNreplConnection(thenDo) {
    if (clientConnectionState.connection) {
        thenDo(null, clientConnectionState.connection);
        return;
    }
    async.waterfall([
        findNreplServerPort,
        function(port, next) {
            console.log("Found nREPL server on port %s", port);
            ensureNreplModule(function(err, nreplClient) {
                next(err, port, nreplClient); });
        },
        function(port, nreplClient, next) {
            clientConnectionState.connection = nreplClient.connect({
                host: "127.0.0.1",
                port: port,
                verbose: true});
            clientConnectionState.connection.once("connect", function() {
                next(null, clientConnectionState.connection); });
            clientConnectionState.connection.once("close", function() {
                clientConnectionState.connection = null; });
        }
    ], function(err, con) {
        if (err) console.error("Error in ensureNreplConnection: ", err);
        thenDo(err, con);
    });
}

function doEval(code, thenDo) {
    debug && console.log("Clojure eval: ", code);
    ensureNreplConnection(function(err, con) {
        if (err) { thenDo(err, null); return; }
        con.messageStream.on("error", function(err) { con.end(); thenDo(err, null); })
        con.messageStream.once("messageSequence", function(messages) { thenDo(null, messages); })
        var id = con.eval(code, function(err, result) {/*currently ignore*/});
        console.log(id);
    });
}

// -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-

require("./LivelyServices").services.clojureEval = function (sessionServer, connection, msg) {
    function answer(data) {
        connection.send({action: msg.action + 'Result',
            inResponseTo: msg.messageId, data: data});
    }
    doEval(msg.data.code, function(err, result) {
        answer({result: err ? String(err) : result});
    });
}


module.exports = function(route, app) {

    app.post(route + "reset", function(req, res) {
        var con = module.exports.clientConnectionState.connection
        con && con.end();
        module.exports.clientConnectionState.connection = null;
        delete require.cache[require.resolve("nrepl-client")];
        res.end("OK");
    });

    app.get(route, function(req, res) {
        res.end("ClojureServer is running!");
    });
}

module.exports.clientConnectionState = clientConnectionState;

{'fraction_non_alphanumeric': 0.17578849721706866, 'fraction_numerical': 0.008928571428571428, 'mean_word_length': 2.578838174273859, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, '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': '6411853', 'n_tokens_mistral': 2779, 'n_tokens_neox': 2661, 'n_words': 502}