JavaScriptObfuscatorInternal.ts 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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. * @type {string}
  34. */
  35. private sourceMapUrl: string = '';
  36. /**
  37. * @param sourceCode
  38. * @param obfuscatorOptions
  39. */
  40. constructor (sourceCode: string, obfuscatorOptions?: IObfuscatorOptions) {
  41. this.sourceCode = sourceCode;
  42. if (obfuscatorOptions) {
  43. this.options = new Options(obfuscatorOptions);
  44. }
  45. }
  46. /**
  47. * @param sourceCode
  48. * @param astTree
  49. * @param options
  50. */
  51. private static generateCode (sourceCode: string, astTree: ESTree.Node, options: IOptions): IGeneratorOutput {
  52. let escodegenParams: escodegen.GenerateOptions = Object.assign(
  53. {},
  54. JavaScriptObfuscatorInternal.escodegenParams
  55. ),
  56. generatorOutput: IGeneratorOutput;
  57. if (options.sourceMap) {
  58. escodegenParams.sourceMap = 'sourceMap';
  59. escodegenParams.sourceContent = sourceCode;
  60. }
  61. escodegenParams.format = {
  62. compact: options.compact
  63. };
  64. generatorOutput = escodegen.generate(astTree, escodegenParams);
  65. generatorOutput.map = generatorOutput.map ? generatorOutput.map.toString() : '';
  66. return generatorOutput;
  67. }
  68. /**
  69. * @returns {IObfuscationResult}
  70. */
  71. public getObfuscationResult (): IObfuscationResult {
  72. return new SourceMapCorrector(
  73. new ObfuscationResult(
  74. this.generatorOutput.code,
  75. this.generatorOutput.map
  76. ),
  77. this.sourceMapUrl,
  78. this.options.sourceMapMode
  79. ).correct();
  80. }
  81. public obfuscate (): void {
  82. let astTree: ESTree.Node = esprima.parse(this.sourceCode, {
  83. loc: true
  84. });
  85. astTree = new Obfuscator(this.options).obfuscateNode(astTree);
  86. this.generatorOutput = JavaScriptObfuscatorInternal.generateCode(this.sourceCode, astTree, this.options);
  87. }
  88. /**
  89. * @param url
  90. */
  91. public setSourceMapUrl (url: string): void {
  92. this.sourceMapUrl = url;
  93. }
  94. }