<commit_before>// Load modules

var Fs = require('fs');
var Http = require('http');
var Https = require('https');
var NodeUtil = require('util');
var Events = require('events');
var Shot = require('shot');
var Utils = require('./utils');
var Err = require('./error');
var Log = require('./log');
var Defaults = require('./defaults');
var Monitor = require('./monitor');
var Session = require('./session');
var Cache = require('./cache');
var Request = require('./request');
var Route = require('./route');
var Debug = require('./debug');


// Declare internals

var internals = {};


// Create and configure server instance

module.exports = internals.Server = function (host, port, options) {

    var self = this;

    Utils.assert(this.constructor === internals.Server, 'Server must be instantiated using new');

    // Register as event emitter
    Events.EventEmitter.call(this);

    // Set basic configuration

    this.settings = Utils.applyToDefaults(Defaults.server, options);
    this.settings.host = host.toLowerCase();
    this.settings.port = port;
    this.settings.name = (this.settings.name ? this.settings.name.toLowerCase() : (this.settings.host + ':' + this.settings.port));
    this.settings.uri = (this.settings.tls ? 'https://' : 'http://') + this.settings.host + ':' + this.settings.port;

    // Set optional configuration
    // false -> null, true -> defaults, {} -> override defaults

    this.settings.monitor = Utils.applyToDefaults(Defaults.monitor, this.settings.monitor);
    this.settings.authentication = Utils.applyToDefaults(Defaults.authentication, this.settings.authentication);
    this.settings.cache = Utils.applyToDefaults(Defaults.cache, this.settings.cache);
    this.settings.debug = Utils.applyToDefaults(Defaults.debug, this.settings.debug);

    // Validate configuration

    Utils.assert(!this.settings.ext.onUnknownRoute || !(this.settings.ext.onUnknownRoute instanceof Array), 'ext.onUnknownRoute cannot be an array');

    // Initialize process monitoring

    if (this.settings.monitor) {
        this._monitor = new Monitor(this);
        Log.event(['info', 'config'], this.settings.name + ': Monitoring enabled');
    }

    // Generate CORS headers

    if (this.settings.cors) {

        this.settings.cors._origin = (this.settings.cors.origin || []).join(' ');
        this.settings.cors._headers = (this.settings.cors.headers || []).concat(this.settings.cors.additionalHeaders || []).join(', ');
        this.settings.cors._methods = (this.settings.cors.methods || []).concat(this.settings.cors.additionalMethods || []).join(', ');
    }

    // Create routing table

    this._routes = {};
    this.routeDefaults = null;

    // Create server

    if (this.settings.tls) {
        var tls = {
            key: Fs.readFileSync(this.settings.tls.key),
            cert: Fs.readFileSync(this.settings.tls.cert)
        };

        this.listener = Https.createServer(tls, this._dispatch());
    }
    else {
        this.listener = Http.createServer(this._dispatch());
    }

    // Setup authentication

    if (this.settings.authentication) {
        Utils.assert(this.settings.authentication.tokenEndpoint &&
                     this.settings.authentication.loadClientFunc &&
                     this.settings.authentication.loadUserFunc &&
                     this.settings.authentication.checkAuthorizationFunc &&
                     this.settings.authentication.aes256Keys.oauthRefresh &&
                     this.settings.authentication.aes256Keys.oauthToken, 'Invalid authentication configuration');

        this.addRoute({
            method: 'POST',
            path: this.settings.authentication.tokenEndpoint,
            config: Session.token
        });

        Log.event(['info', 'config'], this.settings.name + ': Authentication enabled');
    }

    // Initialize cache engine

    if (this.settings.cache) {
        this.cache = new Cache.Client(this.settings.cache);
        this.cache.on('ready', function (err) {

            Utils.assert(!err, 'Failed to initialize cache engine: ' + err);
        });

        Log.event(['info', 'config'], this.settings.name + ': Caching enabled');
    }
    else {
        this.cache = null;
    }

    // Setup debug endpoint

    if (this.settings.debug) {

        this._debugConsole = new Debug.Console(this, this.settings.debug);
        this.addRoute({ 
            method: 'GET',
            path: this.settings.debug.debugEndpoint,
            config: this._debugConsole.endpoint()
        });

        Log.event(['info', 'config'], this.settings.name + ': Debug console enabled');
    }

    return this;
};

