NodeObfuscator.ts 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. import { INode } from '../interfaces/INode';
  2. import { INodeObfuscator } from '../interfaces/INodeObfuscator';
  3. import { Utils } from '../Utils';
  4. export abstract class NodeObfuscator implements INodeObfuscator {
  5. /**
  6. * @type Map <string, Node>
  7. */
  8. protected nodes: Map <string, INode>;
  9. /**
  10. * @param nodes
  11. */
  12. constructor(nodes: Map <string, INode>) {
  13. this.nodes = nodes;
  14. }
  15. /**
  16. * @param node
  17. * @param parentNode
  18. */
  19. public abstract obfuscateNode (node: any, parentNode?: any): void;
  20. /**
  21. * @param node
  22. * @param parentNode
  23. * @param namesMap
  24. */
  25. protected replaceNodeIdentifierByNewValue (node: any, parentNode: any, namesMap: Map <string, string>) {
  26. if (node.type === 'Identifier' && namesMap.has(node.name)) {
  27. if (
  28. (parentNode.type === 'Property' && parentNode.key === node) ||
  29. (parentNode.type === 'MemberExpression' && parentNode.computed === false && parentNode.property === node)
  30. ) {
  31. return;
  32. }
  33. node.name = namesMap.get(node.name);
  34. }
  35. }
  36. /**
  37. * @param nodeValue
  38. * @returns {string}
  39. */
  40. protected replaceLiteralStringByArrayElement (nodeValue: string): string {
  41. let value: string = Utils.stringToUnicode(nodeValue),
  42. unicodeArray = this.nodes.get('unicodeArrayNode').getNodeData(),
  43. sameIndex: number = unicodeArray.indexOf(value),
  44. index: number;
  45. if (sameIndex < 0) {
  46. index = unicodeArray.length;
  47. unicodeArray.push(Utils.stringToUnicode(nodeValue));
  48. } else {
  49. index = sameIndex;
  50. }
  51. return `${this.nodes.get('unicodeArrayNode').getNodeIdentifier()}[${index}]`;
  52. }
  53. }