1
- import Punycode from 'punycode/punycode.js' ;
2
-
1
+ import punycode from 'punycode/punycode.js' ;
3
2
import rules from './data/rules.js' ;
4
3
5
- var internals = { } ;
6
-
7
-
8
4
//
9
5
// Parse rules from file.
10
6
//
11
7
const rulesByPunySuffix = rules . reduce (
12
8
( map , rule ) => {
13
9
const suffix = rule . replace ( / ^ ( \* \. | \! ) / , '' ) ;
14
- const punySuffix = Punycode . toASCII ( suffix ) ;
10
+ const punySuffix = punycode . toASCII ( suffix ) ;
15
11
const firstChar = rule . charAt ( 0 ) ;
16
12
17
13
if ( map . has ( punySuffix ) ) {
@@ -31,25 +27,24 @@ const rulesByPunySuffix = rules.reduce(
31
27
new Map ( ) ,
32
28
) ;
33
29
34
-
35
30
//
36
31
// Find rule for a given domain.
37
32
//
38
- internals . findRule = function ( domain ) {
33
+ const findRule = ( domain ) => {
34
+ const punyDomain = punycode . toASCII ( domain ) ;
35
+ const punyDomainChunks = punyDomain . split ( '.' ) ;
39
36
40
- var punyDomain = Punycode . toASCII ( domain ) ;
41
- var punyDomainChunks = punyDomain . split ( '.' ) ;
42
- for ( var i = 0 ; i < punyDomainChunks . length ; i ++ ) {
43
- var suffix = punyDomainChunks . slice ( i ) . join ( '.' ) ;
44
- var matchingRules = rulesByPunySuffix . get ( suffix ) ;
37
+ for ( let i = 0 ; i < punyDomainChunks . length ; i ++ ) {
38
+ const suffix = punyDomainChunks . slice ( i ) . join ( '.' ) ;
39
+ const matchingRules = rulesByPunySuffix . get ( suffix ) ;
45
40
if ( matchingRules ) {
46
41
return matchingRules ;
47
42
}
48
43
}
44
+
49
45
return null ;
50
46
} ;
51
47
52
-
53
48
//
54
49
// Error codes and messages.
55
50
//
@@ -63,7 +58,6 @@ export const errorCodes = {
63
58
LABEL_INVALID_CHARS : 'Domain name label can only contain alphanumeric characters or dashes.'
64
59
} ;
65
60
66
-
67
61
//
68
62
// Validate domain name and throw if not valid.
69
63
//
@@ -83,10 +77,9 @@ export const errorCodes = {
83
77
// * http://en.wikipedia.org/wiki/Domain_name
84
78
// * http://en.wikipedia.org/wiki/Hostname
85
79
//
86
- internals . validate = function ( input ) {
87
-
80
+ const validate = ( input ) => {
88
81
// Before we can validate we need to take care of IDNs with unicode chars.
89
- var ascii = Punycode . toASCII ( input ) ;
82
+ const ascii = punycode . toASCII ( input ) ;
90
83
91
84
if ( ascii . length < 1 ) {
92
85
return 'DOMAIN_TOO_SHORT' ;
@@ -96,10 +89,10 @@ internals.validate = function (input) {
96
89
}
97
90
98
91
// Check each part's length and allowed chars.
99
- var labels = ascii . split ( '.' ) ;
100
- var label ;
92
+ const labels = ascii . split ( '.' ) ;
93
+ let label ;
101
94
102
- for ( var i = 0 ; i < labels . length ; ++ i ) {
95
+ for ( let i = 0 ; i < labels . length ; ++ i ) {
103
96
label = labels [ i ] ;
104
97
if ( ! label . length ) {
105
98
return 'LABEL_TOO_SHORT' ;
@@ -119,23 +112,20 @@ internals.validate = function (input) {
119
112
}
120
113
} ;
121
114
122
-
123
115
//
124
116
// Public API
125
117
//
126
118
127
-
128
119
//
129
120
// Parse domain.
130
121
//
131
- export const parse = function ( input ) {
132
-
122
+ export const parse = ( input ) => {
133
123
if ( typeof input !== 'string' ) {
134
124
throw new TypeError ( 'Domain name must be a string.' ) ;
135
125
}
136
126
137
127
// Force domain to lowercase.
138
- var domain = input . slice ( 0 ) . toLowerCase ( ) ;
128
+ let domain = input . slice ( 0 ) . toLowerCase ( ) ;
139
129
140
130
// Handle FQDN.
141
131
// TODO: Simply remove trailing dot?
@@ -144,7 +134,7 @@ export const parse = function (input) {
144
134
}
145
135
146
136
// Validate and sanitise input.
147
- var error = internals . validate ( domain ) ;
137
+ const error = validate ( domain ) ;
148
138
if ( error ) {
149
139
return {
150
140
input : input ,
@@ -155,7 +145,7 @@ export const parse = function (input) {
155
145
} ;
156
146
}
157
147
158
- var parsed = {
148
+ const parsed = {
159
149
input : input ,
160
150
tld : null ,
161
151
sld : null ,
@@ -164,28 +154,27 @@ export const parse = function (input) {
164
154
listed : false
165
155
} ;
166
156
167
- var domainParts = domain . split ( '.' ) ;
157
+ const domainParts = domain . split ( '.' ) ;
168
158
169
159
// Non-Internet TLD
170
160
if ( domainParts [ domainParts . length - 1 ] === 'local' ) {
171
161
return parsed ;
172
162
}
173
163
174
- var handlePunycode = function ( ) {
175
-
164
+ const handlePunycode = ( ) => {
176
165
if ( ! / x n - - / . test ( domain ) ) {
177
166
return parsed ;
178
167
}
179
168
if ( parsed . domain ) {
180
- parsed . domain = Punycode . toASCII ( parsed . domain ) ;
169
+ parsed . domain = punycode . toASCII ( parsed . domain ) ;
181
170
}
182
171
if ( parsed . subdomain ) {
183
- parsed . subdomain = Punycode . toASCII ( parsed . subdomain ) ;
172
+ parsed . subdomain = punycode . toASCII ( parsed . subdomain ) ;
184
173
}
185
174
return parsed ;
186
175
} ;
187
176
188
- var rule = internals . findRule ( domain ) ;
177
+ const rule = findRule ( domain ) ;
189
178
190
179
// Unlisted tld.
191
180
if ( ! rule ) {
@@ -205,8 +194,8 @@ export const parse = function (input) {
205
194
// At this point we know the public suffix is listed.
206
195
parsed . listed = true ;
207
196
208
- var tldParts = rule . suffix . split ( '.' ) ;
209
- var privateParts = domainParts . slice ( 0 , domainParts . length - tldParts . length ) ;
197
+ const tldParts = rule . suffix . split ( '.' ) ;
198
+ const privateParts = domainParts . slice ( 0 , domainParts . length - tldParts . length ) ;
210
199
211
200
if ( rule . exception ) {
212
201
privateParts . push ( tldParts . shift ( ) ) ;
@@ -237,25 +226,21 @@ export const parse = function (input) {
237
226
return handlePunycode ( ) ;
238
227
} ;
239
228
240
-
241
229
//
242
230
// Get domain.
243
231
//
244
- export const get = function ( domain ) {
245
-
232
+ export const get = ( domain ) => {
246
233
if ( ! domain ) {
247
234
return null ;
248
235
}
249
236
return parse ( domain ) . domain || null ;
250
237
} ;
251
238
252
-
253
239
//
254
240
// Check whether domain belongs to a known public suffix.
255
241
//
256
- export const isValid = function ( domain ) {
257
-
258
- var parsed = parse ( domain ) ;
242
+ export const isValid = ( domain ) => {
243
+ const parsed = parse ( domain ) ;
259
244
return Boolean ( parsed . domain && parsed . listed ) ;
260
245
} ;
261
246
0 commit comments