external-ip
Advanced tools
Comparing version 1.0.0 to 1.0.1
@@ -6,2 +6,3 @@ #!/usr/bin/env node | ||
const extIP = require('./extIP'); | ||
const version = require('../package.json').version; | ||
@@ -14,2 +15,3 @@ const collect = (service, services) => { | ||
program | ||
.version(version) | ||
.option('-R, --replace', 'replace internal services instead of extending them.') | ||
@@ -16,0 +18,0 @@ .option('-s, --services <url>', 'service url, see examples, required if using -R', collect, []) |
@@ -7,2 +7,3 @@ 'use strict'; | ||
// validate the external configuration | ||
const isValid = utils.validateConfig(externalConfig); | ||
@@ -15,5 +16,9 @@ | ||
// merge the external configuration with the default | ||
const config = utils.mergeConfig(externalConfig, defaultConfig); | ||
// create a request instance for each service in the configuration | ||
const requests = config.services.map((url) => utils.requestFactory(config, url)); | ||
// sequential and parallel mode handlers | ||
const getIP = { | ||
@@ -29,4 +34,5 @@ sequential: (cb) => { | ||
current += 1; | ||
// when every single service has failed tell the bad news | ||
if (current === requests.length) { | ||
return cb(errors, null); | ||
return cb(utils.concatErrors(errors), null); | ||
} | ||
@@ -55,3 +61,3 @@ return loop(); | ||
const onResponse = function (err, ip) { | ||
const onResponse = (err, ip) => { | ||
@@ -70,5 +76,3 @@ if (done) { | ||
if (errors.length === requests.length) { | ||
done = true; | ||
abort(ongoingRequests); //async | ||
return cb(errors, null); | ||
return cb(utils.concatErrors(errors), null); | ||
} | ||
@@ -82,3 +86,4 @@ }; | ||
// return the sequential or the parallel handler according to the configuration | ||
return getIP[config.getIP]; | ||
}; |
@@ -65,7 +65,7 @@ 'use strict'; | ||
if (error) { | ||
return cb(new Error(JSON.stringify({ code: error.code, url })), null); | ||
return cb(new Error(`${error.code} from ${url}`), null); | ||
} | ||
// Parse and validate the body | ||
body = body.toString().replace('\n', ''); | ||
return cb.apply(null, isIP(body) ? [null, body] : [new Error(JSON.stringify({ code: 'invalid IP', url })), null]); | ||
return cb.apply(null, isIP(body) ? [null, body] : [new Error(`Got invalid IP from ${url}`), null]); | ||
}); | ||
@@ -75,2 +75,12 @@ }; | ||
const concatErrors = (errors) => { | ||
if (errors.length === 1) { | ||
return errors[0]; | ||
} | ||
let msg = errors.reduce((acc, current) => { | ||
return acc + ` ${current.message} \n`; | ||
}, 'Multiple errors: \n'); | ||
return new Error(msg); | ||
}; | ||
module.exports = { | ||
@@ -81,2 +91,3 @@ isIP, | ||
requestFactory, | ||
concatErrors | ||
}; |
{ | ||
"name": "external-ip", | ||
"version": "1.0.0", | ||
"version": "1.0.1", | ||
"description": "A node.js library to get your external ip from multiple services", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -6,7 +6,7 @@ 'use strict'; | ||
// Integration test | ||
var extIP = require('../index'); | ||
var utils = require('../lib/utils'); | ||
var expect = require('chai').expect; | ||
const extIP = require('../index'); | ||
const utils = require('../lib/utils'); | ||
const expect = require('chai').expect; | ||
var timeout = 3000; | ||
const timeout = 3000; | ||
@@ -17,3 +17,3 @@ | ||
this.timeout(timeout); | ||
var getIP = extIP(); | ||
let getIP = extIP(); | ||
getIP(function (err, ip) { | ||
@@ -29,3 +29,3 @@ expect(err).to.equal(null); | ||
var getIP = extIP({ | ||
let getIP = extIP({ | ||
replace: true, // true: replace the default services list, false: extend it, default: false | ||
@@ -32,0 +32,0 @@ services: ['http://ident.me/', 'http://icanhazip.com/'], |
@@ -5,4 +5,4 @@ 'use strict'; | ||
var utils = require('../lib/utils'); | ||
var expect = require('chai').expect; | ||
const utils = require('../lib/utils'); | ||
const expect = require('chai').expect; | ||
@@ -27,3 +27,3 @@ | ||
it('should allow valid config', function () { | ||
var config = { | ||
const config = { | ||
a: { | ||
@@ -55,3 +55,3 @@ replace: false, | ||
var config = { | ||
const config = { | ||
a: { | ||
@@ -79,3 +79,3 @@ replace: 'batman', | ||
var config = { | ||
const config = { | ||
default: { | ||
@@ -99,3 +99,3 @@ replace: false, | ||
var merged = utils.mergeConfig(config.a, config.default); | ||
let merged = utils.mergeConfig(config.a, config.default); | ||
expect(merged).to.have.property('timeout', 500); | ||
@@ -102,0 +102,0 @@ expect(merged).to.have.property('services').with.lengthOf(2); |
@@ -1,4 +0,4 @@ | ||
//'http://ip.appspot.com/', | ||
//'http://ifconfig.co/x-real-ip', | ||
//'http://ifconfig.io/ip', | ||
'http://ip.appspot.com/', | ||
'http://ifconfig.co/x-real-ip', | ||
'http://ifconfig.io/ip', | ||
'http://whatismyip.akamai.com/', | ||
@@ -5,0 +5,0 @@ |
19562
373