Refactor: full span tracer

This commit is contained in:
SukkaW
2024-01-14 22:10:16 +08:00
parent 0f257e992a
commit 9bb0c14d5f
23 changed files with 238 additions and 183 deletions

View File

@@ -3,8 +3,10 @@ import { readFileByLine } from './fetch-text-by-line';
import { surgeDomainsetToClashDomainset, surgeRulesetToClashClassicalTextRuleset } from './clash';
import { traceAsync } from './trace-runner';
import picocolors from 'picocolors';
import type { Span } from '../trace';
import path from 'path';
export async function compareAndWriteFile(linesA: string[], filePath: string) {
export async function compareAndWriteFile(span: Span, linesA: string[], filePath: string) {
let isEqual = true;
const file = Bun.file(filePath);
@@ -17,48 +19,44 @@ export async function compareAndWriteFile(linesA: string[], filePath: string) {
console.log(`Nothing to write to ${filePath}...`);
isEqual = false;
} else {
isEqual = await traceAsync(
picocolors.gray(`comparing ${filePath}`),
async () => {
let index = 0;
isEqual = await span.traceChild(`comparing ${filePath}`).traceAsyncFn(async () => {
let index = 0;
for await (const lineB of readFileByLine(file)) {
const lineA = linesA[index];
index++;
for await (const lineB of readFileByLine(file)) {
const lineA = linesA[index];
index++;
if (lineA == null) {
// The file becomes smaller
return false;
}
if (lineA == null) {
// The file becomes smaller
return false;
}
if (lineA[0] === '#' && lineB[0] === '#') {
continue;
}
if (
lineA[0] === '/'
if (lineA[0] === '#' && lineB[0] === '#') {
continue;
}
if (
lineA[0] === '/'
&& lineA[1] === '/'
&& lineA[3] === '#'
&& lineB[0] === '/'
&& lineB[1] === '/'
&& lineB[3] === '#'
) {
continue;
}
if (lineA !== lineB) {
return false;
}
) {
continue;
}
if (index !== linesALen) {
// The file becomes larger
if (lineA !== lineB) {
return false;
}
}
return true;
},
picocolors.gray
);
if (index !== linesALen) {
// The file becomes larger
return false;
}
return true;
});
}
if (isEqual) {
@@ -66,7 +64,7 @@ export async function compareAndWriteFile(linesA: string[], filePath: string) {
return;
}
await traceAsync(picocolors.gray(`writing ${filePath}`), async () => {
await span.traceChild(`writing ${filePath}`).traceAsyncFn(async () => {
if (linesALen < 10000) {
return Bun.write(file, `${linesA.join('\n')}\n`);
}
@@ -79,7 +77,7 @@ export async function compareAndWriteFile(linesA: string[], filePath: string) {
}
return writer.end();
}, picocolors.gray);
});
}
export const withBannerArray = (title: string, description: string[] | readonly string[], date: Date, content: string[]) => {
@@ -96,27 +94,28 @@ export const withBannerArray = (title: string, description: string[] | readonly
};
export const createRuleset = (
parentSpan: Span,
title: string, description: string[] | readonly string[], date: Date, content: string[],
type: 'ruleset' | 'domainset', surgePath: string, clashPath: string
) => {
) => parentSpan.traceChild(`create ruleset: ${path.basename(surgePath, path.extname(surgePath))}`).traceAsyncFn((childSpan) => {
const surgeContent = withBannerArray(title, description, date, content);
const clashContent = childSpan.traceChild('convert incoming ruleset to clash').traceSyncFn(() => {
let _clashContent;
switch (type) {
case 'domainset':
_clashContent = surgeDomainsetToClashDomainset(content);
break;
case 'ruleset':
_clashContent = surgeRulesetToClashClassicalTextRuleset(content);
break;
default:
throw new TypeError(`Unknown type: ${type as any}`);
}
return withBannerArray(title, description, date, _clashContent);
});
let _clashContent;
switch (type) {
case 'domainset':
_clashContent = surgeDomainsetToClashDomainset(content);
break;
case 'ruleset':
_clashContent = surgeRulesetToClashClassicalTextRuleset(content);
break;
default:
throw new TypeError(`Unknown type: ${type as any}`);
}
const clashContent = withBannerArray(title, description, date, _clashContent);
return [
compareAndWriteFile(surgeContent, surgePath),
compareAndWriteFile(clashContent, clashPath)
];
};
return Promise.all([
compareAndWriteFile(childSpan, surgeContent, surgePath),
compareAndWriteFile(childSpan, clashContent, clashPath)
]);
});

View File

@@ -41,17 +41,16 @@ export function readFileByLine(file: string | URL | BunFile) {
return createTextLineAsyncGeneratorFromStreamSource(file.stream());
}
export function createReadlineInterfaceFromResponse(resp: Response) {
export function createReadlineInterfaceFromResponse(this: void, resp: Response) {
if (!resp.body) {
throw new Error('Failed to fetch remote text');
}
if (resp.bodyUsed) {
throw new Error('Body has already been consumed.');
}
return createTextLineAsyncGeneratorFromStreamSource(resp.body);
}
export function fetchRemoteTextByLine(url: string | URL) {
return fetchWithRetry(url, defaultRequestInit).then(res => createReadlineInterfaceFromResponse(res));
return fetchWithRetry(url, defaultRequestInit).then(createReadlineInterfaceFromResponse);
}

View File

@@ -96,12 +96,12 @@ const enum ParseType {
}
export async function processFilterRules(
span: Span,
parentSpan: Span,
filterRulesUrl: string,
fallbackUrls?: readonly string[] | undefined | null,
ttl: number | null = null
): Promise<{ white: string[], black: string[], foundDebugDomain: boolean }> {
const [white, black, warningMessages] = await span.traceChild('process filter rules: domainListsUrl').traceAsyncFn(() => fsCache.apply<Readonly<[
const [white, black, warningMessages] = await parentSpan.traceChild(`process filter rules: ${filterRulesUrl}`).traceAsyncFn((span) => fsCache.apply<Readonly<[
white: string[],
black: string[],
warningMessages: string[]
@@ -179,18 +179,15 @@ export async function processFilterRules(
// Avoid event loop starvation, so we wait for a macrotask before we start fetching.
await Promise.resolve();
const filterRules = (await traceAsync(
picocolors.gray(`- download ${filterRulesUrl}`),
() => fetchAssets(filterRulesUrl, fallbackUrls),
picocolors.gray
)).split('\n');
const filterRules = await span.traceChild('download adguard filter').traceAsyncFn(() => {
return fetchAssets(filterRulesUrl, fallbackUrls).then(text => text.split('\n'));
});
const key = picocolors.gray(`- parse adguard filter ${filterRulesUrl}`);
console.time(key);
for (let i = 0, len = filterRules.length; i < len; i++) {
lineCb(filterRules[i]);
}
console.timeEnd(key);
span.traceChild('parse adguard filter').traceSyncFn(() => {
for (let i = 0, len = filterRules.length; i < len; i++) {
lineCb(filterRules[i]);
}
});
}
return [

View File

@@ -47,7 +47,9 @@ export const DOMAIN_LISTS = [
['https://raw.githubusercontent.com/AdguardTeam/cname-trackers/master/data/combined_disguised_mail_trackers_justdomains.txt', true, TTL.THREE_DAYS()]
] as const;
export const ADGUARD_FILTERS = [
type AdGuardFilterSource = string | [main: string, mirrors: string[] | null, ttl: number];
export const ADGUARD_FILTERS: AdGuardFilterSource[] = [
// EasyList
[
'https://easylist.to/easylist/easylist.txt',
@@ -156,7 +158,7 @@ export const ADGUARD_FILTERS = [
// Not actively maintained, let's use a 10 days cache ttl
['https://raw.githubusercontent.com/Spam404/lists/master/adblock-list.txt', null, TTL.TEN_DAYS()],
// Brave First Party & First Party CNAME
'https://raw.githubusercontent.com/brave/adblock-lists/master/brave-lists/brave-firstparty.txt'
['https://raw.githubusercontent.com/brave/adblock-lists/master/brave-lists/brave-firstparty.txt', null, TTL.ONE_DAY()]
] as const;
export const PREDEFINED_WHITELIST = [