NodeUtil.inherits(internals.Server, Events.EventEmitter);


internals.Server.prototype._dispatch = function (options) {

    var self = this;

    return function (req, res) {

        // Create request object
        var request = new Request(self, req, res, options);

        // Execute onRequest extensions (can change request method and url)

        request._onRequestExt(self.settings.ext.onRequest, function () {

            // Lookup route

            var method = (request.method === 'head' ? 'get' : request.method);
            var routes = self._routes[method];
            if (routes) {
                for (var i = 0, il = routes.length; i < il; ++i) {
                    var route = routes[i];
                    if (route.match(request)) {
                        return request._execute(route);
                    }
                }
            }

            request._execute();
        });
    };
};


// Find a route match

internals.Server.prototype.match = function (method, path) {

    // Lookup route

    method = method.toLowerCase();
    method = (method === 'head' ? 'get' : method);
    var routes = this._routes[method];
    if (routes) {
        for (var i = 0, il = routes.length; i < il; ++i) {
            var route = routes[i];
            if (route.test(path)) {
                return route;
            }
        }
    }

    return null;
};


// Start server listener

internals.Server.prototype.start = function () {

    this.listener.listen(this.settings.port, this.settings.host);
    Log.event('info', this.settings.name + ': Instance started at ' + this.settings.uri);
};

<commit_msg>Forgot to save final changes...<commit_after>// Load modules

var Fs = require('fs');
var Http = require('http');
var Https = require('https');
var NodeUtil = require('util');
var Events = require('events');
var Shot = require('shot');
var Utils = require('./utils');
var Err = require('./error');
var Log = require('./log');
var Defaults = require('./defaults');
var Monitor = require('./monitor');
var Session = require('./session');
var Cache = require('./cache');
var Request = require('./request');
var Route = require('./route');
var Debug = require('./debug');


// Declare internals

var internals = {};


// Create and configure server instance

