All files / src/options OptionsNormalizer.ts

100% Statements 43/43
100% Branches 18/18
100% Functions 0/0
100% Lines 40/40
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 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173          1x   1x       1x                   1x               1x             1x                             2642x   18494x 18494x     2642x               2642x 3x   5x 5x     3x         2642x               2642x 532x     2642x               2642x   2642x 2632x       2632x     10x 3x         10x               2642x   2642x 10x       10x         2642x               2642x 2092x     2642x               2642x 1x     2642x               2642x 2093x     2642x      
import { IObfuscatorOptions } from '../interfaces/IObfuscatorOptions';
import { IOptions } from '../interfaces/IOptions';
 
import { TOptionsNormalizerRule } from '../types/TOptionsNormalizerRule';
 
import { Utils } from '../Utils';
 
export class OptionsNormalizer {
    /**
     * @type {IObfuscatorOptions}
     */
    private static DISABLED_UNICODE_ARRAY_OPTIONS: IObfuscatorOptions = {
        rotateStringArray: false,
        stringArray: false,
        stringArrayEncoding: false,
        stringArrayThreshold: 0
    };
 
    /**
     * @type {IObfuscatorOptions}
     */
    private static SELF_DEFENDING_OPTIONS: IObfuscatorOptions = {
        compact: true,
        selfDefending: true
    };
 
    /**
     * @type {IObfuscatorOptions}
     */
    private static UNICODE_ARRAY_ENCODING_OPTIONS: IObfuscatorOptions = {
        stringArrayEncoding: 'base64'
    };
 
    /**
     * @type {TOptionsNormalizerRule[]}
     */
    private static normalizerRules: TOptionsNormalizerRule[] = [
        OptionsNormalizer.domainLockRule,
        OptionsNormalizer.selfDefendingRule,
        OptionsNormalizer.sourceMapBaseUrlRule,
        OptionsNormalizer.sourceMapFileNameRule,
        OptionsNormalizer.stringArrayRule,
        OptionsNormalizer.stringArrayEncodingRule,
        OptionsNormalizer.stringArrayThresholdRule,
    ];
 
    /**
     * @param options
     * @returns {IOptions}
     */
    public static normalizeOptions (options: IOptions): IOptions {
        let normalizedOptions: IOptions = Object.assign({}, options);
 
        for (const normalizerRule of OptionsNormalizer.normalizerRules) {
            normalizedOptions = normalizerRule(normalizedOptions);
        }
 
        return normalizedOptions;
    }
 
    /**
     * @param options
     * @returns {IOptions}
     */
    private static domainLockRule (options: IOptions): IOptions {
        if (options.domainLock.length) {
            let normalizedDomains: string[] = [];
 
            for (const domain of options.domainLock) {
                normalizedDomains.push(Utils.extractDomainFromUrl(domain));
            }
 
            Object.assign(options, {
                domainLock: normalizedDomains
            });
        }
 
        return options;
    }
 
    /**
     * @param options
     * @returns {IOptions}
     */
    private static selfDefendingRule (options: IOptions): IOptions {
        if (options.selfDefending) {
            Object.assign(options, OptionsNormalizer.SELF_DEFENDING_OPTIONS);
        }
 
        return options;
    }
 
    /**
     * @param options
     * @returns {IOptions}
     */
    private static sourceMapBaseUrlRule (options: IOptions): IOptions {
        let sourceMapBaseUrl: string = options.sourceMapBaseUrl;
 
        if (!options.sourceMapFileName) {
            Object.assign(options, {
                sourceMapBaseUrl: ''
            });
 
            return options;
        }
 
        if (sourceMapBaseUrl && !sourceMapBaseUrl.endsWith('/')) {
            Object.assign(options, {
                sourceMapBaseUrl: `${sourceMapBaseUrl}/`
            });
        }
 
        return options;
    }
 
    /**
     * @param options
     * @returns {IOptions}
     */
    private static sourceMapFileNameRule (options: IOptions): IOptions {
        let sourceMapFileName: string = options.sourceMapFileName;
 
        if (sourceMapFileName) {
            sourceMapFileName = sourceMapFileName
                .replace(/^\/+/, '')
                .split('.')[0];
 
            Object.assign(options, {
                sourceMapFileName: `${sourceMapFileName}.js.map`
            });
        }
 
        return options;
    }
 
    /**
     * @param options
     * @returns {IOptions}
     */
    private static stringArrayRule (options: IOptions): IOptions {
        if (!options.stringArray) {
            Object.assign(options, OptionsNormalizer.DISABLED_UNICODE_ARRAY_OPTIONS);
        }
 
        return options;
    }
 
    /**
     * @param options
     * @returns {IOptions}
     */
    private static stringArrayEncodingRule (options: IOptions): IOptions {
        if (options.stringArrayEncoding === true) {
            Object.assign(options, OptionsNormalizer.UNICODE_ARRAY_ENCODING_OPTIONS);
        }
 
        return options;
    }
 
    /**
     * @param options
     * @returns {IOptions}
     */
    private static stringArrayThresholdRule (options: IOptions): IOptions {
        if (options.stringArrayThreshold === 0) {
            Object.assign(options, OptionsNormalizer.DISABLED_UNICODE_ARRAY_OPTIONS);
        }
 
        return options;
    }
}