[add] libcalls
This commit is contained in:
151
src/index.mjs
151
src/index.mjs
@@ -1,84 +1,66 @@
|
||||
|
||||
import assert from 'node:assert';
|
||||
import { getASTAndScope } from './ast/analysis.mjs';
|
||||
|
||||
import { getRequireCallsAndConstantArgs } from './calls.mjs';
|
||||
import { readFileSync ,realpathSync ,mkdirSync} from 'node:fs';
|
||||
import { writeFile } from 'node:fs/promises';
|
||||
import tsc, { Project, SyntaxKind } from 'ts-morph';
|
||||
import tsm, { Project, SyntaxKind ,ts} from 'ts-morph';
|
||||
import {getSliceAndInfoSync} from 'slice-js/src/slice-code/test/helpers/utils.js';
|
||||
import path, { dirname,join } from 'node:path';
|
||||
// import tsc from 'typescript'
|
||||
import { getImportCallsAndArgumentTypes } from './tsCalls.mjs';
|
||||
import { LibraryCallsRecorder } from './libcalls.mjs';
|
||||
import { wpCompress } from '../src_bundle/index.mjs';
|
||||
/**
|
||||
* Call parameter generation
|
||||
*
|
||||
* @param {LibraryCallsRecorder['calls']} calls
|
||||
* @param {string} FILE_PATH
|
||||
*/
|
||||
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);
|
||||
|
||||
logCallList(calls);
|
||||
|
||||
export async function sliceAndWriteCalls(calls, FILE_PATH) {
|
||||
const writePromises = [];
|
||||
|
||||
for (const [moduleName, callBox] of calls) {
|
||||
if (!isRelativeModule(moduleName)) { // not relative module
|
||||
if (isRelativeModule(moduleName) || isNodeModule(moduleName)) { // not relative module
|
||||
console.warn(`Skipping module ${moduleName} - relative or inbuilt Node.js module`);
|
||||
continue;
|
||||
}
|
||||
|
||||
const relatedModuleNamePath = join(realpathSync(dirname(FILE_PATH)) ,moduleName);
|
||||
console.log(`Slicing module ${moduleName} - ${callBox.size} calls`);
|
||||
|
||||
// const relatedModuleNamePath = import.meta.resolve(moduleName);
|
||||
// console.log(`Related module path`, relatedModuleNamePath);
|
||||
|
||||
const relatedModuleNamePath = await wpCompress(moduleName)
|
||||
const fileSource = readFileSync(relatedModuleNamePath).toString('utf-8');
|
||||
const {slicedCode} = getSliceAndInfoSync(fileSource, (moduleExports) => {
|
||||
return [...callBox.entries()].flatMap(([methodName, methodArgsList])=>{
|
||||
// continue; // TODO - handle relative modules
|
||||
const { slicedCode } = getSliceAndInfoSync(fileSource, (moduleExports) => {
|
||||
return [...callBox.entries()].flatMap(([methodName, methodArgsList]) => {
|
||||
const methodNameNormed = methodName.substring(1);
|
||||
console.log("Calls for ",methodNameNormed,methodArgsList)
|
||||
return methodArgsList.map(methodArgsList=>{
|
||||
const methodObj = methodNameNormed===''?moduleExports:moduleExports[methodNameNormed];
|
||||
methodObj.apply(moduleExports[methodNameNormed],methodArgsList)
|
||||
console.log("Calls for ", methodNameNormed, methodArgsList);
|
||||
return methodArgsList.map(methodArgsList => {
|
||||
const methodObj = (methodNameNormed === '') ? moduleExports : moduleExports[methodNameNormed];
|
||||
methodObj.apply(moduleExports[methodNameNormed], methodArgsList);
|
||||
});
|
||||
})
|
||||
},relatedModuleNamePath);
|
||||
// console.log(`Sliced code ${moduleName}\n`,slicedCode);
|
||||
});
|
||||
}, relatedModuleNamePath);
|
||||
|
||||
console.log(`Sliced code ${moduleName}\n`,slicedCode);
|
||||
continue;
|
||||
const writePath = path.resolve('./dist', moduleName);
|
||||
if(writePath===moduleName){
|
||||
throw Error("Will overwrite!!!!");
|
||||
if (writePath === moduleName) {
|
||||
throw Error("Unexpected Directory rewrite. Not allowed.");
|
||||
}
|
||||
mkdirSync(path.dirname(writePath),{recursive: true});
|
||||
console.log(`Writing to`,writePath);
|
||||
mkdirSync(path.dirname(writePath), { recursive: true });
|
||||
console.log(`Writing to`, writePath);
|
||||
|
||||
writePromises.push(writeFile(writePath,slicedCode));
|
||||
writePromises.push(writeFile(writePath, slicedCode));
|
||||
|
||||
}
|
||||
|
||||
Promise.all(writePromises).then(p=>{
|
||||
console.log("write finished")
|
||||
Promise.all(writePromises).then(p => {
|
||||
console.log("write finished");
|
||||
}).catch(console.log);
|
||||
}
|
||||
|
||||
class ImportCall{
|
||||
/**
|
||||
* @type {'import'|'importExpr'|'require'}
|
||||
*/
|
||||
importType;
|
||||
/**
|
||||
* @type {string}
|
||||
*/
|
||||
importSyntax;
|
||||
/**
|
||||
*
|
||||
* @param {'import'|'importExpr'|'require'} importType
|
||||
* @param {string} importSyntax
|
||||
*/
|
||||
constructor(importType, importSyntax){
|
||||
this.importSyntax = importSyntax;
|
||||
this.importType = importType;
|
||||
}
|
||||
}
|
||||
|
||||
function main2() {
|
||||
const FILE_PATH = './test_src/index.cjs';
|
||||
function main() {
|
||||
// const FILE_PATH = './test_src/index.cjs';
|
||||
const FILE_PATH = './test_src/index.mjs';
|
||||
|
||||
const project = new Project({compilerOptions:{allowJs: true, checkJs: false,}});
|
||||
project.addSourceFileAtPathIfExists(FILE_PATH);
|
||||
@@ -87,45 +69,29 @@ function main2() {
|
||||
const checker = project.getTypeChecker();
|
||||
|
||||
const sourceFile = project.getSourceFile(FILE_PATH)
|
||||
|
||||
|
||||
const importDecls = sourceFile.getImportStringLiterals()
|
||||
for(const importStringDecl of importDecls){
|
||||
console.log(importStringDecl);
|
||||
const importDecl = importStringDecl.getFirstAncestor();
|
||||
if(importDecl.isKind(SyntaxKind.CallExpression)){
|
||||
// the declaration is callExpression. Verify its based an identifier aliasing import or require
|
||||
const importExpr = importDecl.getExpression();
|
||||
const type = checker.getTypeAtLocation(importExpr);
|
||||
console.log("Type of import expression",checker.compilerObject.resolveName());
|
||||
console.log(importExpr);
|
||||
if(importExpr.isKind(SyntaxKind.Identifier)){
|
||||
// import is a require or import
|
||||
const importName = importExpr.getText();
|
||||
if(importName==='require' || importName==='import'){
|
||||
console.log("Found require/import call",importExpr);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}else if(importDecl.isKind(SyntaxKind.ImportDeclaration)){
|
||||
// TODO pending extract the calls.
|
||||
}else{
|
||||
console.error("Unexpected import specifier",SyntaxKind[importDecl]);
|
||||
}
|
||||
const importThing = importStringDecl.getParent()
|
||||
}
|
||||
// foreach library, get a list of import calls
|
||||
|
||||
console.log(importDecls);
|
||||
const calls = getImportCallsAndArgumentTypes(importDecls,checker,FILE_PATH);
|
||||
|
||||
const callMap = calls.generateAllArgumentsForRecordedCalls();
|
||||
|
||||
logCallList(callMap);
|
||||
sliceAndWriteCalls(callMap, FILE_PATH).then(()=>{
|
||||
console.log("Slicing and writing calls done");
|
||||
});
|
||||
}
|
||||
|
||||
if (process.argv[1] === import.meta.filename) {
|
||||
console.log("[SafeImport] started");
|
||||
main2();
|
||||
main();
|
||||
console.log("done");
|
||||
}
|
||||
|
||||
|
||||
function logCallList(calls) {
|
||||
export function logCallList(calls) {
|
||||
console.log(`[Call Log] Call List for ${calls.size} modules`);
|
||||
for (const [moduleName, callBoxes] of calls.entries()) {
|
||||
if (isRelativeModule(moduleName)) {
|
||||
console.log('Importing', moduleName, callBoxes);
|
||||
@@ -134,8 +100,21 @@ function logCallList(calls) {
|
||||
}
|
||||
}
|
||||
console.log(`Call List`, calls);
|
||||
console.log(`[Call Log] End List for ${calls.size} modules`);
|
||||
|
||||
}
|
||||
|
||||
function isRelativeModule(moduleName) {
|
||||
return moduleName.startsWith('.');
|
||||
}
|
||||
|
||||
/**
|
||||
* True if an inbuilt Node.js module.
|
||||
* @param {string} moduleName
|
||||
* @returns
|
||||
*/
|
||||
function isNodeModule(moduleName) {
|
||||
if(moduleName.startsWith('node:')) return true;
|
||||
const nodeModules = ['fs', 'fs/promises', 'path', 'http', 'https', 'os', 'crypto']
|
||||
return nodeModules.includes(moduleName);
|
||||
}
|
Reference in New Issue
Block a user