module.exports = internals.Server = function (host, port, options) {

    var self = this;

    Utils.assert(this.constructor === internals.Server, 'Server must be instantiated using new');

    // Register as event emitter
    Events.EventEmitter.call(this);

    // Set basic configuration

    this.settings = Utils.applyToDefaults(Defaults.server, options);
    this.settings.host = host.toLowerCase();
    this.settings.port = port;
    this.settings.name = (this.settings.name ? this.settings.name.toLowerCase() : (this.settings.host + ':' + this.settings.port));
    this.settings.uri = (this.settings.tls ? 'https://' : 'http://') + this.settings.host + ':' + this.settings.port;

    // Set optional configuration
    // false -> null, true -> defaults, {} -> override defaults

    this.settings.monitor = Utils.applyToDefaults(Defaults.monitor, this.settings.monitor);
    this.settings.authentication = Utils.applyToDefaults(Defaults.authentication, this.settings.authentication);
    this.settings.cache = Utils.applyToDefaults(Defaults.cache, this.settings.cache);
    this.settings.debug = Utils.applyToDefaults(Defaults.debug, this.settings.debug);

    // Validate configuration

    Utils.assert(!this.settings.ext.onUnknownRoute || !(this.settings.ext.onUnknownRoute instanceof Array), 'ext.onUnknownRoute cannot be an array');

    // Initialize process monitoring

    if (this.settings.monitor) {
        this._monitor = new Monitor(this);
        Log.event(['info', 'config'], this.settings.name + ': Monitoring enabled');
    }

    // Generate CORS headers

    if (this.settings.cors) {

        this.settings.cors._origin = (this.settings.cors.origin || []).join(' ');
        this.settings.cors._headers = (this.settings.cors.headers || []).concat(this.settings.cors.additionalHeaders || []).join(', ');
        this.settings.cors._methods = (this.settings.cors.methods || []).concat(this.settings.cors.additionalMethods || []).join(', ');
    }

    // Create routing table

    this._routes = {};
    this.routeDefaults = null;

    // Create server

    if (this.settings.tls) {
        var tls = {
            key: Fs.readFileSync(this.settings.tls.key),
            cert: Fs.readFileSync(this.settings.tls.cert)
        };

        this.listener = Https.createServer(tls, this._dispatch());
    }
    else {
        this.listener = Http.createServer(this._dispatch());
    }

    // Setup authentication

    if (this.settings.authentication) {
        Utils.assert(this.settings.authentication.tokenEndpoint &&
                     this.settings.authentication.loadClientFunc &&
                     this.settings.authentication.loadUserFunc &&
                     this.settings.authentication.checkAuthorizationFunc &&
                     this.settings.authentication.aes256Keys.oauthRefresh &&
                     this.settings.authentication.aes256Keys.oauthToken, 'Invalid authentication configuration');

        this.addRoute({
            method: 'POST',
            path: this.settings.authentication.tokenEndpoint,
            config: Session.token
        });

        Log.event(['info', 'config'], this.settings.name + ': Authentication enabled');
    }

    // Initialize cache engine

    if (this.settings.cache) {
        this.cache = new Cache.Client(this.settings.cache);
        this.cache.on('ready', function (err) {

            Utils.assert(!err, 'Failed to initialize cache engine: ' + err);
        });

        Log.event(['info', 'config'], this.settings.name + ': Caching enabled');
    }
    else {
        this.cache = null;
    }

    // Setup debug endpoint

    if (this.settings.debug) {

        this._debugConsole = new Debug.Console(this, this.settings.debug);
        this.addRoute({ 
            method: 'GET',
            path: this.settings.debug.debugEndpoint,
            config: this._debugConsole.endpoint()
        });

        Log.event(['info', 'config'], this.settings.name + ': Debug console enabled');
    }

    return this;
};

NodeUtil.inherits(internals.Server, Events.EventEmitter);


internals.Server.prototype._dispatch = function (options) {

    var self = this;

    return function (req, res) {

        // Create request object
        var request = new Request(self, req, res, options);

        // Execute onRequest extensions (can change request method and url)

        request._onRequestExt(self.settings.ext.onRequest, function () {

            // Lookup route

            var method = (request.method === 'head' ? 'get' : request.method);
            var routes = self._routes[method];
            if (routes) {
                for (var i = 0, il = routes.length; i < il; ++i) {
                    var route = routes[i];
                    if (route.match(request)) {
                        return request._execute(route);
                    }
                }
            }

            request._execute();
        });
    };
};


// Find a route match

internals.Server.prototype._match = function (method, path) {

    // Lookup route

    method = method.toLowerCase();
    method = (method === 'head' ? 'get' : method);
    var routes = this._routes[method];
    if (routes) {
        for (var i = 0, il = routes.length; i < il; ++i) {
            var route = routes[i];
            if (route.test(path)) {
                return route;
            }
        }
    }

    return null;
};


// Start server listener

internals.Server.prototype.start = function () {

    this.listener.listen(this.settings.port, this.settings.host);
    Log.event('info', this.settings.name + ': Instance started at ' + this.settings.uri);
};


{'fraction_non_alphanumeric': 0.1436749622326469, 'fraction_numerical': 0.0012721634730062813, 'mean_word_length': 2.4592959295929595, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 9, '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': '6132919', 'n_tokens_mistral': 3564, 'n_tokens_neox': 3401, 'n_words': 760}