<commit_before>var net = require('net'),
  BSON = require('bson'),
  Snappy = require('snappy'),
  zlib = require('zlib'),
  MESSAGE_HEADER_SIZE = require('../../../lib/wireprotocol/shared').MESSAGE_HEADER_SIZE,
  opcodes = require('../../../lib/wireprotocol/shared').opcodes,
  compressorIDs = require('../../../lib/wireprotocol/compression').compressorIDs,
  Request = require('./request'),
  Query = require('./protocol').Query,
  GetMore = require('./protocol').GetMore,
  KillCursor = require('./protocol').KillCursor,
  Insert = require('./protocol').Insert,
  Update = require('./protocol').Update,
  Delete = require('./protocol').Delete,
  EventEmitter = require('events').EventEmitter,
  inherits = require('util').inherits;

/*
 * Server class
 */
var Server = function(port, host, options) {
  EventEmitter.call(this);

  // Special handlers
  options = options || {};

  // Do we have an onRead function
  this.onRead = typeof options.onRead == 'function' ? options.onRead : null;

  // Create a bson instance
  this.bson = new BSON();
  // Save the settings
  this.host = host;
  this.port = port;
  // Create a server socket
  this.socket = net.createServer();
  // Responses
  this.messages = [];
  // state
  this.state = 'stopped';
  // Number of connections
  this.connections = 0;
  // sockets
  this.sockets = [];
};

inherits(Server, EventEmitter);

Server.prototype.destroy = function() {
  this.state = 'destroyed';
  this.socket.close();

  this.sockets.forEach(function(x) {
    x.destroy();
  });
};

Server.prototype.start = function() {
  var self = this;

  // Return start promise
  return new Promise(function(resolve, reject) {
    self.socket.on('error', function(err) {
      console.log('!!!!!!!!!!!!!!!!!!!! error reject');
      reject(err);
    });

    self.socket.on('connection', function(c) {
      self.connections = self.connections + 1;
      self.sockets.push(c);

      c.on('error', function(e) {
        console.warn('connection error: ', e);
      });

      c.on(
        'data',
        dataHandler(
          self,
          {
            buffer: new Buffer(0),
            stubBuffer: new Buffer(0),
            sizeOfMessage: 0,
            bytesRead: 0,
            maxBsonMessageSize: 1024 * 1024 * 48
          },
          c
        )
      );

      c.on('close', function() {
        self.connections = self.connections - 1;
        var index = self.sockets.indexOf(c);

        if (index != -1) {
          self.sockets.splice(index, 1);
        }
      });
    });

    self.socket.listen(self.port, self.host, function() {
      resolve(self);
    });

    self.on('message', function(message, connection) {
      var request = new Request(self, connection, message);
      self.messages.push(request);
    });

    self.state = 'running';
  });
};
<commit_msg>refactor(mock-server): rename `socket` => `server`<commit_after>var net = require('net'),
  BSON = require('bson'),
  Snappy = require('snappy'),
  zlib = require('zlib'),
  MESSAGE_HEADER_SIZE = require('../../../lib/wireprotocol/shared').MESSAGE_HEADER_SIZE,
  opcodes = require('../../../lib/wireprotocol/shared').opcodes,
  compressorIDs = require('../../../lib/wireprotocol/compression').compressorIDs,
  Request = require('./request'),
  Query = require('./protocol').Query,
  GetMore = require('./protocol').GetMore,
  KillCursor = require('./protocol').KillCursor,
  Insert = require('./protocol').Insert,
  Update = require('./protocol').Update,
  Delete = require('./protocol').Delete,
  EventEmitter = require('events').EventEmitter,
  inherits = require('util').inherits;

/*
 * Server class
 */
var Server = function(port, host, options) {
  EventEmitter.call(this);

  // Special handlers
  options = options || {};

  // Do we have an onRead function
  this.onRead = typeof options.onRead == 'function' ? options.onRead : null;

  // Create a bson instance
  this.bson = new BSON();
  // Save the settings
  this.host = host;
  this.port = port;
  // Create a server socket
  this.server = net.createServer();
  // Responses
  this.messages = [];
  // state
  this.state = 'stopped';
  // Number of connections
  this.connections = 0;
  // sockets
  this.sockets = [];
};

inherits(Server, EventEmitter);

Server.prototype.destroy = function() {
  this.state = 'destroyed';
  this.server.close();

  this.sockets.forEach(function(x) {
    x.destroy();
  });
};

Server.prototype.start = function() {
  var self = this;

  // Return start promise
  return new Promise(function(resolve, reject) {
    self.server.on('error', function(err) {
      console.log("!!!!!!!!!!!!!!!!!!!! error reject")
      reject(err);
    });

    self.server.on('connection', function(c) {
      self.connections = self.connections + 1;
      self.sockets.push(c);

      c.on('error', function(e) {
        console.warn('connection error: ', e);
      });

      c.on(
        'data',
        dataHandler(
          self,
          {
            buffer: new Buffer(0),
            stubBuffer: new Buffer(0),
            sizeOfMessage: 0,
            bytesRead: 0,
            maxBsonMessageSize: 1024 * 1024 * 48
          },
          c
        )
      );

      c.on('close', function() {
        self.connections = self.connections - 1;
        var index = self.sockets.indexOf(c);

        if (index != -1) {
          self.sockets.splice(index, 1);
        }
      });
    });

    self.server.listen(self.port, self.host, function() {
      resolve(self);
    });

    self.on('message', function(message, connection) {
      var request = new Request(self, connection, message);
      self.messages.push(request);
    });

    self.state = 'running';
  });
};

{'fraction_non_alphanumeric': 0.17317158931082982, 'fraction_numerical': 0.006680731364275668, 'mean_word_length': 2.56900878293601, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 4, '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': '3184704', 'n_tokens_mistral': 1774, 'n_tokens_neox': 1698, 'n_words': 353}