ControlFlowStorageNode.ts 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. import { injectable, inject } from 'inversify';
  2. import { ServiceIdentifiers } from '../../container/ServiceIdentifiers';
  3. import * as format from 'string-template';
  4. import { TNodeWithBlockStatement } from '../../types/node/TNodeWithBlockStatement';
  5. import { TObfuscationEvent } from '../../types/event-emitters/TObfuscationEvent';
  6. import { ICustomNode } from '../../interfaces/custom-nodes/ICustomNode';
  7. import { IOptions } from '../../interfaces/options/IOptions';
  8. import { IStackTraceData } from '../../interfaces/stack-trace-analyzer/IStackTraceData';
  9. import { IStorage } from '../../interfaces/storages/IStorage';
  10. import { ObfuscationEvents } from '../../enums/ObfuscationEvents';
  11. import { initializable } from '../../decorators/Initializable';
  12. import { ControlFlowStorageTemplate } from '../../templates/custom-nodes/control-flow-storage-nodes/ControlFlowStorageTemplate';
  13. import { AbstractCustomNode } from '../AbstractCustomNode';
  14. import { NodeAppender } from '../../node/NodeAppender';
  15. @injectable()
  16. export class ControlFlowStorageNode extends AbstractCustomNode {
  17. /**
  18. * @type {TObfuscationEvent}
  19. */
  20. protected readonly appendEvent: TObfuscationEvent = ObfuscationEvents.AfterObfuscation;
  21. /**
  22. * @type {IStorage <ICustomNode>}
  23. */
  24. @initializable()
  25. private controlFlowStorage: IStorage <ICustomNode>;
  26. /**
  27. * @type {string}
  28. */
  29. @initializable()
  30. private controlFlowStorageName: string;
  31. /**
  32. * @param options
  33. */
  34. constructor (
  35. @inject(ServiceIdentifiers.IOptions) options: IOptions
  36. ) {
  37. super(options);
  38. }
  39. /**
  40. * @param controlFlowStorage
  41. * @param controlFlowStorageName
  42. */
  43. public initialize (controlFlowStorage: IStorage <ICustomNode>, controlFlowStorageName: string): void {
  44. this.controlFlowStorage = controlFlowStorage;
  45. this.controlFlowStorageName = controlFlowStorageName;
  46. }
  47. /**
  48. * @param blockScopeNode
  49. * @param stackTraceData
  50. */
  51. public appendNode (blockScopeNode: TNodeWithBlockStatement, stackTraceData: IStackTraceData[]): void {
  52. NodeAppender.prependNode(blockScopeNode, this.getNode());
  53. }
  54. /**
  55. * @returns {string}
  56. */
  57. public getCode (): string {
  58. return format(ControlFlowStorageTemplate(), {
  59. controlFlowStorage: this.controlFlowStorage.toString(),
  60. controlFlowStorageName: this.controlFlowStorageName
  61. });
  62. }
  63. }