javascript-obfuscator
Version:
JavaScript obfuscator
88 lines (73 loc) • 3.49 kB
text/typescript
import { inject, injectable, } from 'inversify';
import { ServiceIdentifiers } from '../../../container/ServiceIdentifiers';
import * as ESTree from 'estree';
import { TControlFlowStorage } from '../../../types/storages/TControlFlowStorage';
import { TIdentifierNamesGeneratorFactory } from '../../../types/container/generators/TIdentifierNamesGeneratorFactory';
import { TStatement } from '../../../types/node/TStatement';
import { ICustomNode } from '../../../interfaces/custom-nodes/ICustomNode';
import { IOptions } from '../../../interfaces/options/IOptions';
import { IRandomGenerator } from '../../../interfaces/utils/IRandomGenerator';
import { ICustomNodeFormatter } from '../../../interfaces/custom-nodes/ICustomNodeFormatter';
import { initializable } from '../../../decorators/Initializable';
import { AbstractCustomNode } from '../../AbstractCustomNode';
import { NodeFactory } from '../../../node/NodeFactory';
import { NodeGuards } from '../../../node/NodeGuards';
import { NodeUtils } from '../../../node/NodeUtils';
()
export class ControlFlowStorageNode extends AbstractCustomNode {
/**
* @type {TControlFlowStorage}
*/
()
private controlFlowStorage!: TControlFlowStorage;
/**
* @param {TIdentifierNamesGeneratorFactory} identifierNamesGeneratorFactory
* @param {ICustomNodeFormatter} customNodeFormatter
* @param {IRandomGenerator} randomGenerator
* @param {IOptions} options
*/
public constructor (
identifierNamesGeneratorFactory: TIdentifierNamesGeneratorFactory,
(ServiceIdentifiers.ICustomNodeFormatter) customNodeFormatter: ICustomNodeFormatter,
(ServiceIdentifiers.IRandomGenerator) randomGenerator: IRandomGenerator,
(ServiceIdentifiers.IOptions) options: IOptions
) {
(ServiceIdentifiers.Factory__IIdentifierNamesGenerator)
super(identifierNamesGeneratorFactory, customNodeFormatter, randomGenerator, options);
}
/**
* @param {TControlFlowStorage} controlFlowStorage
*/
public initialize (controlFlowStorage: TControlFlowStorage): void {
this.controlFlowStorage = controlFlowStorage;
}
/**
* @param {string} nodeTemplate
* @returns {TStatement[]}
*/
protected getNodeStructure (nodeTemplate: string): TStatement[] {
const propertyNodes: ESTree.Property[] = Array
.from<[string, ICustomNode]>(this.controlFlowStorage.getStorage())
.map(([key, value]: [string, ICustomNode]) => {
const node: ESTree.Node = value.getNode()[0];
if (!NodeGuards.isExpressionStatementNode(node)) {
throw new Error('Function node for control flow storage object should be passed inside the `ExpressionStatement` node!');
}
return NodeFactory.propertyNode(
NodeFactory.identifierNode(key),
node.expression
);
});
let structure: ESTree.Node = NodeFactory.variableDeclarationNode(
[
NodeFactory.variableDeclaratorNode(
NodeFactory.identifierNode(this.controlFlowStorage.getStorageId()),
NodeFactory.objectExpressionNode(propertyNodes)
)
],
'const'
);
structure = NodeUtils.parentizeAst(structure);
return [structure];
}
}