JavaScriptObfuscatorInternal.ts 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. import * as esprima from 'esprima';
  2. import * as escodegen from 'escodegen';
  3. import * as ESTree from 'estree';
  4. import { Chance } from 'chance';
  5. import { IGeneratorOutput } from './interfaces/IGeneratorOutput';
  6. import { IObfuscationResult } from './interfaces/IObfuscationResult';
  7. import { IOptions } from './interfaces/IOptions';
  8. import { ObfuscationResult } from './ObfuscationResult';
  9. import { Obfuscator } from './Obfuscator';
  10. import { SourceMapCorrector } from './SourceMapCorrector';
  11. import { Utils } from './Utils';
  12. export class JavaScriptObfuscatorInternal {
  13. /**
  14. * @type {GenerateOptions}
  15. */
  16. private static readonly escodegenParams: escodegen.GenerateOptions = {
  17. verbatim: 'x-verbatim-property',
  18. sourceMapWithCode: true
  19. };
  20. /**
  21. * @type {esprima.Options}
  22. */
  23. private static readonly esprimaParams: esprima.Options = {
  24. loc: true
  25. };
  26. /**
  27. * @type {IOptions}
  28. */
  29. private readonly options: IOptions;
  30. /**
  31. * @param options
  32. */
  33. constructor (options: IOptions) {
  34. this.options = options;
  35. }
  36. /**
  37. * @param sourceCode
  38. * @param astTree
  39. * @param options
  40. */
  41. private static generateCode (sourceCode: string, astTree: ESTree.Program, options: IOptions): IGeneratorOutput {
  42. const escodegenParams: escodegen.GenerateOptions = Object.assign(
  43. {},
  44. JavaScriptObfuscatorInternal.escodegenParams
  45. );
  46. if (options.sourceMap) {
  47. escodegenParams.sourceMap = 'sourceMap';
  48. escodegenParams.sourceContent = sourceCode;
  49. }
  50. escodegenParams.format = {
  51. compact: options.compact
  52. };
  53. const generatorOutput: IGeneratorOutput = escodegen.generate(astTree, escodegenParams);
  54. generatorOutput.map = generatorOutput.map ? generatorOutput.map.toString() : '';
  55. return generatorOutput;
  56. }
  57. /**
  58. * @param generatorOutput
  59. * @returns {IObfuscationResult}
  60. */
  61. public getObfuscationResult (generatorOutput: IGeneratorOutput): IObfuscationResult {
  62. return new SourceMapCorrector(
  63. new ObfuscationResult(
  64. generatorOutput.code,
  65. generatorOutput.map
  66. ),
  67. this.options.sourceMapBaseUrl + this.options.sourceMapFileName,
  68. this.options.sourceMapMode
  69. ).correct();
  70. }
  71. /**
  72. * @param sourceCode
  73. * @returns {IObfuscationResult}
  74. */
  75. public obfuscate (sourceCode: string): IObfuscationResult {
  76. if (this.options.seed !== 0) {
  77. Utils.setRandomGenerator(new Chance(this.options.seed));
  78. }
  79. const astTree: ESTree.Program = esprima.parse(sourceCode, JavaScriptObfuscatorInternal.esprimaParams);
  80. const obfuscatedAstTree: ESTree.Program = new Obfuscator(this.options).obfuscateAstTree(astTree);
  81. const generatorOutput: IGeneratorOutput = JavaScriptObfuscatorInternal.generateCode(
  82. sourceCode,
  83. obfuscatedAstTree,
  84. this.options
  85. );
  86. return this.getObfuscationResult(generatorOutput);
  87. }
  88. }