Chore: minor changes

This commit is contained in:
SukkaW
2025-04-25 21:31:54 +08:00
parent 1eee3e4a72
commit 4f56b84adb
3 changed files with 42 additions and 15 deletions

View File

@@ -31,7 +31,7 @@ const pool = new Worktank({
isEqual = await fileEqual(linesA, readFileByLine(filePath));
} else {
console.log(`${filePath} does not exists, writing...`);
isEqual = false;
// isEqual = false; // isEqual is false by default anyway
}
if (isEqual) {

View File

@@ -5,25 +5,42 @@ import { onBlackFound } from './shared';
const rSpace = /\s+/;
function hostsLineCb(line: string, set: string[], includeAllSubDomain: boolean, meta: string) {
const _domain = line.split(rSpace, 3)[1]?.trim();
function hostsLineCb(line: string, set: string[], meta: string) {
const _domain = line.split(rSpace, 3)[1];
if (!_domain) {
return;
}
const domain = fastNormalizeDomainWithoutWww(_domain);
const domain = fastNormalizeDomainWithoutWww(_domain.trim());
if (!domain) {
return;
}
onBlackFound(domain, meta);
set.push(includeAllSubDomain ? `.${domain}` : domain);
set.push(domain);
}
function hostsLineCbIncludeAllSubdomain(line: string, set: string[], meta: string) {
const _domain = line.split(rSpace, 3)[1];
if (!_domain) {
return;
}
const domain = fastNormalizeDomainWithoutWww(_domain.trim());
if (!domain) {
return;
}
onBlackFound(domain, meta);
set.push('.' + domain);
}
export function processHosts(
span: Span,
hostsUrl: string, mirrors: string[] | null, includeAllSubDomain = false
) {
const cb = includeAllSubDomain ? hostsLineCbIncludeAllSubdomain : hostsLineCb;
return span.traceChildAsync(`process hosts: ${hostsUrl}`, async (span) => {
const filterRules = await span.traceChild('download').traceAsyncFn(() => fetchAssets(hostsUrl, mirrors, true));
@@ -31,7 +48,7 @@ export function processHosts(
span.traceChild('parse hosts').traceSyncFn(() => {
for (let i = 0, len = filterRules.length; i < len; i++) {
hostsLineCb(filterRules[i], domainSets, includeAllSubDomain, hostsUrl);
cb(filterRules[i], domainSets, hostsUrl);
}
});
@@ -41,6 +58,7 @@ export function processHosts(
export function processHostsWithPreload(hostsUrl: string, mirrors: string[] | null, includeAllSubDomain = false) {
const downloadPromise = fetchAssets(hostsUrl, mirrors, true);
const cb = includeAllSubDomain ? hostsLineCbIncludeAllSubdomain : hostsLineCb;
return (span: Span) => span.traceChildAsync(`process hosts: ${hostsUrl}`, async (span) => {
const filterRules = await span.traceChild('download').tracePromise(downloadPromise);
@@ -49,7 +67,7 @@ export function processHostsWithPreload(hostsUrl: string, mirrors: string[] | nu
span.traceChild('parse hosts').traceSyncFn(() => {
for (let i = 0, len = filterRules.length; i < len; i++) {
hostsLineCb(filterRules[i], domainSets, includeAllSubDomain, hostsUrl);
cb(filterRules[i], domainSets, hostsUrl);
}
});