Skip to content

Commit d224995

Browse files
committed
refactoring
1 parent 818e31b commit d224995

12 files changed

Lines changed: 112 additions & 106 deletions

File tree

‎dist/index.js‎

Lines changed: 33 additions & 29 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎src/JavaScriptObfuscatorInternal.ts‎

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,12 @@ import { IGeneratorOutput } from './interfaces/IGeneratorOutput';
88
import { IObfuscationResult } from './interfaces/IObfuscationResult';
99
import { IOptions } from './interfaces/IOptions';
1010

11+
import { CustomNodesStorage } from './storages/custom-nodes/CustomNodesStorage';
12+
import { ObfuscationEventEmitter } from './event-emitters/ObfuscationEventEmitter';
1113
import { ObfuscationResult } from './ObfuscationResult';
1214
import { Obfuscator } from './Obfuscator';
1315
import { SourceMapCorrector } from './SourceMapCorrector';
16+
import { StackTraceAnalyzer } from './stack-trace-analyzer/StackTraceAnalyzer';
1417
import { Utils } from './Utils';
1518

1619
export class JavaScriptObfuscatorInternal {
@@ -92,13 +95,20 @@ export class JavaScriptObfuscatorInternal {
9295
Utils.setRandomGenerator(new Chance(this.options.seed));
9396
}
9497

98+
// parse AST tree
9599
const astTree: ESTree.Program = esprima.parse(sourceCode, JavaScriptObfuscatorInternal.esprimaParams);
96-
const obfuscatedAstTree: ESTree.Program = new Obfuscator(this.options).obfuscateAstTree(astTree);
97-
const generatorOutput: IGeneratorOutput = JavaScriptObfuscatorInternal.generateCode(
98-
sourceCode,
99-
obfuscatedAstTree,
100+
101+
// obfuscate AST tree
102+
const obfuscatedAstTree: ESTree.Program = new Obfuscator(
103+
new ObfuscationEventEmitter(),
104+
new StackTraceAnalyzer(),
105+
new CustomNodesStorage(this.options),
100106
this.options
101-
);
107+
).obfuscateAstTree(astTree);
108+
109+
// generate code
110+
const generatorOutput: IGeneratorOutput = JavaScriptObfuscatorInternal
111+
.generateCode(sourceCode, obfuscatedAstTree, this.options);
102112

103113
return this.getObfuscationResult(generatorOutput);
104114
}

‎src/Obfuscator.ts‎

Lines changed: 38 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,22 +18,44 @@ import { VisitorDirection } from './enums/VisitorDirection';
1818
import { NodeControlFlowTransformersFactory } from './node-transformers/node-control-flow-transformers/factory/NodeControlFlowTransformersFactory';
1919
import { NodeObfuscatorsFactory } from './node-transformers/node-obfuscators/factory/NodeObfuscatorsFactory';
2020

21-
import { CustomNodesStorage } from './storages/custom-nodes/CustomNodesStorage';
2221
import { Node } from './node/Node';
2322
import { NodeUtils } from './node/NodeUtils';
24-
import { StackTraceAnalyzer } from './stack-trace-analyzer/StackTraceAnalyzer';
25-
import { ObfuscationEventEmitter } from './event-emitters/ObfuscationEventEmitter';
2623

