|
1 | | -console.log('hihi'); |
| 1 | +import fs from 'fs'; |
| 2 | +import path from 'path'; |
| 3 | +import crypto from 'crypto'; |
| 4 | + |
| 5 | +interface ExternalTokenList { |
| 6 | + name: string; |
| 7 | + chainIds: number[]; |
| 8 | + url: string; |
| 9 | +} |
| 10 | + |
| 11 | +interface ExternalJson { |
| 12 | + externalTokenLists: ExternalTokenList[]; |
| 13 | +} |
| 14 | + |
| 15 | +function calculateSha256(content: string): string { |
| 16 | + return crypto.createHash('sha256').update(content).digest('hex'); |
| 17 | +} |
| 18 | + |
| 19 | +async function checkExternalTokenLists() { |
| 20 | + console.log('Starting external token lists check...'); |
| 21 | + |
| 22 | + try { |
| 23 | + // Read the external.json file |
| 24 | + const externalJsonPath = path.join(process.cwd(), 'index', 'external.json'); |
| 25 | + const externalJsonContent = fs.readFileSync(externalJsonPath, 'utf-8'); |
| 26 | + |
| 27 | + // Validate JSON format |
| 28 | + let externalData: ExternalJson; |
| 29 | + try { |
| 30 | + externalData = JSON.parse(externalJsonContent); |
| 31 | + } catch (error) { |
| 32 | + console.error('Error: external.json is not valid JSON'); |
| 33 | + throw new Error('Invalid JSON in external.json'); |
| 34 | + } |
| 35 | + |
| 36 | + if (!externalData.externalTokenLists || !Array.isArray(externalData.externalTokenLists)) { |
| 37 | + console.error('Error: external.json does not contain an externalTokenLists array'); |
| 38 | + throw new Error('Invalid structure in external.json'); |
| 39 | + } |
| 40 | + |
| 41 | + // Check each URL |
| 42 | + const results = await Promise.allSettled( |
| 43 | + externalData.externalTokenLists.map(async (tokenList) => { |
| 44 | + try { |
| 45 | + console.log(`Fetching ${tokenList.name} from ${tokenList.url}...`); |
| 46 | + const response = await fetch(tokenList.url, { redirect: 'follow' }); |
| 47 | + |
| 48 | + if (!response.ok) { |
| 49 | + return { |
| 50 | + name: tokenList.name, |
| 51 | + url: tokenList.url, |
| 52 | + status: response.status, |
| 53 | + error: `HTTP error: ${response.status} ${response.statusText}` |
| 54 | + }; |
| 55 | + } |
| 56 | + |
| 57 | + // Try to parse the response as JSON |
| 58 | + try { |
| 59 | + const text = await response.text(); |
| 60 | + // Calculate size in MB and hash |
| 61 | + const sizeInMB = (text.length / (1024 * 1024)).toFixed(2); |
| 62 | + const hash = calculateSha256(text); |
| 63 | + |
| 64 | + JSON.parse(text); |
| 65 | + return { |
| 66 | + name: tokenList.name, |
| 67 | + url: tokenList.url, |
| 68 | + status: response.status, |
| 69 | + sizeInMB, |
| 70 | + hash, |
| 71 | + success: true |
| 72 | + }; |
| 73 | + } catch (error) { |
| 74 | + return { |
| 75 | + name: tokenList.name, |
| 76 | + url: tokenList.url, |
| 77 | + status: response.status, |
| 78 | + error: 'Invalid JSON response' |
| 79 | + }; |
| 80 | + } |
| 81 | + } catch (error) { |
| 82 | + return { |
| 83 | + name: tokenList.name, |
| 84 | + url: tokenList.url, |
| 85 | + error: error instanceof Error ? error.message : String(error) |
| 86 | + }; |
| 87 | + } |
| 88 | + }) |
| 89 | + ); |
| 90 | + |
| 91 | + // Report results |
| 92 | + const failures = results.filter( |
| 93 | + (result) => result.status === 'rejected' || (result.status === 'fulfilled' && 'error' in result.value) |
| 94 | + ); |
| 95 | + |
| 96 | + console.log('\nResults:'); |
| 97 | + results.forEach((result) => { |
| 98 | + if (result.status === 'fulfilled') { |
| 99 | + if ('error' in result.value) { |
| 100 | + console.error(`❌ ${result.value.name}: ${result.value.error}`); |
| 101 | + } else { |
| 102 | + console.log(`✅ ${result.value.name}: Successfully fetched and validated JSON (${result.value.sizeInMB} MB)`); |
| 103 | + console.log(` Hash: ${result.value.hash}`); |
| 104 | + } |
| 105 | + } else { |
| 106 | + console.error(`❌ Failed to check: ${result.reason}`); |
| 107 | + } |
| 108 | + }); |
| 109 | + |
| 110 | + // Calculate total size |
| 111 | + let totalSizeMB = 0; |
| 112 | + results.forEach((result) => { |
| 113 | + if (result.status === 'fulfilled' && 'sizeInMB' in result.value && result.value.sizeInMB) { |
| 114 | + totalSizeMB += parseFloat(result.value.sizeInMB); |
| 115 | + } |
| 116 | + }); |
| 117 | + |
| 118 | + if (failures.length > 0) { |
| 119 | + console.error(`\nFound ${failures.length} failing external token lists out of ${results.length} total.`); |
| 120 | + console.log(`Total size of successful downloads: ${totalSizeMB.toFixed(2)} MB`); |
| 121 | + throw new Error('External token list check failed'); |
| 122 | + } else { |
| 123 | + console.log(`\nAll ${results.length} external token lists are valid and return valid JSON.`); |
| 124 | + console.log(`Total size of all downloads: ${totalSizeMB.toFixed(2)} MB`); |
| 125 | + } |
| 126 | + } catch (error) { |
| 127 | + console.error('Error during external token list check:', error); |
| 128 | + process.exit(1); |
| 129 | + } |
| 130 | +} |
| 131 | + |
| 132 | +// Run the check |
| 133 | +checkExternalTokenLists(); |
0 commit comments