<commit_before>'use strict';

var net = require('net');

/**
 * Create new HA proxy mock server.
 *
 * @param {Object} options
 * @api public
 */
exports.createServer = function createServer(options) {
  return new Hamock(options || {});
};

/**
 * A HAProxy unix-socket mockup server.
 *
 * @constructor
 * @param {Object} options
 * @api private
 */
function Hamock(options) {
  this.server = options.server || net.createServer();
  this.server.on('connection', this.handle.bind(this));
  this.commands = options.commands || require('./fixtures');
  this.sockets = [];
}

/**
 * Handles requests.
 *
 * @param {Socket} socket TCP socket of the connection.
 * @api private
 */
Hamock.prototype.handle = function handle(socket) {
  this.sockets.push(socket);

  var command = '';
  socket.setEncoding('utf-8');

  socket.on('data', function data(buffer) {
    command += buffer;
  }).on('end', function end() {
    var handler = command.trim()
      , data = this.commands[handler];

    socket.end(data || 'Unkown command');
  }.bind(this));

  socket.once('close', function close() {
    this.sockets.splice(this.sockets.indexOf(socket), 1);
  }.bind(this));
};

/**
 * Start listening on a something. Accepts what ever a plain net.Server accepts.
 *
 * @param {String} path Location of the socket;
 * @param {Function} fn Callback function
 * @api public
 */
Hamock.prototype.listen = function listen(path, fn) {
  if ('function' === typeof path) {
    fn = path;
    path = null;
  }

  this.server(path || '/tmp/haproxy.sock', fn);
  return this;
};

/**
 * Closes the server and kills all the connections.
 *
 * @param {Function} fn Callback when everthing is cleaned.
 * @api public
 */
Hamock.prototype.close = function close(fn) {
  this.sockets.forEach(function each(socket) {
    socket.end();

    if (socket.unref) socket.unref();
  });

  this.server.close(fn);
  return this;
};<commit_msg>[fix] Automatically unlink the socket or we will EADDRINSUE [fix] Allow half open sockets.<commit_after>'use strict';

var net = require('net')
  , fs = require('fs');

/**
 * Create new HA proxy mock server.
 *
 * @param {Object} options
 * @api public
 */
exports.createServer = function createServer(options) {
  return new Hamock(options || {});
};

/**
 * A HAProxy unix-socket mockup server.
 *
 * @constructor
 * @param {Object} options
 * @api private
 */
function Hamock(options) {
  this.server = options.server || net.createServer({ allowHalfOpen: true });
  this.server.on('connection', this.handle.bind(this));
  this.commands = options.commands || require('./fixtures');
  this.socket = '/tmp/haproxy.sock';
  this.sockets = [];
}

/**
 * Handles requests.
 *
 * @param {Socket} socket TCP socket of the connection.
 * @api private
 */
Hamock.prototype.handle = function handle(socket) {
  this.sockets.push(socket);

  var command = '';
  socket.setEncoding('utf-8');

  socket.on('data', function data(buffer) {
    command += buffer;
  }).on('end', function end() {
    var handler = command.trim()
      , data = this.commands[handler];

    socket.end(data || 'Unkown command');
  }.bind(this));

  socket.once('close', function close() {
    this.sockets.splice(this.sockets.indexOf(socket), 1);
  }.bind(this));
};

/**
 * Start listening on a something. Accepts what ever a plain net.Server accepts.
 *
 * @param {String} path Location of the socket;
 * @param {Function} fn Callback function
 * @api public
 */
Hamock.prototype.listen = function listen(path, fn) {
  if ('function' === typeof path) {
    fn = path;
    path = null;
  }

  if (path) this.socket = path;

  fs.unlinkSync(this.socket);
  this.server.listen(this.socket, fn);
  return this;
};

/**
 * Closes the server and kills all the connections.
 *
 * @param {Function} fn Callback when everthing is cleaned.
 * @api public
 */
Hamock.prototype.close = function close(fn) {
  this.sockets.forEach(function each(socket) {
    socket.end();

    if (socket.unref) socket.unref();
  });

  this.server.close(function closed() {
    fs.unlinkSync(this.socket);

    if (fn) fn.apply(fn, arguments);
  }.bind(this));

  return this;
};
{'fraction_non_alphanumeric': 0.1623455294402714, 'fraction_numerical': 0.000969227041434456, 'mean_word_length': 3.57649667405765, 'pattern_counts': {'":': 0, '<': 3, '<?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': '5345738', 'n_tokens_mistral': 1337, 'n_tokens_neox': 1303, 'n_words': 348}