Perf: avoid spread operator

This commit is contained in:
SukkaW 2024-06-26 17:49:32 +08:00
parent e7766281d0
commit 33636285e9
8 changed files with 69 additions and 56 deletions

View File

@ -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,

View File

@ -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(

View File

@ -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;
}

View File

@ -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)

View File

@ -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,

View File

@ -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;
];

View File

@ -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) {

View File

@ -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>) {