All files / src/custom-nodes/string-array-nodes/factory StringArrayCustomNodesFactory.ts

100% Statements 23/23
100% Branches 6/6
100% Functions 1/1
100% Lines 22/22
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          1x   1x 1x 1x   1x 1x 1x     2619x       2619x         2619x         2619x                       2619x 2090x     529x 514x   15x     529x 529x           529x                             529x 514x                     529x      
import { TObfuscationEvent } from '../../../types/event-emitters/TObfuscationEvent';
 
import { ICustomNode } from '../../../interfaces/custom-nodes/ICustomNode';
import { IStackTraceData } from '../../../interfaces/stack-trace-analyzer/IStackTraceData';
 
import { ObfuscationEvents } from '../../../enums/ObfuscationEvents';
 
import { StringArrayCallsWrapper } from '../StringArrayCallsWrapper';
import { StringArrayNode } from '../StringArrayNode';
import { StringArrayRotateFunctionNode } from '../StringArrayRotateFunctionNode';
 
import { AbstractCustomNodesFactory } from '../../AbstractCustomNodesFactory';
import { StringArrayStorage } from '../../../storages/string-array/StringArrayStorage';
import { Utils } from '../../../Utils';
import { IStorage } from '../../../interfaces/storages/IStorage';
 
export class StringArrayCustomNodesFactory extends AbstractCustomNodesFactory {
    /**
     * @type {TObfuscationEvent}
     */
    protected appendEvent: TObfuscationEvent = ObfuscationEvents.AfterObfuscation;
 
    /**
     * @type {string}
     */
    private stringArrayName: string = Utils.getRandomVariableName(StringArrayNode.ARRAY_RANDOM_LENGTH);
 
    /**
     * @type {string}
     */
    private stringArrayCallsWrapper: string = Utils.getRandomVariableName(StringArrayNode.ARRAY_RANDOM_LENGTH);
 
    /**
     * @type {number}
     */
    private stringArrayRotateValue: number;
 
    /**
     * @param stackTraceData
     * @returns {Map<string, ICustomNode>}
     */
    public initializeCustomNodes (stackTraceData: IStackTraceData[]): Map <string, ICustomNode> | undefined {
        if (!this.options.stringArray) {
            return;
        }
 
        if (this.options.rotateStringArray) {
            this.stringArrayRotateValue = Utils.getRandomInteger(100, 500);
        } else {
            this.stringArrayRotateValue = 0;
        }
 
        const stringArray: IStorage <string> = new StringArrayStorage();
        const stringArrayNode: ICustomNode = new StringArrayNode(
            stringArray,
            this.stringArrayName,
            this.stringArrayRotateValue,
            this.options
        );
        const customNodes: Map <string, ICustomNode> = new Map <string, ICustomNode> ([
            [
                'stringArrayNode', stringArrayNode,
            ],
            [
                'stringArrayCallsWrapper',
                new StringArrayCallsWrapper(
                    this.stringArrayCallsWrapper,
                    this.stringArrayName,
                    stringArray,
                    this.options
                )
            ]
        ]);
 
        if (this.options.rotateStringArray) {
            customNodes.set(
                'stringArrayRotateFunctionNode',
                new StringArrayRotateFunctionNode(
                    this.stringArrayName,
                    stringArray,
                    this.stringArrayRotateValue,
                    this.options
                )
            );
        }
 
        return this.syncCustomNodesWithNodesFactory(customNodes);
    }
}