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