proxy.test.js (2535B)
1 import t from "ava"; 2 import fs from "fs-extra"; 3 import path from "path"; 4 5 const requiredRecordsToProxy = new Set(["A", "AAAA", "CNAME"]); 6 7 const domainCache = {}; 8 9 function getDomainData(file) { 10 if (domainCache[file]) { 11 return domainCache[file]; 12 } 13 14 try { 15 const data = fs.readJsonSync(path.join(domainsPath, file)); 16 domainCache[file] = data; 17 return data; 18 } catch (error) { 19 throw new Error(`Failed to read JSON for ${file}: ${error.message}`); 20 } 21 } 22 23 function validateProxiedRecords(t, data, file) { 24 const recordTypes = Array.from(requiredRecordsToProxy).join(", "); 25 26 if (data.proxied) { 27 const hasProxiedRecord = Object.keys(data.records).some((key) => requiredRecordsToProxy.has(key)); 28 29 t.true( 30 hasProxiedRecord, 31 `${file}: Proxied is true but there are no records that can be proxied (${recordTypes} expected)` 32 ); 33 } 34 } 35 36 const domainsPath = path.resolve("domains"); 37 const files = fs.readdirSync(domainsPath).filter((file) => file.endsWith(".json")); 38 39 t("Domains with proxy enabled must have at least one proxy-able record", (t) => { 40 files.forEach((file) => { 41 const data = getDomainData(file); 42 43 validateProxiedRecords(t, data, file); 44 }); 45 46 t.pass(); 47 }); 48 49 const unproxyable = [ 50 { 51 type: "CNAME", 52 value: "*.onrender.com" 53 } 54 ]; 55 56 t("Domains with specific records must not have proxy enabled", (t) => { 57 files.forEach((file) => { 58 const data = getDomainData(file); 59 60 if (data.proxied) { 61 unproxyable.forEach((record) => { 62 let recordExists = false; 63 64 if (!data.records[record.type]) { 65 t.pass(); 66 return; 67 } else if (Array.isArray(data.records[record.type])) { 68 recordExists = data.records[record.type].some((r) => 69 record.value.startsWith("*.") ? r.endsWith(record.value.slice(2)) : r === record.value 70 ); 71 } else { 72 recordExists = record.value.startsWith("*.") 73 ? data.records[record.type].endsWith(record.value.slice(2)) 74 : data.records[record.type] === record.value; 75 } 76 77 t.false( 78 recordExists, 79 `${file}: Records matching \`${record.type}: "${record.value}"\` cannot be proxied` 80 ); 81 }); 82 } 83 }); 84 85 t.pass(); 86 });