@@ -5,14 +5,14 @@ import validUrl from 'valid-url';
5
5
import { Label } from 'types' ;
6
6
7
7
export function validateHttpTarget ( target : string ) {
8
- let message = 'Target must be a valid web URL' ;
8
+ const message = 'Target must be a valid web URL' ;
9
9
10
10
try {
11
11
const httpEncoded = encodeURI ( target ) ;
12
12
const isValidUrl = Boolean ( validUrl . isWebUri ( httpEncoded ) ) ;
13
13
14
14
if ( ! isValidUrl ) {
15
- throw new Error ( message ) ;
15
+ return message ;
16
16
}
17
17
} catch {
18
18
return message ;
@@ -22,8 +22,7 @@ export function validateHttpTarget(target: string) {
22
22
const parsedUrl = new URL ( target ) ;
23
23
24
24
if ( ! parsedUrl . protocol ) {
25
- message = 'Target must have a valid protocol' ;
26
- throw new Error ( message ) ;
25
+ return 'Target must have a valid protocol' ;
27
26
}
28
27
29
28
// isWebUri will allow some invalid hostnames, so we need addional validation
@@ -33,7 +32,7 @@ export function validateHttpTarget(target: string) {
33
32
}
34
33
35
34
const hostname = parsedUrl . hostname ;
36
- if ( validateHostname ( hostname ) ) {
35
+ if ( validateHostAddress ( hostname ) ) {
37
36
return 'Target must have a valid hostname' ;
38
37
}
39
38
} catch {
@@ -44,7 +43,7 @@ export function validateHttpTarget(target: string) {
44
43
}
45
44
46
45
function isIpV6FromUrl ( target : string ) {
47
- let isIpV6 = true ;
46
+ let isIpV6 ;
48
47
try {
49
48
const address = Address6 . fromURL ( target ) ;
50
49
isIpV6 = Boolean ( address . address ) ;
@@ -165,10 +164,8 @@ export function validateDomain(target: string): string | undefined {
165
164
166
165
const filteredElements = rawElements . filter ( ( element , index ) => {
167
166
const isLast = index === rawElements . length - 1 ;
168
- if ( isLast && element === '' ) {
169
- return false ;
170
- }
171
- return true ;
167
+
168
+ return ! ( isLast && element === '' ) ;
172
169
} ) ;
173
170
174
171
const errors = filteredElements
@@ -196,7 +193,7 @@ function isCharacterLetter(character: string): boolean {
196
193
}
197
194
198
195
function isValidDomainCharacter ( character : string ) : boolean {
199
- const regex = new RegExp ( / [ - A - Z a - z 0 - 9 \ .] / ) ;
196
+ const regex = new RegExp ( / [ - A - Z a - z 0 - 9 . ] / ) ;
200
197
return Boolean ( ! character . match ( regex ) ?. length ) ;
201
198
}
202
199
@@ -235,7 +232,7 @@ function validateDomainElement(element: string, isLast: boolean): string | undef
235
232
return undefined ;
236
233
}
237
234
238
- export function validateHostname ( target : string ) : string | undefined {
235
+ export function validateHostAddress ( target : string ) : string | undefined {
239
236
const ipv4 = isIpV4 ( target ) ;
240
237
const ipv6 = isIpV6 ( target ) ;
241
238
const pc = punycode . toASCII ( target ) ;
@@ -245,14 +242,14 @@ export function validateHostname(target: string): string | undefined {
245
242
'i'
246
243
) ;
247
244
if ( ! pc . match ( re ) && ! ipv4 && ! ipv6 ) {
248
- return 'Target must be a valid hostname ' ;
245
+ return 'Target must be a valid host address ' ;
249
246
}
250
247
251
248
return undefined ;
252
249
}
253
250
254
251
export function validateHostPort ( target : string ) : string | undefined {
255
- const re = new RegExp ( / ^ (?: \[ ( [ 0 - 9 a - f : . ] + ) \ ]| ( [ ^ : ] + ) ) : ( \d + ) $ / , 'i' ) ;
252
+ const re = new RegExp ( / ^ (?: \[ ( [ 0 - 9 a - f : . ] + ) ] | ( [ ^ : ] + ) ) : ( \d + ) $ / , 'i' ) ;
256
253
const match = target . match ( re ) ;
257
254
258
255
if ( match === null ) {
@@ -273,5 +270,5 @@ export function validateHostPort(target: string): string | undefined {
273
270
return 'Port must be greater than 0' ;
274
271
}
275
272
276
- return validateHostname ( host ) ;
273
+ return validateHostAddress ( host ) ;
277
274
}
0 commit comments