mirror of
https://github.com/tiennm99/is-a-dev.git
synced 2026-10-18 10:14:59 +00:00
feat(ci): reserved domains
This commit is contained in:
1 parent
f54d98fef4
commit
3367d04dfc
1 file changed
+56
-4
+56
-4
@@ -7,9 +7,9 @@ const files = fs.readdirSync(domainsPath);
|
|||||||
|
|
||||||
function getParentSubdomain(subdomain) {
|
function getParentSubdomain(subdomain) {
|
||||||
const parts = subdomain.split(".");
|
const parts = subdomain.split(".");
|
||||||
|
|
||||||
if (parts.length <= 1) return null; // No parent for top-level subdomains
|
if (parts.length <= 1) return null; // No parent for top-level subdomains
|
||||||
|
|
||||||
// Attempt to find the parent subdomain by removing the last part
|
// Attempt to find the parent subdomain by removing the last part
|
||||||
for (let i = parts.length - 1; i > 0; i--) {
|
for (let i = parts.length - 1; i > 0; i--) {
|
||||||
const potentialParent = parts.slice(i - 1).join(".");
|
const potentialParent = parts.slice(i - 1).join(".");
|
||||||
@@ -17,11 +17,10 @@ function getParentSubdomain(subdomain) {
|
|||||||
return potentialParent; // Return the parent subdomain if it exists
|
return potentialParent; // Return the parent subdomain if it exists
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return null; // Return null if no valid parent is found
|
return null; // Return null if no valid parent is found
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
function getDomainData(subdomain) {
|
function getDomainData(subdomain) {
|
||||||
try {
|
try {
|
||||||
return fs.readJsonSync(path.join(domainsPath, `${subdomain}.json`));
|
return fs.readJsonSync(path.join(domainsPath, `${subdomain}.json`));
|
||||||
@@ -30,6 +29,33 @@ function getDomainData(subdomain) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function expandReservedDomains() {
|
||||||
|
const reserved = require("../util/reserved-domains.json");
|
||||||
|
const expandedList = [...reserved];
|
||||||
|
|
||||||
|
for (const item of reserved) {
|
||||||
|
const rangeMatch = item.match(/\[(\d+)-(\d+)\]/); // Matches [min-max]
|
||||||
|
|
||||||
|
if (rangeMatch) {
|
||||||
|
const prefix = item.split("[")[0];
|
||||||
|
const start = parseInt(rangeMatch[1], 10);
|
||||||
|
const end = parseInt(rangeMatch[2], 10);
|
||||||
|
|
||||||
|
if (start < end) {
|
||||||
|
for (let i = start; i <= end; i++) {
|
||||||
|
expandedList.push(prefix + i);
|
||||||
|
}
|
||||||
|
|
||||||
|
expandedList.splice(expandedList.indexOf(item), 1);
|
||||||
|
} else {
|
||||||
|
throw new Error(`[reserved-domains.json] Invalid range [${start}-${end}] in "${item}"`);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return expandedList;
|
||||||
|
}
|
||||||
|
|
||||||
t("Nested subdomains should not exist without a parent subdomain", (t) => {
|
t("Nested subdomains should not exist without a parent subdomain", (t) => {
|
||||||
for (const file of files) {
|
for (const file of files) {
|
||||||
const subdomain = file.replace(/\.json$/, "");
|
const subdomain = file.replace(/\.json$/, "");
|
||||||
@@ -75,3 +101,29 @@ t("Nested subdomains should be owned by the parent subdomain's owner", (t) => {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
const reservedDomains = expandReservedDomains();
|
||||||
|
|
||||||
|
t("Subdomain names must not be reserved", (t) => {
|
||||||
|
for (const file of files) {
|
||||||
|
const subdomain = file.replace(/\.json$/, "");
|
||||||
|
|
||||||
|
t.true(!reservedDomains.includes(subdomain), `${file}: Subdomain name is reserved`);
|
||||||
|
}
|
||||||
|
|
||||||
|
t.pass();
|
||||||
|
});
|
||||||
|
|
||||||
|
t("Reserved domains file should be valid", (t) => {
|
||||||
|
const subdomainRegex = /^_?[a-zA-Z0-9]+([-\.][a-zA-Z0-9]+)*(\[\d+-\d+\])?$/;
|
||||||
|
|
||||||
|
for (const item of reservedDomains) {
|
||||||
|
t.regex(
|
||||||
|
item,
|
||||||
|
subdomainRegex,
|
||||||
|
`[util/reserved-domains.json] Invalid subdomain name "${item}" at index ${reservedDomains.indexOf(item)}`
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
t.pass();
|
||||||
|
});
|
||||||
Reference in new issue
Block a user