From 57e7a7959717399b2f35a49534a27917d05c4329 Mon Sep 17 00:00:00 2001 From: alice Date: Mon, 16 Feb 2026 22:27:46 -0800 Subject: [PATCH] Update applyIteratively to use an Arborist object. --- src/utils/applyIteratively.js | 30 +++++++++++++++++++++++------- 1 file changed, 23 insertions(+), 7 deletions(-) diff --git a/src/utils/applyIteratively.js b/src/utils/applyIteratively.js index 13877f5..495b54e 100644 --- a/src/utils/applyIteratively.js +++ b/src/utils/applyIteratively.js @@ -4,22 +4,21 @@ import {createHash} from 'node:crypto'; const generateHash = str => createHash('sha256').update(str).digest('hex'); - /** * Apply functions to modify the script repeatedly until they are no long effective or the max number of iterations is reached. - * @param {string} script The target script to run the functions on. + * @param {Arborist} arborist Arborist instance with AST to run the functions on. * @param {function[]} funcs * @param {number?} maxIterations (optional) Stop the loop after this many iterations at most. - * @return {string} The possibly modified script. + * @return {Arborist} The possibly modified Arborist object. */ -function applyIteratively(script, funcs, maxIterations = 500) { +function applyIterativelyArborist(arborist, funcs, maxIterations = 500) { let scriptSnapshot = ''; let currentIteration = 0; let changesCounter = 0; let iterationsCounter = 0; + let script = arborist.script; try { let scriptHash = generateHash(script); - let arborist = new Arborist(script); while (arborist.ast?.length && scriptSnapshot !== script && currentIteration < maxIterations) { const iterationStartTime = Date.now(); scriptSnapshot = script; @@ -60,7 +59,24 @@ function applyIteratively(script, funcs, maxIterations = 500) { } catch (e) { logger.error(`[-] Error on iteration #${iterationsCounter}: ${e}\n${e.stack}`); } - return script; + return arborist; +} + +/** + * Apply functions to modify the script repeatedly until they are no long effective or the max number of iterations is reached. + * @param {string} script The target script to run the functions on. + * @param {function[]} funcs + * @param {number?} maxIterations (optional) Stop the loop after this many iterations at most. + * @return {string} The possibly modified script. + */ +function applyIteratively(script, funcs, maxIterations = 500) { + let arborist; + try { + arborist = new Arborist(script); + } catch (e) { + logger.error(`[-] Error creating Arborist instance: ${e}\n${e.stack}`); + } + return applyIterativelyArborist(arborist, funcs, maxIterations).script; } -export {applyIteratively}; \ No newline at end of file +export {applyIteratively, applyIterativelyArborist}; \ No newline at end of file