Options.ts 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. import { inject, injectable } from 'inversify';
  2. import { ServiceIdentifiers } from '../container/ServiceIdentifiers';
  3. import {
  4. ArrayUnique,
  5. IsArray,
  6. IsBoolean,
  7. IsIn,
  8. IsNumber,
  9. IsString,
  10. IsUrl,
  11. Max,
  12. Min,
  13. ValidateIf,
  14. validateSync,
  15. ValidationError,
  16. ValidatorOptions
  17. } from 'class-validator';
  18. import { TInputOptions } from '../types/options/TInputOptions';
  19. import { TStringArrayEncoding } from '../types/options/TStringArrayEncoding';
  20. import { IOptions } from '../interfaces/options/IOptions';
  21. import { IOptionsNormalizer } from '../interfaces/options/IOptionsNormalizer';
  22. import { IdentifierNamesGenerator } from '../enums/generators/identifier-names-generators/IdentifierNamesGenerator';
  23. import { ObfuscationTarget } from '../enums/ObfuscationTarget';
  24. import { SourceMapMode } from '../enums/source-map/SourceMapMode';
  25. import { StringArrayEncoding } from '../enums/StringArrayEncoding';
  26. import { DEFAULT_PRESET } from './presets/Default';
  27. import { ValidationErrorsFormatter } from './ValidationErrorsFormatter';
  28. @injectable()
  29. export class Options implements IOptions {
  30. /**
  31. * @type {ValidatorOptions}
  32. */
  33. private static readonly validatorOptions: ValidatorOptions = {
  34. validationError: {
  35. target: false
  36. }
  37. };
  38. /**
  39. * @type {boolean}
  40. */
  41. @IsBoolean()
  42. public readonly compact!: boolean;
  43. /**
  44. * @type {boolean}
  45. */
  46. @IsBoolean()
  47. public readonly controlFlowFlattening!: boolean;
  48. /**
  49. * @type {boolean}
  50. */
  51. @IsNumber()
  52. @Min(0)
  53. @Max(1)
  54. public readonly controlFlowFlatteningThreshold!: number;
  55. /**
  56. * @type {boolean}
  57. */
  58. @IsBoolean()
  59. public readonly deadCodeInjection!: boolean;
  60. /**
  61. * @type {number}
  62. */
  63. @IsNumber()
  64. public readonly deadCodeInjectionThreshold!: number;
  65. /**
  66. * @type {boolean}
  67. */
  68. @IsBoolean()
  69. public readonly debugProtection!: boolean;
  70. /**
  71. * @type {boolean}
  72. */
  73. @IsBoolean()
  74. public readonly debugProtectionInterval!: boolean;
  75. /**
  76. * @type {boolean}
  77. */
  78. @IsBoolean()
  79. public readonly disableConsoleOutput!: boolean;
  80. /**
  81. * @type {string[]}
  82. */
  83. @IsArray()
  84. @ArrayUnique()
  85. @IsString({
  86. each: true
  87. })
  88. public readonly domainLock!: string[];
  89. /**
  90. * @type {IdentifierNamesGenerator}
  91. */
  92. @IsIn([
  93. IdentifierNamesGenerator.HexadecimalIdentifierNamesGenerator,
  94. IdentifierNamesGenerator.MangledIdentifierNamesGenerator
  95. ])
  96. public readonly identifierNamesGenerator!: IdentifierNamesGenerator;
  97. /**
  98. * @type {string}
  99. */
  100. @IsString()
  101. public readonly identifiersPrefix!: string;
  102. /**
  103. * @type {string}
  104. */
  105. @IsString()
  106. public readonly inputFileName!: string;
  107. /**
  108. * @type {boolean}
  109. */
  110. @IsBoolean()
  111. public readonly log!: boolean;
  112. /**
  113. * @type {boolean}
  114. */
  115. @IsBoolean()
  116. public readonly renameGlobals!: boolean;
  117. /**
  118. * @type {string[]}
  119. */
  120. @IsArray()
  121. @ArrayUnique()
  122. @IsString({
  123. each: true
  124. })
  125. public readonly reservedNames!: string[];
  126. /**
  127. * @type {boolean}
  128. */
  129. @IsBoolean()
  130. public readonly rotateStringArray!: boolean;
  131. /**
  132. * @type {number}
  133. */
  134. @IsNumber()
  135. public readonly seed!: number;
  136. /**
  137. * @type {boolean}
  138. */
  139. @IsBoolean()
  140. public readonly selfDefending!: boolean;
  141. /**
  142. * @type {boolean}
  143. */
  144. @IsBoolean()
  145. public readonly sourceMap!: boolean;
  146. /**
  147. * @type {string}
  148. */
  149. @IsString()
  150. @ValidateIf((options: IOptions) => Boolean(options.sourceMapBaseUrl))
  151. @IsUrl({
  152. require_protocol: true,
  153. require_tld: false,
  154. require_valid_protocol: true
  155. })
  156. public readonly sourceMapBaseUrl!: string;
  157. /**
  158. * @type {string}
  159. */
  160. @IsString()
  161. public readonly sourceMapFileName!: string;
  162. /**
  163. * @type {SourceMapMode}
  164. */
  165. @IsIn([SourceMapMode.Inline, SourceMapMode.Separate])
  166. public readonly sourceMapMode!: SourceMapMode;
  167. /**
  168. * @type {boolean}
  169. */
  170. @IsBoolean()
  171. public readonly stringArray!: boolean;
  172. /**
  173. * @type {TStringArrayEncoding}
  174. */
  175. @IsIn([true, false, StringArrayEncoding.Base64, StringArrayEncoding.Rc4])
  176. public readonly stringArrayEncoding!: TStringArrayEncoding;
  177. /**
  178. * @type {number}
  179. */
  180. @IsNumber()
  181. @Min(0)
  182. @Max(1)
  183. public readonly stringArrayThreshold!: number;
  184. /**
  185. * @type {ObfuscationTarget}
  186. */
  187. @IsIn([ObfuscationTarget.Browser, ObfuscationTarget.BrowserNoEval, ObfuscationTarget.Node])
  188. public readonly target!: ObfuscationTarget;
  189. /**
  190. * @type {boolean}
  191. */
  192. @IsBoolean()
  193. public readonly transformObjectKeys!: boolean;
  194. /**
  195. * @type {boolean}
  196. */
  197. @IsBoolean()
  198. public readonly unicodeEscapeSequence!: boolean;
  199. /**
  200. * @param {TInputOptions} inputOptions
  201. * @param {IOptionsNormalizer} optionsNormalizer
  202. */
  203. constructor (
  204. @inject(ServiceIdentifiers.TInputOptions) inputOptions: TInputOptions,
  205. @inject(ServiceIdentifiers.IOptionsNormalizer) optionsNormalizer: IOptionsNormalizer
  206. ) {
  207. Object.assign(this, DEFAULT_PRESET, inputOptions);
  208. const errors: ValidationError[] = validateSync(this, Options.validatorOptions);
  209. if (errors.length) {
  210. throw new ReferenceError(`Validation failed. errors:\n${ValidationErrorsFormatter.format(errors)}`);
  211. }
  212. Object.assign(this, optionsNormalizer.normalize(this));
  213. }
  214. }