<commit_before>/*jslint indent: 2, nomen: true, node: true*/
"use strict";

var fs = require("fs"),
  async = require("async"),
  spawn = require("child_process").spawn,
  http = require("http"),
  os = require("os"),
  socketIo = require("socket.io"),
  phantomjs = require("phantomjs"),
  arg = require("arg-err"),
  phantomProcess,
  socketServer,
  socketConnection,
  httpServer,
  currentScript = null;

/*
 * This uses a websockets method heavily borrowed from <NAME>'s node-phantom
 * https://github.com/alexscheelmeyer/node-phantom
 */

exports.cleanup = function () {
  if (!currentScript) {
    return;
  }

  socketConnection.emit("input", "exit");
  socketConnection.removeAllListeners("output");
  phantomProcess.stderr.removeAllListeners("data");
  phantomProcess.stdout.removeAllListeners("data");
  httpServer.close();
  socketServer.set('client store expiration', 0);

  httpServer = null;
  socketServer = null;
  socketConnection = null;
  phantomProcess = null;
  currentScript = null;
};

function initScript(callback) {
  if (phantomProcess) {
    phantomProcess.disconnect();
  }

  /*jslint unparam: true*/
  httpServer = http.createServer(function (req, res) {
    res.writeHead(200, {"Content-Type": "text/html"});
    res.end('<html><head><script src="/socket.io/socket.io.js" type="text/javascript"></script><script type="text/javascript">\n'
      + 'window.onload=function(){\n'
      + 'var socket = new io.connect("http://" + window.location.hostname);\n'
      + 'socket.on("input", function(msg){\n'
      + 'alert(msg);\n'
      + '});\n'
      + 'window.socket = socket;\n'
      + '};\n'
      + '</script></head><body></body></html>');
  }).listen(function () {
    var port = httpServer.address().port;
    socketServer = socketIo.listen(httpServer, { "log level": 1 });

    phantomProcess = spawn(phantomjs.path, [
      __dirname + "/phantom-scripts/bridge.js",<commit_msg>changed the way phantom process is killed<commit_after>/*jslint indent: 2, nomen: true, node: true*/
"use strict";

var fs = require("fs"),
  async = require("async"),
  spawn = require("child_process").spawn,
  http = require("http"),
  os = require("os"),
  socketIo = require("socket.io"),
  phantomjs = require("phantomjs"),
  arg = require("arg-err"),
  phantomProcess,
  socketServer,
  socketConnection,
  httpServer,
  currentScript = null;

/*
 * This uses a websockets method heavily borrowed from <NAME>'s node-phantom
 * https://github.com/alexscheelmeyer/node-phantom
 */

exports.cleanup = function () {
  if (!currentScript) {
    return;
  }

  socketConnection.emit("input", "exit");
  socketConnection.removeAllListeners("output");
  phantomProcess.stderr.removeAllListeners("data");
  phantomProcess.stdout.removeAllListeners("data");
  httpServer.close();
  socketServer.set('client store expiration', 0);

  httpServer = null;
  socketServer = null;
  socketConnection = null;
  phantomProcess = null;
  currentScript = null;
};

function initScript(callback) {
  if (phantomProcess) {
    phantomProcess.kill();
  }

  /*jslint unparam: true*/
  httpServer = http.createServer(function (req, res) {
    res.writeHead(200, {"Content-Type": "text/html"});
    res.end('<html><head><script src="/socket.io/socket.io.js" type="text/javascript"></script><script type="text/javascript">\n'
      + 'window.onload=function(){\n'
      + 'var socket = new io.connect("http://" + window.location.hostname);\n'
      + 'socket.on("input", function(msg){\n'
      + 'alert(msg);\n'
      + '});\n'
      + 'window.socket = socket;\n'
      + '};\n'
      + '</script></head><body></body></html>');
  }).listen(function () {
    var port = httpServer.address().port;
    socketServer = socketIo.listen(httpServer, { "log level": 1 });

    phantomProcess = spawn(phantomjs.path, [
      __dirname + "/phantom-scripts/bridge.js",
{'fraction_non_alphanumeric': 0.1774779449922159, 'fraction_numerical': 0.0031136481577581734, 'mean_word_length': 3.987063389391979, 'pattern_counts': {'":': 4, '<': 25, '<?xml version=': 0, '>': 25, 'https://': 2, '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': '3911884', 'n_tokens_mistral': 1266, 'n_tokens_neox': 1201, 'n_words': 233}