Chore: improve trace output

This commit is contained in:
SukkaW 2025-03-02 00:28:44 +08:00
parent 64317794b0
commit 34c316f509
6 changed files with 33 additions and 16 deletions

View File

@ -1,6 +1,6 @@
import type { Span } from '../../trace'; import type { Span } from '../../trace';
import { HostnameSmolTrie } from '../trie'; import { HostnameSmolTrie } from '../trie';
import { invariant, not } from 'foxts/guard'; import { not, nullthrow } from 'foxts/guard';
import type { MaybePromise } from '../misc'; import type { MaybePromise } from '../misc';
import type { BaseWriteStrategy } from '../writing-strategy/base'; import type { BaseWriteStrategy } from '../writing-strategy/base';
import { merge as mergeCidr } from 'fast-cidr-tools'; import { merge as mergeCidr } from 'fast-cidr-tools';
@ -442,17 +442,15 @@ export class FileOutput {
return childSpan.traceChildAsync('output to disk', (childSpan) => { return childSpan.traceChildAsync('output to disk', (childSpan) => {
const promises: Array<Promise<void> | void> = []; const promises: Array<Promise<void> | void> = [];
invariant(this.title, 'Missing title');
invariant(this.description, 'Missing description');
for (let i = 0, len = this.strategies.length; i < len; i++) { for (let i = 0, len = this.strategies.length; i < len; i++) {
const strategy = this.strategies[i]; const strategy = this.strategies[i];
if (strategy) { if (strategy) {
const basename = (strategy.overwriteFilename || this.id) + '.' + strategy.fileExtension; const basename = (strategy.overwriteFilename || this.id) + '.' + strategy.fileExtension;
promises.push(strategy.output( promises.push(
childSpan.traceChildAsync('write ' + strategy.name, (childSpan) => Promise.resolve(strategy.output(
childSpan, childSpan,
this.title, nullthrow(this.title, 'Missing title'),
this.description, nullthrow(this.description, 'Missing description'),
this.date, this.date,
path.join( path.join(
strategy.outputDir, strategy.outputDir,
@ -460,7 +458,8 @@ export class FileOutput {
? path.join(strategy.type, basename) ? path.join(strategy.type, basename)
: basename : basename
) )
)); )))
);
} }
} }

View File

@ -4,6 +4,8 @@ import { noop } from 'foxts/noop';
import { notSupported } from '../misc'; import { notSupported } from '../misc';
export class AdGuardHome extends BaseWriteStrategy { export class AdGuardHome extends BaseWriteStrategy {
public readonly name = 'adguardhome';
// readonly type = 'domainset'; // readonly type = 'domainset';
readonly fileExtension = 'txt'; readonly fileExtension = 'txt';
readonly type = ''; readonly type = '';

View File

@ -6,6 +6,8 @@ import { compareAndWriteFile } from '../create-file';
* date is written to a file. * date is written to a file.
*/ */
export abstract class BaseWriteStrategy { export abstract class BaseWriteStrategy {
public abstract readonly name: string;
/** /**
* Sometimes a ruleset will create extra files (e.g. reject-url-regex w/ mitm.sgmodule), * Sometimes a ruleset will create extra files (e.g. reject-url-regex w/ mitm.sgmodule),
* and doesn't share the same filename and id. This property is used to overwrite the filename. * and doesn't share the same filename and id. This property is used to overwrite the filename.

View File

@ -6,6 +6,8 @@ import { OUTPUT_CLASH_DIR } from '../../constants/dir';
import { appendArrayInPlace } from '../append-array-in-place'; import { appendArrayInPlace } from '../append-array-in-place';
export class ClashDomainSet extends BaseWriteStrategy { export class ClashDomainSet extends BaseWriteStrategy {
public readonly name = 'clash domainset';
// readonly type = 'domainset'; // readonly type = 'domainset';
readonly fileExtension = 'txt'; readonly fileExtension = 'txt';
readonly type = 'domainset'; readonly type = 'domainset';
@ -43,6 +45,8 @@ export class ClashDomainSet extends BaseWriteStrategy {
} }
export class ClashIPSet extends BaseWriteStrategy { export class ClashIPSet extends BaseWriteStrategy {
public readonly name = 'clash ipcidr';
// readonly type = 'domainset'; // readonly type = 'domainset';
readonly fileExtension = 'txt'; readonly fileExtension = 'txt';
readonly type = 'ip'; readonly type = 'ip';
@ -80,6 +84,8 @@ export class ClashIPSet extends BaseWriteStrategy {
} }
export class ClashClassicRuleSet extends BaseWriteStrategy { export class ClashClassicRuleSet extends BaseWriteStrategy {
public readonly name = 'clash classic ruleset';
readonly fileExtension = 'txt'; readonly fileExtension = 'txt';
protected result: string[] = ['DOMAIN,this_ruleset_is_made_by_sukkaw.ruleset.skk.moe']; protected result: string[] = ['DOMAIN,this_ruleset_is_made_by_sukkaw.ruleset.skk.moe'];

View File

@ -26,6 +26,8 @@ export interface SingboxSourceFormat {
} }
export class SingboxSource extends BaseWriteStrategy { export class SingboxSource extends BaseWriteStrategy {
public readonly name = 'singbox';
readonly fileExtension = 'json'; readonly fileExtension = 'json';
static readonly jsonToLines = (json: unknown): string[] => stringify(json).split('\n'); static readonly jsonToLines = (json: unknown): string[] => stringify(json).split('\n');

View File

@ -9,6 +9,8 @@ import { OUTPUT_MODULES_DIR, OUTPUT_SURGE_DIR } from '../../constants/dir';
import { withBannerArray, withIdentityContent } from '../misc'; import { withBannerArray, withIdentityContent } from '../misc';
export class SurgeDomainSet extends BaseWriteStrategy { export class SurgeDomainSet extends BaseWriteStrategy {
public readonly name = 'surge domainset';
// readonly type = 'domainset'; // readonly type = 'domainset';
readonly fileExtension = 'conf'; readonly fileExtension = 'conf';
type = 'domainset'; type = 'domainset';
@ -46,6 +48,8 @@ export class SurgeDomainSet extends BaseWriteStrategy {
} }
export class SurgeRuleSet extends BaseWriteStrategy { export class SurgeRuleSet extends BaseWriteStrategy {
public readonly name = 'surge ruleset';
readonly fileExtension = 'conf'; readonly fileExtension = 'conf';
protected result: string[] = ['DOMAIN,this_ruleset_is_made_by_sukkaw.ruleset.skk.moe']; protected result: string[] = ['DOMAIN,this_ruleset_is_made_by_sukkaw.ruleset.skk.moe'];
@ -132,6 +136,8 @@ export class SurgeRuleSet extends BaseWriteStrategy {
} }
export class SurgeMitmSgmodule extends BaseWriteStrategy { export class SurgeMitmSgmodule extends BaseWriteStrategy {
public readonly name = 'surge sgmodule';
// readonly type = 'domainset'; // readonly type = 'domainset';
readonly fileExtension = 'sgmodule'; readonly fileExtension = 'sgmodule';
readonly type = ''; readonly type = '';