import * as escodegen from 'escodegen';
import { INode } from '../../interfaces/nodes/INode';
import { IOptions } from "../../interfaces/IOptions";
import { IVariableDeclarationNode } from "../../interfaces/nodes/IVariableDeclarationNode";
import { TNodeWithBlockStatement } from "../../types/TNodeWithBlockStatement";
import { AppendState } from '../../enums/AppendState';
import { NodeType } from "../../enums/NodeType";
import { Node } from '../Node';
import { NodeUtils } from "../../NodeUtils";
import { Utils } from '../../Utils';
export class UnicodeArrayNode extends Node {
/**
* @type {number}
*/
public static UNICODE_ARRAY_RANDOM_LENGTH: number = 4;
/**
* @type {AppendState}
*/
protected appendState: AppendState = AppendState.AfterObfuscation;
/**
* @type {string[]}
*/
private unicodeArray: string[] = [];
/**
* @type {string}
*/
private unicodeArrayName: string;
/**
* @type {number}
*/
private unicodeArrayRotateValue: number;
/**
* @param unicodeArrayName
* @param unicodeArrayRotateValue
* @param options
*/
constructor (
unicodeArrayName: string,
unicodeArrayRotateValue: number = 0,
options: IOptions
) {
super(options);
this.unicodeArrayName = unicodeArrayName;
this.unicodeArrayRotateValue = unicodeArrayRotateValue;
this.node = this.getNodeStructure();
}
/**
* @param blockScopeNode
*/
public appendNode (blockScopeNode: TNodeWithBlockStatement): void {
NodeUtils.prependNode(blockScopeNode.body, this.getNode());
}
/**
* @returns {string}
*/
public getNodeIdentifier (): string {
returnI this.unicodeArrayName;
}
/**
* @returns {string[]}
*/
public getNodeData (): string[] {
return this.unicodeArray;
}
/**
* @returns {INode}
*/
public getNode (): INode {
if (!this.unicodeArray.length) {
return;
}
Utils.arrayRotate <string> (this.unicodeArray, this.unicodeArrayRotateValue);
this.updateNode();
return super.getNode();
}
/**
* @param data
*/
public updateNodeData (data: string): void {
this.unicodeArray.push(data);
}
/**
* @returns {INode}
*/
protected getNodeStructure (): IVariableDeclarationNode {
return {
'type': NodeType.VariableDeclaration,
'declarations': [
{
'type': NodeType.VariableDeclarator,
'id': {
'type': NodeType.Identifier,
'name': this.unicodeArrayName
},
'init': {
'type': NodeType.ArrayExpression,
'elements': this.unicodeArray.map((value: string) => {
return {
'type': NodeType.Literal,
'value': value,
'raw': `'${value}'`,
'x-verbatim-property': {
'content' : value,
precedence: escodegen.Precedence.Primary
}
};
})
}
}
],
'kind': 'var'
};
}
}
// WEBPACK FOOTER //
// ./src/custom-nodes/unicode-array-nodes/UnicodeArrayNode.ts |