1 line
34 KiB
Text
1 line
34 KiB
Text
|
{"version":3,"file":"index.js","sources":["../../src/domain.ts","../../src/domain-without-suffix.ts","../../src/extract-hostname.ts","../../src/is-ip.ts","../../src/is-valid.ts","../../src/options.ts","../../src/subdomain.ts","../../src/factory.ts","../../src/lookup/fast-path.ts"],"sourcesContent":["import { IOptions } from './options';\n\n/**\n * Check if `vhost` is a valid suffix of `hostname` (top-domain)\n *\n * It means that `vhost` needs to be a suffix of `hostname` and we then need to\n * make sure that: either they are equal, or the character preceding `vhost` in\n * `hostname` is a '.' (it should not be a partial label).\n *\n * * hostname = 'not.evil.com' and vhost = 'vil.com' => not ok\n * * hostname = 'not.evil.com' and vhost = 'evil.com' => ok\n * * hostname = 'not.evil.com' and vhost = 'not.evil.com' => ok\n */\nfunction shareSameDomainSuffix(hostname: string, vhost: string): boolean {\n if (hostname.endsWith(vhost)) {\n return (\n hostname.length === vhost.length ||\n hostname[hostname.length - vhost.length - 1] === '.'\n );\n }\n\n return false;\n}\n\n/**\n * Given a hostname and its public suffix, extract the general domain.\n */\nfunction extractDomainWithSuffix(\n hostname: string,\n publicSuffix: string,\n): string {\n // Locate the index of the last '.' in the part of the `hostname` preceding\n // the public suffix.\n //\n // examples:\n // 1. not.evil.co.uk => evil.co.uk\n // ^ ^\n // | | start of public suffix\n // | index of the last dot\n //\n // 2. example.co.uk => example.co.uk\n // ^ ^\n // | | start of public suffix\n // |\n // | (-1) no dot found before the public suffix\n const publicSuffixIndex = hostname.length - publicSuffix.length - 2;\n const lastDotBeforeSuffixIndex = hostname.lastIndexOf('.', publicSuffixIndex);\n\n // No '.' found, then `hostname` is the general domain (no sub-domain)\n if (lastDotBeforeSuffixIndex === -1) {\n return hostname;\n }\n\n // Extract the part between the last '.'\n return hostname.slice(lastDotBeforeSuffixIndex + 1);\n}\n\n/**\n * Detects the domain based on rules and upon and a host string\n */\nexport default function getDomain(\n suffix: string,\n hostname: string,\n options: IOptions,\n): string | null {\n // Check if `hostname` ends with a member of `validHosts`.\n if (options.validHosts !== null) {\n const validHosts = options.validHosts;\n for (const vhost of validHosts) {\n if (/*@__INLINE__*/ shareSameDomainSuffix(hostname, vhost)) {\n return vhost;\n }\n }\n }\n\n let numberOfLeadingDots = 0;\n if (hostname.startsWith('.')) {\n while (\n numberOfLeadingDots < hostname.length &&\n hostname[numberOfLeadingDots] === '.'\n ) {\n numberOfLeadingDots += 1;\n }\n }\n\n // If `hostname` is a valid public suffix, then there is no domain to return.\n // Since we already know that `getPublicSuffix` returns a suffix of `hostname`\n // there is no need to perform a string comparison and we only compare the\n // size.\n if (suffix.length === hostname.length - numberOfLeadingDots) {\n return null;\n }\n\n // To extract the general domain, we start by identifying the public suffix\n // (if any), then consider the domain to be the public suffix with one added\n // level of depth. (e.g.: if hostname is `not.evil.co.uk` and public suffix:\n // `co.uk`, then we take one more level: `evil`, giving the final result:\n // `evil.co.uk`).\n return /*@__INLINE__*/ extractDomainWithSuffix(hostname, suffix);\n}\n","/**\n * Return the part of domain without suffix.\n *\n * Example: for domain 'foo.com', the result would be 'foo'.\n */\nexport default function getDomainWithoutSuffix(\n domain: string,\n suffix: string,\n): string {\n // Note: here `domain` and `suffix` cannot have the same length because in\n // this case we set `domain` to `null` instead. It is thus safe to assume\n // that `suffix` is shorter than `domain`.\n return domain.slice(0, -suffix.length - 1);\n}\n",
|