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
6 changes: 5 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
Change Log

v2.1.0
---
* **New API:** `getOptionsByPreset` allows to get options for the passed options preset name

v2.0.0
---
* **Breaking change:** `stringArrayEncoding` option now accepts an array of encodings. Each string will be randomly encoded with passed encoding.
* **Breaking change:** `stringArrayEncoding` option now accepts an array of encodings. Each string will be randomly encoded with passed encoding

v1.12.1
---
Expand Down
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,10 @@ Accepts `sourceCodesObject` that is a map which keys are identifiers of source c

Returns a map object which keys are identifiers of source codes and values are `ObfuscationResult` objects.

### `getOptionsByPreset(optionsPreset)`

Returns an options object for the passed options preset name.

## CLI usage

See [CLI options](#cli-options).
Expand Down
12 changes: 6 additions & 6 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.

11 changes: 9 additions & 2 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { TInputOptions } from './src/types/options/TInputOptions';
import { TDictionary } from './src/types/TDictionary';
import { TInputOptions } from './src/types/options/TInputOptions';
import { TObfuscationResultsObject } from './src/types/TObfuscationResultsObject';
import { TOptionsPreset } from './src/types/options/TOptionsPreset';

import { IObfuscatedCode } from './src/interfaces/source-code/IObfuscatedCode';
import { TObfuscationResultsObject } from './src/types/TObfuscationResultsObject';

export type ObfuscatorOptions = TInputOptions;

Expand All @@ -25,6 +26,12 @@ export function obfuscateMultiple <TSourceCodesObject extends TDictionary<string
inputOptions?: TInputOptions
): TObfuscationResultsObject<TSourceCodesObject>;

/**
* @param {TOptionsPreset} optionsPreset
* @returns {TInputOptions}
*/
export function getOptionsByPreset (optionsPreset: TOptionsPreset): TInputOptions;

/**
* @type {string}
*/
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "javascript-obfuscator",
"version": "2.0.0",
"version": "2.1.0",
"description": "JavaScript obfuscator",
"keywords": [
"obfuscator",
Expand Down Expand Up @@ -57,7 +57,7 @@
"@types/mkdirp": "1.0.1",
"@types/mocha": "8.0.3",
"@types/multimatch": "4.0.0",
"@types/node": "14.6.3",
"@types/node": "14.6.4",
"@types/rimraf": "3.0.0",
"@types/sinon": "9.0.5",
"@types/string-template": "1.0.2",
Expand Down
12 changes: 11 additions & 1 deletion src/JavaScriptObfuscatorFacade.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,17 @@ import 'reflect-metadata';

import { ServiceIdentifiers } from './container/ServiceIdentifiers';

import { TDictionary } from './types/TDictionary';
import { TInputOptions } from './types/options/TInputOptions';
import { TObfuscationResultsObject } from './types/TObfuscationResultsObject';
import { TDictionary } from './types/TDictionary';
import { TOptionsPreset } from './types/options/TOptionsPreset';

import { IInversifyContainerFacade } from './interfaces/container/IInversifyContainerFacade';
import { IJavaScriptObfuscator } from './interfaces/IJavaScriptObfsucator';
import { IObfuscatedCode } from './interfaces/source-code/IObfuscatedCode';

import { InversifyContainerFacade } from './container/InversifyContainerFacade';
import { Options } from './options/Options';
import { Utils } from './utils/Utils';

class JavaScriptObfuscatorFacade {
Expand Down Expand Up @@ -78,6 +80,14 @@ class JavaScriptObfuscatorFacade {
<TObfuscationResultsObject<TSourceCodesObject>>{}
);
}

/**
* @param {TOptionsPreset} optionsPreset
* @returns {TInputOptions}
*/
public static getOptionsByPreset (optionsPreset: TOptionsPreset): TInputOptions {
return Options.getOptionsByPreset(optionsPreset);
}
}

