mirror of
https://github.com/SukkaW/Surge.git
synced 2026-01-29 01:51:52 +08:00
Improve ruleset build process
This commit is contained in:
@@ -5,56 +5,53 @@ import { RuleOutput } from './base';
|
||||
import type { SingboxSourceFormat } from '../singbox';
|
||||
import { nullthrow } from 'foxact/nullthrow';
|
||||
|
||||
export class DomainsetOutput extends RuleOutput {
|
||||
type Preprocessed = string[];
|
||||
|
||||
export class DomainsetOutput extends RuleOutput<Preprocessed> {
|
||||
protected type = 'domainset' as const;
|
||||
|
||||
private $sorted: string[] | null = null;
|
||||
preprocess() {
|
||||
const kwfilter = createKeywordFilter(this.domainKeywords);
|
||||
|
||||
get sorted() {
|
||||
if (!this.$sorted) {
|
||||
const kwfilter = createKeywordFilter(this.domainKeywords);
|
||||
const results: string[] = [];
|
||||
|
||||
const results: string[] = [];
|
||||
const dumped = this.domainTrie.dump();
|
||||
|
||||
const dumped = this.domainTrie.dump();
|
||||
|
||||
for (let i = 0, len = dumped.length; i < len; i++) {
|
||||
const domain = dumped[i];
|
||||
if (!kwfilter(domain)) {
|
||||
results.push(domain);
|
||||
}
|
||||
for (let i = 0, len = dumped.length; i < len; i++) {
|
||||
const domain = dumped[i];
|
||||
if (!kwfilter(domain)) {
|
||||
results.push(domain);
|
||||
}
|
||||
|
||||
const sorted = sortDomains(results, this.apexDomainMap, this.subDomainMap);
|
||||
sorted.push('this_ruleset_is_made_by_sukkaw.ruleset.skk.moe');
|
||||
|
||||
this.$sorted = sorted;
|
||||
}
|
||||
return this.$sorted;
|
||||
|
||||
const sorted = sortDomains(results, this.apexDomainMap, this.subDomainMap);
|
||||
sorted.push('this_ruleset_is_made_by_sukkaw.ruleset.skk.moe');
|
||||
|
||||
return sorted;
|
||||
}
|
||||
|
||||
calcDomainMap() {
|
||||
if (!this.apexDomainMap || !this.subDomainMap) {
|
||||
const { domainMap, subdomainMap } = buildParseDomainMap(this.sorted);
|
||||
const { domainMap, subdomainMap } = buildParseDomainMap(this.$preprocessed);
|
||||
this.apexDomainMap = domainMap;
|
||||
this.subDomainMap = subdomainMap;
|
||||
}
|
||||
}
|
||||
|
||||
surge(): string[] {
|
||||
return this.sorted;
|
||||
return this.$preprocessed;
|
||||
}
|
||||
|
||||
clash(): string[] {
|
||||
return this.sorted.map(i => (i[0] === '.' ? `+${i}` : i));
|
||||
return this.$preprocessed.map(i => (i[0] === '.' ? `+${i}` : i));
|
||||
}
|
||||
|
||||
singbox(): string[] {
|
||||
const domains: string[] = [];
|
||||
const domainSuffixes: string[] = [];
|
||||
|
||||
for (let i = 0, len = this.sorted.length; i < len; i++) {
|
||||
const domain = this.sorted[i];
|
||||
for (let i = 0, len = this.$preprocessed.length; i < len; i++) {
|
||||
const domain = this.$preprocessed[i];
|
||||
if (domain[0] === '.') {
|
||||
domainSuffixes.push(domain.slice(1));
|
||||
} else {
|
||||
@@ -72,11 +69,11 @@ export class DomainsetOutput extends RuleOutput {
|
||||
}
|
||||
|
||||
getStatMap() {
|
||||
invariant(this.sorted, 'Non dumped yet');
|
||||
invariant(this.$preprocessed, 'Non dumped yet');
|
||||
invariant(this.apexDomainMap, 'Missing apex domain map');
|
||||
|
||||
return Array.from(
|
||||
nullthrow(this.sorted, 'Non dumped yet')
|
||||
nullthrow(this.$preprocessed, 'Non dumped yet')
|
||||
.reduce<Map<string, number>>((acc, cur) => {
|
||||
const suffix = this.apexDomainMap!.get(cur);
|
||||
if (suffix) {
|
||||
|
||||
Reference in New Issue
Block a user