AbstractCustomNodeGroup.ts 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. import { inject, injectable } from 'inversify';
  2. import { ServiceIdentifiers } from '../container/ServiceIdentifiers';
  3. import { TIdentifierNamesGeneratorFactory } from '../types/container/generators/TIdentifierNamesGeneratorFactory';
  4. import { TNodeWithStatements } from '../types/node/TNodeWithStatements';
  5. import { ICallsGraphData } from '../interfaces/analyzers/calls-graph-analyzer/ICallsGraphData';
  6. import { ICustomNode } from '../interfaces/custom-nodes/ICustomNode';
  7. import { ICustomNodeGroup } from '../interfaces/custom-nodes/ICustomNodeGroup';
  8. import { IIdentifierNamesGenerator } from '../interfaces/generators/identifier-names-generators/IIdentifierNamesGenerator';
  9. import { IOptions } from '../interfaces/options/IOptions';
  10. import { IRandomGenerator } from '../interfaces/utils/IRandomGenerator';
  11. import { CustomNode } from '../enums/custom-nodes/CustomNode';
  12. import { ObfuscationEvent } from '../enums/event-emitters/ObfuscationEvent';
  13. @injectable()
  14. export abstract class AbstractCustomNodeGroup implements ICustomNodeGroup {
  15. /**
  16. * @type {IIdentifierNamesGenerator}
  17. */
  18. protected readonly identifierNamesGenerator: IIdentifierNamesGenerator;
  19. /**
  20. * @type {IOptions}
  21. */
  22. protected readonly options: IOptions;
  23. /**
  24. * @type {IRandomGenerator}
  25. */
  26. protected readonly randomGenerator: IRandomGenerator;
  27. /**
  28. * @type {ObfuscationEvent}
  29. */
  30. protected abstract readonly appendEvent: ObfuscationEvent;
  31. /**
  32. * @type {Map<CustomNode, ICustomNode>}
  33. */
  34. protected abstract customNodes: Map <CustomNode, ICustomNode>;
  35. /**
  36. * @param {TIdentifierNamesGeneratorFactory} identifierNamesGeneratorFactory
  37. * @param {IRandomGenerator} randomGenerator
  38. * @param {IOptions} options
  39. */
  40. public constructor (
  41. @inject(ServiceIdentifiers.Factory__IIdentifierNamesGenerator)
  42. identifierNamesGeneratorFactory: TIdentifierNamesGeneratorFactory,
  43. @inject(ServiceIdentifiers.IRandomGenerator) randomGenerator: IRandomGenerator,
  44. @inject(ServiceIdentifiers.IOptions) options: IOptions
  45. ) {
  46. this.identifierNamesGenerator = identifierNamesGeneratorFactory(options);
  47. this.randomGenerator = randomGenerator;
  48. this.options = options;
  49. }
  50. /**
  51. * @returns {ObfuscationEvent}
  52. */
  53. public getAppendEvent (): ObfuscationEvent {
  54. return this.appendEvent;
  55. }
  56. /**
  57. * @returns {Map<CustomNode, ICustomNode>}
  58. */
  59. public getCustomNodes (): Map <CustomNode, ICustomNode> {
  60. return this.customNodes;
  61. }
  62. /**
  63. * @param {CustomNode} customNodeName
  64. * @param {callback} 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 {number} callsGraphLength
  75. * @returns {number}
  76. */
  77. protected getRandomCallsGraphIndex (callsGraphLength: number): number {
  78. return this.randomGenerator.getRandomInteger(0, Math.max(0, Math.round(callsGraphLength - 1)));
  79. }
  80. /**
  81. * @param {TNodeWithStatements} nodeWithStatements
  82. * @param {ICallsGraphData[]} callsGraphData
  83. */
  84. public abstract appendCustomNodes (nodeWithStatements: TNodeWithStatements, callsGraphData: ICallsGraphData[]): void;
  85. public abstract initialize (): void;
  86. }