CallsControllerFunctionCodeHelper.ts 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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 { ICustomCodeHelperObfuscator } from '../../interfaces/custom-code-helpers/ICustomCodeHelperObfuscator';
  6. import { ICustomCodeHelperFormatter } from '../../interfaces/custom-code-helpers/ICustomCodeHelperFormatter';
  7. import { IOptions } from '../../interfaces/options/IOptions';
  8. import { IRandomGenerator } from '../../interfaces/utils/IRandomGenerator';
  9. import { ObfuscationEvent } from '../../enums/event-emitters/ObfuscationEvent';
  10. import { initializable } from '../../decorators/Initializable';
  11. import { SingleCallControllerTemplate } from '../common/templates/SingleCallControllerTemplate';
  12. import { AbstractCustomCodeHelper } from '../AbstractCustomCodeHelper';
  13. import { NodeUtils } from '../../node/NodeUtils';
  14. @injectable()
  15. export class CallsControllerFunctionCodeHelper extends AbstractCustomCodeHelper {
  16. /**
  17. * @type {string}
  18. */
  19. @initializable()
  20. protected callsControllerFunctionName!: string;
  21. /**
  22. * @type {ObfuscationEvent}
  23. */
  24. @initializable()
  25. private appendEvent!: ObfuscationEvent;
  26. /**
  27. * @param {TIdentifierNamesGeneratorFactory} identifierNamesGeneratorFactory
  28. * @param {ICustomCodeHelperFormatter} customCodeHelperFormatter
  29. * @param {ICustomCodeHelperObfuscator} customCodeHelperObfuscator
  30. * @param {IRandomGenerator} randomGenerator
  31. * @param {IOptions} options
  32. */
  33. public constructor (
  34. @inject(ServiceIdentifiers.Factory__IIdentifierNamesGenerator)
  35. identifierNamesGeneratorFactory: TIdentifierNamesGeneratorFactory,
  36. @inject(ServiceIdentifiers.ICustomCodeHelperFormatter) customCodeHelperFormatter: ICustomCodeHelperFormatter,
  37. @inject(ServiceIdentifiers.ICustomCodeHelperObfuscator) customCodeHelperObfuscator: ICustomCodeHelperObfuscator,
  38. @inject(ServiceIdentifiers.IRandomGenerator) randomGenerator: IRandomGenerator,
  39. @inject(ServiceIdentifiers.IOptions) options: IOptions
  40. ) {
  41. super(
  42. identifierNamesGeneratorFactory,
  43. customCodeHelperFormatter,
  44. customCodeHelperObfuscator,
  45. randomGenerator,
  46. options
  47. );
  48. }
  49. /**
  50. * @param {ObfuscationEvent} appendEvent
  51. * @param {string} callsControllerFunctionName
  52. */
  53. public initialize (appendEvent: ObfuscationEvent, callsControllerFunctionName: string): void {
  54. this.appendEvent = appendEvent;
  55. this.callsControllerFunctionName = callsControllerFunctionName;
  56. }
  57. /**
  58. * @param {string} codeHelperTemplate
  59. * @returns {TStatement[]}
  60. */
  61. protected getNodeStructure (codeHelperTemplate: string): TStatement[] {
  62. return NodeUtils.convertCodeToStructure(codeHelperTemplate);
  63. }
  64. /**
  65. * @returns {string}
  66. */
  67. protected getCodeHelperTemplate (): string {
  68. if (this.appendEvent === ObfuscationEvent.AfterObfuscation) {
  69. return this.customCodeHelperObfuscator.obfuscateTemplate(
  70. this.customCodeHelperFormatter.formatTemplate(SingleCallControllerTemplate(), {
  71. callControllerFunctionName: this.callsControllerFunctionName
  72. })
  73. );
  74. }
  75. return this.customCodeHelperFormatter.formatTemplate(SingleCallControllerTemplate(), {
  76. callControllerFunctionName: this.callsControllerFunctionName
  77. });
  78. }
  79. }