Files
safeImport/src/index.mjs

81 lines
2.9 KiB
JavaScript
Raw Normal View History

2025-07-09 13:26:51 +01:00
import assert from 'node:assert';
import { getASTAndScope } from './ast/analysis.mjs';
import { getRequireCallsAndConstantArgs } from './calls.mjs';
import { analyze, instrumentString, instrumentDir } from 'jalangi2';
2025-07-21 17:01:43 +01:00
import { readFileSync ,realpathSync ,mkdirSync} from 'node:fs';
import { writeFile } from 'node:fs/promises';
2025-07-09 13:26:51 +01:00
2025-07-21 17:01:43 +01:00
import {getSliceAndInfoSync} from 'slice-js/src/slice-code/test/helpers/utils.js';
import path, { dirname,join } from 'node:path';
2025-07-09 13:26:51 +01:00
/**
* Call parameter generation
*/
function main() {
const FILE_PATH = './test_src/index.cjs';
const { scopeManager, _parsedModAST } = getASTAndScope(FILE_PATH);
assert(scopeManager.scopes.length >= 2, "expected atleast global and module scope");
assert(scopeManager.scopes[1].type === 'function', "expected the 'module' scope to have function scope");
const calls = getRequireCallsAndConstantArgs(scopeManager);
2025-07-21 17:01:43 +01:00
logCallList(calls);
2025-07-09 13:26:51 +01:00
2025-07-21 17:01:43 +01:00
const writePromises = [];
2025-07-09 13:26:51 +01:00
for (const [moduleName, callBox] of calls) {
2025-07-21 17:01:43 +01:00
if (!isRelativeModule(moduleName)) { // not relative module
2025-07-09 13:26:51 +01:00
continue;
}
const relatedModuleNamePath = join(realpathSync(dirname(FILE_PATH)) ,moduleName);
const fileSource = readFileSync(relatedModuleNamePath).toString('utf-8');
const {slicedCode} = getSliceAndInfoSync(fileSource, (moduleExports) => {
return [...callBox.entries()].flatMap(([methodName, methodArgsList])=>{
const methodNameNormed = methodName.substring(1);
console.log("Calls for ",methodNameNormed,methodArgsList)
2025-07-21 17:01:43 +01:00
return methodArgsList.map(methodArgsList=>{
const methodObj = methodNameNormed===''?moduleExports:moduleExports[methodNameNormed];
methodObj.apply(moduleExports[methodNameNormed],methodArgsList)
});
2025-07-09 13:26:51 +01:00
})
},relatedModuleNamePath);
2025-07-21 17:01:43 +01:00
// console.log(`Sliced code ${moduleName}\n`,slicedCode);
const writePath = path.resolve('./dist', moduleName);
if(writePath===moduleName){
throw Error("Will overwrite!!!!");
}
mkdirSync(path.dirname(writePath),{recursive: true});
console.log(`Writing to`,writePath);
2025-07-09 13:26:51 +01:00
2025-07-21 17:01:43 +01:00
writePromises.push(writeFile(writePath,slicedCode));
}
2025-07-09 13:26:51 +01:00
2025-07-21 17:01:43 +01:00
Promise.all(writePromises).then(p=>{
console.log("write finished")
}).catch(console.log);
2025-07-09 13:26:51 +01:00
}
if (process.argv[1] === import.meta.filename) {
console.log("[SafeImport] started")
main();
}
2025-07-21 17:01:43 +01:00
function logCallList(calls) {
for (const [moduleName, callBoxes] of calls.entries()) {
if (isRelativeModule(moduleName)) {
console.log('Importing', moduleName, callBoxes);
} else {
console.log(`Module "${moduleName}" - System module. FIXME skipping`);
}
}
console.log(`Call List`, calls);
}
function isRelativeModule(moduleName) {
return moduleName.startsWith('.');
}