diff --git a/packages/phishing/src/addrcheck.spec.ts b/packages/phishing/src/addrcheck.spec.ts index 53263f051..382e8a5e8 100644 --- a/packages/phishing/src/addrcheck.spec.ts +++ b/packages/phishing/src/addrcheck.spec.ts @@ -20,6 +20,16 @@ Missing entries found for ${site}: ${JSON.stringify(missing)} return null; } +async function fetchWithTimeout (url: string, timeout = 2000): Promise { + const controller = new AbortController(); + const id = setTimeout(() => controller.abort(), timeout); + const response = await fetch(url, { signal: controller.signal }); + + clearTimeout(id); + + return response; +} + async function loopSome (ours: Record, site: string, matcher: () => Promise): Promise { const all = Object.values(ours).reduce((all: string[], addrs: string[]): string[] => { all.push(...addrs); @@ -32,13 +42,11 @@ async function loopSome (ours: Record, site: string, matcher: try { const addresses = await matcher(); - if (addresses) { - addresses.forEach((address): void => { - if (address && !found.includes(address)) { - found.push(address); - } - }); - } + addresses && addresses.forEach((address): void => { + if (address && !found.includes(address)) { + found.push(address); + } + }); } catch (error) { // ignore } @@ -54,7 +62,7 @@ async function loopSome (ours: Record, site: string, matcher: // shared between polkadot.center & polkadot-event.com (addresses are also the same on first run) function checkGetWallet (ours: Record, site: string): Promise { return loopSome(ours, site, async (): Promise => { - const result = await (await fetch(`https://${site}/get_wallet.php`)).json() as Record; + const result = await (await fetchWithTimeout(`https://${site}/get_wallet.php`)).json() as Record; return (result && result.wallet) ? [result.wallet.replace('\r', '').trim()] @@ -67,7 +75,7 @@ function checkTag (ours: Record, url: string, tag: string, att const site = url.split('/')[2]; return loopSome(ours, site, async (): Promise => { - const result = await (await fetch(url)).text(); + const result = await (await fetchWithTimeout(url)).text(); // /

(.*?)<\/p>/g const match = new RegExp(`<${tag}${attr ? ` ${attr}` : ''}>(.*?)`, 'g').exec(result); @@ -83,7 +91,7 @@ describe('addrcheck', (): void => { let ours: Record; beforeAll(async (): Promise => { - jest.setTimeout(10 * 60 * 1000); + jest.setTimeout(5 * 60 * 1000); ours = await retrieveAddrList(); });