Options.ts 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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 { IInputOptions } from '../interfaces/IInputOptions';
  18. import { IOptions } from '../interfaces/IOptions';
  19. import { TSourceMapMode } from '../types/TSourceMapMode';
  20. import { TStringArrayEncoding } from '../types/TStringArrayEncoding';
  21. import { DEFAULT_PRESET } from '../preset-options/DefaultPreset';
  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. @IsBoolean()
  48. public readonly debugProtection: boolean;
  49. /**
  50. * @type {boolean}
  51. */
  52. @IsBoolean()
  53. public readonly debugProtectionInterval: boolean;
  54. /**
  55. * @type {boolean}
  56. */
  57. @IsBoolean()
  58. public readonly disableConsoleOutput: boolean;
  59. /**
  60. * @type {string[]}
  61. */
  62. @IsArray()
  63. @ArrayUnique()
  64. @IsString({
  65. each: true
  66. })
  67. public readonly domainLock: string[];
  68. /**
  69. * @type {string[]}
  70. */
  71. @IsArray()
  72. @ArrayUnique()
  73. @IsString({
  74. each: true
  75. })
  76. public readonly reservedNames: string[];
  77. /**
  78. * @type {boolean}
  79. */
  80. @IsBoolean()
  81. public readonly rotateStringArray: boolean;
  82. /**
  83. * @type {number}
  84. */
  85. @IsNumber()
  86. public readonly seed: number;
  87. /**
  88. * @type {boolean}
  89. */
  90. @IsBoolean()
  91. public readonly selfDefending: boolean;
  92. /**
  93. * @type {boolean}
  94. */
  95. @IsBoolean()
  96. public readonly sourceMap: boolean;
  97. /**
  98. * @type {string}
  99. */
  100. @IsString()
  101. @ValidateIf((options: IOptions) => Boolean(options.sourceMapBaseUrl))
  102. @IsUrl({
  103. require_protocol: true,
  104. require_valid_protocol: true
  105. })
  106. public readonly sourceMapBaseUrl: string;
  107. /**
  108. * @type {string}
  109. */
  110. @IsString()
  111. public readonly sourceMapFileName: string;
  112. /**
  113. * @type {TSourceMapMode}
  114. */
  115. @IsIn(['inline', 'separate'])
  116. public readonly sourceMapMode: TSourceMapMode;
  117. /**
  118. * @type {boolean}
  119. */
  120. @IsBoolean()
  121. public readonly stringArray: boolean;
  122. /**
  123. * @type {TStringArrayEncoding}
  124. */
  125. @IsIn([true, false, 'base64', 'rc4'])
  126. public readonly stringArrayEncoding: TStringArrayEncoding;
  127. /**
  128. * @type {number}
  129. */
  130. @IsNumber()
  131. @Min(0)
  132. @Max(1)
  133. public readonly stringArrayThreshold: number;
  134. /**
  135. * @type {boolean}
  136. */
  137. @IsBoolean()
  138. public readonly unicodeEscapeSequence: boolean;
  139. /**
  140. * @param inputOptions
  141. */
  142. constructor (inputOptions: IInputOptions) {
  143. Object.assign(this, DEFAULT_PRESET, inputOptions);
  144. const errors: ValidationError[] = validateSync(this, Options.validatorOptions);
  145. if (errors.length) {
  146. throw new ReferenceError(`Validation failed. errors:\n${ValidationErrorsFormatter.format(errors)}`);
  147. }
  148. Object.assign(this, OptionsNormalizer.normalizeOptions(this));
  149. }
  150. }