<commit_before>var net = require('net');
var tls = require('tls');

var initialize = function(api, options, next){

  //////////
  // INIT //
  //////////

  var type = 'socket';
  var attributes = {
    canChat: true,
    logConnections: true,
    logExits: true,
    pendingShutdownWaitLimit: 5000,
    sendWelcomeMessage: true,
    verbs: [
      'quit',
      'exit',
      'documentation',
      'paramAdd',
      'paramDelete',
      'paramView',
      'paramsView',
      'paramsDelete',
      'roomAdd',
      'roomLeave',
      'roomView',
      'detailsView',
      'say'
    ]
  };

  var server = new api.genericServer(type, options, attributes);

  //////////////////////
  // REQUIRED METHODS //
  //////////////////////

  server.start = function(next){
    if(options.secure === false){
      server.server = net.createServer(api.config.servers.socket.serverOptions, function(rawConnection){
        handleConnection(rawConnection);
      });
    }else{
      server.server = tls.createServer(api.config.servers.socket.serverOptions, function(rawConnection){
        handleConnection(rawConnection);
      });
    }

    server.server.on('error', function(e){
      return next(new Error('Cannot start socket server @ ' + options.bindIP + ':' + options.port + ' => ' + e.message));
    });

    server.server.listen(options.port, options.bindIP, function(){
      process.nextTick(function(){
        next();
      });
    });
  };

  server.stop = function(next){
    gracefulShutdown(next);
  };

  server.sendMessage = function(connection, message, messageCount){
    if(message.error){
      message.error = api.config.errors.serializers.servers.socket(message.error);
    }

    if(connection.respondingTo){
      message.messageCount = messageCount;
      connection.respondingTo = null;
    }else if(message.context === 'response'){
      if(messageCount){
        message.messageCount = messageCount;
      }else{
        message.messageCount = connection.messageCount;
      }
    }
    try{
      connection.rawConnection.write(JSON.stringify(message) + '\r\n');
    }catch(e){
      api.log(['socket write error: %s', e], 'error');
    }
  };

  server.goodbye = function(connection){
    try{
      connection.rawConnection.end(JSON.stringify({status: connection.localize(api.config.servers.socket.goodbyeMessage), context: 'api'}) + '\r\n');
    }catch(e){}
  };

  server.sendFile = function(connection, error, fileStream){
    if(error){
      server.sendMessage(connection, error, connection.messageCount);
    }else{
      fileStream.pipe(connection.rawConnection, {end: false});
    }
  };

  ////////////
  // EVENTS //
  ////////////

  server.on('connection', function(connection){
    connection.params = {};

    var parseLine = function(line){
      var blen = Buffer.byteLength(line, 'utf8');
      if(blen > api.config.servers.socket.maxDataLength && api.config.servers.socket.maxDataLength !== 0){
        var error = api.config.errors.dataLengthTooLarge(api.config.servers.socket.maxDataLength,blen);
        server.log(error, 'error');
        server.sendMessage(connection, {status:'error', error: error, context: 'response'});
        return;
      }
      if(blen > 0){
        // increment at the start of the request so that responses can be caught in order on the client
        // this is not handled by the genericServer
        connection.messageCount++;
        parseRequest(connection, line);
      }
    };

    connection.rawConnection.on('data', function(chunk){
      if(checkBreakChars(chunk)){
        connection.destroy();
      }else{
        // Replace all carriage returns with newlines.
        connection.rawConnection.socketDataString += chunk.toString('utf-8').replace(/\r/g, '\n');
        var index;<commit_msg>Data byteLength is now calculated only If needed.<commit_after>var net = require('net');
var tls = require('tls');

var initialize = function(api, options, next){

  //////////
  // INIT //
  //////////

  var type = 'socket';
  var attributes = {
    canChat: true,
    logConnections: true,
    logExits: true,
    pendingShutdownWaitLimit: 5000,
    sendWelcomeMessage: true,
    verbs: [
      'quit',
      'exit',
      'documentation',
      'paramAdd',
      'paramDelete',
      'paramView',
      'paramsView',
      'paramsDelete',
      'roomAdd',
      'roomLeave',
      'roomView',
      'detailsView',
      'say'
    ]
  };

  var server = new api.genericServer(type, options, attributes);

  //////////////////////
  // REQUIRED METHODS //
  //////////////////////

  server.start = function(next){
    if(options.secure === false){
      server.server = net.createServer(api.config.servers.socket.serverOptions, function(rawConnection){
        handleConnection(rawConnection);
      });
    }else{
      server.server = tls.createServer(api.config.servers.socket.serverOptions, function(rawConnection){
        handleConnection(rawConnection);
      });
    }

    server.server.on('error', function(e){
      return next(new Error('Cannot start socket server @ ' + options.bindIP + ':' + options.port + ' => ' + e.message));
    });

    server.server.listen(options.port, options.bindIP, function(){
      process.nextTick(function(){
        next();
      });
    });
  };

  server.stop = function(next){
    gracefulShutdown(next);
  };

  server.sendMessage = function(connection, message, messageCount){
    if(message.error){
      message.error = api.config.errors.serializers.servers.socket(message.error);
    }

    if(connection.respondingTo){
      message.messageCount = messageCount;
      connection.respondingTo = null;
    }else if(message.context === 'response'){
      if(messageCount){
        message.messageCount = messageCount;
      }else{
        message.messageCount = connection.messageCount;
      }
    }
    try{
      connection.rawConnection.write(JSON.stringify(message) + '\r\n');
    }catch(e){
      api.log(['socket write error: %s', e], 'error');
    }
  };

  server.goodbye = function(connection){
    try{
      connection.rawConnection.end(JSON.stringify({status: connection.localize(api.config.servers.socket.goodbyeMessage), context: 'api'}) + '\r\n');
    }catch(e){}
  };

  server.sendFile = function(connection, error, fileStream){
    if(error){
      server.sendMessage(connection, error, connection.messageCount);
    }else{
      fileStream.pipe(connection.rawConnection, {end: false});
    }
  };

  ////////////
  // EVENTS //
  ////////////

  server.on('connection', function(connection){
    connection.params = {};

    var parseLine = function(line){
      if(api.config.servers.socket.maxDataLength !== 0){
        var blen = Buffer.byteLength(line, 'utf8');
        if( blen > api.config.servers.socket.maxDataLength){
          var error = api.config.errors.dataLengthTooLarge(api.config.servers.socket.maxDataLength,blen);
          server.log(error, 'error');
          server.sendMessage(connection, {status:'error', error: error, context: 'response'});
          return;
        }
      }
      if(line.length > 0){
        // increment at the start of the request so that responses can be caught in order on the client
        // this is not handled by the genericServer
        connection.messageCount++;
        parseRequest(connection, line);
      }
    };

    connection.rawConnection.on('data', function(chunk){
      if(checkBreakChars(chunk)){
        connection.destroy();
      }else{
        // Replace all carriage returns with newlines.
        connection.rawConnection.socketDataString += chunk.toString('utf-8').replace(/\r/g, '\n');
        var index;
{'fraction_non_alphanumeric': 0.16151832460732984, 'fraction_numerical': 0.0020942408376963353, 'mean_word_length': 2.585640544345378, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 9, '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': '4580068', 'n_tokens_mistral': 2229, 'n_tokens_neox': 2181, 'n_words': 419}