All files / src JavaScriptObfuscatorInternal.ts

100% Statements 29/29
100% Branches 6/6
100% Functions 1/1
100% Lines 29/29
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 1341x 1x   1x 1x     1x                     1x   1x 1x       1x               1x                                                                       2622x 2622x 2622x 2622x               2622x         2622x 9x 9x     2622x       2622x   2622x   2622x               2622x                     2622x 20x       2622x     2622x     2622x   2622x      
import { injectable, inject } from 'inversify';
import { ServiceIdentifiers } from './container/ServiceIdentifiers';
 
import * as esprima from 'esprima';
import * as escodegen from 'escodegen';
import * as ESTree from 'estree';
 
import { Chance } from 'chance';
 
import { ICustomNode } from './interfaces/custom-nodes/ICustomNode';
import { IJavaScriptObfuscator } from './interfaces/IJavaScriptObfsucator';
import { IObfuscationResult } from './interfaces/IObfuscationResult';
import { IObfuscator } from './interfaces/IObfuscator';
import { IGeneratorOutput } from './interfaces/IGeneratorOutput';
import { IOptions } from './interfaces/options/IOptions';
import { ISourceMapCorrector } from './interfaces/ISourceMapCorrector';
import { IStorage } from './interfaces/storages/IStorage';
 
import { Utils } from './Utils';
 
@injectable()
export class JavaScriptObfuscatorInternal implements IJavaScriptObfuscator {
    /**
     * @type {GenerateOptions}
     */
    private static readonly escodegenParams: escodegen.GenerateOptions = {
        verbatim: 'x-verbatim-property',
        sourceMapWithCode: true
    };
 
    /**
     * @type {esprima.Options}
     */
    private static readonly esprimaParams: esprima.Options = {
        loc: true
    };
 
    /**
     * @type {IStorage<ICustomNode>}
     */
    private readonly customNodesStorage: IStorage<ICustomNode>;
 
    /**
     * @type {IObfuscator}
     */
    private readonly obfuscator: IObfuscator;
 
    /**
     * @type {IOptions}
     */
    private readonly options: IOptions;
 
    /**
     * @type {ISourceMapCorrector}
     */
    private readonly sourceMapCorrector: ISourceMapCorrector;
 
    /**
     * @param obfuscator
     * @param sourceMapCorrector
     * @param customNodesStorage
     * @param options
     */
    constructor (
        @inject(ServiceIdentifiers.IObfuscator) obfuscator: IObfuscator,
        @inject(ServiceIdentifiers.ISourceMapCorrector) sourceMapCorrector: ISourceMapCorrector,
        @inject(ServiceIdentifiers['IStorage<ICustomNode>']) customNodesStorage: IStorage<ICustomNode>,
        @inject(ServiceIdentifiers.IOptions) options: IOptions
    ) {
        this.obfuscator = obfuscator;
        this.sourceMapCorrector = sourceMapCorrector;
        this.customNodesStorage = customNodesStorage;
        this.options = options;
    }
 
    /**
     * @param sourceCode
     * @param astTree
     */
    private generateCode (sourceCode: string, astTree: ESTree.Program): IGeneratorOutput {
        const escodegenParams: escodegen.GenerateOptions = Object.assign(
            {},
            JavaScriptObfuscatorInternal.escodegenParams
        );
 
        if (this.options.sourceMap) {
            escodegenParams.sourceMap = 'sourceMap';
            escodegenParams.sourceContent = sourceCode;
        }
 
        escodegenParams.format = {
            compact: this.options.compact
        };
 
        const generatorOutput: IGeneratorOutput = escodegen.generate(astTree, escodegenParams);
 
        generatorOutput.map = generatorOutput.map ? generatorOutput.map.toString() : '';
 
        return generatorOutput;
    }
 
    /**
     * @param generatorOutput
     * @returns {IObfuscationResult}
     */
    private getObfuscationResult (generatorOutput: IGeneratorOutput): IObfuscationResult {
        return this.sourceMapCorrector.correct(
            generatorOutput.code,
            generatorOutput.map
        );
    }
 
    /**
     * @param sourceCode
     * @returns {IObfuscationResult}
     */
    public obfuscate (sourceCode: string): IObfuscationResult {
        if (this.options.seed !== 0) {
            Utils.setRandomGenerator(new Chance(this.options.seed));
        }
 
        // parse AST tree
        const astTree: ESTree.Program = esprima.parse(sourceCode, JavaScriptObfuscatorInternal.esprimaParams);
 
        // obfuscate AST tree
        const obfuscatedAstTree: ESTree.Program = this.obfuscator.obfuscateAstTree(astTree, this.customNodesStorage);
 
        // generate code
        const generatorOutput: IGeneratorOutput = this.generateCode(sourceCode, obfuscatedAstTree);
 
        return this.getObfuscationResult(generatorOutput);
    }
}