@@ -28,20 +28,25 @@ export const RESERVED_TLDS = new Set([
2828/** A TLD label: lowercase letters, digits and dashes; no leading/trailing dash. */
2929const LABEL = / ^ [ a - z 0 - 9 ] (?: [ a - z 0 - 9 - ] { 0 , 61 } [ a - z 0 - 9 ] ) ? $ / ;
3030
31+ /** A hostname label. Unlike a TLD, an all-numeric label is valid. */
32+ export function normalizeLabel ( input ) {
33+ const raw = String ( input ?? "" ) . trim ( ) . toLowerCase ( ) ;
34+ return raw && raw . length <= 63 && LABEL . test ( raw ) ? raw : null ;
35+ }
36+
3137/**
3238 * Normalise user input into a bare TLD label, or null when it could never be
3339 * one. Accepts ".eggs", "eggs", " .EGGS " -- people type the dot.
3440 */
3541export function normalizeTld ( input ) {
3642 const raw = String ( input ?? "" ) . trim ( ) . toLowerCase ( ) . replace ( / ^ \. + / , "" ) ;
37- if ( ! raw || raw . length > 63 ) return null ;
3843 // A dot means they gave a domain, not a TLD. Say so rather than silently
3944 // registering the wrong thing.
40- if ( raw . includes ( "." ) ) return null ;
41- if ( ! LABEL . test ( raw ) ) return null ;
45+ const label = normalizeLabel ( raw ) ;
46+ if ( ! label ) return null ;
4247 // All-numeric would be ambiguous against an IPv4 literal in a hostname.
43- if ( / ^ \d + $ / . test ( raw ) ) return null ;
44- return raw ;
48+ if ( / ^ \d + $ / . test ( label ) ) return null ;
49+ return label ;
4550}
4651
4752/** Why a TLD cannot be registered, or null when it is fine. */
@@ -64,10 +69,10 @@ export function parseMoshpitName(input) {
6469 const parts = raw . split ( "." ) ;
6570 if ( parts . length !== 2 ) return null ;
6671 const [ label , tld ] = parts ;
67- // Both halves are hostname labels, and normalizeTld already encodes exactly
68- // that rule -- so reuse it rather than keeping a second copy that can drift.
69- if ( ! normalizeTld ( label ) || ! normalizeTld ( tld ) ) return null ;
70- return { label, tld } ;
72+ const normalizedLabel = normalizeLabel ( label ) ;
73+ const normalizedTld = normalizeTld ( tld ) ;
74+ if ( ! normalizedLabel || ! normalizedTld ) return null ;
75+ return { label : normalizedLabel , tld : normalizedTld } ;
7176}
7277
7378/* ---- resolution precedence (tronbrowser.dev) ---- */
0 commit comments