Options.ts 3.5 KB

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