2724
export class Obfuscator implements IObfuscator {
25+
/**
26+
* @type {IStorage<ICustomNode>}
27+
*/
28+
private readonly customNodesStorage: IStorage<ICustomNode>;
29+
/**
30+
* @type {IObfuscationEventEmitter}
31+
*/
32+
private readonly obfuscationEventEmitter: IObfuscationEventEmitter;
33+
2834
/**
2935
* @type {IOptions}
3036
*/
3137
private readonly options: IOptions;
3238

3339
/**
40+
* @type {IStackTraceAnalyzer}
41+
*/
42+
private readonly stackTraceAnalyzer: IStackTraceAnalyzer;
43+
44+
/**
45+
* @param obfuscationEventEmitter
46+
* @param stackTraceAnalyzer
47+
* @param customNodesStorage
3448
* @param options
3549
*/
36-
constructor (options: IOptions) {
50+
constructor (
51+
obfuscationEventEmitter: IObfuscationEventEmitter,
52+
stackTraceAnalyzer: IStackTraceAnalyzer,
53+
customNodesStorage: IStorage<ICustomNode>,
54+
options: IOptions
55+
) {
56+
this.obfuscationEventEmitter = obfuscationEventEmitter;
57+
this.stackTraceAnalyzer = stackTraceAnalyzer;
58+
this.customNodesStorage = customNodesStorage;
3759
this.options = options;
3860
}
3961

@@ -46,31 +68,34 @@ export class Obfuscator implements IObfuscator {
4668
return astTree;
4769
}
4870

71+
// zero pass: parentize all nodes
4972
NodeUtils.parentize(astTree);
5073

51-
const obfuscationEventEmitter: IObfuscationEventEmitter = new ObfuscationEventEmitter();
52-
const stackTraceAnalyzer: IStackTraceAnalyzer = new StackTraceAnalyzer();
53-
const customNodesStorage: IStorage<ICustomNode> = new CustomNodesStorage(this.options);
54-
55-
customNodesStorage.initialize(obfuscationEventEmitter, stackTraceAnalyzer.analyze(astTree.body));
74+
// prepare custom nodes
75+
this.customNodesStorage.initialize(this.stackTraceAnalyzer.analyze(astTree.body));
76+
this.customNodesStorage
77+
.getStorage()
78+
.forEach((customNode: ICustomNode) => {
79+
this.obfuscationEventEmitter.on(customNode.getAppendEvent(), customNode.appendNode.bind(customNode));
80+
});
5681

57-
obfuscationEventEmitter.emit(ObfuscationEvents.BeforeObfuscation, astTree);
82+
this.obfuscationEventEmitter.emit(ObfuscationEvents.BeforeObfuscation, astTree);
5883

5984
// first pass: control flow flattening
6085
if (this.options.controlFlowFlattening) {
6186
this.transformAstTree(astTree, VisitorDirection.leave, new NodeControlFlowTransformersFactory(
62-
customNodesStorage,
87+
this.customNodesStorage,
6388
this.options
6489
));
6590
}
6691

6792
// second pass: nodes obfuscation
6893
this.transformAstTree(astTree, VisitorDirection.enter, new NodeObfuscatorsFactory(
69-
customNodesStorage,
94+
this.customNodesStorage,
7095
this.options
7196
));
7297

73-
obfuscationEventEmitter.emit(ObfuscationEvents.AfterObfuscation, astTree);
98+
this.obfuscationEventEmitter.emit(ObfuscationEvents.AfterObfuscation, astTree);
7499

75100
return astTree;
76101
}

‎src/custom-nodes/AbstractCustomNodesFactory.ts‎

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import { TObfuscationEvent } from '../types/TObfuscationEvent';
22

33
import { ICustomNode } from '../interfaces/custom-nodes/ICustomNode';
44
import { ICustomNodesFactory } from '../interfaces/ICustomNodesFactory';
5-
import { IObfuscationEventEmitter } from '../interfaces/IObfuscationEventEmitter';
65
import { IOptions } from '../interfaces/IOptions';
76
import { IStackTraceData } from '../interfaces/stack-trace-analyzer/IStackTraceData';
87

@@ -32,28 +31,18 @@ export abstract class AbstractCustomNodesFactory implements ICustomNodesFactory
3231
}
3332

3433
/**
35-
* @param obfuscationEventEmitter
3634
* @param stackTraceData
3735
* @returns {Map<string, ICustomNode> | undefined}
3836
*/
39-
public abstract initializeCustomNodes (
40-
obfuscationEventEmitter: IObfuscationEventEmitter,
41-
stackTraceData: IStackTraceData[]
42-
): Map <string, ICustomNode> | undefined;
37+
public abstract initializeCustomNodes (stackTraceData: IStackTraceData[]): Map <string, ICustomNode> | undefined;
4338

4439
/**
45-
* @param obfuscationEventEmitter
4640
* @param customNodes
4741
* @returns {Map<string, ICustomNode>}
4842
*/
49-
protected syncCustomNodesWithNodesFactory (
50-
obfuscationEventEmitter: IObfuscationEventEmitter,
51-
customNodes: Map <string, ICustomNode>
52-
): Map <string, ICustomNode> {
53-
customNodes.forEach((node: ICustomNode) => {
54-
node.setAppendEvent(this.appendEvent);
55-
56-
obfuscationEventEmitter.on(node.getAppendEvent(), node.appendNode.bind(node));
43+
protected syncCustomNodesWithNodesFactory (customNodes: Map <string, ICustomNode>): Map <string, ICustomNode> {
44+
customNodes.forEach((customNode: ICustomNode) => {
45+
customNode.setAppendEvent(this.appendEvent);
5746
});
5847

5948
return customNodes;

0 commit comments

Comments
 (0)