Skip to content

Fixed performance regression of Initializing stage after 1.2.0 #646

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 28, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
Change Log

v1.2.2
---
* Fixed performance regression of `Initializing` stage after `1.2.0`

v1.2.1
---
* Support of old browsers when `selfDefending` is enabled. https://github.com/javascript-obfuscator/javascript-obfuscator/issues/615
Expand Down
12 changes: 6 additions & 6 deletions dist/index.browser.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/index.cli.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/index.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "javascript-obfuscator",
"version": "1.2.1",
"version": "1.2.2",
"description": "JavaScript obfuscator",
"keywords": [
"obfuscator",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { IVisitor } from '../../interfaces/node-transformers/IVisitor';
import { NodeTransformationStage } from '../../enums/node-transformers/NodeTransformationStage';

import { AbstractNodeTransformer } from '../AbstractNodeTransformer';
import { ConditionalCommentObfuscatingGuard } from '../preparing-transformers/obfuscating-guards/ConditionalCommentObfuscatingGuard';
import { NodeGuards } from '../../node/NodeGuards';

@injectable()
Expand All @@ -32,8 +33,6 @@ export class CommentsTransformer extends AbstractNodeTransformer {
@inject(ServiceIdentifiers.IOptions) options: IOptions
) {
super(randomGenerator, options);

this.filterComment = this.filterComment.bind(this);
}

/**
Expand All @@ -55,7 +54,7 @@ export class CommentsTransformer extends AbstractNodeTransformer {
return {
leave: (node: ESTree.Node): ESTree.Node | undefined => {
if (NodeGuards.isProgramNode(node)) {
return this.filterComments(node);
return this.filterCommentsOnFinalizingTraverse(node);
}
}
};
Expand All @@ -69,6 +68,8 @@ export class CommentsTransformer extends AbstractNodeTransformer {
* Moves comments to their nodes
*/
public transformNode (rootNode: ESTree.Program): ESTree.Node {
rootNode = this.filterCommentsOnPrimaryTraverse(rootNode);

if (!rootNode.comments || !rootNode.comments.length) {
return rootNode;
}
Expand Down Expand Up @@ -113,22 +114,43 @@ export class CommentsTransformer extends AbstractNodeTransformer {
return rootNode;
}

/**
* Removes all comments from node except comments that contain
* preserved words or `javascript-obfuscator` words
*
* @param {ESTree.Program} rootNode
* @returns {ESTree.Program}
*/
private filterCommentsOnPrimaryTraverse (rootNode: ESTree.Program): ESTree.Program {
rootNode.comments = rootNode.comments?.filter((comment: ESTree.Comment) =>
this.filterComment(comment, true)
);

return rootNode;
}

/**
* Removes all comments from node except comments that contain preserved words
*
* @param {Node} rootNode
* @returns {NodeGuards}
* @param {ESTree.Program} rootNode
* @returns {ESTree.Program}
*/
public filterComments (rootNode: ESTree.Program): ESTree.Node {
estraverse.traverse(rootNode, {
enter: (node: ESTree.Node): void => {
private filterCommentsOnFinalizingTraverse (rootNode: ESTree.Program): ESTree.Program {
estraverse.replace(rootNode, {
enter: (node: ESTree.Node): ESTree.Node => {
if (node.leadingComments) {
node.leadingComments = node.leadingComments?.filter(this.filterComment);
node.leadingComments = node.leadingComments?.filter((comment: ESTree.Comment) =>
this.filterComment(comment, false)
);
}

if (node.trailingComments) {
node.trailingComments = node.trailingComments?.filter(this.filterComment);
node.trailingComments = node.trailingComments?.filter((comment: ESTree.Comment) =>
this.filterComment(comment, false)
);
}

return node;
}
});

Expand All @@ -137,9 +159,14 @@ export class CommentsTransformer extends AbstractNodeTransformer {

/**
* @param {ESTree.Comment} comment
* @param {boolean} keepConditionalComment
* @returns {boolean}
*/
private filterComment (comment: ESTree.Comment): boolean {
private filterComment (comment: ESTree.Comment, keepConditionalComment: boolean): boolean {
if (keepConditionalComment && ConditionalCommentObfuscatingGuard.isConditionalComment(comment)) {
return true;
}

return CommentsTransformer.preservedWords
.some((preservedWord: string) => comment.value.includes(preservedWord));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,15 @@ export class ConditionalCommentObfuscatingGuard implements IObfuscatingGuard {
*/
private obfuscationAllowed: boolean = true;

/**
* @param {Comment} comment
* @returns {boolean}
*/
public static isConditionalComment (comment: ESTree.Comment): boolean {
return ConditionalCommentObfuscatingGuard.obfuscationEnableCommentRegExp.test(comment.value) ||
ConditionalCommentObfuscatingGuard.obfuscationDisableCommentRegExp.test(comment.value);
}

/**
* @returns {boolean}
* @param node
Expand Down