Refactor: use foxts more
Some checks failed
Build / Build (push) Has been cancelled
Build / Diff output (push) Has been cancelled
Build / Deploy to Cloudflare Pages (3.114.6) (push) Has been cancelled
Build / Deploy to GitHub and GitLab (push) Has been cancelled

This commit is contained in:
SukkaW
2025-06-02 00:44:07 +08:00
parent 017ff38b5e
commit bdc1f5ec82
9 changed files with 33 additions and 57 deletions

View File

@@ -80,7 +80,11 @@ const domesticDohServers: Array<[string, DNS2.DnsResolver]> = ([
const domainAliveMutex = createKeyedAsyncMutex('isDomainAlive');
export async function isDomainAlive(domain: string, isIncludeAllSubdomain: boolean = domain[0] === '.'): Promise<boolean> {
export async function isDomainAlive(
domain: string,
// we dont need to check domain[0] here, this is only from runAgainstSourceFile
isIncludeAllSubdomain: boolean
): Promise<boolean> {
if (domainAliveMap.has(domain)) {
return domainAliveMap.get(domain)!;
}
@@ -102,8 +106,6 @@ export async function isDomainAlive(domain: string, isIncludeAllSubdomain: boole
return domainAliveMutex.acquire(domain, async () => {
domain = domain[0] === '.' ? domain.slice(1) : domain;
const $domain = isIncludeAllSubdomain ? '.' + domain : domain;
const aDns: string[] = [];
const aaaaDns: string[] = [];
@@ -113,7 +115,7 @@ export async function isDomainAlive(domain: string, isIncludeAllSubdomain: boole
// eslint-disable-next-line no-await-in-loop -- sequential
const aRecords = (await $resolve(domain, 'A', servers[i]));
if (aRecords.answers.length > 0) {
domainAliveMap.set($domain, true);
domainAliveMap.set(domain, true);
return true;
}
@@ -123,7 +125,7 @@ export async function isDomainAlive(domain: string, isIncludeAllSubdomain: boole
// eslint-disable-next-line no-await-in-loop -- sequential
const aaaaRecords = (await $resolve(domain, 'AAAA', servers[i]));
if (aaaaRecords.answers.length > 0) {
domainAliveMap.set($domain, true);
domainAliveMap.set(domain, true);
return true;
}
@@ -135,7 +137,7 @@ export async function isDomainAlive(domain: string, isIncludeAllSubdomain: boole
// eslint-disable-next-line no-await-in-loop -- sequential
const aRecords = (await $resolve(domain, 'A', pickOne(domesticDohServers)));
if (aRecords.answers.length > 0) {
domainAliveMap.set($domain, true);
domainAliveMap.set(domain, true);
return true;
}
aDns.push(aRecords.dns);
@@ -144,7 +146,7 @@ export async function isDomainAlive(domain: string, isIncludeAllSubdomain: boole
// eslint-disable-next-line no-await-in-loop -- sequential
const aaaaRecords = (await $resolve(domain, 'AAAA', pickOne(domesticDohServers)));
if (aaaaRecords.answers.length > 0) {
domainAliveMap.set($domain, true);
domainAliveMap.set(domain, true);
return true;
}
aaaaDns.push(aaaaRecords.dns);
@@ -152,7 +154,7 @@ export async function isDomainAlive(domain: string, isIncludeAllSubdomain: boole
console.log(picocolors.red('[domain dead]'), 'no A/AAAA records', { domain, a: aDns, aaaa: aaaaDns });
domainAliveMap.set($domain, false);
domainAliveMap.set(domain, false);
return false;
});
}

View File

@@ -1,26 +0,0 @@
const notError = Symbol('notError');
export function createMemoizedPromise<T>(
fn: () => Promise<T>,
/** whether to create promise immediately or only create after first access */
preload = true
): () => Promise<T> {
let error: Error | typeof notError = notError;
let promise: Promise<T> | null = preload
? fn().catch(e => {
// Here we record the error so that we can throw it later when the function is called
error = e;
// Here we make sure the Promise still returns the never type
throw e;
})
: null;
return () => {
if (error !== notError) {
return Promise.reject(error);
}
promise ??= fn();
return promise;
};
}