Use undici fetch

This commit is contained in:
SukkaW 2025-01-10 00:50:54 +08:00
parent 0c679645e7
commit 5e40f2cbe0
8 changed files with 22 additions and 23 deletions

View File

@ -3,9 +3,9 @@ import { task } from './trace';
import { SHARED_DESCRIPTION } from './constants/description';
import { createMemoizedPromise } from './lib/memo-promise';
import { DomainsetOutput } from './lib/create-file';
import { $fetch } from './lib/make-fetch-happen';
import { $$fetch } from './lib/fetch-retry';
export const getAppleCdnDomainsPromise = createMemoizedPromise(() => $fetch('https://raw.githubusercontent.com/felixonmars/dnsmasq-china-list/master/apple.china.conf').then(parseFelixDnsmasqFromResp));
export const getAppleCdnDomainsPromise = createMemoizedPromise(() => $$fetch('https://raw.githubusercontent.com/felixonmars/dnsmasq-china-list/master/apple.china.conf').then(parseFelixDnsmasqFromResp));
export const buildAppleCdn = task(require.main === module, __filename)(async (span) => {
const res: string[] = await span.traceChildPromise('get apple cdn domains', getAppleCdnDomainsPromise());

View File

@ -8,10 +8,10 @@ import { fsFetchCache, getFileContentHash } from './lib/cache-filesystem';
import { processLine } from './lib/process-line';
import { RulesetOutput } from './lib/create-file';
import { SOURCE_DIR } from './constants/dir';
import { $fetch } from './lib/make-fetch-happen';
import { $$fetch } from './lib/fetch-retry';
const BOGUS_NXDOMAIN_URL = 'https://raw.githubusercontent.com/felixonmars/dnsmasq-china-list/master/bogus-nxdomain.china.conf';
const getBogusNxDomainIPsPromise: Promise<[ipv4: string[], ipv6: string[]]> = $fetch(BOGUS_NXDOMAIN_URL).then(async (resp) => {
const getBogusNxDomainIPsPromise: Promise<[ipv4: string[], ipv6: string[]]> = $$fetch(BOGUS_NXDOMAIN_URL).then(async (resp) => {
const ipv4: string[] = [];
const ipv6: string[] = [];

View File

@ -2,13 +2,13 @@ import path from 'node:path';
import tldts from 'tldts-experimental';
import { task } from './trace';
import { $fetch } from './lib/make-fetch-happen';
import { SHARED_DESCRIPTION } from './constants/description';
import { readFileIntoProcessedArray } from './lib/fetch-text-by-line';
import { DomainsetOutput } from './lib/create-file';
import { OUTPUT_SURGE_DIR, SOURCE_DIR } from './constants/dir';
import { newQueue } from '@henrygd/queue';
import { $$fetch } from './lib/fetch-retry';
const KEYWORDS = [
'Hong Kong',
@ -44,6 +44,9 @@ const KEYWORDS = [
const s = newQueue(2);
const latestTopUserAgentsPromise = $$fetch('https://raw.githubusercontent.com/microlinkhq/top-user-agents/master/src/desktop.json')
.then(res => res.json() as Promise<string[]>)
.then((userAgents: string[]) => userAgents.filter(ua => ua.startsWith('Mozilla/5.0 ')));
const getSpeedtestHostsGroupsPromise = Promise.all(KEYWORDS.flatMap(querySpeedtestApi));
export const buildSpeedtestDomainSet = task(require.main === module, __filename)(async (span) => {
@ -74,10 +77,6 @@ export const buildSpeedtestDomainSet = task(require.main === module, __filename)
return output.write();
});
const latestTopUserAgentsPromise = $fetch('https://raw.githubusercontent.com/microlinkhq/top-user-agents/master/src/desktop.json')
.then(res => res.json())
.then((userAgents: string[]) => userAgents.filter(ua => ua.startsWith('Mozilla/5.0 ')));
async function querySpeedtestApi(keyword: string) {
const topUserAgents = await latestTopUserAgentsPromise;
@ -86,7 +85,7 @@ async function querySpeedtestApi(keyword: string) {
try {
const randomUserAgent = topUserAgents[Math.floor(Math.random() * topUserAgents.length)];
const data = await s.add<Array<{ url: string, host: string }>>(() => $fetch(url, {
const data = await s.add<Array<{ url: string, host: string }>>(() => $$fetch(url, {
headers: {
dnt: '1',
Referer: 'https://www.speedtest.net/',
@ -103,8 +102,8 @@ async function querySpeedtestApi(keyword: string) {
}
: {})
},
timeout: 1000 * 60
}).then(res => res.json()));
signal: AbortSignal.timeout(1000 * 60)
}).then(res => res.json() as any));
return data.reduce<string[]>(
(prev, cur) => {

View File

@ -5,10 +5,10 @@ import { task } from './trace';
import { SHARED_DESCRIPTION } from './constants/description';
import { createMemoizedPromise } from './lib/memo-promise';
import { RulesetOutput } from './lib/create-file';
import { $fetch } from './lib/make-fetch-happen';
import { $$fetch } from './lib/fetch-retry';
export const getTelegramCIDRPromise = createMemoizedPromise(async () => {
const resp = await $fetch('https://core.telegram.org/resources/cidr.txt');
const resp = await $$fetch('https://core.telegram.org/resources/cidr.txt');
const lastModified = resp.headers.get('last-modified');
const date = lastModified ? new Date(lastModified) : new Date();

View File

@ -4,7 +4,7 @@ import fs from 'node:fs';
import { pipeline } from 'node:stream/promises';
import { OUTPUT_MOCK_DIR } from './constants/dir';
import { mkdirp } from './lib/misc';
import { $fetch } from './lib/make-fetch-happen';
import { $$fetch } from './lib/fetch-retry';
const ASSETS_LIST = {
'www-google-analytics-com_ga.js': 'https://unpkg.com/@adguard/scriptlets@latest/dist/redirect-files/google-analytics-ga.js',
@ -23,7 +23,7 @@ export const downloadMockAssets = task(require.main === module, __filename)(asyn
return Promise.all(Object.entries(ASSETS_LIST).map(
([filename, url]) => span
.traceChildAsync(url, async () => {
const res = await $fetch(url);
const res = await $$fetch(url);
if (!res.ok) {
console.error(`Failed to download ${url}`);

View File

@ -7,7 +7,7 @@ import process from 'node:process';
import { exec } from 'tinyexec';
import { mkdirp } from './misc';
import { $fetch } from './make-fetch-happen';
import { $$fetch } from './fetch-retry';
const mihomoBinaryDir = path.join(__dirname, '../../node_modules/.cache/mihomo');
const mihomoBinaryPath = path.join(mihomoBinaryDir, 'mihomo');
@ -32,7 +32,7 @@ async function ensureMihomoBinary() {
throw new Error(`Unsupported platform: ${process.platform} ${process.arch}`);
}
const res = await $fetch(downloadUrl);
const res = await $$fetch(downloadUrl);
if (!res.ok || !res.body) {
throw new Error(`Failed to download mihomo binary: ${res.statusText}`);

View File

@ -8,8 +8,8 @@ import { TextLineStream } from './text-line-transform-stream';
import type { ReadableStream } from 'node:stream/web';
import { TextDecoderStream } from 'node:stream/web';
import { processLine, ProcessLineStream } from './process-line';
import { $fetch } from './make-fetch-happen';
import type { NodeFetchResponse } from './make-fetch-happen';
import { $$fetch } from './fetch-retry';
import type { UndiciResponseData } from './fetch-retry';
import type { Response as UnidiciWebResponse } from 'undici';
@ -70,7 +70,7 @@ export const createReadlineInterfaceFromResponse: ((resp: NodeFetchResponse | Un
};
export function fetchRemoteTextByLine(url: string, processLine = false): Promise<AsyncIterable<string>> {
return $fetch(url).then(resp => createReadlineInterfaceFromResponse(resp, processLine));
return $$fetch(url).then(resp => createReadlineInterfaceFromResponse(resp, processLine));
}
export async function readFileIntoProcessedArray(file: string /* | FileHandle */) {

View File

@ -4,15 +4,15 @@ import { HostnameSmolTrie } from './lib/trie';
import path from 'node:path';
import { processLine } from './lib/process-line';
import { SOURCE_DIR } from './constants/dir';
import { $fetch } from './lib/make-fetch-happen';
import { parseFelixDnsmasqFromResp } from './lib/parse-dnsmasq';
import { $$fetch } from './lib/fetch-retry';
export async function parseDomesticList() {
const trie = new HostnameSmolTrie(await parseFelixDnsmasqFromResp(await $fetch('https://raw.githubusercontent.com/felixonmars/dnsmasq-china-list/master/accelerated-domains.china.conf')));
const trie = new HostnameSmolTrie(await parseFelixDnsmasqFromResp(await $$fetch('https://raw.githubusercontent.com/felixonmars/dnsmasq-china-list/master/accelerated-domains.china.conf')));
const top5000 = new Set<string>();
const res = await (await $fetch('https://radar.cloudflare.com/charts/LargerTopDomainsTable/attachment?id=1077&top=10000', {
const res = await (await $$fetch('https://radar.cloudflare.com/charts/LargerTopDomainsTable/attachment?id=1077&top=10000', {
headers: {
accept: 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7',
'accept-language': 'en-US,en;q=0.9,zh-CN;q=0.8,zh;q=0.7,zh-TW;q=0.6,es;q=0.5',