index.ts 965 B

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. "use strict";
  2. declare let module: any;
  3. import { Obfuscator } from './src/Obfuscator';
  4. let escodegen = require('escodegen'),
  5. esprima = require('esprima');
  6. export class JavaScriptObfuscator {
  7. /**
  8. * @type any
  9. */
  10. private static escodegenParams: any = {
  11. format: {
  12. compact: true
  13. },
  14. verbatim: 'x-verbatim-property'
  15. };
  16. /**
  17. * @param sourceCode
  18. * @param options
  19. */
  20. public static obfuscate (sourceCode: string, options: any): string {
  21. let astTree: any = esprima.parse(sourceCode),
  22. obfuscator: Obfuscator = new Obfuscator(options);
  23. obfuscator.obfuscateNode(astTree);
  24. return JavaScriptObfuscator.generateCode(astTree);
  25. }
  26. /**
  27. * @param astTree
  28. */
  29. private static generateCode (astTree: any): string {
  30. return escodegen.generate(astTree, JavaScriptObfuscator.escodegenParams);
  31. }
  32. }
  33. module.exports = JavaScriptObfuscator;