@@ -18,22 +18,44 @@ import { VisitorDirection } from './enums/VisitorDirection';
1818import { NodeControlFlowTransformersFactory } from './node-transformers/node-control-flow-transformers/factory/NodeControlFlowTransformersFactory' ;
1919import { NodeObfuscatorsFactory } from './node-transformers/node-obfuscators/factory/NodeObfuscatorsFactory' ;
2020
21- import { CustomNodesStorage } from './storages/custom-nodes/CustomNodesStorage' ;
2221import { Node } from './node/Node' ;
2322import { NodeUtils } from './node/NodeUtils' ;
24- import { StackTraceAnalyzer } from './stack-trace-analyzer/StackTraceAnalyzer' ;
25- import { ObfuscationEventEmitter } from './event-emitters/ObfuscationEventEmitter' ;
2623
2724export 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 }
0 commit comments