소스 검색

Improved source map file name normalizer logic
Fixed https://github.com/javascript-obfuscator/javascript-obfuscator/issues/406

sanex3339 5 년 전
부모
커밋
cd728875f6

+ 1 - 0
CHANGELOG.md

@@ -3,6 +3,7 @@ Change Log
 v0.21.0
 ---
 * Improved `transformObjectKeys` transformation to cover more cases
+* Fixed https://github.com/javascript-obfuscator/javascript-obfuscator/issues/406
 
 v0.20.4
 ---

파일 크기가 너무 크기때문에 변경 상태를 표시하지 않습니다.
+ 0 - 0
dist/index.browser.js


파일 크기가 너무 크기때문에 변경 상태를 표시하지 않습니다.
+ 0 - 0
dist/index.cli.js


파일 크기가 너무 크기때문에 변경 상태를 표시하지 않습니다.
+ 0 - 0
dist/index.js


+ 5 - 3
src/cli/utils/CLIUtils.ts

@@ -4,6 +4,8 @@ import * as path from 'path';
 
 import { TObject } from '../../types/TObject';
 
+import { StringSeparator } from '../../enums/StringSeparator';
+
 import { JavaScriptObfuscatorCLI } from '../JavaScriptObfuscatorCLI';
 
 export class CLIUtils {
@@ -14,11 +16,11 @@ export class CLIUtils {
     public static getOutputCodePath (inputPath: string): string {
         return path
             .normalize(inputPath)
-            .split('.')
+            .split(StringSeparator.Dot)
             .map((value: string, index: number) => {
                 return index === 0 ? `${value}${JavaScriptObfuscatorCLI.obfuscatedFilePrefix}` : value;
             })
-            .join('.');
+            .join(StringSeparator.Dot);
     }
 
     /**
@@ -34,7 +36,7 @@ export class CLIUtils {
         }
 
         if (!/\.js\.map$/.test(outputCodePath)) {
-            outputCodePath = `${outputCodePath.split('.')[0]}.js.map`;
+            outputCodePath = `${outputCodePath.split(StringSeparator.Dot)[0]}.js.map`;
         } else if (/\.js$/.test(outputCodePath)) {
             outputCodePath += '.map';
         }

+ 3 - 0
src/enums/StringSeparator.ts

@@ -0,0 +1,3 @@
+export enum StringSeparator {
+    Dot = '.'
+}

+ 4 - 2
src/options/normalizer-rules/InputFileNameRule.ts

@@ -2,6 +2,8 @@ import { TOptionsNormalizerRule } from '../../types/options/TOptionsNormalizerRu
 
 import { IOptions } from '../../interfaces/options/IOptions';
 
+import { StringSeparator } from '../../enums/StringSeparator';
+
 /**
  * @param {IOptions} options
  * @returns {IOptions}
@@ -12,9 +14,9 @@ export const InputFileNameRule: TOptionsNormalizerRule = (options: IOptions): IO
     if (inputFileName) {
         inputFileName = inputFileName
             .replace(/^\/+/, '')
-            .split('.')
+            .split(StringSeparator.Dot)
             .slice(0, -1)
-            .join('.') || inputFileName;
+            .join(StringSeparator.Dot) || inputFileName;
 
         options = {
             ...options,

+ 14 - 1
src/options/normalizer-rules/SourceMapFileNameRule.ts

@@ -2,6 +2,8 @@ import { TOptionsNormalizerRule } from '../../types/options/TOptionsNormalizerRu
 
 import { IOptions } from '../../interfaces/options/IOptions';
 
+import { StringSeparator } from '../../enums/StringSeparator';
+
 /**
  * @param {IOptions} options
  * @returns {IOptions}
@@ -12,7 +14,18 @@ export const SourceMapFileNameRule: TOptionsNormalizerRule = (options: IOptions)
     if (sourceMapFileName) {
         sourceMapFileName = sourceMapFileName
             .replace(/^\/+/, '')
-            .split('.')[0];
+            .replace(/(?:\.js)?(?:\.map)?$/, '');
+
+        let sourceMapFileNameParts: string[] = sourceMapFileName.split(StringSeparator.Dot);
+        const sourceMapFileNamePartsCount: number = sourceMapFileNameParts.length;
+        const lastPart: string = sourceMapFileNameParts[sourceMapFileNamePartsCount - 1];
+
+        // try to predict if last part is extension or not
+        if (sourceMapFileNamePartsCount > 1 && lastPart.length <= 3) {
+            sourceMapFileNameParts = sourceMapFileNameParts.slice(0, -1);
+        }
+
+        sourceMapFileName = sourceMapFileNameParts.join(StringSeparator.Dot);
 
         options = {
             ...options,

+ 134 - 12
test/functional-tests/options/OptionsNormalizer.spec.ts

@@ -374,22 +374,144 @@ describe('OptionsNormalizer', () => {
         });
 
         describe('sourceMapFileNameRule', () => {
-            before(() => {
-                optionsPreset = getNormalizedOptions({
-                    ...getDefaultOptions(),
-                    sourceMapBaseUrl: 'http://localhost:9000',
-                    sourceMapFileName: '//outputSourceMapName'
+            describe('Base filename without extension', () => {
+                before(() => {
+                    optionsPreset = getNormalizedOptions({
+                        ...getDefaultOptions(),
+                        sourceMapBaseUrl: 'http://localhost:9000',
+                        sourceMapFileName: 'outputSourceMapName'
+                    });
+
+                    expectedOptionsPreset = {
+                        ...getDefaultOptions(),
+                        sourceMapBaseUrl: 'http://localhost:9000/',
+                        sourceMapFileName: 'outputSourceMapName.js.map'
+                    };
                 });
 
-                expectedOptionsPreset = {
-                    ...getDefaultOptions(),
-                    sourceMapBaseUrl: 'http://localhost:9000/',
-                    sourceMapFileName: 'outputSourceMapName.js.map'
-                };
+                it('should normalize options preset', () => {
+                    assert.deepEqual(optionsPreset, expectedOptionsPreset);
+                });
             });
 
-            it('should normalize options preset', () => {
-                assert.deepEqual(optionsPreset, expectedOptionsPreset);
+            describe('Slashes in file name', () => {
+                before(() => {
+                    optionsPreset = getNormalizedOptions({
+                        ...getDefaultOptions(),
+                        sourceMapBaseUrl: 'http://localhost:9000',
+                        sourceMapFileName: '//outputSourceMapName'
+                    });
+
+                    expectedOptionsPreset = {
+                        ...getDefaultOptions(),
+                        sourceMapBaseUrl: 'http://localhost:9000/',
+                        sourceMapFileName: 'outputSourceMapName.js.map'
+                    };
+                });
+
+                it('should normalize options preset', () => {
+                    assert.deepEqual(optionsPreset, expectedOptionsPreset);
+                });
+            });
+
+            describe('`js` file extension in file name', () => {
+                before(() => {
+                    optionsPreset = getNormalizedOptions({
+                        ...getDefaultOptions(),
+                        sourceMapBaseUrl: 'http://localhost:9000',
+                        sourceMapFileName: 'outputSourceMapName.js'
+                    });
+
+                    expectedOptionsPreset = {
+                        ...getDefaultOptions(),
+                        sourceMapBaseUrl: 'http://localhost:9000/',
+                        sourceMapFileName: 'outputSourceMapName.js.map'
+                    };
+                });
+
+                it('should normalize options preset', () => {
+                    assert.deepEqual(optionsPreset, expectedOptionsPreset);
+                });
+            });
+
+            describe('Non `js` file extension in file name', () => {
+                before(() => {
+                    optionsPreset = getNormalizedOptions({
+                        ...getDefaultOptions(),
+                        sourceMapBaseUrl: 'http://localhost:9000',
+                        sourceMapFileName: 'outputSourceMapName.exe'
+                    });
+
+                    expectedOptionsPreset = {
+                        ...getDefaultOptions(),
+                        sourceMapBaseUrl: 'http://localhost:9000/',
+                        sourceMapFileName: 'outputSourceMapName.js.map'
+                    };
+                });
+
+                it('should normalize options preset', () => {
+                    assert.deepEqual(optionsPreset, expectedOptionsPreset);
+                });
+            });
+
+            describe('File hash in file name', () => {
+                before(() => {
+                    optionsPreset = getNormalizedOptions({
+                        ...getDefaultOptions(),
+                        sourceMapBaseUrl: 'http://localhost:9000',
+                        sourceMapFileName: 'outputSourceMapName.7e2c49a622975ebd9b7e'
+                    });
+
+                    expectedOptionsPreset = {
+                        ...getDefaultOptions(),
+                        sourceMapBaseUrl: 'http://localhost:9000/',
+                        sourceMapFileName: 'outputSourceMapName.7e2c49a622975ebd9b7e.js.map'
+                    };
+                });
+
+                it('should normalize options preset', () => {
+                    assert.deepEqual(optionsPreset, expectedOptionsPreset);
+                });
+            });
+
+            describe('File hash and `js` file extension in file name #1', () => {
+                before(() => {
+                    optionsPreset = getNormalizedOptions({
+                        ...getDefaultOptions(),
+                        sourceMapBaseUrl: 'http://localhost:9000',
+                        sourceMapFileName: 'outputSourceMapName.7e2c49a622975ebd9b7e.js'
+                    });
+
+                    expectedOptionsPreset = {
+                        ...getDefaultOptions(),
+                        sourceMapBaseUrl: 'http://localhost:9000/',
+                        sourceMapFileName: 'outputSourceMapName.7e2c49a622975ebd9b7e.js.map'
+                    };
+                });
+
+                it('should normalize options preset', () => {
+                    assert.deepEqual(optionsPreset, expectedOptionsPreset);
+                });
+            });
+
+            describe('File hash and non `js` file extension in file name', () => {
+                before(() => {
+                    optionsPreset = getNormalizedOptions({
+                        ...getDefaultOptions(),
+                        sourceMapBaseUrl: 'http://localhost:9000',
+                        sourceMapFileName: 'outputSourceMapName.7e2c49a622975ebd9b7e.exe'
+                    });
+
+                    expectedOptionsPreset = {
+                        ...getDefaultOptions(),
+                        sourceMapBaseUrl: 'http://localhost:9000/',
+                        sourceMapFileName: 'outputSourceMapName.7e2c49a622975ebd9b7e.js.map'
+                    };
+                });
+
+                it('should normalize options preset', () => {
+                    assert.deepEqual(optionsPreset, expectedOptionsPreset);
+                });
             });
         });
 

이 변경점에서 너무 많은 파일들이 변경되어 몇몇 파일들은 표시되지 않았습니다.