<commit_before>/**
 * @module lib/utils/modules
 */

'use strict';

var assign = require('lodash.assign');
var semver = require('semver');

/**
 * Hard-coded modules whitelist.
 *
 * @example
 * <caption>Use with semver-compatible versions or ranges, like in package.json</caption>
 * {
 *   'my-module': '^2.3.0',
 *   'my-other-module': '0.5.2',
 *   'my-best-module': '~8.0.0'
 * }
 *
 * @type {object}
 */
var MODULES_WHITELIST = {
    'rx-lite': '^3.1.2',
    'weak-map': '^1.0.0',
};

/**
 * User-added modules whitelist.
 *
 * These are kept separate mostly for the sake of testing.
 *
 * @see module:lib/utils/modules#whitelist
 *
 * @type {object}
 */
var userWhitelist = {};

/**
 * Check if module is permitted.
 *
 * @method module:lib/utils/modules#isPermitted
 *
 * @param {string} name Module's name
 * @param {string} version Module's semver version
 * @returns {boolean}
 */
function isPermitted(name, version) {
    var whitelist = assign({}, MODULES_WHITELIST, userWhitelist);
    var target;

    if (!semver.valid(version)) {
        throw new Error('Invalid version: ' + version);
    }

    if (Object.keys(whitelist).indexOf(name) !== -1) {
        target = whitelist[name];

        if (semver.valid(target)) {
            return semver.gte(version, target);
        } else if (semver.validRange(target)) {
            return semver.satisfies(version, target);
        } else {
            throw new Error(
                'Invalid range or version for ' + name + ': ' + target
            );
        }
    }

    return false;
}

/**
 * Add user's module(s) to whitelist.
 *
 * @method module:lib/utils/modules#whitelist
 *
 * @example
 * whitelist({
 *   'module-1': '0.1.0',
 *   'my-module-2': '1.0.0',
 * })
 *
 * @param {object} modules name-version hash of modules to whitelist
 * @returns {undefined}
 */
function whitelist(modules) {
    Object.keys(modules).forEach(function(name) {
        userWhitelist[name] = modules[name];
    });
}

/**
 * Clear user's whitelisted modules.
 *
 * @method module:lib/utils/modules#_clearUserWhitelist
 */
function clearUserWhitelist() {
    userWhitelist = {};
}

module.exports = {
    _clearUserWhitelist: clearUserWhitelist,
    isPermitted: isPermitted,
    whitelist: whitelist,
};
<commit_msg>Improve error message.<commit_after>/**
 * @module lib/utils/modules
 */

'use strict';

var assign = require('lodash.assign');
var semver = require('semver');

/**
 * Hard-coded modules whitelist.
 *
 * @example
 * <caption>Use with semver-compatible versions or ranges, like in package.json</caption>
 * {
 *   'my-module': '^2.3.0',
 *   'my-other-module': '0.5.2',
 *   'my-best-module': '~8.0.0'
 * }
 *
 * @type {object}
 */
var MODULES_WHITELIST = {
    'rx-lite': '^3.1.2',
    'weak-map': '^1.0.0',
};

/**
 * User-added modules whitelist.
 *
 * These are kept separate mostly for the sake of testing.
 *
 * @see module:lib/utils/modules#whitelist
 *
 * @type {object}
 */
var userWhitelist = {};

/**
 * Check if module is permitted.
 *
 * @method module:lib/utils/modules#isPermitted
 *
 * @param {string} name Module's name
 * @param {string} version Module's semver version
 * @returns {boolean}
 */
function isPermitted(name, version) {
    var whitelist = assign({}, MODULES_WHITELIST, userWhitelist);
    var target;

    if (!semver.valid(version)) {
        throw new Error('Invalid version ' + name + ': ' + version);
    }

    if (Object.keys(whitelist).indexOf(name) !== -1) {
        target = whitelist[name];

        if (semver.valid(target)) {
            return semver.gte(version, target);
        } else if (semver.validRange(target)) {
            return semver.satisfies(version, target);
        } else {
            throw new Error(
                'Invalid range or version for ' + name + ': ' + target
            );
        }
    }

    return false;
}

/**
 * Add user's module(s) to whitelist.
 *
 * @method module:lib/utils/modules#whitelist
 *
 * @example
 * whitelist({
 *   'module-1': '0.1.0',
 *   'my-module-2': '1.0.0',
 * })
 *
 * @param {object} modules name-version hash of modules to whitelist
 * @returns {undefined}
 */
function whitelist(modules) {
    Object.keys(modules).forEach(function(name) {
        userWhitelist[name] = modules[name];
    });
}

/**
 * Clear user's whitelisted modules.
 *
 * @method module:lib/utils/modules#_clearUserWhitelist
 */
function clearUserWhitelist() {
    userWhitelist = {};
}

module.exports = {
    _clearUserWhitelist: clearUserWhitelist,
    isPermitted: isPermitted,
    whitelist: whitelist,
};

{'fraction_non_alphanumeric': 0.16184844502847132, 'fraction_numerical': 0.010512483574244415, 'mean_word_length': 2.87362171331637, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, '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': '2317107', 'n_tokens_mistral': 1586, 'n_tokens_neox': 1529, 'n_words': 339}