|
|
|
|
|
|
|
|
|
|
|
|
|
import getDomain from './domain'; |
|
import getDomainWithoutSuffix from './domain-without-suffix'; |
|
import extractHostname from './extract-hostname'; |
|
import isIp from './is-ip'; |
|
import isValidHostname from './is-valid'; |
|
import { IPublicSuffix, ISuffixLookupOptions } from './lookup/interface'; |
|
import { IOptions, setDefaults } from './options'; |
|
import getSubdomain from './subdomain'; |
|
|
|
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; |
|
} |
|
|
|
export function getEmptyResult(): IResult { |
|
return { |
|
domain: null, |
|
domainWithoutSuffix: null, |
|
hostname: null, |
|
isIcann: null, |
|
isIp: null, |
|
isPrivate: null, |
|
publicSuffix: null, |
|
subdomain: null, |
|
}; |
|
} |
|
|
|
export function resetResult(result: IResult): void { |
|
result.domain = null; |
|
result.domainWithoutSuffix = null; |
|
result.hostname = null; |
|
result.isIcann = null; |
|
result.isIp = null; |
|
result.isPrivate = null; |
|
result.publicSuffix = null; |
|
result.subdomain = null; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
export const enum FLAG { |
|
HOSTNAME, |
|
IS_VALID, |
|
PUBLIC_SUFFIX, |
|
DOMAIN, |
|
SUB_DOMAIN, |
|
ALL, |
|
} |
|
|
|
export function parseImpl( |
|
url: string, |
|
step: FLAG, |
|
suffixLookup: ( |
|
_1: string, |
|
_2: ISuffixLookupOptions, |
|
_3: IPublicSuffix, |
|
) => void, |
|
partialOptions: Partial<IOptions>, |
|
result: IResult, |
|
): IResult { |
|
const options: IOptions = setDefaults(partialOptions); |
|
|
|
|
|
|
|
|
|
if (typeof url !== 'string') { |
|
return result; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (!options.extractHostname) { |
|
result.hostname = url; |
|
} else if (options.mixedInputs) { |
|
result.hostname = extractHostname(url, isValidHostname(url)); |
|
} else { |
|
result.hostname = extractHostname(url, false); |
|
} |
|
|
|
if (step === FLAG.HOSTNAME || result.hostname === null) { |
|
return result; |
|
} |
|
|
|
|
|
if (options.detectIp) { |
|
result.isIp = isIp(result.hostname); |
|
if (result.isIp) { |
|
return result; |
|
} |
|
} |
|
|
|
|
|
|
|
if ( |
|
options.validateHostname && |
|
options.extractHostname && |
|
!isValidHostname(result.hostname) |
|
) { |
|
result.hostname = null; |
|
return result; |
|
} |
|
|
|
|
|
suffixLookup(result.hostname, options, result); |
|
if (step === FLAG.PUBLIC_SUFFIX || result.publicSuffix === null) { |
|
return result; |
|
} |
|
|
|
|
|
result.domain = getDomain(result.publicSuffix, result.hostname, options); |
|
if (step === FLAG.DOMAIN || result.domain === null) { |
|
return result; |
|
} |
|
|
|
|
|
result.subdomain = getSubdomain(result.hostname, result.domain); |
|
if (step === FLAG.SUB_DOMAIN) { |
|
return result; |
|
} |
|
|
|
|
|
result.domainWithoutSuffix = getDomainWithoutSuffix( |
|
result.domain, |
|
result.publicSuffix, |
|
); |
|
|
|
return result; |
|
} |
|
|