Skip to content

Commit f47dbdd

Browse files
committed
refactoring
1 parent 7b308b3 commit f47dbdd

File tree

3 files changed

+78
-63
lines changed

3 files changed

+78
-63
lines changed

dist/index.js

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

src/node-transformers/node-obfuscators/ObjectExpressionObfuscator.ts

Lines changed: 34 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -34,61 +34,58 @@ export class ObjectExpressionObfuscator extends AbstractNodeTransformer {
3434
super(options);
3535
}
3636

37-
/**
38-
* @param objectExpressionNode
39-
*/
40-
public transformNode (objectExpressionNode: ESTree.ObjectExpression): void {
41-
objectExpressionNode.properties
42-
.forEach((property: ESTree.Property) => {
43-
if (property.shorthand) {
44-
property.shorthand = false;
45-
}
46-
47-
estraverse.traverse(property.key, {
48-
enter: (node: ESTree.Node, parentNode: ESTree.Node): any => {
49-
if (Node.isLiteralNode(node)) {
50-
this.obfuscateLiteralPropertyKey(node);
51-
52-
return;
53-
}
54-
55-
if (Node.isIdentifierNode(node)) {
56-
this.obfuscateIdentifierPropertyKey(node);
57-
}
58-
}
59-
});
60-
});
61-
}
62-
6337
/**
6438
* @param node
39+
* @returns {ESTree.Literal}
6540
*/
66-
private obfuscateLiteralPropertyKey (node: ESTree.Literal): void {
41+
private static obfuscateLiteralPropertyKey (node: ESTree.Literal): ESTree.Literal {
6742
if (typeof node.value === 'string' && !node['x-verbatim-property']) {
6843
node['x-verbatim-property'] = {
6944
content : `'${Utils.stringToUnicodeEscapeSequence(node.value)}'`,
7045
precedence: escodegen.Precedence.Primary
7146
};
7247
}
48+
49+
return node;
7350
}
7451

7552
/**
7653
* @param node
54+
* @returns {ESTree.Literal}
7755
*/
78-
private obfuscateIdentifierPropertyKey (node: ESTree.Identifier): void {
79-
const nodeValue: string = node.name;
80-
const literalNode: ESTree.Literal = {
81-
raw: `'${nodeValue}'`,
56+
private static obfuscateIdentifierPropertyKey (node: ESTree.Identifier): ESTree.Literal {
57+
return {
58+
type: NodeType.Literal,
59+
value: node.name,
60+
raw: `'${node.name}'`,
8261
'x-verbatim-property': {
83-
content : `'${Utils.stringToUnicodeEscapeSequence(nodeValue)}'`,
62+
content : `'${Utils.stringToUnicodeEscapeSequence(node.name)}'`,
8463
precedence: escodegen.Precedence.Primary
85-
},
86-
type: NodeType.Literal,
87-
value: nodeValue
64+
}
8865
};
66+
}
67+
68+
/**
69+
* @param objectExpressionNode
70+
*/
71+
public transformNode (objectExpressionNode: ESTree.ObjectExpression): void {
72+
objectExpressionNode.properties
73+
.forEach((property: ESTree.Property) => {
74+
if (property.shorthand) {
75+
property.shorthand = false;
76+
}
8977

90-
delete node.name;
78+
estraverse.replace(property.key, {
79+
enter: (node: ESTree.Node, parentNode: ESTree.Node): any => {
80+
if (Node.isLiteralNode(node)) {
81+
property.key = ObjectExpressionObfuscator.obfuscateLiteralPropertyKey(node);
82+
}
9183

92-
Object.assign(node, literalNode);
84+
if (Node.isIdentifierNode(node)) {
85+
property.key = ObjectExpressionObfuscator.obfuscateIdentifierPropertyKey(node);
86+
}
87+
}
88+
});
89+
});
9390
}
9491
}

src/utils/Utils.ts

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@ export class Utils {
77
*/
88
public static readonly hexadecimalPrefix: string = '0x';
99

10+
/**
11+
* @type {Map<string, string>}
12+
*/
13+
private static readonly stringToUnicodeEscapeSequenceCache: Map <string, string> = new Map();
14+
1015
/**
1116
* @param array
1217
* @param times
@@ -128,6 +133,12 @@ export class Utils {
128133
* @returns {string}
129134
*/
130135
public static stringToUnicodeEscapeSequence (string: string, nonLatinAndNonDigitsOnly: boolean = false): string {
136+
const cacheKey: string = `${string}-${String(nonLatinAndNonDigitsOnly)}`;
137+
138+
if (Utils.stringToUnicodeEscapeSequenceCache.has(cacheKey)) {
139+
return <string>Utils.stringToUnicodeEscapeSequenceCache.get(cacheKey);
140+
}
141+
131142
const radix: number = 16;
132143
const replaceRegExp: RegExp = new RegExp('[\\s\\S]', 'g');
133144
const escapeRegExp: RegExp = new RegExp('[^a-zA-Z0-9]');
@@ -136,11 +147,11 @@ export class Utils {
136147
let prefix: string,
137148
template: string;
138149

139-
return `${string.replace(replaceRegExp, (escape: string): string => {
150+
const result: string = string.replace(replaceRegExp, (escape: string): string => {
140151
if (nonLatinAndNonDigitsOnly && !escapeRegExp.test(escape)) {
141152
return escape;
142153
}
143-
154+
144155
if (regexp.test(escape)) {
145156
prefix = '\\x';
146157
template = '0'.repeat(2);
@@ -150,6 +161,10 @@ export class Utils {
150161
}
151162

152163
return `${prefix}${(template + escape.charCodeAt(0).toString(radix)).slice(-template.length)}`;
153-
})}`;
164+
});
165+
166+
Utils.stringToUnicodeEscapeSequenceCache.set(cacheKey, result);
167+
168+
return result;
154169
}
155170
}

0 commit comments

Comments
 (0)