index.ts 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. "use strict";
  2. import * as esprima from 'esprima';
  3. import * as escodegen from 'escodegen';
  4. import { IProgramNode } from './src/interfaces/nodes/IProgramNode';
  5. import { Obfuscator } from './src/Obfuscator';
  6. class JavaScriptObfuscator {
  7. /**
  8. * @type any
  9. */
  10. private static defaultOptions: any = {
  11. compact: true,
  12. debugProtection: false,
  13. debugProtectionInterval: false,
  14. disableConsoleOutput: true,
  15. rotateUnicodeArray: true,
  16. wrapUnicodeArrayCalls: true
  17. };
  18. /**
  19. * @type any
  20. */
  21. private static escodegenParams: any = {
  22. verbatim: 'x-verbatim-property'
  23. };
  24. /**
  25. * @param sourceCode
  26. * @param customOptions
  27. */
  28. public static obfuscate (sourceCode: string, customOptions?: any): string {
  29. let astTree: IProgramNode = esprima.parse(sourceCode),
  30. options: any = Object.assign(JavaScriptObfuscator.defaultOptions, customOptions),
  31. obfuscator: Obfuscator = new Obfuscator(options);
  32. obfuscator.obfuscateNode(astTree);
  33. return JavaScriptObfuscator.generateCode(astTree, options);
  34. }
  35. /**
  36. * @param astTree
  37. * @param options
  38. */
  39. private static generateCode (astTree: IProgramNode, options: any): string {
  40. let escodegenParams: any = Object.assign({}, JavaScriptObfuscator.escodegenParams);
  41. if (options.hasOwnProperty('compact')) {
  42. escodegenParams.format = {};
  43. escodegenParams.format.compact = options.compact;
  44. }
  45. return escodegen.generate(astTree, escodegenParams);
  46. }
  47. }
  48. module.exports = JavaScriptObfuscator;