Skip to content

Improved source map file name normalizer logic #478

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
Jan 5, 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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
---
Expand Down
8 changes: 4 additions & 4 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.

8 changes: 5 additions & 3 deletions src/cli/utils/CLIUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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);
}

/**
Expand All @@ -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';
}
Expand Down
3 changes: 3 additions & 0 deletions src/enums/StringSeparator.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export enum StringSeparator {
Dot = '.'
}
6 changes: 4 additions & 2 deletions src/options/normalizer-rules/InputFileNameRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand All @@ -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,
Expand Down
15 changes: 14 additions & 1 deletion src/options/normalizer-rules/SourceMapFileNameRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand All @@ -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,
Expand Down
146 changes: 134 additions & 12 deletions test/functional-tests/options/OptionsNormalizer.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
});
});

Expand Down