-
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.
- Loading branch information
Showing
7 changed files
with
324 additions
and
51 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,76 @@ | ||
'use strict'; | ||
|
||
|
||
var isIp = require('is-ip'); | ||
|
||
|
||
/** | ||
* 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` could be a valid ipv6 address (contains only characters | ||
* in the range: [0-9a-f:]). 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 isProbablyIpv6(hostname) { | ||
for (var i = 0; i < hostname.length; i += 1) { | ||
var code = hostname.charCodeAt(i); | ||
if (!( | ||
code === 58 || // ':' | ||
(code >= 48 && code <= 57) || // 0-9 | ||
(code >= 97 && code <= 102) // a-f | ||
)) { | ||
return false; | ||
} | ||
} | ||
|
||
return true; | ||
} | ||
|
||
|
||
/** | ||
* Check if `hostname` is a valid ip addr (either ipv6 or ipv4). | ||
* | ||
* @param {string} hostname | ||
* @return {boolean} | ||
*/ | ||
module.exports = function (hostname) { | ||
if (typeof hostname !== 'string') { | ||
return false; | ||
} | ||
|
||
if (hostname.length === 0) { | ||
return false; | ||
} | ||
|
||
// We always perform a very fast verification to check if the `hostname` has a | ||
// chance of being an ip. If so, we perform a more expensive, exhaustive check | ||
// based on regexps. | ||
return ( | ||
(isProbablyIpv6(hostname) && isIp.v6(hostname)) || | ||
(isProbablyIpv4(hostname) && isIp.v4(hostname)) | ||
); | ||
}; |
Oops, something went wrong.