javascript-obfuscator
Version:
JavaScript obfuscator
93 lines (78 loc) • 3.63 kB
text/typescript
import { inject, injectable, } from 'inversify';
import { ServiceIdentifiers } from '../../container/ServiceIdentifiers';
import { TIdentifierNamesGeneratorFactory } from '../../types/container/generators/TIdentifierNamesGeneratorFactory';
import { TStatement } from '../../types/node/TStatement';
import { IOptions } from '../../interfaces/options/IOptions';
import { IRandomGenerator } from '../../interfaces/utils/IRandomGenerator';
import { ObfuscationEvent } from '../../enums/event-emitters/ObfuscationEvent';
import { initializable } from '../../decorators/Initializable';
import { SingleNodeCallControllerTemplate } from '../../templates/SingleNodeCallControllerTemplate';
import { NO_ADDITIONAL_NODES_PRESET } from '../../options/presets/NoCustomNodes';
import { AbstractCustomNode } from '../AbstractCustomNode';
import { JavaScriptObfuscator } from '../../JavaScriptObfuscatorFacade';
import { NodeUtils } from '../../node/NodeUtils';
import { ICustomNodeFormatter } from '../../interfaces/custom-nodes/ICustomNodeFormatter';
export class NodeCallsControllerFunctionNode extends AbstractCustomNode {
/**
* @type {string}
*/
protected callsControllerFunctionName!: string;
/**
* @type {ObfuscationEvent}
*/
private appendEvent!: ObfuscationEvent;
/**
* @param {TIdentifierNamesGeneratorFactory} identifierNamesGeneratorFactory
* @param {ICustomNodeFormatter} customNodeFormatter
* @param {IRandomGenerator} randomGenerator
* @param {IOptions} options
*/
public constructor (
identifierNamesGeneratorFactory: TIdentifierNamesGeneratorFactory,
customNodeFormatter: ICustomNodeFormatter,
randomGenerator: IRandomGenerator,
options: IOptions
) {
super(identifierNamesGeneratorFactory, customNodeFormatter, randomGenerator, options);
}
/**
* @param {ObfuscationEvent} appendEvent
* @param {string} callsControllerFunctionName
*/
public initialize (appendEvent: ObfuscationEvent, callsControllerFunctionName: string): void {
this.appendEvent = appendEvent;
this.callsControllerFunctionName = callsControllerFunctionName;
}
/**
* @param {string} nodeTemplate
* @returns {TStatement[]}
*/
protected getNodeStructure (nodeTemplate: string): TStatement[] {
return NodeUtils.convertCodeToStructure(nodeTemplate);
}
/**
* @returns {string}
*/
protected getNodeTemplate (): string {
if (this.appendEvent === ObfuscationEvent.AfterObfuscation) {
return JavaScriptObfuscator.obfuscate(
this.customNodeFormatter.formatTemplate(SingleNodeCallControllerTemplate(), {
singleNodeCallControllerFunctionName: this.callsControllerFunctionName
}),
{
...NO_ADDITIONAL_NODES_PRESET,
identifierNamesGenerator: this.options.identifierNamesGenerator,
identifiersDictionary: this.options.identifiersDictionary,
seed: this.options.seed
}
).getObfuscatedCode();
}
return this.customNodeFormatter.formatTemplate(SingleNodeCallControllerTemplate(), {
singleNodeCallControllerFunctionName: this.callsControllerFunctionName
});
}
}