index.ts 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  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 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: IProgramNode = esprima.parse(sourceCode),
  22. obfuscator: Obfuscator = new Obfuscator(options);
  23. obfuscator.obfuscateNode(astTree);
  24. return JavaScriptObfuscator.generateCode(astTree, options);
  25. }
  26. /**
  27. * @param astTree
  28. * @param options
  29. */
  30. private static generateCode (astTree: IProgramNode, options: any = {}): string {
  31. let escodegenParams: any = Object.assign({}, JavaScriptObfuscator.escodegenParams);
  32. if (options.hasOwnProperty('compact')) {
  33. escodegenParams.format.compact = options.compact;
  34. }
  35. return escodegen.generate(astTree, escodegenParams);
  36. }
  37. }
  38. module.exports = JavaScriptObfuscator;