From 8628cbcda7006df163c7ec51663b38c8a31b3a65 Mon Sep 17 00:00:00 2001 From: ariadnaroman Date: Tue, 20 Jun 2023 15:47:23 +0200 Subject: [PATCH] fix: remove infinite loop 422 --- src/lib/api/poll-import/index.ts | 41 ++++++++------------------------ 1 file changed, 10 insertions(+), 31 deletions(-) diff --git a/src/lib/api/poll-import/index.ts b/src/lib/api/poll-import/index.ts index e7bd615c..d163b6ea 100644 --- a/src/lib/api/poll-import/index.ts +++ b/src/lib/api/poll-import/index.ts @@ -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'; @@ -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"`); @@ -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[],