Browse Source

Refactor: some changes

SukkaW 1 year ago
parent
commit
85e42772ce
2 changed files with 18 additions and 13 deletions
  1. 2 5
      Build/build-deprecate-files.ts
  2. 16 8
      Build/build-speedtest-domainset.ts

+ 2 - 5
Build/build-deprecate-files.ts

@@ -28,9 +28,6 @@ export const buildDeprecateFiles = task(require.main === module, __filename)((sp
     ));
     ));
 
 
   for (const [filePath, description] of DEPRECATED_FILES) {
   for (const [filePath, description] of DEPRECATED_FILES) {
-    const surgeFile = path.resolve(OUTPUT_SURGE_DIR, `${filePath}.conf`);
-    const clashFile = path.resolve(OUTPUT_CLASH_DIR, `${filePath}.txt`);
-
     const content = [
     const content = [
       '#########################################',
       '#########################################',
       '# Sukka\'s Ruleset - Deprecated',
       '# Sukka\'s Ruleset - Deprecated',
@@ -39,8 +36,8 @@ export const buildDeprecateFiles = task(require.main === module, __filename)((sp
     ];
     ];
 
 
     promises.push(
     promises.push(
-      compareAndWriteFile(childSpan, content, surgeFile),
-      compareAndWriteFile(childSpan, content, clashFile)
+      compareAndWriteFile(childSpan, content, path.resolve(OUTPUT_SURGE_DIR, `${filePath}.conf`)),
+      compareAndWriteFile(childSpan, content, path.resolve(OUTPUT_CLASH_DIR, `${filePath}.txt`))
     );
     );
   }
   }
 
 

+ 16 - 8
Build/build-speedtest-domainset.ts

@@ -1,6 +1,5 @@
 import path from 'node:path';
 import path from 'node:path';
 
 
-import { Sema } from 'async-sema';
 import { getHostname } from 'tldts-experimental';
 import { getHostname } from 'tldts-experimental';
 import { task } from './trace';
 import { task } from './trace';
 import { $fetch } from './lib/make-fetch-happen';
 import { $fetch } from './lib/make-fetch-happen';
@@ -10,6 +9,7 @@ import { readFileIntoProcessedArray } from './lib/fetch-text-by-line';
 import { DomainsetOutput } from './lib/create-file';
 import { DomainsetOutput } from './lib/create-file';
 import { OUTPUT_SURGE_DIR } from './constants/dir';
 import { OUTPUT_SURGE_DIR } from './constants/dir';
 import { createMemoizedPromise } from './lib/memo-promise';
 import { createMemoizedPromise } from './lib/memo-promise';
+import { Sema } from 'async-sema';
 
 
 const KEYWORDS = [
 const KEYWORDS = [
   'Hong Kong',
   'Hong Kong',
@@ -142,7 +142,7 @@ const latestTopUserAgentsPromise = $fetch('https://cdn.jsdelivr.net/npm/top-user
   .then(res => res.json())
   .then(res => res.json())
   .then((userAgents: string[]) => userAgents.filter(ua => ua.startsWith('Mozilla/5.0 ')));
   .then((userAgents: string[]) => userAgents.filter(ua => ua.startsWith('Mozilla/5.0 ')));
 
 
-async function querySpeedtestApi(keyword: string): Promise<Array<string | null>> {
+async function querySpeedtestApi(keyword: string) {
   const topUserAgents = await latestTopUserAgentsPromise;
   const topUserAgents = await latestTopUserAgentsPromise;
 
 
   const url = `https://www.speedtest.net/api/js/servers?engine=js&search=${keyword}&limit=100`;
   const url = `https://www.speedtest.net/api/js/servers?engine=js&search=${keyword}&limit=100`;
@@ -150,7 +150,9 @@ async function querySpeedtestApi(keyword: string): Promise<Array<string | null>>
   try {
   try {
     const randomUserAgent = topUserAgents[Math.floor(Math.random() * topUserAgents.length)];
     const randomUserAgent = topUserAgents[Math.floor(Math.random() * topUserAgents.length)];
 
 
-    return await s.acquire().then(() => $fetch(url, {
+    await s.acquire();
+
+    const r = await $fetch(url, {
       headers: {
       headers: {
         dnt: '1',
         dnt: '1',
         Referer: 'https://www.speedtest.net/',
         Referer: 'https://www.speedtest.net/',
@@ -168,19 +170,25 @@ async function querySpeedtestApi(keyword: string): Promise<Array<string | null>>
           : {})
           : {})
       },
       },
       timeout: 1000 * 60
       timeout: 1000 * 60
-    })).then(r => r.json() as any).then((data: Array<{ url: string, host: string }>) => data.reduce<string[]>(
+    });
+
+    const data: Array<{ url: string, host: string }> = await r.json();
+
+    return data.reduce<string[]>(
       (prev, cur) => {
       (prev, cur) => {
-        const line = cur.host || cur.url;
-        const hn = getHostname(line, { detectIp: false, validateHostname: true });
+        const hn = getHostname(cur.host || cur.url, { detectIp: false, validateHostname: true });
         if (hn) {
         if (hn) {
           prev.push(hn);
           prev.push(hn);
         }
         }
         return prev;
         return prev;
-      }, []
-    )).finally(() => s.release());
+      },
+      []
+    );
   } catch (e) {
   } catch (e) {
     console.error(e);
     console.error(e);
     return [];
     return [];
+  } finally {
+    s.release();
   }
   }
 }
 }