X Tutup
Skip to content
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.17.0
---
* **Browser version**: Added browser version dist
* **New Node API option:** `inputFileName` allows to set name of the input file with source code. This name will used internally, for example, for source map generation.
* [#274](https://github.com/javascript-obfuscator/javascript-obfuscator/pull/274)`domainLock` now will work in SVG.
<br/>
Fixed https://github.com/javascript-obfuscator/javascript-obfuscator/issues/273
Expand Down
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,7 @@ Following options are available for the JS Obfuscator:
domainLock: [],
identifierNamesGenerator: 'hexadecimal',
identifiersPrefix: '',
inputFileName: '',
log: false,
renameGlobals: false,
reservedNames: [],
Expand Down Expand Up @@ -600,6 +601,11 @@ Sets prefix for all global identifiers.

Use this option when you want to obfuscate multiple files. This option helps to avoid conflicts between global identifiers of these files. Prefix should be different for every file.

### `inputFileName`
Type: `string` Default: `''`

Allows to set name of the input file with source code. This name will used internally for source map generation.

### `log`
Type: `boolean` Default: `false`

Expand Down
6 changes: 3 additions & 3 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 src/JavaScriptObfuscator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ export class JavaScriptObfuscator implements IJavaScriptObfuscator {
};

if (this.options.sourceMap) {
escodegenParams.sourceMap = 'sourceMap';
escodegenParams.sourceMap = this.options.inputFileName || 'sourceMap';
escodegenParams.sourceContent = sourceCode;
}

Expand Down
4 changes: 3 additions & 1 deletion src/cli/JavaScriptObfuscatorCLI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -173,11 +173,13 @@ export class JavaScriptObfuscatorCLI implements IInitializable {
const configFilePath: string | undefined = this.inputCLIOptions.config;
const configFileLocation: string = configFilePath ? path.resolve(configFilePath, '.') : '';
const configFileOptions: TInputOptions = configFileLocation ? CLIUtils.getUserConfig(configFileLocation) : {};
const inputFileName: string = path.basename(this.inputPath);

return {
...DEFAULT_PRESET,
...configFileOptions,
...inputCLIOptions
...inputCLIOptions,
inputFileName
};
}

Expand Down
1 change: 1 addition & 0 deletions src/interfaces/options/IOptions.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export interface IOptions {
readonly domainLock: string[];
readonly identifierNamesGenerator: IdentifierNamesGenerator;
readonly identifiersPrefix: string;
readonly inputFileName: string;
readonly log: boolean;
readonly renameGlobals: boolean;
readonly reservedNames: string[];
Expand Down
6 changes: 6 additions & 0 deletions src/options/Options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,12 @@ export class Options implements IOptions {
@IsString()
public readonly identifiersPrefix!: string;

/**
* @type {string}
*/
@IsString()
public readonly inputFileName!: string;

/**
* @type {boolean}
*/
Expand Down
2 changes: 2 additions & 0 deletions src/options/OptionsNormalizer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { ControlFlowFlatteningThresholdRule } from './normalizer-rules/ControlFl
import { DeadCodeInjectionRule } from './normalizer-rules/DeadCodeInjectionRule';
import { DeadCodeInjectionThresholdRule } from './normalizer-rules/DeadCodeInjectionThresholdRule';
import { DomainLockRule } from './normalizer-rules/DomainLockRule';
import { InputFileNameRule } from './normalizer-rules/InputFileNameRule';
import { SelfDefendingRule } from './normalizer-rules/SelfDefendingRule';
import { SourceMapBaseUrlRule } from './normalizer-rules/SourceMapBaseUrlRule';
import { SourceMapFileNameRule } from './normalizer-rules/SourceMapFileNameRule';
Expand All @@ -26,6 +27,7 @@ export class OptionsNormalizer implements IOptionsNormalizer {
DeadCodeInjectionRule,
DeadCodeInjectionThresholdRule,
DomainLockRule,
InputFileNameRule,
SelfDefendingRule,
SourceMapBaseUrlRule,
SourceMapFileNameRule,
Expand Down
26 changes: 26 additions & 0 deletions src/options/normalizer-rules/InputFileNameRule.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { TOptionsNormalizerRule } from '../../types/options/TOptionsNormalizerRule';

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

/**
* @param {IOptions} options
* @returns {IOptions}
*/
export const InputFileNameRule: TOptionsNormalizerRule = (options: IOptions): IOptions => {
let { inputFileName } = options;

if (inputFileName) {
inputFileName = inputFileName
.replace(/^\/+/, '')
.split('.')
.slice(0, -1)
.join('.') || inputFileName;

options = {
...options,
inputFileName: `${inputFileName}.js`
};
}

return options;
};
1 change: 1 addition & 0 deletions src/options/presets/Default.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export const DEFAULT_PRESET: TInputOptions = Object.freeze({
exclude: [],
identifierNamesGenerator: IdentifierNamesGenerator.HexadecimalIdentifierNamesGenerator,
identifiersPrefix: '',
inputFileName: '',
log: false,
renameGlobals: false,
reservedNames: [],
Expand Down
1 change: 1 addition & 0 deletions src/options/presets/NoCustomNodes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export const NO_ADDITIONAL_NODES_PRESET: TInputOptions = Object.freeze({
exclude: [],
identifierNamesGenerator: IdentifierNamesGenerator.HexadecimalIdentifierNamesGenerator,
identifiersPrefix: '',
inputFileName: '',
log: false,
renameGlobals: false,
reservedNames: [],
Expand Down
18 changes: 18 additions & 0 deletions test/functional-tests/cli/JavaScriptObfuscatorCLI.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import * as fs from 'fs';
import * as mkdirp from 'mkdirp';
import * as path from 'path';
import * as rimraf from 'rimraf';
import * as sinon from 'sinon';

Expand Down Expand Up @@ -499,6 +500,8 @@ describe('JavaScriptObfuscatorCLI', function (): void {

describe('Variant #1: `--sourceMapMode` option value is `separate`', () => {
describe('Variant #1: default behaviour', () => {
const expectedSourceMapSourceName: string = path.basename(fixtureFileName);

let isFileExist: boolean,
sourceMapObject: any;

Expand Down Expand Up @@ -539,6 +542,10 @@ describe('JavaScriptObfuscatorCLI', function (): void {
assert.property(sourceMapObject, 'sources');
});

it('source map source should has correct name', () => {
assert.equal(sourceMapObject.sources[0], expectedSourceMapSourceName);
});

it('source map from created file should contains property `names`', () => {
assert.property(sourceMapObject, 'names');
});
Expand All @@ -550,6 +557,8 @@ describe('JavaScriptObfuscatorCLI', function (): void {
});

describe('Variant #2: `sourceMapBaseUrl` option is set', () => {
const expectedSourceMapSourceName: string = path.basename(fixtureFileName);

let isFileExist: boolean,
sourceMapObject: any;

Expand Down Expand Up @@ -592,6 +601,10 @@ describe('JavaScriptObfuscatorCLI', function (): void {
assert.property(sourceMapObject, 'sources');
});

it('source map source should has correct name', () => {
assert.equal(sourceMapObject.sources[0], expectedSourceMapSourceName);
});

it('source map from created file should contains property `names`', () => {
assert.property(sourceMapObject, 'names');
});
Expand All @@ -603,6 +616,7 @@ describe('JavaScriptObfuscatorCLI', function (): void {
});

describe('Variant #3: `--sourceMapFileName` option is set', () => {
const expectedSourceMapSourceName: string = path.basename(fixtureFileName);
const sourceMapFileName: string = 'test';
const sourceMapFilePath: string = `${sourceMapFileName}.js.map`;
const outputSourceMapFilePath: string = `${outputDirName}/${sourceMapFilePath}`;
Expand Down Expand Up @@ -649,6 +663,10 @@ describe('JavaScriptObfuscatorCLI', function (): void {
assert.property(sourceMapObject, 'sources');
});

it('source map source should has correct name', () => {
assert.equal(sourceMapObject.sources[0], expectedSourceMapSourceName);
});

it('source map from created file should contains property `names`', () => {
assert.property(sourceMapObject, 'names');
});
Expand Down
74 changes: 74 additions & 0 deletions test/functional-tests/options/OptionsNormalizer.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,80 @@ describe('OptionsNormalizer', () => {
});
});

describe('inputFileNameRule', () => {
describe('Variant #1: extension isn\'t set', () => {
before(() => {
optionsPreset = getNormalizedOptions({
...DEFAULT_PRESET,
inputFileName: 'foo'
});

expectedOptionsPreset = {
...DEFAULT_PRESET,
inputFileName: 'foo.js'
};
});

it('should normalize options preset', () => {
assert.deepEqual(optionsPreset, expectedOptionsPreset);
});
});

describe('Variant #2: extension is set', () => {
before(() => {
optionsPreset = getNormalizedOptions({
...DEFAULT_PRESET,
inputFileName: 'foo.js'
});

expectedOptionsPreset = {
...DEFAULT_PRESET,
inputFileName: 'foo.js'
};
});

it('should normalize options preset', () => {
assert.deepEqual(optionsPreset, expectedOptionsPreset);
});
});

describe('Variant #3: extension in set with `.map` postfix', () => {
before(() => {
optionsPreset = getNormalizedOptions({
...DEFAULT_PRESET,
inputFileName: 'foo.map.js'
});

expectedOptionsPreset = {
...DEFAULT_PRESET,
inputFileName: 'foo.map.js'
};
});

it('should normalize options preset', () => {
assert.deepEqual(optionsPreset, expectedOptionsPreset);
});
});

describe('Variant #4: no file name', () => {
before(() => {
optionsPreset = getNormalizedOptions({
...DEFAULT_PRESET,
inputFileName: ''
});

expectedOptionsPreset = {
...DEFAULT_PRESET,
inputFileName: ''
};
});

it('should normalize options preset', () => {
assert.deepEqual(optionsPreset, expectedOptionsPreset);
});
});
});

describe('selfDefendingRule', () => {
before(() => {
optionsPreset = getNormalizedOptions({
Expand Down
X Tutup