NodeCallsControllerFunctionNode.ts 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. import { inject, injectable, } from 'inversify';
  2. import { ServiceIdentifiers } from '../../container/ServiceIdentifiers';
  3. import * as format from 'string-template';
  4. import { TIdentifierNameGeneratorFactory } from '../../types/container/generators/TIdentifierNameGeneratorFactory';
  5. import { TStatement } from '../../types/node/TStatement';
  6. import { IOptions } from '../../interfaces/options/IOptions';
  7. import { IRandomGenerator } from '../../interfaces/utils/IRandomGenerator';
  8. import { ObfuscationEvent } from '../../enums/event-emitters/ObfuscationEvent';
  9. import { initializable } from '../../decorators/Initializable';
  10. import { SingleNodeCallControllerTemplate } from '../../templates/SingleNodeCallControllerTemplate';
  11. import { NO_CUSTOM_NODES_PRESET } from '../../options/presets/NoCustomNodes';
  12. import { AbstractCustomNode } from '../AbstractCustomNode';
  13. import { JavaScriptObfuscator } from '../../JavaScriptObfuscatorFacade';
  14. import { NodeUtils } from '../../node/NodeUtils';
  15. @injectable()
  16. export class NodeCallsControllerFunctionNode extends AbstractCustomNode {
  17. /**
  18. * @type {string}
  19. */
  20. @initializable()
  21. protected callsControllerFunctionName: string;
  22. /**
  23. * @type {ObfuscationEvent}
  24. */
  25. @initializable()
  26. private appendEvent: ObfuscationEvent;
  27. /**
  28. * @param {TIdentifierNameGeneratorFactory} identifierNameGeneratorFactory
  29. * @param {IRandomGenerator} randomGenerator
  30. * @param {IOptions} options
  31. */
  32. constructor (
  33. @inject(ServiceIdentifiers.Factory__IIdentifierNameGenerator)
  34. identifierNameGeneratorFactory: TIdentifierNameGeneratorFactory,
  35. @inject(ServiceIdentifiers.IRandomGenerator) randomGenerator: IRandomGenerator,
  36. @inject(ServiceIdentifiers.IOptions) options: IOptions
  37. ) {
  38. super(identifierNameGeneratorFactory, randomGenerator, options);
  39. }
  40. /**
  41. * @param {ObfuscationEvent} appendEvent
  42. * @param {string} callsControllerFunctionName
  43. */
  44. public initialize (appendEvent: ObfuscationEvent, callsControllerFunctionName: string): void {
  45. this.appendEvent = appendEvent;
  46. this.callsControllerFunctionName = callsControllerFunctionName;
  47. }
  48. /**
  49. * @returns {TStatement[]}
  50. */
  51. protected getNodeStructure (): TStatement[] {
  52. return NodeUtils.convertCodeToStructure(this.getTemplate());
  53. }
  54. /**
  55. * @returns {string}
  56. */
  57. protected getTemplate (): string {
  58. if (this.appendEvent === ObfuscationEvent.AfterObfuscation) {
  59. return JavaScriptObfuscator.obfuscate(
  60. format(SingleNodeCallControllerTemplate(), {
  61. singleNodeCallControllerFunctionName: this.callsControllerFunctionName
  62. }),
  63. {
  64. ...NO_CUSTOM_NODES_PRESET,
  65. mangle: this.options.mangle,
  66. seed: this.options.seed
  67. }
  68. ).getObfuscatedCode();
  69. }
  70. return format(SingleNodeCallControllerTemplate(), {
  71. singleNodeCallControllerFunctionName: this.callsControllerFunctionName
  72. });
  73. }
  74. }