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