From 85e42772cee5795cc340a5d143b953373a4ff191 Mon Sep 17 00:00:00 2001 From: SukkaW Date: Sat, 9 Nov 2024 03:18:49 +0800 Subject: [PATCH] Refactor: some changes --- Build/build-deprecate-files.ts | 7 ++----- Build/build-speedtest-domainset.ts | 24 ++++++++++++++++-------- 2 files changed, 18 insertions(+), 13 deletions(-) diff --git a/Build/build-deprecate-files.ts b/Build/build-deprecate-files.ts index 11249db0..eea03f2b 100644 --- a/Build/build-deprecate-files.ts +++ b/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) { - const surgeFile = path.resolve(OUTPUT_SURGE_DIR, `${filePath}.conf`); - const clashFile = path.resolve(OUTPUT_CLASH_DIR, `${filePath}.txt`); - const content = [ '#########################################', '# Sukka\'s Ruleset - Deprecated', @@ -39,8 +36,8 @@ export const buildDeprecateFiles = task(require.main === module, __filename)((sp ]; 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`)) ); } diff --git a/Build/build-speedtest-domainset.ts b/Build/build-speedtest-domainset.ts index 22a039aa..e7ed62d4 100644 --- a/Build/build-speedtest-domainset.ts +++ b/Build/build-speedtest-domainset.ts @@ -1,6 +1,5 @@ import path from 'node:path'; -import { Sema } from 'async-sema'; import { getHostname } from 'tldts-experimental'; import { task } from './trace'; 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 { OUTPUT_SURGE_DIR } from './constants/dir'; import { createMemoizedPromise } from './lib/memo-promise'; +import { Sema } from 'async-sema'; const KEYWORDS = [ 'Hong Kong', @@ -142,7 +142,7 @@ const latestTopUserAgentsPromise = $fetch('https://cdn.jsdelivr.net/npm/top-user .then(res => res.json()) .then((userAgents: string[]) => userAgents.filter(ua => ua.startsWith('Mozilla/5.0 '))); -async function querySpeedtestApi(keyword: string): Promise> { +async function querySpeedtestApi(keyword: string) { const topUserAgents = await latestTopUserAgentsPromise; 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> try { 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: { dnt: '1', Referer: 'https://www.speedtest.net/', @@ -168,19 +170,25 @@ async function querySpeedtestApi(keyword: string): Promise> : {}) }, timeout: 1000 * 60 - })).then(r => r.json() as any).then((data: Array<{ url: string, host: string }>) => data.reduce( + }); + + const data: Array<{ url: string, host: string }> = await r.json(); + + return data.reduce( (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) { prev.push(hn); } return prev; - }, [] - )).finally(() => s.release()); + }, + [] + ); } catch (e) { console.error(e); return []; + } finally { + s.release(); } }