UNPKG

1.95 kBTypeScriptView Raw
1import { ipVersion } from "is-ip";
2import { Label } from "./parse-domain.js";
3import { NO_HOSTNAME } from "./from-url.js";
4export declare enum Validation {
5 /**
6 * Allows any octets as labels
7 * but still restricts the length of labels and the overall domain.
8 *
9 * @see https://www.rfc-editor.org/rfc/rfc2181#section-11
10 **/
11 Lax = "LAX",
12 /**
13 * Only allows ASCII letters, digits and hyphens (aka LDH),
14 * forbids hyphens at the beginning or end of a label
15 * and requires top-level domain names not to be all-numeric.
16 *
17 * This is the default if no validation is configured.
18 *
19 * @see https://datatracker.ietf.org/doc/html/rfc3696#section-2
20 */
21 Strict = "STRICT"
22}
23export declare enum ValidationErrorType {
24 NoHostname = "NO_HOSTNAME",
25 DomainMaxLength = "DOMAIN_MAX_LENGTH",
26 LabelMinLength = "LABEL_MIN_LENGTH",
27 LabelMaxLength = "LABEL_MAX_LENGTH",
28 LabelInvalidCharacter = "LABEL_INVALID_CHARACTER",
29 LastLabelInvalid = "LAST_LABEL_INVALID"
30}
31export type ValidationError = {
32 type: ValidationErrorType;
33 message: string;
34 column: number;
35};
36export declare enum SanitizationResultType {
37 ValidIp = "VALID_IP",
38 ValidDomain = "VALID_DOMAIN",
39 Error = "ERROR"
40}
41export type SanitizationResultValidIp = {
42 type: SanitizationResultType.ValidIp;
43 ip: string;
44 ipVersion: Exclude<ReturnType<typeof ipVersion>, undefined>;
45};
46export type SanitizationResultValidDomain = {
47 type: SanitizationResultType.ValidDomain;
48 domain: string;
49 labels: Array<Label>;
50};
51export type SanitizationResultError = {
52 type: SanitizationResultType.Error;
53 errors: Array<ValidationError>;
54};
55export type SanitizationResult = SanitizationResultValidIp | SanitizationResultValidDomain | SanitizationResultError;
56export declare const sanitize: (input: string | typeof NO_HOSTNAME, options?: {
57 validation?: Validation;
58}) => SanitizationResult;