Skip to content

Commit

Permalink
Merge pull request #451 from snyk-tech-services/fix/remove-infinite-l…
Browse files Browse the repository at this point in the history
…oop-422

fix: remove infinite loop 422
  • Loading branch information
ariadna-roman authored Jun 20, 2023
2 parents 00a585e + 8628cbc commit 6bd9137
Showing 1 changed file with 10 additions and 31 deletions.
41 changes: 10 additions & 31 deletions src/lib/api/poll-import/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import 'source-map-support/register';
import * as url from 'url';
import { requestsManager } from 'snyk-request-manager';
import type { requestsManager } from 'snyk-request-manager';
import * as sleep from 'sleep-promise';
import * as debugLib from 'debug';
import * as _ from 'lodash';
Expand Down Expand Up @@ -33,28 +33,25 @@ export async function pollImportUrl(
}
try {
const { pathname = '' } = url.parse(locationUrl);
const res = await tryPolling(
requestManager,
(pathname as string).split('/api/v1/')[1],
);
const res = await requestManager.request({
verb: 'get',
url: (pathname as string).split('/api/v1/')[1],
body: JSON.stringify({}),
});
const importStatus: PollImportResponse = res.data;
const statusCode = res.statusCode || res.status;

if (!statusCode || ![422, 200].includes(statusCode)) {
if (!statusCode || statusCode !== 200) {
throw new Error(
'Expected a 200 response, instead received: ' +
JSON.stringify({ data: res.data, status: statusCode }),
);
}
if (statusCode == 422) {
retryCount = 3;
}
debug(`Import task status is "${importStatus.status}"`);
if (
statusCode == 422 ||
(importStatus.status &&
importStatus.status !== 'complete' &&
retryCount > 0)
importStatus.status &&
importStatus.status !== 'complete' &&
retryCount > 0
) {
await sleep(retryWaitTime);
debug(`Will re-check import task in "${retryWaitTime} ms"`);
Expand All @@ -79,24 +76,6 @@ export async function pollImportUrl(
}
}

async function tryPolling(requestManager: requestsManager, pollingUrl: string) {
try {
const res = await requestManager.request({
verb: 'get',
url: pollingUrl,
body: JSON.stringify({}),
});

return res;
} catch (e) {
if (e.message?.response?.status == 422) {
return e.message?.response;
}

throw e;
}
}

export async function pollImportUrls(
requestManager: requestsManager,
locationUrls: string[],
Expand Down

0 comments on commit 6bd9137

Please sign in to comment.