javascript-obfuscator
Version:
JavaScript obfuscator
399 lines (340 loc) • 9.9 kB
text/typescript
import { TypeFromEnum } from '@gradecam/tsenum';
import { inject, injectable } from 'inversify';
import { ServiceIdentifiers } from '../container/ServiceIdentifiers';
import {
ArrayNotEmpty,
ArrayUnique,
IsArray,
IsBoolean,
IsIn,
IsNumber,
IsString,
IsUrl,
Max,
Min,
ValidateIf,
validateSync,
ValidationError,
ValidatorOptions
} from 'class-validator';
import { TInputOptions } from '../types/options/TInputOptions';
import { TOptionsPreset } from '../types/options/TOptionsPreset';
import { TStringArrayEncoding } from '../types/options/TStringArrayEncoding';
import { TStringArrayWrappersType } from '../types/options/TStringArrayWrappersType';
import { IOptions } from '../interfaces/options/IOptions';
import { IOptionsNormalizer } from '../interfaces/options/IOptionsNormalizer';
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';
import { StringArrayEncoding } from '../enums/node-transformers/string-array-transformers/StringArrayEncoding';
import { StringArrayWrappersType } from '../enums/node-transformers/string-array-transformers/StringArrayWrappersType';
import { DEFAULT_PRESET } from './presets/Default';
import { LOW_OBFUSCATION_PRESET } from './presets/LowObfuscation';
import { MEDIUM_OBFUSCATION_PRESET } from './presets/MediumObfuscation';
import { HIGH_OBFUSCATION_PRESET } from './presets/HighObfuscation';
import { ValidationErrorsFormatter } from './ValidationErrorsFormatter';
import { IsAllowedForObfuscationTargets } from './validators/IsAllowedForObfuscationTargets';
export class Options implements IOptions {
/**
* @type {Map<TOptionsPreset, TInputOptions>}
*/
private static readonly optionPresetsMap: Map<TOptionsPreset, TInputOptions> = new Map([
[OptionsPreset.Default, DEFAULT_PRESET],
[OptionsPreset.LowObfuscation, LOW_OBFUSCATION_PRESET],
[OptionsPreset.MediumObfuscation, MEDIUM_OBFUSCATION_PRESET],
[OptionsPreset.HighObfuscation, HIGH_OBFUSCATION_PRESET]
]);
/**
* @type {ValidatorOptions}
*/
private static readonly validatorOptions: ValidatorOptions = {
validationError: {
target: false
}
};
/**
* @type {boolean}
*/
public readonly compact!: boolean;
/**
* @type {boolean}
*/
public readonly controlFlowFlattening!: boolean;
/**
* @type {boolean}
*/
public readonly controlFlowFlatteningThreshold!: number;
/**
* @type {boolean}
*/
public readonly deadCodeInjection!: boolean;
/**
* @type {number}
*/
public readonly deadCodeInjectionThreshold!: number;
/**
* @type {boolean}
*/
public readonly debugProtection!: boolean;
/**
* @type {boolean}
*/
public readonly debugProtectionInterval!: boolean;
/**
* @type {boolean}
*/
public readonly disableConsoleOutput!: boolean;
/**
* @type {string[]}
*/
public readonly domainLock!: string[];
/**
* @type {string[]}
*/
public readonly forceTransformStrings!: string[];
/**
* @type {IdentifierNamesGenerator}
*/
public readonly identifierNamesGenerator!: TypeFromEnum<typeof IdentifierNamesGenerator>;
/**
* @type {string}
*/
public readonly identifiersPrefix!: string;
public readonly identifiersDictionary!: string[];
/**
* @type {string}
*/
public readonly inputFileName!: string;
/**
* @type {boolean}
*/
public readonly log!: boolean;
/**
* @type {boolean}
*/
public readonly numbersToExpressions!: boolean;
/**
* @type {TOptionsPreset}
*/
public readonly optionsPreset!: TOptionsPreset;
/**
* @type {boolean}
*/
public readonly renameGlobals!: boolean;
/**
* @type {boolean}
*/
public readonly renameProperties!: boolean;
/**
* @type {string[]}
*/
public readonly reservedNames!: string[];
/**
* @type {string[]}
*/
public readonly reservedStrings!: string[];
/**
* @type {boolean}
*/
public readonly rotateStringArray!: boolean;
/**
* @type {boolean}
*/
public readonly selfDefending!: boolean;
/**
* @type {boolean}
*/
public readonly shuffleStringArray!: boolean;
/**
* @type {boolean}
*/
public readonly simplify!: boolean;
/**
* @type {boolean}
*/
public readonly sourceMap!: boolean;
/**
* @type {string}
*/
public readonly sourceMapBaseUrl!: string;
/**
* @type {string}
*/
public readonly sourceMapFileName!: string;
/**
* @type {SourceMapMode}
*/
public readonly sourceMapMode!: TypeFromEnum<typeof SourceMapMode>;
/**
* @type {boolean}
*/
public readonly splitStrings!: boolean;
/**
* @type {number}
*/
public readonly splitStringsChunkLength!: number;
/**
* @type {boolean}
*/
public readonly stringArray!: boolean;
/**
* @type {TStringArrayEncoding[]}
*/
public readonly stringArrayEncoding!: TStringArrayEncoding[];
/**
* @type {boolean}
*/
public readonly stringArrayWrappersChainedCalls!: boolean;
/**
* @type {boolean}
*/
public readonly stringArrayWrappersCount!: number;
/**
* @type {TStringArrayWrappersType}
*/
public readonly stringArrayWrappersType!: TStringArrayWrappersType;
/**
* @type {number}
*/
public readonly stringArrayThreshold!: number;
/**
* @type {ObfuscationTarget}
*/
public readonly target!: TypeFromEnum<typeof ObfuscationTarget>;
/**
* @type {boolean}
*/
public readonly transformObjectKeys!: boolean;
/**
* @type {boolean}
*/
public readonly unicodeEscapeSequence!: boolean;
/**
* @type {string | number}
*/
public readonly seed!: string | number;
/**
* @param {TInputOptions} inputOptions
* @param {IOptionsNormalizer} optionsNormalizer
*/
public constructor (
inputOptions: TInputOptions,
optionsNormalizer: IOptionsNormalizer
) {
const optionsPreset: TInputOptions = Options.getOptionsByPreset(
inputOptions.optionsPreset ?? OptionsPreset.Default
);
Object.assign(this, optionsPreset, inputOptions);
const errors: ValidationError[] = validateSync(this, Options.validatorOptions);
if (errors.length) {
throw new ReferenceError(`Validation failed. errors:\n${ValidationErrorsFormatter.format(errors)}`);
}
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;
}
}