<commit_before>var net = require('net');
var constant = require('./constants');

var parseExitCode = function(buffer, defaultCode) {
  var tailPos = buffer.length - Buffer.byteLength(constant.EXIT_CODE_0);

  if (tailPos < 0) {
    return defaultCode;
  }

  // tail buffer which might contain the message
  var tail = buffer.slice(tailPos);
  if (tail.toString() === constant.EXIT_CODE_0) {
    tail.fill('\x00');
    return 0;
  }

  return defaultCode;
};


exports.run = function(config) {
  var port = config.runnerPort || constant.DEFAULT_RUNNER_PORT;
  var socket = net.connect(port);
  var exitCode = 1;

  // TODO(vojta): error when no Testacular listening on this port
  socket.on('data', function(buffer) {
    exitCode = parseExitCode(buffer, exitCode);
    process.stdout.write(buffer);
  });

  socket.on('error', function(e) {
    if (e.code === 'ECONNREFUSED') {
      console.error('There is no server listening on port %d', port);
      process.exit(1);
    } else {
      throw e;
    }
  });

  socket.on('end', function() {
    process.exit(exitCode);
  });
};<commit_msg>Fix runner to wait for stdout, stderr<commit_after>var net = require('net');
var constant = require('./constants');

var parseExitCode = function(buffer, defaultCode) {
  var tailPos = buffer.length - Buffer.byteLength(constant.EXIT_CODE_0);

  if (tailPos < 0) {
    return defaultCode;
  }

  // tail buffer which might contain the message
  var tail = buffer.slice(tailPos);
  if (tail.toString() === constant.EXIT_CODE_0) {
    tail.fill('\x00');
    return 0;
  }

  return defaultCode;
};


exports.run = function(config) {
  var port = config.runnerPort || constant.DEFAULT_RUNNER_PORT;
  var socket = net.connect(port);
  var exitCode = 1;

  // TODO(vojta): error when no Testacular listening on this port
  socket.on('data', function(buffer) {
    exitCode = parseExitCode(buffer, exitCode);
    process.stdout.write(buffer);
  });

  socket.on('error', function(e) {
    if (e.code === 'ECONNREFUSED') {
      console.error('There is no server listening on port %d', port);
      process.exit(1);
    } else {
      throw e;
    }
  });

  socket.on('close', function() {
    process.exit(exitCode);
  });
};
{'fraction_non_alphanumeric': 0.1469258589511754, 'fraction_numerical': 0.007233273056057866, 'mean_word_length': 3.434869739478958, 'pattern_counts': {'":': 0, '<': 5, '<?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': '2429382', 'n_tokens_mistral': 726, 'n_tokens_neox': 713, 'n_words': 176}