浏览代码

Perf/Refactor: `processHosts` now returns `string[]`

SukkaW 1 年之前
父节点
当前提交
6b0151be29
共有 3 个文件被更改,包括 8 次插入11 次删除
  1. 1 1
      Build/build-reject-domainset.ts
  2. 1 2
      Build/lib/normalize-domain.ts
  3. 6 8
      Build/lib/parse-filter.ts

+ 1 - 1
Build/build-reject-domainset.ts

@@ -36,7 +36,7 @@ export const buildRejectDomainSet = task(import.meta.path, async (span) => {
       let shouldStop = false;
       await Promise.all([
         // 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))),
 

+ 1 - 2
Build/lib/normalize-domain.ts

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

+ 6 - 8
Build/lib/parse-filter.ts

@@ -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);
   if (!line) {
     return;
@@ -66,15 +66,15 @@ const hostsLineCb = (l: string, set: Set<string>, includeAllSubDomain: boolean,
     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) {
-  const domainSets = new Set<string>();
-
   return span.traceChild(`processhosts: ${hostsUrl}`).traceAsyncFn((childSpan) => fsFetchCache.apply(
     hostsUrl,
     async () => {
+      const domainSets: string[] = [];
+
       if (mirrors == null || mirrors.length === 0) {
         for await (const l of await fetchRemoteTextByLine(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;
     },
     {
       ttl,
       temporaryBypass,
-      serializer: serializeSet,
-      deserializer: deserializeSet
+      serializer: serializeArray,
+      deserializer: deserializeArray
     }
   ));
 }