# All files / src JavaScriptObfuscatorInstance.ts

78.26% Statements 18/23
0% Branches 0/4
100% Functions 1/1
78.26% Lines 18/23

    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
    1121x
    1x

    1x
    1x
    1x

    1x

    1x

    8x
    8x

    8x

    8x

    8x

    8x

    8x

    8x

    8x

    8x

    8x

     import * as esprima from 'esprima';
    import * as escodegen from 'escodegen';

    import { IGeneratorOutput } from "./interfaces/IGeneratorOutput";
    import { INode } from './interfaces/nodes/INode';
    import { IOptions } from './interfaces/IOptions';
    import { IOptionsPreset } from "./interfaces/IOptionsPreset";

    import { TSourceMapModes } from "./types/TSourceMapModes";

    import { Obfuscator } from "./Obfuscator";
    import { Options } from "./Options";
    import { SourceMapInjector } from "./SourceMapInjector";

    export class JavaScriptObfuscatorInstance {
        /**
         * @type {GenerateOptions}
         */
        private static escodegenParams: escodegen.GenerateOptions = {
            verbatim: 'x-verbatim-property',
            sourceMapWithCode: true
        };

        /**
         * @type {IGeneratorOutput}
         */
        private generatorOutput: IGeneratorOutput;

        /**
         * @type {IOptions}
         */
        private options: IOptions;

        /**
         * @type {string}
         */
        private sourceCode: string;

        /**
         * @type {string}
         */
        private sourceMapUrl: string;

        /**
         * @param sourceCode
         * @param customOptions
         */
        constructor (sourceCode: string, customOptions?: IOptionsPreset) {
            this.sourceCode = sourceCode;
            this.options = new Options(customOptions);
        }

        /**
         * @param sourceCode
         * @param astTree
         * @param options
         */
        private static generateCode (sourceCode: string, astTree: INode, options: IOptions): IGeneratorOutput {
            let escodegenParams: escodegen.GenerateOptions = Object.assign({}, JavaScriptObfuscatorInstance.escodegenParams);

            if (options.get<boolean>('sourceMap')) {
                escodegenParams.sourceMap = 'sourceMap';
                escodegenParams.sourceContent = sourceCode;
            }

            escodegenParams.format = {
                compact: options.get<boolean>('compact')
            };

            return escodegen.generate(astTree, escodegenParams);
        }

        /**
         * @returns {string}
         */
        public getObfuscatedCode (): string {
            if (this.generatorOutput.map) {
                return SourceMapInjector.inject(
                    this.generatorOutput.code,
                    this.sourceMapUrl || this.generatorOutput.map.toString(),
                    this.options.get<TSourceMapModes>('sourceMapMode')
                );
            }

            return this.generatorOutput.code;
        }

        /**
         * @returns {string}
         */
        public getSourceMap (): string {
            return this.generatorOutput.map;
        }

        public obfuscate (): void {
            let astTree: INode = esprima.parse(this.sourceCode, {
                loc: true
            });

            astTree = new Obfuscator(this.options).obfuscateNode(astTree);

            this.generatorOutput = JavaScriptObfuscatorInstance.generateCode(this.sourceCode, astTree, this.options);
        }

        /**
         * @param url
         */
        public setSourceMapUrl (url: string): void {
            this.sourceMapUrl = url;
        }
    }