Spaces:
Running
Running
File size: 5,696 Bytes
d4b85c0 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 |
import punycode from 'punycode/punycode.js';
import rules from './data/rules.js';
//
// Parse rules from file.
//
const rulesByPunySuffix = rules.reduce(
(map, rule) => {
const suffix = rule.replace(/^(\*\.|\!)/, '');
const punySuffix = punycode.toASCII(suffix);
const firstChar = rule.charAt(0);
if (map.has(punySuffix)) {
throw new Error(`Multiple rules found for ${rule} (${punySuffix})`);
}
map.set(punySuffix, {
rule,
suffix,
punySuffix,
wildcard: firstChar === '*',
exception: firstChar === '!'
});
return map;
},
new Map(),
);
//
// Find rule for a given domain.
//
const findRule = (domain) => {
const punyDomain = punycode.toASCII(domain);
const punyDomainChunks = punyDomain.split('.');
for (let i = 0; i < punyDomainChunks.length; i++) {
const suffix = punyDomainChunks.slice(i).join('.');
const matchingRules = rulesByPunySuffix.get(suffix);
if (matchingRules) {
return matchingRules;
}
}
return null;
};
//
// Error codes and messages.
//
export const errorCodes = {
DOMAIN_TOO_SHORT: 'Domain name too short.',
DOMAIN_TOO_LONG: 'Domain name too long. It should be no more than 255 chars.',
LABEL_STARTS_WITH_DASH: 'Domain name label can not start with a dash.',
LABEL_ENDS_WITH_DASH: 'Domain name label can not end with a dash.',
LABEL_TOO_LONG: 'Domain name label should be at most 63 chars long.',
LABEL_TOO_SHORT: 'Domain name label should be at least 1 character long.',
LABEL_INVALID_CHARS: 'Domain name label can only contain alphanumeric characters or dashes.'
};
//
// Validate domain name and throw if not valid.
//
// From wikipedia:
//
// Hostnames are composed of series of labels concatenated with dots, as are all
// domain names. Each label must be between 1 and 63 characters long, and the
// entire hostname (including the delimiting dots) has a maximum of 255 chars.
//
// Allowed chars:
//
// * `a-z`
// * `0-9`
// * `-` but not as a starting or ending character
// * `.` as a separator for the textual portions of a domain name
//
// * http://en.wikipedia.org/wiki/Domain_name
// * http://en.wikipedia.org/wiki/Hostname
//
const validate = (input) => {
// Before we can validate we need to take care of IDNs with unicode chars.
const ascii = punycode.toASCII(input);
if (ascii.length < 1) {
return 'DOMAIN_TOO_SHORT';
}
if (ascii.length > 255) {
return 'DOMAIN_TOO_LONG';
}
// Check each part's length and allowed chars.
const labels = ascii.split('.');
let label;
for (let i = 0; i < labels.length; ++i) {
label = labels[i];
if (!label.length) {
return 'LABEL_TOO_SHORT';
}
if (label.length > 63) {
return 'LABEL_TOO_LONG';
}
if (label.charAt(0) === '-') {
return 'LABEL_STARTS_WITH_DASH';
}
if (label.charAt(label.length - 1) === '-') {
return 'LABEL_ENDS_WITH_DASH';
}
if (!/^[a-z0-9\-_]+$/.test(label)) {
return 'LABEL_INVALID_CHARS';
}
}
};
//
// Public API
//
//
// Parse domain.
//
export const parse = (input) => {
if (typeof input !== 'string') {
throw new TypeError('Domain name must be a string.');
}
// Force domain to lowercase.
let domain = input.slice(0).toLowerCase();
// Handle FQDN.
// TODO: Simply remove trailing dot?
if (domain.charAt(domain.length - 1) === '.') {
domain = domain.slice(0, domain.length - 1);
}
// Validate and sanitise input.
const error = validate(domain);
if (error) {
return {
input: input,
error: {
message: errorCodes[error],
code: error
}
};
}
const parsed = {
input: input,
tld: null,
sld: null,
domain: null,
subdomain: null,
listed: false
};
const domainParts = domain.split('.');
// Non-Internet TLD
if (domainParts[domainParts.length - 1] === 'local') {
return parsed;
}
const handlePunycode = () => {
if (!/xn--/.test(domain)) {
return parsed;
}
if (parsed.domain) {
parsed.domain = punycode.toASCII(parsed.domain);
}
if (parsed.subdomain) {
parsed.subdomain = punycode.toASCII(parsed.subdomain);
}
return parsed;
};
const rule = findRule(domain);
// Unlisted tld.
if (!rule) {
if (domainParts.length < 2) {
return parsed;
}
parsed.tld = domainParts.pop();
parsed.sld = domainParts.pop();
parsed.domain = [parsed.sld, parsed.tld].join('.');
if (domainParts.length) {
parsed.subdomain = domainParts.pop();
}
return handlePunycode();
}
// At this point we know the public suffix is listed.
parsed.listed = true;
const tldParts = rule.suffix.split('.');
const privateParts = domainParts.slice(0, domainParts.length - tldParts.length);
if (rule.exception) {
privateParts.push(tldParts.shift());
}
parsed.tld = tldParts.join('.');
if (!privateParts.length) {
return handlePunycode();
}
if (rule.wildcard) {
tldParts.unshift(privateParts.pop());
parsed.tld = tldParts.join('.');
}
if (!privateParts.length) {
return handlePunycode();
}
parsed.sld = privateParts.pop();
parsed.domain = [parsed.sld, parsed.tld].join('.');
if (privateParts.length) {
parsed.subdomain = privateParts.join('.');
}
return handlePunycode();
};
//
// Get domain.
//
export const get = (domain) => {
if (!domain) {
return null;
}
return parse(domain).domain || null;
};
//
// Check whether domain belongs to a known public suffix.
//
export const isValid = (domain) => {
const parsed = parse(domain);
return Boolean(parsed.domain && parsed.listed);
};
export default { parse, get, isValid };
|