Options.ts 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  1. import { inject, injectable } from 'inversify';
  2. import { ServiceIdentifiers } from '../container/ServiceIdentifiers';
  3. import {
  4. ArrayNotEmpty,
  5. ArrayUnique,
  6. IsArray,
  7. IsBoolean,
  8. IsIn,
  9. IsNumber,
  10. IsString,
  11. IsUrl,
  12. Max,
  13. Min,
  14. ValidateIf,
  15. validateSync,
  16. ValidationError,
  17. ValidatorOptions
  18. } from 'class-validator';
  19. import { TInputOptions } from '../types/options/TInputOptions';
  20. import { TOptionsPreset } from '../types/options/TOptionsPreset';
  21. import { TStringArrayIndexesType } from '../types/options/TStringArrayIndexesType';
  22. import { TStringArrayEncoding } from '../types/options/TStringArrayEncoding';
  23. import { TStringArrayWrappersType } from '../types/options/TStringArrayWrappersType';
  24. import { TTypeFromEnum } from '../types/utils/TTypeFromEnum';
  25. import { IOptions } from '../interfaces/options/IOptions';
  26. import { IOptionsNormalizer } from '../interfaces/options/IOptionsNormalizer';
  27. import { IdentifierNamesGenerator } from '../enums/generators/identifier-names-generators/IdentifierNamesGenerator';
  28. import { ObfuscationTarget } from '../enums/ObfuscationTarget';
  29. import { OptionsPreset } from '../enums/options/presets/OptionsPreset';
  30. import { SourceMapMode } from '../enums/source-map/SourceMapMode';
  31. import { StringArrayIndexesType } from '../enums/node-transformers/string-array-transformers/StringArrayIndexesType';
  32. import { StringArrayEncoding } from '../enums/node-transformers/string-array-transformers/StringArrayEncoding';
  33. import { StringArrayWrappersType } from '../enums/node-transformers/string-array-transformers/StringArrayWrappersType';
  34. import { DEFAULT_PRESET } from './presets/Default';
  35. import { LOW_OBFUSCATION_PRESET } from './presets/LowObfuscation';
  36. import { MEDIUM_OBFUSCATION_PRESET } from './presets/MediumObfuscation';
  37. import { HIGH_OBFUSCATION_PRESET } from './presets/HighObfuscation';
  38. import { ValidationErrorsFormatter } from './ValidationErrorsFormatter';
  39. import { IsAllowedForObfuscationTargets } from './validators/IsAllowedForObfuscationTargets';
  40. @injectable()
  41. export class Options implements IOptions {
  42. /**
  43. * @type {Map<TOptionsPreset, TInputOptions>}
  44. */
  45. private static readonly optionPresetsMap: Map<TOptionsPreset, TInputOptions> = new Map([
  46. [OptionsPreset.Default, DEFAULT_PRESET],
  47. [OptionsPreset.LowObfuscation, LOW_OBFUSCATION_PRESET],
  48. [OptionsPreset.MediumObfuscation, MEDIUM_OBFUSCATION_PRESET],
  49. [OptionsPreset.HighObfuscation, HIGH_OBFUSCATION_PRESET]
  50. ]);
  51. /**
  52. * @type {ValidatorOptions}
  53. */
  54. private static readonly validatorOptions: ValidatorOptions = {
  55. validationError: {
  56. target: false
  57. }
  58. };
  59. /**
  60. * @type {boolean}
  61. */
  62. @IsBoolean()
  63. public readonly compact!: boolean;
  64. /**
  65. * @type {boolean}
  66. */
  67. @IsBoolean()
  68. public readonly controlFlowFlattening!: boolean;
  69. /**
  70. * @type {boolean}
  71. */
  72. @IsNumber()
  73. @Min(0)
  74. @Max(1)
  75. public readonly controlFlowFlatteningThreshold!: number;
  76. /**
  77. * @type {boolean}
  78. */
  79. @IsBoolean()
  80. public readonly deadCodeInjection!: boolean;
  81. /**
  82. * @type {number}
  83. */
  84. @IsNumber()
  85. public readonly deadCodeInjectionThreshold!: number;
  86. /**
  87. * @type {boolean}
  88. */
  89. @IsBoolean()
  90. public readonly debugProtection!: boolean;
  91. /**
  92. * @type {boolean}
  93. */
  94. @IsBoolean()
  95. public readonly debugProtectionInterval!: boolean;
  96. /**
  97. * @type {boolean}
  98. */
  99. @IsBoolean()
  100. public readonly disableConsoleOutput!: boolean;
  101. /**
  102. * @type {string[]}
  103. */
  104. @IsArray()
  105. @ArrayUnique()
  106. @IsString({
  107. each: true
  108. })
  109. @IsAllowedForObfuscationTargets([
  110. ObfuscationTarget.Browser,
  111. ObfuscationTarget.BrowserNoEval,
  112. ])
  113. public readonly domainLock!: string[];
  114. /**
  115. * @type {string[]}
  116. */
  117. @IsArray()
  118. @ArrayUnique()
  119. @IsString({
  120. each: true
  121. })
  122. public readonly forceTransformStrings!: string[];
  123. /**
  124. * @type {IdentifierNamesGenerator}
  125. */
  126. @IsIn([
  127. IdentifierNamesGenerator.DictionaryIdentifierNamesGenerator,
  128. IdentifierNamesGenerator.HexadecimalIdentifierNamesGenerator,
  129. IdentifierNamesGenerator.MangledIdentifierNamesGenerator,
  130. IdentifierNamesGenerator.MangledShuffledIdentifierNamesGenerator
  131. ])
  132. public readonly identifierNamesGenerator!: TTypeFromEnum<typeof IdentifierNamesGenerator>;
  133. /**
  134. * @type {string}
  135. */
  136. @IsString()
  137. public readonly identifiersPrefix!: string;
  138. @IsArray()
  139. @ArrayUnique()
  140. @IsString({
  141. each: true
  142. })
  143. @ValidateIf((options: IOptions) =>
  144. options.identifierNamesGenerator === IdentifierNamesGenerator.DictionaryIdentifierNamesGenerator
  145. )
  146. @ArrayNotEmpty()
  147. public readonly identifiersDictionary!: string[];
  148. /**
  149. * @type {boolean}
  150. */
  151. @IsBoolean()
  152. public readonly ignoreRequireImports!: boolean;
  153. /**
  154. * @type {string}
  155. */
  156. @IsString()
  157. public readonly inputFileName!: string;
  158. /**
  159. * @type {boolean}
  160. */
  161. @IsBoolean()
  162. public readonly log!: boolean;
  163. /**
  164. * @type {boolean}
  165. */
  166. @IsBoolean()
  167. public readonly numbersToExpressions!: boolean;
  168. /**
  169. * @type {TOptionsPreset}
  170. */
  171. @IsIn([
  172. OptionsPreset.Default,
  173. OptionsPreset.LowObfuscation,
  174. OptionsPreset.MediumObfuscation,
  175. OptionsPreset.HighObfuscation
  176. ])
  177. public readonly optionsPreset!: TOptionsPreset;
  178. /**
  179. * @type {boolean}
  180. */
  181. @IsBoolean()
  182. public readonly renameGlobals!: boolean;
  183. /**
  184. * @type {boolean}
  185. */
  186. @IsBoolean()
  187. public readonly renameProperties!: boolean;
  188. /**
  189. * @type {string[]}
  190. */
  191. @IsArray()
  192. @ArrayUnique()
  193. @IsString({
  194. each: true
  195. })
  196. public readonly reservedNames!: string[];
  197. /**
  198. * @type {string[]}
  199. */
  200. @IsArray()
  201. @ArrayUnique()
  202. @IsString({
  203. each: true
  204. })
  205. public readonly reservedStrings!: string[];
  206. /**
  207. * @type {boolean}
  208. */
  209. @IsBoolean()
  210. public readonly rotateStringArray!: boolean;
  211. /**
  212. * @type {boolean}
  213. */
  214. @IsBoolean()
  215. public readonly selfDefending!: boolean;
  216. /**
  217. * @type {boolean}
  218. */
  219. @IsBoolean()
  220. public readonly shuffleStringArray!: boolean;
  221. /**
  222. * @type {boolean}
  223. */
  224. @IsBoolean()
  225. public readonly simplify!: boolean;
  226. /**
  227. * @type {boolean}
  228. */
  229. @IsBoolean()
  230. public readonly sourceMap!: boolean;
  231. /**
  232. * @type {string}
  233. */
  234. @IsString()
  235. @ValidateIf((options: IOptions) => Boolean(options.sourceMapBaseUrl))
  236. @IsUrl({
  237. require_protocol: true,
  238. require_tld: false,
  239. require_valid_protocol: true
  240. })
  241. public readonly sourceMapBaseUrl!: string;
  242. /**
  243. * @type {string}
  244. */
  245. @IsString()
  246. public readonly sourceMapFileName!: string;
  247. /**
  248. * @type {SourceMapMode}
  249. */
  250. @IsIn([SourceMapMode.Inline, SourceMapMode.Separate])
  251. public readonly sourceMapMode!: TTypeFromEnum<typeof SourceMapMode>;
  252. /**
  253. * @type {boolean}
  254. */
  255. @IsBoolean()
  256. public readonly splitStrings!: boolean;
  257. /**
  258. * @type {number}
  259. */
  260. @IsNumber()
  261. @ValidateIf((options: IOptions) => Boolean(options.splitStrings))
  262. @Min(1)
  263. public readonly splitStringsChunkLength!: number;
  264. /**
  265. * @type {boolean}
  266. */
  267. @IsBoolean()
  268. public readonly stringArray!: boolean;
  269. /**
  270. * @type {TStringArrayEncoding[]}
  271. */
  272. @IsArray()
  273. @ArrayUnique()
  274. @IsIn([StringArrayEncoding.None, StringArrayEncoding.Base64, StringArrayEncoding.Rc4], { each: true })
  275. public readonly stringArrayEncoding!: TStringArrayEncoding[];
  276. /**
  277. * @type {TStringArrayIndexesType[]}
  278. */
  279. @IsArray()
  280. @ArrayNotEmpty()
  281. @ArrayUnique()
  282. @IsIn([StringArrayIndexesType.HexadecimalNumber, StringArrayIndexesType.HexadecimalNumericString], { each: true })
  283. public readonly stringArrayIndexesType!: TStringArrayIndexesType[];
  284. /**
  285. * @type {boolean}
  286. */
  287. @IsBoolean()
  288. public readonly stringArrayIndexShift!: boolean;
  289. /**
  290. * @type {boolean}
  291. */
  292. @IsBoolean()
  293. public readonly stringArrayWrappersChainedCalls!: boolean;
  294. /**
  295. * @type {boolean}
  296. */
  297. @IsNumber()
  298. @Min(0)
  299. public readonly stringArrayWrappersCount!: number;
  300. /**
  301. * @type {boolean}
  302. */
  303. @IsNumber()
  304. @Min(2)
  305. public readonly stringArrayWrappersParametersMaxCount!: number;
  306. /**
  307. * @type {TStringArrayWrappersType}
  308. */
  309. @IsIn([StringArrayWrappersType.Variable, StringArrayWrappersType.Function])
  310. public readonly stringArrayWrappersType!: TStringArrayWrappersType;
  311. /**
  312. * @type {number}
  313. */
  314. @IsNumber()
  315. @Min(0)
  316. @Max(1)
  317. public readonly stringArrayThreshold!: number;
  318. /**
  319. * @type {ObfuscationTarget}
  320. */
  321. @IsIn([ObfuscationTarget.Browser, ObfuscationTarget.BrowserNoEval, ObfuscationTarget.Node])
  322. public readonly target!: TTypeFromEnum<typeof ObfuscationTarget>;
  323. /**
  324. * @type {boolean}
  325. */
  326. @IsBoolean()
  327. public readonly transformObjectKeys!: boolean;
  328. /**
  329. * @type {boolean}
  330. */
  331. @IsBoolean()
  332. public readonly unicodeEscapeSequence!: boolean;
  333. /**
  334. * @type {string | number}
  335. */
  336. public readonly seed!: string | number;
  337. /**
  338. * @param {TInputOptions} inputOptions
  339. * @param {IOptionsNormalizer} optionsNormalizer
  340. */
  341. public constructor (
  342. @inject(ServiceIdentifiers.TInputOptions) inputOptions: TInputOptions,
  343. @inject(ServiceIdentifiers.IOptionsNormalizer) optionsNormalizer: IOptionsNormalizer
  344. ) {
  345. const optionsPreset: TInputOptions = Options.getOptionsByPreset(
  346. inputOptions.optionsPreset ?? OptionsPreset.Default
  347. );
  348. Object.assign(this, optionsPreset, inputOptions);
  349. const errors: ValidationError[] = validateSync(this, Options.validatorOptions);
  350. if (errors.length) {
  351. throw new ReferenceError(`Validation failed. errors:\n${ValidationErrorsFormatter.format(errors)}`);
  352. }
  353. Object.assign(this, optionsNormalizer.normalize(this));
  354. }
  355. /**
  356. * @param {TOptionsPreset} optionsPreset
  357. * @returns {TInputOptions}
  358. */
  359. public static getOptionsByPreset (optionsPreset: TOptionsPreset): TInputOptions {
  360. const options: TInputOptions | null = Options.optionPresetsMap.get(optionsPreset) ?? null;
  361. if (!options) {
  362. throw new Error(`Options for preset name \`${optionsPreset}\` are not found`);
  363. }
  364. return options;
  365. }
  366. }