ソースを参照

Chore: improve trace output

SukkaW 1 年間 前
コミット
34c316f509

+ 15 - 16
Build/lib/rules/base.ts

@@ -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,25 +442,24 @@ 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(
-              childSpan,
-              this.title,
-              this.description,
-              this.date,
-              path.join(
-                strategy.outputDir,
-                strategy.type
-                  ? path.join(strategy.type, basename)
-                  : basename
-              )
-            ));
+            promises.push(
+              childSpan.traceChildAsync('write ' + strategy.name, (childSpan) => Promise.resolve(strategy.output(
+                childSpan,
+                nullthrow(this.title, 'Missing title'),
+                nullthrow(this.description, 'Missing description'),
+                this.date,
+                path.join(
+                  strategy.outputDir,
+                  strategy.type
+                    ? path.join(strategy.type, basename)
+                    : basename
+                )
+              )))
+            );
           }
           }
         }
         }
 
 

+ 2 - 0
Build/lib/writing-strategy/adguardhome.ts

@@ -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 = '';

+ 2 - 0
Build/lib/writing-strategy/base.ts

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

+ 6 - 0
Build/lib/writing-strategy/clash.ts

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

+ 2 - 0
Build/lib/writing-strategy/singbox.ts

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

+ 6 - 0
Build/lib/writing-strategy/surge.ts

@@ -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 = '';