Merge pull request #735 from javascript-obfuscator/get-optons-for-preset

New API: `getOptionsByPreset`
This commit is contained in:
Timofey Kachalov
2020-09-04 19:23:31 +03:00
committed by GitHub
14 changed files with 117 additions and 27 deletions
+5 -1
View File
@@ -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
---
+4
View File
@@ -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).
+6 -6
View File
File diff suppressed because one or more lines are too long
+1 -1
View File
File diff suppressed because one or more lines are too long
+1 -1
View File
File diff suppressed because one or more lines are too long
Vendored
+9 -2
View File
@@ -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;
@@ -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}
*/
+2 -2
View File
@@ -1,6 +1,6 @@
{
"name": "javascript-obfuscator",
"version": "2.0.0",
"version": "2.1.0",
"description": "JavaScript obfuscator",
"keywords": [
"obfuscator",
@@ -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",
+11 -1
View File
@@ -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 {
@@ -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 };
+2 -2
View File
@@ -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 {
@@ -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[];
+22 -7
View File
@@ -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';
@@ -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],
@@ -172,7 +173,7 @@ export class Options implements IOptions {
public readonly numbersToExpressions!: boolean;
/**
* @type {OptionsPreset}
* @type {TOptionsPreset}
*/
@IsIn([
OptionsPreset.Default,
@@ -180,7 +181,7 @@ export class Options implements IOptions {
OptionsPreset.MediumObfuscation,
OptionsPreset.HighObfuscation
])
public readonly optionsPreset!: TypeFromEnum<typeof OptionsPreset>;
public readonly optionsPreset!: TOptionsPreset;
/**
* @type {boolean}
@@ -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);
@@ -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
View File
@@ -0,0 +1,5 @@
import { TypeFromEnum } from '@gradecam/tsenum';
import { OptionsPreset } from '../../enums/options/presets/OptionsPreset';
export type TOptionsPreset = TypeFromEnum<typeof OptionsPreset>;
+1
View File
@@ -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';
+44
View File
@@ -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');
});
});
});
});
+4 -4
View File
@@ -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"