All files / src/options Options.ts

96.97% Statements 32/33
50% Branches 1/2
100% Functions 2/2
96.97% Lines 32/33
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 174 175 176 177 178 1791x   1x                                           1x   1x 1x   1x 1x       1x                 1x           1x           1x           1x           1x           1x                   1x                   1x           1x           1x           1x           1x 2634x                   1x           1x           1x           1x           1x               1x           1x 2634x   2634x   2634x       2634x      
import { injectable } from 'inversify';
 
import {
ArrayUnique,
IsBoolean,
IsArray,
IsIn,
IsNumber,
IsString,
IsUrl,
Min,
Max,
ValidateIf,
validateSync,
ValidationError,
ValidatorOptions
} from 'class-validator';
 
import { IInputOptions } from '../interfaces/options/IInputOptions';
import { IOptions } from '../interfaces/options/IOptions';
 
import { TSourceMapMode } from '../types/TSourceMapMode';
import { TStringArrayEncoding } from '../types/options/TStringArrayEncoding';
 
import { DEFAULT_PRESET } from '../preset-options/DefaultPreset';
 
import { OptionsNormalizer } from './OptionsNormalizer';
import { ValidationErrorsFormatter } from './ValidationErrorsFormatter';
 
@injectable()
export class Options implements IOptions {
    /**
     * @type {ValidatorOptions}
     */
    private static validatorOptions: ValidatorOptions = {
        validationError: {
            target: false
        }
    };
 
    /**
     * @type {boolean}
     */
    @IsBoolean()
    public readonly compact: boolean;
 
    /**
     * @type {boolean}
     */
    @IsBoolean()
    public readonly controlFlowFlattening: boolean;
 
    /**
     * @type {boolean}
     */
    @IsBoolean()
    public readonly debugProtection: boolean;
 
    /**
     * @type {boolean}
     */
    @IsBoolean()
    public readonly debugProtectionInterval: boolean;
 
    /**
     * @type {boolean}
     */
    @IsBoolean()
    public readonly disableConsoleOutput: boolean;
 
    /**
     * @type {string[]}
     */
    @IsArray()
    @ArrayUnique()
    @IsString({
        each: true
    })
    public readonly domainLock: string[];
 
    /**
     * @type {string[]}
     */
    @IsArray()
    @ArrayUnique()
    @IsString({
        each: true
    })
    public readonly reservedNames: string[];
 
    /**
     * @type {boolean}
     */
    @IsBoolean()
    public readonly rotateStringArray: boolean;
 
    /**
     * @type {number}
     */
    @IsNumber()
    public readonly seed: number;
 
    /**
     * @type {boolean}
     */
    @IsBoolean()
    public readonly selfDefending: boolean;
 
    /**
     * @type {boolean}
     */
    @IsBoolean()
    public readonly sourceMap: boolean;
 
    /**
     * @type {string}
     */
    @IsString()
    @ValidateIf((options: IOptions) => Boolean(options.sourceMapBaseUrl))
    @IsUrl({
        require_protocol: true,
        require_valid_protocol: true
    })
    public readonly sourceMapBaseUrl: string;
 
    /**
     * @type {string}
     */
    @IsString()
    public readonly sourceMapFileName: string;
 
    /**
     * @type {TSourceMapMode}
     */
    @IsIn(['inline', 'separate'])
    public readonly sourceMapMode: TSourceMapMode;
 
    /**
     * @type {boolean}
     */
    @IsBoolean()
    public readonly stringArray: boolean;
 
    /**
     * @type {TStringArrayEncoding}
     */
    @IsIn([true, false, 'base64', 'rc4'])
    public readonly stringArrayEncoding: TStringArrayEncoding;
 
    /**
     * @type {number}
     */
    @IsNumber()
    @Min(0)
    @Max(1)
    public readonly stringArrayThreshold: number;
 
    /**
     * @type {boolean}
     */
    @IsBoolean()
    public readonly unicodeEscapeSequence: boolean;
 
    /**
     * @param inputOptions
     */
    constructor (inputOptions: IInputOptions) {
        Object.assign(this, DEFAULT_PRESET, inputOptions);
 
        const errors: ValidationError[] = validateSync(this, Options.validatorOptions);
 
        Iif (errors.length) {
            throw new ReferenceError(`Validation failed. errors:\n${ValidationErrorsFormatter.format(errors)}`);
        }
 
        Object.assign(this, OptionsNormalizer.normalizeOptions(this));
    }
}