瀏覽代碼

Chore/Perf: fetch enable HTTP/2

SukkaW 1 年之前
父節點
當前提交
de9429ce92

+ 9 - 2
Build/download-previous-build.ts

@@ -1,4 +1,5 @@
 import path from 'node:path';
+import fs from 'node:fs';
 import { pipeline } from 'node:stream/promises';
 import { task } from './trace';
 import { defaultRequestInit, fetchWithRetry } from './lib/fetch-retry';
@@ -6,11 +7,19 @@ import { extract as tarExtract } from 'tar-fs';
 import type { Headers as TarEntryHeaders } from 'tar-fs';
 import zlib from 'node:zlib';
 import { Readable } from 'node:stream';
+import picocolors from 'picocolors';
 
 const GITHUB_CODELOAD_URL = 'https://codeload.github.com/sukkalab/ruleset.skk.moe/tar.gz/master';
 const GITLAB_CODELOAD_URL = 'https://gitlab.com/SukkaW/ruleset.skk.moe/-/archive/master/ruleset.skk.moe-master.tar.gz';
 
 export const downloadPreviousBuild = task(require.main === module, __filename)(async (span) => {
+  const publicDir = path.resolve(__dirname, '..', 'public');
+
+  if (fs.existsSync(publicDir)) {
+    console.log(picocolors.blue('Public directory exists, skip downloading previous build'));
+    return;
+  }
+
   const tarGzUrl = await span.traceChildAsync('get tar.gz url', async () => {
     const resp = await fetchWithRetry(GITHUB_CODELOAD_URL, {
       ...defaultRequestInit,
@@ -27,8 +36,6 @@ export const downloadPreviousBuild = task(require.main === module, __filename)(a
     return GITHUB_CODELOAD_URL;
   });
 
-  const publicDir = path.resolve(__dirname, '..', 'public');
-
   return span.traceChildAsync('download & extract previoud build', async () => {
     const resp = await fetchWithRetry(tarGzUrl, {
       headers: {

+ 2 - 1
Build/lib/convert-clash-meta-mrs.ts

@@ -8,6 +8,7 @@ import process from 'node:process';
 
 import { async as ezspawn } from '@jsdevtools/ez-spawn';
 import { mkdirp } from './misc';
+import { fetchWithRetry } from './fetch-retry';
 
 const mihomoBinaryDir = path.join(__dirname, '../../node_modules/.cache/mihomo');
 const mihomoBinaryPath = path.join(mihomoBinaryDir, 'mihomo');
@@ -32,7 +33,7 @@ const ensureMihomoBinary = async () => {
       throw new Error(`Unsupported platform: ${process.platform} ${process.arch}`);
     }
 
-    const res = await fetch(downloadUrl);
+    const res = await fetchWithRetry(downloadUrl);
 
     if (!res.ok || !res.body) {
       throw new Error(`Failed to download mihomo binary: ${res.statusText}`);

+ 4 - 0
Build/lib/fetch-retry.ts

@@ -2,6 +2,10 @@ import retry from 'async-retry';
 import picocolors from 'picocolors';
 import { setTimeout } from 'node:timers/promises';
 
+import { setGlobalDispatcher, Agent } from 'undici';
+
+setGlobalDispatcher(new Agent({ allowH2: true }));
+
 function isClientError(err: unknown): err is NodeJS.ErrnoException {
   if (!err || typeof err !== 'object') return false;
 

+ 2 - 1
Build/validate-domestic.ts

@@ -5,13 +5,14 @@ import path from 'node:path';
 import { processLine } from './lib/process-line';
 import { parseFelixDnsmasq } from './lib/parse-dnsmasq';
 import { SOURCE_DIR } from './constants/dir';
+import { fetchWithRetry } from './lib/fetch-retry';
 
 export const parseDomesticList = async () => {
   const trie = createTrie(await parseFelixDnsmasq('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 fetchWithRetry('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',

+ 4 - 3
Build/validate-gfwlist.ts

@@ -6,12 +6,13 @@ import { parse } from 'csv-parse/sync';
 import { readFileByLine } from './lib/fetch-text-by-line';
 import path from 'node:path';
 import { SOURCE_DIR } from './constants/dir';
+import { fetchWithRetry } from './lib/fetch-retry';
 
 export const parseGfwList = async () => {
   const whiteSet = new Set<string>();
   const blackSet = new Set<string>();
 
-  const text = await (await fetch('https://raw.githubusercontent.com/gfwlist/gfwlist/master/gfwlist.txt')).text();
+  const text = await (await fetchWithRetry('https://raw.githubusercontent.com/gfwlist/gfwlist/master/gfwlist.txt')).text();
   for (const l of atob(text).split('\n')) {
     const line = processLine(l);
     if (!line) continue;
@@ -54,13 +55,13 @@ export const parseGfwList = async () => {
       continue;
     }
   }
-  for (const l of (await (await fetch('https://raw.githubusercontent.com/Loyalsoldier/cn-blocked-domain/release/domains.txt')).text()).split('\n')) {
+  for (const l of (await (await fetchWithRetry('https://raw.githubusercontent.com/Loyalsoldier/cn-blocked-domain/release/domains.txt')).text()).split('\n')) {
     blackSet.add(l);
   }
 
   const top500Gfwed = new Set<string>();
 
-  const res = await (await fetch('https://radar.cloudflare.com/charts/LargerTopDomainsTable/attachment?id=1077&top=10000', {
+  const res = await (await fetchWithRetry('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',

+ 1 - 0
package.json

@@ -39,6 +39,7 @@
     "tar-fs": "^3.0.6",
     "tldts": "^6.1.50",
     "tldts-experimental": "^6.1.50",
+    "undici": "^6.19.8",
     "yaml": "^2.5.1"
   },
   "devDependencies": {

+ 9 - 0
pnpm-lock.yaml

@@ -68,6 +68,9 @@ importers:
       tldts-experimental:
         specifier: ^6.1.50
         version: 6.1.50
+      undici:
+        specifier: ^6.19.8
+        version: 6.19.8
       yaml:
         specifier: ^2.5.1
         version: 2.5.1
@@ -1490,6 +1493,10 @@ packages:
   undici-types@5.26.5:
     resolution: {integrity: sha512-JlCMO+ehdEIKqlFxk6IfVoAUVmgz7cU7zD/h9XZ0qzeosSHmUJVOzSQvvYSYWXkFXC+IfLKSIffhv0sVZup6pA==}
 
+  undici@6.19.8:
+    resolution: {integrity: sha512-U8uCCl2x9TK3WANvmBavymRzxbfFYG+tAu+fgx3zxQy3qdagQqBLwJVrdyO1TBfUXvfKveMKJZhpvUYoOjM+4g==}
+    engines: {node: '>=18.17'}
+
   uri-js@4.4.1:
     resolution: {integrity: sha512-7rKUyy33Q1yc98pQ1DAmLtwX109F7TIfWlW1Ydo8Wl1ii1SeHieeh0HHfPeL2fMXK6z0s8ecKs9frCuLJvndBg==}
 
@@ -2988,6 +2995,8 @@ snapshots:
 
   undici-types@5.26.5: {}
 
+  undici@6.19.8: {}
+
   uri-js@4.4.1:
     dependencies:
       punycode: 2.3.1