ソースを参照

Perf: preload/hoist more promises, make ts happy

SukkaW 2 年 前
コミット
a3e1a85c70

+ 21 - 25
Build/build-anti-bogus-domain.ts

@@ -8,36 +8,32 @@ import { SHARED_DESCRIPTION } from './lib/constants';
 import { isProbablyIpv4, isProbablyIpv6 } from './lib/is-fast-ip';
 import { TTL, deserializeArray, fsCache, serializeArray } from './lib/cache-filesystem';
 
-const getBogusNxDomainIPs = async () => {
-  const url = 'https://raw.githubusercontent.com/felixonmars/dnsmasq-china-list/master/bogus-nxdomain.china.conf';
+const URL = 'https://raw.githubusercontent.com/felixonmars/dnsmasq-china-list/master/bogus-nxdomain.china.conf';
 
-  return fsCache.apply(
-    url,
-    async () => {
-      const result: string[] = [];
-      for await (const line of await fetchRemoteTextByLine('https://raw.githubusercontent.com/felixonmars/dnsmasq-china-list/master/bogus-nxdomain.china.conf')) {
-        if (line && line.startsWith('bogus-nxdomain=')) {
-          const ip = line.slice(15).trim();
-          if (isProbablyIpv4(ip)) {
-            result.push(`IP-CIDR,${ip}/32,no-resolve`);
-          } else if (isProbablyIpv6(ip)) {
-            result.push(`IP-CIDR6,${ip}/128,no-resolve`);
-          }
+const getBogusNxDomainIPsPromise = fsCache.apply(
+  URL,
+  async () => {
+    const result: string[] = [];
+    for await (const line of await fetchRemoteTextByLine(URL)) {
+      if (line && line.startsWith('bogus-nxdomain=')) {
+        const ip = line.slice(15).trim();
+        if (isProbablyIpv4(ip)) {
+          result.push(`IP-CIDR,${ip}/32,no-resolve`);
+        } else if (isProbablyIpv6(ip)) {
+          result.push(`IP-CIDR6,${ip}/128,no-resolve`);
         }
       }
-      return result;
-    },
-    {
-      ttl: TTL.ONE_WEEK(),
-      serializer: serializeArray,
-      deserializer: deserializeArray
     }
-  );
-};
+    return result;
+  },
+  {
+    ttl: TTL.ONE_WEEK(),
+    serializer: serializeArray,
+    deserializer: deserializeArray
+  }
+);
 
 export const buildAntiBogusDomain = task(import.meta.path, async (span) => {
-  const bogusIpPromise = getBogusNxDomainIPs();
-
   const result: string[] = [];
   for await (const line of readFileByLine(path.resolve(import.meta.dir, '../Source/ip/reject.conf'))) {
     const l = processLine(line);
@@ -46,7 +42,7 @@ export const buildAntiBogusDomain = task(import.meta.path, async (span) => {
     }
   }
 
-  result.push(...(await bogusIpPromise));
+  result.push(...(await getBogusNxDomainIPsPromise));
 
   const description = [
     ...SHARED_DESCRIPTION,

+ 8 - 14
Build/build-apple-cdn.ts

@@ -2,25 +2,19 @@
 import path from 'path';
 import { createRuleset } from './lib/create-file';
 import { parseFelixDnsmasq } from './lib/parse-dnsmasq';
-import { traceAsync } from './lib/trace-runner';
 import { task } from './trace';
 import { SHARED_DESCRIPTION } from './lib/constants';
-import picocolors from 'picocolors';
 import { createMemoizedPromise } from './lib/memo-promise';
 import { TTL, deserializeArray, fsCache, serializeArray } from './lib/cache-filesystem';
 
-export const getAppleCdnDomainsPromise = createMemoizedPromise(() => traceAsync(
-  picocolors.gray('download dnsmasq-china-list apple.china.conf'),
-  () => fsCache.apply(
-    'https://raw.githubusercontent.com/felixonmars/dnsmasq-china-list/master/apple.china.conf',
-    () => parseFelixDnsmasq('https://raw.githubusercontent.com/felixonmars/dnsmasq-china-list/master/apple.china.conf'),
-    {
-      ttl: TTL.THREE_DAYS(),
-      serializer: serializeArray,
-      deserializer: deserializeArray
-    }
-  ),
-  picocolors.gray
+export const getAppleCdnDomainsPromise = createMemoizedPromise(() => fsCache.apply(
+  'https://raw.githubusercontent.com/felixonmars/dnsmasq-china-list/master/apple.china.conf',
+  () => parseFelixDnsmasq('https://raw.githubusercontent.com/felixonmars/dnsmasq-china-list/master/apple.china.conf'),
+  {
+    ttl: TTL.THREE_DAYS(),
+    serializer: serializeArray,
+    deserializer: deserializeArray
+  }
 ));
 
 export const buildAppleCdn = task(import.meta.path, async (span) => {

+ 3 - 4
Build/build-cdn-conf.ts

@@ -6,7 +6,8 @@ import { task } from './trace';
 import { processLine } from './lib/process-line';
 import { SHARED_DESCRIPTION } from './lib/constants';
 import { getPublicSuffixListTextPromise } from './download-publicsuffixlist';
-const getS3OSSDomains = async (): Promise<Set<string>> => {
+
+const getS3OSSDomainsPromise = (async (): Promise<Set<string>> => {
   const trie = createTrie((await getPublicSuffixListTextPromise()).split('\n'));
 
   /**
@@ -39,14 +40,12 @@ const getS3OSSDomains = async (): Promise<Set<string>> => {
   });
 
   return S3OSSDomains;
-};
+})();
 
 const buildCdnConf = task(import.meta.path, async (span) => {
   /** @type {string[]} */
   const cdnDomainsList: string[] = [];
 
-  const getS3OSSDomainsPromise: Promise<Set<string>> = getS3OSSDomains();
-
   for await (const l of readFileByLine(path.resolve(import.meta.dir, '../Source/non_ip/cdn.conf'))) {
     const line = processLine(l);
     if (line) {

+ 28 - 26
Build/build-common.ts

@@ -125,30 +125,32 @@ async function transformDomainset(parentSpan: Span, sourcePath: string, relative
  * Output Surge RULE-SET and Clash classical text format
  */
 async function transformRuleset(parentSpan: Span, sourcePath: string, relativePath: string) {
-  const span = parentSpan.traceChild(`transform ruleset: ${path.basename(sourcePath, path.extname(sourcePath))}`);
-
-  const res = await processFile(span, sourcePath);
-  if (!res) return null;
-
-  const [title, descriptions, lines] = res;
-
-  const description = [
-    ...SHARED_DESCRIPTION,
-    ...(
-      descriptions.length
-        ? ['', ...descriptions]
-        : []
-    )
-  ];
-
-  return createRuleset(
-    span,
-    title,
-    description,
-    new Date(),
-    lines,
-    'ruleset',
-    path.resolve(outputSurgeDir, relativePath),
-    path.resolve(outputClashDir, `${relativePath.slice(0, -path.extname(relativePath).length)}.txt`)
-  );
+  return parentSpan
+    .traceChild(`transform ruleset: ${path.basename(sourcePath, path.extname(sourcePath))}`)
+    .traceAsyncFn(async (span) => {
+      const res = await processFile(span, sourcePath);
+      if (!res) return null;
+
+      const [title, descriptions, lines] = res;
+
+      const description = [
+        ...SHARED_DESCRIPTION,
+        ...(
+          descriptions.length
+            ? ['', ...descriptions]
+            : []
+        )
+      ];
+
+      return createRuleset(
+        span,
+        title,
+        description,
+        new Date(),
+        lines,
+        'ruleset',
+        path.resolve(outputSurgeDir, relativePath),
+        path.resolve(outputClashDir, `${relativePath.slice(0, -path.extname(relativePath).length)}.txt`)
+      );
+    });
 }

+ 2 - 0
Build/build-microsoft-cdn.ts

@@ -24,6 +24,7 @@ const BLACKLIST = [
 
 export const getMicrosoftCdnRulesetPromise = createMemoizedPromise(async () => {
   const set = await traceAsync('fetch accelerated-domains.china.conf', async () => {
+    // First trie is to find the microsoft domains that matches probe domains
     const trie = createTrie();
     for await (const line of await fetchRemoteTextByLine('https://raw.githubusercontent.com/felixonmars/dnsmasq-china-list/master/accelerated-domains.china.conf')) {
       if (line.startsWith('server=/') && line.endsWith('/114.114.114.114')) {
@@ -34,6 +35,7 @@ export const getMicrosoftCdnRulesetPromise = createMemoizedPromise(async () => {
     return new Set(PROBE_DOMAINS.flatMap(domain => trie.find(domain, false)));
   });
 
+  // Second trie is to remove blacklisted domains
   const trie2 = createTrie(set);
   const black = BLACKLIST.flatMap(domain => trie2.find(domain, true));
   for (let i = 0, len = black.length; i < len; i++) {

+ 22 - 15
Build/build-public.ts

@@ -16,22 +16,29 @@ const folderAndFilesToBeDeployed = [
   'LICENSE'
 ];
 
-export const buildPublic = task(import.meta.path, async () => {
-  const filesToBeCopied = (await listDir(
-    rootPath, {
-      ignoreHidden: true,
-      ignorePattern: /node_modules|Build|public/
-    }
-  )).filter(file => folderAndFilesToBeDeployed.some(folderOrFile => file.startsWith(folderOrFile)));
-
-  await Promise.all(filesToBeCopied.map(file => {
-    const src = path.resolve(rootPath, file);
-    const dest = path.resolve(publicPath, file);
-
-    return Bun.write(dest, Bun.file(src));
-  }));
+export const buildPublic = task(import.meta.path, async (span) => {
+  await span
+    .traceChild('copy public files')
+    .traceAsyncFn(async () => {
+      const filesToBeCopied = (await listDir(
+        rootPath, {
+          ignoreHidden: true,
+          ignorePattern: /node_modules|Build|public/
+        }
+      )).filter(file => folderAndFilesToBeDeployed.some(folderOrFile => file.startsWith(folderOrFile)));
+
+      return Promise.all(filesToBeCopied.map(file => {
+        const src = path.resolve(rootPath, file);
+        const dest = path.resolve(publicPath, file);
+
+        return Bun.write(dest, Bun.file(src));
+      }));
+    });
+
+  const html = await span
+    .traceChild('generate index.html')
+    .traceAsyncFn(() => treeDir(publicPath).then(generateHtml));
 
-  const html = generateHtml(await treeDir(publicPath));
   return Bun.write(path.join(publicPath, 'index.html'), html);
 });
 

+ 1 - 1
Build/lib/get-gorhill-publicsuffix.ts

@@ -3,7 +3,7 @@ import { traceAsync } from './trace-runner';
 import { createMemoizedPromise } from './memo-promise';
 import { getPublicSuffixListTextPromise } from '../download-publicsuffixlist';
 
-const customFetch = (url: string | URL) => Promise.resolve(Bun.file(url));
+const customFetch = (url: string | URL): Promise<Blob> => Promise.resolve(Bun.file(url));
 
 export const getGorhillPublicSuffixPromise = createMemoizedPromise(() => traceAsync('create gorhill public suffix instance', async () => {
   const [publicSuffixListDat, { default: gorhill }] = await Promise.all([