JavaScriptObfuscatorInternal.ts 2.8 KB

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