mirror of
https://github.com/SukkaW/Surge.git
synced 2025-12-12 09:10:35 +08:00
Perf: make normalizeDomain faster
This commit is contained in:
parent
7683775dee
commit
264e30726a
@ -1,6 +1,6 @@
|
|||||||
import type * as tldts from 'tldts';
|
import type * as tldts from 'tldts';
|
||||||
|
|
||||||
export const looseTldtsOpt: Parameters<typeof tldts.getSubdomain>[1] = {
|
export const looseTldtsOpt: NonNullable<Parameters<typeof tldts.getSubdomain>[1]> = {
|
||||||
allowPrivateDomains: false,
|
allowPrivateDomains: false,
|
||||||
extractHostname: false,
|
extractHostname: false,
|
||||||
validateHostname: false,
|
validateHostname: false,
|
||||||
@ -8,12 +8,15 @@ export const looseTldtsOpt: Parameters<typeof tldts.getSubdomain>[1] = {
|
|||||||
mixedInputs: false
|
mixedInputs: false
|
||||||
};
|
};
|
||||||
|
|
||||||
export const loosTldOptWithPrivateDomains: Parameters<typeof tldts.getSubdomain>[1] = {
|
export const loosTldOptWithPrivateDomains: NonNullable<Parameters<typeof tldts.getSubdomain>[1]> = {
|
||||||
...looseTldtsOpt,
|
...looseTldtsOpt,
|
||||||
allowPrivateDomains: true
|
allowPrivateDomains: true
|
||||||
};
|
};
|
||||||
|
|
||||||
export const normalizeTldtsOpt: Parameters<typeof tldts.getSubdomain>[1] = {
|
export const normalizeTldtsOpt: NonNullable<Parameters<typeof tldts.getSubdomain>[1]> = {
|
||||||
allowPrivateDomains: true,
|
allowPrivateDomains: true,
|
||||||
detectIp: true
|
// in normalizeDomain, we only care if it contains IP, we don't care if we need to extract it
|
||||||
|
// by setting detectIp to false and manually check ip outside tldts.parse, we can skip the tldts
|
||||||
|
// inner "extractHostname" call
|
||||||
|
detectIp: false
|
||||||
};
|
};
|
||||||
|
|||||||
@ -3,14 +3,21 @@
|
|||||||
// import tldts from 'tldts-experimental';
|
// import tldts from 'tldts-experimental';
|
||||||
import tldts from 'tldts';
|
import tldts from 'tldts';
|
||||||
import { normalizeTldtsOpt } from '../constants/loose-tldts-opt';
|
import { normalizeTldtsOpt } from '../constants/loose-tldts-opt';
|
||||||
|
import { isProbablyIpv4, isProbablyIpv6 } from 'foxts/is-probably-ip';
|
||||||
|
|
||||||
type TldTsParsed = ReturnType<typeof tldts.parse>;
|
type TldTsParsed = ReturnType<typeof tldts.parse>;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Skipped the input non-empty check, the `domain` should not be empty.
|
* Skipped the input non-empty check, the `domain` should not be empty.
|
||||||
*/
|
*/
|
||||||
export function fastNormalizeDomain(domain: string, parsed: TldTsParsed = tldts.parse(domain, normalizeTldtsOpt)) {
|
export function fastNormalizeDomain(domain: string, parsed: TldTsParsed | null = null) {
|
||||||
if (parsed.isIp) return null;
|
// We don't want tldts to call its own "extractHostname" on ip, bail out ip first.
|
||||||
|
// Now ip has been bailed out, we can safely set normalizeTldtsOpt.detectIp to false.
|
||||||
|
if (isProbablyIpv4(domain) || isProbablyIpv6(domain)) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
parsed ??= tldts.parse(domain, normalizeTldtsOpt);
|
||||||
// Private invalid domain (things like .tor, .dn42, etc)
|
// Private invalid domain (things like .tor, .dn42, etc)
|
||||||
if (!parsed.isIcann && !parsed.isPrivate) return null;
|
if (!parsed.isIcann && !parsed.isPrivate) return null;
|
||||||
|
|
||||||
@ -20,9 +27,11 @@ export function fastNormalizeDomain(domain: string, parsed: TldTsParsed = tldts.
|
|||||||
export function normalizeDomain(domain: string, parsed: TldTsParsed | null = null) {
|
export function normalizeDomain(domain: string, parsed: TldTsParsed | null = null) {
|
||||||
if (domain.length === 0) return null;
|
if (domain.length === 0) return null;
|
||||||
|
|
||||||
parsed ??= tldts.parse(domain, normalizeTldtsOpt);
|
if (isProbablyIpv4(domain) || isProbablyIpv6(domain)) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
if (parsed.isIp) return null;
|
parsed ??= tldts.parse(domain, normalizeTldtsOpt);
|
||||||
// Private invalid domain (things like .tor, .dn42, etc)
|
// Private invalid domain (things like .tor, .dn42, etc)
|
||||||
if (!parsed.isIcann && !parsed.isPrivate) return null;
|
if (!parsed.isIcann && !parsed.isPrivate) return null;
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user