mirror of
https://github.com/SukkaW/Surge.git
synced 2025-12-12 01:00:34 +08:00
Perf: avoid spread operator
This commit is contained in:
parent
e7766281d0
commit
33636285e9
@ -8,8 +8,8 @@ import { domainDeduper } from './lib/domain-deduper';
|
||||
import type { Span } from './trace';
|
||||
import { task } from './trace';
|
||||
import { SHARED_DESCRIPTION } from './lib/constants';
|
||||
import picocolors from 'picocolors';
|
||||
import { fdir as Fdir } from 'fdir';
|
||||
import { appendArrayInPlace } from './lib/append-array-in-place';
|
||||
|
||||
const MAGIC_COMMAND_SKIP = '# $ custom_build_script';
|
||||
const MAGIC_COMMAND_TITLE = '# $ meta_title ';
|
||||
@ -24,31 +24,45 @@ export const buildCommon = task(import.meta.main, import.meta.path)(async (span)
|
||||
|
||||
const paths = await new Fdir()
|
||||
.withRelativePaths()
|
||||
// .exclude((dirName, dirPath) => {
|
||||
// if (dirName === 'domainset' || dirName === 'ip' || dirName === 'non_ip') {
|
||||
// return false;
|
||||
// }
|
||||
|
||||
// console.error(picocolors.red(`[build-comman] Unknown dir: ${dirPath}`));
|
||||
|
||||
// return true;
|
||||
// })
|
||||
.filter((filepath, isDirectory) => {
|
||||
if (isDirectory) return true;
|
||||
|
||||
const extname = path.extname(filepath);
|
||||
if (extname === '.js' || extname === '.ts') {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
})
|
||||
.crawl(sourceDir)
|
||||
.withPromise();
|
||||
|
||||
for (let i = 0, len = paths.length; i < len; i++) {
|
||||
const relativePath = paths[i];
|
||||
|
||||
const extname = path.extname(relativePath);
|
||||
if (extname === '.js' || extname === '.ts') {
|
||||
continue;
|
||||
}
|
||||
const fullPath = sourceDir + path.sep + relativePath;
|
||||
|
||||
if (relativePath.startsWith('domainset/')) {
|
||||
promises.push(transformDomainset(span, fullPath, relativePath));
|
||||
continue;
|
||||
}
|
||||
if (
|
||||
relativePath.startsWith('ip/')
|
||||
|| relativePath.startsWith('non_ip/')
|
||||
) {
|
||||
promises.push(transformRuleset(span, fullPath, relativePath));
|
||||
continue;
|
||||
}
|
||||
// if (
|
||||
// relativePath.startsWith('ip/')
|
||||
// || relativePath.startsWith('non_ip/')
|
||||
// ) {
|
||||
promises.push(transformRuleset(span, fullPath, relativePath));
|
||||
// continue;
|
||||
// }
|
||||
|
||||
console.error(picocolors.red(`[build-comman] Unknown file: ${relativePath}`));
|
||||
// console.error(picocolors.red(`[build-comman] Unknown file: ${relativePath}`));
|
||||
}
|
||||
|
||||
return Promise.all(promises);
|
||||
@ -103,14 +117,15 @@ function transformDomainset(parentSpan: Span, sourcePath: string, relativePath:
|
||||
const [title, descriptions, lines] = res;
|
||||
|
||||
const deduped = domainDeduper(lines);
|
||||
const description = [
|
||||
...SHARED_DESCRIPTION,
|
||||
...(
|
||||
descriptions.length
|
||||
? ['', ...descriptions]
|
||||
: []
|
||||
)
|
||||
];
|
||||
|
||||
let description: string[];
|
||||
if (descriptions.length) {
|
||||
description = SHARED_DESCRIPTION.slice();
|
||||
description.push('');
|
||||
appendArrayInPlace(description, descriptions);
|
||||
} else {
|
||||
description = SHARED_DESCRIPTION;
|
||||
}
|
||||
|
||||
return createRuleset(
|
||||
span,
|
||||
@ -138,14 +153,14 @@ async function transformRuleset(parentSpan: Span, sourcePath: string, relativePa
|
||||
|
||||
const [title, descriptions, lines] = res;
|
||||
|
||||
const description = [
|
||||
...SHARED_DESCRIPTION,
|
||||
...(
|
||||
descriptions.length
|
||||
? ['', ...descriptions]
|
||||
: []
|
||||
)
|
||||
];
|
||||
let description: string[];
|
||||
if (descriptions.length) {
|
||||
description = SHARED_DESCRIPTION.slice();
|
||||
description.push('');
|
||||
appendArrayInPlace(description, descriptions);
|
||||
} else {
|
||||
description = SHARED_DESCRIPTION;
|
||||
}
|
||||
|
||||
return createRuleset(
|
||||
span,
|
||||
|
||||
@ -8,6 +8,7 @@ import { task } from './trace';
|
||||
import { SHARED_DESCRIPTION } from './lib/constants';
|
||||
import { createMemoizedPromise } from './lib/memo-promise';
|
||||
import * as yaml from 'yaml';
|
||||
import { appendArrayInPlace } from './lib/append-array-in-place';
|
||||
|
||||
export const getDomesticAndDirectDomainsRulesetPromise = createMemoizedPromise(async () => {
|
||||
const domestics = await readFileIntoProcessedArray(path.resolve(import.meta.dir, '../Source/non_ip/domestic.conf'));
|
||||
@ -15,13 +16,13 @@ export const getDomesticAndDirectDomainsRulesetPromise = createMemoizedPromise(a
|
||||
const lans: string[] = [];
|
||||
|
||||
Object.entries(DOMESTICS).forEach(([, { domains }]) => {
|
||||
domestics.push(...domains.map((domain) => `DOMAIN-SUFFIX,${domain}`));
|
||||
appendArrayInPlace(domestics, domains.map((domain) => `DOMAIN-SUFFIX,${domain}`));
|
||||
});
|
||||
Object.entries(DIRECTS).forEach(([, { domains }]) => {
|
||||
directs.push(...domains.map((domain) => `DOMAIN-SUFFIX,${domain}`));
|
||||
appendArrayInPlace(directs, domains.map((domain) => `DOMAIN-SUFFIX,${domain}`));
|
||||
});
|
||||
Object.entries(LANS).forEach(([, { domains }]) => {
|
||||
lans.push(...domains.map((domain) => `DOMAIN-SUFFIX,${domain}`));
|
||||
appendArrayInPlace(lans, domains.map((domain) => `DOMAIN-SUFFIX,${domain}`));
|
||||
});
|
||||
|
||||
return [domestics, directs, lans] as const;
|
||||
@ -30,7 +31,9 @@ export const getDomesticAndDirectDomainsRulesetPromise = createMemoizedPromise(a
|
||||
export const buildDomesticRuleset = task(import.meta.main, import.meta.path)(async (span) => {
|
||||
const res = await getDomesticAndDirectDomainsRulesetPromise();
|
||||
|
||||
const dataset = [...Object.entries(DOMESTICS), ...Object.entries(DIRECTS), ...Object.entries(LANS)];
|
||||
const dataset = Object.entries(DOMESTICS);
|
||||
appendArrayInPlace(dataset, Object.entries(DIRECTS));
|
||||
appendArrayInPlace(dataset, Object.entries(LANS));
|
||||
|
||||
return Promise.all([
|
||||
createRuleset(
|
||||
|
||||
@ -61,9 +61,9 @@ const PRESET_MITM_HOSTNAMES = [
|
||||
.replaceAll('^https://', '')
|
||||
.replaceAll('^http://', '')
|
||||
.split('/')[0]
|
||||
.replaceAll('\\.', '.')
|
||||
.replaceAll(String.raw`\.`, '.')
|
||||
.replaceAll('.+', '*')
|
||||
.replaceAll('\\d', '*')
|
||||
.replaceAll(String.raw`\d`, '*')
|
||||
.replaceAll('([a-z])', '*')
|
||||
.replaceAll('[a-z]', '*')
|
||||
.replaceAll('([0-9])', '*')
|
||||
@ -104,7 +104,7 @@ const PRESET_MITM_HOSTNAMES = [
|
||||
return new RegExp(
|
||||
escapeRegExp(i)
|
||||
.replaceAll('{www or not}', '(www.)?')
|
||||
.replaceAll('\\*', '(.*)')
|
||||
.replaceAll(String.raw`\*`, '(.*)')
|
||||
);
|
||||
});
|
||||
|
||||
@ -135,7 +135,7 @@ const PRESET_MITM_HOSTNAMES = [
|
||||
}));
|
||||
console.log('--------------------');
|
||||
console.log('Parsed Failed');
|
||||
console.log([...parsedFailures].join('\n'));
|
||||
console.log(Array.from(parsedFailures).join('\n'));
|
||||
})();
|
||||
|
||||
/** Util function */
|
||||
@ -159,6 +159,6 @@ function escapeRegExp(string = '') {
|
||||
const reHasRegExpChar = new RegExp(reRegExpChar.source);
|
||||
|
||||
return string && reHasRegExpChar.test(string)
|
||||
? string.replaceAll(reRegExpChar, '\\$&')
|
||||
? string.replaceAll(reRegExpChar, String.raw`\$&`)
|
||||
: string;
|
||||
}
|
||||
|
||||
@ -55,11 +55,10 @@ export const buildRejectDomainSet = task(import.meta.main, import.meta.path)(asy
|
||||
'https://raw.githubusercontent.com/AdguardTeam/AdGuardSDNSFilter/master/Filters/exceptions.txt',
|
||||
'https://raw.githubusercontent.com/AdguardTeam/AdGuardSDNSFilter/master/Filters/exclusions.txt'
|
||||
].map(
|
||||
input => processFilterRules(childSpan, input)
|
||||
.then(({ white, black }) => {
|
||||
setAddFromArray(filterRuleWhitelistDomainSets, white);
|
||||
setAddFromArray(filterRuleWhitelistDomainSets, black);
|
||||
})
|
||||
input => processFilterRules(childSpan, input).then(({ white, black }) => {
|
||||
setAddFromArray(filterRuleWhitelistDomainSets, white);
|
||||
setAddFromArray(filterRuleWhitelistDomainSets, black);
|
||||
})
|
||||
)),
|
||||
getPhishingDomains(childSpan).then(appendArrayToDomainSets),
|
||||
getRejectSukkaConfPromise.then(appendArrayToDomainSets)
|
||||
|
||||
@ -8,7 +8,7 @@ function escapeRegExp(string = '') {
|
||||
const reHasRegExpChar = new RegExp(reRegExpChar.source);
|
||||
|
||||
return string && reHasRegExpChar.test(string)
|
||||
? string.replaceAll(reRegExpChar, '\\$&')
|
||||
? string.replaceAll(reRegExpChar, String.raw`\$&`)
|
||||
: string;
|
||||
}
|
||||
|
||||
@ -119,14 +119,10 @@ const REDIRECT_FAKEWEBSITES = [
|
||||
] as const;
|
||||
|
||||
export const buildRedirectModule = task(import.meta.main, import.meta.path)(async (span) => {
|
||||
const domains = Array.from(
|
||||
new Set(
|
||||
[
|
||||
...REDIRECT_MIRROR.map(([from]) => getHostname(from, { detectIp: false })),
|
||||
...REDIRECT_FAKEWEBSITES.flatMap(([from]) => [from, `www.${from}`])
|
||||
]
|
||||
)
|
||||
).filter(Boolean);
|
||||
const domains = Array.from(new Set([
|
||||
...REDIRECT_MIRROR.map(([from]) => getHostname(from, { detectIp: false })),
|
||||
...REDIRECT_FAKEWEBSITES.flatMap(([from]) => [from, `www.${from}`])
|
||||
])).filter(Boolean);
|
||||
|
||||
return compareAndWriteFile(
|
||||
span,
|
||||
|
||||
@ -2,4 +2,4 @@ export const SHARED_DESCRIPTION = [
|
||||
'License: AGPL 3.0',
|
||||
'Homepage: https://ruleset.skk.moe',
|
||||
'GitHub: https://github.com/SukkaW/Surge'
|
||||
] as const;
|
||||
];
|
||||
|
||||
@ -26,7 +26,7 @@ export async function compareAndWriteFile(span: Span, linesA: string[], filePath
|
||||
let index = 0;
|
||||
|
||||
for await (const lineB of readFileByLine(file)) {
|
||||
const lineA = linesA[index];
|
||||
const lineA = linesA[index] as string | undefined;
|
||||
index++;
|
||||
|
||||
if (lineA == null) {
|
||||
|
||||
@ -28,7 +28,7 @@ export class PolyfillTextDecoderStream extends TransformStream<Uint8Array, strin
|
||||
) {
|
||||
const decoder = new TextDecoder(encoding, { fatal, ignoreBOM });
|
||||
|
||||
const nonLastChunkDecoderOpt: TextDecodeOptions = { stream: true };
|
||||
const nonLastChunkDecoderOpt = { stream: true };
|
||||
|
||||
super({
|
||||
transform(chunk: Uint8Array, controller: TransformStreamDefaultController<string>) {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user