mirror of
https://github.com/AikidoSec/safe-chain.git
synced 2026-05-26 20:20:49 +00:00
Handle pr comments
This commit is contained in:
parent
8d2655a4bf
commit
a5d545f29b
2 changed files with 23 additions and 13 deletions
|
|
@ -21,6 +21,8 @@ const malwareDatabaseUrls = {
|
||||||
* @returns {Promise<{malwareDatabase: MalwarePackage[], version: string | undefined}>}
|
* @returns {Promise<{malwareDatabase: MalwarePackage[], version: string | undefined}>}
|
||||||
*/
|
*/
|
||||||
export async function fetchMalwareDatabase() {
|
export async function fetchMalwareDatabase() {
|
||||||
|
const numberOfAttempts = 4;
|
||||||
|
|
||||||
return retry(async () => {
|
return retry(async () => {
|
||||||
const ecosystem = getEcoSystem();
|
const ecosystem = getEcoSystem();
|
||||||
const malwareDatabaseUrl =
|
const malwareDatabaseUrl =
|
||||||
|
|
@ -43,13 +45,15 @@ export async function fetchMalwareDatabase() {
|
||||||
} catch (/** @type {any} */ error) {
|
} catch (/** @type {any} */ error) {
|
||||||
throw new Error(`Error parsing malware database: ${error.message}`);
|
throw new Error(`Error parsing malware database: ${error.message}`);
|
||||||
}
|
}
|
||||||
}, 3);
|
}, numberOfAttempts);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @returns {Promise<string | undefined>}
|
* @returns {Promise<string | undefined>}
|
||||||
*/
|
*/
|
||||||
export async function fetchMalwareDatabaseVersion() {
|
export async function fetchMalwareDatabaseVersion() {
|
||||||
|
const numberOfAttempts = 4;
|
||||||
|
|
||||||
return retry(async () => {
|
return retry(async () => {
|
||||||
const ecosystem = getEcoSystem();
|
const ecosystem = getEcoSystem();
|
||||||
const malwareDatabaseUrl =
|
const malwareDatabaseUrl =
|
||||||
|
|
@ -66,7 +70,7 @@ export async function fetchMalwareDatabaseVersion() {
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
return response.headers.get("etag") || undefined;
|
return response.headers.get("etag") || undefined;
|
||||||
}, 3);
|
}, numberOfAttempts);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -74,21 +78,27 @@ export async function fetchMalwareDatabaseVersion() {
|
||||||
*
|
*
|
||||||
* @template T
|
* @template T
|
||||||
* @param {() => Promise<T>} func - The asynchronous function to retry
|
* @param {() => Promise<T>} func - The asynchronous function to retry
|
||||||
* @param {number} times - The number of retry attempts (will execute times + 1 total attempts)
|
* @param {number} attempts - The number of attempts
|
||||||
* @returns {Promise<T>} The return value of the function if successful
|
* @returns {Promise<T>} The return value of the function if successful
|
||||||
* @throws {Error} The last error encountered if all retry attempts fail
|
* @throws {Error} The last error encountered if all retry attempts fail
|
||||||
*/
|
*/
|
||||||
async function retry(func, times) {
|
async function retry(func, attempts) {
|
||||||
let lastError;
|
let lastError;
|
||||||
|
|
||||||
for (let i = 0; i <= times; i++) {
|
for (let i = 0; i < attempts; i++) {
|
||||||
try {
|
try {
|
||||||
return await func();
|
return await func();
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
lastError = error;
|
lastError = error;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (i < times) {
|
if (i < attempts - 1) {
|
||||||
|
// When this is not the last try, back-off expenentially:
|
||||||
|
// 1st attempt - 500ms delay
|
||||||
|
// 2nd attempt - 1000ms delay
|
||||||
|
// 3rd attempt - 2000ms delay
|
||||||
|
// 4th attempt - 4000ms delay
|
||||||
|
// ...
|
||||||
await new Promise((resolve) => setTimeout(resolve, Math.pow(2, i) * 500));
|
await new Promise((resolve) => setTimeout(resolve, Math.pow(2, i) * 500));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -48,13 +48,13 @@ export async function openMalwareDatabase() {
|
||||||
*/
|
*/
|
||||||
function getPackageStatus(name, version) {
|
function getPackageStatus(name, version) {
|
||||||
const normalizedName = normalizePackageName(name);
|
const normalizedName = normalizePackageName(name);
|
||||||
const packageData = malwareDatabase.find((pkg) => {
|
const packageData = malwareDatabase.find(
|
||||||
|
(pkg) => {
|
||||||
const normalizedPkgName = normalizePackageName(pkg.package_name);
|
const normalizedPkgName = normalizePackageName(pkg.package_name);
|
||||||
return (
|
return normalizedPkgName === normalizedName &&
|
||||||
normalizedPkgName === normalizedName &&
|
(pkg.version === version || pkg.version === "*");
|
||||||
(pkg.version === version || pkg.version === "*")
|
}
|
||||||
);
|
);
|
||||||
});
|
|
||||||
|
|
||||||
if (!packageData) {
|
if (!packageData) {
|
||||||
return MALWARE_STATUS_OK;
|
return MALWARE_STATUS_OK;
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue