tldts
Version:
Library to work against complex domain names, subdomains and URIs.
953 lines (942 loc) • 188 kB
JavaScript
'use strict';
/**
* Check if `vhost` is a valid suffix of `hostname` (top-domain)
*
* It means that `vhost` needs to be a suffix of `hostname` and we then need to
* make sure that: either they are equal, or the character preceding `vhost` in
* `hostname` is a '.' (it should not be a partial label).
*
* * hostname = 'not.evil.com' and vhost = 'vil.com' => not ok
* * hostname = 'not.evil.com' and vhost = 'evil.com' => ok
* * hostname = 'not.evil.com' and vhost = 'not.evil.com' => ok
*/
function shareSameDomainSuffix(hostname, vhost) {
if (hostname.endsWith(vhost)) {
return (hostname.length === vhost.length ||
hostname[hostname.length - vhost.length - 1] === '.');
}
return false;
}
/**
* Given a hostname and its public suffix, extract the general domain.
*/
function extractDomainWithSuffix(hostname, publicSuffix) {
// Locate the index of the last '.' in the part of the `hostname` preceding
// the public suffix.
//
// examples:
// 1. not.evil.co.uk => evil.co.uk
// ^ ^
// | | start of public suffix
// | index of the last dot
//
// 2. example.co.uk => example.co.uk
// ^ ^
// | | start of public suffix
// |
// | (-1) no dot found before the public suffix
const publicSuffixIndex = hostname.length - publicSuffix.length - 2;
const lastDotBeforeSuffixIndex = hostname.lastIndexOf('.', publicSuffixIndex);
// No '.' found, then `hostname` is the general domain (no sub-domain)
if (lastDotBeforeSuffixIndex === -1) {
return hostname;
}
// Extract the part between the last '.'
return hostname.slice(lastDotBeforeSuffixIndex + 1);
}
/**
* Detects the domain based on rules and upon and a host string
*/
function getDomain$1(suffix, hostname, options) {
// Check if `hostname` ends with a member of `validHosts`.
if (options.validHosts !== null) {
const validHosts = options.validHosts;
for (const vhost of validHosts) {
if ( /*@__INLINE__*/shareSameDomainSuffix(hostname, vhost)) {
return vhost;
}
}
}
let numberOfLeadingDots = 0;
if (hostname.startsWith('.')) {
while (numberOfLeadingDots < hostname.length &&
hostname[numberOfLeadingDots] === '.') {
numberOfLeadingDots += 1;
}
}
// If `hostname` is a valid public suffix, then there is no domain to return.
// Since we already know that `getPublicSuffix` returns a suffix of `hostname`
// there is no need to perform a string comparison and we only compare the
// size.
if (suffix.length === hostname.length - numberOfLeadingDots) {
return null;
}
// To extract the general domain, we start by identifying the public suffix
// (if any), then consider the domain to be the public suffix with one added
// level of depth. (e.g.: if hostname is `not.evil.co.uk` and public suffix:
// `co.uk`, then we take one more level: `evil`, giving the final result:
// `evil.co.uk`).
return /*@__INLINE__*/ extractDomainWithSuffix(hostname, suffix);
}
/**
* Return the part of domain without suffix.
*
* Example: for domain 'foo.com', the result would be 'foo'.
*/
function getDomainWithoutSuffix$1(domain, suffix) {
// Note: here `domain` and `suffix` cannot have the same length because in
// this case we set `domain` to `null` instead. It is thus safe to assume
// that `suffix` is shorter than `domain`.
return domain.slice(0, -suffix.length - 1);
}
/**
* Matches an ASCII tab (U+0009) or newline (U+000A / U+000D). The WHATWG URL
* parser strips these before parsing; we only allocate a cleaned copy (and
* re-parse) on the rare input that actually contains one.
*/
const CONTROL_CHARS = /[\t\n\r]/g;
// Set by `extractHostname` (a module-scope flag, read synchronously by
// `parseImpl` right after the call — same pattern as the reused RESULT object).
// `true` ONLY when extraction validated the returned host inline (a confirmed-
// valid, "simple" authority) so `parseImpl` can skip the separate
// `isValidHostname` pass. `false` in every other case (validation disabled, a
// complex authority — userinfo/port/brackets/trailing-dot/control — an invalid
// host, or a non-main return path); `parseImpl` then validates as usual. The
// fast path can only ever SKIP a redundant scan for hosts already known valid,
// never accept an invalid one.
let extractedHostnameValidated = false;
/**
* True if char `code` is a valid hostname character. This is the per-char half
* of `is-valid.ts`'s `isValidAscii` (a-z, 0-9, > U+007F) PLUS three additions:
* A-Z (the host is lowercased before validation, so uppercase ≡ a valid
* lowercase letter) and '-' / '_' (valid inside a label). KEEP IN SYNC with
* `is-valid.ts`: these rules are deliberately duplicated to validate during
* extraction, so any change to the accepted character set there must be
* mirrored here (and vice-versa).
*/
function isValidHostnameChar(code) {
return ((code >= 97 && code <= 122) || // a-z
(code >= 48 && code <= 57) || // 0-9
code > 127 || // non-ASCII (accepted, not punycode-checked)
(code >= 65 && code <= 90) || // A-Z (becomes valid once lowercased)
code === 45 || // '-'
code === 95 // '_'
);
}
/**
* Classify scheme `url.slice(schemeStart, colonIndex)` as a WHATWG special
* scheme without allocating a substring (case-insensitive via `| 32`).
* Special schemes: ftp, file, http, https, ws, wss
* (https://url.spec.whatwg.org/#special-scheme).
*
* @returns 0 = not special, 1 = special, 2 = file (its host sits only between
* "//" and the next slash).
*/
function getSpecialScheme(url, schemeStart, colonIndex) {
const length = colonIndex - schemeStart;
const c0 = url.charCodeAt(schemeStart) | 32;
if (length === 2) {
return c0 === 119 && (url.charCodeAt(schemeStart + 1) | 32) === 115 ? 1 : 0; // ws
}
else if (length === 3) {
const c1 = url.charCodeAt(schemeStart + 1) | 32;
const c2 = url.charCodeAt(schemeStart + 2) | 32;
if (c0 === 119 && c1 === 115 && c2 === 115)
return 1; // wss
if (c0 === 102 && c1 === 116 && c2 === 112)
return 1; // ftp
return 0;
}
else if (length === 4) {
const c1 = url.charCodeAt(schemeStart + 1) | 32;
const c2 = url.charCodeAt(schemeStart + 2) | 32;
const c3 = url.charCodeAt(schemeStart + 3) | 32;
if (c0 === 104 && c1 === 116 && c2 === 116 && c3 === 112)
return 1; // http
if (c0 === 102 && c1 === 105 && c2 === 108 && c3 === 101)
return 2; // file
return 0;
}
else if (length === 5) {
return c0 === 104 &&
(url.charCodeAt(schemeStart + 1) | 32) === 116 &&
(url.charCodeAt(schemeStart + 2) | 32) === 116 &&
(url.charCodeAt(schemeStart + 3) | 32) === 112 &&
(url.charCodeAt(schemeStart + 4) | 32) === 115
? 1
: 0; // https
}
return 0;
}
/**
* Extract a hostname from `url`, matching a WHATWG URL parser's host-boundary
* behaviour (https://url.spec.whatwg.org/#concept-basic-url-parser) for tldts'
* scope. It deliberately does NOT normalise the host (no IDNA/punycode or IPv4
* canonicalisation; IPv6 brackets are stripped, not compressed), strips trailing
* dots, and stays lenient where a strict parser rejects (bare host:port,
* out-of-range port, user@host) — all documented deviations.
*
* @param urlIsValidHostname - when true, `url` is already a valid hostname and is
* returned by the same reference (factory.ts skips re-validation on that
* identity), keeping the common path allocation-free.
* @param validate - when true, validate the host inline during the authority
* scan and publish the verdict via `extractedHostnameValidated` so `parseImpl`
* can skip the redundant `isValidHostname` pass for simple authorities.
*/
function extractHostname(url, urlIsValidHostname, validate = false) {
let start = 0;
let end = url.length;
let hasUpper = false;
let isSpecial = false;
extractedHostnameValidated = false;
if (!urlIsValidHostname) {
// Data URLs never carry a host (and may be huge — short-circuit them).
if (url.startsWith('data:')) {
return null;
}
// WHATWG step 1: trim leading/trailing C0 control or space (<= U+0020).
// Tab/newline elsewhere are handled lazily below.
while (start < url.length && url.charCodeAt(start) <= 32) {
start += 1;
}
while (end > start + 1 && url.charCodeAt(end - 1) <= 32) {
end -= 1;
}
if (url.charCodeAt(start) === 47 /* '/' */ &&
url.charCodeAt(start + 1) === 47 /* '/' */) {
// Scheme-relative reference ("//host/path").
start += 2;
}
else {
const indexOfProtocol = url.indexOf(':/', start);
if (indexOfProtocol !== -1) {
// "scheme://…". Classify the scheme, then position `start` at the host.
const special = getSpecialScheme(url, start, indexOfProtocol);
if (special === 1) {
// Special scheme: skip the run of '/' and '\' after it
// (special-authority-(ignore-)slashes states; '\' acts as '/').
isSpecial = true;
start = indexOfProtocol + 2;
while (url.charCodeAt(start) === 47 /* '/' */ ||
url.charCodeAt(start) === 92 /* '\' */) {
start += 1;
}
}
else if (special === 2) {
// file: the host is only what sits between "//" and the next slash, so
// "file://h/x" => "h" but "file:///x" / "file:/x" => no host.
isSpecial = true;
start = indexOfProtocol + 1;
let slashes = 0;
while ((url.charCodeAt(start) === 47 || url.charCodeAt(start) === 92) &&
slashes < 2) {
start += 1;
slashes += 1;
}
if (slashes < 2) {
return null;
}
}
else {
// Unknown scheme: validate the WHATWG scheme grammar [A-Za-z0-9+.-];
// a control char means it was split by a tab/newline (strip + re-parse).
for (let i = start; i < indexOfProtocol; i += 1) {
const code = url.charCodeAt(i) | 32;
if (!(((code >= 97 && code <= 122) || // [a, z]
(code >= 48 && code <= 57) || // [0, 9]
code === 46 || // '.'
code === 45 || // '-'
code === 43) // '+'
)) {
const raw = url.charCodeAt(i);
if (raw === 9 || raw === 10 || raw === 13) {
return extractHostname(url.replace(CONTROL_CHARS, ''), urlIsValidHostname, validate);
}
return null;
}
}
// A non-special scheme has an authority only after "//" (else it is an
// opaque path with no host). `indexOf(':/')` already gave the first '/'.
if (url.charCodeAt(indexOfProtocol + 2) === 47 /* '/' */) {
start = indexOfProtocol + 3;
}
else {
return null;
}
}
}
else if (url.charCodeAt(start) !== 91 /* '[' */) {
// Cold path: no scheme "://", and not a bare IPv6 literal (whose first
// ':' would otherwise look like a scheme separator; "[…]" falls through
// to the ipv6 handling below). May be a bare host, a host:port, a
// user@host, a slash-less special scheme ("https:host"), or an opaque
// URI ("mailto:", "tel:", "urn:…").
let indexOfColon = -1;
for (let i = start; i < end; i += 1) {
const code = url.charCodeAt(i);
if (code === 9 || code === 10 || code === 13) {
return extractHostname(url.replace(CONTROL_CHARS, ''), urlIsValidHostname, validate);
}
if (code === 58 /* ':' */) {
indexOfColon = i;
break;
}
if (code === 47 || code === 92 || code === 63 || code === 35) {
break;
}
}
if (indexOfColon !== -1) {
// An '@' before the next delimiter => the ':' is userinfo, not a
// scheme ("user:pass@host", "mailto:a@b"): keep the whole authority.
let hasIdentifier = false;
for (let i = indexOfColon + 1; i < end; i += 1) {
const code = url.charCodeAt(i);
if (code === 47 || code === 92 || code === 63 || code === 35) {
break;
}
if (code === 64 /* '@' */) {
hasIdentifier = true;
break;
}
}
if (!hasIdentifier) {
// All-digits after ':' => a bare "host:port" (tldts accepts
// hostnames too); keep `start` and let the port handling trim it.
let allDigits = true;
let i = indexOfColon + 1;
for (; i < end; i += 1) {
const code = url.charCodeAt(i);
if (code === 47 || code === 92 || code === 63 || code === 35) {
break;
}
if (code < 48 /* '0' */ || code > 57 /* '9' */) {
allDigits = false;
break;
}
}
if (i === indexOfColon + 1) {
allDigits = false; // nothing after ':' => not a port
}
if (!allDigits) {
const special = getSpecialScheme(url, start, indexOfColon);
if (special === 0) {
// No "://" anywhere on the cold path and not a special scheme.
// A second ':' before the host's end marks a bare, unbracketed
// IPv6 literal ("2a01:e35::1"): fall through and let the host
// loop + isIp classify it. Without one this is an opaque path
// with no host ("mailto:x", "foo:bar").
let isBareIpv6 = false;
for (let j = indexOfColon + 1; j < end; j += 1) {
const code = url.charCodeAt(j);
if (code === 47 ||
code === 92 ||
code === 63 ||
code === 35) {
break;
}
if (code === 58 /* ':' */) {
isBareIpv6 = true;
break;
}
}
if (!isBareIpv6) {
return null;
}
}
else {
isSpecial = true;
start = indexOfColon + 1;
if (special === 2) {
// file (e.g. "file:\\host"): host only between "//" and next slash.
let slashes = 0;
while ((url.charCodeAt(start) === 47 ||
url.charCodeAt(start) === 92) &&
slashes < 2) {
start += 1;
slashes += 1;
}
if (slashes < 2) {
return null;
}
}
else {
while (url.charCodeAt(start) === 47 ||
url.charCodeAt(start) === 92) {
start += 1;
}
}
}
}
}
}
}
}
// Find the host's end: first '/', '?' or '#' (and '\' for special URLs,
// which WHATWG treats like '/'). Track the last '@', ']' and ':' for
// userinfo, ipv6 and port, plus the first ':' of the host (reset at each
// '@') to tell a bare IPv6 (>= 2 colons) from a host:port (exactly one);
// flag uppercase and a stray tab/newline. The loop is split on `code < 64`
// so common host characters take fewer comparisons.
//
// When `validate`, also accumulate `is-valid.ts`'s checks over the scanned
// run so a simple authority's host can be validated in this single pass.
// `vValid` only stays meaningful for a "simple" authority (no userinfo, port,
// brackets, control or trailing dot); those cases clear it / are rejected by
// the guard below, falling back to `isValidHostname`.
let indexOfIdentifier = -1;
let indexOfClosingBracket = -1;
let indexOfPort = -1;
let indexOfFirstColon = -1;
let hasControl = false;
let vValid = validate; // seeded true when validating; cleared on the first invalid char
let vLastDot = start - 1; // mirrors is-valid.ts `lastDotIndex = -1` at host start
let vLastCode = -1;
if (validate && start < end) {
// First-char rule: must be a valid host char, '.', or '_' (NOT '-').
const c0 = url.charCodeAt(start);
if (!(
/*@__INLINE__*/ (isValidHostnameChar(c0) ||
c0 === 46 /* '.' */ ||
c0 === 95 /* '_' */)) ||
c0 === 45 /* '-' (isValidHostnameChar allows it mid-label, not first) */) {
vValid = false;
}
}
for (let i = start; i < end; i += 1) {
const code = url.charCodeAt(i);
if (code < 64) {
if (code === 47 || code === 35 || code === 63) {
end = i;
break;
}
else if (code === 58 /* ':' */) {
if (indexOfFirstColon === -1) {
indexOfFirstColon = i;
}
indexOfPort = i;
}
else if (code === 9 || code === 10 || code === 13) {
hasControl = true;
}
else if (validate) {
if (code === 46 /* '.' */) {
if (i - vLastDot > 64 || vLastCode === 46 || vLastCode === 45) {
vValid = false;
}
vLastDot = i;
}
else if (code < 48 || code > 57) {
// < 64 and not a delimiter/dot/digit => only '-' (45) is a valid
// host char here; everything else (space, %, !, etc.) is invalid.
// A '-' must also not START a label (the byte right after a '.') —
// mirrors is-valid.ts; the first label is covered by the first-char
// rule above. (RFC 1034 §3.5 / RFC 1035 §2.3.1 LDH.)
if (code !== 45 || vLastCode === 46 /* label-leading '-' */) {
vValid = false;
}
}
}
}
else if (isSpecial && code === 92 /* '\' */) {
end = i;
break;
}
else if (code === 64 /* '@' */) {
indexOfIdentifier = i;
indexOfFirstColon = -1; // colons before '@' are userinfo, not the host
}
else if (code === 93 /* ']' */) {
indexOfClosingBracket = i;
}
else if (code >= 65 && code <= 90) {
hasUpper = true;
}
else if (validate && !( /*@__INLINE__*/isValidHostnameChar(code))) {
// >= 64, not '@'/']'/upper: valid only if a-z, '_', or non-ASCII.
vValid = false;
}
if (validate) {
vLastCode = code;
}
}
// A tab/newline inside the authority: strip everything and re-parse (rare).
if (hasControl) {
return extractHostname(url.replace(CONTROL_CHARS, ''), urlIsValidHostname, validate);
}
// Skip userinfo. '>= start' so an empty userinfo ("http://@host") works too.
if (indexOfIdentifier !== -1 &&
indexOfIdentifier >= start &&
indexOfIdentifier < end) {
start = indexOfIdentifier + 1;
}
if (url.charCodeAt(start) === 91 /* '[' */) {
// ipv6 address: return what is between the brackets, or null if unclosed.
if (indexOfClosingBracket !== -1) {
return url.slice(start + 1, indexOfClosingBracket).toLowerCase();
}
return null;
}
else if (indexOfPort !== -1 &&
indexOfPort > start &&
indexOfPort < end &&
// A host:port has exactly one ':' in the host (so its first ':' is its
// last); a bare, unbracketed IPv6 literal ("2a01:e35::1") has >= 2, so
// its first ':' precedes the last. Only the former has a ':port' to trim.
indexOfFirstColon === indexOfPort) {
end = indexOfPort; // trim ':port'
}
// Empty authority ("http://", "file:///path", "//"); only reachable here via
// extraction — a bare valid hostname never lands here.
if (start >= end) {
return null;
}
// Publish the inline-validation verdict — but only for a "simple" authority,
// where the scanned run equals the final host: no userinfo skip, no port
// trim, no brackets, no trailing dot (trimmed below), and length within RFC
// limits. Anything else leaves it `false` so `parseImpl` re-validates.
//
// Every clause below is load-bearing for CORRECTNESS, not just speed: the
// loop accumulates `vValid` over the whole scanned run (it does not stop at
// ':' or '@', so any port/userinfo bytes are included), so the verdict is
// only sound when that run equals the final host. Do not drop a clause as
// "redundant" — e.g. without `indexOfPort === -1`, `host:8080` would be
// wrongly accepted.
if (validate &&
vValid &&
indexOfIdentifier === -1 &&
indexOfPort === -1 &&
indexOfClosingBracket === -1 &&
url.charCodeAt(end - 1) !== 46 /* no trailing dot */ &&
end - start <= 255 && // total length
end - vLastDot - 1 <= 63 && // last label length
vLastCode !== 45 /* last char not '-' */) {
extractedHostnameValidated = true;
}
}
// Trim trailing dots
while (end > start + 1 && url.charCodeAt(end - 1) === 46 /* '.' */) {
end -= 1;
}
const hostname = start !== 0 || end !== url.length ? url.slice(start, end) : url;
if (hasUpper) {
return hostname.toLowerCase();
}
return hostname;
}
/**
* Check if a hostname is an IP. You should be aware that this only works
* because `hostname` is already garanteed to be a valid hostname!
*/
function isProbablyIpv4(hostname) {
// Cannot be shorted than 1.1.1.1
if (hostname.length < 7) {
return false;
}
// Cannot be longer than: 255.255.255.255
if (hostname.length > 15) {
return false;
}
let numberOfDots = 0;
for (let i = 0; i < hostname.length; i += 1) {
const code = hostname.charCodeAt(i);
if (code === 46 /* '.' */) {
numberOfDots += 1;
}
else if (code < 48 /* '0' */ || code > 57 /* '9' */) {
return false;
}
}
return (numberOfDots === 3 &&
hostname.charCodeAt(0) !== 46 /* '.' */ &&
hostname.charCodeAt(hostname.length - 1) !== 46 /* '.' */);
}
/**
* Similar to isProbablyIpv4.
*/
function isProbablyIpv6(hostname) {
if (hostname.length < 3) {
return false;
}
let start = hostname.startsWith('[') ? 1 : 0;
let end = hostname.length;
if (hostname[end - 1] === ']') {
end -= 1;
}
// We only consider the maximum size of a normal IPV6. Note that this will
// fail on so-called "IPv4 mapped IPv6 addresses" but this is a corner-case
// and a proper validation library should be used for these.
if (end - start > 39) {
return false;
}
let hasColon = false;
for (; start < end; start += 1) {
const code = hostname.charCodeAt(start);
if (code === 58 /* ':' */) {
hasColon = true;
}
else if (!(((code >= 48 && code <= 57) || // 0-9
(code >= 97 && code <= 102) || // a-f
(code >= 65 && code <= 70)) // A-F (RFC 4291 §2.2: an IPv6 hextet is hex digits only)
)) {
return false;
}
}
return hasColon;
}
/**
* Check if `hostname` is *probably* a valid ip addr (either ipv6 or ipv4).
* This *will not* work on any string. We need `hostname` to be a valid
* hostname.
*/
function isIp(hostname) {
return isProbablyIpv6(hostname) || isProbablyIpv4(hostname);
}
/**
* Special-use domain names from the IANA "Special-Use Domain Names" registry:
* the authoritative list, created by RFC 6761 and maintained as new RFCs add to
* it: https://www.iana.org/assignments/special-use-domain-names/
* Snapshot: 2026-05-24. (RFC 6761 is not obsoleted; draft-hoffman-rfc6761bis
* proposes to retire its prose but keep this registry, so the registry is the
* source of truth; re-sync this list against it.)
*
* These names never correspond to a public registration, yet neither
* `isIcann` nor `isPrivate` marks one as special-use: most are absent from the
* Public Suffix List (so `a.test` looks like a registrable domain), and the
* few that are listed (`onion`, `home.arpa`) appear there as ordinary ICANN
* suffixes. `isSpecialUse` is the single signal that covers them all.
*
* Per the registry and RFC 6761 ("and any names falling within these domains"),
* the designation covers each listed name AND all of its sub-domains. DNS labels
* are case-insensitive (RFC 4343); `hostname` is expected to be already
* lower-cased and trailing-dot-stripped, as produced by `extractHostname`, the
* same normalization the Public-Suffix-List lookup relies on.
*
* Two groups of registry entries are intentionally excluded: the numeric
* reverse-DNS delegation zones (`10.in-addr.arpa`, the `*.ip6.arpa` ranges, …),
* which are reverse-DNS PTR zones rather than hostnames and whose parents
* (`in-addr.arpa`/`ip6.arpa`) are already in the Public Suffix List; and the
* deprecated `eap-noob.arpa` entry.
*/
const SPECIAL_USE_DOMAINS = [
'test', // RFC 6761
'localhost', // RFC 6761
'invalid', // RFC 6761
'example', // RFC 6761
'example.com', // RFC 6761
'example.net', // RFC 6761
'example.org', // RFC 6761
'local', // RFC 6762 (mDNS)
'onion', // RFC 7686 (Tor)
'alt', // RFC 9476
'home.arpa', // RFC 8375
'ipv4only.arpa', // RFC 8880
'resolver.arpa', // RFC 9462
'service.arpa', // RFC 9665
'6tisch.arpa', // RFC 9031
'eap.arpa', // RFC 9965
];
/**
* Return `true` if `hostname` is, or is a sub-domain of, a special-use domain
* (see the registry note above). Expects an already-normalized `hostname`.
*/
function isSpecialUse(hostname) {
for (const name of SPECIAL_USE_DOMAINS) {
// Match on a label boundary: `hostname` is either exactly `name` or ends
// with `.name` (so `latest` is not matched by `test`, nor `myexample.com`
// by `example.com`).
if (hostname.endsWith(name) &&
(hostname.length === name.length ||
hostname.charCodeAt(hostname.length - name.length - 1) === 46) /* '.' */) {
return true;
}
}
return false;
}
/**
* Implements fast shallow verification of hostnames. This does not perform a
* struct check on the content of labels (classes of Unicode characters, etc.)
* but instead check that the structure is valid (number of labels, length of
* labels, etc.).
*
* If you need stricter validation, consider using an external library.
*/
// KEEP IN SYNC with `extract-hostname.ts` `isValidHostnameChar` + its inline
// scan/verdict, which duplicate these structural rules to validate during
// extraction (a perf fusion). That copy additionally accepts A-Z (the host is
// not yet lowercased there) and folds in '-' / '_'. Any change to the accepted
// character set or the label/length rules here must be mirrored there.
function isValidAscii(code) {
return ((code >= 97 && code <= 122) || (code >= 48 && code <= 57) || code > 127);
}
/**
* Check if a hostname string is valid. It's usually a preliminary check before
* trying to use getDomain or anything else.
*
* Beware: it does not check if the TLD exists.
*/
function isValidHostname (hostname) {
if (hostname.length > 255) {
return false;
}
if (hostname.length === 0) {
return false;
}
if (
/*@__INLINE__*/ !isValidAscii(hostname.charCodeAt(0)) &&
hostname.charCodeAt(0) !== 46 && // '.' (dot)
hostname.charCodeAt(0) !== 95 // '_' (underscore)
) {
return false;
}
// Validate hostname according to RFC
let lastDotIndex = -1;
let lastCharCode = -1;
const len = hostname.length;
for (let i = 0; i < len; i += 1) {
const code = hostname.charCodeAt(i);
if (code === 46 /* '.' */) {
if (
// Check that previous label is < 63 bytes long (64 = 63 + '.')
i - lastDotIndex > 64 ||
// Check that previous character was not already a '.'
lastCharCode === 46 ||
// Check that the previous label does not end with '-' (RFC 1035 §2.3.1 LDH).
// '_' is intentionally NOT restricted: DNS allows any octet (RFC 2181 §11) and
// WHATWG URL does not treat '_' as a forbidden host code point.
lastCharCode === 45) {
return false;
}
lastDotIndex = i;
}
else if (
// A forbidden character in the label...
!( /*@__INLINE__*/(isValidAscii(code) || code === 45 || code === 95)) ||
// ...or a '-' starting a label (the byte right after a '.'). A label must
// not begin with a hyphen (RFC 1034 §3.5 / RFC 1035 §2.3.1 LDH, as amended
// by RFC 1123 §2.1; cf. UTS #46 CheckHyphens). The first label is covered by
// the leading-character guard above; mirrors the trailing-'-' rule below.
(code === 45 && lastCharCode === 46)) {
return false;
}
lastCharCode = code;
}
return (
// Check that last label is shorter than 63 chars
len - lastDotIndex - 1 <= 63 &&
// Check that the last character is an allowed trailing label character.
// Since we already checked that the char is a valid hostname character,
// we only need to check that it's different from '-'.
lastCharCode !== 45);
}
function setDefaultsImpl({ allowIcannDomains = true, allowPrivateDomains = false, detectIp = true, detectSpecialUse = false, extractHostname = true, mixedInputs = true, validHosts = null, validateHostname = true, }) {
return {
allowIcannDomains,
allowPrivateDomains,
detectIp,
detectSpecialUse,
extractHostname,
mixedInputs,
validHosts,
validateHostname,
};
}
const DEFAULT_OPTIONS = /*@__INLINE__*/ setDefaultsImpl({});
function setDefaults(options) {
if (options === undefined) {
return DEFAULT_OPTIONS;
}
return /*@__INLINE__*/ setDefaultsImpl(options);
}
/**
* Returns the subdomain of a hostname string
*/
function getSubdomain$1(hostname, domain) {
// If `hostname` and `domain` are the same, then there is no sub-domain
if (domain.length === hostname.length) {
return '';
}
return hostname.slice(0, -domain.length - 1);
}
/**
* Implement a factory allowing to plug different implementations of suffix
* lookup (e.g.: using a trie or the packed hashes datastructures). This is used
* and exposed in `tldts.ts` and `tldts-experimental.ts` bundle entrypoints.
*/
function getEmptyResult() {
return {
domain: null,
domainWithoutSuffix: null,
hostname: null,
isIcann: null,
isIp: null,
isPrivate: null,
isSpecialUse: null,
publicSuffix: null,
subdomain: null,
};
}
function resetResult(result) {
result.domain = null;
result.domainWithoutSuffix = null;
result.hostname = null;
result.isIcann = null;
result.isIp = null;
result.isPrivate = null;
result.isSpecialUse = null;
result.publicSuffix = null;
result.subdomain = null;
}
function parseImpl(url, step, suffixLookup, partialOptions, result) {
const options = /*@__INLINE__*/ setDefaults(partialOptions);
// Very fast approximate check to make sure `url` is a string. This is needed
// because the library will not necessarily be used in a typed setup and
// values of arbitrary types might be given as argument.
if (typeof url !== 'string') {
return result;
}
// Extract hostname from `url` only if needed. This can be made optional
// using `options.extractHostname`. This option will typically be used
// whenever we are sure the inputs to `parse` are already hostnames and not
// arbitrary URLs.
//
// `mixedInput` allows to specify if we expect a mix of URLs and hostnames
// as input. If only hostnames are expected then `extractHostname` can be
// set to `false` to speed-up parsing. If only URLs are expected then
// `mixedInputs` can be set to `false`. The `mixedInputs` is only a hint
// and will not change the behavior of the library.
// Whether `url` itself was already a valid hostname (only computed on the
// mixedInputs path). Lets us skip the post-extraction validation below when
// extractHostname returned `url` unchanged (same reference).
let urlIsValid = false;
if (!options.extractHostname) {
result.hostname = url;
}
else if (options.mixedInputs) {
urlIsValid = isValidHostname(url);
result.hostname = extractHostname(url, urlIsValid, options.validateHostname);
}
else {
result.hostname = extractHostname(url, false, options.validateHostname);
}
// Check if `hostname` is a valid ip address
if (options.detectIp && result.hostname !== null) {
result.isIp = isIp(result.hostname);
if (result.isIp) {
return result;
}
}
// Perform hostname validation if enabled. If hostname is not valid, no need to
// go further as there will be no valid domain or sub-domain. This validation
// is applied before any early returns to ensure consistent behavior across
// all API methods including getHostname().
if (options.validateHostname &&
options.extractHostname &&
result.hostname !== null &&
// Skip the re-scan when `url` was already validated and extractHostname
// returned it unchanged (same reference => identical string, still valid).
!(urlIsValid && result.hostname === url) &&
// Skip the re-scan when extractHostname already validated the host inline
// (a confirmed-valid simple authority — see extract-hostname.ts).
!extractedHostnameValidated &&
!isValidHostname(result.hostname)) {
result.hostname = null;
return result;
}
if (step === 0 /* FLAG.HOSTNAME */ || result.hostname === null) {
return result;
}
// Flag special-use domains, only when opted in (`detectSpecialUse`) and only
// for the full `parse()` result (FLAG.ALL). Computed here, before the
// public-suffix/domain early-returns below, so single-label names like
// `localhost` (which have no registrable domain) are still flagged.
if (step === 5 /* FLAG.ALL */ && options.detectSpecialUse) {
result.isSpecialUse = isSpecialUse(result.hostname);
}
// Extract public suffix
suffixLookup(result.hostname, options, result);
if (step === 2 /* FLAG.PUBLIC_SUFFIX */ || result.publicSuffix === null) {
return result;
}
// Extract domain
result.domain = getDomain$1(result.publicSuffix, result.hostname, options);
if (step === 3 /* FLAG.DOMAIN */ || result.domain === null) {
return result;
}
// Extract subdomain
result.subdomain = getSubdomain$1(result.hostname, result.domain);
if (step === 4 /* FLAG.SUB_DOMAIN */) {
return result;
}
// Extract domain without suffix
result.domainWithoutSuffix = getDomainWithoutSuffix$1(result.domain, result.publicSuffix);
return result;
}
function fastPathLookup (hostname, options, out) {
// Fast path for very popular suffixes; this allows to by-pass lookup
// completely as well as any extra allocation or string manipulation.
if (!options.allowPrivateDomains && hostname.length > 3) {
const last = hostname.length - 1;
const c3 = hostname.charCodeAt(last);
const c2 = hostname.charCodeAt(last - 1);
const c1 = hostname.charCodeAt(last - 2);
const c0 = hostname.charCodeAt(last - 3);
if (c3 === 109 /* 'm' */ &&
c2 === 111 /* 'o' */ &&
c1 === 99 /* 'c' */ &&
c0 === 46 /* '.' */) {
out.isIcann = true;
out.isPrivate = false;
out.publicSuffix = 'com';
return true;
}
else if (c3 === 103 /* 'g' */ &&
c2 === 114 /* 'r' */ &&
c1 === 111 /* 'o' */ &&
c0 === 46 /* '.' */) {
out.isIcann = true;
out.isPrivate = false;
out.publicSuffix = 'org';
return true;
}
else if (c3 === 117 /* 'u' */ &&
c2 === 100 /* 'd' */ &&
c1 === 101 /* 'e' */ &&
c0 === 46 /* '.' */) {
out.isIcann = true;
out.isPrivate = false;
out.publicSuffix = 'edu';
return true;
}
else if (c3 === 118 /* 'v' */ &&
c2 === 111 /* 'o' */ &&
c1 === 103 /* 'g' */ &&
c0 === 46 /* '.' */) {
out.isIcann = true;
out.isPrivate = false;
out.publicSuffix = 'gov';
return true;
}
else if (c3 === 116 /* 't' */ &&
c2 === 101 /* 'e' */ &&
c1 === 110 /* 'n' */ &&
c0 === 46 /* '.' */) {
out.isIcann = true;
out.isPrivate = false;
out.publicSuffix = 'net';
return true;
}
else if (c3 === 101 /* 'e' */ &&
c2 === 100 /* 'd' */ &&
c1 === 46 /* '.' */) {
out.isIcann = true;
out.isPrivate = false;
out.publicSuffix = 'de';
return true;
}
}
return false;
}
// Auto-generated flat public-suffix trie. Do not edit.
const nodeFlags = /*#__PURE__*/ new Uint8Array([1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 2, 2, 2, 0, 2, 2, 0, 2, 0, 0, 1, 0, 0, 2, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 0, 1, 1, 1, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 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, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 0, 2, 0, 2, 2, 2, 2, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 2, 2, 0, 0, 0, 2, 2, 1, 1, 1, 1, 2, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 2, 2, 0, 0, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 2, 2, 1, 2, 1, 1, 1, 2, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0]);
const edgeStart = /*#__PURE__*/ new Uint16Array([0, 0, 0, 10, 11, 18, 106, 111, 117, 124, 130, 136, 145, 146, 147, 148, 149, 150, 151, 153, 154, 155, 157, 159, 226, 239, 241, 242, 243, 258, 265, 266, 269, 270, 271, 274, 276, 295, 296, 298, 307, 312, 313, 331, 332, 335, 337, 338, 340, 374, 375, 377, 380, 381, 385, 387, 391, 394, 426, 429, 442, 443, 451, 452, 454, 464, 478, 479, 480, 489, 490, 527, 532, 548, 568, 574, 615, 616, 643, 670, 671, 819, 825, 828, 829, 830, 835, 840, 849, 871, 872, 873, 874, 875, 876, 877, 895, 897, 898, 901, 903, 904, 906, 908, 923, 938, 943, 944, 946, 947, 948, 949, 950, 952, 955, 960, 961, 962, 964, 965, 968, 971, 972, 973, 986, 988, 1000, 1011, 1019, 1021, 1061, 1064, 1068, 1069, 1071, 1074, 1085, 1087, 1097, 1099, 1105, 1107, 1108, 1110, 1113, 1114, 1115, 1167, 1169, 1171, 1191, 1192, 1193, 1194, 1196, 1207, 1238, 1249, 1261, 1270, 1277, 1282, 1295, 1306, 1319, 1320, 1331, 1365, 1366, 1367, 1382, 1397, 1469, 1470, 1472, 1473, 1507, 1508, 1509, 1512, 1516, 1518, 1547, 1548, 1556, 1557, 1558, 1560, 1562, 1563, 1565, 1566, 1567, 1578, 1579, 1580, 1581, 1582, 1583, 1584, 1585, 1586, 1587, 1588, 1589, 1590, 1591, 1592, 1594, 1595, 1596, 1598, 2053, 2056, 2057, 2059, 2066, 2073, 2083, 2087, 2098, 2099, 2100, 2112, 2113, 2115, 2117, 2118, 2125, 2126, 2128, 2129, 2131, 2132, 2133, 2134, 2135, 2209, 2211, 2232, 2233, 2234, 2236, 2262, 2263, 2314, 2315, 2317, 2324, 2330, 2340, 2350, 2403, 2404, 2405, 2415, 2429, 2430, 2433, 2440, 2441, 2449, 2450, 2451, 2452, 2453, 2464, 2465, 2466, 2468, 2469, 2470, 2471, 2473, 2483, 2495, 2501, 2533, 2537, 2539, 2540, 2542, 2543, 2554, 2555, 2557, 2565, 2572, 2578, 2583, 2584, 2590, 2593, 2599, 2606, 2607, 2614, 2622, 2623, 2624, 2662, 2668, 2683, 2684, 2689, 2707, 2738, 2756, 2758, 2761, 2769, 2771, 2778, 2830, 2854, 2855, 2856, 2857, 2858, 2859, 2860, 2861, 2868, 2869, 2870, 2871, 2872, 2873, 2875, 2876, 2880, 2964, 2977, 2978, 3414, 3418, 3432, 3484, 3512, 3534, 3592, 3614, 3629, 3692, 3743, 3781, 3817, 3842, 3984, 4030, 4081, 4100, 4134, 4149, 4169, 4199, 4230, 4253, 4284, 4314, 4346, 4373, 4448, 4470, 4508, 4518, 4552, 4571, 4597, 4639, 4689, 4715, 4784, 4785, 4787, 4810, 4833, 4869, 4900, 4917, 4974, 4987, 5011, 5040, 5042, 5076, 5092, 5120, 5425, 5434, 5443, 5450, 5467, 5471, 5477, 5516, 5518, 5525, 5532, 5541, 5548, 5549, 5560, 5563, 5578, 5579, 5588, 5589, 5598, 5607, 5613, 5615, 5616, 5617, 5653, 5654, 5656, 5664, 5671, 5684, 5688, 5690, 5691, 5697, 5704, 5718, 5728, 5733, 5741, 5749, 5755, 5756, 5760, 5762, 5763, 5775, 5776, 5777, 5778, 5780, 5781, 5784, 5786, 5789, 5793, 5794, 5800, 5801, 5802, 5804, 5806, 5808, 5809, 5810, 5813, 5815, 5818, 5819, 5821, 6019, 6026, 6027, 6037, 6042, 6059, 6073, 6082, 6083, 6084, 6088, 6089, 6091, 6093, 6099, 6100, 6103, 6104, 6106, 6107, 6108, 7008, 7009, 7013, 7031, 7040, 7043, 7050, 7051, 7053, 7054, 7055, 7057, 7109, 7110, 7113, 7114, 7231, 7232, 7243, 7254, 7261, 7264, 7273, 7274, 7275, 7290, 7345, 7536, 7538, 7539, 7541, 7546, 7559, 7574, 7581, 7590, 7593, 7596, 7603, 7611, 7615, 7616, 7617, 7631, 7635, 7644, 7645, 7646, 7650, 7685, 7686, 7703, 7710, 7718, 7719, 7724, 7732, 7776, 7777, 7783, 7786, 7797, 7802, 7803, 7806, 7839, 7840, 7846, 7853, 7854, 7863, 7872, 7887, 7891, 7943, 7944, 7949, 7951, 7954, 7956, 7957, 7958, 7967, 7981, 7989, 8003, 8015, 8016, 8018, 8020, 8042, 8053, 8059, 8060, 8072, 8084, 8171, 8183, 8185, 8194, 8197, 8203, 8228, 8231, 8232, 8233, 8235, 8238, 8241, 8252, 8254, 8256, 8284, 8358, 8365, 8369, 8370, 8379, 8401, 8402, 8407, 8408, 8487, 8489, 8491, 8500, 8504, 8510, 8516, 8522, 8532, 8537, 8538, 8556, 8567, 8572, 8577, 8587, 8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