mirror of
https://github.com/SukkaW/Surge.git
synced 2026-01-29 01:51:52 +08:00
Refactor: full span tracer
This commit is contained in:
@@ -6,6 +6,7 @@ import { readFileByLine } from './lib/fetch-text-by-line';
|
||||
import { processLine } from './lib/process-line';
|
||||
import { createRuleset } from './lib/create-file';
|
||||
import { domainDeduper } from './lib/domain-deduper';
|
||||
import type { Span } from './trace';
|
||||
import { task } from './trace';
|
||||
import { SHARED_DESCRIPTION } from './lib/constants';
|
||||
|
||||
@@ -17,7 +18,7 @@ const sourceDir = path.resolve(import.meta.dir, '../Source');
|
||||
const outputSurgeDir = path.resolve(import.meta.dir, '../List');
|
||||
const outputClashDir = path.resolve(import.meta.dir, '../Clash');
|
||||
|
||||
export const buildCommon = task(import.meta.path, async () => {
|
||||
export const buildCommon = task(import.meta.path, async (span) => {
|
||||
const promises: Array<Promise<unknown>> = [];
|
||||
|
||||
const pw = new PathScurry(sourceDir);
|
||||
@@ -33,14 +34,14 @@ export const buildCommon = task(import.meta.path, async () => {
|
||||
|
||||
const relativePath = entry.relative();
|
||||
if (relativePath.startsWith('domainset/')) {
|
||||
promises.push(transformDomainset(entry.fullpath(), relativePath));
|
||||
promises.push(transformDomainset(span, entry.fullpath(), relativePath));
|
||||
continue;
|
||||
}
|
||||
if (
|
||||
relativePath.startsWith('ip/')
|
||||
|| relativePath.startsWith('non_ip/')
|
||||
) {
|
||||
promises.push(transformRuleset(entry.fullpath(), relativePath));
|
||||
promises.push(transformRuleset(span, entry.fullpath(), relativePath));
|
||||
continue;
|
||||
}
|
||||
}
|
||||
@@ -52,46 +53,50 @@ if (import.meta.main) {
|
||||
buildCommon();
|
||||
}
|
||||
|
||||
const processFile = async (sourcePath: string) => {
|
||||
console.log('Processing', sourcePath);
|
||||
const processFile = (span: Span, sourcePath: string) => {
|
||||
// console.log('Processing', sourcePath);
|
||||
return span.traceChild(`process file: ${sourcePath}`).traceAsyncFn(async () => {
|
||||
const lines: string[] = [];
|
||||
|
||||
const lines: string[] = [];
|
||||
let title = '';
|
||||
const descriptions: string[] = [];
|
||||
|
||||
let title = '';
|
||||
const descriptions: string[] = [];
|
||||
try {
|
||||
for await (const line of readFileByLine(sourcePath)) {
|
||||
if (line === MAGIC_COMMAND_SKIP) {
|
||||
return null;
|
||||
}
|
||||
|
||||
try {
|
||||
for await (const line of readFileByLine(sourcePath)) {
|
||||
if (line === MAGIC_COMMAND_SKIP) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (line.startsWith(MAGIC_COMMAND_TITLE)) {
|
||||
title = line.slice(MAGIC_COMMAND_TITLE.length).trim();
|
||||
continue;
|
||||
}
|
||||
|
||||
if (line.startsWith(MAGIC_COMMAND_DESCRIPTION)) {
|
||||
descriptions.push(line.slice(MAGIC_COMMAND_DESCRIPTION.length).trim());
|
||||
continue;
|
||||
}
|
||||
|
||||
const l = processLine(line);
|
||||
if (l) {
|
||||
lines.push(l);
|
||||
if (line.startsWith(MAGIC_COMMAND_TITLE)) {
|
||||
title = line.slice(MAGIC_COMMAND_TITLE.length).trim();
|
||||
continue;
|
||||
}
|
||||
|
||||
if (line.startsWith(MAGIC_COMMAND_DESCRIPTION)) {
|
||||
descriptions.push(line.slice(MAGIC_COMMAND_DESCRIPTION.length).trim());
|
||||
continue;
|
||||
}
|
||||
|
||||
const l = processLine(line);
|
||||
if (l) {
|
||||
lines.push(l);
|
||||
}
|
||||
}
|
||||
} catch (e) {
|
||||
console.error('Error processing', sourcePath);
|
||||
console.trace(e);
|
||||
}
|
||||
} catch (e) {
|
||||
console.error('Error processing', sourcePath);
|
||||
console.trace(e);
|
||||
}
|
||||
|
||||
return [title, descriptions, lines] as const;
|
||||
return [title, descriptions, lines] as const;
|
||||
});
|
||||
};
|
||||
|
||||
async function transformDomainset(sourcePath: string, relativePath: string) {
|
||||
const res = await processFile(sourcePath);
|
||||
async function transformDomainset(parentSpan: Span, sourcePath: string, relativePath: string) {
|
||||
const span = parentSpan.traceChild(`transform domainset: ${path.basename(sourcePath, path.extname(sourcePath))}`);
|
||||
|
||||
const res = await processFile(span, sourcePath);
|
||||
if (!res) return;
|
||||
|
||||
const [title, descriptions, lines] = res;
|
||||
|
||||
const deduped = domainDeduper(lines);
|
||||
@@ -104,7 +109,8 @@ async function transformDomainset(sourcePath: string, relativePath: string) {
|
||||
)
|
||||
];
|
||||
|
||||
return Promise.all(createRuleset(
|
||||
return createRuleset(
|
||||
span,
|
||||
title,
|
||||
description,
|
||||
new Date(),
|
||||
@@ -112,15 +118,18 @@ async function transformDomainset(sourcePath: string, relativePath: string) {
|
||||
'domainset',
|
||||
path.resolve(outputSurgeDir, relativePath),
|
||||
path.resolve(outputClashDir, `${relativePath.slice(0, -path.extname(relativePath).length)}.txt`)
|
||||
));
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Output Surge RULE-SET and Clash classical text format
|
||||
*/
|
||||
async function transformRuleset(sourcePath: string, relativePath: string) {
|
||||
const res = await processFile(sourcePath);
|
||||
if (!res) return;
|
||||
async function transformRuleset(parentSpan: Span, sourcePath: string, relativePath: string) {
|
||||
const span = parentSpan.traceChild(`transform ruleset: ${path.basename(sourcePath, path.extname(sourcePath))}`);
|
||||
|
||||
const res = await processFile(span, sourcePath);
|
||||
if (!res) return null;
|
||||
|
||||
const [title, descriptions, lines] = res;
|
||||
|
||||
const description = [
|
||||
@@ -132,7 +141,8 @@ async function transformRuleset(sourcePath: string, relativePath: string) {
|
||||
)
|
||||
];
|
||||
|
||||
return Promise.all(createRuleset(
|
||||
return createRuleset(
|
||||
span,
|
||||
title,
|
||||
description,
|
||||
new Date(),
|
||||
@@ -140,5 +150,5 @@ async function transformRuleset(sourcePath: string, relativePath: string) {
|
||||
'ruleset',
|
||||
path.resolve(outputSurgeDir, relativePath),
|
||||
path.resolve(outputClashDir, `${relativePath.slice(0, -path.extname(relativePath).length)}.txt`)
|
||||
));
|
||||
);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user