<commit_before>var EventEmitter = require("events").EventEmitter,
    http         = require("http"),
    https        = require("https"),
    url          = require("url");

var plugins;

function defaultPlugin (options) {
    options.autostart = "autostart" in options ? options.autostart : true;
    options.hostname  = options.hostname || "localhost";
    options.path      = options.path || "/";
    options.port      = "port" in options ? options.port : 0;
    options.scheme    = options.scheme || "http";
    return options;
}

var createServer = function (options, handler) {
    
    var api = new EventEmitter(),
        ports, schemes, server;
    
    function emitError (error) {
        process.nextTick(function () {
            api.emit("error", error);
        });
    }
    
    function getPorts (descriptor) {
        if (typeof descriptor === "number") {
            descriptor = {
                private : descriptor,
                public  : descriptor
            };
        }
        
        return descriptor;
    }
    
    function createServer (options) {
        var validSchemes = [ "http", "https" ],
            server;
        
        schemes = getSchemes(options.scheme);
        if (
            validSchemes.indexOf(schemes.private) < 0 ||
            validSchemes.indexOf(schemes.public) < 0
        ) {
            throw new Error("Server scheme must be 'http' or 'https'.");
        }
        
        if (schemes.private === "https") {
            server = https.createServer(
                {
                    key  : options.key,
                    cert : options.cert
                },
                handler
            );
        }
        else {
            server = http.createServer(handler);
        }
        
        return server;
    }
    
    function getSchemes (descriptor) {
        if (typeof descriptor === "string") {
            descriptor = {
                private : descriptor,
                public  : descriptor
            };
        }
        
        return descriptor;
    }
    
    function registerCallback (callback, events) {
        function handler () {
            events.forEach(function (event) {
                api.removeListener(event, handler);
            });
            return callback.apply(this, arguments);
        }
        
        events.forEach(function (event) {
            api.once(event, handler);
        });
    }
    
    function started () {
        // Convert object to boolean.
        return !! server.address();
    }
    
    // Argument wrangling...
    if (typeof options === "function") {
        handler = options;
        options = null;
    }
    // Defensive copy to protect caller options object.
    options = Object.create(options || null);
    // Compute default argument values.
    plugins.concat(defaultPlugin).forEach(function (plugin) {
        options = plugin(options);
    });
    
    server = createServer(options);
    // Forward low-level errors.
    server.on("error", function (error) {
        api.emit("error", error);
    });
    
    api.server = function () {
        return Object.create(server);
    };
    
    api.start = function (port, callback) {
        if (typeof arguments[0] === "function") {
            callback = port;
            port     = options.port;
        }
        if (arguments.length === 0) {
            port = options.port;
        }
        
        if (typeof callback === "function") {
            registerCallback(callback, [ "ready", "error" ]);
        }
        
        if (started()) {
            emitError(new Error(
                "Server cannot be started if already listening."
            ));
        }
        else {
            ports = getPorts(port);
            server.listen(ports.private, function () {
                api.emit("ready");
            });
        }
        
        return this;
    };
    
    api.stop = function (callback) {
        if (typeof callback === "function") {
            registerCallback(callback, [ "stopped", "error" ]);
        }
        
        if (! started()) {
            emitError(new Error("Server cannot be stopped if not listening."));
        }
        else {
            server.close(function () {<commit_msg>Rename `registerCallback` to `oneTimeCallback` for readability.<commit_after>var EventEmitter = require("events").EventEmitter,
    http         = require("http"),
    https        = require("https"),
    url          = require("url");

var plugins;

function defaultPlugin (options) {
    options.autostart = "autostart" in options ? options.autostart : true;
    options.hostname  = options.hostname || "localhost";
    options.path      = options.path || "/";
    options.port      = "port" in options ? options.port : 0;
    options.scheme    = options.scheme || "http";
    return options;
}

var createServer = function (options, handler) {
    
    var api = new EventEmitter(),
        ports, schemes, server;
    
    function emitError (error) {
        process.nextTick(function () {
            api.emit("error", error);
        });
    }
    
    function getPorts (descriptor) {
        if (typeof descriptor === "number") {
            descriptor = {
                private : descriptor,
                public  : descriptor
            };
        }
        
        return descriptor;
    }
    
    function createServer (options) {
        var validSchemes = [ "http", "https" ],
            server;
        
        schemes = getSchemes(options.scheme);
        if (
            validSchemes.indexOf(schemes.private) < 0 ||
            validSchemes.indexOf(schemes.public) < 0
        ) {
            throw new Error("Server scheme must be 'http' or 'https'.");
        }
        
        if (schemes.private === "https") {
            server = https.createServer(
                {
                    key  : options.key,
                    cert : options.cert
                },
                handler
            );
        }
        else {
            server = http.createServer(handler);
        }
        
        return server;
    }
    
    function getSchemes (descriptor) {
        if (typeof descriptor === "string") {
            descriptor = {
                private : descriptor,
                public  : descriptor
            };
        }
        
        return descriptor;
    }
    
    function oneTimeCallback (callback, events) {
        function handler () {
            events.forEach(function (event) {
                api.removeListener(event, handler);
            });
            return callback.apply(this, arguments);
        }
        
        events.forEach(function (event) {
            api.once(event, handler);
        });
    }
    
    function started () {
        // Convert object to boolean.
        return !! server.address();
    }
    
    // Argument wrangling...
    if (typeof options === "function") {
        handler = options;
        options = null;
    }
    // Defensive copy to protect caller options object.
    options = Object.create(options || null);
    // Compute default argument values.
    plugins.concat(defaultPlugin).forEach(function (plugin) {
        options = plugin(options);
    });
    
    server = createServer(options);
    // Forward low-level errors.
    server.on("error", function (error) {
        api.emit("error", error);
    });
    
    api.server = function () {
        return Object.create(server);
    };
    
    api.start = function (port, callback) {
        if (typeof arguments[0] === "function") {
            callback = port;
            port     = options.port;
        }
        if (arguments.length === 0) {
            port = options.port;
        }
        
        if (typeof callback === "function") {
            oneTimeCallback(callback, [ "ready", "error" ]);
        }
        
        if (started()) {
            emitError(new Error(
                "Server cannot be started if already listening."
            ));
        }
        else {
            ports = getPorts(port);
            server.listen(ports.private, function () {
                api.emit("ready");
            });
        }
        
        return this;
    };
    
    api.stop = function (callback) {
        if (typeof callback === "function") {
            oneTimeCallback(callback, [ "stopped", "error" ]);
        }
        
        if (! started()) {
            emitError(new Error("Server cannot be stopped if not listening."));
        }
        else {
            server.close(function () {
{'fraction_non_alphanumeric': 0.11695291506017058, 'fraction_numerical': 0.0011683607898118939, 'mean_word_length': 1.4790037648421663, 'pattern_counts': {'":': 0, '<': 7, '<?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': '3163574', 'n_tokens_mistral': 2194, 'n_tokens_neox': 2165, 'n_words': 515}