AbstractCustomNodeGroup.ts 3.6 KB

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