Options.ts 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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 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 {boolean}
  104. */
  105. @IsBoolean()
  106. public readonly log: boolean;
  107. /**
  108. * @type {boolean}
  109. */
  110. @IsBoolean()
  111. public readonly renameGlobals: boolean;
  112. /**
  113. * @type {string[]}
  114. */
  115. @IsArray()
  116. @ArrayUnique()
  117. @IsString({
  118. each: true
  119. })
  120. public readonly reservedNames: string[];
  121. /**
  122. * @type {boolean}
  123. */
  124. @IsBoolean()
  125. public readonly rotateStringArray: boolean;
  126. /**
  127. * @type {number}
  128. */
  129. @IsNumber()
  130. public readonly seed: number;
  131. /**
  132. * @type {boolean}
  133. */
  134. @IsBoolean()
  135. public readonly selfDefending: boolean;
  136. /**
  137. * @type {boolean}
  138. */
  139. @IsBoolean()
  140. public readonly sourceMap: boolean;
  141. /**
  142. * @type {string}
  143. */
  144. @IsString()
  145. @ValidateIf((options: IOptions) => Boolean(options.sourceMapBaseUrl))
  146. @IsUrl({
  147. require_protocol: true,
  148. require_valid_protocol: true
  149. })
  150. public readonly sourceMapBaseUrl: string;
  151. /**
  152. * @type {string}
  153. */
  154. @IsString()
  155. public readonly sourceMapFileName: string;
  156. /**
  157. * @type {SourceMapMode}
  158. */
  159. @IsIn([SourceMapMode.Inline, SourceMapMode.Separate])
  160. public readonly sourceMapMode: SourceMapMode;
  161. /**
  162. * @type {boolean}
  163. */
  164. @IsBoolean()
  165. public readonly stringArray: boolean;
  166. /**
  167. * @type {TStringArrayEncoding}
  168. */
  169. @IsIn([true, false, StringArrayEncoding.Base64, StringArrayEncoding.Rc4])
  170. public readonly stringArrayEncoding: TStringArrayEncoding;
  171. /**
  172. * @type {number}
  173. */
  174. @IsNumber()
  175. @Min(0)
  176. @Max(1)
  177. public readonly stringArrayThreshold: number;
  178. /**
  179. * @type {ObfuscationTarget}
  180. */
  181. @IsIn([ObfuscationTarget.Browser, ObfuscationTarget.Extension, ObfuscationTarget.Node])
  182. public readonly target: ObfuscationTarget;
  183. /**
  184. * @type {boolean}
  185. */
  186. @IsBoolean()
  187. public readonly transformObjectKeys: boolean;
  188. /**
  189. * @type {boolean}
  190. */
  191. @IsBoolean()
  192. public readonly unicodeEscapeSequence: boolean;
  193. /**
  194. * @param {TInputOptions} inputOptions
  195. * @param {IOptionsNormalizer} optionsNormalizer
  196. */
  197. constructor (
  198. @inject(ServiceIdentifiers.TInputOptions) inputOptions: TInputOptions,
  199. @inject(ServiceIdentifiers.IOptionsNormalizer) optionsNormalizer: IOptionsNormalizer
  200. ) {
  201. Object.assign(this, DEFAULT_PRESET, inputOptions);
  202. const errors: ValidationError[] = validateSync(this, Options.validatorOptions);
  203. if (errors.length) {
  204. throw new ReferenceError(`Validation failed. errors:\n${ValidationErrorsFormatter.format(errors)}`);
  205. }
  206. Object.assign(this, optionsNormalizer.normalize(this));
  207. }
  208. }