Perf/Refactor: processHosts now returns string[]

This commit is contained in:
SukkaW 2024-05-26 18:22:44 +08:00
parent aa3cb9e586
commit 6b0151be29
3 changed files with 8 additions and 11 deletions

View File

@ -36,7 +36,7 @@ export const buildRejectDomainSet = task(import.meta.path, async (span) => {
let shouldStop = false; let shouldStop = false;
await Promise.all([ await Promise.all([
// Parse from remote hosts & domain lists // Parse from remote hosts & domain lists
...HOSTS.map(entry => processHosts(childSpan, entry[0], entry[1], entry[2], entry[3]).then(hosts => SetAdd(domainSets, hosts))), ...HOSTS.map(entry => processHosts(childSpan, entry[0], entry[1], entry[2], entry[3]).then(setAddFromArrayCurried(domainSets))),
...DOMAIN_LISTS.map(entry => processDomainLists(childSpan, entry[0], entry[1], entry[2]).then(setAddFromArrayCurried(domainSets))), ...DOMAIN_LISTS.map(entry => processDomainLists(childSpan, entry[0], entry[1], entry[2]).then(setAddFromArrayCurried(domainSets))),

View File

@ -21,6 +21,5 @@ export const normalizeDomain = (domain: string) => {
h = h.slice(sliceStart, sliceEnd); h = h.slice(sliceStart, sliceEnd);
} }
if (h) return h; return h || null;
return null;
}; };

View File

@ -47,7 +47,7 @@ export function processDomainLists(span: Span, domainListsUrl: string, includeAl
)); ));
} }
const hostsLineCb = (l: string, set: Set<string>, includeAllSubDomain: boolean, meta: string) => { const hostsLineCb = (l: string, set: string[], includeAllSubDomain: boolean, meta: string) => {
const line = processLine(l); const line = processLine(l);
if (!line) { if (!line) {
return; return;
@ -66,15 +66,15 @@ const hostsLineCb = (l: string, set: Set<string>, includeAllSubDomain: boolean,
foundDebugDomain = true; foundDebugDomain = true;
} }
set.add(includeAllSubDomain ? `.${domain}` : domain); set.push(includeAllSubDomain ? `.${domain}` : domain);
}; };
export function processHosts(span: Span, hostsUrl: string, mirrors: string[] | null, includeAllSubDomain = false, ttl: number | null = null) { export function processHosts(span: Span, hostsUrl: string, mirrors: string[] | null, includeAllSubDomain = false, ttl: number | null = null) {
const domainSets = new Set<string>();
return span.traceChild(`processhosts: ${hostsUrl}`).traceAsyncFn((childSpan) => fsFetchCache.apply( return span.traceChild(`processhosts: ${hostsUrl}`).traceAsyncFn((childSpan) => fsFetchCache.apply(
hostsUrl, hostsUrl,
async () => { async () => {
const domainSets: string[] = [];
if (mirrors == null || mirrors.length === 0) { if (mirrors == null || mirrors.length === 0) {
for await (const l of await fetchRemoteTextByLine(hostsUrl)) { for await (const l of await fetchRemoteTextByLine(hostsUrl)) {
hostsLineCb(l, domainSets, includeAllSubDomain, hostsUrl); hostsLineCb(l, domainSets, includeAllSubDomain, hostsUrl);
@ -91,15 +91,13 @@ export function processHosts(span: Span, hostsUrl: string, mirrors: string[] | n
}); });
} }
console.log(picocolors.gray('[process hosts]'), picocolors.gray(hostsUrl), picocolors.gray(domainSets.size));
return domainSets; return domainSets;
}, },
{ {
ttl, ttl,
temporaryBypass, temporaryBypass,
serializer: serializeSet, serializer: serializeArray,
deserializer: deserializeSet deserializer: deserializeArray
} }
)); ));
} }