DebugProtectionFunctionNode.ts 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. import { inject, injectable, } from 'inversify';
  2. import { ServiceIdentifiers } from '../../container/ServiceIdentifiers';
  3. import * as format from 'string-template';
  4. import { TIdentifierNameGeneratorFactory } from '../../types/container/generators/TIdentifierNameGeneratorFactory';
  5. import { TStatement } from '../../types/node/TStatement';
  6. import { IOptions } from '../../interfaces/options/IOptions';
  7. import { IRandomGenerator } from '../../interfaces/utils/IRandomGenerator';
  8. import { ObfuscationTarget } from '../../enums/ObfuscationTarget';
  9. import { initializable } from '../../decorators/Initializable';
  10. import { DebuggerTemplate } from '../../templates/debug-protection-nodes/debug-protection-function-node/DebuggerTemplate';
  11. import { DebuggerTemplateNoEval } from '../../templates/debug-protection-nodes/debug-protection-function-node/DebuggerTemplateNoEval';
  12. import { DebugProtectionFunctionTemplate } from '../../templates/debug-protection-nodes/debug-protection-function-node/DebugProtectionFunctionTemplate';
  13. import { AbstractCustomNode } from '../AbstractCustomNode';
  14. import { NodeUtils } from '../../node/NodeUtils';
  15. @injectable()
  16. export class DebugProtectionFunctionNode extends AbstractCustomNode {
  17. /**
  18. * @type {string}
  19. */
  20. @initializable()
  21. private debugProtectionFunctionName: string;
  22. /**
  23. * @param {TIdentifierNameGeneratorFactory} identifierNameGeneratorFactory
  24. * @param {IRandomGenerator} randomGenerator
  25. * @param {IOptions} options
  26. */
  27. constructor (
  28. @inject(ServiceIdentifiers.Factory__IIdentifierNameGenerator)
  29. identifierNameGeneratorFactory: TIdentifierNameGeneratorFactory,
  30. @inject(ServiceIdentifiers.IRandomGenerator) randomGenerator: IRandomGenerator,
  31. @inject(ServiceIdentifiers.IOptions) options: IOptions
  32. ) {
  33. super(identifierNameGeneratorFactory, randomGenerator, options);
  34. }
  35. /**
  36. * @param {string} debugProtectionFunctionName
  37. */
  38. public initialize (debugProtectionFunctionName: string): void {
  39. this.debugProtectionFunctionName = debugProtectionFunctionName;
  40. }
  41. /**
  42. * @returns {TStatement[]}
  43. */
  44. protected getNodeStructure (): TStatement[] {
  45. return NodeUtils.convertCodeToStructure(this.getTemplate());
  46. }
  47. /**
  48. * @returns {string}
  49. */
  50. protected getTemplate (): string {
  51. const debuggerTemplate: string = this.options.target !== ObfuscationTarget.Extension
  52. ? DebuggerTemplate()
  53. : DebuggerTemplateNoEval();
  54. return format(DebugProtectionFunctionTemplate(), {
  55. debuggerTemplate,
  56. debugProtectionFunctionName: this.debugProtectionFunctionName
  57. });
  58. }
  59. }