瀏覽代碼

Perf: make `normalizeDomain` faster

SukkaW 1 年之前
父節點
當前提交
264e30726a
共有 2 個文件被更改,包括 20 次插入8 次删除
  1. 7 4
      Build/constants/loose-tldts-opt.ts
  2. 13 4
      Build/lib/normalize-domain.ts

+ 7 - 4
Build/constants/loose-tldts-opt.ts

@@ -1,6 +1,6 @@
 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,
   extractHostname: false,
   validateHostname: false,
@@ -8,12 +8,15 @@ export const looseTldtsOpt: Parameters<typeof tldts.getSubdomain>[1] = {
   mixedInputs: false
 };
 
-export const loosTldOptWithPrivateDomains: Parameters<typeof tldts.getSubdomain>[1] = {
+export const loosTldOptWithPrivateDomains: NonNullable<Parameters<typeof tldts.getSubdomain>[1]> = {
   ...looseTldtsOpt,
   allowPrivateDomains: true
 };
 
-export const normalizeTldtsOpt: Parameters<typeof tldts.getSubdomain>[1] = {
+export const normalizeTldtsOpt: NonNullable<Parameters<typeof tldts.getSubdomain>[1]> = {
   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
 };

+ 13 - 4
Build/lib/normalize-domain.ts

@@ -3,14 +3,21 @@
 // import tldts from 'tldts-experimental';
 import tldts from 'tldts';
 import { normalizeTldtsOpt } from '../constants/loose-tldts-opt';
+import { isProbablyIpv4, isProbablyIpv6 } from 'foxts/is-probably-ip';
 
 type TldTsParsed = ReturnType<typeof tldts.parse>;
 
 /**
  * Skipped the input non-empty check, the `domain` should not be empty.
  */
-export function fastNormalizeDomain(domain: string, parsed: TldTsParsed = tldts.parse(domain, normalizeTldtsOpt)) {
-  if (parsed.isIp) return null;
+export function fastNormalizeDomain(domain: string, parsed: TldTsParsed | null = 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)
   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) {
   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)
   if (!parsed.isIcann && !parsed.isPrivate) return null;