更新
This commit is contained in:
+955
@@ -0,0 +1,955 @@
|
||||
'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(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(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(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(result.publicSuffix, result.hostname, options);
|
||||
if (step === 3 /* FLAG.DOMAIN */ || result.domain === null) {
|
||||
return result;
|
||||
}
|
||||
// Extract subdomain
|
||||
result.subdomain = getSubdomain(result.hostname, result.domain);
|
||||
if (step === 4 /* FLAG.SUB_DOMAIN */) {
|
||||
return result;
|
||||
}
|
||||
// Extract domain without suffix
|
||||
result.domainWithoutSuffix = getDomainWithoutSuffix(result.domain, result.publicSuffix);
|
||||
return result;
|
||||
}
|
||||
|
||||
function fastPath (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;
|
||||
}
|
||||
|
||||
exports.fastPathLookup = fastPath;
|
||||
exports.getEmptyResult = getEmptyResult;
|
||||
exports.parseImpl = parseImpl;
|
||||
exports.resetResult = resetResult;
|
||||
exports.setDefaults = setDefaults;
|
||||
//# sourceMappingURL=index.js.map
|
||||
+1
File diff suppressed because one or more lines are too long
+15
@@ -0,0 +1,15 @@
|
||||
"use strict";
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.default = getDomainWithoutSuffix;
|
||||
/**
|
||||
* Return the part of domain without suffix.
|
||||
*
|
||||
* Example: for domain 'foo.com', the result would be 'foo'.
|
||||
*/
|
||||
function getDomainWithoutSuffix(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);
|
||||
}
|
||||
//# sourceMappingURL=domain-without-suffix.js.map
|
||||
+1
@@ -0,0 +1 @@
|
||||
{"version":3,"file":"domain-without-suffix.js","sourceRoot":"","sources":["../../../src/domain-without-suffix.ts"],"names":[],"mappings":";;AAKA,yCAQC;AAbD;;;;GAIG;AACH,SAAwB,sBAAsB,CAC5C,MAAc,EACd,MAAc;IAEd,0EAA0E;IAC1E,yEAAyE;IACzE,0CAA0C;IAC1C,OAAO,MAAM,CAAC,KAAK,CAAC,CAAC,EAAE,CAAC,MAAM,CAAC,MAAM,GAAG,CAAC,CAAC,CAAC;AAC7C,CAAC"}
|
||||
+83
@@ -0,0 +1,83 @@
|
||||
"use strict";
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.default = getDomain;
|
||||
/**
|
||||
* 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(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);
|
||||
}
|
||||
//# sourceMappingURL=domain.js.map
|
||||
+1
@@ -0,0 +1 @@
|
||||
{"version":3,"file":"domain.js","sourceRoot":"","sources":["../../../src/domain.ts"],"names":[],"mappings":";;AA4DA,4BAuCC;AAjGD;;;;;;;;;;GAUG;AACH,SAAS,qBAAqB,CAAC,QAAgB,EAAE,KAAa;IAC5D,IAAI,QAAQ,CAAC,QAAQ,CAAC,KAAK,CAAC,EAAE,CAAC;QAC7B,OAAO,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IAAI,QAAQ,CAAC,UAAU,CAAC,GAAG,CAAC,EAAE,CAAC;QAC7B,OACE,mBAAmB,GAAG,QAAQ,CAAC,MAAM;YACrC,QAAQ,CAAC,mBAAmB,CAAC,KAAK,GAAG,EACrC,CAAC;YACD,mBAAmB,IAAI,CAAC,CAAC;QAC3B,CAAC;IACH,CAAC;IAED,6EAA6E;IAC7E,8EAA8E;IAC9E,0EAA0E;IAC1E,QAAQ;IACR,IAAI,MAAM,CAAC,MAAM,KAAK,QAAQ,CAAC,MAAM,GAAG,mBAAmB,EAAE,CAAC;QAC5D,OAAO,IAAI,CAAC;IACd,CAAC;IAED,2EAA2E;IAC3E,4EAA4E;IAC5E,4EAA4E;IAC5E,yEAAyE;IACzE,iBAAiB;IACjB,OAAO,eAAe,CAAC,uBAAuB,CAAC,QAAQ,EAAE,MAAM,CAAC,CAAC;AACnE,CAAC"}
|
||||
+439
@@ -0,0 +1,439 @@
|
||||
"use strict";
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.extractedHostnameValidated = void 0;
|
||||
exports.default = extractHostname;
|
||||
/**
|
||||
* 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.
|
||||
exports.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;
|
||||
exports.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 '-' */) {
|
||||
exports.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;
|
||||
}
|
||||
//# sourceMappingURL=extract-hostname.js.map
|
||||
+1
File diff suppressed because one or more lines are too long
+128
@@ -0,0 +1,128 @@
|
||||
"use strict";
|
||||
/**
|
||||
* 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.
|
||||
*/
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.getEmptyResult = getEmptyResult;
|
||||
exports.resetResult = resetResult;
|
||||
exports.parseImpl = parseImpl;
|
||||
const domain_1 = require("./domain");
|
||||
const domain_without_suffix_1 = require("./domain-without-suffix");
|
||||
const extract_hostname_1 = require("./extract-hostname");
|
||||
const is_ip_1 = require("./is-ip");
|
||||
const is_special_use_1 = require("./is-special-use");
|
||||
const is_valid_1 = require("./is-valid");
|
||||
const options_1 = require("./options");
|
||||
const subdomain_1 = require("./subdomain");
|
||||
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__*/ (0, options_1.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 = (0, is_valid_1.default)(url);
|
||||
result.hostname = (0, extract_hostname_1.default)(url, urlIsValid, options.validateHostname);
|
||||
}
|
||||
else {
|
||||
result.hostname = (0, extract_hostname_1.default)(url, false, options.validateHostname);
|
||||
}
|
||||
// Check if `hostname` is a valid ip address
|
||||
if (options.detectIp && result.hostname !== null) {
|
||||
result.isIp = (0, is_ip_1.default)(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).
|
||||
!extract_hostname_1.extractedHostnameValidated &&
|
||||
!(0, is_valid_1.default)(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 = (0, is_special_use_1.default)(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 = (0, domain_1.default)(result.publicSuffix, result.hostname, options);
|
||||
if (step === 3 /* FLAG.DOMAIN */ || result.domain === null) {
|
||||
return result;
|
||||
}
|
||||
// Extract subdomain
|
||||
result.subdomain = (0, subdomain_1.default)(result.hostname, result.domain);
|
||||
if (step === 4 /* FLAG.SUB_DOMAIN */) {
|
||||
return result;
|
||||
}
|
||||
// Extract domain without suffix
|
||||
result.domainWithoutSuffix = (0, domain_without_suffix_1.default)(result.domain, result.publicSuffix);
|
||||
return result;
|
||||
}
|
||||
//# sourceMappingURL=factory.js.map
|
||||
+1
@@ -0,0 +1 @@
|
||||
{"version":3,"file":"factory.js","sourceRoot":"","sources":["../../../src/factory.ts"],"names":[],"mappings":";AAAA;;;;GAIG;;AA2CH,wCAYC;AAED,kCAUC;AAeD,8BAgHC;AAhMD,qCAAiC;AACjC,mEAA6D;AAC7D,yDAE4B;AAC5B,mCAA2B;AAC3B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AAM,CAAC,QAAQ,KAAK,IAAI,EAAE,CAAC;QACvD,OAAO,MAAM,CAAC;IAChB,CAAC;IAED,6EAA6E;IAC7E,sEAAsE;IACtE,uEAAuE;IACvE,oEAAoE;IACpE,IAAI,IAAI,qBAAa,IAAI,OAAO,CAAC,gBAAgB,EAAE,CAAC;QAClD,MAAM,CAAC,YAAY,GAAG,IAAA,wBAAY,EAAC,MAAM,CAAC,QAAQ,CAAC,CAAC;IACtD,CAAC;IAED,wBAAwB;IACxB,YAAY,CAAC,MAAM,CAAC,QAAQ,EAAE,OAAO,EAAE,MAAM,CAAC,CAAC;IAC/C,IAAI,IAAI,+BAAuB,IAAI,MAAM,CAAC,YAAY,KAAK,IAAI,EAAE,CAAC;QAChE,OAAO,MAAM,CAAC;IAChB,CAAC;IAED,iBAAiB;IACjB,MAAM,CAAC,MAAM,GAAG,IAAA,gBAAS,EAAC,MAAM,CAAC,YAAY,EAAE,MAAM,CAAC,QAAQ,EAAE,OAAO,CAAC,CAAC;IACzE,IAAI,IAAI,wBAAgB,IAAI,MAAM,CAAC,MAAM,KAAK,IAAI,EAAE,CAAC;QACnD,OAAO,MAAM,CAAC;IAChB,CAAC;IAED,oBAAoB;IACpB,MAAM,CAAC,SAAS,GAAG,IAAA,mBAAY,EAAC,MAAM,CAAC,QAAQ,EAAE,MAAM,CAAC,MAAM,CAAC,CAAC;IAChE,IAAI,IAAI,4BAAoB,EAAE,CAAC;QAC7B,OAAO,MAAM,CAAC;IAChB,CAAC;IAED,gCAAgC;IAChC,MAAM,CAAC,mBAAmB,GAAG,IAAA,+BAAsB,EACjD,MAAM,CAAC,MAAM,EACb,MAAM,CAAC,YAAY,CACpB,CAAC;IAEF,OAAO,MAAM,CAAC;AAChB,CAAC"}
|
||||
+72
@@ -0,0 +1,72 @@
|
||||
"use strict";
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.default = isIp;
|
||||
/**
|
||||
* 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);
|
||||
}
|
||||
//# sourceMappingURL=is-ip.js.map
|
||||
+1
@@ -0,0 +1 @@
|
||||
{"version":3,"file":"is-ip.js","sourceRoot":"","sources":["../../../src/is-ip.ts"],"names":[],"mappings":";;AAoFA,uBAEC;AAtFD;;;GAGG;AACH,SAAS,cAAc,CAAC,QAAgB;IACtC,iCAAiC;IACjC,IAAI,QAAQ,CAAC,MAAM,GAAG,CAAC,EAAE,CAAC;QACxB,OAAO,KAAK,CAAC;IACf,CAAC;IAED,yCAAyC;IACzC,IAAI,QAAQ,CAAC,MAAM,GAAG,EAAE,EAAE,CAAC;QACzB,OAAO,KAAK,CAAC;IACf,CAAC;IAED,IAAI,YAAY,GAAG,CAAC,CAAC;IAErB,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,QAAQ,CAAC,MAAM,EAAE,CAAC,IAAI,CAAC,EAAE,CAAC;QAC5C,MAAM,IAAI,GAAG,QAAQ,CAAC,UAAU,CAAC,CAAC,CAAC,CAAC;QAEpC,IAAI,IAAI,KAAK,EAAE,CAAC,SAAS,EAAE,CAAC;YAC1B,YAAY,IAAI,CAAC,CAAC;QACpB,CAAC;aAAM,IAAI,IAAI,GAAG,EAAE,CAAC,SAAS,IAAI,IAAI,GAAG,EAAE,CAAC,SAAS,EAAE,CAAC;YACtD,OAAO,KAAK,CAAC;QACf,CAAC;IACH,CAAC;IAED,OAAO,CACL,YAAY,KAAK,CAAC;QAClB,QAAQ,CAAC,UAAU,CAAC,CAAC,CAAC,KAAK,EAAE,CAAC,SAAS;QACvC,QAAQ,CAAC,UAAU,CAAC,QAAQ,CAAC,MAAM,GAAG,CAAC,CAAC,KAAK,EAAE,CAAC,SAAS,CAC1D,CAAC;AACJ,CAAC;AAED;;GAEG;AACH,SAAS,c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Ac,CAAC,QAAQ,CAAC,CAAC;AAC9D,CAAC"}
|
||||
+65
@@ -0,0 +1,65 @@
|
||||
"use strict";
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.default = isSpecialUse;
|
||||
/**
|
||||
* 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;
|
||||
}
|
||||
//# sourceMappingURL=is-special-use.js.map
|
||||
+1
@@ -0,0 +1 @@
|
||||
{"version":3,"file":"is-special-use.js","sourceRoot":"","sources":["../../../src/is-special-use.ts"],"names":[],"mappings":";;AAiDA,+BAeC;AAhED;;;;;;;;;;;;;;;;;;;;;;;;;GAyBG;AACH,MAAM,mBAAmB,GAAsB;IAC7C,MAAM,EAAE,WAAW;IACnB,WAAW,EAAE,WAAW;IACxB,SAAS,EAAE,WAAW;IACtB,SAAS,EAAE,WAAW;IACtB,aAAa,EAAE,WAAW;IAC1B,aAAa,EAAE,WAAW;IAC1B,aAAa,EAAE,WAAW;IAC1B,OAAO,EAAE,kBAAkB;IAC3B,OAAO,EAAE,iBAAiB;IAC1B,KAAK,EAAE,WAAW;IAClB,WAAW,EAAE,WAAW;IACxB,eAAe,EAAE,WAAW;IAC5B,eAAe,EAAE,WAAW;IAC5B,cAAc,EAAE,WAAW;IAC3B,aAAa,EAAE,WAAW;IAC1B,UAAU,EAAE,WAAW;CACxB,CAAC;AAEF;;;GAGG;AACH,SAAwB,YAAY,CAAC,QAAgB;IACnD,KAAK,MAAM,IAAI,IAAI,mBAAmB,EAAE,CAAC;QACvC,yEAAyE;QACzE,0EAA0E;QAC1E,qBAAqB;QACrB,IACE,QAAQ,CAAC,QAAQ,CAAC,IAAI,CAAC;YACvB,CAAC,QAAQ,CAAC,MAAM,KAAK,IAAI,CAAC,MAAM;gBAC9B,QAAQ,CAAC,UAAU,CAAC,QAAQ,CAAC,MAAM,GAAG,IAAI,CAAC,MAAM,GAAG,CAAC,CAAC,KAAK,EAAE,CAAC,CAAC,SAAS,EAC1E,CAAC;YACD,OAAO,IAAI,CAAC;QACd,CAAC;IACH,CAAC;IAED,OAAO,KAAK,CAAC;AACf,CAAC"}
|
||||
+80
@@ -0,0 +1,80 @@
|
||||
"use strict";
|
||||
/**
|
||||
* 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.
|
||||
*/
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.default = default_1;
|
||||
// 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 default_1(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);
|
||||
}
|
||||
//# sourceMappingUR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QACpC,IAAI,IAAI,KAAK,EAAE,CAAC,SAAS,EAAE,CAAC;YAC1B;YACE,+DAA+D;YAC/D,CAAC,GAAG,YAAY,GAAG,EAAE;gBACrB,sDAAsD;gBACtD,YAAY,KAAK,EAAE;gBACnB,6EAA6E;gBAC7E,+EAA+E;gBAC/E,gEAAgE;gBAChE,YAAY,KAAK,EAAE,EACnB,CAAC;gBACD,OAAO,KAAK,CAAC;YACf,CAAC;YAED,YAAY,GAAG,CAAC,CAAC;QACnB,CAAC;aAAM;QACL,wCAAwC;QACxC,CAAC,EAAC,eAAgB,CAAC,YAAY,CAAC,IAAI,CAAC,IAAI,IAAI,KAAK,EAAE,IAAI,IAAI,KAAK,EAAE,CAAC,CAAC;YACrE,0EAA0E;YAC1E,2EAA2E;YAC3E,6EAA6E;YAC7E,0EAA0E;YAC1E,CAAC,IAAI,KAAK,EAAE,IAAI,YAAY,KAAK,EAAE,CAAC,EACpC,CAAC;YACD,OAAO,KAAK,CAAC;QACf,CAAC;QAED,YAAY,GAAG,IAAI,CAAC;IACtB,CAAC;IAED,OAAO;IACL,iDAAiD;IACjD,GAAG,GAAG,YAAY,GAAG,CAAC,IAAI,EAAE;QAC5B,wEAAwE;QACxE,wEAAwE;QACxE,sDAAsD;QACtD,YAAY,KAAK,EAAE,CACpB,CAAC;AACJ,CAAC"}
|
||||
+69
@@ -0,0 +1,69 @@
|
||||
"use strict";
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.default = default_1;
|
||||
function default_1(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;
|
||||
}
|
||||
//# sourceMappingURL=fast-path.js.map
|
||||
+1
@@ -0,0 +1 @@
|
||||
{"version":3,"file":"fast-path.js","sourceRoot":"","sources":["../../../../src/lookup/fast-path.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AAC;IAED,OAAO,KAAK,CAAC;AACf,CAAC"}
|
||||
+3
@@ -0,0 +1,3 @@
|
||||
"use strict";
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
//# sourceMappingURL=interface.js.map
|
||||
+1
@@ -0,0 +1 @@
|
||||
{"version":3,"file":"interface.js","sourceRoot":"","sources":["../../../../src/lookup/interface.ts"],"names":[],"mappings":""}
|
||||
+23
@@ -0,0 +1,23 @@
|
||||
"use strict";
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.setDefaults = setDefaults;
|
||||
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);
|
||||
}
|
||||
//# sourceMappingURL=options.js.map
|
||||
+1
@@ -0,0 +1 @@
|
||||
{"version":3,"file":"options.js","sourceRoot":"","sources":["../../../src/options.ts"],"names":[],"mappings":";;AAsCA,kCAMC;AA9BD,SAAS,eAAe,CAAC,EACvB,iBAAiB,GAAG,IAAI,EACxB,mBAAmB,GAAG,KAAK,EAC3B,QAAQ,GAAG,IAAI,EACf,gBAAgB,GAAG,KAAK,EACxB,eAAe,GAAG,IAAI,EACtB,WAAW,GAAG,IAAI,EAClB,UAAU,GAAG,IAAI,EACjB,gBAAgB,GAAG,IAAI,GACL;IAClB,OAAO;QACL,iBAAiB;QACjB,mBAAmB;QACnB,QAAQ;QACR,gBAAgB;QAChB,eAAe;QACf,WAAW;QACX,UAAU;QACV,gBAAgB;KACjB,CAAC;AACJ,CAAC;AAED,MAAM,eAAe,GAAG,eAAe,CAAC,eAAe,CAAC,EAAE,CAAC,CAAC;AAE5D,SAAgB,WAAW,CAAC,OAA2B;IACrD,IAAI,OAAO,KAAK,SAAS,EAAE,CAAC;QAC1B,OAAO,eAAe,CAAC;IACzB,CAAC;IAED,OAAO,eAAe,CAAC,eAAe,CAAC,OAAO,CAAC,CAAC;AAClD,CAAC"}
|
||||
+14
@@ -0,0 +1,14 @@
|
||||
"use strict";
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.default = getSubdomain;
|
||||
/**
|
||||
* Returns the subdomain of a hostname string
|
||||
*/
|
||||
function getSubdomain(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);
|
||||
}
|
||||
//# sourceMappingURL=subdomain.js.map
|
||||
+1
@@ -0,0 +1 @@
|
||||
{"version":3,"file":"subdomain.js","sourceRoot":"","sources":["../../../src/subdomain.ts"],"names":[],"mappings":";;AAGA,+BAOC;AAVD;;GAEG;AACH,SAAwB,YAAY,CAAC,QAAgB,EAAE,MAAc;IACnE,uEAAuE;IACvE,IAAI,MAAM,CAAC,MAAM,KAAK,QAAQ,CAAC,MAAM,EAAE,CAAC;QACtC,OAAO,EAAE,CAAC;IACZ,CAAC;IAED,OAAO,QAAQ,CAAC,KAAK,CAAC,CAAC,EAAE,CAAC,MAAM,CAAC,MAAM,GAAG,CAAC,CAAC,CAAC;AAC/C,CAAC"}
|
||||
+1
File diff suppressed because one or more lines are too long
+4
@@ -0,0 +1,4 @@
|
||||
export { parseImpl, getEmptyResult, resetResult, } from './src/factory';
|
||||
export { default as fastPathLookup } from './src/lookup/fast-path';
|
||||
export { setDefaults } from './src/options';
|
||||
//# sourceMappingURL=index.js.map
|
||||
+1
@@ -0,0 +1 @@
|
||||
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../index.ts"],"names":[],"mappings":"AAAA,OAAO,EAEL,SAAS,EAET,cAAc,EACd,WAAW,GACZ,MAAM,eAAe,CAAC;AAEvB,OAAO,EAAE,OAAO,IAAI,cAAc,EAAE,MAAM,wBAAwB,CAAC;AACnE,OAAO,EAAY,WAAW,EAAE,MAAM,eAAe,CAAC"}
|
||||
+12
@@ -0,0 +1,12 @@
|
||||
/**
|
||||
* Return the part of domain without suffix.
|
||||
*
|
||||
* Example: for domain 'foo.com', the result would be 'foo'.
|
||||
*/
|
||||
export default function getDomainWithoutSuffix(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);
|
||||
}
|
||||
//# sourceMappingURL=domain-without-suffix.js.map
|
||||
+1
@@ -0,0 +1 @@
|
||||
{"version":3,"file":"domain-without-suffix.js","sourceRoot":"","sources":["../../../src/domain-without-suffix.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AACH,MAAM,CAAC,OAAO,UAAU,sBAAsB,CAC5C,MAAc,EACd,MAAc;IAEd,0EAA0E;IAC1E,yEAAyE;IACzE,0CAA0C;IAC1C,OAAO,MAAM,CAAC,KAAK,CAAC,CAAC,EAAE,CAAC,MAAM,CAAC,MAAM,GAAG,CAAC,CAAC,CAAC;AAC7C,CAAC"}
|
||||
+80
@@ -0,0 +1,80 @@
|
||||
/**
|
||||
* 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
|
||||
*/
|
||||
export default function getDomain(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);
|
||||
}
|
||||
//# sourceMappingURL=domain.js.map
|
||||
+1
@@ -0,0 +1 @@
|
||||
{"version":3,"file":"domain.js","sourceRoot":"","sources":["../../../src/domain.ts"],"names":[],"mappings":"AAEA;;;;;;;;;;GAUG;AACH,SAAS,qBAAqB,CAAC,QAAgB,EAAE,KAAa;IAC5D,IAAI,QAAQ,CAAC,QAAQ,CAAC,KAAK,CAAC,EAAE,CAAC;QAC7B,OAAO,CACL,QAAQ,CAAC,MAAM,KAAK,KAAK,CAAC,MAAM;YAChC,QAAQ,CAAC,QAAQ,CAAC,MAAM,GAAG,KAAK,CAAC,MAAM,GAAG,CAAC,CAAC,KAAK,GAAG,CACrD,CAAC;IACJ,CAAC;IAED,OAAO,KAAK,CAAC;AACf,CAAC;AAED;;GAEG;AACH,SAAS,uBAAuB,CAC9B,QAAgB,EAChB,YAAoB;IAEpB,2EAA2E;IAC3E,qBAAqB;IACrB,EAAE;IACF,YAAY;IACZ,qCAAqC;IACrC,iBAAiB;IACjB,wCAAwC;IACxC,kCAAkC;IAClC,EAAE;IACF,wCAAwC;IACxC,gBAAgB;IAChB,uCAAuC;IACvC,QAAQ;IACR,mDAAmD;IACnD,MAAM,iBAAiB,GAAG,QAAQ,CAAC,MAAM,GAAG,YAAY,CAAC,MAAM,GAAG,CAAC,CAAC;IACpE,MAAM,wBAAwB,GAAG,QAAQ,CAAC,WA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0,0 +1,435 @@
|
||||
/**
|
||||
* 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.
|
||||
export 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.
|
||||
*/
|
||||
export default 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;
|
||||
}
|
||||
//# sourceMappingURL=extract-hostname.js.map
|
||||
+1
File diff suppressed because one or more lines are too long
+123
@@ -0,0 +1,123 @@
|
||||
/**
|
||||
* 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.
|
||||
*/
|
||||
import getDomain from './domain';
|
||||
import getDomainWithoutSuffix from './domain-without-suffix';
|
||||
import extractHostname, { extractedHostnameValidated, } from './extract-hostname';
|
||||
import isIp from './is-ip';
|
||||
import isSpecialUse from './is-special-use';
|
||||
import isValidHostname from './is-valid';
|
||||
import { setDefaults } from './options';
|
||||
import getSubdomain from './subdomain';
|
||||
export function getEmptyResult() {
|
||||
return {
|
||||
domain: null,
|
||||
domainWithoutSuffix: null,
|
||||
hostname: null,
|
||||
isIcann: null,
|
||||
isIp: null,
|
||||
isPrivate: null,
|
||||
isSpecialUse: null,
|
||||
publicSuffix: null,
|
||||
subdomain: null,
|
||||
};
|
||||
}
|
||||
export 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;
|
||||
}
|
||||
export 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(result.publicSuffix, result.hostname, options);
|
||||
if (step === 3 /* FLAG.DOMAIN */ || result.domain === null) {
|
||||
return result;
|
||||
}
|
||||
// Extract subdomain
|
||||
result.subdomain = getSubdomain(result.hostname, result.domain);
|
||||
if (step === 4 /* FLAG.SUB_DOMAIN */) {
|
||||
return result;
|
||||
}
|
||||
// Extract domain without suffix
|
||||
result.domainWithoutSuffix = getDomainWithoutSuffix(result.domain, result.publicSuffix);
|
||||
return result;
|
||||
}
|
||||
//# sourceMappingURL=factory.js.map
|
||||
+1
@@ -0,0 +1 @@
|
||||
{"version":3,"file":"factory.js","sourceRoot":"","sources":["../../../src/factory.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,OAAO,SAAS,MAAM,UAAU,CAAC;AACjC,OAAO,sBAAsB,MAAM,yBAAyB,CAAC;AAC7D,OAAO,eAAe,EAAE,EACtB,0BAA0B,GAC3B,MAAM,oBAAoB,CAAC;AAC5B,OAAO,IAAI,MAAM,SAAS,CAAC;AAC3B,OAAO,YAAY,MAAM,kBAAkB,CAAC;AAC5C,OAAO,eAAe,MAAM,YAAY,CAAC;AAEzC,OAAO,EAAY,WAAW,EAAE,MAAM,WAAW,CAAC;AAClD,OAAO,YAAY,MAAM,aAAa,CAAC;AA+BvC,MAAM,UAAU,cAAc;IAC5B,OAAO;QACL,MAAM,EAAE,IAAI;QACZ,mBAAmB,EAAE,IAAI;QACzB,QAAQ,EAAE,IAAI;QACd,OAAO,EAAE,IAAI;QACb,IAAI,EAAE,IAAI;QACV,SAAS,EAAE,IAAI;QACf,YAAY,EAAE,IAAI;QAClB,YAAY,EAAE,IAAI;QAClB,SAAS,EAAE,IAAI;KAChB,CAAC;AACJ,CAAC;AAED,MAAM,UAAU,WAAW,CAAC,MAAe;IACzC,MAAM,CAAC,MAAM,GAAG,IAAI,CAAC;IACrB,MAAM,CAAC,mBAAmB,GAAG,IAAI,CAAC;IAClC,MAAM,CAAC,QAAQ,GAAG,IAAI,CAAC;IACvB,MAAM,CAAC,OAAO,GAAG,IAAI,CAAC;IACtB,MAAM,CAAC,IAAI,GAAG,IAAI,CAAC;IACnB,MAAM,CAAC,SAAS,GAAG,IAAI,CAAC;IACxB,MAAM,CAAC,YAAY,GAAG,IAAI,CAAC;IAC3B,MAAM,CAAC,YAAY,GAAG,IAAI,CAAC;IAC3B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AAC,SAAS,GAAG,YAAY,CAAC,MAAM,CAAC,QAAQ,EAAE,MAAM,CAAC,MAAM,CAAC,CAAC;IAChE,IAAI,IAAI,4BAAoB,EAAE,CAAC;QAC7B,OAAO,MAAM,CAAC;IAChB,CAAC;IAED,gCAAgC;IAChC,MAAM,CAAC,mBAAmB,GAAG,sBAAsB,CACjD,MAAM,CAAC,MAAM,EACb,MAAM,CAAC,YAAY,CACpB,CAAC;IAEF,OAAO,MAAM,CAAC;AAChB,CAAC"}
|
||||
+69
@@ -0,0 +1,69 @@
|
||||
/**
|
||||
* 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.
|
||||
*/
|
||||
export default function isIp(hostname) {
|
||||
return isProbablyIpv6(hostname) || isProbablyIpv4(hostname);
|
||||
}
|
||||
//# sourceMappingURL=is-ip.js.map
|
||||
+1
@@ -0,0 +1 @@
|
||||
{"version":3,"file":"is-ip.js","sourceRoot":"","sources":["../../../src/is-ip.ts"],"names":[],"mappings":"AAAA;;;GAGG;AACH,SAAS,cAAc,CAAC,QAAgB;IACtC,iCAAiC;IACjC,IAAI,QAAQ,CAAC,MAAM,GAAG,CAAC,EAAE,CAAC;QACxB,OAAO,KAAK,CAAC;IACf,CAAC;IAED,yCAAyC;IACzC,IAAI,QAAQ,CAAC,MAAM,GAAG,EAAE,EAAE,CAAC;QACzB,OAAO,KAAK,CAAC;IACf,CAAC;IAED,IAAI,YAAY,GAAG,CAAC,CAAC;IAErB,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,QAAQ,CAAC,MAAM,EAAE,CAAC,IAAI,CAAC,EAAE,CAAC;QAC5C,MAAM,IAAI,GAAG,QAAQ,CAAC,UAAU,CAAC,CAAC,CAAC,CAAC;QAEpC,IAAI,IAAI,KAAK,EAAE,CAAC,SAAS,EAAE,CAAC;YAC1B,YAAY,IAAI,CAAC,CAAC;QACpB,CAAC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AAI,IAAI,EAAE,CAAC,IAAI,MAAM;YACpC,CAAC,IAAI,IAAI,EAAE,IAAI,IAAI,IAAI,GAAG,CAAC,IAAI,MAAM;YACrC,CAAC,IAAI,IAAI,EAAE,IAAI,IAAI,IAAI,EAAE,CAAC,CAC3B,CAAC,yDAAyD;SAC5D,EACD,CAAC;YACD,OAAO,KAAK,CAAC;QACf,CAAC;IACH,CAAC;IAED,OAAO,QAAQ,CAAC;AAClB,CAAC;AAED;;;;GAIG;AACH,MAAM,CAAC,OAAO,UAAU,IAAI,CAAC,QAAgB;IAC3C,OAAO,cAAc,CAAC,QAAQ,CAAC,IAAI,cAAc,CAAC,QAAQ,CAAC,CAAC;AAC9D,CAAC"}
|
||||
+62
@@ -0,0 +1,62 @@
|
||||
/**
|
||||
* 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`.
|
||||
*/
|
||||
export default 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;
|
||||
}
|
||||
//# sourceMappingURL=is-special-use.js.map
|
||||
+1
@@ -0,0 +1 @@
|
||||
{"version":3,"file":"is-special-use.js","sourceRoot":"","sources":["../../../src/is-special-use.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;GAyBG;AACH,MAAM,mBAAmB,GAAsB;IAC7C,MAAM,EAAE,WAAW;IACnB,WAAW,EAAE,WAAW;IACxB,SAAS,EAAE,WAAW;IACtB,SAAS,EAAE,WAAW;IACtB,aAAa,EAAE,WAAW;IAC1B,aAAa,EAAE,WAAW;IAC1B,aAAa,EAAE,WAAW;IAC1B,OAAO,EAAE,kBAAkB;IAC3B,OAAO,EAAE,iBAAiB;IAC1B,KAAK,EAAE,WAAW;IAClB,WAAW,EAAE,WAAW;IACxB,eAAe,EAAE,WAAW;IAC5B,eAAe,EAAE,WAAW;IAC5B,cAAc,EAAE,WAAW;IAC3B,aAAa,EAAE,WAAW;IAC1B,UAAU,EAAE,WAAW;CACxB,CAAC;AAEF;;;GAGG;AACH,MAAM,CAAC,OAAO,UAAU,YAAY,CAAC,QAAgB;IACnD,KAAK,MAAM,IAAI,IAAI,mBAAmB,EAAE,CAAC;QACvC,yEAAyE;QACzE,0EAA0E;QAC1E,qBAAqB;QACrB,IACE,QAAQ,CAAC,QAAQ,CAAC,IAAI,CAAC;YACvB,CAAC,QAAQ,CAAC,MAAM,KAAK,IAAI,CAAC,MAAM;gBAC9B,QAAQ,CAAC,UAAU,CAAC,QAAQ,CAAC,MAAM,GAAG,IAAI,CAAC,MAAM,GAAG,CAAC,CAAC,KAAK,EAAE,CAAC,CAAC,SAAS,EAC1E,CAAC;YACD,OAAO,IAAI,CAAC;QACd,CAAC;IACH,CAAC;IAED,OAAO,KAAK,CAAC;AACf,CAAC"}
|
||||
+77
@@ -0,0 +1,77 @@
|
||||
/**
|
||||
* 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.
|
||||
*/
|
||||
export default function (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);
|
||||
}
|
||||
//# sourceMappingURL=is-valid.js.map
|
||||
+1
@@ -0,0 +1 @@
|
||||
{"version":3,"file":"is-valid.js","sourceRoot":"","sources":["../../../src/is-valid.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAEH,6EAA6E;AAC7E,0EAA0E;AAC1E,8EAA8E;AAC9E,+EAA+E;AAC/E,uEAAuE;AACvE,SAAS,YAAY,CAAC,IAAY;IAChC,OAAO,CACL,CAAC,IAAI,IAAI,EAAE,IAAI,IAAI,IAAI,GAAG,CAAC,IAAI,CAAC,IAAI,IAAI,EAAE,IAAI,IAAI,IAAI,EAAE,CAAC,IAAI,IAAI,GAAG,GAAG,CACxE,CAAC;AACJ,CAAC;AAED;;;;;GAKG;AACH,MAAM,CAAC,OAAO,WAAW,QAAgB;IACvC,IAAI,QAAQ,CAAC,MAAM,GAAG,GAAG,EAAE,CAAC;QAC1B,OAAO,KAAK,CAAC;IACf,CAAC;IAED,IAAI,QAAQ,CAAC,MAAM,KAAK,CAAC,EAAE,CAAC;QAC1B,OAAO,KAAK,CAAC;IACf,CAAC;IAED;IACE,eAAe,CAAC,CAAC,YAAY,CAAC,QAAQ,CAAC,UAAU,CAAC,CAAC,CAAC,CAAC;QACrD,QAAQ,CAAC,UAAU,CAAC,CAAC,CAAC,KAAK,EAAE,IAAI,YAAY;QAC7C,QAAQ,CAAC,UAAU,CAAC,CAAC,CAAC,KAAK,EAAE,CAAC,mBAAmB;MACjD,CAAC;QACD,OAAO,KAAK,CAAC;IACf,CAAC;IAED,qCAAqC;IACrC,IAAI,YAAY,GAAG,CAAC,CAAC,CAAC;IACtB,IAAI,YAAY,GAAG,CAAC,CAAC,CAAC;IACtB,MAAM,GAAG,GAAG,QAAQ,CAAC,MAAM,CAAC;IAE5B,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,GAAG,EAAE,CAAC,IAAI,CAAC,EAAE,CAAC;QAChC,MAAM,IAAI,GAAG,QAAQ,CAAC,UAAU,CAAC,CAAC,CAAC,CAAC;QACpC,IAAI,IAAI,KAAK,EAAE,CAAC,SAAS,EAAE,CAAC;YAC1B;YACE,+DAA+D;YAC/D,CAAC,GAAG,YAAY,GAAG,EAAE;gBACrB,sDAAsD;gBACtD,YAAY,KAAK,EAAE;gBACnB,6EAA6E;gBAC7E,+EAA+E;gBAC/E,gEAAgE;gBAChE,YAAY,KAAK,EAAE,EACnB,CAAC;gBACD,OAAO,KAAK,CAAC;YACf,CAAC;YAED,YAAY,GAAG,CAAC,CAAC;QACnB,CAAC;aAAM;QACL,wCAAwC;QACxC,CAAC,EAAC,eAAgB,CAAC,YAAY,CAAC,IAAI,CAAC,IAAI,IAAI,KAAK,EAAE,IAAI,IAAI,KAAK,EAAE,CAAC,CAAC;YACrE,0EAA0E;YAC1E,2EAA2E;YAC3E,6EAA6E;YAC7E,0EAA0E;YAC1E,CAAC,IAAI,KAAK,EAAE,IAAI,YAAY,KAAK,EAAE,CAAC,EACpC,CAAC;YACD,OAAO,KAAK,CAAC;QACf,CAAC;QAED,YAAY,GAAG,IAAI,CAAC;IACtB,CAAC;IAED,OAAO;IACL,iDAAiD;IACjD,GAAG,GAAG,YAAY,GAAG,CAAC,IAAI,EAAE;QAC5B,wEAAwE;QACxE,wEAAwE;QACxE,sDAAsD;QACtD,YAAY,KAAK,EAAE,CACpB,CAAC;AACJ,CAAC"}
|
||||
+66
@@ -0,0 +1,66 @@
|
||||
export default function (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;
|
||||
}
|
||||
//# sourceMappingURL=fast-path.js.map
|
||||
+1
@@ -0,0 +1 @@
|
||||
{"version":3,"file":"fast-path.js","sourceRoot":"","sources":["../../../../src/lookup/fast-path.ts"],"names":[],"mappings":"AAEA,MAAM,CAAC,OAAO,WACZ,QAAgB,EAChB,OAA6B,EAC7B,GAAkB;IAElB,qEAAqE;IACrE,qEAAqE;IACrE,IAAI,CAAC,OAAO,CAAC,mBAAmB,IAAI,QAAQ,CAAC,MAAM,GAAG,CAAC,EAAE,CAAC;QACxD,MAAM,IAAI,GAAW,QAAQ,CAAC,MAAM,GAAG,CAAC,CAAC;QACzC,MAAM,EAAE,GAAW,QAAQ,CAAC,UAAU,CAAC,IAAI,CAAC,CAAC;QAC7C,MAAM,EAAE,GAAW,QAAQ,CAAC,UAAU,CAAC,IAAI,GAAG,CAAC,CAAC,CAAC;QACjD,MAAM,EAAE,GAAW,QAAQ,CAAC,UAAU,CAAC,IAAI,GAAG,CAAC,CAAC,CAAC;QACjD,MAAM,EAAE,GAAW,QAAQ,CAAC,UAAU,CAAC,IAAI,GAAG,CAAC,CAAC,CAAC;QAEjD,IACE,EAAE,KAAK,GAAG,CAAC,SAAS;YACpB,EAAE,KAAK,GAAG,CAAC,SAAS;YACpB,EAAE,KAAK,EAAE,CAAC,SAAS;YACnB,EAAE,KAAK,EAAE,CAAC,SAAS,EACnB,CAAC;YACD,GAAG,CAAC,OAAO,GAAG,IAAI,CAAC;YACnB,GAAG,CAAC,SAAS,GAAG,KAAK,CAAC;YACtB,GAAG,CAAC,YAAY,GAAG,KAAK,CAAC;YACzB,OAAO,IAAI,CAAC;QACd,CAAC;aAAM,IACL,EAAE,KAAK,GAAG,CAAC,SAAS;YACpB,EAAE,KAAK,GAAG,CAAC,SAAS;YACpB,EAAE,KAAK,GAAG,CAAC,SAAS;YACpB,EAAE,KAAK,EAAE,CAAC,SAAS,E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AE,CAAC,SAAS,EACnB,CAAC;YACD,GAAG,CAAC,OAAO,GAAG,IAAI,CAAC;YACnB,GAAG,CAAC,SAAS,GAAG,KAAK,CAAC;YACtB,GAAG,CAAC,YAAY,GAAG,IAAI,CAAC;YACxB,OAAO,IAAI,CAAC;QACd,CAAC;IACH,CAAC;IAED,OAAO,KAAK,CAAC;AACf,CAAC"}
|
||||
+2
@@ -0,0 +1,2 @@
|
||||
export {};
|
||||
//# sourceMappingURL=interface.js.map
|
||||
+1
@@ -0,0 +1 @@
|
||||
{"version":3,"file":"interface.js","sourceRoot":"","sources":["../../../../src/lookup/interface.ts"],"names":[],"mappings":""}
|
||||
+20
@@ -0,0 +1,20 @@
|
||||
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({});
|
||||
export function setDefaults(options) {
|
||||
if (options === undefined) {
|
||||
return DEFAULT_OPTIONS;
|
||||
}
|
||||
return /*@__INLINE__*/ setDefaultsImpl(options);
|
||||
}
|
||||
//# sourceMappingURL=options.js.map
|
||||
+1
@@ -0,0 +1 @@
|
||||
{"version":3,"file":"options.js","sourceRoot":"","sources":["../../../src/options.ts"],"names":[],"mappings":"AAcA,SAAS,eAAe,CAAC,EACvB,iBAAiB,GAAG,IAAI,EACxB,mBAAmB,GAAG,KAAK,EAC3B,QAAQ,GAAG,IAAI,EACf,gBAAgB,GAAG,KAAK,EACxB,eAAe,GAAG,IAAI,EACtB,WAAW,GAAG,IAAI,EAClB,UAAU,GAAG,IAAI,EACjB,gBAAgB,GAAG,IAAI,GACL;IAClB,OAAO;QACL,iBAAiB;QACjB,mBAAmB;QACnB,QAAQ;QACR,gBAAgB;QAChB,eAAe;QACf,WAAW;QACX,UAAU;QACV,gBAAgB;KACjB,CAAC;AACJ,CAAC;AAED,MAAM,eAAe,GAAG,eAAe,CAAC,eAAe,CAAC,EAAE,CAAC,CAAC;AAE5D,MAAM,UAAU,WAAW,CAAC,OAA2B;IACrD,IAAI,OAAO,KAAK,SAAS,EAAE,CAAC;QAC1B,OAAO,eAAe,CAAC;IACzB,CAAC;IAED,OAAO,eAAe,CAAC,eAAe,CAAC,OAAO,CAAC,CAAC;AAClD,CAAC"}
|
||||
+11
@@ -0,0 +1,11 @@
|
||||
/**
|
||||
* Returns the subdomain of a hostname string
|
||||
*/
|
||||
export default function getSubdomain(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);
|
||||
}
|
||||
//# sourceMappingURL=subdomain.js.map
|
||||
+1
@@ -0,0 +1 @@
|
||||
{"version":3,"file":"subdomain.js","sourceRoot":"","sources":["../../../src/subdomain.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,MAAM,CAAC,OAAO,UAAU,YAAY,CAAC,QAAgB,EAAE,MAAc;IACnE,uEAAuE;IACvE,IAAI,MAAM,CAAC,MAAM,KAAK,QAAQ,CAAC,MAAM,EAAE,CAAC;QACtC,OAAO,EAAE,CAAC;IACZ,CAAC;IAED,OAAO,QAAQ,CAAC,KAAK,CAAC,CAAC,EAAE,CAAC,MAAM,CAAC,MAAM,GAAG,CAAC,CAAC,CAAC;AAC/C,CAAC"}
|
||||
+1
File diff suppressed because one or more lines are too long
+4
@@ -0,0 +1,4 @@
|
||||
export { FLAG, parseImpl, IResult, getEmptyResult, resetResult, } from './src/factory';
|
||||
export { IPublicSuffix, ISuffixLookupOptions } from './src/lookup/interface';
|
||||
export { default as fastPathLookup } from './src/lookup/fast-path';
|
||||
export { IOptions, setDefaults } from './src/options';
|
||||
+6
@@ -0,0 +1,6 @@
|
||||
/**
|
||||
* Return the part of domain without suffix.
|
||||
*
|
||||
* Example: for domain 'foo.com', the result would be 'foo'.
|
||||
*/
|
||||
export default function getDomainWithoutSuffix(domain: string, suffix: string): string;
|
||||
+5
@@ -0,0 +1,5 @@
|
||||
import { IOptions } from './options';
|
||||
/**
|
||||
* Detects the domain based on rules and upon and a host string
|
||||
*/
|
||||
export default function getDomain(suffix: string, hostname: string, options: IOptions): string | null;
|
||||
+17
@@ -0,0 +1,17 @@
|
||||
export declare let extractedHostnameValidated: boolean;
|
||||
/**
|
||||
* 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.
|
||||
*/
|
||||
export default function extractHostname(url: string, urlIsValidHostname: boolean, validate?: boolean): string | null;
|
||||
+29
@@ -0,0 +1,29 @@
|
||||
/**
|
||||
* 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.
|
||||
*/
|
||||
import { IPublicSuffix, ISuffixLookupOptions } from './lookup/interface';
|
||||
import { IOptions } from './options';
|
||||
export interface IResult {
|
||||
hostname: string | null;
|
||||
isIp: boolean | null;
|
||||
subdomain: string | null;
|
||||
domain: string | null;
|
||||
publicSuffix: string | null;
|
||||
domainWithoutSuffix: string | null;
|
||||
isIcann: boolean | null;
|
||||
isPrivate: boolean | null;
|
||||
isSpecialUse: boolean | null;
|
||||
}
|
||||
export declare function getEmptyResult(): IResult;
|
||||
export declare function resetResult(result: IResult): void;
|
||||
export declare const enum FLAG {
|
||||
HOSTNAME = 0,
|
||||
IS_VALID = 1,
|
||||
PUBLIC_SUFFIX = 2,
|
||||
DOMAIN = 3,
|
||||
SUB_DOMAIN = 4,
|
||||
ALL = 5
|
||||
}
|
||||
export declare function parseImpl(url: string, step: FLAG, suffixLookup: (_1: string, _2: ISuffixLookupOptions, _3: IPublicSuffix) => void, partialOptions: Partial<IOptions> | undefined, result: IResult): IResult;
|
||||
+6
@@ -0,0 +1,6 @@
|
||||
/**
|
||||
* 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.
|
||||
*/
|
||||
export default function isIp(hostname: string): boolean;
|
||||
+5
@@ -0,0 +1,5 @@
|
||||
/**
|
||||
* 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`.
|
||||
*/
|
||||
export default function isSpecialUse(hostname: string): boolean;
|
||||
+15
@@ -0,0 +1,15 @@
|
||||
/**
|
||||
* 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.
|
||||
*/
|
||||
/**
|
||||
* 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.
|
||||
*/
|
||||
export default function (hostname: string): boolean;
|
||||
+2
@@ -0,0 +1,2 @@
|
||||
import { IPublicSuffix, ISuffixLookupOptions } from './interface';
|
||||
export default function (hostname: string, options: ISuffixLookupOptions, out: IPublicSuffix): boolean;
|
||||
+9
@@ -0,0 +1,9 @@
|
||||
export interface IPublicSuffix {
|
||||
isIcann: boolean | null;
|
||||
isPrivate: boolean | null;
|
||||
publicSuffix: string | null;
|
||||
}
|
||||
export interface ISuffixLookupOptions {
|
||||
allowIcannDomains: boolean;
|
||||
allowPrivateDomains: boolean;
|
||||
}
|
||||
+11
@@ -0,0 +1,11 @@
|
||||
export interface IOptions {
|
||||
allowIcannDomains: boolean;
|
||||
allowPrivateDomains: boolean;
|
||||
detectIp: boolean;
|
||||
detectSpecialUse: boolean;
|
||||
extractHostname: boolean;
|
||||
mixedInputs: boolean;
|
||||
validHosts: string[] | null;
|
||||
validateHostname: boolean;
|
||||
}
|
||||
export declare function setDefaults(options?: Partial<IOptions>): IOptions;
|
||||
+4
@@ -0,0 +1,4 @@
|
||||
/**
|
||||
* Returns the subdomain of a hostname string
|
||||
*/
|
||||
export default function getSubdomain(hostname: string, domain: string): string;
|
||||
Reference in New Issue
Block a user