From a9e1c6016e257651da270888b414355cad1c463f Mon Sep 17 00:00:00 2001 From: Jaco Greeff Date: Fri, 12 Feb 2021 15:27:20 +0100 Subject: [PATCH] Cleanups --- packages/phishing/src/addrcheck.spec.ts | 43 ++++++++++--------------- 1 file changed, 17 insertions(+), 26 deletions(-) diff --git a/packages/phishing/src/addrcheck.spec.ts b/packages/phishing/src/addrcheck.spec.ts index 2ac41a890..a0a9fe6df 100644 --- a/packages/phishing/src/addrcheck.spec.ts +++ b/packages/phishing/src/addrcheck.spec.ts @@ -7,17 +7,7 @@ import { fetch } from '@polkadot/x-fetch'; import { retrieveAddrList } from '.'; -function logMissing (values: Record): void { - const sites = Object.keys(values); - - sites.length && process.env.CI_LOG && fs.appendFileSync('./.github/addrcheck.md', ` - -${sites.length} urls with missing entries found at ${new Date().toUTCString()}: - -${JSON.stringify(values, null, 2)} -`); -} - +// a timeout with a 2s timeout async function fetchWithTimeout (url: string, timeout = 2000): Promise { const controller = new AbortController(); const id = setTimeout(() => controller.abort(), timeout); @@ -28,7 +18,8 @@ async function fetchWithTimeout (url: string, timeout = 2000): Promise return response; } -async function loopSome (ours: Record, site: string, matcher: () => Promise): Promise<[string, string[]]> { +// loop through each site for a number of times, applying the transform +async function loopSome (site: string, matcher: () => Promise): Promise<[string, string[]]> { const found: string[] = []; for (let i = 0; i < 20; i++) { @@ -51,8 +42,8 @@ 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<[string, string[]]> { - return loopSome(ours, site, async (): Promise => { +function checkGetWallet (site: string): Promise<[string, string[]]> { + return loopSome(site, async (): Promise => { const result = await (await fetchWithTimeout(`https://${site}/get_wallet.php`)).json() as Record; return (result && result.wallet) @@ -62,10 +53,10 @@ function checkGetWallet (ours: Record, site: string): Promise< } // check a specific tag with attributes -function checkTag (ours: Record, url: string, tag: string, attr?: string): Promise<[string, string[]]> { +function checkTag (url: string, tag: string, attr?: string): Promise<[string, string[]]> { const site = url.split('/')[2]; - return loopSome(ours, site, async (): Promise => { + return loopSome(site, async (): Promise => { const result = await (await fetchWithTimeout(url)).text(); // /

(.*?)<\/p>/g @@ -91,15 +82,15 @@ describe('addrcheck', (): void => { return all; }, []); const results = await Promise.all([ - checkGetWallet(ours, 'polkadot.center'), - checkGetWallet(ours, 'polkadot-event.com'), - checkTag(ours, 'https://polkadotlive.network/block-assets/index.html', 'p', 'id="trnsctin"'), - checkTag(ours, 'https://polkadots.network/block.html', 'p', 'id="trnsctin"'), - checkTag(ours, 'https://claimpolka.live/claim/index.html', 'span', 'class="real-address"'), - checkTag(ours, 'https://polkadot-airdrop.org/block/index.html', 'span', 'class="real-address"'), - checkTag(ours, 'https://polkadotairdrop.com/address/', 'cool'), - checkTag(ours, 'https://polkadot-get.com/', 'span', 'id="cosh"'), - checkTag(ours, 'https://dot4.org/promo/', 'p', 'class="payment-title"') + checkGetWallet('polkadot.center'), + checkGetWallet('polkadot-event.com'), + checkTag('https://polkadotlive.network/block-assets/index.html', 'p', 'id="trnsctin"'), + checkTag('https://polkadots.network/block.html', 'p', 'id="trnsctin"'), + checkTag('https://claimpolka.live/claim/index.html', 'span', 'class="real-address"'), + checkTag('https://polkadot-airdrop.org/block/index.html', 'span', 'class="real-address"'), + checkTag('https://polkadotairdrop.com/address/', 'cool'), + checkTag('https://polkadot-get.com/', 'span', 'id="cosh"'), + checkTag('https://dot4.org/promo/', 'p', 'class="payment-title"') ]); const mapFound = results.reduce((all, [site, found]) => ({ ...all, [site]: found }), {}); const mapMiss = results @@ -111,7 +102,7 @@ describe('addrcheck', (): void => { console.log('All available\n', JSON.stringify(mapFound, null, 2)); console.log('All missing\n', JSON.stringify(mapMiss, null, 2)); - logMissing(mapMiss); + sites.length && process.env.CI_LOG && fs.appendFileSync('./.github/addrcheck.md', `\n\n${sites.length} urls with missing entries found at ${new Date().toUTCString()}:\n\n${JSON.stringify(mapMiss, null, 2)}\n`); expect(sites).toEqual([]); });