<commit_before>(function () {
  'use strict';

  var http = require('http')
    , net = require('net')
    , express = require('express')
    , fs = require('fs')
    , exec = require('child_process').exec
    , app = express()
    , config = require('./config')
    , superPort
    , superServer
    , connectionPool = {}
    ;

  app.use(express.basicAuth(config.username, config.password));
  app.use(express.json());
  // TODO transmit over https
  app.use('/new.pem', function (req, res) {
    var filename = Math.random() + '.pem'
      ;

    res.setHeader('Content-Type', 'application/x-pem-file');
    exec('openssl genrsa -out ' + filename + ' 1024', function () {
      fs.createReadStream(filename).on('end', function () {
        fs.unlink(filename);
      }).pipe(res);
    });
  });
  app.use(function (req, res) {
    var token = Math.random().toString()
      ;

    connectionPool[token] = [];
    res.send({ port: superPort, token: token });
  });

  superServer = net.createServer(function (socket) {
    console.log('got master or client');
    socket.once('data', function (chunk) {
      console.log('got data from said socket');
      var data
        ;

      try {
        data = JSON.parse(chunk.toString('utf8'));
      } catch (e) {
        data = {};
      }

      if (!connectionPool[data.token] /*|| master.connection.remoteAddr !== connectionPool[token].remoteAddr*/) {
        socket.end('500 expected master connection');
        return;
      }

      function expandPool() {
        if (connectionPool[data.token].length < 3) {
          connectionPool[data.token].master.write('{ "cmd": "uno mas ' + connectionPool[data.token].length + '" }');
        }
      }

      if (!data.master) {
        expandPool();
        connectionPool[data.token].push(socket);
        return;
      }

      connectionPool[data.token].master = socket;
      expandPool();
      connectionPool[data.token].server = net.createServer(function (customer) {
        var client = connectionPool[data.token].shift()
          ;

        if (!client) {
          customer.end('500 server temporarily unavailable');
          return;
        }

        customer.pipe(client);
        client.pipe(customer);
      }).listen(function () {
        var port = this.address().port.toString()
          ;

        console.log('port', port);
        socket.write('{ "port": "' + port + '" }');
      });

      socket.on('close', function () {<commit_msg>fixes issue #1: replinish connection pool as it depletes<commit_after>(function () {
  'use strict';

  var http = require('http')
    , net = require('net')
    , express = require('express')
    , fs = require('fs')
    , exec = require('child_process').exec
    , app = express()
    , config = require('./config')
    , superPort
    , superServer
    , connectionPool = {}
    ;

  app.use(express.basicAuth(config.username, config.password));
  app.use(express.json());
  // TODO transmit over https
  app.use('/new.pem', function (req, res) {
    var filename = Math.random() + '.pem'
      ;

    res.setHeader('Content-Type', 'application/x-pem-file');
    exec('openssl genrsa -out ' + filename + ' 1024', function () {
      fs.createReadStream(filename).on('end', function () {
        fs.unlink(filename);
      }).pipe(res);
    });
  });
  app.use(function (req, res) {
    var token = Math.random().toString()
      ;

    connectionPool[token] = [];
    res.send({ port: superPort, token: token });
  });

  superServer = net.createServer(function (socket) {
    console.log('[*] new socket from behind the NAT');
    socket.once('data', function (chunk) {
      console.log('[D] got data from socket');
      var data
        ;

      try {
        data = JSON.parse(chunk.toString('utf8'));
      } catch (e) {
        data = {};
      }

      if (!connectionPool[data.token] /*|| master.connection.remoteAddr !== connectionPool[token].remoteAddr*/) {
        socket.end('500 expected master connection');
        return;
      }

      function expandPool() {
        if (connectionPool[data.token].length < 3) {
          connectionPool[data.token].master.write('{ "cmd": "uno mas ' + connectionPool[data.token].length + '" }');
        }
      }

      if (!data.master) {
        console.log('[S] socket is a slave');
        expandPool();
        connectionPool[data.token].push(socket);
        return;
      }
      console.log('[MASTER] socket is a master');

      connectionPool[data.token].master = socket;
      expandPool();
      connectionPool[data.token].server = net.createServer(function (customer) {
        var client = connectionPool[data.token].shift()
          ;

        expandPool();

        if (!client) {
          customer.end('500 server temporarily unavailable');
          return;
        }

        customer.pipe(client);
        client.pipe(customer);
      }).listen(function () {
        var port = this.address().port.toString()
          ;

        console.log('port', port);
        socket.write('{ "port": "' + port + '" }');
      });

      socket.on('close', function () {
{'fraction_non_alphanumeric': 0.1663723759073965, 'fraction_numerical': 0.004904845987835982, 'mean_word_length': 2.371693121693122, 'pattern_counts': {'":': 4, '<': 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': '7415599', 'n_tokens_mistral': 1520, 'n_tokens_neox': 1467, 'n_words': 294}