10000 Merge pull request #478 from javascript-obfuscator/fix-source-map-fil… · sec-js/javascript-obfuscator@5b27365 · GitHub
[go: up one dir, main page]

Skip to content

Commit 5b27365

Browse files
authored
Merge pull request javascript-obfuscator#478 from javascript-obfuscator/fix-source-map-file-name-rule
Improved source map file name normalizer logic
2 parents 84492b3 + cd72887 commit 5b27365

File tree

9 files changed

+167
-24
lines changed

9 files changed

+167
-24
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ Change Log
33
v0.21.0
44
---
55
* Improved `transformObjectKeys` transformation to cover more cases
6+
* Fixed https://github.com/javascript-obfuscator/javascript-obfuscator/issues/406
67

78
v0.20.4
89
---

dist/index.browser.js

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

dist/index.cli.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/index.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/cli/utils/CLIUtils.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import * as path from 'path';
44

55
import { TObject } from '../../types/TObject';
66

7+
import { StringSeparator } from '../../enums/StringSeparator';
8+
79
import { JavaScriptObfuscatorCLI } from '../JavaScriptObfuscatorCLI';
810

911
export class CLIUtils {
@@ -14,11 +16,11 @@ export class CLIUtils {
1416
public static getOutputCodePath (inputPath: string): string {
1517
return path
1618
.normalize(inputPath)
17-
.split('.')
19+
.split(StringSeparator.Dot)
1820
.map((value: string, index: number) => {
1921
return index === 0 ? `${value}${JavaScriptObfuscatorCLI.obfuscatedFilePrefix}` : value;
2022
})
21-
.join('.');
23+
.join(StringSeparator.Dot);
2224
}
2325

2426
/**
@@ -34,7 +36,7 @@ export class CLIUtils {
3436
}
3537

3638
if (!/\.js\.map$/.test(outputCodePath)) {
37-
outputCodePath = `${outputCodePath.split('.')[0]}.js.map`;
39+
outputCodePath = `${outputCodePath.split(StringSeparator.Dot)[0]}.js.map`;
3840
} else if (/\.js$/.test(outputCodePath)) {
3941
outputCodePath += '.map';
4042
}

src/enums/StringSeparator.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export enum StringSeparator {
2+
Dot = '.'
3+
}

src/options/normalizer-rules/InputFileNameRule.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ import { TOptionsNormalizerRule } from '../../types/options/TOptionsNormalizerRu
22

33
import { IOptions } from '../../interfaces/options/IOptions';
44

5+
import { StringSeparator } from '../../enums/StringSeparator';
6+
57
/**
68
* @param {IOptions} options
79
* @returns {IOptions}
@@ -12,9 +14,9 @@ export const InputFileNameRule: TOptionsNormalizerRule = (options: IOptions): IO
1214
if (inputFileName) {
1315
inputFileName = inputFileName
1416
.replace(/^\/+/, '')
15-
.split('.')
17+
.split(StringSeparator.Dot)
1618
.slice(0, -1)
17-
.join('.') || inputFileName;
19+
.join(StringSeparator.Dot) || inputFileName;
1820

1921
options = {
2022
...options,

src/options/normalizer-rules/SourceMapFileNameRule.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ import { TOptionsNormalizerRule } from '../../types/options/TOptionsNormalizerRu
22

33
import { IOptions } from '../../interfaces/options/IOptions';
44

5+
import { StringSeparator } from '../../enums/StringSeparator';
6+
57
/**
68
* @param {IOptions} options
79
* @returns {IOptions}
@@ -12,7 +14,18 @@ export const SourceMapFileNameRule: TOptionsNormalizerRule = (options: IOptions)
1214
if (sourceMapFileName) {
1315
sourceMapFileName = sourceMapFileName
1416
.replace(/^\/+/, '')
15-
.split('.')[0];
17+
.replace(/(?:\.js)?(?:\.map)?$/, '');
18+
19+
let sourceMapFileNameParts: string[] = sourceMapFileName.split(StringSeparator.Dot);
20+
const sourceMapFileNamePartsCount: number = sourceMapFileNameParts.length;
21+
const lastPart: string = sourceMapFileNameParts[sourceMapFileNamePartsCount - 1];
22+
23< F438 span class="diff-text-marker">+
// try to predict if last part is extension or not
24+
if (sourceMapFileNamePartsCount > 1 && lastPart.length <= 3) {
25+
sourceMapFileNameParts = sourceMapFileNameParts.slice(0, -1);
26+
}
27+
28+
sourceMapFileName = sourceMapFileNameParts.join(StringSeparator.Dot);
1629

1730
options = {
1831
...options,

test/functional-tests/options/OptionsNormalizer.spec.ts

Lines changed: 134 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -374,22 +374,144 @@ describe('OptionsNormalizer', () => {
374374
});
375375

376376
describe('sourceMapFileNameRule', () => {
377-
before(() => {
378-
optionsPreset = getNormalizedOptions({
379-
...getDefaultOptions(),
380-
sourceMapBaseUrl: 'http://localhost:9000',
381-
sourceMapFileName: '//outputSourceMapName'
377+
describe('Base filename without extension', () => {
378+
before(() => {
379+
optionsPreset = getNormalizedOptions({
380+
...getDefaultOptions(),
381+
sourceMapBaseUrl: 'http://localhost:9000',
382+
sourceMapFileName: 'outputSourceMapName'
383+
});
384+
385+
expectedOptionsPreset = {
386+
...getDefaultOptions(),
387+
sourceMapBaseUrl: 'http://localhost:9000/',
388+
sourceMapFileName: 'outputSourceMapName.js.map'
389+
};
382390
});
383391

384-
expectedOptionsPreset = {
385-
...getDefaultOptions(),
386-
sourceMapBaseUrl: 'http://localhost:9000/',
387-
sourceMapFileName: 'outputSourceMapName.js.map'
388-
};
392+
it('should normalize options preset', () => {
393+
assert.deepEqual(optionsPreset, expectedOptionsPreset);
394+
});
389395
});
390396

391-
it('should normalize options preset', () => {
392-
assert.deepEqual(optionsPreset, expectedOptionsPreset);
397+
describe('Slashes in file name', () => {
398+
before(() => {
399+
optionsPreset = getNormalizedOptions({
400+
...getDefaultOptions(),
401+
sourceMapBaseUrl: 'http://localhost:9000',
402+
sourceMapFileName: '//outputSourceMapName'
403+
});
404+
405+
expectedOptionsPreset = {
406+
...getDefaultOptions(),
407+
sourceMapBaseUrl: 'http://localhost:9000/',
408+
sourceMapFileName: 'outputSourceMapName.js.map'
409+
};
410+
});
411+
412+
it('should normalize options preset', () => {
413+
assert.deepEqual(optionsPreset, expectedOptionsPreset);
414+
});
415+
});
416+
417+
describe('`js` file extension in file name', () => {
418+
before(() => {
419+
optionsPreset = getNormalizedOptions({
420+
...getDefaultOptions(),
421+
sourceMapBaseUrl: 'http://localhost:9000',
422+
sourceMapFileName: 'outputSourceMapName.js'
423+
});
424+
425+
expectedOptionsPreset = {
426+
...getDefaultOptions(),
427+
sourceMapBaseUrl: 'http://localhost:9000/',
428+
sourceMapFileName: 'outputSourceMapName.js.map'
429+
};
430+
});
431+
432+
it('should normalize options preset', () => {
433+
assert.deepEqual(optionsPreset, expectedOptionsPreset);
434+
});
435+
});
436+
437+
describe('Non `js` file extension in file name', () => {
438+
before(() => {
439+
optionsPreset = getNormalizedOptions({
440+
...getDefaultOptions(),
441+
sourceMapBaseUrl: 'http://localhost:9000',
442+
sourceMapFileName: 'outputSourceMapName.exe'
443+
});
444+
445+
expectedOptionsPreset = {
446+
...getDefaultOptions(),
447+
sourceMapBaseUrl: 'http://localhost:9000/',
448+
sourceMapFileName: 'outputSourceMapName.js.map'
449+
};
450+
});
451+
452+
it('should normalize options preset', () => {
453+
assert.deepEqual(optionsPreset, expectedOptionsPreset);
454+
});
455+
});
456+
457+
describe('File hash in file name', () => {
458+
before(() => {
459+
optionsPreset = getNormalizedOptions({
460+
...getDefaultOptions(),
461+
sourceMapBaseUrl: 'http://localhost:9000',
462+
sourceMapFileName: 'outputSourceMapName.7e2c49a622975ebd9b7e'
463+
});
464+
465+
expectedOptionsPreset = {
466+
...getDefaultOptions(),
467+
sourceMapBaseUrl: 'http://localhost:9000/',
468+
sourceMapFileName: 'outputSourceMapName.7e2c49a622975ebd9b7e.js.map'
469+
};
470+
});
471+
472+
it('should normalize options preset', () => {
473+
assert.deepEqual(optionsPreset, expectedOptionsPreset);
474+
});
4 17AE 75+
});
476+
477+
describe('File hash and `js` file extension in file name #1', () => {
478+
before(() => {
479+
optionsPreset = getNormalizedOptions({
480+
...getDefaultOptions(),
481+
sourceMapBaseUrl: 'http://localhost:9000',
482+
sourceMapFileName: 'outputSourceMapName.7e2c49a622975ebd9b7e.js'
483+
});
484+
485+
expectedOptionsPreset = {
486+
...getDefaultOptions(),
487+
sourceMapBaseUrl: 'http://localhost:9000/',
488+
sourceMapFileName: 'outputSourceMapName.7e2c49a622975ebd9b7e.js.map'
489+
};
490+
});
491+
492+
it('should normalize options preset', () => {
493+
assert.deepEqual(optionsPreset, expectedOptionsPreset);
494+
});
495+
});
496+
497+
describe('File hash and non `js` file extension in file name', () => {
498+
before(() => {
499+
optionsPreset = getNormalizedOptions({
500+
...getDefaultOptions(),
501+
sourceMapBaseUrl: 'http://localhost:9000',
502+
sourceMapFileName: 'outputSourceMapName.7e2c49a622975ebd9b7e.exe'
503+
});
504+
505+
expectedOptionsPreset = {
506+
...getDefaultOptions(),
507+
sourceMapBaseUrl: 'http://localhost:9000/',
508+
sourceMapFileName: 'outputSourceMapName.7e2c49a622975ebd9b7e.js.map'
509+
};
510+
});
511+
512+
it('should normalize options preset', () => {
513+
assert.deepEqual(optionsPreset, expectedOptionsPreset);
514+
});
393515
});
394516
});
395517

0 commit comments

Comments
 (0)
0