summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorsilverwind <me@silverwind.io>2017-09-30 09:48:48 +0200
committersilverwind <me@silverwind.io>2017-09-30 09:58:20 +0200
commitfadc318e1cd89d11aef61b6e9d03da8164a5b3de (patch)
treefea7412adf286a66cffbc2f8b36a9132e1b5a825
parent586ae808bf1942cd5e85a34f41f5a98f944979ed (diff)
downloadinternal-ip-fadc318e1cd89d11aef61b6e9d03da8164a5b3de.tar
internal-ip-fadc318e1cd89d11aef61b6e9d03da8164a5b3de.tar.gz
fix tests, reorder readme examples
-rw-r--r--index.js4
-rw-r--r--readme.md14
-rw-r--r--test.js50
3 files changed, 44 insertions, 24 deletions
diff --git a/index.js b/index.js
index e320a10..7c6d4ce 100644
--- a/index.js
+++ b/index.js
@@ -27,14 +27,14 @@ function findIp(gateway) {
function promise(family) {
return defaultGateway[family]().then(result => {
- return findIp(result.gateway);
+ return findIp(result.gateway) || null;
}).catch(() => null);
}
function sync(family) {
try {
const result = defaultGateway[family].sync();
- return findIp(result.gateway);
+ return findIp(result.gateway) || null;
} catch (err) {
return null;
}
diff --git a/readme.md b/readme.md
index bb5d900..c6561f3 100644
--- a/readme.md
+++ b/readme.md
@@ -15,21 +15,21 @@ $ npm install internal-ip
```js
const internalIp = require('internal-ip');
-internalIp.v4().then(ip => {
- console.log(ip);
- //=> '10.0.0.79'
-});
-
internalIp.v6().then(ip => {
console.log(ip);
//=> 'fe80::1'
});
-console.log(internalIp.v4().sync())
-//=> '10.0.0.79'
+internalIp.v4().then(ip => {
+ console.log(ip);
+ //=> '10.0.0.79'
+});
console.log(internalIp.v6().sync())
//=> 'fe80::1'
+
+console.log(internalIp.v4().sync())
+//=> '10.0.0.79'
```
The module returns the address of the internet-facing interface, as determined from the default gateway. When the adress cannot be determined for any reason, `null` will be returned.
diff --git a/test.js b/test.js
index c762d76..371757c 100644
--- a/test.js
+++ b/test.js
@@ -2,22 +2,42 @@ import {isIPv4, isIPv6} from 'net';
import test from 'ava';
import m from '.';
-// Skip tests on Travis as their VMs don't have IPs on their interfaces
+// Travis VMs don't have IPs on their interfaces
// https://docs.travis-ci.com/user/ci-environment/#Networking
-if (!process.env.CI) {
- test('IPv6', async t => {
- t.true(isIPv6(await m.v6()));
- });
+const ci = Boolean(process.env.CI);
- test('IPv4', async t => {
- t.true(isIPv4(await m.v4()));
- });
+test('IPv6', async t => {
+ const ip = await m.v6();
+ if (ci) {
+ t.is(ip, null);
+ } else {
+ t.true(isIPv6(ip));
+ }
+});
- test('synchronous IPv6', t => {
- t.true(isIPv6(m.v6.sync()));
- });
+test('IPv4', async t => {
+ const ip = await m.v4();
+ if (ci) {
+ t.is(ip, null);
+ } else {
+ t.true(isIPv4(ip));
+ }
+});
- test('synchronous IPv4', t => {
- t.true(isIPv4(m.v4.sync()));
- });
-}
+test('synchronous IPv6', t => {
+ const ip = m.v6.sync();
+ if (ci) {
+ t.is(ip, null);
+ } else {
+ t.true(isIPv6(ip));
+ }
+});
+
+test('synchronous IPv4', t => {
+ const ip = m.v4.sync();
+ if (ci) {
+ t.is(ip, null);
+ } else {
+ t.true(isIPv4(ip));
+ }
+});