Skip to content

Commit

Permalink
Fix(#32): validate existing hostnames
Browse files Browse the repository at this point in the history
  • Loading branch information
SukkaW committed Jul 7, 2024
1 parent 11f810a commit eb3c0c8
Showing 1 changed file with 13 additions and 12 deletions.
25 changes: 13 additions & 12 deletions Build/build-speedtest-domainset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import { SHARED_DESCRIPTION } from './lib/constants';
import picocolors from 'picocolors';
import { readFileIntoProcessedArray } from './lib/fetch-text-by-line';
import { TTL, deserializeArray, fsFetchCache, serializeArray } from './lib/cache-filesystem';
import { createMemoizedPromise } from './lib/memo-promise';

import { createTrie } from './lib/trie';

Expand Down Expand Up @@ -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[]>(
(prev, cur) => {
const hn = getHostname(cur.url, { detectIp: false });
const hn = getHostname(cur.url, { detectIp: false, validateHostname: true });
if (hn) {
prev.push(hn);
}
Expand All @@ -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) => {
const domainTrie = createTrie(
[
Expand Down Expand Up @@ -185,8 +176,18 @@ export const buildSpeedtestDomainSet = task(import.meta.main, import.meta.path)(

await span.traceChildAsync(
'fetch previous speedtest domainset',
() => getPreviousSpeedtestDomainsPromise()
.then(prevDomains => prevDomains.forEach(domainTrie.add))
async () => {
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) => {
Expand Down

0 comments on commit eb3c0c8

Please sign in to comment.