UNPKG

js-confuser

Version:

JavaScript Obfuscation Tool.

107 lines (98 loc) 5.17 kB
"use strict"; function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); } Object.defineProperty(exports, "__esModule", { value: true }); exports["default"] = void 0; var t = _interopRequireWildcard(require("@babel/types")); var _assert = require("assert"); var _order = require("../../order"); var _astUtils = require("../../utils/ast-utils"); var _node = require("../../utils/node"); function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function _interopRequireWildcard(e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, "default": e }; if (null === e || "object" != _typeof(e) && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (var _t in e) "default" !== _t && {}.hasOwnProperty.call(e, _t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, _t)) && (i.get || i.set) ? o(f, _t, i) : f[_t] = e[_t]); return f; })(e, t); } function fail() { throw new Error("Assertion failed"); } var _default = exports["default"] = function _default(_ref) { var Plugin = _ref.Plugin; var me = Plugin(_order.Order.DuplicateLiteralsRemoval, { changeData: { literals: 0 } }); return { visitor: { Program: { enter: function enter(programPath) { var arrayName = me.getPlaceholder() + "_dlrArray"; // Collect all literals var literalsMap = new Map(); var firstTimeMap = new Map(); var arrayExpression = t.arrayExpression([]); var createMemberExpression = function createMemberExpression(index) { return t.memberExpression(t.identifier(arrayName), (0, _node.numericLiteral)(index), true); }; // Traverse through all nodes to find literals programPath.traverse({ "StringLiteral|BooleanLiteral|NumericLiteral|NullLiteral|Identifier": function StringLiteralBooleanLiteralNumericLiteralNullLiteralIdentifier(_path) { var literalPath = _path; // Don't change module imports if (literalPath.isStringLiteral()) { if ((0, _astUtils.isModuleImport)(literalPath)) return; } var node = literalPath.node; var isUndefined = false; if (literalPath.isIdentifier()) { // Only referenced variable names if (!literalPath.isReferencedIdentifier()) return; // undefined = true; // Skip if (literalPath.isBindingIdentifier()) return; // Allow 'undefined' to be redefined if (literalPath.scope.hasBinding(literalPath.node.name, { noGlobals: true })) return; if (literalPath.node.name === "undefined") { isUndefined = true; } else { return; } } if (t.isRegExpLiteral(node) || t.isTemplateLiteral(node) || t.isDirectiveLiteral(node)) return; var value = isUndefined ? undefined : t.isNullLiteral(node) ? null : t.isLiteral(node) ? node.value : fail(); if (typeof value !== "string" && typeof value !== "number" && typeof value !== "boolean" && value !== null && value !== undefined) { return; } // Skip empty strings if (typeof value === "string" && value.length === 0) return; var index = -1; if (literalsMap.has(value)) { index = literalsMap.get(value); } else if (firstTimeMap.has(value)) { // Create new index index = literalsMap.size; literalsMap.set(value, index); var firstPath = firstTimeMap.get(value); me.changeData.literals++; (0, _astUtils.ensureComputedExpression)(firstPath); firstPath.replaceWith(createMemberExpression(index)); arrayExpression.elements.push((0, _node.createLiteral)(value)); } else { firstTimeMap.set(value, literalPath); return; } (0, _assert.ok)(index !== -1); me.changeData.literals++; (0, _astUtils.ensureComputedExpression)(literalPath); literalPath.replaceWith(createMemberExpression(index)); literalPath.skip(); } }); if (arrayExpression.elements.length === 0) return; // Create the literals array declaration var itemsArrayDeclaration = t.variableDeclaration("const", [t.variableDeclarator(t.identifier(arrayName), arrayExpression)]); (0, _astUtils.prepend)(programPath, itemsArrayDeclaration); } } } }; };