mirror of
https://github.com/SukkaW/Surge.git
synced 2025-12-12 01:00:34 +08:00
Perf: preload/hoist more promises, make ts happy
This commit is contained in:
parent
6daf8e3bb4
commit
a3e1a85c70
@ -8,36 +8,32 @@ import { SHARED_DESCRIPTION } from './lib/constants';
|
|||||||
import { isProbablyIpv4, isProbablyIpv6 } from './lib/is-fast-ip';
|
import { isProbablyIpv4, isProbablyIpv6 } from './lib/is-fast-ip';
|
||||||
import { TTL, deserializeArray, fsCache, serializeArray } from './lib/cache-filesystem';
|
import { TTL, deserializeArray, fsCache, serializeArray } from './lib/cache-filesystem';
|
||||||
|
|
||||||
const getBogusNxDomainIPs = async () => {
|
const URL = 'https://raw.githubusercontent.com/felixonmars/dnsmasq-china-list/master/bogus-nxdomain.china.conf';
|
||||||
const url = 'https://raw.githubusercontent.com/felixonmars/dnsmasq-china-list/master/bogus-nxdomain.china.conf';
|
|
||||||
|
|
||||||
return fsCache.apply(
|
const getBogusNxDomainIPsPromise = fsCache.apply(
|
||||||
url,
|
URL,
|
||||||
async () => {
|
async () => {
|
||||||
const result: string[] = [];
|
const result: string[] = [];
|
||||||
for await (const line of await fetchRemoteTextByLine('https://raw.githubusercontent.com/felixonmars/dnsmasq-china-list/master/bogus-nxdomain.china.conf')) {
|
for await (const line of await fetchRemoteTextByLine(URL)) {
|
||||||
if (line && line.startsWith('bogus-nxdomain=')) {
|
if (line && line.startsWith('bogus-nxdomain=')) {
|
||||||
const ip = line.slice(15).trim();
|
const ip = line.slice(15).trim();
|
||||||
if (isProbablyIpv4(ip)) {
|
if (isProbablyIpv4(ip)) {
|
||||||
result.push(`IP-CIDR,${ip}/32,no-resolve`);
|
result.push(`IP-CIDR,${ip}/32,no-resolve`);
|
||||||
} else if (isProbablyIpv6(ip)) {
|
} else if (isProbablyIpv6(ip)) {
|
||||||
result.push(`IP-CIDR6,${ip}/128,no-resolve`);
|
result.push(`IP-CIDR6,${ip}/128,no-resolve`);
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return result;
|
|
||||||
},
|
|
||||||
{
|
|
||||||
ttl: TTL.ONE_WEEK(),
|
|
||||||
serializer: serializeArray,
|
|
||||||
deserializer: deserializeArray
|
|
||||||
}
|
}
|
||||||
);
|
return result;
|
||||||
};
|
},
|
||||||
|
{
|
||||||
|
ttl: TTL.ONE_WEEK(),
|
||||||
|
serializer: serializeArray,
|
||||||
|
deserializer: deserializeArray
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
export const buildAntiBogusDomain = task(import.meta.path, async (span) => {
|
export const buildAntiBogusDomain = task(import.meta.path, async (span) => {
|
||||||
const bogusIpPromise = getBogusNxDomainIPs();
|
|
||||||
|
|
||||||
const result: string[] = [];
|
const result: string[] = [];
|
||||||
for await (const line of readFileByLine(path.resolve(import.meta.dir, '../Source/ip/reject.conf'))) {
|
for await (const line of readFileByLine(path.resolve(import.meta.dir, '../Source/ip/reject.conf'))) {
|
||||||
const l = processLine(line);
|
const l = processLine(line);
|
||||||
@ -46,7 +42,7 @@ export const buildAntiBogusDomain = task(import.meta.path, async (span) => {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
result.push(...(await bogusIpPromise));
|
result.push(...(await getBogusNxDomainIPsPromise));
|
||||||
|
|
||||||
const description = [
|
const description = [
|
||||||
...SHARED_DESCRIPTION,
|
...SHARED_DESCRIPTION,
|
||||||
|
|||||||
@ -2,25 +2,19 @@
|
|||||||
import path from 'path';
|
import path from 'path';
|
||||||
import { createRuleset } from './lib/create-file';
|
import { createRuleset } from './lib/create-file';
|
||||||
import { parseFelixDnsmasq } from './lib/parse-dnsmasq';
|
import { parseFelixDnsmasq } from './lib/parse-dnsmasq';
|
||||||
import { traceAsync } from './lib/trace-runner';
|
|
||||||
import { task } from './trace';
|
import { task } from './trace';
|
||||||
import { SHARED_DESCRIPTION } from './lib/constants';
|
import { SHARED_DESCRIPTION } from './lib/constants';
|
||||||
import picocolors from 'picocolors';
|
|
||||||
import { createMemoizedPromise } from './lib/memo-promise';
|
import { createMemoizedPromise } from './lib/memo-promise';
|
||||||
import { TTL, deserializeArray, fsCache, serializeArray } from './lib/cache-filesystem';
|
import { TTL, deserializeArray, fsCache, serializeArray } from './lib/cache-filesystem';
|
||||||
|
|
||||||
export const getAppleCdnDomainsPromise = createMemoizedPromise(() => traceAsync(
|
export const getAppleCdnDomainsPromise = createMemoizedPromise(() => fsCache.apply(
|
||||||
picocolors.gray('download dnsmasq-china-list apple.china.conf'),
|
'https://raw.githubusercontent.com/felixonmars/dnsmasq-china-list/master/apple.china.conf',
|
||||||
() => fsCache.apply(
|
() => parseFelixDnsmasq('https://raw.githubusercontent.com/felixonmars/dnsmasq-china-list/master/apple.china.conf'),
|
||||||
'https://raw.githubusercontent.com/felixonmars/dnsmasq-china-list/master/apple.china.conf',
|
{
|
||||||
() => parseFelixDnsmasq('https://raw.githubusercontent.com/felixonmars/dnsmasq-china-list/master/apple.china.conf'),
|
ttl: TTL.THREE_DAYS(),
|
||||||
{
|
serializer: serializeArray,
|
||||||
ttl: TTL.THREE_DAYS(),
|
deserializer: deserializeArray
|
||||||
serializer: serializeArray,
|
}
|
||||||
deserializer: deserializeArray
|
|
||||||
}
|
|
||||||
),
|
|
||||||
picocolors.gray
|
|
||||||
));
|
));
|
||||||
|
|
||||||
export const buildAppleCdn = task(import.meta.path, async (span) => {
|
export const buildAppleCdn = task(import.meta.path, async (span) => {
|
||||||
|
|||||||
@ -6,7 +6,8 @@ import { task } from './trace';
|
|||||||
import { processLine } from './lib/process-line';
|
import { processLine } from './lib/process-line';
|
||||||
import { SHARED_DESCRIPTION } from './lib/constants';
|
import { SHARED_DESCRIPTION } from './lib/constants';
|
||||||
import { getPublicSuffixListTextPromise } from './download-publicsuffixlist';
|
import { getPublicSuffixListTextPromise } from './download-publicsuffixlist';
|
||||||
const getS3OSSDomains = async (): Promise<Set<string>> => {
|
|
||||||
|
const getS3OSSDomainsPromise = (async (): Promise<Set<string>> => {
|
||||||
const trie = createTrie((await getPublicSuffixListTextPromise()).split('\n'));
|
const trie = createTrie((await getPublicSuffixListTextPromise()).split('\n'));
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -39,14 +40,12 @@ const getS3OSSDomains = async (): Promise<Set<string>> => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
return S3OSSDomains;
|
return S3OSSDomains;
|
||||||
};
|
})();
|
||||||
|
|
||||||
const buildCdnConf = task(import.meta.path, async (span) => {
|
const buildCdnConf = task(import.meta.path, async (span) => {
|
||||||
/** @type {string[]} */
|
/** @type {string[]} */
|
||||||
const cdnDomainsList: string[] = [];
|
const cdnDomainsList: string[] = [];
|
||||||
|
|
||||||
const getS3OSSDomainsPromise: Promise<Set<string>> = getS3OSSDomains();
|
|
||||||
|
|
||||||
for await (const l of readFileByLine(path.resolve(import.meta.dir, '../Source/non_ip/cdn.conf'))) {
|
for await (const l of readFileByLine(path.resolve(import.meta.dir, '../Source/non_ip/cdn.conf'))) {
|
||||||
const line = processLine(l);
|
const line = processLine(l);
|
||||||
if (line) {
|
if (line) {
|
||||||
|
|||||||
@ -125,30 +125,32 @@ async function transformDomainset(parentSpan: Span, sourcePath: string, relative
|
|||||||
* Output Surge RULE-SET and Clash classical text format
|
* Output Surge RULE-SET and Clash classical text format
|
||||||
*/
|
*/
|
||||||
async function transformRuleset(parentSpan: Span, sourcePath: string, relativePath: string) {
|
async function transformRuleset(parentSpan: Span, sourcePath: string, relativePath: string) {
|
||||||
const span = parentSpan.traceChild(`transform ruleset: ${path.basename(sourcePath, path.extname(sourcePath))}`);
|
return parentSpan
|
||||||
|
.traceChild(`transform ruleset: ${path.basename(sourcePath, path.extname(sourcePath))}`)
|
||||||
|
.traceAsyncFn(async (span) => {
|
||||||
|
const res = await processFile(span, sourcePath);
|
||||||
|
if (!res) return null;
|
||||||
|
|
||||||
const res = await processFile(span, sourcePath);
|
const [title, descriptions, lines] = res;
|
||||||
if (!res) return null;
|
|
||||||
|
|
||||||
const [title, descriptions, lines] = res;
|
const description = [
|
||||||
|
...SHARED_DESCRIPTION,
|
||||||
|
...(
|
||||||
|
descriptions.length
|
||||||
|
? ['', ...descriptions]
|
||||||
|
: []
|
||||||
|
)
|
||||||
|
];
|
||||||
|
|
||||||
const description = [
|
return createRuleset(
|
||||||
...SHARED_DESCRIPTION,
|
span,
|
||||||
...(
|
title,
|
||||||
descriptions.length
|
description,
|
||||||
? ['', ...descriptions]
|
new Date(),
|
||||||
: []
|
lines,
|
||||||
)
|
'ruleset',
|
||||||
];
|
path.resolve(outputSurgeDir, relativePath),
|
||||||
|
path.resolve(outputClashDir, `${relativePath.slice(0, -path.extname(relativePath).length)}.txt`)
|
||||||
return createRuleset(
|
);
|
||||||
span,
|
});
|
||||||
title,
|
|
||||||
description,
|
|
||||||
new Date(),
|
|
||||||
lines,
|
|
||||||
'ruleset',
|
|
||||||
path.resolve(outputSurgeDir, relativePath),
|
|
||||||
path.resolve(outputClashDir, `${relativePath.slice(0, -path.extname(relativePath).length)}.txt`)
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -24,6 +24,7 @@ const BLACKLIST = [
|
|||||||
|
|
||||||
export const getMicrosoftCdnRulesetPromise = createMemoizedPromise(async () => {
|
export const getMicrosoftCdnRulesetPromise = createMemoizedPromise(async () => {
|
||||||
const set = await traceAsync('fetch accelerated-domains.china.conf', async () => {
|
const set = await traceAsync('fetch accelerated-domains.china.conf', async () => {
|
||||||
|
// First trie is to find the microsoft domains that matches probe domains
|
||||||
const trie = createTrie();
|
const trie = createTrie();
|
||||||
for await (const line of await fetchRemoteTextByLine('https://raw.githubusercontent.com/felixonmars/dnsmasq-china-list/master/accelerated-domains.china.conf')) {
|
for await (const line of await fetchRemoteTextByLine('https://raw.githubusercontent.com/felixonmars/dnsmasq-china-list/master/accelerated-domains.china.conf')) {
|
||||||
if (line.startsWith('server=/') && line.endsWith('/114.114.114.114')) {
|
if (line.startsWith('server=/') && line.endsWith('/114.114.114.114')) {
|
||||||
@ -34,6 +35,7 @@ export const getMicrosoftCdnRulesetPromise = createMemoizedPromise(async () => {
|
|||||||
return new Set(PROBE_DOMAINS.flatMap(domain => trie.find(domain, false)));
|
return new Set(PROBE_DOMAINS.flatMap(domain => trie.find(domain, false)));
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// Second trie is to remove blacklisted domains
|
||||||
const trie2 = createTrie(set);
|
const trie2 = createTrie(set);
|
||||||
const black = BLACKLIST.flatMap(domain => trie2.find(domain, true));
|
const black = BLACKLIST.flatMap(domain => trie2.find(domain, true));
|
||||||
for (let i = 0, len = black.length; i < len; i++) {
|
for (let i = 0, len = black.length; i < len; i++) {
|
||||||
|
|||||||
@ -16,22 +16,29 @@ const folderAndFilesToBeDeployed = [
|
|||||||
'LICENSE'
|
'LICENSE'
|
||||||
];
|
];
|
||||||
|
|
||||||
export const buildPublic = task(import.meta.path, async () => {
|
export const buildPublic = task(import.meta.path, async (span) => {
|
||||||
const filesToBeCopied = (await listDir(
|
await span
|
||||||
rootPath, {
|
.traceChild('copy public files')
|
||||||
ignoreHidden: true,
|
.traceAsyncFn(async () => {
|
||||||
ignorePattern: /node_modules|Build|public/
|
const filesToBeCopied = (await listDir(
|
||||||
}
|
rootPath, {
|
||||||
)).filter(file => folderAndFilesToBeDeployed.some(folderOrFile => file.startsWith(folderOrFile)));
|
ignoreHidden: true,
|
||||||
|
ignorePattern: /node_modules|Build|public/
|
||||||
|
}
|
||||||
|
)).filter(file => folderAndFilesToBeDeployed.some(folderOrFile => file.startsWith(folderOrFile)));
|
||||||
|
|
||||||
await Promise.all(filesToBeCopied.map(file => {
|
return Promise.all(filesToBeCopied.map(file => {
|
||||||
const src = path.resolve(rootPath, file);
|
const src = path.resolve(rootPath, file);
|
||||||
const dest = path.resolve(publicPath, file);
|
const dest = path.resolve(publicPath, file);
|
||||||
|
|
||||||
return Bun.write(dest, Bun.file(src));
|
return Bun.write(dest, Bun.file(src));
|
||||||
}));
|
}));
|
||||||
|
});
|
||||||
|
|
||||||
|
const html = await span
|
||||||
|
.traceChild('generate index.html')
|
||||||
|
.traceAsyncFn(() => treeDir(publicPath).then(generateHtml));
|
||||||
|
|
||||||
const html = generateHtml(await treeDir(publicPath));
|
|
||||||
return Bun.write(path.join(publicPath, 'index.html'), html);
|
return Bun.write(path.join(publicPath, 'index.html'), html);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
@ -3,7 +3,7 @@ import { traceAsync } from './trace-runner';
|
|||||||
import { createMemoizedPromise } from './memo-promise';
|
import { createMemoizedPromise } from './memo-promise';
|
||||||
import { getPublicSuffixListTextPromise } from '../download-publicsuffixlist';
|
import { getPublicSuffixListTextPromise } from '../download-publicsuffixlist';
|
||||||
|
|
||||||
const customFetch = (url: string | URL) => Promise.resolve(Bun.file(url));
|
const customFetch = (url: string | URL): Promise<Blob> => Promise.resolve(Bun.file(url));
|
||||||
|
|
||||||
export const getGorhillPublicSuffixPromise = createMemoizedPromise(() => traceAsync('create gorhill public suffix instance', async () => {
|
export const getGorhillPublicSuffixPromise = createMemoizedPromise(() => traceAsync('create gorhill public suffix instance', async () => {
|
||||||
const [publicSuffixListDat, { default: gorhill }] = await Promise.all([
|
const [publicSuffixListDat, { default: gorhill }] = await Promise.all([
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user