mirror of
https://github.com/javascript-obfuscator/javascript-obfuscator
synced 2026-08-09 12:42:29 +00:00
refactoring
This commit is contained in:
Vendored
+27
-24
@@ -673,6 +673,10 @@ exports.Node = Node;
|
||||
"use strict";
|
||||
|
||||
|
||||
var _map = __webpack_require__(11);
|
||||
|
||||
var _map2 = _interopRequireDefault(_map);
|
||||
|
||||
var _from = __webpack_require__(43);
|
||||
|
||||
var _from2 = _interopRequireDefault(_from);
|
||||
@@ -805,13 +809,17 @@ var Utils = function () {
|
||||
value: function stringToUnicodeEscapeSequence(string) {
|
||||
var nonLatinAndNonDigitsOnly = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
|
||||
|
||||
var cacheKey = string + "-" + String(nonLatinAndNonDigitsOnly);
|
||||
if (Utils.stringToUnicodeEscapeSequenceCache.has(cacheKey)) {
|
||||
return Utils.stringToUnicodeEscapeSequenceCache.get(cacheKey);
|
||||
}
|
||||
var radix = 16;
|
||||
var replaceRegExp = new RegExp('[\\s\\S]', 'g');
|
||||
var escapeRegExp = new RegExp('[^a-zA-Z0-9]');
|
||||
var regexp = new RegExp('[\\x00-\\x7F]');
|
||||
var prefix = void 0,
|
||||
template = void 0;
|
||||
return "" + string.replace(replaceRegExp, function (escape) {
|
||||
var result = string.replace(replaceRegExp, function (escape) {
|
||||
if (nonLatinAndNonDigitsOnly && !escapeRegExp.test(escape)) {
|
||||
return escape;
|
||||
}
|
||||
@@ -824,12 +832,15 @@ var Utils = function () {
|
||||
}
|
||||
return "" + prefix + (template + escape.charCodeAt(0).toString(radix)).slice(-template.length);
|
||||
});
|
||||
Utils.stringToUnicodeEscapeSequenceCache.set(cacheKey, result);
|
||||
return result;
|
||||
}
|
||||
}]);
|
||||
return Utils;
|
||||
}();
|
||||
|
||||
Utils.hexadecimalPrefix = '0x';
|
||||
Utils.stringToUnicodeEscapeSequenceCache = new _map2.default();
|
||||
exports.Utils = Utils;
|
||||
|
||||
/***/ },
|
||||
@@ -5371,10 +5382,6 @@ var MethodDefinitionObfuscator_1;
|
||||
"use strict";
|
||||
|
||||
|
||||
var _assign = __webpack_require__(44);
|
||||
|
||||
var _assign2 = _interopRequireDefault(_assign);
|
||||
|
||||
var _getPrototypeOf = __webpack_require__(5);
|
||||
|
||||
var _getPrototypeOf2 = _interopRequireDefault(_getPrototypeOf);
|
||||
@@ -5406,7 +5413,7 @@ var NodeType_1 = __webpack_require__(16);
|
||||
var AbstractNodeTransformer_1 = __webpack_require__(17);
|
||||
var Node_1 = __webpack_require__(12);
|
||||
var Utils_1 = __webpack_require__(13);
|
||||
var ObjectExpressionObfuscator = function (_AbstractNodeTransfor) {
|
||||
var ObjectExpressionObfuscator = ObjectExpressionObfuscator_1 = function (_AbstractNodeTransfor) {
|
||||
(0, _inherits3.default)(ObjectExpressionObfuscator, _AbstractNodeTransfor);
|
||||
|
||||
function ObjectExpressionObfuscator(options) {
|
||||
@@ -5417,26 +5424,23 @@ var ObjectExpressionObfuscator = function (_AbstractNodeTransfor) {
|
||||
(0, _createClass3.default)(ObjectExpressionObfuscator, [{
|
||||
key: "transformNode",
|
||||
value: function transformNode(objectExpressionNode) {
|
||||
var _this2 = this;
|
||||
|
||||
objectExpressionNode.properties.forEach(function (property) {
|
||||
if (property.shorthand) {
|
||||
property.shorthand = false;
|
||||
}
|
||||
estraverse.traverse(property.key, {
|
||||
estraverse.replace(property.key, {
|
||||
enter: function enter(node, parentNode) {
|
||||
if (Node_1.Node.isLiteralNode(node)) {
|
||||
_this2.obfuscateLiteralPropertyKey(node);
|
||||
return;
|
||||
property.key = ObjectExpressionObfuscator_1.obfuscateLiteralPropertyKey(node);
|
||||
}
|
||||
if (Node_1.Node.isIdentifierNode(node)) {
|
||||
_this2.obfuscateIdentifierPropertyKey(node);
|
||||
property.key = ObjectExpressionObfuscator_1.obfuscateIdentifierPropertyKey(node);
|
||||
}
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
}, {
|
||||
}], [{
|
||||
key: "obfuscateLiteralPropertyKey",
|
||||
value: function obfuscateLiteralPropertyKey(node) {
|
||||
if (typeof node.value === 'string' && !node['x-verbatim-property']) {
|
||||
@@ -5445,28 +5449,27 @@ var ObjectExpressionObfuscator = function (_AbstractNodeTransfor) {
|
||||
precedence: escodegen.Precedence.Primary
|
||||
};
|
||||
}
|
||||
return node;
|
||||
}
|
||||
}, {
|
||||
key: "obfuscateIdentifierPropertyKey",
|
||||
value: function obfuscateIdentifierPropertyKey(node) {
|
||||
var nodeValue = node.name;
|
||||
var literalNode = {
|
||||
raw: "'" + nodeValue + "'",
|
||||
'x-verbatim-property': {
|
||||
content: "'" + Utils_1.Utils.stringToUnicodeEscapeSequence(nodeValue) + "'",
|
||||
precedence: escodegen.Precedence.Primary
|
||||
},
|
||||
return {
|
||||
type: NodeType_1.NodeType.Literal,
|
||||
value: nodeValue
|
||||
value: node.name,
|
||||
raw: "'" + node.name + "'",
|
||||
'x-verbatim-property': {
|
||||
content: "'" + Utils_1.Utils.stringToUnicodeEscapeSequence(node.name) + "'",
|
||||
precedence: escodegen.Precedence.Primary
|
||||
}
|
||||
};
|
||||
delete node.name;
|
||||
(0, _assign2.default)(node, literalNode);
|
||||
}
|
||||
}]);
|
||||
return ObjectExpressionObfuscator;
|
||||
}(AbstractNodeTransformer_1.AbstractNodeTransformer);
|
||||
ObjectExpressionObfuscator = tslib_1.__decorate([inversify_1.injectable(), tslib_1.__param(0, inversify_1.inject(ServiceIdentifiers_1.ServiceIdentifiers.IOptions)), tslib_1.__metadata("design:paramtypes", [Object])], ObjectExpressionObfuscator);
|
||||
ObjectExpressionObfuscator = ObjectExpressionObfuscator_1 = tslib_1.__decorate([inversify_1.injectable(), tslib_1.__param(0, inversify_1.inject(ServiceIdentifiers_1.ServiceIdentifiers.IOptions)), tslib_1.__metadata("design:paramtypes", [Object])], ObjectExpressionObfuscator);
|
||||
exports.ObjectExpressionObfuscator = ObjectExpressionObfuscator;
|
||||
var ObjectExpressionObfuscator_1;
|
||||
|
||||
/***/ },
|
||||
/* 95 */
|
||||
|
||||
@@ -34,6 +34,37 @@ export class ObjectExpressionObfuscator extends AbstractNodeTransformer {
|
||||
super(options);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param node
|
||||
* @returns {ESTree.Literal}
|
||||
*/
|
||||
private static obfuscateLiteralPropertyKey (node: ESTree.Literal): ESTree.Literal {
|
||||
if (typeof node.value === 'string' && !node['x-verbatim-property']) {
|
||||
node['x-verbatim-property'] = {
|
||||
content : `'${Utils.stringToUnicodeEscapeSequence(node.value)}'`,
|
||||
precedence: escodegen.Precedence.Primary
|
||||
};
|
||||
}
|
||||
|
||||
return node;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param node
|
||||
* @returns {ESTree.Literal}
|
||||
*/
|
||||
private static obfuscateIdentifierPropertyKey (node: ESTree.Identifier): ESTree.Literal {
|
||||
return {
|
||||
type: NodeType.Literal,
|
||||
value: node.name,
|
||||
raw: `'${node.name}'`,
|
||||
'x-verbatim-property': {
|
||||
content : `'${Utils.stringToUnicodeEscapeSequence(node.name)}'`,
|
||||
precedence: escodegen.Precedence.Primary
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* @param objectExpressionNode
|
||||
*/
|
||||
@@ -44,51 +75,17 @@ export class ObjectExpressionObfuscator extends AbstractNodeTransformer {
|
||||
property.shorthand = false;
|
||||
}
|
||||
|
||||
estraverse.traverse(property.key, {
|
||||
estraverse.replace(property.key, {
|
||||
enter: (node: ESTree.Node, parentNode: ESTree.Node): any => {
|
||||
if (Node.isLiteralNode(node)) {
|
||||
this.obfuscateLiteralPropertyKey(node);
|
||||
|
||||
return;
|
||||
property.key = ObjectExpressionObfuscator.obfuscateLiteralPropertyKey(node);
|
||||
}
|
||||
|
||||
if (Node.isIdentifierNode(node)) {
|
||||
this.obfuscateIdentifierPropertyKey(node);
|
||||
property.key = ObjectExpressionObfuscator.obfuscateIdentifierPropertyKey(node);
|
||||
}
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* @param node
|
||||
*/
|
||||
private obfuscateLiteralPropertyKey (node: ESTree.Literal): void {
|
||||
if (typeof node.value === 'string' && !node['x-verbatim-property']) {
|
||||
node['x-verbatim-property'] = {
|
||||
content : `'${Utils.stringToUnicodeEscapeSequence(node.value)}'`,
|
||||
precedence: escodegen.Precedence.Primary
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param node
|
||||
*/
|
||||
private obfuscateIdentifierPropertyKey (node: ESTree.Identifier): void {
|
||||
const nodeValue: string = node.name;
|
||||
const literalNode: ESTree.Literal = {
|
||||
raw: `'${nodeValue}'`,
|
||||
'x-verbatim-property': {
|
||||
content : `'${Utils.stringToUnicodeEscapeSequence(nodeValue)}'`,
|
||||
precedence: escodegen.Precedence.Primary
|
||||
},
|
||||
type: NodeType.Literal,
|
||||
value: nodeValue
|
||||
};
|
||||
|
||||
delete node.name;
|
||||
|
||||
Object.assign(node, literalNode);
|
||||
}
|
||||
}
|
||||
|
||||
+18
-3
@@ -7,6 +7,11 @@ export class Utils {
|
||||
*/
|
||||
public static readonly hexadecimalPrefix: string = '0x';
|
||||
|
||||
/**
|
||||
* @type {Map<string, string>}
|
||||
*/
|
||||
private static readonly stringToUnicodeEscapeSequenceCache: Map <string, string> = new Map();
|
||||
|
||||
/**
|
||||
* @param array
|
||||
* @param times
|
||||
@@ -128,6 +133,12 @@ export class Utils {
|
||||
* @returns {string}
|
||||
*/
|
||||
public static stringToUnicodeEscapeSequence (string: string, nonLatinAndNonDigitsOnly: boolean = false): string {
|
||||
const cacheKey: string = `${string}-${String(nonLatinAndNonDigitsOnly)}`;
|
||||
|
||||
if (Utils.stringToUnicodeEscapeSequenceCache.has(cacheKey)) {
|
||||
return <string>Utils.stringToUnicodeEscapeSequenceCache.get(cacheKey);
|
||||
}
|
||||
|
||||
const radix: number = 16;
|
||||
const replaceRegExp: RegExp = new RegExp('[\\s\\S]', 'g');
|
||||
const escapeRegExp: RegExp = new RegExp('[^a-zA-Z0-9]');
|
||||
@@ -136,11 +147,11 @@ export class Utils {
|
||||
let prefix: string,
|
||||
template: string;
|
||||
|
||||
return `${string.replace(replaceRegExp, (escape: string): string => {
|
||||
const result: string = string.replace(replaceRegExp, (escape: string): string => {
|
||||
if (nonLatinAndNonDigitsOnly && !escapeRegExp.test(escape)) {
|
||||
return escape;
|
||||
}
|
||||
|
||||
|
||||
if (regexp.test(escape)) {
|
||||
prefix = '\\x';
|
||||
template = '0'.repeat(2);
|
||||
@@ -150,6 +161,10 @@ export class Utils {
|
||||
}
|
||||
|
||||
return `${prefix}${(template + escape.charCodeAt(0).toString(radix)).slice(-template.length)}`;
|
||||
})}`;
|
||||
});
|
||||
|
||||
Utils.stringToUnicodeEscapeSequenceCache.set(cacheKey, result);
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user