ReservedStringObfuscatingGuard.ts 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. import { inject, injectable } from 'inversify';
  2. import * as ESTree from 'estree';
  3. import { IObfuscatingGuard } from '../../../interfaces/node-transformers/preparing-transformers/obfuscating-guards/IObfuscatingGuard';
  4. import { IOptions } from '../../../interfaces/options/IOptions';
  5. import { ObfuscatingGuardResult } from '../../../enums/node/ObfuscatingGuardResult';
  6. import { ServiceIdentifiers } from '../../../container/ServiceIdentifiers';
  7. import { NodeGuards } from '../../../node/NodeGuards';
  8. @injectable()
  9. export class ReservedStringObfuscatingGuard implements IObfuscatingGuard {
  10. /**
  11. * @type {IOptions}
  12. */
  13. private readonly options: IOptions;
  14. /**
  15. * @param {IOptions} options
  16. */
  17. public constructor (
  18. @inject(ServiceIdentifiers.IOptions) options: IOptions
  19. ) {
  20. this.options = options;
  21. }
  22. /**
  23. * @param {Node} node
  24. * @returns {ObfuscatingGuardResult}
  25. */
  26. public check (node: ESTree.Node): ObfuscatingGuardResult {
  27. if (
  28. this.options.reservedStrings.length
  29. && NodeGuards.isLiteralNode(node)
  30. && typeof node.value === 'string'
  31. ) {
  32. return !this.isReservedString(node.value)
  33. ? ObfuscatingGuardResult.Transform
  34. : ObfuscatingGuardResult.Ignore;
  35. }
  36. return ObfuscatingGuardResult.Transform;
  37. }
  38. /**
  39. * @param {string} value
  40. * @returns {boolean}
  41. */
  42. private isReservedString (value: string): boolean {
  43. return this.options.reservedStrings
  44. .some((reservedString: string) => {
  45. return new RegExp(reservedString, 'g').exec(value) !== null;
  46. });
  47. }
  48. }