source: frontend/node_modules/psl/index.js

Last change on this file was 9af201e, checked in by MBK <marija.karapandzova@…>, 12 days ago

Fix frontend appearance

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