mirror of
https://github.com/SukkaW/Surge.git
synced 2025-12-12 17:20:35 +08:00
Chore/Perf: fetch enable HTTP/2
This commit is contained in:
parent
92985a409d
commit
de9429ce92
@ -1,4 +1,5 @@
|
|||||||
import path from 'node:path';
|
import path from 'node:path';
|
||||||
|
import fs from 'node:fs';
|
||||||
import { pipeline } from 'node:stream/promises';
|
import { pipeline } from 'node:stream/promises';
|
||||||
import { task } from './trace';
|
import { task } from './trace';
|
||||||
import { defaultRequestInit, fetchWithRetry } from './lib/fetch-retry';
|
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 type { Headers as TarEntryHeaders } from 'tar-fs';
|
||||||
import zlib from 'node:zlib';
|
import zlib from 'node:zlib';
|
||||||
import { Readable } from 'node:stream';
|
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 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';
|
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) => {
|
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 tarGzUrl = await span.traceChildAsync('get tar.gz url', async () => {
|
||||||
const resp = await fetchWithRetry(GITHUB_CODELOAD_URL, {
|
const resp = await fetchWithRetry(GITHUB_CODELOAD_URL, {
|
||||||
...defaultRequestInit,
|
...defaultRequestInit,
|
||||||
@ -27,8 +36,6 @@ export const downloadPreviousBuild = task(require.main === module, __filename)(a
|
|||||||
return GITHUB_CODELOAD_URL;
|
return GITHUB_CODELOAD_URL;
|
||||||
});
|
});
|
||||||
|
|
||||||
const publicDir = path.resolve(__dirname, '..', 'public');
|
|
||||||
|
|
||||||
return span.traceChildAsync('download & extract previoud build', async () => {
|
return span.traceChildAsync('download & extract previoud build', async () => {
|
||||||
const resp = await fetchWithRetry(tarGzUrl, {
|
const resp = await fetchWithRetry(tarGzUrl, {
|
||||||
headers: {
|
headers: {
|
||||||
|
|||||||
@ -8,6 +8,7 @@ import process from 'node:process';
|
|||||||
|
|
||||||
import { async as ezspawn } from '@jsdevtools/ez-spawn';
|
import { async as ezspawn } from '@jsdevtools/ez-spawn';
|
||||||
import { mkdirp } from './misc';
|
import { mkdirp } from './misc';
|
||||||
|
import { fetchWithRetry } from './fetch-retry';
|
||||||
|
|
||||||
const mihomoBinaryDir = path.join(__dirname, '../../node_modules/.cache/mihomo');
|
const mihomoBinaryDir = path.join(__dirname, '../../node_modules/.cache/mihomo');
|
||||||
const mihomoBinaryPath = path.join(mihomoBinaryDir, 'mihomo');
|
const mihomoBinaryPath = path.join(mihomoBinaryDir, 'mihomo');
|
||||||
@ -32,7 +33,7 @@ const ensureMihomoBinary = async () => {
|
|||||||
throw new Error(`Unsupported platform: ${process.platform} ${process.arch}`);
|
throw new Error(`Unsupported platform: ${process.platform} ${process.arch}`);
|
||||||
}
|
}
|
||||||
|
|
||||||
const res = await fetch(downloadUrl);
|
const res = await fetchWithRetry(downloadUrl);
|
||||||
|
|
||||||
if (!res.ok || !res.body) {
|
if (!res.ok || !res.body) {
|
||||||
throw new Error(`Failed to download mihomo binary: ${res.statusText}`);
|
throw new Error(`Failed to download mihomo binary: ${res.statusText}`);
|
||||||
|
|||||||
@ -2,6 +2,10 @@ import retry from 'async-retry';
|
|||||||
import picocolors from 'picocolors';
|
import picocolors from 'picocolors';
|
||||||
import { setTimeout } from 'node:timers/promises';
|
import { setTimeout } from 'node:timers/promises';
|
||||||
|
|
||||||
|
import { setGlobalDispatcher, Agent } from 'undici';
|
||||||
|
|
||||||
|
setGlobalDispatcher(new Agent({ allowH2: true }));
|
||||||
|
|
||||||
function isClientError(err: unknown): err is NodeJS.ErrnoException {
|
function isClientError(err: unknown): err is NodeJS.ErrnoException {
|
||||||
if (!err || typeof err !== 'object') return false;
|
if (!err || typeof err !== 'object') return false;
|
||||||
|
|
||||||
|
|||||||
@ -5,13 +5,14 @@ import path from 'node:path';
|
|||||||
import { processLine } from './lib/process-line';
|
import { processLine } from './lib/process-line';
|
||||||
import { parseFelixDnsmasq } from './lib/parse-dnsmasq';
|
import { parseFelixDnsmasq } from './lib/parse-dnsmasq';
|
||||||
import { SOURCE_DIR } from './constants/dir';
|
import { SOURCE_DIR } from './constants/dir';
|
||||||
|
import { fetchWithRetry } from './lib/fetch-retry';
|
||||||
|
|
||||||
export const parseDomesticList = async () => {
|
export const parseDomesticList = async () => {
|
||||||
const trie = createTrie(await parseFelixDnsmasq('https://raw.githubusercontent.com/felixonmars/dnsmasq-china-list/master/accelerated-domains.china.conf'));
|
const trie = createTrie(await parseFelixDnsmasq('https://raw.githubusercontent.com/felixonmars/dnsmasq-china-list/master/accelerated-domains.china.conf'));
|
||||||
|
|
||||||
const top5000 = new Set<string>();
|
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: {
|
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: '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',
|
'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',
|
||||||
|
|||||||
@ -6,12 +6,13 @@ import { parse } from 'csv-parse/sync';
|
|||||||
import { readFileByLine } from './lib/fetch-text-by-line';
|
import { readFileByLine } from './lib/fetch-text-by-line';
|
||||||
import path from 'node:path';
|
import path from 'node:path';
|
||||||
import { SOURCE_DIR } from './constants/dir';
|
import { SOURCE_DIR } from './constants/dir';
|
||||||
|
import { fetchWithRetry } from './lib/fetch-retry';
|
||||||
|
|
||||||
export const parseGfwList = async () => {
|
export const parseGfwList = async () => {
|
||||||
const whiteSet = new Set<string>();
|
const whiteSet = new Set<string>();
|
||||||
const blackSet = 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')) {
|
for (const l of atob(text).split('\n')) {
|
||||||
const line = processLine(l);
|
const line = processLine(l);
|
||||||
if (!line) continue;
|
if (!line) continue;
|
||||||
@ -54,13 +55,13 @@ export const parseGfwList = async () => {
|
|||||||
continue;
|
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);
|
blackSet.add(l);
|
||||||
}
|
}
|
||||||
|
|
||||||
const top500Gfwed = new Set<string>();
|
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: {
|
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: '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',
|
'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',
|
||||||
|
|||||||
@ -39,6 +39,7 @@
|
|||||||
"tar-fs": "^3.0.6",
|
"tar-fs": "^3.0.6",
|
||||||
"tldts": "^6.1.50",
|
"tldts": "^6.1.50",
|
||||||
"tldts-experimental": "^6.1.50",
|
"tldts-experimental": "^6.1.50",
|
||||||
|
"undici": "^6.19.8",
|
||||||
"yaml": "^2.5.1"
|
"yaml": "^2.5.1"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
|
|||||||
9
pnpm-lock.yaml
generated
9
pnpm-lock.yaml
generated
@ -68,6 +68,9 @@ importers:
|
|||||||
tldts-experimental:
|
tldts-experimental:
|
||||||
specifier: ^6.1.50
|
specifier: ^6.1.50
|
||||||
version: 6.1.50
|
version: 6.1.50
|
||||||
|
undici:
|
||||||
|
specifier: ^6.19.8
|
||||||
|
version: 6.19.8
|
||||||
yaml:
|
yaml:
|
||||||
specifier: ^2.5.1
|
specifier: ^2.5.1
|
||||||
version: 2.5.1
|
version: 2.5.1
|
||||||
@ -1490,6 +1493,10 @@ packages:
|
|||||||
undici-types@5.26.5:
|
undici-types@5.26.5:
|
||||||
resolution: {integrity: sha512-JlCMO+ehdEIKqlFxk6IfVoAUVmgz7cU7zD/h9XZ0qzeosSHmUJVOzSQvvYSYWXkFXC+IfLKSIffhv0sVZup6pA==}
|
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:
|
uri-js@4.4.1:
|
||||||
resolution: {integrity: sha512-7rKUyy33Q1yc98pQ1DAmLtwX109F7TIfWlW1Ydo8Wl1ii1SeHieeh0HHfPeL2fMXK6z0s8ecKs9frCuLJvndBg==}
|
resolution: {integrity: sha512-7rKUyy33Q1yc98pQ1DAmLtwX109F7TIfWlW1Ydo8Wl1ii1SeHieeh0HHfPeL2fMXK6z0s8ecKs9frCuLJvndBg==}
|
||||||
|
|
||||||
@ -2988,6 +2995,8 @@ snapshots:
|
|||||||
|
|
||||||
undici-types@5.26.5: {}
|
undici-types@5.26.5: {}
|
||||||
|
|
||||||
|
undici@6.19.8: {}
|
||||||
|
|
||||||
uri-js@4.4.1:
|
uri-js@4.4.1:
|
||||||
dependencies:
|
dependencies:
|
||||||
punycode: 2.3.1
|
punycode: 2.3.1
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user