Просмотр исходного кода

Fixed identifiers prefix generation for `obfuscateMultiple` method

sanex3339 5 лет назад
Родитель
Сommit
bbc672e80e

+ 4 - 0
CHANGELOG.md

@@ -1,5 +1,9 @@
 Change Log
 
+v0.27.2
+---
+* Fixed identifiers prefix generation for `obfuscateMultiple` method
+
 v0.27.1
 ---
 * Dependencies update, fixed https://www.npmjs.com/advisories/1488

Разница между файлами не показана из-за своего большого размера
+ 0 - 0
dist/index.browser.js


Разница между файлами не показана из-за своего большого размера
+ 0 - 0
dist/index.cli.js


Разница между файлами не показана из-за своего большого размера
+ 0 - 0
dist/index.js


+ 1 - 1
package.json

@@ -1,6 +1,6 @@
 {
   "name": "javascript-obfuscator",
-  "version": "0.27.1",
+  "version": "0.27.2",
   "description": "JavaScript obfuscator",
   "keywords": [
     "obfuscator",

+ 16 - 2
src/JavaScriptObfuscatorFacade.ts

@@ -11,6 +11,7 @@ import { IJavaScriptObfuscator } from './interfaces/IJavaScriptObfsucator';
 import { IObfuscatedCode } from './interfaces/source-code/IObfuscatedCode';
 
 import { InversifyContainerFacade } from './container/InversifyContainerFacade';
+import { Utils } from './utils/Utils';
 
 class JavaScriptObfuscatorFacade {
     /**
@@ -53,12 +54,25 @@ class JavaScriptObfuscatorFacade {
         return Object
             .keys(sourceCodesObject)
             .reduce(
-                (acc: TObfuscationResultsObject<TSourceCodesObject>, sourceCodeIdentifier: keyof TSourceCodesObject) => {
+                (
+                    acc: TObfuscationResultsObject<TSourceCodesObject>,
+                    sourceCodeIdentifier: keyof TSourceCodesObject,
+                    index: number
+                ) => {
+                    const identifiersPrefix: string = Utils.getIdentifiersPrefixForMultipleSources(
+                        inputOptions.identifiersPrefix,
+                        index
+                    );
+
                     const sourceCode: string = sourceCodesObject[sourceCodeIdentifier];
+                    const sourceCodeOptions: TInputOptions = {
+                        ...inputOptions,
+                        identifiersPrefix
+                    };
 
                     return {
                         ...acc,
-                        [sourceCodeIdentifier]: JavaScriptObfuscatorFacade.obfuscate(sourceCode, inputOptions)
+                        [sourceCodeIdentifier]: JavaScriptObfuscatorFacade.obfuscate(sourceCode, sourceCodeOptions)
                     };
                 },
                 <TObfuscationResultsObject<TSourceCodesObject>>{}

+ 4 - 8
src/cli/JavaScriptObfuscatorCLI.ts

@@ -46,11 +46,6 @@ export class JavaScriptObfuscatorCLI implements IInitializable {
      */
     public static readonly obfuscatedFilePrefix: string = '-obfuscated';
 
-    /**
-     * @type {string}
-     */
-    private static readonly baseIdentifiersPrefix: string = 'a';
-
     /**
      * @type {string[]}
      */
@@ -413,9 +408,10 @@ export class JavaScriptObfuscatorCLI implements IInitializable {
         );
 
         if (sourceCodeIndex !== null) {
-            const baseIdentifiersPrefix: string = this.inputCLIOptions.identifiersPrefix
-                ?? JavaScriptObfuscatorCLI.baseIdentifiersPrefix;
-            const identifiersPrefix: string = `${baseIdentifiersPrefix}${sourceCodeIndex}`;
+            const identifiersPrefix: string = Utils.getIdentifiersPrefixForMultipleSources(
+                this.inputCLIOptions.identifiersPrefix,
+                sourceCodeIndex
+            );
 
             options = {
                 ...options,

+ 21 - 0
src/utils/Utils.ts

@@ -1,4 +1,9 @@
 export class Utils {
+    /**
+     * @type {string}
+     */
+    public static readonly baseMultipleSourcesIdentifiersPrefix: string = 'a';
+
     /**
      * @type {string}
      */
@@ -36,4 +41,20 @@ export class Utils {
 
         return domain;
     }
+
+    /**
+     * @param {string | undefined} identifiersPrefix
+     * @param {number} sourceCodeIndex
+     * @returns {string}
+     */
+    public static getIdentifiersPrefixForMultipleSources (
+        identifiersPrefix: string | undefined,
+        sourceCodeIndex: number
+    ): string {
+        const baseIdentifiersPrefix: string = !!identifiersPrefix
+            ? identifiersPrefix
+            : Utils.baseMultipleSourcesIdentifiersPrefix;
+
+        return `${baseIdentifiersPrefix}${sourceCodeIndex}`;
+    }
 }

+ 2 - 2
test/functional-tests/javascript-obfuscator/JavaScriptObfuscator.spec.ts

@@ -903,8 +903,8 @@ describe('JavaScriptObfuscator', () => {
 
     describe('obfuscateMultiple', () => {
         describe('multiple source codes', () => {
-            const regExp1: RegExp = /var _0x(\w){4,6} *= *0x1;/;
-            const regExp2: RegExp = /var _0x(\w){4,6} *= *'abc';/;
+            const regExp1: RegExp = /var a0_0x(\w){4,6} *= *0x1;/;
+            const regExp2: RegExp = /var a1_0x(\w){4,6} *= *'abc';/;
 
             let obfuscatedCode1: string;
             let obfuscatedCode2: string;

+ 36 - 0
test/unit-tests/utils/Utils.spec.ts

@@ -123,4 +123,40 @@ describe('Utils', () => {
             });
         });
     });
+
+    describe('getIdentifiersPrefixForMultipleSources', () => {
+        describe('Variant #1: should get identifiers prefix for identifiers prefix from options', () => {
+            const expectedIdentifiersPrefix: string = 'foo1';
+
+            let identifiersPrefix: string;
+
+            before(() => {
+                identifiersPrefix = Utils.getIdentifiersPrefixForMultipleSources(
+                    'foo',
+                    1
+                );
+            });
+
+            it('should return correct identifiers prefix', () => {
+                assert.equal(identifiersPrefix, expectedIdentifiersPrefix);
+            });
+        });
+
+        describe('Variant #2: should get identifiers prefix for base identifiers prefix for multiple sources', () => {
+            const expectedIdentifiersPrefix: string = 'a1';
+
+            let identifiersPrefix: string;
+
+            before(() => {
+                identifiersPrefix = Utils.getIdentifiersPrefixForMultipleSources(
+                    undefined,
+                    1
+                );
+            });
+
+            it('should return correct identifiers prefix', () => {
+                assert.equal(identifiersPrefix, expectedIdentifiersPrefix);
+            });
+        });
+    });
 });

Некоторые файлы не были показаны из-за большого количества измененных файлов