Files
safeImport/src/index.mjs

137 lines
5.1 KiB
JavaScript
Raw Normal View History

2025-07-09 13:26:51 +01:00
2025-08-03 15:55:20 +01:00
import { readFileSync, mkdirSync } from 'node:fs';
2025-07-21 17:01:43 +01:00
import { writeFile } from 'node:fs/promises';
2025-08-03 15:55:20 +01:00
import { Project } from 'ts-morph';
import { getSliceAndInfoSync } from 'slice-js/src/slice-code/test/helpers/utils.js';
import path from 'node:path';
2025-08-03 15:55:20 +01:00
import { getImportCallsAndArgumentTypes, isNodeModule, isRelativeModule, logCallList } from './tsCalls.mjs';
import { wpCompress } from './bundle/index.mjs';
import { LibraryTypesRecorder } from './libcalls.mjs';
2025-07-09 13:26:51 +01:00
/**
2025-08-01 20:19:24 +01:00
*
* @param {ReturnType<LibraryTypesRecorder['generateAllArgumentsForRecordedCalls']>} calls
2025-07-09 13:26:51 +01:00
*/
2025-08-04 12:44:24 +01:00
export async function sliceAndWriteCalls(calls) {
2025-07-21 17:01:43 +01:00
const writePromises = [];
2025-08-01 20:19:24 +01:00
2025-07-09 13:26:51 +01:00
for (const [moduleName, callBox] of calls) {
2025-08-01 20:19:24 +01:00
if (isRelativeModule(moduleName) || isNodeModule(moduleName)) { // not relative module
console.warn(`Skipping module ${moduleName} - relative or inbuilt Node.js module`);
2025-07-09 13:26:51 +01:00
continue;
}
2025-08-01 20:19:24 +01:00
console.log(`Slicing module ${moduleName} - ${callBox.size} calls`);
2025-08-03 15:55:20 +01:00
2025-08-01 20:19:24 +01:00
// const relatedModuleNamePath = import.meta.resolve(moduleName);
// console.log(`Related module path`, relatedModuleNamePath);
2025-08-03 15:55:20 +01:00
2025-08-01 20:19:24 +01:00
const relatedModuleNamePath = await wpCompress(moduleName)
2025-07-09 13:26:51 +01:00
const fileSource = readFileSync(relatedModuleNamePath).toString('utf-8');
2025-08-01 20:19:24 +01:00
// continue; // TODO - handle relative modules
const { slicedCode } = getSliceAndInfoSync(fileSource, (moduleExports) => {
return [...callBox.entries()].flatMap(([methodName, methodArgsList]) => {
2025-07-09 13:26:51 +01:00
const methodNameNormed = methodName.substring(1);
2025-08-01 20:19:24 +01:00
console.log("Calls for ", methodNameNormed, methodArgsList);
return methodArgsList.map(methodArgsList => {
const methodObj = (methodNameNormed === '') ? moduleExports : moduleExports[methodNameNormed];
2025-08-04 12:44:24 +01:00
if(methodObj === undefined) {
console.warn(`Method ${methodNameNormed} not found in module ${moduleName}`);
return;
}
try{
methodObj.apply(moduleExports[methodNameNormed], methodArgsList);
} catch(e) {
console.warn(`Error calling method ${methodNameNormed} with args ${methodArgsList} in module ${moduleName}`, e);
return;
}
2025-07-21 17:01:43 +01:00
});
2025-08-01 20:19:24 +01:00
});
}, relatedModuleNamePath);
2025-08-03 15:55:20 +01:00
2025-08-01 20:51:30 +01:00
// console.log(`Sliced code ${moduleName}\n`,slicedCode);
// continue;
2025-08-03 15:55:20 +01:00
const writePath = path.resolve('./dist', moduleName, 'index.cjs');
2025-08-01 20:19:24 +01:00
if (writePath === moduleName) {
throw Error("Unexpected Directory rewrite. Not allowed.");
2025-07-21 17:01:43 +01:00
}
2025-08-01 20:19:24 +01:00
mkdirSync(path.dirname(writePath), { recursive: true });
2025-08-01 20:51:30 +01:00
console.log(`Writing module '${moduleName}' to '${writePath}'`);
2025-07-09 13:26:51 +01:00
2025-08-01 20:19:24 +01:00
writePromises.push(writeFile(writePath, slicedCode));
2025-07-26 13:44:32 +01:00
2025-07-21 17:01:43 +01:00
}
2025-07-09 13:26:51 +01:00
2025-08-01 20:19:24 +01:00
Promise.all(writePromises).then(p => {
2025-08-01 20:51:30 +01:00
// console.log("write finished");
2025-07-21 17:01:43 +01:00
}).catch(console.log);
2025-07-09 13:26:51 +01:00
}
2025-08-04 12:44:24 +01:00
/**
*
* @param {string} filePath
*/
function driver(folderPath = './test_src/anymatch') {
2025-08-01 20:19:24 +01:00
// const FILE_PATH = './test_src/index.cjs';
2025-07-26 13:44:32 +01:00
2025-08-03 15:55:20 +01:00
const project = new Project({ compilerOptions: { allowJs: true, checkJs: false, } });
2025-07-26 13:44:32 +01:00
2025-08-04 12:44:24 +01:00
const scriptGlobs = constructJavascriptGlobInFolder(folderPath)
project.addSourceFilesAtPaths(scriptGlobs);
const sourceFiles = project.getSourceFiles()
const libraryTypesRecorder = new LibraryTypesRecorder(project.getTypeChecker());
2025-07-26 13:44:32 +01:00
// const project = tsc.createProgram([FILE_PATH],);
const checker = project.getTypeChecker();
2025-08-04 12:44:24 +01:00
console.log(`Source files found: ${sourceFiles.length}`, ...sourceFiles.map(sf => sf.getFilePath()));
for (const sourceFile of sourceFiles) {
const filePath = sourceFile.getFilePath();
console.log(`[analyzer] Processing file: ${filePath}`);
const importDecls = sourceFile.getImportStringLiterals()
// foreach library, get a list of import calls
getImportCallsAndArgumentTypes(importDecls, checker, filePath,libraryTypesRecorder);
}
2025-07-26 13:44:32 +01:00
2025-08-04 12:44:24 +01:00
const callMap = libraryTypesRecorder.generateAllArgumentsForRecordedCalls();
2025-08-01 20:19:24 +01:00
2025-08-03 15:55:20 +01:00
2025-08-04 12:44:24 +01:00
logCallList(callMap, 'FakeModuleName');
sliceAndWriteCalls(callMap).then(() => {
2025-08-01 20:19:24 +01:00
console.log("Slicing and writing calls done");
});
2025-07-26 13:44:32 +01:00
}
2025-07-09 13:26:51 +01:00
if (process.argv[1] === import.meta.filename) {
2025-07-26 13:44:32 +01:00
console.log("[SafeImport] started");
2025-08-04 12:44:24 +01:00
driver();
2025-07-21 17:01:43 +01:00
}
2025-08-01 20:19:24 +01:00
2025-08-04 12:44:24 +01:00
/**
*
* @param {string} folderPath
* @returns {string[]}
*/
function constructJavascriptGlobInFolder(folderPath) {
return [
["**/*.js", true],
["**/*.mjs", true],
["**/*.cjs", true],
["**/*.d.ts", false],
["**/*.ts", true],
["**/node_modules/**", false],
["**/dist/**", false],
["**/build/**", false],
["**/out/**", false],
["**/coverage/**", false],
["**/test/**", false],
["**/tests/**", false],
["**/__tests__/**", false],
["**/__mocks__/**", false],
].map(glob => {
const prefix = glob[1] ? '' : '!';
return prefix+path.resolve(folderPath, glob[0])});
}