export { JavaScriptObfuscatorFacade as JavaScriptObfuscator };
4 changes: 2 additions & 2 deletions src/interfaces/options/IOptions.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { TypeFromEnum } from '@gradecam/tsenum';

import { TOptionsPreset } from '../../types/options/TOptionsPreset';
import { TStringArrayEncoding } from '../../types/options/TStringArrayEncoding';

import { IdentifierNamesGenerator } from '../../enums/generators/identifier-names-generators/IdentifierNamesGenerator';
import { ObfuscationTarget } from '../../enums/ObfuscationTarget';
import { OptionsPreset } from '../../enums/options/presets/OptionsPreset';
import { SourceMapMode } from '../../enums/source-map/SourceMapMode';

export interface IOptions {
Expand All @@ -23,7 +23,7 @@ export interface IOptions {
readonly inputFileName: string;
readonly log: boolean;
readonly numbersToExpressions: boolean;
readonly optionsPreset: TypeFromEnum<typeof OptionsPreset>;
readonly optionsPreset: TOptionsPreset;
readonly renameGlobals: boolean;
readonly renameProperties: boolean;
readonly reservedNames: string[];
Expand Down
29 changes: 22 additions & 7 deletions src/options/Options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import {
} from 'class-validator';

import { TInputOptions } from '../types/options/TInputOptions';
import { TOptionsPreset } from '../types/options/TOptionsPreset';
import { TStringArrayEncoding } from '../types/options/TStringArrayEncoding';

import { IOptions } from '../interfaces/options/IOptions';
Expand All @@ -43,9 +44,9 @@ import { IsAllowedForObfuscationTargets } from './validators/IsAllowedForObfusca
@injectable()
export class Options implements IOptions {
/**
* @type {Map<TypeFromEnum<typeof OptionsPreset>, TInputOptions>}
* @type {Map<TOptionsPreset, TInputOptions>}
*/
private static readonly optionPresetsMap: Map<TypeFromEnum<typeof OptionsPreset>, TInputOptions> = new Map([
private static readonly optionPresetsMap: Map<TOptionsPreset, TInputOptions> = new Map([
[OptionsPreset.Default, DEFAULT_PRESET],
[OptionsPreset.LowObfuscation, LOW_OBFUSCATION_PRESET],
[OptionsPreset.MediumObfuscation, MEDIUM_OBFUSCATION_PRESET],
Expand Down Expand Up @@ -172,15 +173,15 @@ export class Options implements IOptions {
public readonly numbersToExpressions!: boolean;

/**
* @type {OptionsPreset}
* @type {TOptionsPreset}
*/
@IsIn([
OptionsPreset.Default,
OptionsPreset.LowObfuscation,
OptionsPreset.MediumObfuscation,
OptionsPreset.HighObfuscation
])
public readonly optionsPreset!: TypeFromEnum<typeof OptionsPreset>;
public readonly optionsPreset!: TOptionsPreset;

/**
* @type {boolean}
Expand Down Expand Up @@ -335,9 +336,9 @@ export class Options implements IOptions {
@inject(ServiceIdentifiers.TInputOptions) inputOptions: TInputOptions,
@inject(ServiceIdentifiers.IOptionsNormalizer) optionsNormalizer: IOptionsNormalizer
) {
const optionsPreset: TInputOptions = Options.optionPresetsMap
.get(inputOptions.optionsPreset ?? OptionsPreset.Default)
?? DEFAULT_PRESET;
const optionsPreset: TInputOptions = Options.getOptionsByPreset(
inputOptions.optionsPreset ?? OptionsPreset.Default
);

Object.assign(this, optionsPreset, inputOptions);

Expand All @@ -349,4 +350,18 @@ export class Options implements IOptions {

Object.assign(this, optionsNormalizer.normalize(this));
}

/**
* @param {TOptionsPreset} optionsPreset
* @returns {TInputOptions}
*/
public static getOptionsByPreset (optionsPreset: TOptionsPreset): TInputOptions {
const options: TInputOptions | null = Options.optionPresetsMap.get(optionsPreset) ?? null;

if (!options) {
throw new Error(`Options for preset name \`${optionsPreset}\` are not found`);
}

return options;
}
}
5 changes: 5 additions & 0 deletions src/types/options/TOptionsPreset.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { TypeFromEnum } from '@gradecam/tsenum';

import { OptionsPreset } from '../../enums/options/presets/OptionsPreset';

export type TOptionsPreset = TypeFromEnum<typeof OptionsPreset>;
1 change: 1 addition & 0 deletions test/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import './unit-tests/node/node-literal-utils/NodeLiteralUtils.spec';
import './unit-tests/node/node-metadata/NodeMetadata.spec';
import './unit-tests/node/node-statement-utils/NodeStatementUtils.spec';
import './unit-tests/node/node-utils/NodeUtils.spec';
import './unit-tests/options/Options.spec';
import './unit-tests/options/ValidationErrorsFormatter.spec';
import './unit-tests/source-code/ObfuscatedCode.spec';
import './unit-tests/storages/ArrayStorage.spec';
Expand Down
44 changes: 44 additions & 0 deletions test/unit-tests/options/Options.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import 'reflect-metadata';

import { assert } from 'chai';

import { TInputOptions } from '../../../src/types/options/TInputOptions';
import { TOptionsPreset } from '../../../src/types/options/TOptionsPreset';

import { OptionsPreset } from '../../../src/enums/options/presets/OptionsPreset';

import { HIGH_OBFUSCATION_PRESET } from '../../../src/options/presets/HighObfuscation';

import { Options } from '../../../src/options/Options';

describe('Options', () => {
describe('getOptionsByPreset', () => {
describe('Variant #1: base behaviour', () => {
const optionsPresetName: TOptionsPreset = OptionsPreset.HighObfuscation;

let options: TInputOptions;

before(() => {
options = Options.getOptionsByPreset(optionsPresetName);
});

it('Should return options for passed options preset name', () => {
assert.deepEqual(options, HIGH_OBFUSCATION_PRESET);
});
});

describe('Variant #2: unknown options preset name', () => {
const optionsPresetName: TOptionsPreset = 'foobar' as TOptionsPreset;

let testFunc: () => TInputOptions;

before(() => {
testFunc = () => Options.getOptionsByPreset(optionsPresetName);
});

it('Should throws an error when unknown option preset is passed', () => {
assert.throws(testFunc, 'Options for preset name `foobar` are not found');
});
});
});
});
8 changes: 4 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -415,10 +415,10 @@
resolved "https://registry.yarnpkg.com/@types/node/-/node-13.9.3.tgz#6356df2647de9eac569f9a52eda3480fa9e70b4d"
integrity sha512-01s+ac4qerwd6RHD+mVbOEsraDHSgUaefQlEdBbUolnQFjKwCr7luvAlEwW1RFojh67u0z4OUTjPn9LEl4zIkA==

"@types/node@14.6.3":
version "14.6.3"
resolved "https://registry.yarnpkg.com/@types/node/-/node-14.6.3.tgz#cc4f979548ca4d8e7b90bc0180052ab99ee64224"
integrity sha512-pC/hkcREG6YfDfui1FBmj8e20jFU5Exjw4NYDm8kEdrW+mOh0T1Zve8DWKnS7ZIZvgncrctcNCXF4Q2I+loyww==
"@types/node@14.6.4":
version "14.6.4"
resolved "https://registry.yarnpkg.com/@types/node/-/node-14.6.4.tgz#a145cc0bb14ef9c4777361b7bbafa5cf8e3acb5a"
integrity sha512-Wk7nG1JSaMfMpoMJDKUsWYugliB2Vy55pdjLpmLixeyMi7HizW2I/9QoxsPCkXl3dO+ZOVqPumKaDUv5zJu2uQ==

"@types/normalize-package-data@^2.4.0":
version "2.4.0"
Expand Down