Skip to content

Commit

Permalink
refactor: Use Promise.all to resolve all promises on detect
Browse files Browse the repository at this point in the history
  • Loading branch information
Ilianna Papastefanou committed Jan 20, 2021
1 parent 8ccd42f commit 544a793
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 15 deletions.
19 changes: 7 additions & 12 deletions src/lib/iac/detect-iac.ts
Expand Up @@ -99,10 +99,9 @@ async function getDirectoryFiles(
});

const iacFiles: IacFileInDirectory[] = [];
const maxConcurrent = 50;
const maxConcurrent = 25;
const queue = new Queue(maxConcurrent);

const promises: Array<Promise<IacFileInDirectory>> = [];
for (const filePath of directoryPaths) {
const fileType = pathLib
.extname(filePath)
Expand All @@ -111,33 +110,29 @@ async function getDirectoryFiles(
if (!fileType || !supportedExtensions.has(fileType)) {
continue;
}
promises.push(
iacFiles.push(
queue.add(async () => {
try {
const projectType = await getProjectTypeForIacFile(filePath);
iacFiles.push({
return {
filePath,
projectType,
fileType,
});
};
} catch (err) {
iacFiles.push({
return {
filePath,
fileType,
failureReason:
err instanceof CustomError ? err.userMessage : 'Unhandled Error',
});
};
}
}),
);
}

if (promises.length > 0) {
await Promise.all(promises);
}

if (iacFiles.length === 0) {
throw IacDirectoryWithoutAnyIacFileError();
}
return iacFiles;
return Promise.all(iacFiles);
}
3 changes: 2 additions & 1 deletion src/lib/request/request.ts
Expand Up @@ -112,13 +112,14 @@ export = function makeRequest(
const proxyUri = getProxyForUrl(url);
if (proxyUri) {
snykDebug('using proxy:', proxyUri);
// proxyAgent type is an EventEmitter and not an http Agent
options.agent = (new ProxyAgent(proxyUri) as unknown) as http.Agent;
} else {
snykDebug('not using proxy');
}

if (global.ignoreUnknownCA) {
debug('Using insecure mode (ignore unkown certificate authority)');
debug('Using insecure mode (ignore unknown certificate authority)');
options.rejectUnauthorized = false;
}

Expand Down
2 changes: 1 addition & 1 deletion src/lib/snyk-test/run-test.ts
Expand Up @@ -234,7 +234,7 @@ async function sendAndParseResults(
if (options.iac) {
const maxConcurrent = 25;
const queue = new Queue(maxConcurrent);
const iacResults: Array<Promise<TestResult>> = [];
const iacResults: Promise<TestResult>[] = [];

await spinner.clear<void>(spinnerLbl)();
await spinner(spinnerLbl);
Expand Down
1 change: 0 additions & 1 deletion test/request.test.ts
Expand Up @@ -4,7 +4,6 @@ import * as proxyquire from 'proxyquire';
import * as needle from 'needle';
import { Global } from '../src/cli/args';
import * as http from 'http';
import * as https from 'https';

declare const global: Global;

Expand Down

0 comments on commit 544a793

Please sign in to comment.