Fix(#32): validate existing hostnames

This commit is contained in:
SukkaW 2024-07-07 17:03:02 +08:00
parent 11f810afe9
commit eb3c0c86d8

View File

@ -11,7 +11,6 @@ import { SHARED_DESCRIPTION } from './lib/constants';
import picocolors from 'picocolors'; import picocolors from 'picocolors';
import { readFileIntoProcessedArray } from './lib/fetch-text-by-line'; import { readFileIntoProcessedArray } from './lib/fetch-text-by-line';
import { TTL, deserializeArray, fsFetchCache, serializeArray } from './lib/cache-filesystem'; import { TTL, deserializeArray, fsFetchCache, serializeArray } from './lib/cache-filesystem';
import { createMemoizedPromise } from './lib/memo-promise';
import { createTrie } from './lib/trie'; import { createTrie } from './lib/trie';
@ -62,7 +61,7 @@ const querySpeedtestApi = async (keyword: string): Promise<Array<string | null>>
} }
})).then(r => r.json() as any).then((data: Array<{ url: string }>) => data.reduce<string[]>( })).then(r => r.json() as any).then((data: Array<{ url: string }>) => data.reduce<string[]>(
(prev, cur) => { (prev, cur) => {
const hn = getHostname(cur.url, { detectIp: false }); const hn = getHostname(cur.url, { detectIp: false, validateHostname: true });
if (hn) { if (hn) {
prev.push(hn); prev.push(hn);
} }
@ -81,14 +80,6 @@ const querySpeedtestApi = async (keyword: string): Promise<Array<string | null>>
} }
}; };
const getPreviousSpeedtestDomainsPromise = createMemoizedPromise(async () => {
try {
return await readFileIntoProcessedArray(path.resolve(import.meta.dir, '../List/domainset/speedtest.conf'));
} catch {
return [];
}
});
export const buildSpeedtestDomainSet = task(import.meta.main, import.meta.path)(async (span) => { export const buildSpeedtestDomainSet = task(import.meta.main, import.meta.path)(async (span) => {
const domainTrie = createTrie( const domainTrie = createTrie(
[ [
@ -185,8 +176,18 @@ export const buildSpeedtestDomainSet = task(import.meta.main, import.meta.path)(
await span.traceChildAsync( await span.traceChildAsync(
'fetch previous speedtest domainset', 'fetch previous speedtest domainset',
() => getPreviousSpeedtestDomainsPromise() async () => {
.then(prevDomains => prevDomains.forEach(domainTrie.add)) try {
const contents = await readFileIntoProcessedArray(path.resolve(import.meta.dir, '../List/domainset/speedtest.conf'));
contents.reduce<string[]>((acc, line) => {
const hn = getHostname(line, { detectIp: false, validateHostname: true });
if (hn) {
acc.push(hn);
}
return acc;
}, []).forEach(domainTrie.add);
} catch { }
}
); );
await new Promise<void>((resolve, reject) => { await new Promise<void>((resolve, reject) => {