AbstractCustomNodeGroup.ts 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. import { injectable, inject } from 'inversify';
  2. import { ServiceIdentifiers } from '../container/ServiceIdentifiers';
  3. import { TNodeWithBlockStatement } from '../types/node/TNodeWithBlockStatement';
  4. import { ICustomNode } from '../interfaces/custom-nodes/ICustomNode';
  5. import { ICustomNodeGroup } from '../interfaces/custom-nodes/ICustomNodeGroup';
  6. import { IOptions } from '../interfaces/options/IOptions';
  7. import { IRandomGenerator } from '../interfaces/utils/IRandomGenerator';
  8. import { IStackTraceData } from '../interfaces/stack-trace-analyzer/IStackTraceData';
  9. import { CustomNode } from '../enums/container/custom-nodes/CustomNode';
  10. import { ObfuscationEvent } from '../enums/event-emitters/ObfuscationEvent';
  11. @injectable()
  12. export abstract class AbstractCustomNodeGroup implements ICustomNodeGroup {
  13. /**
  14. * @type {ObfuscationEvent}
  15. */
  16. protected abstract readonly appendEvent: ObfuscationEvent;
  17. /**
  18. * @type {Map<CustomNode, ICustomNode>}
  19. */
  20. protected abstract customNodes: Map <CustomNode, ICustomNode>;
  21. /**
  22. * @type {IStackTraceData[]}
  23. */
  24. protected readonly stackTraceData: IStackTraceData[];
  25. /**
  26. * @type {IOptions}
  27. */
  28. protected readonly options: IOptions;
  29. /**
  30. * @type {IRandomGenerator}
  31. */
  32. protected readonly randomGenerator: IRandomGenerator;
  33. /**
  34. * @param randomGenerator
  35. * @param options
  36. */
  37. constructor (
  38. @inject(ServiceIdentifiers.IRandomGenerator) randomGenerator: IRandomGenerator,
  39. @inject(ServiceIdentifiers.IOptions) options: IOptions
  40. ) {
  41. this.randomGenerator = randomGenerator;
  42. this.options = options;
  43. }
  44. /**
  45. * @param blockScopeNode
  46. * @param stackTraceData
  47. */
  48. public abstract appendCustomNodes (blockScopeNode: TNodeWithBlockStatement, stackTraceData: IStackTraceData[]): void;
  49. /**
  50. * @returns {ObfuscationEvent}
  51. */
  52. public getAppendEvent (): ObfuscationEvent {
  53. return this.appendEvent;
  54. }
  55. /**
  56. * @returns {Map<CustomNode, ICustomNode>}
  57. */
  58. public getCustomNodes (): Map <CustomNode, ICustomNode> {
  59. return this.customNodes;
  60. }
  61. public abstract initialize (): void;
  62. /**
  63. * @param customNodeName
  64. * @param callback
  65. */
  66. protected appendCustomNodeIfExist (customNodeName: CustomNode, callback: (customNode: ICustomNode) => void): void {
  67. const customNode: ICustomNode | undefined = this.customNodes.get(customNodeName);
  68. if (!customNode) {
  69. return;
  70. }
  71. callback(customNode);
  72. }
  73. /**
  74. * @param stackTraceLength
  75. */
  76. protected getRandomStackTraceIndex (stackTraceLength: number): number {
  77. return this.randomGenerator.getRandomInteger(0, Math.max(0, Math.round(stackTraceLength - 1)));
  78. }
  79. }