1 | import { ValidationError, SanitizationResultValidIp, Validation } from "./sanitize.js";
|
2 | import { NO_HOSTNAME } from "./from-url.js";
|
3 | export declare const RESERVED_TOP_LEVEL_DOMAINS: string[];
|
4 | export type Label = string;
|
5 | export declare enum ParseResultType {
|
6 | /**
|
7 | * This parse result is returned in case the given hostname does not adhere to [RFC 1034](https://tools.ietf.org/html/rfc1034).
|
8 | */
|
9 | Invalid = "INVALID",
|
10 | /**
|
11 | * This parse result is returned if the given hostname was an IPv4 or IPv6.
|
12 | */
|
13 | Ip = "IP",
|
14 | /**
|
15 | * This parse result is returned when the given hostname
|
16 | * - is the root domain (the empty string `""`)
|
17 | * - belongs to the top-level domain `localhost`, `local`, `example`, `invalid` or `test`
|
18 | */
|
19 | Reserved = "RESERVED",
|
20 | /**
|
21 | * This parse result is returned when the given hostname is valid and does not belong to a reserved top-level domain, but is not listed in the public suffix list.
|
22 | */
|
23 | NotListed = "NOT_LISTED",
|
24 | /**
|
25 | * This parse result is returned when the given hostname belongs to a top-level domain that is listed in the public suffix list.
|
26 | */
|
27 | Listed = "LISTED"
|
28 | }
|
29 | type ParseResultCommon<Type extends ParseResultType> = {
|
30 | /**
|
31 | * The type of the parse result. Use switch or if to distinguish between different results.
|
32 | */
|
33 | type: Type;
|
34 | /**
|
35 | * The original hostname that was passed to parseDomain().
|
36 | */
|
37 | hostname: Type extends ParseResultType.Invalid ? string | typeof NO_HOSTNAME : string;
|
38 | };
|
39 | export type ParseResultInvalid = ParseResultCommon<ParseResultType.Invalid> & {
|
40 | /**
|
41 | * An array of validation errors.
|
42 | */
|
43 | errors: Array<ValidationError>;
|
44 | };
|
45 | type ParseResultCommonValidDomain = {
|
46 | /**
|
47 | * An array of labels that were separated by a dot character in the given hostname.
|
48 | */
|
49 | labels: Array<Label>;
|
50 | };
|
51 | export type ParseResultIp = ParseResultCommon<ParseResultType.Ip> & Pick<SanitizationResultValidIp, "ipVersion">;
|
52 | export type ParseResultReserved = ParseResultCommon<ParseResultType.Reserved> & ParseResultCommonValidDomain;
|
53 | export type ParseResultNotListed = ParseResultCommon<ParseResultType.NotListed> & ParseResultCommonValidDomain;
|
54 | type ParseResultListedDomains = {
|
55 | /**
|
56 | * An array of labels that belong to the subdomain. Can be empty if there was no subdomain in the given hostname.
|
57 | */
|
58 | subDomains: Array<Label>;
|
59 | /**
|
60 | * The first label that belongs to the user-controlled section of the hostname. Can be undefined if just a top-level domain was passed to parseDomain().
|
61 | */
|
62 | domain: Label | undefined;
|
63 | /**
|
64 | * An array of labels that are controlled by the domain registrar.
|
65 | */
|
66 | topLevelDomains: Array<Label>;
|
67 | };
|
68 | export type ParseResultListed = ParseResultCommon<ParseResultType.Listed> & ParseResultCommonValidDomain & ParseResultListedDomains & {
|
69 | /**
|
70 | * The parse result according to ICANN only without private top-level domains.
|
71 | */
|
72 | icann: ParseResultListedDomains;
|
73 | };
|
74 | export type ParseResult = ParseResultInvalid | ParseResultIp | ParseResultReserved | ParseResultNotListed | ParseResultListed;
|
75 | export type ParseDomainOptions = {
|
76 | /**
|
77 | * If no validation is specified, Validation.Strict will be used.
|
78 | **/
|
79 | validation?: Validation;
|
80 | };
|
81 | /**
|
82 | * Splits the given hostname in topLevelDomains, a domain and subDomains.
|
83 | */
|
84 | export declare const parseDomain: (hostname: string | typeof NO_HOSTNAME, options?: ParseDomainOptions) => ParseResult;
|
85 | export {};
|