AbstractCustomNodeGroup.ts 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. import { inject, injectable } 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/analyzers/stack-trace-analyzer/IStackTraceData';
  9. import { CustomNode } from '../enums/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 {IRandomGenerator} randomGenerator
  35. * @param {IOptions} 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 {TNodeWithBlockStatement} blockScopeNode
  46. * @param {IStackTraceData[]} 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 {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} stackTraceLength
  75. * @returns {number}
  76. */
  77. protected getRandomStackTraceIndex (stackTraceLength: number): number {
  78. return this.randomGenerator.getRandomInteger(0, Math.max(0, Math.round(stackTraceLength - 1)));
  79. }
  80. }