# all files / http-proxy-middleware/lib/ path-rewriter.js

100% Statements 35/35
94.74% Branches 18/19
100% Functions 6/6
100% Lines 35/35

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    731×
    1×

    1×

    1×
    60×

    60×
    34×

    22×
    7×
    7×

    15×
    15×

    1×
    15×

    15×
    30×
    12×
    12×
    12×

    15×

    1×
    60×
    7×
    53×
    15×
    38×

    34×

    4×

    1×
    15×

    15×
    15×
    46×

    46×

    15×

     var _      = require('lodash');
    var logger = require('./logger').getInstance();

    module.exports = {
        create: createPathRewriter
    };

    /**
     * Create rewrite function, to cache parsed rewrite rules.
     *
     * @returns {function} Function to rewrite paths; This function should accept `path` (request.url) as parameter
     */
    function createPathRewriter(rewriteConfig) {
        var rulesCache;

        if (!isValidRewriteConfig(rewriteConfig)) {
            return;
        }

        if (_.isFunction(rewriteConfig)) {
            var customRewriteFn = rewriteConfig;
            return customRewriteFn;
        } else {
            rulesCache = parsePathRewriteRules(rewriteConfig);
            return rewritePath;
        }

        function rewritePath(path) {
            var result = path;

            _.forEach(rulesCache, function(rule) {
                if (rule.regex.test(path)) {
                    result = result.replace(rule.regex, rule.value);
                    logger.debug('[HPM] Rewriting path from "%s" to "%s"', path, result);
                    return false;
                }
            });

            return result;
        }
    }

    function isValidRewriteConfig(rewriteConfig) {
        if (_.isFunction(rewriteConfig)) {
            return true;
        } else if (!_.isEmpty(rewriteConfig) && _.isPlainObject(rewriteConfig)) {
            return true;
        } else if (_.isUndefined(rewriteConfig) ||
                   _.isNull(rewriteConfig) ||
                   _.isEqual(rewriteConfig, {})) {
            return false;
        } else {
            throw new Error('[HPM] Invalid pathRewrite config. Expecting object with pathRewrite config or a rewrite function');
        }
    }

    function parsePathRewriteRules(rewriteConfig) {
        var rules = [];

        Eif (_.isPlainObject(rewriteConfig)) {
            _.forIn(rewriteConfig, function(value, key) {
                rules.push({
                    regex: new RegExp(key),
                    value: rewriteConfig[key]
                });
                logger.info('[HPM] Proxy rewrite rule created: "%s" ~> "%s"', key, rewriteConfig[key]);
            });
        }

        return rules;
    }