mirror of
https://github.com/SukkaW/Surge.git
synced 2026-01-29 01:51:52 +08:00
Perf: use tldts-experimental when possible
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
import { getGorhillPublicSuffixPromise } from './get-gorhill-publicsuffix';
|
||||
import { processDomainLists } from './parse-filter';
|
||||
import * as tldts from 'tldts';
|
||||
import { getSubdomain } from 'tldts';
|
||||
import { TTL } from './cache-filesystem';
|
||||
|
||||
import { add as SetAdd } from 'mnemonist/set';
|
||||
@@ -177,7 +177,7 @@ export function calcDomainAbuseScore(line: string) {
|
||||
}
|
||||
}
|
||||
|
||||
const subdomain = tldts.getSubdomain(line, { detectIp: false });
|
||||
const subdomain = getSubdomain(line, { detectIp: false });
|
||||
|
||||
if (subdomain) {
|
||||
if (subdomain.slice(1).includes('.')) {
|
||||
|
||||
@@ -1,10 +1,10 @@
|
||||
import * as tldts from 'tldts';
|
||||
import { parse as tldtsParse } from 'tldts';
|
||||
import { isProbablyIpv4 } from './is-fast-ip';
|
||||
export const normalizeDomain = (domain: string) => {
|
||||
if (!domain) return null;
|
||||
if (isProbablyIpv4(domain)) return null;
|
||||
|
||||
const parsed = tldts.parse(domain, { allowPrivateDomains: true, detectIp: false });
|
||||
const parsed = tldtsParse(domain, { allowPrivateDomains: true, detectIp: false });
|
||||
// if (parsed.isIp) return null;
|
||||
if (!parsed.hostname) return null;
|
||||
if (!parsed.isIcann && !parsed.isPrivate) return null;
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
import { fetchRemoteTextByLine } from './fetch-text-by-line';
|
||||
import { parse } from 'tldts';
|
||||
import { parse as tldtsParse } from 'tldts';
|
||||
|
||||
const isDomainLoose = (domain: string): boolean => {
|
||||
const { isIcann, isPrivate, isIp } = parse(domain);
|
||||
const { isIcann, isPrivate, isIp } = tldtsParse(domain);
|
||||
return !!(!isIp && (isIcann || isPrivate));
|
||||
};
|
||||
|
||||
|
||||
@@ -1,4 +1,7 @@
|
||||
import * as tldts from 'tldts';
|
||||
// tldts-experimental is way faster than tldts, but very little bit inaccurate
|
||||
// (since it is hashes based). But the result is still deterministic, which is
|
||||
// enough when sorting.
|
||||
import * as tldts from 'tldts-experimental';
|
||||
import { sort } from './timsort';
|
||||
|
||||
export const compare = (a: string, b: string) => {
|
||||
@@ -6,11 +9,11 @@ export const compare = (a: string, b: string) => {
|
||||
return (a.length - b.length) || a.localeCompare(b);
|
||||
};
|
||||
|
||||
const tldtsOpt = {
|
||||
extractHostname: false,
|
||||
const tldtsOpt: Parameters<typeof tldts.getDomain>[1] = {
|
||||
allowPrivateDomains: false,
|
||||
detectIp: false,
|
||||
extractHostname: false,
|
||||
validateHostname: false,
|
||||
detectIp: false,
|
||||
mixedInputs: false
|
||||
};
|
||||
|
||||
@@ -36,14 +39,16 @@ export const sortDomains = (inputs: string[]) => {
|
||||
const main_domain_a = domainMap.get(a)!;
|
||||
const main_domain_b = domainMap.get(b)!;
|
||||
|
||||
let t = compare(main_domain_a, main_domain_b);
|
||||
let t = compare(
|
||||
main_domain_a,
|
||||
main_domain_b
|
||||
) || compare(
|
||||
/** subdomain_a */ subdomainMap.get(a)!,
|
||||
/** subdomain_b */ subdomainMap.get(b)!
|
||||
);
|
||||
if (t !== 0) return t;
|
||||
|
||||
if (t === 0) {
|
||||
const subdomain_a = subdomainMap.get(a)!;
|
||||
const subdomain_b = subdomainMap.get(b)!;
|
||||
t = compare(subdomain_a, subdomain_b);
|
||||
}
|
||||
if (t === 0 && (a !== main_domain_a || b !== main_domain_b)) {
|
||||
if (a !== main_domain_a || b !== main_domain_b) {
|
||||
t = compare(a, b);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user