UNPKG

@hclsoftware/secagent

Version:

IAST agent

94 lines (81 loc) 3.65 kB
//IASTIGNORE /* * **************************************************** * Licensed Materials - Property of HCL. * (c) Copyright HCL Technologies Ltd. 2017, 2025. * Note to U.S. Government Users *Restricted Rights. * **************************************************** */ const {Component, Type} = require("./Component") const PackageJsonParser = require("./PackageJsonParser"); const fs = require("fs"); const path = require("path"); class FileSystemComponentsReader { // Read predefined libraries from the runtime file system, starting from the given path upwards (up to root dir). // Return the found libraries as a component list. readComponentsUpwards(startingPath) { const componentsToFind = ["express", "react-scripts"] const componentToPathMap = FileSystemComponentsReader.trySearchingPackageJsonFilesUpwards(startingPath, componentsToFind) const components = [] const parser = new PackageJsonParser() for (const entry of componentToPathMap.entries()) { const componentName = entry[0] const jsonPath = entry[1] let version = parser.findVersion(jsonPath) version = version != null ? version : "" const type = componentName === "express" ? Type.Server : Type.Library; components.push(new Component(componentName, version, type)) } return components } static trySearchingPackageJsonFilesUpwards(dir, componentsToFind) { let result = new Map() try { result = FileSystemComponentsReader.searchPackageJsonFilesUpwards(dir, componentsToFind) } catch (error) { console.origError(error) } return result } // Starting from startingDir dir upwards, recursively search package.json files of the given required // components and put static searchPackageJsonFilesUpwards(startingDir, componentsToFind) { if (startingDir === path.parse(startingDir).root) { return new Map() } const files = fs.readdirSync(startingDir) for (const currentFileName of files) { const currentFilePath = path.join(startingDir, currentFileName) if (!fs.statSync(currentFilePath).isDirectory()) { continue } if (currentFileName === "node_modules") { const currentPaths = FileSystemComponentsReader.getPackageJsonPathsFromNodeModules(currentFilePath, componentsToFind) const result = new Map() for (const [key, value] of currentPaths) { result.set(key, value) } return result } } return FileSystemComponentsReader.searchPackageJsonFilesUpwards(path.dirname(startingDir), componentsToFind) } static getPackageJsonPathsFromNodeModules(nodeModulesDir, componentsToFind) { const componentToPathMap = new Map() for (const currentFileName of fs.readdirSync(nodeModulesDir)) { const currentFilePath = path.join(nodeModulesDir, currentFileName) if (!fs.statSync(currentFilePath).isDirectory()) { continue } if (componentsToFind.origArrayIncludes(currentFileName)) { const packageJsonPath = path.join(currentFilePath, "package.json"); if (fs.existsSync(packageJsonPath)) { componentToPathMap.set(currentFileName, packageJsonPath); } } } return componentToPathMap } } module.exports = FileSystemComponentsReader;