index.ts 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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. export class JavaScriptObfuscator {
  7. /**
  8. * @type any
  9. */
  10. private static defaultOptions: any = {
  11. compact: true,
  12. debugProtection: false,
  13. debugProtectionInterval: false,
  14. rotateUnicodeArray: true
  15. };
  16. /**
  17. * @type any
  18. */
  19. private static escodegenParams: any = {
  20. verbatim: 'x-verbatim-property'
  21. };
  22. /**
  23. * @param sourceCode
  24. * @param customOptions
  25. */
  26. public static obfuscate (sourceCode: string, customOptions: any): string {
  27. let astTree: IProgramNode = esprima.parse(sourceCode),
  28. options: any = Object.assign(JavaScriptObfuscator.defaultOptions, customOptions),
  29. obfuscator: Obfuscator = new Obfuscator(options);
  30. obfuscator.obfuscateNode(astTree);
  31. return JavaScriptObfuscator.generateCode(astTree, options);
  32. }
  33. /**
  34. * @param astTree
  35. * @param options
  36. */
  37. private static generateCode (astTree: IProgramNode, options: any): string {
  38. let escodegenParams: any = Object.assign({}, JavaScriptObfuscator.escodegenParams);
  39. if (options.hasOwnProperty('compact')) {
  40. escodegenParams.format = {};
  41. escodegenParams.format.compact = options.compact;
  42. }
  43. return escodegen.generate(astTree, escodegenParams);
  44. }
  45. }