<commit_before>/* global require, process */
(function () {
    var os = require('os'),
        fs = require('fs'),
        _ = require('lodash'),
        url = require('url');

    function maybeLoadConfigFile(path) {
        var content;
        try {
            content = fs.readFileSync(path);
        } catch (e) {
            console.error("Tried to load config file " + path + " but it doesn't exist.");
            return {};
        }
        return JSON.parse(content);
    }

    function ConfigBuilder() {
        this.objects = [];
    }

    ConfigBuilder.prototype.addObj = function (obj) {
        this.objects.push(obj);
    };

    ConfigBuilder.prototype.addFile = function (path) {
        this.objects.push(maybeLoadConfigFile(path));
    };

    ConfigBuilder.prototype.addDir = function (path) {
        this.objects.push(maybeLoadConfigFile(path + '/default.json'));
        if (process.env.NODE_ENV) {
            this.objects.push(maybeLoadConfigFile(path + '/' + process.env.NODE_ENV + '.json'));
        }
    };

    ConfigBuilder.prototype.build = function () {
        return _.merge.apply(null, [{}].concat(this.objects));
    };

    var defaultConfig = {
        "log4js": {
            "appenders": [
                {
                    "type": "console",
                    "layout": {
                        "type": "pattern",
                        "pattern": "%d{ISO8601} %h %c %p %m%n"
                    }
                }
            ]
        },
        "serviceName": "defaultService",
        "listenPort": 8001,
        "forwardPort": 8000,
        "forwardHost": "localhost",
        "configRefreshInterval": 60000,
        "configEndpoint": "/rate-limiter/",
        "bucketHeaderName": "X-RateLimiter-Bucket"
    };

    exports.updateDerivedSettings = function(settings) {
        settings.forwardUrl = "http://" + settings.forwardHost + ":" + settings.forwardPort;

        var configUrl = url.parse(settings.configEndpoint);
        if (configUrl.protocol === null) {
            configUrl = url.parse(settings.forwardUrl);
            configUrl.pathname = settings.configEndpoint;
            settings.fullConfigEndpoint = url.format(configUrl);
        } else {
            settings.fullConfigEndpoint = settings.configEndpoint;
        }
        return settings;
    };

    exports.load = function (extraConfigFile, hook) {
        var builder = new ConfigBuilder();
        builder.addObj(defaultConfig);
        builder.addDir("config");
        if (hook) { hook(builder); }
        if (extraConfigFile) {
            builder.addFile(extraConfigFile);
        }

        var settings = builder.build();
        return this.updateDerivedSettings(settings);
    };

    exports.init = function () {
        var opts = require('nomnom')
            .option('config', {
                abbr: 'c',
                default: null,
                help: 'Configuration file to use'
            })
            .option('version', {
                abbr: 'v',
                flag: true,
                help: 'Print version and exit',
                callback: function () {
                    return "reverse-proxy-rate-limiter version " + require('../../package.json').version;
                }
            })
            .parse();

        return this.load(opts.config);
    };
}());<commit_msg>Separate declaring and exposing functions in settings module<commit_after>/* global require, process */
(function () {
    var os = require('os'),
        fs = require('fs'),
        _ = require('lodash'),
        url = require('url');

    exports.init = init;
    exports.load = load;
    exports.updateDerivedSettings = updateDerivedSettings;

    function maybeLoadConfigFile(path) {
        var content;
        try {
            content = fs.readFileSync(path);
        } catch (e) {
            console.error("Tried to load config file " + path + " but it doesn't exist.");
            return {};
        }
        return JSON.parse(content);
    }

    function ConfigBuilder() {
        this.objects = [];
    }

    ConfigBuilder.prototype.addObj = function (obj) {
        this.objects.push(obj);
    };

    ConfigBuilder.prototype.addFile = function (path) {
        this.objects.push(maybeLoadConfigFile(path));
    };

    ConfigBuilder.prototype.addDir = function (path) {
        this.objects.push(maybeLoadConfigFile(path + '/default.json'));
        if (process.env.NODE_ENV) {
            this.objects.push(maybeLoadConfigFile(path + '/' + process.env.NODE_ENV + '.json'));
        }
    };

    ConfigBuilder.prototype.build = function () {
        return _.merge.apply(null, [{}].concat(this.objects));
    };

    var defaultConfig = {
        "log4js": {
            "appenders": [
                {
                    "type": "console",
                    "layout": {
                        "type": "pattern",
                        "pattern": "%d{ISO8601} %h %c %p %m%n"
                    }
                }
            ]
        },
        "serviceName": "defaultService",
        "listenPort": 8001,
        "forwardPort": 8000,
        "forwardHost": "localhost",
        "configRefreshInterval": 60000,
        "configEndpoint": "/rate-limiter/",
        "bucketHeaderName": "X-RateLimiter-Bucket"
    };

    function updateDerivedSettings(settings) {
        settings.forwardUrl = "http://" + settings.forwardHost + ":" + settings.forwardPort;

        var configUrl = url.parse(settings.configEndpoint);
        if (configUrl.protocol === null) {
            configUrl = url.parse(settings.forwardUrl);
            configUrl.pathname = settings.configEndpoint;
            settings.fullConfigEndpoint = url.format(configUrl);
        } else {
            settings.fullConfigEndpoint = settings.configEndpoint;
        }
        return settings;
    }

    function load(extraConfigFile, hook) {
        var builder = new ConfigBuilder();
        builder.addObj(defaultConfig);
        builder.addDir("config");
        if (hook) { hook(builder); }
        if (extraConfigFile) {
            builder.addFile(extraConfigFile);
        }

        var settings = builder.build();
        return updateDerivedSettings(settings);
    }

    function init() {
        var opts = require('nomnom')
            .option('config', {
                abbr: 'c',
                default: null,
                help: 'Configuration file to use'
            })
            .option('version', {
                abbr: 'v',
                flag: true,
                help: 'Print version and exit',
                callback: function () {
                    return "reverse-proxy-rate-limiter version " + require('../../package.json').version;
                }
            })
            .parse();

        return this.load(opts.config);
    }
}());
{'fraction_non_alphanumeric': 0.1322301947576512, 'fraction_numerical': 0.0052716356714013765, 'mean_word_length': 1.7254588986432562, 'pattern_counts': {'":': 28, '<': 3, '<?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': '1853316', 'n_tokens_mistral': 1868, 'n_tokens_neox': 1769, 'n_words': 348}