external-ip
Advanced tools
Comparing version 1.2.1 to 1.3.0
@@ -37,2 +37,3 @@ #!/usr/bin/env node | ||
.option('-u, --userAgent <User-Agent>', `provide a User-Agent header, default: ${defaultConfig.userAgent}`, null, '') | ||
.option('-v, --verbose', 'provide additional details') | ||
.parse(process.argv); | ||
@@ -55,2 +56,6 @@ | ||
} | ||
if (cliConf.verbose) { | ||
config.verbose = cliConf.verbose; | ||
} | ||
return config; | ||
@@ -57,0 +62,0 @@ }; |
@@ -14,3 +14,4 @@ 'use strict'; | ||
timeout: 1000, | ||
userAgent: 'curl/' | ||
userAgent: 'curl/', | ||
verbose: false | ||
}; |
@@ -8,2 +8,15 @@ 'use strict'; | ||
/** | ||
* Minimal logger implementation for verbose mode | ||
* @param {Object} config | ||
* @return {Object} logger instance | ||
*/ | ||
const loggerFactory = (config) => { | ||
const noop = () => {}; | ||
return { | ||
error: config.verbose ? console.log.bind(console, '[error]: ') : noop, | ||
info: config.verbose ? console.log.bind(console, '[info]: ') : noop | ||
}; | ||
}; | ||
/** | ||
* Checks if an IP is a valid v4 or v6 | ||
@@ -67,3 +80,4 @@ * @param str | ||
getIP: externalConfig.getIP || defaultConfig.getIP, | ||
userAgent: externalConfig.userAgent || defaultConfig.userAgent | ||
userAgent: externalConfig.userAgent || defaultConfig.userAgent, | ||
verbose: externalConfig.verbose || defaultConfig.verbose | ||
}; | ||
@@ -79,3 +93,5 @@ }; | ||
const requestFactory = (config, url) => { | ||
const logger = loggerFactory(config); | ||
return (cb) => { | ||
logger.info(`requesting IP from: ${url}`); | ||
return get.concat({ | ||
@@ -89,2 +105,3 @@ url: url, | ||
if (error) { | ||
logger.error(`${url} ${error.message}`); | ||
return cb(new Error(`${error.message} from ${url}`), null); | ||
@@ -94,3 +111,8 @@ } | ||
body = body.toString().replace('\n', ''); | ||
return cb.apply(null, isIP(body) ? [null, body] : [new Error(`Got invalid IP from ${url}`), null]); | ||
if (isIP(body)) { | ||
logger.info(`got valid IP from: ${url}`); | ||
return cb(null, body); | ||
} | ||
logger.error(`Got invalid IP from ${url}`); | ||
return cb(new Error(`Got invalid IP from ${url}`), null); | ||
}); | ||
@@ -97,0 +119,0 @@ }; |
{ | ||
"name": "external-ip", | ||
"version": "1.2.1", | ||
"version": "1.3.0", | ||
"description": "A node.js library to get your external ip from multiple services", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -65,4 +65,5 @@ # external-ip | ||
* **timeout:** Timeout per request in ms, default `1000` | ||
* **getIP:** `'sequential'` Makes a request to the first url in the list, if that fails sends to the next and so on. `'parallel'` Makes requests to all the sites in the list, on the first valid response all the pending requests are canceled. default: `'sequential'` | ||
* **getIP:** `'sequential'` Makes a request to the first url in the list, if that fails sends to the next and so on. `'parallel'` Makes requests to all the sites in the list, on the first valid response all the pending requests are canceled, default: `'sequential'` | ||
* **userAgent:** `String` Set a custom `User-Agent` header, default: `curl/` | ||
* **verbose:** `Boolean` Log additional information to the console, default: `false` | ||
@@ -92,2 +93,3 @@ Returns the configured getIP instance. | ||
-u, --userAgent <User-Agent> provide a User-Agent header, default: curl/ | ||
-v, --verbose provide additional details | ||
@@ -94,0 +96,0 @@ |
@@ -6,2 +6,4 @@ more tests | ||
eliminate nasty IFs | ||
verbose mode | ||
22720
442
119