Perf: optimiaztions, avoid spread operator

This commit is contained in:
SukkaW 2024-06-26 18:30:41 +08:00
parent 33636285e9
commit 098e8815ae
9 changed files with 110 additions and 62 deletions

View File

@ -18,8 +18,7 @@ export const getAppleCdnDomainsPromise = createMemoizedPromise(() => fsFetchCach
)); ));
export const buildAppleCdn = task(import.meta.main, import.meta.path)(async (span) => { export const buildAppleCdn = task(import.meta.main, import.meta.path)(async (span) => {
const promise = getAppleCdnDomainsPromise(); const res: string[] = await span.traceChildPromise('get apple cdn domains', getAppleCdnDomainsPromise());
const res: string[] = await span.traceChildPromise('get apple cdn domains', promise);
const description = [ const description = [
...SHARED_DESCRIPTION, ...SHARED_DESCRIPTION,

View File

@ -11,9 +11,7 @@ export const buildCloudMounterRules = task(import.meta.main, import.meta.path)(a
// AND,((SRC-IP,192.168.1.110), (DOMAIN, example.com)) // AND,((SRC-IP,192.168.1.110), (DOMAIN, example.com))
const results = DOMAINS.flatMap(domain => { const results = DOMAINS.flatMap(domain => {
return PROCESS_NAMES.map(process => { return PROCESS_NAMES.map(process => `AND,((${domain}),(PROCESS-NAME,${process}))`);
return `AND,((${domain}),(PROCESS-NAME,${process}))`;
});
}); });
const description = SHARED_DESCRIPTION; const description = SHARED_DESCRIPTION;

View File

@ -85,12 +85,8 @@ export const buildDomesticRuleset = task(import.meta.main, import.meta.path)(asy
`#!desc=Last Updated: ${new Date().toISOString()}`, `#!desc=Last Updated: ${new Date().toISOString()}`,
'', '',
'[Host]', '[Host]',
...dataset.flatMap(([, { domains, dns, ...rest }]) => [ ...dataset.flatMap(([, { domains, dns, hosts }]) => [
...( ...Object.entries(hosts).flatMap(([dns, ips]: [dns: string, ips: string[]]) => `${dns} = ${ips.join(', ')}`),
'hosts' in rest
? Object.entries(rest.hosts).flatMap(([dns, ips]: [dns: string, ips: string[]]) => `${dns} = ${ips.join(', ')}`)
: []
),
...domains.flatMap((domain) => [ ...domains.flatMap((domain) => [
`${domain} = server:${dns}`, `${domain} = server:${dns}`,
`*.${domain} = server:${dns}` `*.${domain} = server:${dns}`

View File

@ -2,6 +2,7 @@ import path from 'path';
import { task } from './trace'; import { task } from './trace';
import { compareAndWriteFile } from './lib/create-file'; import { compareAndWriteFile } from './lib/create-file';
import { getHostname } from 'tldts'; import { getHostname } from 'tldts';
import { isTruthy } from './lib/misc';
function escapeRegExp(string = '') { function escapeRegExp(string = '') {
const reRegExpChar = /[$()*+.?[\\\]^{|}]/g; const reRegExpChar = /[$()*+.?[\\\]^{|}]/g;
@ -122,7 +123,7 @@ export const buildRedirectModule = task(import.meta.main, import.meta.path)(asyn
const domains = Array.from(new Set([ const domains = Array.from(new Set([
...REDIRECT_MIRROR.map(([from]) => getHostname(from, { detectIp: false })), ...REDIRECT_MIRROR.map(([from]) => getHostname(from, { detectIp: false })),
...REDIRECT_FAKEWEBSITES.flatMap(([from]) => [from, `www.${from}`]) ...REDIRECT_FAKEWEBSITES.flatMap(([from]) => [from, `www.${from}`])
])).filter(Boolean); ])).filter(isTruthy);
return compareAndWriteFile( return compareAndWriteFile(
span, span,

View File

@ -10,6 +10,8 @@ import { getChnCidrPromise } from './build-chn-cidr';
import { getTelegramCIDRPromise } from './build-telegram-cidr'; import { getTelegramCIDRPromise } from './build-telegram-cidr';
import { compareAndWriteFile } from './lib/create-file'; import { compareAndWriteFile } from './lib/create-file';
import { getMicrosoftCdnRulesetPromise } from './build-microsoft-cdn'; import { getMicrosoftCdnRulesetPromise } from './build-microsoft-cdn';
import { isTruthy } from './lib/misc';
import { appendArrayInPlace } from './lib/append-array-in-place';
const POLICY_GROUPS: Array<[name: string, insertProxy: boolean, insertDirect: boolean]> = [ const POLICY_GROUPS: Array<[name: string, insertProxy: boolean, insertDirect: boolean]> = [
['Default Proxy', true, false], ['Default Proxy', true, false],
@ -120,8 +122,6 @@ export const buildSSPanelUIMAppProfile = task(import.meta.main, import.meta.path
); );
}); });
const isTruthy = <T>(i: T | 0 | '' | false | null | undefined): i is T => !!i;
function generateAppProfile( function generateAppProfile(
directDomains: string[], directDomains: string[],
microsoftAppleDomains: string[], microsoftAppleDomains: string[],
@ -135,7 +135,7 @@ function generateAppProfile(
globalCidrs: string[], globalCidrs: string[],
lanCidrs: string[] lanCidrs: string[]
) { ) {
return [ const redults = [
'<?php', '<?php',
'', '',
`// # Build ${new Date().toISOString()}`, `// # Build ${new Date().toISOString()}`,
@ -172,8 +172,12 @@ function generateAppProfile(
return acc; return acc;
}, [])).slice(1, -1)}];`, }, [])).slice(1, -1)}];`,
'$_ENV[\'Clash_Group_Config\'] = [', '$_ENV[\'Clash_Group_Config\'] = [',
' \'proxy-groups\' => [', ' \'proxy-groups\' => ['
...POLICY_GROUPS.flatMap(([name, insertProxy, insertDirect]) => { ];
appendArrayInPlace(
redults,
POLICY_GROUPS.flatMap(([name, insertProxy, insertDirect]) => {
return [ return [
' [', ' [',
` 'name' => '${name}',`, ` 'name' => '${name}',`,
@ -184,33 +188,79 @@ function generateAppProfile(
' ],', ' ],',
' ],' ' ],'
].filter(isTruthy); ].filter(isTruthy);
}), })
);
appendArrayInPlace(
redults,
[
' ],', ' ],',
' \'rules\' => [', ' \'rules\' => ['
]
);
// domestic - domains // domestic - domains
...directDomains.map(line => ` '${line},Domestic',`), appendArrayInPlace(
redults,
directDomains.map(line => ` '${line},Domestic',`)
);
// microsoft & apple - domains // microsoft & apple - domains
...microsoftAppleDomains.map(line => ` '${line},Microsoft & Apple',`), appendArrayInPlace(
redults,
microsoftAppleDomains.map(line => ` '${line},Microsoft & Apple',`)
);
// stream - domains // stream - domains
...streamDomains.map(line => ` '${line},Stream',`), appendArrayInPlace(
redults,
streamDomains.map(line => ` '${line},Stream',`)
);
// steam download - domains // steam download - domains
...steamDomains.map(line => ` '${line},Steam Download',`), appendArrayInPlace(
redults,
steamDomains.map(line => ` '${line},Steam Download',`)
);
// global - domains // global - domains
...globalDomains.map(line => ` '${line},Global',`), appendArrayInPlace(
redults,
globalDomains.map(line => ` '${line},Global',`)
);
// microsoft & apple - ip cidr (nope) // microsoft & apple - ip cidr (nope)
// lan - domains // lan - domains
...lanDomains.map(line => ` '${line},DIRECT',`), appendArrayInPlace(
redults,
lanDomains.map(line => ` '${line},DIRECT',`)
);
// stream - ip cidr // stream - ip cidr
...streamCidrs.map(line => ` '${line},Stream',`), appendArrayInPlace(
redults,
streamCidrs.map(line => ` '${line},Stream',`)
);
// global - ip cidr // global - ip cidr
...globalCidrs.map(line => ` '${line},Global',`), appendArrayInPlace(
redults,
globalCidrs.map(line => ` '${line},Global',`)
);
// domestic - ip cidr // domestic - ip cidr
...directCidrs.map(line => ` '${line},Domestic',`), appendArrayInPlace(
redults,
directCidrs.map(line => ` '${line},Domestic',`)
);
// lan - ip cidr // lan - ip cidr
...lanCidrs.map(line => ` '${line},DIRECT',`), appendArrayInPlace(
redults,
lanCidrs.map(line => ` '${line},DIRECT',`)
);
// match // match
appendArrayInPlace(
redults,
[
' \'MATCH,Final Match\',', ' \'MATCH,Final Match\',',
' ],', ' ],',
'];' '];'
]; ]
);
return redults;
} }

1
Build/lib/misc.ts Normal file
View File

@ -0,0 +1 @@
export const isTruthy = <T>(i: T | 0 | '' | false | null | undefined): i is T => !!i;

View File

@ -72,26 +72,20 @@ export const createSpan = (name: string, parentTraceResult?: TraceResult): Span
stop, stop,
traceChild, traceChild,
traceSyncFn<T>(fn: (span: Span) => T) { traceSyncFn<T>(fn: (span: Span) => T) {
try { const res = fn(span);
return fn(span);
} finally {
span.stop(); span.stop();
} return res;
}, },
async traceAsyncFn<T>(fn: (span: Span) => T | Promise<T>): Promise<T> { async traceAsyncFn<T>(fn: (span: Span) => T | Promise<T>): Promise<T> {
try { const res = await fn(span);
return await fn(span);
} finally {
span.stop(); span.stop();
} return res;
}, },
traceResult: curTraceResult, traceResult: curTraceResult,
async tracePromise<T>(promise: Promise<T>): Promise<T> { async tracePromise<T>(promise: Promise<T>): Promise<T> {
try { const res = await promise;
return await promise;
} finally {
span.stop(); span.stop();
} return res;
}, },
traceChildSync: <T>(name: string, fn: (span: Span) => T): T => traceChild(name).traceSyncFn(fn), traceChildSync: <T>(name: string, fn: (span: Span) => T): T => traceChild(name).traceSyncFn(fn),
traceChildAsync: <T>(name: string, fn: (span: Span) => T | Promise<T>): Promise<T> => traceChild(name).traceAsyncFn(fn), traceChildAsync: <T>(name: string, fn: (span: Span) => T | Promise<T>): Promise<T> => traceChild(name).traceAsyncFn(fn),

View File

@ -1,5 +1,5 @@
export interface DNSMapping { export interface DNSMapping {
hosts?: { hosts: {
[domain: string]: string[] [domain: string]: string[]
}, },
dns: string, dns: string,
@ -9,6 +9,7 @@ export interface DNSMapping {
export const DIRECTS = { export const DIRECTS = {
ROUTER: { ROUTER: {
dns: 'system', dns: 'system',
hosts: {},
domains: [ domains: [
// Aruba Router // Aruba Router
'instant.arubanetworks.com', 'instant.arubanetworks.com',
@ -77,6 +78,7 @@ export const DIRECTS = {
}, },
SYSTEM: { SYSTEM: {
dns: 'system', dns: 'system',
hosts: {},
domains: [ domains: [
'_hotspot_.m2m', '_hotspot_.m2m',
'hotspot.cslwifi.com', 'hotspot.cslwifi.com',
@ -92,6 +94,7 @@ export const DIRECTS = {
export const LANS = { export const LANS = {
LAN: { LAN: {
dns: 'system', dns: 'system',
hosts: {},
domains: [ domains: [
'lan', 'lan',
'localhost', 'localhost',

View File

@ -141,18 +141,21 @@ export const DOMESTICS = {
}, },
BILIBILI_ALI: { BILIBILI_ALI: {
dns: 'quic://dns.alidns.com:853', dns: 'quic://dns.alidns.com:853',
hosts: {},
domains: [ domains: [
'upos-sz-mirrorali.bilivideo.com' 'upos-sz-mirrorali.bilivideo.com'
] ]
}, },
BILIBILI_BD: { BILIBILI_BD: {
dns: '180.76.76.76', dns: '180.76.76.76',
hosts: {},
domains: [ domains: [
'upos-sz-mirrorbos.bilivideo.com' 'upos-sz-mirrorbos.bilivideo.com'
] ]
}, },
BILIBILI: { BILIBILI: {
dns: 'https://doh.pub/dns-query', dns: 'https://doh.pub/dns-query',
hosts: {},
domains: [ domains: [
'upos-sz-mirrorcoso1.bilivideo.com', 'upos-sz-mirrorcoso1.bilivideo.com',
'acg.tv', 'acg.tv',
@ -179,6 +182,7 @@ export const DOMESTICS = {
}, },
XIAOMI: { XIAOMI: {
dns: 'https://doh.pub/dns-query', dns: 'https://doh.pub/dns-query',
hosts: {},
domains: [ domains: [
'mi.com', 'mi.com',
'duokan.com', 'duokan.com',
@ -195,6 +199,7 @@ export const DOMESTICS = {
}, },
BYTEDANCE: { BYTEDANCE: {
dns: '180.184.2.2', dns: '180.184.2.2',
hosts: {},
domains: [ domains: [
'bytecdn.cn', 'bytecdn.cn',
'toutiaoimg.com', 'toutiaoimg.com',
@ -235,6 +240,7 @@ export const DOMESTICS = {
}, },
BAIDU: { BAIDU: {
dns: '180.76.76.76', dns: '180.76.76.76',
hosts: {},
domains: [ domains: [
'91.com', '91.com',
'hao123.com', 'hao123.com',