index.ts 1.7 KB

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