mirror of
https://github.com/SukkaW/Surge.git
synced 2025-12-12 09:10:35 +08:00
Perf: reduce condition when output to strategy
This commit is contained in:
parent
02e07668d0
commit
f926970b42
@ -13,7 +13,7 @@ import { SurgeMitmSgmodule } from '../writing-strategy/surge';
|
|||||||
* This class is not about format, instead it will call the class that does
|
* This class is not about format, instead it will call the class that does
|
||||||
*/
|
*/
|
||||||
export class FileOutput {
|
export class FileOutput {
|
||||||
protected strategies: Array<BaseWriteStrategy | false> = [];
|
protected strategies: BaseWriteStrategy[] = [];
|
||||||
|
|
||||||
public domainTrie = new HostnameSmolTrie(null);
|
public domainTrie = new HostnameSmolTrie(null);
|
||||||
protected domainKeywords = new Set<string>();
|
protected domainKeywords = new Set<string>();
|
||||||
@ -57,15 +57,13 @@ export class FileOutput {
|
|||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public withStrategies(strategies: Array<BaseWriteStrategy | false>) {
|
public withStrategies(strategies: BaseWriteStrategy[]) {
|
||||||
this.strategies = strategies;
|
this.strategies = strategies;
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
withExtraStrategies(strategy: BaseWriteStrategy | false) {
|
withExtraStrategies(strategy: BaseWriteStrategy) {
|
||||||
if (strategy) {
|
this.strategies.push(strategy);
|
||||||
this.strategies.push(strategy);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
protected description: string[] | readonly string[] | null = null;
|
protected description: string[] | readonly string[] | null = null;
|
||||||
@ -304,27 +302,24 @@ export class FileOutput {
|
|||||||
throw new Error('No strategies to write ' + this.id);
|
throw new Error('No strategies to write ' + this.id);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const strategiesLen = this.strategies.length;
|
||||||
this.domainTrie.dumpWithoutDot((domain, includeAllSubdomain) => {
|
this.domainTrie.dumpWithoutDot((domain, includeAllSubdomain) => {
|
||||||
if (kwfilter(domain)) {
|
if (kwfilter(domain)) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
for (let i = 0, len = this.strategies.length; i < len; i++) {
|
for (let i = 0; i < strategiesLen; i++) {
|
||||||
const strategy = this.strategies[i];
|
const strategy = this.strategies[i];
|
||||||
if (strategy) {
|
if (includeAllSubdomain) {
|
||||||
if (includeAllSubdomain) {
|
strategy.writeDomainSuffix(domain);
|
||||||
strategy.writeDomainSuffix(domain);
|
} else {
|
||||||
} else {
|
strategy.writeDomain(domain);
|
||||||
strategy.writeDomain(domain);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}, true);
|
}, true);
|
||||||
|
|
||||||
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) continue;
|
|
||||||
|
|
||||||
if (this.domainKeywords.size) {
|
if (this.domainKeywords.size) {
|
||||||
strategy.writeDomainKeywords(this.domainKeywords);
|
strategy.writeDomainKeywords(this.domainKeywords);
|
||||||
}
|
}
|
||||||
@ -348,28 +343,23 @@ export class FileOutput {
|
|||||||
if (this.sourceIpOrCidr.size) {
|
if (this.sourceIpOrCidr.size) {
|
||||||
const sourceIpOrCidr = Array.from(this.sourceIpOrCidr);
|
const sourceIpOrCidr = Array.from(this.sourceIpOrCidr);
|
||||||
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];
|
this.strategies[i].writeSourceIpCidrs(sourceIpOrCidr);
|
||||||
if (strategy) {
|
|
||||||
strategy.writeSourceIpCidrs(sourceIpOrCidr);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
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 (this.sourcePort.size) {
|
||||||
if (this.sourcePort.size) {
|
strategy.writeSourcePorts(this.sourcePort);
|
||||||
strategy.writeSourcePorts(this.sourcePort);
|
}
|
||||||
}
|
if (this.destPort.size) {
|
||||||
if (this.destPort.size) {
|
strategy.writeDestinationPorts(this.destPort);
|
||||||
strategy.writeDestinationPorts(this.destPort);
|
}
|
||||||
}
|
if (this.otherRules.length) {
|
||||||
if (this.otherRules.length) {
|
strategy.writeOtherRules(this.otherRules);
|
||||||
strategy.writeOtherRules(this.otherRules);
|
}
|
||||||
}
|
if (this.urlRegex.size) {
|
||||||
if (this.urlRegex.size) {
|
strategy.writeUrlRegexes(this.urlRegex);
|
||||||
strategy.writeUrlRegexes(this.urlRegex);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -393,34 +383,32 @@ export class FileOutput {
|
|||||||
|
|
||||||
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) {
|
// no-resolve
|
||||||
// no-resolve
|
if (ipcidrNoResolve?.length) {
|
||||||
if (ipcidrNoResolve?.length) {
|
strategy.writeIpCidrs(ipcidrNoResolve, true);
|
||||||
strategy.writeIpCidrs(ipcidrNoResolve, true);
|
}
|
||||||
}
|
if (ipcidr6NoResolve?.length) {
|
||||||
if (ipcidr6NoResolve?.length) {
|
strategy.writeIpCidr6s(ipcidr6NoResolve, true);
|
||||||
strategy.writeIpCidr6s(ipcidr6NoResolve, true);
|
}
|
||||||
}
|
if (this.ipasnNoResolve.size) {
|
||||||
if (this.ipasnNoResolve.size) {
|
strategy.writeIpAsns(this.ipasnNoResolve, true);
|
||||||
strategy.writeIpAsns(this.ipasnNoResolve, true);
|
}
|
||||||
}
|
if (this.groipNoResolve.size) {
|
||||||
if (this.groipNoResolve.size) {
|
strategy.writeGeoip(this.groipNoResolve, true);
|
||||||
strategy.writeGeoip(this.groipNoResolve, true);
|
}
|
||||||
}
|
|
||||||
|
|
||||||
// triggers DNS resolution
|
// triggers DNS resolution
|
||||||
if (ipcidr?.length) {
|
if (ipcidr?.length) {
|
||||||
strategy.writeIpCidrs(ipcidr, false);
|
strategy.writeIpCidrs(ipcidr, false);
|
||||||
}
|
}
|
||||||
if (ipcidr6?.length) {
|
if (ipcidr6?.length) {
|
||||||
strategy.writeIpCidr6s(ipcidr6, false);
|
strategy.writeIpCidr6s(ipcidr6, false);
|
||||||
}
|
}
|
||||||
if (this.ipasn.size) {
|
if (this.ipasn.size) {
|
||||||
strategy.writeIpAsns(this.ipasn, false);
|
strategy.writeIpAsns(this.ipasn, false);
|
||||||
}
|
}
|
||||||
if (this.geoip.size) {
|
if (this.geoip.size) {
|
||||||
strategy.writeGeoip(this.geoip, false);
|
strategy.writeGeoip(this.geoip, false);
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -435,23 +423,22 @@ export class FileOutput {
|
|||||||
|
|
||||||
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) {
|
|
||||||
const basename = (strategy.overwriteFilename || this.id) + '.' + strategy.fileExtension;
|
const basename = (strategy.overwriteFilename || this.id) + '.' + strategy.fileExtension;
|
||||||
promises.push(
|
promises.push(
|
||||||
childSpan.traceChildAsync('write ' + strategy.name, (childSpan) => Promise.resolve(strategy.output(
|
childSpan.traceChildAsync('write ' + strategy.name, (childSpan) => Promise.resolve(strategy.output(
|
||||||
childSpan,
|
childSpan,
|
||||||
nullthrow(this.title, 'Missing title'),
|
nullthrow(this.title, 'Missing title'),
|
||||||
nullthrow(this.description, 'Missing description'),
|
nullthrow(this.description, 'Missing description'),
|
||||||
this.date,
|
this.date,
|
||||||
path.join(
|
path.join(
|
||||||
strategy.outputDir,
|
strategy.outputDir,
|
||||||
strategy.type
|
strategy.type
|
||||||
? path.join(strategy.type, basename)
|
? path.join(strategy.type, basename)
|
||||||
: basename
|
: basename
|
||||||
)
|
)
|
||||||
)))
|
)))
|
||||||
);
|
);
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return Promise.all(promises);
|
return Promise.all(promises);
|
||||||
@ -464,11 +451,7 @@ export class FileOutput {
|
|||||||
this.writeToStrategies();
|
this.writeToStrategies();
|
||||||
|
|
||||||
return this.strategies.reduce<Array<string[] | null>>((acc, strategy) => {
|
return this.strategies.reduce<Array<string[] | null>>((acc, strategy) => {
|
||||||
if (strategy) {
|
acc.push(strategy.content);
|
||||||
acc.push(strategy.content);
|
|
||||||
} else {
|
|
||||||
acc.push(null);
|
|
||||||
}
|
|
||||||
return acc;
|
return acc;
|
||||||
}, []);
|
}, []);
|
||||||
}
|
}
|
||||||
|
|||||||
@ -7,7 +7,7 @@ import { SurgeDomainSet } from '../writing-strategy/surge';
|
|||||||
import { FileOutput } from './base';
|
import { FileOutput } from './base';
|
||||||
|
|
||||||
export class DomainsetOutput extends FileOutput {
|
export class DomainsetOutput extends FileOutput {
|
||||||
strategies: Array<false | BaseWriteStrategy> = [
|
strategies: BaseWriteStrategy[] = [
|
||||||
new SurgeDomainSet(),
|
new SurgeDomainSet(),
|
||||||
new ClashDomainSet(),
|
new ClashDomainSet(),
|
||||||
new SingboxSource('domainset')
|
new SingboxSource('domainset')
|
||||||
@ -15,7 +15,7 @@ export class DomainsetOutput extends FileOutput {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export class AdGuardHomeOutput extends FileOutput {
|
export class AdGuardHomeOutput extends FileOutput {
|
||||||
strategies: Array<false | BaseWriteStrategy>;
|
strategies: BaseWriteStrategy[];
|
||||||
|
|
||||||
constructor(
|
constructor(
|
||||||
span: Span,
|
span: Span,
|
||||||
|
|||||||
@ -6,7 +6,7 @@ import { SurgeRuleSet } from '../writing-strategy/surge';
|
|||||||
import { FileOutput } from './base';
|
import { FileOutput } from './base';
|
||||||
|
|
||||||
export class IPListOutput extends FileOutput {
|
export class IPListOutput extends FileOutput {
|
||||||
strategies: Array<false | BaseWriteStrategy>;
|
strategies: BaseWriteStrategy[];
|
||||||
|
|
||||||
constructor(span: Span, id: string, private readonly clashUseRule = true) {
|
constructor(span: Span, id: string, private readonly clashUseRule = true) {
|
||||||
super(span, id);
|
super(span, id);
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user