ConsoleOutputDisableExpressionNode.ts 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. import { inject, injectable, } from 'inversify';
  2. import { ServiceIdentifiers } from '../../container/ServiceIdentifiers';
  3. import { TIdentifierNamesGeneratorFactory } from '../../types/container/generators/TIdentifierNamesGeneratorFactory';
  4. import { TStatement } from '../../types/node/TStatement';
  5. import { IOptions } from '../../interfaces/options/IOptions';
  6. import { IRandomGenerator } from '../../interfaces/utils/IRandomGenerator';
  7. import { ICustomNodeFormatter } from '../../interfaces/custom-nodes/ICustomNodeFormatter';
  8. import { ObfuscationTarget } from '../../enums/ObfuscationTarget';
  9. import { ConsoleOutputDisableExpressionTemplate } from '../../templates/console-output-nodes/console-output-disable-expression-node/ConsoleOutputDisableExpressionTemplate';
  10. import { GlobalVariableNoEvalTemplate } from '../../templates/GlobalVariableNoEvalTemplate';
  11. import { initializable } from '../../decorators/Initializable';
  12. import { AbstractCustomNode } from '../AbstractCustomNode';
  13. import { NodeUtils } from '../../node/NodeUtils';
  14. @injectable()
  15. export class ConsoleOutputDisableExpressionNode extends AbstractCustomNode {
  16. /**
  17. * @type {string}
  18. */
  19. @initializable()
  20. private callsControllerFunctionName!: string;
  21. /**
  22. * @param {TIdentifierNamesGeneratorFactory} identifierNamesGeneratorFactory
  23. * @param {ICustomNodeFormatter} customNodeFormatter
  24. * @param {IRandomGenerator} randomGenerator
  25. * @param {IOptions} options
  26. */
  27. public constructor (
  28. @inject(ServiceIdentifiers.Factory__IIdentifierNamesGenerator)
  29. identifierNamesGeneratorFactory: TIdentifierNamesGeneratorFactory,
  30. @inject(ServiceIdentifiers.ICustomNodeFormatter) customNodeFormatter: ICustomNodeFormatter,
  31. @inject(ServiceIdentifiers.IRandomGenerator) randomGenerator: IRandomGenerator,
  32. @inject(ServiceIdentifiers.IOptions) options: IOptions
  33. ) {
  34. super(identifierNamesGeneratorFactory, customNodeFormatter, randomGenerator, options);
  35. }
  36. /**
  37. * @param {string} callsControllerFunctionName
  38. */
  39. public initialize (callsControllerFunctionName: string): void {
  40. this.callsControllerFunctionName = callsControllerFunctionName;
  41. }
  42. /**
  43. * @param {string} nodeTemplate
  44. * @returns {TStatement[]}
  45. */
  46. protected getNodeStructure (nodeTemplate: string): TStatement[] {
  47. return NodeUtils.convertCodeToStructure(nodeTemplate);
  48. }
  49. /**
  50. * @returns {string}
  51. */
  52. protected getNodeTemplate (): string {
  53. const globalVariableTemplate: string = this.options.target !== ObfuscationTarget.BrowserNoEval
  54. ? this.getGlobalVariableTemplate()
  55. : GlobalVariableNoEvalTemplate();
  56. return this.customNodeFormatter.formatTemplate(ConsoleOutputDisableExpressionTemplate(), {
  57. consoleLogDisableFunctionName: this.identifierNamesGenerator.generate(),
  58. globalVariableTemplate,
  59. singleNodeCallControllerFunctionName: this.callsControllerFunctionName
  60. });
  61. }
  62. }