reverse-skills-max: initial public release (restored, redacted, hardened)

This commit is contained in:
baradok609hecl1pq6qi
2026-10-20 02:17:15 +03:00
commit da24c2bd20
2227 changed files with 443931 additions and 0 deletions

View File

@@ -0,0 +1,271 @@
const fs = require("fs");
const path = require("path");
const parser = require("@babel/parser");
const traverse = require("@babel/traverse").default;
const generator = require("@babel/generator").default;
const t = require("@babel/types");
function countMatches(text, regex) {
const matches = text.match(regex);
return matches ? matches.length : 0;
}
function parseText(text) {
return parser.parse(text, {
sourceType: "unambiguous",
plugins: ["jsx"]
});
}
function printNode(node) {
return generator(node, {
compact: true,
comments: false,
jsescOption: { minimal: true }
}).code;
}
function isLiteralLike(node) {
return (
t.isStringLiteral(node) ||
t.isNumericLiteral(node) ||
t.isBooleanLiteral(node) ||
t.isNullLiteral(node) ||
t.isBigIntLiteral(node) ||
t.isIdentifier(node, { name: "undefined" })
);
}
function isOpcodeDiscriminant(node) {
return t.isIdentifier(node) || t.isMemberExpression(node);
}
function getOpcodeIfDescriptor(node) {
if (!t.isIfStatement(node) || node.alternate) {
return null;
}
const test = node.test;
if (!t.isBinaryExpression(test) || !["==", "===", "!=", "!=="].includes(test.operator)) {
return null;
}
if (isLiteralLike(test.left) && isOpcodeDiscriminant(test.right)) {
return {
discriminantKey: printNode(test.right),
literalKey: printNode(test.left)
};
}
if (isLiteralLike(test.right) && isOpcodeDiscriminant(test.left)) {
return {
discriminantKey: printNode(test.left),
literalKey: printNode(test.right)
};
}
return null;
}
function countOpcodeIfChains(ast) {
const statementLists = [];
traverse(ast, {
Program(path) {
statementLists.push(path.node.body);
},
BlockStatement(path) {
statementLists.push(path.node.body);
},
SwitchCase(path) {
statementLists.push(path.node.consequent);
}
});
let count = 0;
statementLists.forEach((statements) => {
for (let index = 0; index < statements.length; index += 1) {
const firstDescriptor = getOpcodeIfDescriptor(statements[index]);
if (!firstDescriptor) {
continue;
}
const seenLiterals = new Set([firstDescriptor.literalKey]);
let chainLength = 1;
let cursor = index + 1;
while (cursor < statements.length) {
const nextDescriptor = getOpcodeIfDescriptor(statements[cursor]);
if (!nextDescriptor || nextDescriptor.discriminantKey !== firstDescriptor.discriminantKey) {
break;
}
seenLiterals.add(nextDescriptor.literalKey);
chainLength += 1;
cursor += 1;
}
if (chainLength >= 2 && seenLiterals.size >= 2) {
count += chainLength;
index = cursor - 1;
}
}
});
return count;
}
function getReturnArgument(fnNode) {
if (t.isArrowFunctionExpression(fnNode) && !t.isBlockStatement(fnNode.body)) {
return fnNode.body;
}
if (!t.isFunctionExpression(fnNode) && !t.isArrowFunctionExpression(fnNode)) {
return null;
}
if (!t.isBlockStatement(fnNode.body) || fnNode.body.body.length !== 1) {
return null;
}
const [statement] = fnNode.body.body;
return t.isReturnStatement(statement) ? statement.argument : null;
}
function collectReferencedParameterNames(node) {
const names = new Set();
if (!node) {
return names;
}
traverse(t.file(t.program([t.expressionStatement(t.cloneNode(node, true))])), {
noScope: true,
Identifier(path) {
if (!path.isReferencedIdentifier()) {
return;
}
names.add(path.node.name);
}
});
return names;
}
function isThinWrapperFunction(fnNode) {
const returnArgument = getReturnArgument(fnNode);
if (!returnArgument) {
return false;
}
const paramNames = new Set(
fnNode.params.filter((param) => t.isIdentifier(param)).map((param) => param.name)
);
if (paramNames.size === 0) {
return false;
}
const referencedNames = collectReferencedParameterNames(returnArgument);
for (const name of referencedNames) {
if (!paramNames.has(name)) {
return false;
}
}
if (t.isBinaryExpression(returnArgument) || t.isLogicalExpression(returnArgument)) {
return true;
}
if (t.isUnaryExpression(returnArgument) && t.isIdentifier(returnArgument.argument)) {
return true;
}
if (t.isCallExpression(returnArgument)) {
return t.isIdentifier(returnArgument.callee) || t.isMemberExpression(returnArgument.callee);
}
return false;
}
function countDispatcherWrappers(ast) {
let count = 0;
traverse(ast, {
ObjectExpression(path) {
let thinWrapperCount = 0;
let literalPropertyCount = 0;
path.get("properties").forEach((propertyPath) => {
if (!propertyPath.isObjectProperty()) {
return;
}
const valuePath = propertyPath.get("value");
if (valuePath.isStringLiteral() || valuePath.isNumericLiteral() || valuePath.isBooleanLiteral() || valuePath.isNullLiteral()) {
literalPropertyCount += 1;
return;
}
if (isThinWrapperFunction(valuePath.node)) {
thinWrapperCount += 1;
}
});
const looksLikeDispatcherObject =
thinWrapperCount >= 2 || (thinWrapperCount >= 1 && literalPropertyCount >= 2);
if (looksLikeDispatcherObject) {
count += thinWrapperCount;
}
}
});
return count;
}
function collectMetrics(text) {
let opcodeIfChainCount = countMatches(text, /if\s*\(\s*(?:0x[\da-f]+|\d+|["'][^"'\\]*(?:\\.[^"'\\]*)*["'])\s*={2,3}\s*[A-Za-z_$][\w$]*\s*\)/gi);
let dispatcherWrapperCount = countMatches(text, /:\s*function\s*\([^)]*\)\s*\{\s*return\b/g);
try {
const ast = parseText(text);
opcodeIfChainCount = countOpcodeIfChains(ast);
dispatcherWrapperCount = countDispatcherWrappers(ast);
} catch (error) {
// Keep regex fallbacks when the file is not parseable.
}
return {
lineCount: text.split(/\r?\n/).length,
splitPipeCount: countMatches(text, /\.split\(\s*["']\|["']\s*\)/g),
loopSwitchCount: countMatches(text, /(?:while|for)\s*\([^)]*\)\s*\{[\s\S]{0,120}?switch\s*\(/g),
opcodeIfChainCount,
dispatcherWrapperCount,
hexIdentifierCount: countMatches(text, /\b_0x[a-f0-9]+\b/gi)
};
}
function collectMetricsFromFile(filePath) {
const text = fs.readFileSync(filePath, "utf8");
return {
filePath: path.resolve(filePath),
...collectMetrics(text)
};
}
function main() {
const [, , inputPath, outputPath = ""] = process.argv;
if (!inputPath) {
console.error("Usage: node collect-residue-metrics.js <input.js> [output.json]");
process.exit(1);
}
const result = collectMetricsFromFile(inputPath);
const json = JSON.stringify(result, null, 2);
if (outputPath) {
fs.writeFileSync(outputPath, json, "utf8");
} else {
process.stdout.write(`${json}\n`);
}
}
if (require.main === module) {
main();
}
module.exports = {
collectMetrics,
collectMetricsFromFile
};

View File

@@ -0,0 +1,54 @@
const fs = require("fs");
const path = require("path");
const { collectMetricsFromFile } = require("./collect-residue-metrics");
function pushGap(gaps, condition, message) {
if (condition) {
gaps.push(message);
}
}
function compareOutputs(caseId, oursPath, referencePath) {
const ours = collectMetricsFromFile(oursPath);
const reference = collectMetricsFromFile(referencePath);
const gaps = [];
pushGap(gaps, ours.splitPipeCount > reference.splitPipeCount, "still contains unresolved split('|') order sources");
pushGap(gaps, ours.loopSwitchCount > reference.loopSwitchCount, "still contains direct loop/switch flattening");
pushGap(gaps, ours.opcodeIfChainCount > reference.opcodeIfChainCount, "still contains opcode if-chain residue");
pushGap(gaps, ours.dispatcherWrapperCount > reference.dispatcherWrapperCount, "still contains uninlined dispatcher wrappers");
pushGap(gaps, ours.hexIdentifierCount > reference.hexIdentifierCount, "still contains more _0x-style identifiers than the reference");
pushGap(gaps, ours.lineCount > reference.lineCount * 1.2, "output is still significantly noisier than the reference");
return {
caseId,
ours,
reference,
gaps,
status: gaps.length === 0 ? "matched-or-better-on-heuristics" : "residue-remains"
};
}
function main() {
const [, , oursPath, referencePath, outputPath = "", caseId = "unknown"] = process.argv;
if (!oursPath || !referencePath) {
console.error("Usage: node compare-with-reference.js <ours.js> <reference.js> [output.json] [caseId]");
process.exit(1);
}
const result = compareOutputs(caseId, oursPath, referencePath);
const json = JSON.stringify(result, null, 2);
if (outputPath) {
fs.writeFileSync(outputPath, json, "utf8");
} else {
process.stdout.write(`${json}\n`);
}
}
if (require.main === module) {
main();
}
module.exports = {
compareOutputs
};

View File

@@ -0,0 +1,43 @@
const fs = require("fs");
const path = require("path");
const { detectPatterns } = require("./pipeline-config");
function main() {
const [, , inputPath, hint = "", outputPath = ""] = process.argv;
if (!inputPath) {
console.error("Usage: node detect-patterns.js <input.js> [hint] [output.json]");
process.exit(1);
}
const sourceText = fs.readFileSync(inputPath, "utf8");
const result = detectPatterns({
inputPath: path.resolve(inputPath),
hint,
sourceText
});
const serializable = {
inputPath: path.resolve(inputPath),
hint,
bestId: result.bestId,
selected: {
id: result.selected.id,
displayName: result.selected.displayName,
reference: result.selected.reference,
families: result.selected.families,
notes: result.selected.notes
},
detections: result.detections
};
const json = JSON.stringify(serializable, null, 2);
if (outputPath) {
fs.writeFileSync(outputPath, json, "utf8");
} else {
process.stdout.write(`${json}\n`);
}
}
if (require.main === module) {
main();
}

View File

@@ -0,0 +1,775 @@
const generator = require("@babel/generator").default;
const { clone, parseArgs, parseFile, reparse, saveAst, t, traverse } = require("./shared");
function getCaseStatements(casePath) {
return casePath.get("consequent").filter(stmt => !stmt.isContinueStatement() && !stmt.isBreakStatement()).map(stmt => clone(stmt.node));
}
function hasLexicalBindings(nodes) {
return nodes.some((node) => t.isVariableDeclaration(node) && node.kind !== "var");
}
function isSimpleDiscriminant(node) {
return t.isIdentifier(node) || t.isLiteral(node);
}
function normalizeCaseConsequent(nodes) {
const statements = nodes.map((node) => clone(node));
if (statements.length === 0) {
return [t.continueStatement()];
}
const last = statements[statements.length - 1];
if (t.isBreakStatement(last)) {
statements.pop();
statements.push(t.continueStatement());
return statements;
}
if (t.isContinueStatement(last) || t.isReturnStatement(last) || t.isThrowStatement(last)) {
return statements;
}
return null;
}
function buildIfChainFromSwitch(switchNode) {
if (!isSimpleDiscriminant(switchNode.discriminant)) {
return null;
}
let root = null;
let current = null;
let defaultBlock = null;
for (const caseNode of switchNode.cases) {
const normalized = normalizeCaseConsequent(caseNode.consequent);
if (!normalized || hasLexicalBindings(normalized)) {
return null;
}
if (caseNode.test === null) {
defaultBlock = t.blockStatement(normalized);
continue;
}
const test = t.logicalExpression(
"&&",
t.binaryExpression("===", clone(switchNode.discriminant), clone(caseNode.test)),
t.booleanLiteral(true)
);
const nextIf = t.ifStatement(test, t.blockStatement(normalized));
if (!root) {
root = nextIf;
} else {
current.alternate = nextIf;
}
current = nextIf;
}
if (!root) {
return null;
}
if (current && defaultBlock) {
current.alternate = defaultBlock;
}
return root;
}
function replaceLoopSwitchWithIf(path) {
const body = path.get("body");
if (!body.isBlockStatement()) {
return false;
}
const statements = body.get("body");
if (statements.length !== 1 || !statements[0].isSwitchStatement()) {
return false;
}
const ifChain = buildIfChainFromSwitch(statements[0].node);
if (!ifChain) {
return false;
}
body.replaceWith(t.blockStatement([ifChain]));
return true;
}
const UNRESOLVED = Symbol("unresolved");
function getResolvedFunctionCallPath(functionPath) {
if (functionPath.parentPath.isCallExpression() && functionPath.parentKey === "callee") {
return functionPath.parentPath;
}
if (
functionPath.parentPath.isUnaryExpression() &&
functionPath.parentPath.parentPath &&
functionPath.parentPath.parentPath.isCallExpression() &&
functionPath.parentPath.parentKey === "callee"
) {
return functionPath.parentPath.parentPath;
}
return null;
}
function resolveObjectPropertyFromBinding(objectPath, propertyValue, seenBindings = new Set()) {
if (!objectPath || !objectPath.node || !objectPath.isIdentifier()) {
return UNRESOLVED;
}
const binding = objectPath.scope.getBinding(objectPath.node.name);
if (!binding || seenBindings.has(binding)) {
return UNRESOLVED;
}
seenBindings.add(binding);
const declaratorPath = getDeclaratorForBinding(binding);
if (!declaratorPath) {
seenBindings.delete(binding);
return UNRESOLVED;
}
const initPath = declaratorPath.get("init");
if (!initPath.node) {
seenBindings.delete(binding);
return UNRESOLVED;
}
if (initPath.isIdentifier()) {
const resolved = resolveObjectPropertyFromBinding(initPath, propertyValue, seenBindings);
seenBindings.delete(binding);
return resolved;
}
if (!initPath.isObjectExpression()) {
seenBindings.delete(binding);
return UNRESOLVED;
}
for (const propertyPath of initPath.get("properties")) {
if (!propertyPath.isObjectProperty()) {
continue;
}
const keyPath = propertyPath.get("key");
const key = propertyPath.node.computed ? resolveStatic(keyPath, seenBindings) : keyPath.isIdentifier() ? keyPath.node.name : resolveStatic(keyPath, seenBindings);
if (key === UNRESOLVED || String(key) !== String(propertyValue)) {
continue;
}
const resolved = resolveStatic(propertyPath.get("value"), seenBindings);
seenBindings.delete(binding);
return resolved;
}
seenBindings.delete(binding);
return UNRESOLVED;
}
function resolveStatic(path, seenBindings = new Set()) {
if (!path || !path.node) {
return UNRESOLVED;
}
if (path.isStringLiteral() || path.isNumericLiteral() || path.isBooleanLiteral()) {
return path.node.value;
}
if (path.isNullLiteral()) {
return null;
}
if (path.isTemplateLiteral() && path.node.expressions.length === 0) {
return path.node.quasis[0].value.cooked;
}
if (path.isIdentifier({ name: "undefined" })) {
return undefined;
}
if (path.isArrayExpression()) {
const values = [];
for (const elementPath of path.get("elements")) {
if (!elementPath.node) {
values.push(undefined);
continue;
}
const value = resolveStatic(elementPath, seenBindings);
if (value === UNRESOLVED) {
return UNRESOLVED;
}
values.push(value);
}
return values;
}
if (path.isObjectExpression()) {
const out = Object.create(null);
for (const propPath of path.get("properties")) {
if (!propPath.isObjectProperty()) {
return UNRESOLVED;
}
const keyPath = propPath.get("key");
const key = propPath.node.computed ? resolveStatic(keyPath, seenBindings) : keyPath.isIdentifier() ? keyPath.node.name : resolveStatic(keyPath, seenBindings);
if (key === UNRESOLVED) {
return UNRESOLVED;
}
const value = resolveStatic(propPath.get("value"), seenBindings);
if (value === UNRESOLVED) {
return UNRESOLVED;
}
out[key] = value;
}
return out;
}
if (path.isIdentifier()) {
const binding = path.scope.getBinding(path.node.name);
if (!binding || seenBindings.has(binding)) {
return UNRESOLVED;
}
seenBindings.add(binding);
let resolved = UNRESOLVED;
if (binding.path.parentPath && binding.path.parentPath.isVariableDeclarator()) {
resolved = resolveStatic(binding.path.parentPath.get("init"), seenBindings);
} else if (binding.kind === "param" && binding.path.parentPath && binding.path.parentPath.isFunction()) {
const callPath = getResolvedFunctionCallPath(binding.path.parentPath);
if (callPath && typeof binding.path.key === "number") {
resolved = resolveStatic(callPath.get(`arguments.${binding.path.key}`), seenBindings);
}
}
seenBindings.delete(binding);
if (resolved !== UNRESOLVED) {
return resolved;
}
}
if (path.isMemberExpression()) {
const propertyValue = path.node.computed
? resolveStatic(path.get("property"), seenBindings)
: path.get("property").isIdentifier()
? path.node.property.name
: resolveStatic(path.get("property"), seenBindings);
if (propertyValue === UNRESOLVED) {
return UNRESOLVED;
}
const directPropertyValue = resolveObjectPropertyFromBinding(path.get("object"), propertyValue, seenBindings);
if (directPropertyValue !== UNRESOLVED) {
return directPropertyValue;
}
const objectValue = resolveStatic(path.get("object"), seenBindings);
if (objectValue === UNRESOLVED) {
return UNRESOLVED;
}
if (Array.isArray(objectValue)) {
const index = Number(propertyValue);
return Number.isInteger(index) ? objectValue[index] : UNRESOLVED;
}
if (objectValue && typeof objectValue === "object" && propertyValue in objectValue) {
return objectValue[propertyValue];
}
return UNRESOLVED;
}
if (
path.isCallExpression() &&
path.get("callee").isMemberExpression() &&
(
path.get("callee.property").isIdentifier({ name: "split" }) ||
path.get("callee.property").isStringLiteral({ value: "split" })
) &&
path.get("arguments.0").isStringLiteral()
) {
const source = resolveStatic(path.get("callee.object"), seenBindings);
if (typeof source === "string") {
return source.split(path.node.arguments[0].value);
}
}
const evaluated = path.evaluate();
return evaluated.confident ? evaluated.value : UNRESOLVED;
}
function getLiteralValue(path) {
const resolved = resolveStatic(path);
if (typeof resolved === "string" || typeof resolved === "number") {
return resolved;
}
return null;
}
function normalizeKey(node) {
return generator(node, {
compact: true,
comments: false,
jsescOption: { minimal: true }
}).code;
}
function getCaseKey(casePath) {
const value = getLiteralValue(casePath.get("test"));
return value === null ? normalizeKey(casePath.node.test) : value;
}
function getOrderFromInit(initPath) {
if (initPath.isArrayExpression()) {
const values = [];
for (const elementPath of initPath.get("elements")) {
if (!elementPath.node) {
return null;
}
const value = getLiteralValue(elementPath);
values.push(value === null ? normalizeKey(elementPath.node) : value);
}
return values;
}
if (
initPath.isCallExpression() &&
initPath.get("callee").isMemberExpression() &&
(
initPath.get("callee.property").isIdentifier({ name: "split" }) ||
initPath.get("callee.property").isStringLiteral({ value: "split" })
) &&
initPath.get("arguments.0").isStringLiteral()
) {
const source = getLiteralValue(initPath.get("callee.object"));
if (typeof source === "string") {
return source.split(initPath.node.arguments[0].value);
}
}
return null;
}
function getOrderFromResolvedValue(value) {
if (!Array.isArray(value) || value.length === 0) {
return null;
}
const order = [];
for (const item of value) {
if (typeof item === "string" || typeof item === "number") {
order.push(item);
continue;
}
return null;
}
return order;
}
function getDeclaratorForBinding(binding) {
if (!binding || !binding.path) {
return null;
}
if (binding.path.isVariableDeclarator()) {
return binding.path;
}
if (binding.path.parentPath && binding.path.parentPath.isVariableDeclarator()) {
return binding.path.parentPath;
}
return null;
}
function getOrderFromBinding(binding) {
const declaratorPath = getDeclaratorForBinding(binding);
if (!declaratorPath) {
return null;
}
const initPath = declaratorPath.get("init");
if (!initPath.node) {
return null;
}
return getOrderFromInit(initPath) || getOrderFromResolvedValue(resolveStatic(initPath));
}
function getOrderFromSplitCallWithObjectTable(initPath, loopPath) {
if (
!initPath.isCallExpression() ||
!initPath.get("callee").isMemberExpression() ||
!(
initPath.get("callee.property").isIdentifier({ name: "split" }) ||
initPath.get("callee.property").isStringLiteral({ value: "split" })
) ||
!initPath.get("arguments.0").isStringLiteral()
) {
return null;
}
const sourcePath = initPath.get("callee.object");
if (!sourcePath.isMemberExpression() || !sourcePath.get("object").isIdentifier()) {
return null;
}
const objectName = sourcePath.get("object").node.name;
const propertyKey = sourcePath.node.computed ? getStaticPropertyKey(sourcePath.node.property) : getStaticPropertyKey(sourcePath.node.property);
if (propertyKey === null) {
return null;
}
const match = collectObjectPipeStringCandidates(loopPath, objectName).find((candidate) => candidate.key === propertyKey);
return match ? match.order : null;
}
function getOrderFromDiscriminant(discriminantPath) {
if (!discriminantPath.isMemberExpression()) {
return null;
}
const objectPath = discriminantPath.get("object");
const directOrder = getOrderFromResolvedValue(resolveStatic(objectPath));
if (directOrder) {
return directOrder;
}
if (!objectPath.isIdentifier()) {
return null;
}
const binding = objectPath.scope.getBinding(objectPath.node.name);
return getOrderFromBinding(binding);
}
function findNearbyOrderDeclaration(loopPath, maxSiblings = 3) {
let siblingPath = loopPath.getPrevSibling();
for (let seen = 0; siblingPath && siblingPath.node && seen < maxSiblings; seen += 1) {
if (!siblingPath.isVariableDeclaration()) {
break;
}
for (const declaratorPath of siblingPath.get("declarations")) {
if (!declaratorPath.node || !declaratorPath.get("init").node) {
continue;
}
const order =
getOrderFromInit(declaratorPath.get("init")) ||
getOrderFromSplitCallWithObjectTable(declaratorPath.get("init"), loopPath) ||
getOrderFromResolvedValue(resolveStatic(declaratorPath.get("init")));
if (order) {
return {
order,
declarationPath: siblingPath
};
}
}
siblingPath = siblingPath.getPrevSibling();
}
return null;
}
function buildBlocksFromSwitchOrder(switchStmt, order) {
if (!order) {
return null;
}
const caseMap = new Map();
switchStmt.get("cases").forEach(casePath => {
if (!casePath.node.test) {
return;
}
caseMap.set(getCaseKey(casePath), getCaseStatements(casePath));
});
const blocks = [];
order.forEach(key => {
const stmts = caseMap.get(key);
if (stmts) {
blocks.push(...stmts);
}
});
return blocks.length > 0 ? blocks : null;
}
function getStaticPropertyKey(node) {
if (t.isIdentifier(node)) {
return node.name;
}
if (t.isStringLiteral(node) || t.isNumericLiteral(node)) {
return String(node.value);
}
return null;
}
function resolveObjectAliasNames(loopPath, objectName) {
const names = new Set();
let currentName = objectName;
while (currentName) {
if (names.has(currentName)) {
break;
}
names.add(currentName);
const binding = loopPath.scope.getBinding(currentName);
const declaratorPath = getDeclaratorForBinding(binding);
if (!declaratorPath || !declaratorPath.get("init").isIdentifier()) {
break;
}
currentName = declaratorPath.get("init").node.name;
}
return names;
}
function splitPipeString(value) {
if (typeof value !== "string" || !value.includes("|")) {
return null;
}
const parts = value.split("|");
return parts.length > 1 ? parts : null;
}
function getPipeOrderFromValuePath(valuePath) {
if (!valuePath || !valuePath.node) {
return null;
}
if (valuePath.isStringLiteral()) {
return splitPipeString(valuePath.node.value);
}
const resolved = resolveStatic(valuePath);
return splitPipeString(resolved);
}
function getCaseKeys(switchStmt) {
const keys = [];
switchStmt.get("cases").forEach(casePath => {
if (!casePath.node.test) {
return;
}
keys.push(getCaseKey(casePath));
});
return keys;
}
function sameSequenceKeySet(order, caseKeys) {
if (!order || order.length !== caseKeys.length) {
return false;
}
const counts = new Map();
caseKeys.forEach((key) => counts.set(String(key), (counts.get(String(key)) || 0) + 1));
for (const item of order) {
const key = String(item);
const count = counts.get(key);
if (!count) {
return false;
}
if (count === 1) {
counts.delete(key);
} else {
counts.set(key, count - 1);
}
}
return counts.size === 0;
}
function getAncestorBodyStatements(loopPath) {
const bodies = [];
const seen = new Set();
let current = loopPath;
while (current) {
if (current.isProgram()) {
const key = `program:${current.node.start || 0}`;
if (!seen.has(key)) {
bodies.push(current.get("body"));
seen.add(key);
}
} else if (current.isBlockStatement() && Array.isArray(current.node.body)) {
const key = `block:${current.node.start || 0}`;
if (!seen.has(key)) {
bodies.push(current.get("body"));
seen.add(key);
}
}
current = current.parentPath;
}
return bodies;
}
function collectObjectPipeStringCandidates(loopPath, objectName) {
if (!objectName) {
return [];
}
const objectNames = resolveObjectAliasNames(loopPath, objectName);
const candidates = [];
const loopStart = loopPath.node.start || 0;
getAncestorBodyStatements(loopPath).forEach((statements) => {
statements.forEach((stmtPath) => {
if (!stmtPath.node || (stmtPath.node.start || 0) >= loopStart) {
return;
}
if (stmtPath.isVariableDeclaration()) {
stmtPath.get("declarations").forEach((declPath) => {
if (!declPath.get("id").isIdentifier() || !objectNames.has(declPath.get("id").node.name) || !declPath.get("init").isObjectExpression()) {
return;
}
declPath.get("init.properties").forEach((propPath) => {
if (!propPath.isObjectProperty()) {
return;
}
const keyPath = propPath.get("key");
const key = propPath.node.computed ? getStaticPropertyKey(keyPath.node) : getStaticPropertyKey(keyPath.node);
const valuePath = propPath.get("value");
const pipeOrder = getPipeOrderFromValuePath(valuePath);
if (pipeOrder) {
candidates.push({ key, order: pipeOrder, start: stmtPath.node.start || 0 });
}
});
});
return;
}
if (!stmtPath.isExpressionStatement()) {
return;
}
const exprPath = stmtPath.get("expression");
if (!exprPath.isAssignmentExpression({ operator: "=" })) {
return;
}
const leftPath = exprPath.get("left");
const rightPath = exprPath.get("right");
if (!leftPath.isMemberExpression() || !leftPath.get("object").isIdentifier() || !objectNames.has(leftPath.get("object").node.name)) {
return;
}
const key = leftPath.node.computed ? getStaticPropertyKey(leftPath.node.property) : getStaticPropertyKey(leftPath.node.property);
if (key === null) {
return;
}
const pipeOrder = getPipeOrderFromValuePath(rightPath);
if (pipeOrder) {
candidates.push({ key, order: pipeOrder, start: stmtPath.node.start || 0 });
}
});
});
candidates.sort((a, b) => b.start - a.start);
return candidates;
}
function getOrderFromObjectStringTable(loopPath, switchStmt) {
const discriminant = switchStmt.get("discriminant");
if (!discriminant.isMemberExpression() || !discriminant.get("object").isIdentifier()) {
return null;
}
const candidates = collectObjectPipeStringCandidates(loopPath, discriminant.get("object").node.name);
if (candidates.length === 0) {
return null;
}
const caseKeys = getCaseKeys(switchStmt);
const match = candidates.find((candidate) => sameSequenceKeySet(candidate.order, caseKeys));
return match ? match.order : null;
}
function flatten(ast) {
let changed = false;
traverse(ast, {
ForStatement(path) {
const body = path.get("body");
if (!body.isBlockStatement() || body.get("body").length === 0) {
return;
}
const firstStmt = body.get("body.0");
if (firstStmt.isSwitchStatement() && path.get("init").isVariableDeclaration() && path.node.update === null) {
let order = null;
const leftovers = [];
path.get("init.declarations").forEach(decl => {
if (decl.node.init === null) {
leftovers.push(t.variableDeclaration("var", [clone(decl.node)]));
return;
}
const candidate =
getOrderFromInit(decl.get("init")) ||
getOrderFromSplitCallWithObjectTable(decl.get("init"), path) ||
getOrderFromResolvedValue(resolveStatic(decl.get("init")));
if (candidate) {
order = candidate;
}
});
if (order) {
const caseMap = new Map();
firstStmt.get("cases").forEach(casePath => {
if (!casePath.node.test) {
return;
}
caseMap.set(getCaseKey(casePath), getCaseStatements(casePath));
});
const blocks = [...leftovers];
order.forEach(key => {
const stmts = caseMap.get(key);
if (stmts) {
blocks.push(...stmts);
}
});
if (blocks.length > 0) {
path.replaceWithMultiple(blocks);
changed = true;
return;
}
}
}
if (replaceLoopSwitchWithIf(path)) {
changed = true;
}
},
WhileStatement(path) {
if (path.get("test").isUnaryExpression() && path.get("body").isBlockStatement()) {
const switchStmt = path.get("body.body.0");
if (switchStmt.isSwitchStatement()) {
const nearby = findNearbyOrderDeclaration(path);
const order =
(nearby && nearby.order) ||
getOrderFromDiscriminant(switchStmt.get("discriminant")) ||
getOrderFromObjectStringTable(path, switchStmt);
const blocks = buildBlocksFromSwitchOrder(switchStmt, order);
if (blocks) {
path.replaceWithMultiple(blocks);
if (nearby && nearby.declarationPath && nearby.declarationPath.node) {
nearby.declarationPath.remove();
}
changed = true;
return;
}
}
}
if (replaceLoopSwitchWithIf(path)) {
changed = true;
}
}
});
return { ast, changed };
}
const { inputPath, outputPath } = parseArgs();
let ast = parseFile(inputPath);
let changed = false;
do {
({ ast, changed } = flatten(ast));
if (changed) {
ast = reparse(ast);
}
} while (changed);
saveAst(ast, outputPath);

View File

@@ -0,0 +1,256 @@
const generator = require("@babel/generator").default;
const { clone, parseArgs, parseFile, reparse, saveAst, t, traverse } = require("./shared");
function normalizeCode(node) {
return generator(node, {
compact: true,
comments: false,
jsescOption: { minimal: true }
}).code;
}
function getCaseInfo(testNode) {
if (!t.isBinaryExpression(testNode) || !["==", "==="].includes(testNode.operator)) {
return null;
}
const leftLiteral = t.isLiteral(testNode.left);
const rightLiteral = t.isLiteral(testNode.right);
if (leftLiteral === rightLiteral) {
return null;
}
if (leftLiteral) {
return {
discriminant: testNode.right,
caseValue: testNode.left
};
}
return {
discriminant: testNode.left,
caseValue: testNode.right
};
}
function toCaseStatements(node) {
if (!node) {
return [];
}
if (t.isBlockStatement(node)) {
return node.body.map((stmt) => clone(stmt));
}
return [clone(node)];
}
function unwrapAlternate(path) {
if (!path.node) {
return { kind: "empty", path: null, node: null };
}
if (path.isIfStatement()) {
return { kind: "if", path, node: path.node };
}
if (path.isBlockStatement()) {
const body = path.get("body");
if (body.length === 1 && body[0].isIfStatement()) {
return { kind: "if", path: body[0], node: body[0].node };
}
return { kind: "default", path, node: path.node };
}
return { kind: "default", path, node: path.node };
}
function isTerminalStatement(node) {
return t.isReturnStatement(node) || t.isThrowStatement(node) || t.isBreakStatement(node) || t.isContinueStatement(node);
}
function collectIfChain(path) {
if (path.parentPath.isIfStatement() && path.key === "alternate") {
return null;
}
let currentPath = path;
let discriminantCode = null;
let discriminantNode = null;
const cases = [];
let defaultStatements = null;
while (currentPath && currentPath.isIfStatement()) {
const info = getCaseInfo(currentPath.node.test);
if (!info) {
return null;
}
const currentCode = normalizeCode(info.discriminant);
if (discriminantCode === null) {
discriminantCode = currentCode;
discriminantNode = clone(info.discriminant);
} else if (currentCode !== discriminantCode) {
return null;
}
cases.push({
test: clone(info.caseValue),
statements: toCaseStatements(currentPath.node.consequent)
});
const alternate = unwrapAlternate(currentPath.get("alternate"));
if (alternate.kind === "empty") {
break;
}
if (alternate.kind === "if") {
currentPath = alternate.path;
continue;
}
defaultStatements = toCaseStatements(alternate.node);
break;
}
if (cases.length < 2) {
return null;
}
return {
discriminant: discriminantNode,
cases,
defaultStatements
};
}
function buildSwitchStatementFromChain(chain) {
const switchCases = chain.cases.map((entry) => {
const statements = entry.statements.slice();
if (statements.length === 0 || !isTerminalStatement(statements[statements.length - 1])) {
statements.push(t.breakStatement());
}
return t.switchCase(entry.test, statements);
});
if (chain.defaultStatements) {
switchCases.push(t.switchCase(null, chain.defaultStatements));
}
return t.switchStatement(chain.discriminant, switchCases);
}
function collectConsecutiveIfCases(statementPaths, startIndex) {
const firstPath = statementPaths[startIndex];
if (!firstPath || !firstPath.isIfStatement() || firstPath.node.alternate) {
return null;
}
const firstInfo = getCaseInfo(firstPath.node.test);
if (!firstInfo) {
return null;
}
const discriminantCode = normalizeCode(firstInfo.discriminant);
const cases = [{
test: clone(firstInfo.caseValue),
statements: toCaseStatements(firstPath.node.consequent)
}];
let endIndex = startIndex + 1;
while (endIndex < statementPaths.length) {
const nextPath = statementPaths[endIndex];
if (!nextPath.isIfStatement() || nextPath.node.alternate) {
break;
}
const info = getCaseInfo(nextPath.node.test);
if (!info || normalizeCode(info.discriminant) !== discriminantCode) {
break;
}
cases.push({
test: clone(info.caseValue),
statements: toCaseStatements(nextPath.node.consequent)
});
endIndex += 1;
}
if (cases.length < 2) {
return null;
}
return {
startIndex,
endIndex,
switchNode: buildSwitchStatementFromChain({
discriminant: clone(firstInfo.discriminant),
cases,
defaultStatements: null
})
};
}
function rewriteStatementList(statementPaths) {
if (!Array.isArray(statementPaths) || statementPaths.length < 2) {
return null;
}
const nextBody = [];
let changed = false;
for (let index = 0; index < statementPaths.length;) {
const run = collectConsecutiveIfCases(statementPaths, index);
if (!run) {
nextBody.push(statementPaths[index].node);
index += 1;
continue;
}
nextBody.push(run.switchNode);
index = run.endIndex;
changed = true;
}
return changed ? nextBody : null;
}
function rewrite(ast) {
let changed = false;
traverse(ast, {
IfStatement(path) {
const chain = collectIfChain(path);
if (!chain) {
return;
}
path.replaceWith(buildSwitchStatementFromChain(chain));
changed = true;
},
"Program|BlockStatement": {
exit(path) {
const nextBody = rewriteStatementList(path.get("body"));
if (nextBody) {
path.node.body = nextBody;
changed = true;
}
}
},
SwitchCase: {
exit(path) {
const nextConsequent = rewriteStatementList(path.get("consequent"));
if (nextConsequent) {
path.node.consequent = nextConsequent;
changed = true;
}
}
}
});
return { ast, changed };
}
const { inputPath, outputPath } = parseArgs();
let ast = parseFile(inputPath);
let changed = false;
do {
({ ast, changed } = rewrite(ast));
if (changed) {
ast = reparse(ast);
}
} while (changed);
saveAst(ast, outputPath);

View File

@@ -0,0 +1,130 @@
const { clone, parseArgs, parseFile, reparse, saveAst, t, traverse } = require("./shared");
function collectDispatcherValue(node, dispatcherMap) {
if (t.isStringLiteral(node) || t.isNumericLiteral(node) || t.isBooleanLiteral(node)) {
return { type: "literal", value: clone(node) };
}
if (t.isMemberExpression(node) && t.isIdentifier(node.object) && !node.computed && t.isIdentifier(node.property)) {
return { type: "member", objectName: node.object.name, key: node.property.name };
}
if (t.isFunctionExpression(node) && node.body.body.length === 1 && t.isReturnStatement(node.body.body[0])) {
const arg = node.body.body[0].argument;
if (t.isBinaryExpression(arg) && t.isIdentifier(arg.left) && t.isIdentifier(arg.right)) {
return { type: "binary", operator: arg.operator };
}
if (t.isLogicalExpression(arg) && t.isIdentifier(arg.left) && t.isIdentifier(arg.right)) {
return { type: "logical", operator: arg.operator };
}
if (t.isCallExpression(arg) && t.isIdentifier(arg.callee)) {
return { type: "call" };
}
if (t.isCallExpression(arg) && t.isMemberExpression(arg.callee) && t.isIdentifier(arg.callee.object) && !arg.callee.computed && t.isIdentifier(arg.callee.property)) {
const target = dispatcherMap[arg.callee.object.name] && dispatcherMap[arg.callee.object.name][arg.callee.property.name];
if (target && (target.type === "binary" || target.type === "logical" || target.type === "call")) {
return target;
}
}
}
return null;
}
function inlineDispatchers(ast) {
let changed = false;
const dispatcherMap = Object.create(null);
const removableEntries = [];
traverse(ast, {
VariableDeclarator(path) {
if (!path.get("id").isIdentifier() || !path.get("init").isObjectExpression()) {
return;
}
const name = path.node.id.name;
const collected = dispatcherMap[name] || Object.create(null);
let localChanged = false;
let supportedPropertyCount = 0;
let collectedPropertyCount = 0;
path.get("init.properties").forEach(propertyPath => {
if (!propertyPath.isObjectProperty()) {
return;
}
supportedPropertyCount += 1;
const keyNode = propertyPath.node.key;
const key = t.isIdentifier(keyNode) ? keyNode.name : keyNode.value;
const value = collectDispatcherValue(propertyPath.node.value, dispatcherMap);
if (value) {
collected[key] = value;
localChanged = true;
collectedPropertyCount += 1;
}
});
if (localChanged) {
dispatcherMap[name] = collected;
removableEntries.push({
path,
removable: supportedPropertyCount > 0 && supportedPropertyCount === collectedPropertyCount
});
}
}
});
traverse(ast, {
CallExpression(path) {
if (!path.get("callee").isMemberExpression()) {
return;
}
const objectName = path.node.callee.object.name;
const key = path.node.callee.computed ? path.node.callee.property.value : path.node.callee.property.name;
const entry = dispatcherMap[objectName] && dispatcherMap[objectName][key];
if (!entry) {
return;
}
const args = path.node.arguments.map(arg => clone(arg));
if (entry.type === "binary" && args.length >= 2) {
path.replaceWith(t.binaryExpression(entry.operator, args[0], args[1]));
changed = true;
} else if (entry.type === "logical" && args.length >= 2) {
path.replaceWith(t.logicalExpression(entry.operator, args[0], args[1]));
changed = true;
} else if (entry.type === "call" && args.length >= 1) {
path.replaceWith(t.callExpression(args[0], args.slice(1)));
changed = true;
}
},
MemberExpression(path) {
if (!path.get("object").isIdentifier()) {
return;
}
const objectName = path.node.object.name;
const key = path.node.computed ? path.node.property.value : path.node.property.name;
const entry = dispatcherMap[objectName] && dispatcherMap[objectName][key];
if (!entry) {
return;
}
if (entry.type === "literal") {
path.replaceWith(clone(entry.value));
changed = true;
}
}
});
if (changed) {
removableEntries.forEach(({ path, removable }) => {
if (removable && !path.removed) {
path.remove();
}
});
}
return { ast, changed };
}
const { inputPath, outputPath } = parseArgs();
let ast = parseFile(inputPath);
let changed = false;
do {
({ ast, changed } = inlineDispatchers(ast));
if (changed) {
ast = reparse(ast);
}
} while (changed);
saveAst(ast, outputPath);

View File

@@ -0,0 +1,190 @@
const { clone, isPrimitiveNode, parseArgs, parseFile, reparse, saveAst, t, traverse } = require("./shared");
function getReplacementTarget(path) {
let current = path;
while (current.parentPath.isMemberExpression() && current.key === "object") {
current = current.parentPath;
}
return current;
}
function isUnsafeReplacementTarget(path) {
const target = getReplacementTarget(path);
if (target.parentPath.isUpdateExpression() && target.key === "argument") {
return true;
}
if ((target.parentPath.isAssignmentExpression() || target.parentPath.isAssignmentPattern()) && target.key === "left") {
return true;
}
if ((target.parentPath.isForInStatement() || target.parentPath.isForOfStatement()) && target.key === "left") {
return true;
}
if (target.parentPath.isUnaryExpression({ operator: "delete" }) && target.key === "argument") {
return true;
}
if (target.parentPath.isObjectProperty() && target.key === "key" && !target.parent.computed) {
return true;
}
return false;
}
function replaceSafely(path, node) {
if (isUnsafeReplacementTarget(path)) {
return false;
}
path.replaceWith(node);
return true;
}
function allRefsAfter(binding, position) {
return binding.referencePaths.every(refPath => typeof refPath.node.start !== "number" || refPath.node.start > position);
}
function inlineLiterals(ast) {
let changed = false;
traverse(ast, {
MemberExpression(path) {
if (t.isArrayExpression(path.node.object) && path.node.object.elements.length === 0 && t.isArrayExpression(path.node.property) && path.node.property.elements.length === 0) {
if (replaceSafely(path, t.identifier("undefined"))) {
changed = true;
}
}
},
VariableDeclarator(path) {
if (!path.get("id").isIdentifier()) {
return;
}
const name = path.node.id.name;
const init = path.node.init;
const binding = path.scope.getBinding(name);
if (!binding) {
return;
}
if (t.isStringLiteral(init) || t.isNumericLiteral(init) || t.isBooleanLiteral(init)) {
if (!binding.constant) {
return;
}
let replacedAll = binding.referencePaths.length > 0;
let replacedAny = false;
binding.referencePaths.forEach(refPath => {
if (!replaceSafely(refPath, clone(init))) {
replacedAll = false;
return;
}
replacedAny = true;
});
if (replacedAny) {
changed = true;
}
if (replacedAll) {
path.remove();
}
return;
}
if (t.isSequenceExpression(init) && init.expressions.length > 0 && init.expressions.every(expr => t.isStringLiteral(expr))) {
path.get("init").replaceWith(clone(init.expressions[init.expressions.length - 1]));
changed = true;
return;
}
if (t.isArrayExpression(init) && init.elements.length > 0 && init.elements.every(el => el && isPrimitiveNode(el))) {
let replacedAll = binding.referencePaths.length > 0;
let replacedCount = 0;
binding.referencePaths.forEach(refPath => {
const parent = refPath.parentPath;
if (!parent.isMemberExpression({ object: refPath.node }) || !parent.get("property").isNumericLiteral()) {
replacedAll = false;
return;
}
const index = parent.node.property.value;
if (!init.elements[index]) {
replacedAll = false;
return;
}
if (!replaceSafely(parent, clone(init.elements[index]))) {
replacedAll = false;
return;
}
replacedCount += 1;
changed = true;
});
if (replacedAll && replacedCount > 0) {
path.remove();
}
}
},
AssignmentExpression(path) {
if (!path.get("left").isIdentifier() || path.node.operator !== "=") {
return;
}
if (!path.parentPath.isExpressionStatement()) {
return;
}
const binding = path.scope.getBinding(path.node.left.name);
if (!binding) {
return;
}
if (typeof path.node.start === "number" && !allRefsAfter(binding, path.node.start)) {
return;
}
if (path.get("right").isStringLiteral() || path.get("right").isNumericLiteral() || path.get("right").isBooleanLiteral()) {
const right = clone(path.node.right);
let replacedAll = binding.referencePaths.length > 0;
let replacedAny = false;
binding.referencePaths.forEach(refPath => {
if (!replaceSafely(refPath, clone(right))) {
replacedAll = false;
return;
}
replacedAny = true;
});
if (replacedAny) {
changed = true;
}
if (replacedAll) {
path.remove();
}
return;
}
if (path.get("right").isArrayExpression() && path.node.right.elements.every(el => el && isPrimitiveNode(el))) {
const elements = path.node.right.elements;
let replacedAll = binding.referencePaths.length > 0;
let replacedCount = 0;
binding.referencePaths.forEach(refPath => {
const parent = refPath.parentPath;
if (!parent.isMemberExpression({ object: refPath.node }) || !parent.get("property").isNumericLiteral()) {
replacedAll = false;
return;
}
const index = parent.node.property.value;
if (!elements[index]) {
replacedAll = false;
return;
}
if (!replaceSafely(parent, clone(elements[index]))) {
replacedAll = false;
return;
}
replacedCount += 1;
changed = true;
});
if (replacedAll && replacedCount > 0) {
path.remove();
}
}
}
});
return { ast, changed };
}
const { inputPath, outputPath } = parseArgs();
let ast = parseFile(inputPath);
let changed = false;
do {
({ ast, changed } = inlineLiterals(ast));
if (changed) {
ast = reparse(ast);
}
} while (changed);
saveAst(ast, outputPath);

View File

@@ -0,0 +1,211 @@
const { parseArgs, parseFile, reparse, saveAst, t, traverse } = require("./shared");
function toStatements(expressions) {
return expressions.map(expr => t.expressionStatement(expr));
}
function hasUnsafeContinue(bodyPath) {
let found = false;
bodyPath.traverse({
Function(path) {
path.skip();
},
ContinueStatement(path) {
found = true;
path.stop();
}
});
return found;
}
function ensureBlock(path) {
if (path.isBlockStatement()) {
return path;
}
path.replaceWith(t.blockStatement([path.node]));
return path.parentPath.get(path.key);
}
function replaceStatement(path, node) {
if (path.inList) {
path.replaceWith(node);
return;
}
path.replaceWith(t.blockStatement([node]));
}
function replaceStatementWithStatements(path, statements) {
if (statements.length === 0) {
path.remove();
return;
}
if (path.inList) {
path.replaceWithMultiple(statements);
return;
}
if (statements.length === 1) {
path.replaceWith(statements[0]);
return;
}
path.replaceWith(t.blockStatement(statements));
}
function normalize(ast) {
let changed = false;
traverse(ast, {
UnaryExpression(path) {
if (path.node.operator === "void" && path.parentPath.isExpressionStatement()) {
path.replaceWith(path.node.argument);
changed = true;
}
},
SequenceExpression(path) {
if (path.parentPath.isExpressionStatement()) {
replaceStatementWithStatements(path.parentPath, toStatements(path.node.expressions));
changed = true;
return;
}
if (path.parentPath.isReturnStatement()) {
if (!path.parentPath.inList) {
return;
}
const exprs = path.node.expressions.slice();
const returnExpr = exprs.pop();
path.parentPath.insertBefore(toStatements(exprs));
path.replaceWith(returnExpr);
changed = true;
return;
}
if (path.parentPath.isIfStatement() && path.key === "test") {
if (!path.parentPath.inList) {
return;
}
const exprs = path.node.expressions.slice();
const testExpr = exprs.pop();
path.parentPath.insertBefore(toStatements(exprs));
path.replaceWith(testExpr);
changed = true;
return;
}
if (path.parentPath.isForStatement() && path.key === "init") {
if (!path.parentPath.inList) {
return;
}
path.parentPath.insertBefore(toStatements(path.node.expressions));
path.remove();
changed = true;
return;
}
if (path.parentPath.isForStatement() && path.key === "test") {
const exprs = path.node.expressions.slice();
const testExpr = exprs.pop();
const forPath = path.parentPath;
const bodyPath = ensureBlock(forPath.get("body"));
bodyPath.unshiftContainer(
"body",
t.ifStatement(t.unaryExpression("!", testExpr, true), t.blockStatement([t.breakStatement()]))
);
bodyPath.unshiftContainer("body", toStatements(exprs));
path.remove();
changed = true;
return;
}
if (path.parentPath.isForStatement() && path.key === "update") {
const forPath = path.parentPath;
const bodyPath = ensureBlock(forPath.get("body"));
if (hasUnsafeContinue(bodyPath)) {
return;
}
bodyPath.pushContainer("body", toStatements(path.node.expressions));
path.remove();
changed = true;
}
},
ConditionalExpression(path) {
if (path.parentPath.isExpressionStatement()) {
replaceStatement(
path.parentPath,
t.ifStatement(
path.node.test,
t.blockStatement([t.expressionStatement(path.node.consequent)]),
t.blockStatement([t.expressionStatement(path.node.alternate)])
)
);
changed = true;
}
},
LogicalExpression(path) {
if (!path.parentPath.isExpressionStatement()) {
return;
}
const body = t.blockStatement([t.expressionStatement(path.node.right)]);
if (path.node.operator === "&&") {
replaceStatement(path.parentPath, t.ifStatement(path.node.left, body, null));
} else if (path.node.operator === "||") {
replaceStatement(path.parentPath, t.ifStatement(t.unaryExpression("!", path.node.left, true), body, null));
} else {
return;
}
changed = true;
},
AssignmentExpression(path) {
if (!path.parentPath.isExpressionStatement() || !t.isConditionalExpression(path.node.right)) {
return;
}
const test = path.node.right.test;
const consequent = t.expressionStatement(
t.assignmentExpression(path.node.operator, t.cloneNode(path.node.left, true), path.node.right.consequent)
);
const alternate = t.expressionStatement(
t.assignmentExpression(path.node.operator, t.cloneNode(path.node.left, true), path.node.right.alternate)
);
replaceStatement(path.parentPath, t.ifStatement(test, t.blockStatement([consequent]), t.blockStatement([alternate])));
changed = true;
},
CallExpression(path) {
if (!path.parentPath.isExpressionStatement()) {
return;
}
if (path.get("callee").isFunctionExpression() && path.node.arguments.length === 0) {
replaceStatementWithStatements(path.parentPath, path.node.callee.body.body);
changed = true;
return;
}
if (
path.get("callee").isMemberExpression() &&
path.get("callee.object").isFunctionExpression() &&
path.get("callee.property").isIdentifier({ name: "call" }) &&
path.node.arguments.length === 1 &&
path.get("arguments.0").isThisExpression()
) {
replaceStatementWithStatements(path.parentPath, path.node.callee.object.body.body);
changed = true;
}
},
BlockStatement(path) {
if (path.parentPath.isSwitchCase()) {
path.replaceWithMultiple(path.node.body);
changed = true;
}
}
});
return { ast, changed };
}
const { inputPath, outputPath } = parseArgs();
let ast = parseFile(inputPath);
let changed = false;
do {
({ ast, changed } = normalize(ast));
if (changed) {
ast = reparse(ast);
}
} while (changed);
saveAst(ast, outputPath);

View File

@@ -0,0 +1,81 @@
const { clone } = require("../shared");
const { runPatternPass } = require("./shared-pattern-pass");
const { getTopLevelIifeInfo, traverse, t } = require("./pattern-utils");
function dingxiangArrayPass(ast) {
let changed = false;
traverse(ast, {
Program(programPath) {
const info = getTopLevelIifeInfo(programPath);
if (!info) {
return;
}
const actualArgs = info.argPaths.map((argPath) => argPath.node);
const formalParams = info.calleePath.node.params;
const paramElementMap = Object.create(null);
formalParams.forEach((paramNode, index) => {
if (!t.isIdentifier(paramNode)) {
return;
}
const actualNode = actualArgs[index];
if (t.isArrayExpression(actualNode)) {
paramElementMap[paramNode.name] = actualNode.elements.map((element) => (element ? clone(element) : t.identifier("undefined")));
}
});
if (Object.keys(paramElementMap).length === 0) {
return;
}
info.statementPath.traverse({
UnaryExpression(path) {
const callPath = path.get("argument");
if (
!callPath.isCallExpression() ||
!callPath.get("callee").isFunctionExpression() ||
callPath.node.arguments.length !== 2 ||
!callPath.get("arguments").every((argPath) => argPath.isArrayExpression())
) {
return;
}
callPath.get("callee.params").forEach((paramPath) => {
if (!paramPath.isIdentifier()) {
return;
}
const elements = paramElementMap[paramPath.node.name];
if (!elements) {
return;
}
const binding = paramPath.scope.getBinding(paramPath.node.name);
if (!binding || !binding.constant) {
return;
}
binding.referencePaths.forEach((refPath) => {
const parent = refPath.parentPath;
if (!parent.isMemberExpression({ object: refPath.node }) || !parent.get("property").isNumericLiteral()) {
return;
}
const index = parent.node.property.value;
if (!Number.isInteger(index) || index < 0 || index >= elements.length) {
return;
}
parent.replaceWith(clone(elements[index]));
changed = true;
});
});
path.replaceWithMultiple(callPath.get("callee.body").node.body.map((node) => clone(node)));
changed = true;
}
});
}
});
return { ast, changed };
}
runPatternPass(dingxiangArrayPass);

View File

@@ -0,0 +1,113 @@
const { clone } = require("../shared");
const { runPatternPass } = require("./shared-pattern-pass");
const { createSandbox, evaluateExpression, evaluateNodes, generateCode, traverse, t } = require("./pattern-utils");
function geetest4GuardedPass(ast) {
let changed = false;
const body = ast.program.body;
if (body.length >= 5) {
const sandbox = createSandbox();
const decriptPropName = body[2] && body[2].expression && body[2].expression.left ? generateCode(body[2].expression.left) : "";
const ctrlVarName = body[3] && body[3].expression && body[3].expression.left ? generateCode(body[3].expression.left) : "";
if (decriptPropName && evaluateNodes(body.slice(0, 5), sandbox)) {
traverse(ast, {
VariableDeclaration(path) {
if (
path.node.declarations.length !== 3 ||
!path.get("declarations.0.init").node ||
generateCode(path.get("declarations.0.init").node) !== decriptPropName
) {
return;
}
const next1 = path.getNextSibling();
const next2 = next1 && next1.getNextSibling();
if (!next1 || !next2 || !next1.node || !next2.node) {
return;
}
const candidateNames = [0, 2]
.map((index) => path.get(`declarations.${index}.id`))
.filter((idPath) => idPath && idPath.isIdentifier())
.map((idPath) => idPath.node.name);
candidateNames.forEach((name) => {
const binding = path.scope.getBinding(name);
if (!binding) {
return;
}
binding.referencePaths.forEach((refPath) => {
const parent = refPath.parentPath;
if (!parent.isCallExpression() || parent.node.arguments.length !== 1 || !parent.get("arguments.0").isNumericLiteral()) {
return;
}
const argValue = parent.node.arguments[0].value;
const result = evaluateExpression(`${decriptPropName}(${argValue})`, sandbox);
if (!result.ok) {
return;
}
parent.replaceWith(t.valueToNode(result.value));
changed = true;
});
});
path.remove();
next1.remove();
next2.remove();
changed = true;
},
ForStatement(path) {
if (
path.node.init !== null ||
path.node.update !== null ||
!path.get("test").isBinaryExpression() ||
!generateCode(path.get("test.right").node).includes(ctrlVarName) ||
!path.get("body").isBlockStatement() ||
path.get("body.body").length !== 1 ||
!path.get("body.body.0").isSwitchStatement()
) {
return;
}
const testNamePath = path.get("test.left");
if (!testNamePath.isIdentifier()) {
return;
}
const testName = testNamePath.node.name;
const statements = [];
path.get("body.body.0.cases").forEach((casePath) => {
casePath.get("consequent").forEach((stmtPath) => {
if (stmtPath.isBreakStatement()) {
return;
}
if (
stmtPath.isExpressionStatement() &&
stmtPath.get("expression").isAssignmentExpression() &&
stmtPath.get("expression.left").isIdentifier({ name: testName })
) {
return;
}
statements.push(clone(stmtPath.node));
});
});
if (statements.length > 0) {
path.replaceWithMultiple(statements);
const prev = path.getPrevSibling();
if (prev && prev.node) {
prev.remove();
}
changed = true;
}
}
});
}
}
return { ast, changed };
}
runPatternPass(geetest4GuardedPass);

View File

@@ -0,0 +1,352 @@
const vm = require("vm");
const { clone } = require("../shared");
const { runPatternPass } = require("./shared-pattern-pass");
const { createSandbox, evaluateExpression, evaluateNodes, evaluatePreludeStatements, foldWrapperObjects, generateCode, literalNodeFromValue, replaceCallsFromSandbox, replaceMembersFromSandbox, replacePrimitiveIdentifiers, seedSandboxFromAliases, traverse, t } = require("./pattern-utils");
function inlineAssignmentObjects(ast) {
let changed = false;
traverse(ast, {
VariableDeclaration(path) {
if (path.node.declarations.length !== 1) {
return;
}
const declaration = path.get("declarations.0");
if (!declaration.get("id").isIdentifier() || !declaration.get("init").isObjectExpression({ properties: [] })) {
return;
}
const name = declaration.node.id.name;
const binding = path.scope.getBinding(name);
if (!binding) {
return;
}
const objectEntries = Object.create(null);
binding.referencePaths.forEach((refPath) => {
const memberPath = refPath.parentPath;
const assignmentPath = memberPath && memberPath.parentPath;
if (
!memberPath ||
!memberPath.isMemberExpression({ object: refPath.node }) ||
!assignmentPath ||
!assignmentPath.isAssignmentExpression({ left: memberPath.node, operator: "=" })
) {
return;
}
const key = memberPath.node.computed ? String(memberPath.node.property.value) : memberPath.node.property.name;
objectEntries[key] = clone(assignmentPath.node.right);
assignmentPath.remove();
changed = true;
});
const properties = Object.entries(objectEntries).map(([key, valueNode]) => t.objectProperty(t.stringLiteral(key), valueNode));
if (properties.length > 0) {
declaration.get("init").replaceWith(t.objectExpression(properties));
changed = true;
}
}
});
return changed;
}
function getLeadingPreludeNodes(ast, maxStatements = 12) {
const collected = [];
for (const node of ast.program.body) {
if (collected.length >= maxStatements) {
break;
}
if (t.isFunctionDeclaration(node) || t.isVariableDeclaration(node)) {
collected.push(node);
continue;
}
break;
}
return collected;
}
function findRotationIifeNode(ast, preludeCount) {
const node = ast.program.body[preludeCount];
if (!node || !t.isExpressionStatement(node)) {
return null;
}
const expr = node.expression;
if (t.isCallExpression(expr) && (t.isFunctionExpression(expr.callee) || t.isArrowFunctionExpression(expr.callee))) {
return node;
}
return null;
}
function findPxSetterFunctionName(ast) {
for (const node of ast.program.body) {
if (!t.isFunctionDeclaration(node) || !node.id) {
continue;
}
let matched = false;
traverse(t.file(t.program([clone(node)])), {
AssignmentExpression(path) {
if (path.get("left").isIdentifier({ name: "PX" })) {
matched = true;
path.stop();
}
}
});
if (matched) {
return node.id.name;
}
}
return null;
}
function findDecoderFunctionName(ast) {
for (const node of ast.program.body) {
if (t.isFunctionDeclaration(node) && node.id && node.id.name) {
return node.id.name;
}
}
return null;
}
function collectValidationTargets(ast) {
const targets = [];
const seen = new Set();
traverse(ast, {
CallExpression(path) {
const firstArgPath = path.get("arguments.0");
if (
!path.get("callee").isMemberExpression() ||
!firstArgPath ||
!firstArgPath.node ||
!firstArgPath.isStringLiteral({ value: "|" })
) {
return;
}
const propertyPath = path.get("callee.property");
if (!propertyPath.isCallExpression() || !propertyPath.get("arguments.0").isNumericLiteral()) {
return;
}
const argValue = propertyPath.get("arguments.0").node.value;
const objectPath = path.get("callee.object");
if (!objectPath.isCallExpression() || !objectPath.get("arguments.0").isNumericLiteral()) {
return;
}
const orderArgValue = objectPath.get("arguments.0").node.value;
const token = `split:${argValue}:order:${orderArgValue}`;
if (seen.has(token)) {
return;
}
seen.add(token);
targets.push({
argValue,
expected: "split",
orderArgValue
});
}
});
return targets;
}
function runCodeInSandbox(code, sandbox, timeoutMs) {
try {
vm.runInNewContext(code, sandbox, { timeout: timeoutMs });
return true;
} catch (error) {
return false;
}
}
function buildDynamicDecoderSandbox(preludeCode, rotationCode, pxSetterName, pxValue) {
const sandbox = createSandbox();
if (!runCodeInSandbox(preludeCode, sandbox, 5000)) {
return null;
}
sandbox[pxSetterName] = function patchedPxSetter() {
sandbox.PX = pxValue;
return "nBfa";
};
// OB variants can spend noticeably longer in the bootstrap rotation loop.
if (!runCodeInSandbox(rotationCode, sandbox, 3000)) {
return null;
}
return sandbox;
}
function validateDynamicDecoderSandbox(sandbox, decoderName, targets) {
if (!sandbox || typeof sandbox[decoderName] !== "function" || targets.length === 0) {
return false;
}
try {
return targets.every((target) => {
if (sandbox[decoderName](target.argValue) !== target.expected) {
return false;
}
if (typeof target.orderArgValue !== "number") {
return true;
}
const orderValue = sandbox[decoderName](target.orderArgValue);
return typeof orderValue === "string" && orderValue.includes("|");
});
} catch (error) {
return false;
}
}
function bootstrapDynamicDecoderSandbox(ast) {
const preludeNodes = getLeadingPreludeNodes(ast);
const rotationNode = findRotationIifeNode(ast, preludeNodes.length);
const pxSetterName = findPxSetterFunctionName(ast);
const decoderName = findDecoderFunctionName(ast);
const targets = collectValidationTargets(ast);
const preludeCode = preludeNodes.map((node) => generateCode(node)).join(";\n");
const rotationCode = rotationNode ? generateCode(rotationNode) : "";
if (!rotationNode || !pxSetterName || !decoderName || targets.length === 0) {
return null;
}
const pxCandidates = [];
for (let pxValue = 256; pxValue <= 512; pxValue += 1) {
pxCandidates.push(pxValue);
}
for (let pxValue = 0; pxValue < 256; pxValue += 1) {
pxCandidates.push(pxValue);
}
for (const pxValue of pxCandidates) {
const sandbox = buildDynamicDecoderSandbox(preludeCode, rotationCode, pxSetterName, pxValue);
if (!validateDynamicDecoderSandbox(sandbox, decoderName, targets)) {
continue;
}
return {
sandbox,
decoderName,
pxValue
};
}
return null;
}
function obVariantPass(ast) {
let changed = false;
const body = ast.program.body;
if (body.length >= 3 && body[2].type === "VariableDeclaration" && body[2].declarations.length > 0 && t.isIdentifier(body[2].declarations[0].id)) {
const sandbox = createSandbox();
const decoderName = body[2].declarations[0].id.name;
if (evaluateNodes(body.slice(0, 3), sandbox) && typeof sandbox[decoderName] === "function") {
ast.program.body = body.slice(3).map((node) => clone(node));
changed = true;
traverse(ast, {
CallExpression(path) {
if (!path.get("callee").isIdentifier({ name: decoderName })) {
return;
}
const result = evaluateExpression(path.toString(), sandbox);
if (!result.ok) {
return;
}
path.replaceWith(t.valueToNode(result.value));
changed = true;
},
BinaryExpression: {
exit(path) {
const evaluated = path.evaluate();
if (evaluated.confident) {
path.replaceWith(t.valueToNode(evaluated.value));
changed = true;
}
}
}
});
}
}
const dynamicDecoder = bootstrapDynamicDecoderSandbox(ast);
const prelude = dynamicDecoder ? null : evaluatePreludeStatements(ast, {
sandbox: createSandbox(),
maxStatements: 8,
timeoutMs: 1500
});
const sandbox = dynamicDecoder ? dynamicDecoder.sandbox : prelude.sandbox;
if (dynamicDecoder || prelude.executedCount > 0) {
if (seedSandboxFromAliases(ast, sandbox)) {
changed = true;
}
if (replaceCallsFromSandbox(ast, sandbox, {
timeoutMs: 800,
allowPredicate: (path) => path.node.arguments.every((arg) => t.isLiteral(arg))
})) {
changed = true;
}
if (replaceMembersFromSandbox(ast, sandbox)) {
changed = true;
}
traverse(ast, {
VariableDeclarator(path) {
const initPath = path.get("init");
if (!initPath.node) {
return;
}
const result = evaluateExpression(generateCode(initPath.node), sandbox, 800);
const node = result.ok ? literalNodeFromValue(result.value) : null;
if (!node) {
return;
}
initPath.replaceWith(node);
changed = true;
}
});
if (replacePrimitiveIdentifiers(ast, sandbox)) {
changed = true;
}
if (replaceMembersFromSandbox(ast, sandbox)) {
changed = true;
}
if (replaceCallsFromSandbox(ast, sandbox, {
timeoutMs: 800,
allowPredicate: (path) => path.node.arguments.every((arg) => t.isLiteral(arg))
})) {
changed = true;
}
}
if (inlineAssignmentObjects(ast)) {
changed = true;
}
let localChanged = false;
do {
({ changed: localChanged } = foldWrapperObjects(ast, {
sandbox,
timeoutMs: 800
}));
if (localChanged) {
changed = true;
}
} while (localChanged);
return { ast, changed };
}
runPatternPass(obVariantPass);

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,66 @@
const { runPatternPass } = require("./shared-pattern-pass");
const { inlineStringMethodCalls, traverse, t } = require("./pattern-utils");
function pruneUnusedTopLevel(ast) {
let changed = false;
traverse(ast, {
Program(path) {
path.get("body").forEach((stmtPath) => {
if (stmtPath.isVariableDeclaration()) {
const removable = [];
stmtPath.get("declarations").forEach((declPath) => {
if (!declPath.get("id").isIdentifier()) {
return;
}
const binding = declPath.scope.getBinding(declPath.node.id.name);
if (binding && !binding.referenced) {
removable.push(declPath);
}
});
if (removable.length === stmtPath.node.declarations.length && removable.length > 0) {
stmtPath.remove();
changed = true;
} else if (removable.length > 0) {
removable.forEach((declPath) => declPath.remove());
changed = true;
}
} else if (stmtPath.isFunctionDeclaration() && stmtPath.node.id) {
const binding = stmtPath.scope.getBinding(stmtPath.node.id.name);
if (binding && !binding.referenced) {
stmtPath.remove();
changed = true;
}
}
});
}
});
return changed;
}
function reese84HeavyPass(ast) {
let changed = false;
if (inlineStringMethodCalls(ast)) {
changed = true;
}
traverse(ast, {
BinaryExpression: {
exit(path) {
if (path.get("left").isStringLiteral() && path.get("right").isStringLiteral()) {
const evaluated = path.evaluate();
if (evaluated.confident) {
path.replaceWith(t.valueToNode(evaluated.value));
changed = true;
}
}
}
}
});
if (pruneUnusedTopLevel(ast)) {
changed = true;
}
return { ast, changed };
}
runPatternPass(reese84HeavyPass);

View File

@@ -0,0 +1,17 @@
const { parseArgs, parseFile, reparse, saveAst } = require("../shared");
function runPatternPass(transform) {
const { inputPath, outputPath } = parseArgs();
let ast = parseFile(inputPath);
const result = typeof transform === "function" ? transform(ast) || { ast, changed: false } : { ast, changed: false };
if (result.changed) {
ast = reparse(result.ast || ast);
} else if (result.ast) {
ast = result.ast;
}
saveAst(ast, outputPath);
}
module.exports = {
runPatternPass
};

View File

@@ -0,0 +1,189 @@
const { runPatternPass } = require("./shared-pattern-pass");
const {
createSandbox,
evaluateNodes,
foldWrapperObjects,
inlineStringMethodCalls,
seedSandboxFromAliases,
traverse,
t
} = require("./pattern-utils");
function getDecoderPreludeNodes(ast) {
const topLevelDeclarator = ast.program.body
.filter((node) => t.isVariableDeclaration(node))
.flatMap((node) => node.declarations)
.find((decl) =>
t.isIdentifier(decl.id) &&
(
t.isFunctionExpression(decl.init) ||
(t.isCallExpression(decl.init) && t.isFunctionExpression(decl.init.callee))
)
);
const rootFunction = !topLevelDeclarator ? null :
t.isFunctionExpression(topLevelDeclarator.init) ? topLevelDeclarator.init :
t.isCallExpression(topLevelDeclarator.init) && t.isFunctionExpression(topLevelDeclarator.init.callee) ? topLevelDeclarator.init.callee :
null;
if (!rootFunction || !t.isBlockStatement(rootFunction.body)) {
return [];
}
const nodes = [];
for (const stmt of rootFunction.body.body) {
if (t.isFunctionDeclaration(stmt) && stmt.id && (stmt.id.name === "t" || stmt.id.name === "u")) {
nodes.push(stmt);
continue;
}
if (t.isVariableDeclaration(stmt) && stmt.declarations.every((decl) => t.isIdentifier(decl.id) && decl.init == null)) {
nodes.push(stmt);
continue;
}
if (t.isExpressionStatement(stmt)) {
nodes.push(stmt);
break;
}
}
return nodes;
}
function tonghuashunOrderPass(ast) {
let changed = false;
let localChanged = false;
const sandbox = createSandbox();
const preludeNodes = getDecoderPreludeNodes(ast);
if (preludeNodes.length > 0 && evaluateNodes(preludeNodes, sandbox, 2000)) {
while (seedSandboxFromAliases(ast, sandbox)) {
changed = true;
}
}
const decoderAliasNames = collectDecoderAliasNames(ast, ["t"]);
do {
localChanged = false;
if (replaceDecoderAliasCalls(ast, sandbox, decoderAliasNames)) {
localChanged = true;
changed = true;
}
if (inlineStringMethodCalls(ast)) {
localChanged = true;
changed = true;
}
const foldResult = foldWrapperObjects(ast, {
ignoreDynamicUsage: true
});
if (foldResult.changed) {
localChanged = true;
changed = true;
}
if (inlineStringMethodCalls(ast)) {
localChanged = true;
changed = true;
}
} while (localChanged);
return { ast, changed };
}
function collectDecoderAliasNames(ast, seedNames) {
const aliasNames = new Set(seedNames);
let expanded = false;
do {
expanded = false;
traverse(ast, {
VariableDeclarator(path) {
if (!path.get("id").isIdentifier() || !path.get("init").isIdentifier()) {
return;
}
const sourceName = path.get("init").node.name;
const aliasName = path.get("id").node.name;
if (!aliasNames.has(sourceName) || aliasNames.has(aliasName)) {
return;
}
aliasNames.add(aliasName);
expanded = true;
},
AssignmentExpression(path) {
if (!path.get("left").isIdentifier() || !path.get("right").isIdentifier()) {
return;
}
const sourceName = path.get("right").node.name;
const aliasName = path.get("left").node.name;
if (!aliasNames.has(sourceName) || aliasNames.has(aliasName)) {
return;
}
aliasNames.add(aliasName);
expanded = true;
}
});
} while (expanded);
return aliasNames;
}
function nodeFromPrimitiveValue(value) {
if (typeof value === "undefined") {
return t.identifier("undefined");
}
if (value === null || typeof value === "string" || typeof value === "number" || typeof value === "boolean") {
return t.valueToNode(value);
}
return null;
}
function replaceDecoderAliasCalls(ast, sandbox, aliasNames) {
if (typeof sandbox.t !== "function" || aliasNames.size === 0) {
return false;
}
let changed = false;
traverse(ast, {
CallExpression(path) {
const calleePath = path.get("callee");
let isDecoderAliasCall = false;
if (calleePath.isIdentifier()) {
isDecoderAliasCall = aliasNames.has(calleePath.node.name);
} else if (
calleePath.isAssignmentExpression({ operator: "=" }) &&
calleePath.get("left").isIdentifier() &&
calleePath.get("right").isIdentifier()
) {
isDecoderAliasCall = aliasNames.has(calleePath.get("right").node.name);
}
if (!isDecoderAliasCall) {
return;
}
if (path.node.arguments.length !== 1 || !path.get("arguments.0").isNumericLiteral()) {
return;
}
let value;
try {
value = sandbox.t(path.get("arguments.0").node.value);
} catch (error) {
return;
}
const literalNode = nodeFromPrimitiveValue(value);
if (!literalNode) {
return;
}
path.replaceWith(literalNode);
changed = true;
}
});
return changed;
}
runPatternPass(tonghuashunOrderPass);

View File

@@ -0,0 +1,257 @@
const { clone } = require("../shared");
const { runPatternPass } = require("./shared-pattern-pass");
const { createSandbox, evaluateExpression, evaluateNodes, foldWrapperObjects, traverse, t } = require("./pattern-utils");
function getOpcodeTestInfo(testPath) {
if (!testPath.isBinaryExpression()) {
return null;
}
if (!["===", "=="].includes(testPath.node.operator)) {
return null;
}
if (testPath.get("left").isNumericLiteral() && testPath.get("right").isIdentifier()) {
return {
discriminant: clone(testPath.get("right").node),
discriminantName: testPath.get("right").node.name,
caseValue: clone(testPath.get("left").node)
};
}
if (testPath.get("right").isNumericLiteral() && testPath.get("left").isIdentifier()) {
return {
discriminant: clone(testPath.get("left").node),
discriminantName: testPath.get("left").node.name,
caseValue: clone(testPath.get("right").node)
};
}
return null;
}
function blockify(node) {
if (!node) {
return [];
}
if (t.isBlockStatement(node)) {
return node.body.map((stmt) => clone(stmt));
}
return [clone(node)];
}
function withTrailingBreak(statements) {
if (statements.length === 0) {
return [t.breakStatement()];
}
const last = statements[statements.length - 1];
if (t.isBreakStatement(last) || t.isReturnStatement(last) || t.isThrowStatement(last) || t.isContinueStatement(last)) {
return statements;
}
return [...statements, t.breakStatement()];
}
function collectOpcodeCasesFromAlternate(node, discriminantName, cases) {
if (!node) {
return;
}
if (t.isIfStatement(node)) {
const info = getOpcodeTestInfo({
isBinaryExpression: () => t.isBinaryExpression(node.test),
node: node.test,
get(key) {
if (key === "left") {
return {
isNumericLiteral: () => t.isNumericLiteral(node.test.left),
isIdentifier: () => t.isIdentifier(node.test.left),
node: node.test.left
};
}
return {
isNumericLiteral: () => t.isNumericLiteral(node.test.right),
isIdentifier: () => t.isIdentifier(node.test.right),
node: node.test.right
};
}
});
if (info && info.discriminantName === discriminantName) {
cases.push(t.switchCase(info.caseValue, withTrailingBreak(blockify(node.consequent))));
collectOpcodeCasesFromAlternate(node.alternate, discriminantName, cases);
return;
}
cases.push(t.switchCase(null, withTrailingBreak(blockify(node))));
return;
}
if (t.isBlockStatement(node)) {
node.body.forEach((stmt) => {
if (t.isIfStatement(stmt)) {
const info = getOpcodeTestInfo({
isBinaryExpression: () => t.isBinaryExpression(stmt.test),
node: stmt.test,
get(key) {
if (key === "left") {
return {
isNumericLiteral: () => t.isNumericLiteral(stmt.test.left),
isIdentifier: () => t.isIdentifier(stmt.test.left),
node: stmt.test.left
};
}
return {
isNumericLiteral: () => t.isNumericLiteral(stmt.test.right),
isIdentifier: () => t.isIdentifier(stmt.test.right),
node: stmt.test.right
};
}
});
if (info && info.discriminantName === discriminantName) {
cases.push(t.switchCase(info.caseValue, withTrailingBreak(blockify(stmt.consequent))));
collectOpcodeCasesFromAlternate(stmt.alternate, discriminantName, cases);
return;
}
}
cases.push(t.switchCase(null, withTrailingBreak([clone(stmt)])));
});
return;
}
cases.push(t.switchCase(null, withTrailingBreak([clone(node)])));
}
function convertOpcodeIfChains(ast) {
let changed = false;
traverse(ast, {
IfStatement(path) {
const info = getOpcodeTestInfo(path.get("test"));
if (!info) {
return;
}
const cases = [
t.switchCase(info.caseValue, withTrailingBreak(blockify(path.node.consequent)))
];
collectOpcodeCasesFromAlternate(path.node.alternate, info.discriminantName, cases);
path.replaceWith(t.switchStatement(info.discriminant, cases));
path.skip();
changed = true;
}
});
return changed;
}
function stripPseudoForLoops(ast) {
let changed = false;
traverse(ast, {
ForStatement(path) {
const testPath = path.get("test");
if (
!testPath.isBinaryExpression() ||
path.node.update !== null ||
!(
testPath.get("left").isNumericLiteral() ||
testPath.get("right").isNumericLiteral()
)
) {
return;
}
if (path.node.init) {
path.replaceWith(clone(path.node.init));
} else {
path.remove();
}
changed = true;
}
});
return changed;
}
function xiaohongshuWrapperPass(ast) {
let changed = false;
const body = ast.program.body;
if (
body.length >= 3 &&
body[1].type === "VariableDeclaration" &&
body[1].declarations.length > 0 &&
t.isIdentifier(body[1].declarations[0].id)
) {
const sandbox = createSandbox();
const decoderName = body[1].declarations[0].id.name;
if (evaluateNodes(body.slice(0, 3), sandbox) && typeof sandbox[decoderName] === "function") {
ast.program.body = body.slice(3).map((node) => clone(node));
changed = true;
const aliasNames = new Set([decoderName]);
let progress = false;
do {
progress = false;
traverse(ast, {
VariableDeclarator(path) {
if (!path.get("id").isIdentifier() || !path.get("init").isIdentifier()) {
return;
}
if (aliasNames.has(path.node.init.name) && !aliasNames.has(path.node.id.name)) {
aliasNames.add(path.node.id.name);
progress = true;
}
},
CallExpression(path) {
if (!path.get("callee").isIdentifier() || !aliasNames.has(path.node.callee.name) || path.node.arguments.length !== 1) {
return;
}
const result = evaluateExpression(`${decoderName}(${path.get("arguments.0").toString()})`, sandbox);
if (!result.ok) {
return;
}
path.replaceWith(t.valueToNode(result.value));
progress = true;
changed = true;
}
});
} while (progress);
traverse(ast, {
VariableDeclarator(path) {
if (!path.get("id").isIdentifier() || !path.get("init").isIdentifier()) {
return;
}
if (aliasNames.has(path.node.id.name) && aliasNames.has(path.node.init.name)) {
path.remove();
changed = true;
}
}
});
}
}
let localChanged = false;
do {
({ changed: localChanged } = foldWrapperObjects(ast));
if (localChanged) {
changed = true;
}
} while (localChanged);
if (convertOpcodeIfChains(ast)) {
changed = true;
}
if (stripPseudoForLoops(ast)) {
changed = true;
}
return { ast, changed };
}
runPatternPass(xiaohongshuWrapperPass);

View File

@@ -0,0 +1,202 @@
const { clone } = require("../shared");
const { runPatternPass } = require("./shared-pattern-pass");
const { createSandbox, evaluateExpression, evaluateNodes, generateCode, traverse, t } = require("./pattern-utils");
function unwrapTopLevelIife(ast) {
const first = ast.program.body[0];
if (!first || first.type !== "ExpressionStatement" || first.expression.type !== "CallExpression") {
return false;
}
const callee = first.expression.callee;
if (callee.type !== "FunctionExpression") {
return false;
}
ast.program.body = callee.body.body.map((node) => clone(node));
return true;
}
function expandSequenceExpressions(ast) {
let changed = false;
traverse(ast, {
SequenceExpression(path) {
if (path.parentPath.isExpressionStatement()) {
path.replaceWithMultiple(path.node.expressions.map((expr) => t.expressionStatement(clone(expr))));
changed = true;
return;
}
if (path.parentPath.isReturnStatement()) {
const expressions = path.node.expressions.map((expr) => clone(expr));
const last = expressions.pop();
if (!last) {
return;
}
path.parentPath.replaceWithMultiple([
...expressions.map((expr) => t.expressionStatement(expr)),
t.returnStatement(last)
]);
changed = true;
return;
}
if (path.parentPath.isVariableDeclarator()) {
const declaratorPath = path.parentPath;
const declarationPath = declaratorPath.parentPath;
if (!declarationPath || !declarationPath.isVariableDeclaration()) {
return;
}
const expressions = path.node.expressions.map((expr) => clone(expr));
const last = expressions.pop();
if (!last) {
return;
}
const inserted = [
...expressions.map((expr) => t.expressionStatement(expr)),
t.variableDeclaration(declarationPath.node.kind, [
t.variableDeclarator(clone(declaratorPath.node.id), last)
])
];
declarationPath.insertAfter(inserted);
declaratorPath.remove();
if (declarationPath.get("declarations").length === 0) {
declarationPath.remove();
}
changed = true;
}
}
});
return changed;
}
function unwrapUnaryIifes(ast) {
let changed = false;
traverse(ast, {
UnaryExpression(path) {
if (
path.node.operator !== "!" ||
!path.get("argument").isCallExpression() ||
path.get("argument.arguments").length !== 0 ||
!path.get("argument.callee").isFunctionExpression() ||
path.get("argument.callee.params").length !== 0
) {
return;
}
path.replaceWithMultiple(path.get("argument.callee.body").node.body.map((node) => clone(node)));
changed = true;
}
});
return changed;
}
function yidunDispatcherPass(ast) {
let changed = false;
if (unwrapTopLevelIife(ast)) {
changed = true;
}
if (expandSequenceExpressions(ast)) {
changed = true;
}
if (unwrapUnaryIifes(ast)) {
changed = true;
}
const body = ast.program.body;
if (body.length >= 2 && body[1].type === "FunctionDeclaration" && body[1].id && body[1].params.length === 2) {
const sandbox = createSandbox();
const decoderName = body[1].id.name;
if (evaluateNodes(body.slice(0, 2), sandbox) && typeof sandbox[decoderName] === "function") {
traverse(ast, {
CallExpression(path) {
if (!path.get("callee").isIdentifier({ name: decoderName }) || path.node.arguments.length !== 1 || !path.get("arguments.0").isNumericLiteral()) {
return;
}
const result = evaluateExpression(path.toString(), sandbox);
if (!result.ok) {
return;
}
path.replaceWith(t.valueToNode(result.value));
changed = true;
}
});
ast.program.body = ast.program.body.slice(2).map((node) => clone(node));
changed = true;
}
}
traverse(ast, {
VariableDeclaration(path) {
if (
path.node.declarations.length !== 5 ||
!path.get("declarations").every((declarationPath) => {
return (
declarationPath.get("init").isMemberExpression() &&
declarationPath.get("init.object").isNewExpression() &&
declarationPath.get("init.property").isStringLiteral()
);
})
) {
return;
}
const prev = path.getPrevSibling();
const next1 = path.getNextSibling();
const next2 = next1 && next1.getNextSibling();
if (!prev || !next1 || !next2 || !prev.node || !next1.node || !next2.node) {
return;
}
const sandbox = createSandbox();
if (!evaluateNodes([prev.node, path.node, next1.node, next2.node], sandbox)) {
return;
}
const names = []
.concat(next1.node.declarations || [])
.concat(next2.node.declarations || [])
.map((declaration) => declaration.id)
.filter((idNode) => t.isIdentifier(idNode))
.map((idNode) => idNode.name);
names.forEach((name) => {
const binding = path.scope.getBinding(name);
if (!binding || !binding.constant) {
return;
}
binding.referencePaths.forEach((refPath) => {
const parent = refPath.parentPath;
if (!parent.isMemberExpression({ object: refPath.node }) || !parent.get("property").isNumericLiteral()) {
return;
}
const result = evaluateExpression(generateCode(parent.node), sandbox);
if (!result.ok) {
return;
}
parent.replaceWith(t.valueToNode(result.value));
changed = true;
});
});
prev.remove();
path.remove();
next1.remove();
next2.remove();
changed = true;
}
});
return { ast, changed };
}
runPatternPass(yidunDispatcherPass);

View File

@@ -0,0 +1,304 @@
const path = require("path");
function step(id, script, options = {}) {
return {
id,
script,
timeoutMs: 120000,
...options
};
}
const STEP_LIBRARY = {
normalize: step("normalize", "normalize-structure.js", { timeoutMs: 60000 }),
prune: step("prune", "prune-fake-branches.js", { timeoutMs: 60000 }),
inlineDispatchers: step("inline_dispatchers", "inline-dispatchers.js", { timeoutMs: 120000 }),
flatten: step("flatten", "flatten-array-control-flow.js", { timeoutMs: 180000 }),
ifToSwitch: step("if_to_switch", "if-chain-to-switch.js", { timeoutMs: 90000 }),
inlineLiterals: step("inline_literals", "inline-literals.js", { timeoutMs: 90000 }),
rename: step("rename_identifiers", "rename-identifiers.js", { timeoutMs: 120000 }),
reese84HeavyPass: step("reese84_heavy_pass", "patterns/reese84-heavy-pass.js", { timeoutMs: 30000 }),
dingxiangArrayPass: step("dingxiang_array_pass", "patterns/dingxiang-array-pass.js", { timeoutMs: 30000 }),
geetest4GuardedPass: step("geetest4_guarded_pass", "patterns/geetest4-guarded-pass.js", { timeoutMs: 30000 }),
tonghuashunOrderPass: step("tonghuashun_order_pass", "patterns/tonghuashun-order-pass.js", { timeoutMs: 30000 }),
yidunDispatcherPass: step("yidun_dispatcher_pass", "patterns/yidun-dispatcher-pass.js", { timeoutMs: 30000 }),
xiaohongshuWrapperPass: step("xiaohongshu_wrapper_pass", "patterns/xiaohongshu-wrapper-pass.js", { timeoutMs: 30000 }),
obVariantPass: step("ob_variant_pass", "patterns/ob-variant-pass.js", { timeoutMs: 360000 })
};
const BASE_PIPELINE = [
STEP_LIBRARY.normalize,
STEP_LIBRARY.prune,
STEP_LIBRARY.inlineDispatchers,
STEP_LIBRARY.flatten,
STEP_LIBRARY.ifToSwitch,
STEP_LIBRARY.prune,
STEP_LIBRARY.normalize
];
const PATTERNS = [
{
id: "generic",
displayName: "generic layered pipeline",
reference: null,
hintTokens: [],
contentRegexes: [],
families: ["generic"],
notes: [
"Use only the generic passes.",
"Escalate to a site adapter when residue is stable and reproducible."
],
steps: BASE_PIPELINE
},
{
id: "reese84",
displayName: "reese84",
reference: "references/patterns/reese84.md",
hintTokens: ["reese84"],
contentRegexes: [/reese84/i],
families: ["vm-heavy", "dispatcher-heavy"],
notes: [
"Skip late inline-literals by default because large reese84 samples can stall there.",
"Prefer comparing dispatcher residue and flattened handlers against the reference decode."
],
steps: [
STEP_LIBRARY.normalize,
STEP_LIBRARY.prune,
STEP_LIBRARY.inlineDispatchers,
STEP_LIBRARY.flatten,
STEP_LIBRARY.ifToSwitch,
STEP_LIBRARY.prune,
STEP_LIBRARY.normalize,
STEP_LIBRARY.reese84HeavyPass,
STEP_LIBRARY.rename
]
},
{
id: "dingxiang",
displayName: "dingxiang",
reference: "references/patterns/dingxiang.md",
hintTokens: ["\u9876\u8c61", "dingxiang", "dx"],
contentRegexes: [/dingxiang/i],
families: ["param-array", "dispatcher-heavy"],
notes: [
"Resolve IIFE parameter backed arrays before judging the flattener result.",
"Do not force a second inline-literals pass unless the file stays small."
],
steps: [
STEP_LIBRARY.normalize,
STEP_LIBRARY.prune,
STEP_LIBRARY.inlineDispatchers,
STEP_LIBRARY.dingxiangArrayPass,
STEP_LIBRARY.flatten,
STEP_LIBRARY.ifToSwitch,
STEP_LIBRARY.prune,
STEP_LIBRARY.normalize
]
},
{
id: "geetest4",
displayName: "geetest4",
reference: "references/patterns/geetest4.md",
hintTokens: ["\u6781\u9a8c4", "geetest4", "geetest"],
contentRegexes: [/geetest/i],
families: ["guarded-switch", "vm-heavy"],
notes: [
"Keep the guarded-if adapter outside the generic flattener unless the pattern becomes broadly reusable.",
"Current focus is reducing direct loop-switch residue before touching naming."
],
steps: [
STEP_LIBRARY.normalize,
STEP_LIBRARY.prune,
STEP_LIBRARY.inlineDispatchers,
STEP_LIBRARY.flatten,
STEP_LIBRARY.geetest4GuardedPass,
STEP_LIBRARY.ifToSwitch,
STEP_LIBRARY.prune,
STEP_LIBRARY.normalize
]
},
{
id: "tonghuashun",
displayName: "tonghuashun",
reference: "references/patterns/tonghuashun.md",
hintTokens: ["\u540c\u82b1\u987a", "tonghuashun", "10jqka"],
contentRegexes: [/10jqka/i, /split\(\s*["']\|["']\s*\)/],
families: ["split-order-source", "dispatcher-wrapper"],
notes: [
"Add order-source adapters around nested wrapper objects instead of bloating the generic flattener.",
"This pipeline intentionally skips inline-literals because it is not the main blocker here."
],
steps: [
STEP_LIBRARY.normalize,
STEP_LIBRARY.prune,
STEP_LIBRARY.inlineDispatchers,
STEP_LIBRARY.tonghuashunOrderPass,
STEP_LIBRARY.flatten,
STEP_LIBRARY.ifToSwitch,
STEP_LIBRARY.prune,
STEP_LIBRARY.normalize
]
},
{
id: "yidun",
displayName: "yidun",
reference: "references/patterns/yidun.md",
hintTokens: ["\u7f51\u6613\u6613\u76fe", "yidun", "163"],
contentRegexes: [/yidun/i],
families: ["dispatcher-heavy"],
notes: [
"One inline-literals pass can still help here, but keep it early enough to avoid blowups.",
"Compare remaining dispatcher wrappers against the reference decode."
],
steps: [
STEP_LIBRARY.normalize,
STEP_LIBRARY.prune,
STEP_LIBRARY.inlineDispatchers,
STEP_LIBRARY.yidunDispatcherPass,
STEP_LIBRARY.flatten,
STEP_LIBRARY.inlineLiterals,
STEP_LIBRARY.ifToSwitch,
STEP_LIBRARY.prune,
STEP_LIBRARY.normalize,
STEP_LIBRARY.rename
]
},
{
id: "xiaohongshu",
displayName: "xiaohongshu",
reference: "references/patterns/xiaohongshu.md",
hintTokens: ["\u5c0f\u7ea2\u4e66", "xiaohongshu", "xhs"],
contentRegexes: [/xiaohongshu/i, /\bxhs\b/i],
families: ["wrapper-light"],
notes: [
"Treat this as a lighter wrapper cleanup path.",
"Prefer readability and structural cleanup over aggressive evaluation."
],
steps: [
STEP_LIBRARY.normalize,
STEP_LIBRARY.prune,
STEP_LIBRARY.xiaohongshuWrapperPass,
STEP_LIBRARY.inlineDispatchers,
STEP_LIBRARY.flatten,
STEP_LIBRARY.xiaohongshuWrapperPass,
STEP_LIBRARY.ifToSwitch,
STEP_LIBRARY.prune,
STEP_LIBRARY.normalize,
STEP_LIBRARY.rename
]
},
{
id: "cn-bidding-ob",
displayName: "cn-bidding-ob",
reference: "references/patterns/cn-bidding-ob.md",
hintTokens: ["\u4e2d\u56fd\u62db\u6807\u6295\u6807\u516c\u5171\u670d\u52a1\u5e73\u53f0", "\u62db\u6807\u6295\u6807", "ob"],
contentRegexes: [/\b_0x[a-f0-9]+\b/i],
families: ["ob-variant"],
notes: [
"Keep OB-family tweaks in a dedicated adapter so the common pipeline stays conservative.",
"Track remaining loop-switch residue after the OB adapter stage."
],
steps: [
STEP_LIBRARY.normalize,
STEP_LIBRARY.prune,
STEP_LIBRARY.inlineDispatchers,
STEP_LIBRARY.obVariantPass,
STEP_LIBRARY.flatten,
STEP_LIBRARY.ifToSwitch,
STEP_LIBRARY.prune,
STEP_LIBRARY.obVariantPass,
STEP_LIBRARY.normalize
]
},
{
id: "mps-ob",
displayName: "mps-ob",
reference: "references/patterns/mps-ob.md",
hintTokens: ["\u4e2d\u534e\u4eba\u6c11\u5171\u548c\u56fd\u516c\u5b89\u90e8", "\u516c\u5b89\u90e8", "mps", "ob"],
contentRegexes: [/\b_0x[a-f0-9]+\b/i],
families: ["ob-variant"],
notes: [
"Share the OB-family adapter but keep a separate rule document because the residue profile is different from the bidding sample.",
"Compare remaining loop-switch blocks directly against the reference decode."
],
steps: [
STEP_LIBRARY.normalize,
STEP_LIBRARY.prune,
STEP_LIBRARY.inlineDispatchers,
STEP_LIBRARY.obVariantPass,
STEP_LIBRARY.flatten,
STEP_LIBRARY.ifToSwitch,
STEP_LIBRARY.prune,
STEP_LIBRARY.obVariantPass,
STEP_LIBRARY.normalize,
STEP_LIBRARY.rename
]
}
];
function normalizeHint(value) {
return String(value || "").toLowerCase();
}
function collectDetection(pattern, inputPath, hint, sourceText) {
let score = 0;
const reasons = [];
const haystacks = [normalizeHint(inputPath), normalizeHint(hint)];
for (const token of pattern.hintTokens) {
const normalized = normalizeHint(token);
if (!normalized) {
continue;
}
if (haystacks.some((value) => value.includes(normalized))) {
score += 10;
reasons.push(`hint:${token}`);
}
}
for (const regex of pattern.contentRegexes) {
if (regex.test(sourceText)) {
score += 3;
reasons.push(`content:${regex}`);
}
}
return {
id: pattern.id,
score,
reasons
};
}
function detectPatterns({ inputPath, hint = "", sourceText }) {
const detections = PATTERNS
.filter((pattern) => pattern.id !== "generic")
.map((pattern) => collectDetection(pattern, inputPath, hint, sourceText))
.filter((entry) => entry.score > 0)
.sort((left, right) => right.score - left.score || left.id.localeCompare(right.id));
const bestId = detections.length > 0 ? detections[0].id : "generic";
return {
bestId,
detections,
selected: getPattern(bestId)
};
}
function getPattern(id) {
return PATTERNS.find((pattern) => pattern.id === id) || PATTERNS[0];
}
function buildPipeline(patternId) {
const pattern = getPattern(patternId);
return pattern.steps.map((entry) => ({
...entry,
scriptPath: path.join(__dirname, entry.script)
}));
}
module.exports = {
BASE_PIPELINE,
PATTERNS,
STEP_LIBRARY,
buildPipeline,
detectPatterns,
getPattern
};

View File

@@ -0,0 +1,97 @@
const { parseArgs, parseFile, reparse, saveAst, t, traverse } = require("./shared");
function isLiteralLike(node) {
return (
t.isStringLiteral(node) ||
t.isNumericLiteral(node) ||
t.isBooleanLiteral(node) ||
t.isNullLiteral(node) ||
t.isIdentifier(node, { name: "undefined" })
);
}
function evaluateBinary(node) {
if (!t.isBinaryExpression(node) || !["==", "===", "!=", "!=="].includes(node.operator)) {
return null;
}
if (!isLiteralLike(node.left) || !isLiteralLike(node.right)) {
return null;
}
const left = t.isIdentifier(node.left, { name: "undefined" }) ? undefined : node.left.value;
const right = t.isIdentifier(node.right, { name: "undefined" }) ? undefined : node.right.value;
switch (node.operator) {
case "==":
return left == right;
case "===":
return left === right;
case "!=":
return left != right;
case "!==":
return left !== right;
default:
return null;
}
}
function replaceStatement(path, node) {
if (!node) {
path.remove();
return;
}
if (t.isBlockStatement(node)) {
path.replaceWithMultiple(node.body);
return;
}
path.replaceWith(node);
}
function simplify(ast) {
let changed = false;
traverse(ast, {
IfStatement: {
exit(path) {
let decision = evaluateBinary(path.node.test);
if (decision === null) {
const evaluated = path.get("test").evaluate();
if (evaluated.confident && typeof evaluated.value === "boolean") {
decision = evaluated.value;
}
}
if (decision === null) {
return;
}
replaceStatement(path, decision ? path.node.consequent : path.node.alternate);
changed = true;
}
},
ConditionalExpression(path) {
let decision = evaluateBinary(path.node.test);
if (decision === null) {
const evaluated = path.get("test").evaluate();
if (evaluated.confident && typeof evaluated.value === "boolean") {
decision = evaluated.value;
}
}
if (decision === null) {
return;
}
path.replaceWith(decision ? path.node.consequent : path.node.alternate);
changed = true;
}
});
return { ast, changed };
}
const { inputPath, outputPath } = parseArgs();
let ast = parseFile(inputPath);
let changed = false;
do {
({ ast, changed } = simplify(ast));
if (changed) {
ast = reparse(ast);
}
} while (changed);
saveAst(ast, outputPath);

View File

@@ -0,0 +1,72 @@
const { parseArgs, parseFile, saveAst, t, traverse } = require("./shared");
const typeAlias = {
ArrayExpression: "array",
AssignmentExpression: "assign",
AwaitExpression: "await",
BinaryExpression: "bin",
BooleanLiteral: "bool",
CallExpression: "call",
ConditionalExpression: "cond",
FunctionExpression: "func",
Identifier: "id",
LogicalExpression: "logic",
MemberExpression: "member",
NewExpression: "new",
NullLiteral: "null",
NumericLiteral: "num",
ObjectExpression: "obj",
SequenceExpression: "seq",
StringLiteral: "str",
ThisExpression: "this",
UnaryExpression: "unary"
};
function shouldRename(name) {
return /^_0x/i.test(name) || /^[a-zA-Z]$/.test(name);
}
function buildName(scopeTag, binding, init) {
const fromInit = init && typeAlias[init.type];
const fromRef = binding && binding.referencePaths[0] && typeAlias[binding.referencePaths[0].parentPath.type];
const base = fromInit || fromRef || "value";
return `${scopeTag}_${base}`;
}
const { inputPath, outputPath } = parseArgs();
const ast = parseFile(inputPath);
traverse(ast, {
VariableDeclarator(path) {
if (!path.get("id").isIdentifier()) {
return;
}
const name = path.node.id.name;
if (!shouldRename(name)) {
return;
}
const binding = path.scope.getBinding(name);
const scopeTag = path.scope.block.type === "Program" ? "glb" : "loc";
const nextName = path.scope.generateUidIdentifier(buildName(scopeTag, binding, path.node.init)).name;
path.scope.rename(name, nextName);
},
CatchClause(path) {
if (path.node.param && t.isIdentifier(path.node.param)) {
path.scope.rename(path.node.param.name, path.scope.generateUidIdentifier("error_msg").name);
}
},
"FunctionDeclaration|FunctionExpression|ArrowFunctionExpression": {
exit(path) {
if (path.node.id && t.isIdentifier(path.node.id) && shouldRename(path.node.id.name)) {
path.scope.rename(path.node.id.name, path.scope.generateUidIdentifier("fn").name);
}
path.node.params.forEach((param, index) => {
if (t.isIdentifier(param) && shouldRename(param.name)) {
path.scope.rename(param.name, `arg_${index + 1}`);
}
});
}
}
});
saveAst(ast, outputPath);

View File

@@ -0,0 +1,122 @@
const fs = require("fs");
const path = require("path");
const { spawnSync } = require("child_process");
const { buildPipeline, detectPatterns, getPattern } = require("./pipeline-config");
const { collectMetricsFromFile } = require("./collect-residue-metrics");
function sanitizeStepName(name) {
return String(name).replace(/[^a-z0-9_]+/gi, "_");
}
function runStep(nodePath, step, currentPath, nextPath) {
const start = Date.now();
const result = spawnSync(nodePath, [step.scriptPath, currentPath, nextPath], {
encoding: "utf8",
env: process.env,
timeout: step.timeoutMs
});
const durationMs = Date.now() - start;
const stdout = result.stdout ? result.stdout.trim() : "";
const stderr = result.stderr ? result.stderr.trim() : "";
const timedOut = Boolean(result.error && result.error.code === "ETIMEDOUT");
return {
id: step.id,
script: step.script,
outputFile: path.basename(nextPath),
timeoutMs: step.timeoutMs,
durationMs,
status: timedOut ? "timed_out" : result.status === 0 ? "ok" : "failed",
exitCode: typeof result.status === "number" ? result.status : null,
stdout,
stderr,
error: result.error ? String(result.error.message || result.error) : ""
};
}
function copyFile(fromPath, toPath) {
fs.copyFileSync(fromPath, toPath);
}
function ensureDir(dirPath) {
fs.mkdirSync(dirPath, { recursive: true });
}
function main() {
const [, , inputPath, outputDir, hint = ""] = process.argv;
if (!inputPath || !outputDir) {
console.error("Usage: node run-pipeline.js <input.js> <output-dir> [hint]");
process.exit(1);
}
const absoluteInput = path.resolve(inputPath);
const absoluteOutputDir = path.resolve(outputDir);
ensureDir(absoluteOutputDir);
const sourceText = fs.readFileSync(absoluteInput, "utf8");
const detection = detectPatterns({
inputPath: absoluteInput,
hint,
sourceText
});
const pattern = getPattern(detection.bestId);
const pipeline = buildPipeline(pattern.id);
const report = {
inputPath: absoluteInput,
outputDir: absoluteOutputDir,
hint,
selectedPattern: {
id: pattern.id,
displayName: pattern.displayName,
reference: pattern.reference,
families: pattern.families,
notes: pattern.notes
},
detections: detection.detections,
steps: [],
status: "ok",
failedStep: null,
lastGoodFile: "00_source.js",
finalMetrics: null
};
const sourceCopyPath = path.join(absoluteOutputDir, "00_source.js");
copyFile(absoluteInput, sourceCopyPath);
let currentPath = sourceCopyPath;
for (let index = 0; index < pipeline.length; index += 1) {
const step = pipeline[index];
const stepName = `${String(index + 1).padStart(2, "0")}_${sanitizeStepName(step.id)}.js`;
const nextPath = path.join(absoluteOutputDir, stepName);
const outcome = runStep(process.execPath, step, currentPath, nextPath);
report.steps.push(outcome);
if (outcome.status !== "ok") {
report.status = outcome.status;
report.failedStep = step.id;
break;
}
currentPath = nextPath;
report.lastGoodFile = stepName;
}
const finalPath = path.join(absoluteOutputDir, "final.js");
copyFile(currentPath, finalPath);
report.finalMetrics = collectMetricsFromFile(finalPath);
fs.writeFileSync(path.join(absoluteOutputDir, "selected-patterns.json"), JSON.stringify({
bestId: detection.bestId,
detections: detection.detections,
selectedPattern: report.selectedPattern
}, null, 2), "utf8");
fs.writeFileSync(path.join(absoluteOutputDir, "pipeline-report.json"), JSON.stringify(report, null, 2), "utf8");
process.stdout.write(`${JSON.stringify({
status: report.status,
selectedPattern: report.selectedPattern.id,
failedStep: report.failedStep,
lastGoodFile: report.lastGoodFile
})}\n`);
}
if (require.main === module) {
main();
}

View File

@@ -0,0 +1,60 @@
const fs = require("fs");
const parser = require("@babel/parser");
const traverse = require("@babel/traverse").default;
const generator = require("@babel/generator").default;
const t = require("@babel/types");
function parseFile(filePath) {
return parser.parse(fs.readFileSync(filePath, "utf8"), {
sourceType: "unambiguous",
plugins: ["jsx"]
});
}
function printAst(ast) {
return generator(ast, {
compact: false,
comments: false,
jsescOption: { minimal: true }
}).code;
}
function saveAst(ast, filePath) {
fs.writeFileSync(filePath, printAst(ast), "utf8");
}
function reparse(ast) {
return parser.parse(printAst(ast), {
sourceType: "unambiguous",
plugins: ["jsx"]
});
}
function clone(node) {
return t.cloneNode(node, true);
}
function isPrimitiveNode(node) {
return t.isStringLiteral(node) || t.isNumericLiteral(node) || t.isBooleanLiteral(node) || t.isNullLiteral(node) || t.isIdentifier(node, { name: "undefined" });
}
function parseArgs() {
const [, , inputPath, outputPath] = process.argv;
if (!inputPath || !outputPath) {
console.error("Usage: node <script> <input.js> <output.js>");
process.exit(1);
}
return { inputPath, outputPath };
}
module.exports = {
clone,
isPrimitiveNode,
parseArgs,
parseFile,
printAst,
reparse,
saveAst,
t,
traverse
};