javascript-obfuscator
Version:
JavaScript obfuscator
309 lines (262 loc) • 7.06 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 { TStringArrayEncoding } from '../types/options/TStringArrayEncoding';
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 { SourceMapMode } from '../enums/source-map/SourceMapMode';
import { StringArrayEncoding } from '../enums/StringArrayEncoding';
import { DEFAULT_PRESET } from './presets/Default';
import { ValidationErrorsFormatter } from './ValidationErrorsFormatter';
import { IsAllowedForObfuscationTargets } from './validators/IsAllowedForObfuscationTargets';
export class Options implements IOptions {
/**
* @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 {IdentifierNamesGenerator}
*/
public readonly identifierNamesGenerator!: TypeFromEnum<typeof IdentifierNamesGenerator>;
/**
* @type {string}
*/
public readonly identifiersPrefix!: string;
public readonly identifiersDictionary!: string[];
/**
* @type {string}
*/
public readonly inputFileName!: string;
/**
* @type {string}
*/
public readonly inputFilePath!: string;
/**
* @type {boolean}
*/
public readonly log!: boolean;
/**
* @type {boolean}
*/
public readonly renameGlobals!: boolean;
/**
* @type {string[]}
*/
public readonly reservedNames!: string[];
/**
* @type {string[]}
*/
public readonly reservedStrings!: string[];
/**
* @type {boolean}
*/
public readonly rotateStringArray!: boolean;
/**
* @type {string | number}
*/
public readonly seed!: string | number;
/**
* @type {boolean}
*/
public readonly selfDefending!: boolean;
/**
* @type {boolean}
*/
public readonly shuffleStringArray!: 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 {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;
/**
* @param {TInputOptions} inputOptions
* @param {IOptionsNormalizer} optionsNormalizer
*/
public constructor (
inputOptions: TInputOptions,
optionsNormalizer: IOptionsNormalizer
) {
Object.assign(this, DEFAULT_PRESET, 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));
}
}