Code coverage report for src/node-obfuscators/NodeObfuscator.js

# All files / src/node-obfuscators NodeObfuscator.js

91.3% Statements 42/46
85.29% Branches 29/34
50% Functions 1/2
90.24% Lines 37/41

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    1x
    1x
    1x
    1x

    80x
    80x
    80x

    19x

    19x
    19x
    19x

    1332x
    55x
    55x

    55x

    55x

    3x

    44x
    44x
    1x

    43x

    38x
    38x

    38x
    38x
    7x

    31x

    31x
    31x
    6x

    25x
    25x

    31x
    31x
    29x

    2x

    1x
     E"use strict";
    const estraverse = require('estraverse');
    const JSFuck_1 = require("../enums/JSFuck");
    const Nodes_1 = require("../Nodes");
    const Utils_1 = require('../Utils');
    class NodeObfuscator {
        constructor(nodes, options) {
            this.nodes = nodes;
            this.options = options;
        }
        isReservedName(name) {
            return this.options.get('reservedNames')
                .some((reservedName) => {
                return new RegExp(reservedName, 'g').test(name);
            });
        }
        storeIdentifiersNames(node, namesMap) {
            if (Nodes_1.Nodes.isIdentifierNode(node) && !this.isReservedName(node.name)) {
                namesMap.set(node.name, Utils_1.Utils.getRandomVariableName());
                return;
            }
            return estraverse.VisitorOption.Skip;
        }
        replaceIdentifiersWithRandomNames(node, parentNode, namesMap) {
            if (Nodes_1.Nodes.isIdentifierNode(node) && namesMap.has(node.name)) {
                const parentNodeIsPropertyNode = (Nodes_1.Nodes.isPropertyNode(parentNode) &&
                    parentNode.key === node), parentNodeIsMemberExpressionNode = (Nodes_1.Nodes.isMemberExpressionNode(parentNode) &&
                    parentNode.computed === false &&
                    parentNode.property === node);
                if (parentNodeIsPropertyNode || parentNodeIsMemberExpressionNode) I{
                    return;
                }
                node.name = namesMap.get(node.name);
            }
        }
        replaceLiteralBooleanWithJSFuck(nodeValue) {
            return nodeValue ? JSFuck_1.JSFuck.True : JSFuck_1.JSFuck.False;
        }
        replaceLiteralNumberWithHexadecimalValue(nodeValue) {
            const prefix = '0x';
            if (!Utils_1.Utils.isInteger(nodeValue)) {
                return String(nodeValue);
            }
            return `${prefix}${Utils_1.Utils.decToHex(nodeValue)}`;
        }
        replaceLiteralValueWithUnicodeValue(nodeValue) {
            let value = nodeValue, replaceWithUnicodeArrayFlag = Math.random() <= this.options.get('unicodeArrayThreshold');
            if (this.options.get('encodeUnicodeArray') && replaceWithUnicodeArrayFlag) I{
                value = Utils_1.Utils.btoa(value);
            }
            value = Utils_1.Utils.stringToUnicode(value);
            if (!this.options.get('unicodeArray') || !replaceWithUnicodeArrayFlag) {
                return value;
            }
            return this.replaceLiteralValueWithUnicodeArrayCall(value);
        }
        replaceLiteralValueWithUnicodeArrayCall(value) {
            let unicodeArrayNode = this.nodes.get('unicodeArrayNode'), unicodeArray = unicodeArrayNode.getNodeData(), sameIndex = unicodeArray.indexOf(value), index, hexadecimalIndex;
            if (sameIndex >= 0) {
                index = sameIndex;
            }
            else {
                index = unicodeArray.length;
                unicodeArrayNode.updateNodeData(value);
            }
            hexadecimalIndex = this.replaceLiteralNumberWithHexadecimalValue(index);
            if (this.options.get('wrapUnicodeArrayCalls')) {
                return `${this.nodes.get('unicodeArrayCallsWrapper').getNodeIdentifier()}('${hexadecimalIndex}')`;
            }
            return `${unicodeArrayNode.getNodeIdentifier()}[${hexadecimalIndex}]`;
        }
    }
    exports.NodeObfuscator = NodeObfuscator;
    //# sourceMappingURL=NodeObfuscator.js.map