Options.ts 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. import { injectable } from 'inversify';
  2. import {
  3. ArrayUnique,
  4. IsBoolean,
  5. IsArray,
  6. IsIn,
  7. IsNumber,
  8. IsString,
  9. IsUrl,
  10. Min,
  11. Max,
  12. ValidateIf,
  13. validateSync,
  14. ValidationError,
  15. ValidatorOptions
  16. } from 'class-validator';
  17. import { TInputOptions } from '../types/options/TInputOptions';
  18. import { IOptions } from '../interfaces/options/IOptions';
  19. import { TSourceMapMode } from '../types/TSourceMapMode';
  20. import { TStringArrayEncoding } from '../types/options/TStringArrayEncoding';
  21. import { DEFAULT_PRESET } from './presets/Default';
  22. import { OptionsNormalizer } from './OptionsNormalizer';
  23. import { ValidationErrorsFormatter } from './ValidationErrorsFormatter';
  24. @injectable()
  25. export class Options implements IOptions {
  26. /**
  27. * @type {ValidatorOptions}
  28. */
  29. private static validatorOptions: ValidatorOptions = {
  30. validationError: {
  31. target: false
  32. }
  33. };
  34. /**
  35. * @type {boolean}
  36. */
  37. @IsBoolean()
  38. public readonly compact: boolean;
  39. /**
  40. * @type {boolean}
  41. */
  42. @IsBoolean()
  43. public readonly controlFlowFlattening: boolean;
  44. /**
  45. * @type {boolean}
  46. */
  47. @IsNumber()
  48. @Min(0)
  49. @Max(1)
  50. public readonly controlFlowFlatteningThreshold: number;
  51. /**
  52. * @type {boolean}
  53. */
  54. @IsBoolean()
  55. public readonly deadCodeInjection: boolean;
  56. /**
  57. * @type {number}
  58. */
  59. @IsNumber()
  60. public readonly deadCodeInjectionThreshold: number;
  61. /**
  62. * @type {boolean}
  63. */
  64. @IsBoolean()
  65. public readonly debugProtection: boolean;
  66. /**
  67. * @type {boolean}
  68. */
  69. @IsBoolean()
  70. public readonly debugProtectionInterval: boolean;
  71. /**
  72. * @type {boolean}
  73. */
  74. @IsBoolean()
  75. public readonly disableConsoleOutput: boolean;
  76. /**
  77. * @type {string[]}
  78. */
  79. @IsArray()
  80. @ArrayUnique()
  81. @IsString({
  82. each: true
  83. })
  84. public readonly domainLock: string[];
  85. /**
  86. * @type {boolean}
  87. */
  88. @IsBoolean()
  89. public readonly mangle: boolean;
  90. /**
  91. * @type {string[]}
  92. */
  93. @IsArray()
  94. @ArrayUnique()
  95. @IsString({
  96. each: true
  97. })
  98. public readonly reservedNames: string[];
  99. /**
  100. * @type {boolean}
  101. */
  102. @IsBoolean()
  103. public readonly rotateStringArray: boolean;
  104. /**
  105. * @type {number}
  106. */
  107. @IsNumber()
  108. public readonly seed: number;
  109. /**
  110. * @type {boolean}
  111. */
  112. @IsBoolean()
  113. public readonly selfDefending: boolean;
  114. /**
  115. * @type {boolean}
  116. */
  117. @IsBoolean()
  118. public readonly sourceMap: boolean;
  119. /**
  120. * @type {string}
  121. */
  122. @IsString()
  123. @ValidateIf((options: IOptions) => Boolean(options.sourceMapBaseUrl))
  124. @IsUrl({
  125. require_protocol: true,
  126. require_valid_protocol: true
  127. })
  128. public readonly sourceMapBaseUrl: string;
  129. /**
  130. * @type {string}
  131. */
  132. @IsString()
  133. public readonly sourceMapFileName: string;
  134. /**
  135. * @type {TSourceMapMode}
  136. */
  137. @IsIn(['inline', 'separate'])
  138. public readonly sourceMapMode: TSourceMapMode;
  139. /**
  140. * @type {boolean}
  141. */
  142. @IsBoolean()
  143. public readonly stringArray: boolean;
  144. /**
  145. * @type {TStringArrayEncoding}
  146. */
  147. @IsIn([true, false, 'base64', 'rc4'])
  148. public readonly stringArrayEncoding: TStringArrayEncoding;
  149. /**
  150. * @type {number}
  151. */
  152. @IsNumber()
  153. @Min(0)
  154. @Max(1)
  155. public readonly stringArrayThreshold: number;
  156. /**
  157. * @type {boolean}
  158. */
  159. @IsBoolean()
  160. public readonly unicodeEscapeSequence: boolean;
  161. /**
  162. * @param inputOptions
  163. */
  164. constructor (inputOptions: TInputOptions) {
  165. Object.assign(this, DEFAULT_PRESET, inputOptions);
  166. const errors: ValidationError[] = validateSync(this, Options.validatorOptions);
  167. if (errors.length) {
  168. throw new ReferenceError(`Validation failed. errors:\n${ValidationErrorsFormatter.format(errors)}`);
  169. }
  170. Object.assign(this, OptionsNormalizer.normalizeOptions(this));
  171. }
  172. }