<commit_before>'use strict';

var _       = require('lodash'),
    http    = require('http'),
    express = require('express'),
    router  = require('./router'),
    util    = require('./util');

module.exports = {
  /**
   * Patches an Express Application to support Swagger.
   * @param {SwaggerServer} server
   */
  patch: function(server) {
    var app = server.app;
    var set = app.set;

    /**
     * Emit the "set" event whenever a setting is changed
     */
    app.set = function(name, value) {
      // Track the old/new value of the setting
      var oldValue = set.call(app, name);
      var result = set.apply(app, arguments);
      var newValue = set.call(app, name);

      // If it changed, then emit the "set" event
      if (oldValue !== newValue) {
        server.emit('set', name, newValue, oldValue);
      }

      return result;
    };

    /**
     * Bubble app errors up to the Swagger Server.
     */
    app.on('error', function() {
      server.emit.apply(server, ['error'].concat(_.slice(arguments)));
    });

    /**
     * Starts listening for connections, once the Swagger API is done being parsed.
     * If no port number is given, then the port number in the Swagger API will be used.
     * @returns {http.Server}
     */
    app.listen = function(port) {
      var args = _.isNumber(port) ? _.drop(arguments, 1) : _.drop(arguments, 0);

      var httpServer = http.createServer(app);

      // Wait until parsing is done
      server.__parser.whenParsed(function(err, api, metadata) {
        if (!_.isNumber(port)) {
          // No port number was provided, so get it from the Swagger API
          var host = api ? api.host || '' : '';
          var hostMatches = host.match(/[^:]+(?:\:(\d+))?$/); // ['hostname', 'port']
          if (hostMatches && hostMatches.length === 2) {
            port = parseInt(hostMatches[1]);
          }
        }

        util.debug('Starting HTTP server');
        httpServer.listen.apply(httpServer, [port].concat(args));
      });

      return httpServer;
    };

    /**
     * The {@link DataStore} object that's used by the mock middleware.
     * See https://github.com/BigstickCarpet/swagger-express-middleware/blob/master/docs/exports/DataStore.md
     *<commit_msg>Modified port selection to allow for named pipes<commit_after>'use strict';

var _       = require('lodash'),
    http    = require('http'),
    express = require('express'),
    router  = require('./router'),
    util    = require('./util');

module.exports = {
  /**
   * Patches an Express Application to support Swagger.
   * @param {SwaggerServer} server
   */
  patch: function(server) {
    var app = server.app;
    var set = app.set;

    /**
     * Emit the "set" event whenever a setting is changed
     */
    app.set = function(name, value) {
      // Track the old/new value of the setting
      var oldValue = set.call(app, name);
      var result = set.apply(app, arguments);
      var newValue = set.call(app, name);

      // If it changed, then emit the "set" event
      if (oldValue !== newValue) {
        server.emit('set', name, newValue, oldValue);
      }

      return result;
    };

    /**
     * Bubble app errors up to the Swagger Server.
     */
    app.on('error', function() {
      server.emit.apply(server, ['error'].concat(_.slice(arguments)));
    });

    /**
     * Starts listening for connections, once the Swagger API is done being parsed.
     * If no port number is given, then the port number in the Swagger API will be used.
     * @returns {http.Server}
     */
    app.listen = function(port) {
      var args = _.isUndefined(port) ? _.drop(arguments, 1) : _.drop(arguments, 0);

      var httpServer = http.createServer(app);

      // Wait until parsing is done
      server.__parser.whenParsed(function(err, api, metadata) {
      if (!_.isNumber(port)) {
          // No port number was provided, so get it from the Swagger API
          var host = api ? api.host || '' : '';
          var hostMatches = host.match(/[^:]+(?:\:(\d+))?$/); // ['hostname', 'port']
          if (hostMatches && hostMatches.length === 2) {
            port = parseInt(hostMatches[1]);
          }
        }

        util.debug('Starting HTTP server');
        httpServer.listen.apply(httpServer, _.union([port], args));
      });

      return httpServer;
    };

    /**
     * The {@link DataStore} object that's used by the mock middleware.
     * See https://github.com/BigstickCarpet/swagger-express-middleware/blob/master/docs/exports/DataStore.md
     *
{'fraction_non_alphanumeric': 0.1440193491644679, 'fraction_numerical': 0.001759014951627089, 'mean_word_length': 2.3180160466812545, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 2, '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': '5479472', 'n_tokens_mistral': 1368, 'n_tokens_neox': 1330, 'n_words': 388}