-
Notifications
You must be signed in to change notification settings - Fork 55
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Switch to whatwg-url for url parsing + add ip validation
- Loading branch information
Showing
7 changed files
with
266 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
'use strict'; | ||
|
||
|
||
var isIpv4 = require('is-ip').v4; | ||
|
||
|
||
/** | ||
* Check if `hostname` could be a valid ipv4 address (contains only characters | ||
* in the range: [0-9.]). If this is the case, we will perform a more exhaustive | ||
* check using 'is-ip' library (which is more expensive). | ||
* | ||
* @param {string} hostname | ||
* @return {boolean} | ||
*/ | ||
function isProbablyIpv4(hostname) { | ||
for (var i = 0; i < hostname.length; i += 1) { | ||
var code = hostname.charCodeAt(i); | ||
// 46 => '.' | ||
// 48 => '0' | ||
// 57 => '9' | ||
if (code !== 46 && (code < 48 || code > 57)) { | ||
return false; | ||
} | ||
} | ||
|
||
return true; | ||
} | ||
|
||
|
||
/** | ||
* Check if `hostname` is a valid ip addr (either ipv6 or ipv4). | ||
* | ||
* @param {string} hostname | ||
* @return {boolean} | ||
*/ | ||
module.exports = function isIp(hostname) { | ||
if (typeof hostname !== 'string') { | ||
return false; | ||
} | ||
|
||
// A valid ipv4 would be at least 7 chars long (e.g.: 1.1.1.1) | ||
// A valid ipv6 would be at least 4 chars long (e.g.: [::]) | ||
if (hostname.length < 4) { | ||
return false; | ||
} | ||
|
||
// We already know that `hostname` is a valid hostname, since it's the output | ||
// of `whatwg-url` parser. So it's enough to check if the hostname is | ||
// contained between '[' and ']' characters. | ||
if (hostname[0] === '[' && hostname[hostname.length - 1] === ']') { | ||
return true; // ipv6 | ||
} | ||
|
||
// Here we perform a first, very naive check to make sure `hostname` has a | ||
// chance of being a valid ipv4 address (contains only characters: [0-9.]). | ||
// If this is the case, then we perform a more expensive check using an | ||
// exhaustive regex. | ||
return isProbablyIpv4(hostname) && isIpv4(hostname); | ||
}; |
Oops, something went wrong.