index.ts 1.7 KB

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