Skip to content

Commit

Permalink
fix: avoid possible unhandled promise rejections
Browse files Browse the repository at this point in the history
  • Loading branch information
adrobuta committed Nov 12, 2024
1 parent af9daef commit 24d315f
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 9 deletions.
5 changes: 5 additions & 0 deletions .snyk
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,9 @@ ignore:
reason: No fix is available at the moment.
expires: 2024-12-13T15:00:00.000Z
created: 2024-05-13T15:00:00.000Z
SNYK-JS-CROSSSPAWN-8303230:
- '*':
reason: Ignoring for a short interval since it is a high.
expires: 2024-12-13T15:00:00.000Z
created: 2024-11-10T15:00:00.000Z
patch: {}
34 changes: 25 additions & 9 deletions lib/analyzer/applications/node-modules-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,17 +42,25 @@ async function createSyntheticManifest(
): Promise<void> {
const tempRootManifestPath = path.join(tempRootManifestDir, manifestName);
debug(`Creating an empty synthetic manifest file: ${tempRootManifestPath}`);
await writeFile(tempRootManifestPath, "{}", "utf-8");
try {
await writeFile(tempRootManifestPath, "{}", "utf-8");
} catch (error) {
debug(
`Error while writing file ${tempRootManifestPath} : ${error.message}`,
);
}
}

async function saveOnDisk(
tempDir: string,
modules: Set<string>,
filePathToContent: FilePathToContent,
) {
): Promise<void> {
for (const module of modules) {
const manifestContent = filePathToContent[module];

if (!manifestContent) {
continue;
}
await createFile(path.join(tempDir, module), manifestContent);
}
}
Expand Down Expand Up @@ -109,9 +117,13 @@ async function persistNodeModules(
}
}

async function createFile(filePath, fileContent) {
await mkdir(path.dirname(filePath), { recursive: true });
await writeFile(filePath, fileContent, "utf-8");
async function createFile(filePath, fileContent): Promise<void> {
try {
await mkdir(path.dirname(filePath), { recursive: true });
await writeFile(filePath, fileContent, "utf-8");
} catch (error) {
debug(`Error while creating file ${filePath} : ${error.message}`);
}
}

function isYarnCacheDependency(filePath: string): boolean {
Expand Down Expand Up @@ -149,7 +161,7 @@ function isPnpmCacheDependency(filePath: string): boolean {
function getNodeModulesParentDir(filePath: string): string | null {
const nodeModulesParentDirMatch = nodeModulesRegex.exec(filePath);

if (nodeModulesParentDirMatch) {
if (nodeModulesParentDirMatch && nodeModulesParentDirMatch.length > 1) {
const nodeModulesParentDir = nodeModulesParentDirMatch[1];
if (nodeModulesParentDir === "") {
return "/"; // ensuring the same behavior of path.dirname for '/' dir
Expand Down Expand Up @@ -194,9 +206,13 @@ function groupFilesByDirectory(
return filesByDir;
}

async function cleanupAppNodeModules(appRootDir: string) {
async function cleanupAppNodeModules(appRootDir: string): Promise<void> {
if (!appRootDir) {
return;
}

try {
rm(appRootDir, { recursive: true });
await rm(appRootDir, { recursive: true });
} catch (error) {
debug(`Error while removing ${appRootDir} : ${error.message}`);
}
Expand Down
6 changes: 6 additions & 0 deletions lib/analyzer/applications/node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,10 +82,16 @@ async function depGraphFromNodeModules(
filePathToContent,
fileNamesGroupedByDirectory,
);

if (!tempDir) {
continue;
}

if (!tempProjectPath) {
await cleanupAppNodeModules(tempDir);
continue;
}

try {
const pkgTree: lockFileParser.PkgTree = await resolveDeps(
tempProjectPath,
Expand Down
22 changes: 22 additions & 0 deletions test/system/application-scans/node.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -375,3 +375,25 @@ describe("node application files grouping", () => {
]);
});
});

describe("Edge testing of node modules scan utils functions", () => {
it("Exceptions should be handled", async () => {
expect(() => nodeUtils.cleanupAppNodeModules("")).not.toThrow();

expect(() => nodeUtils.groupFilesByDirectory({ "": "" })).not.toThrow();
expect(() =>
nodeUtils.persistNodeModules(
"",
{ "": "" },
new Map<string, Set<string>>(),
),
).not.toThrow();
expect(
await nodeUtils.persistNodeModules(
"",
{ "": "" },
new Map<string, Set<string>>(),
),
).toEqual({ tempDir: "", tempProjectPath: "" });
});
});

0 comments on commit 24d315f

Please sign in to comment.