index.ts 1.2 KB

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