Skip to content

Commit

Permalink
Return undefined instead of null when gateway cannot be determined
Browse files Browse the repository at this point in the history
  • Loading branch information
sindresorhus committed Oct 16, 2019
1 parent 15425f1 commit 6467da3
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 50 deletions.
76 changes: 44 additions & 32 deletions index.d.ts
@@ -1,45 +1,57 @@
interface v6 {
/**
* @returns The IPv6 address of the internet-facing interface, as determined from the default gateway. When the address cannot be determined for any reason, `null` will be returned.
*
* @example
*
* console.log(await internalIp.v6());
* //=> 'fe80::1'
*/
(): Promise<string>;
@returns The IPv6 address of the internet-facing interface, as determined from the default gateway. When the address cannot be determined for any reason, `undefined` will be returned.
@example
```
import internalIp = require('internal-ip');
console.log(await internalIp.v6());
//=> 'fe80::1'
```
*/
(): Promise<string | undefined>;

/**
* @returns The IPv6 address of the internet-facing interface, as determined from the default gateway. When the address cannot be determined for any reason, `null` will be returned.
*
* @example
*
* console.log(internalIp.v6.sync());
* //=> 'fe80::1'
*/
sync(): string;
@returns The IPv6 address of the internet-facing interface, as determined from the default gateway. When the address cannot be determined for any reason, `undefined` will be returned.
@example
```
import internalIp = require('internal-ip');
console.log(internalIp.v6.sync());
//=> 'fe80::1'
```
*/
sync(): string | undefined;
}

interface v4 {
/**
* @returns The IPv4 address of the internet-facing interface, as determined from the default gateway. When the address cannot be determined for any reason, `null` will be returned.
*
* @example
*
* console.log(await internalIp.v4())
* //=> '10.0.0.79'
*/
(): Promise<string>;
@returns The IPv4 address of the internet-facing interface, as determined from the default gateway. When the address cannot be determined for any reason, `undefined` will be returned.
@example
```
import internalIp = require('internal-ip');
console.log(await internalIp.v4())
//=> '10.0.0.79'
```
*/
(): Promise<string | undefined>;

/**
* @returns The IPv4 address of the internet-facing interface, as determined from the default gateway. When the address cannot be determined for any reason, `null` will be returned.
*
* @example
*
* console.log(internalIp.v4.sync())
* //=> '10.0.0.79'
*/
sync(): string;
@returns The IPv4 address of the internet-facing interface, as determined from the default gateway. When the address cannot be determined for any reason, `undefined` will be returned.
@example
```
import internalIp = require('internal-ip');
console.log(internalIp.v4.sync())
//=> '10.0.0.79'
```
*/
sync(): string | undefined;
}

declare const internalIp: {
Expand Down
12 changes: 4 additions & 8 deletions index.js
Expand Up @@ -28,19 +28,15 @@ function findIp(gateway) {
async function promise(family) {
try {
const result = await defaultGateway[family]();
return findIp(result.gateway) || null;
} catch (_) {
return null;
}
return findIp(result.gateway);
} catch (_) {}
}

function sync(family) {
try {
const result = defaultGateway[family].sync();
return findIp(result.gateway) || null;
} catch (_) {
return null;
}
return findIp(result.gateway);
} catch (_) {}
}

const internalIp = {};
Expand Down
8 changes: 4 additions & 4 deletions index.test-d.ts
@@ -1,8 +1,8 @@
import {expectType} from 'tsd';
import internalIp = require('.');

expectType<string>(await internalIp.v4());
expectType<string>(await internalIp.v6());
expectType<string | undefined>(await internalIp.v4());
expectType<string | undefined>(await internalIp.v6());

expectType<string>(internalIp.v4.sync());
expectType<string>(internalIp.v6.sync());
expectType<string | undefined>(internalIp.v4.sync());
expectType<string | undefined>(internalIp.v6.sync());
2 changes: 1 addition & 1 deletion readme.md
Expand Up @@ -30,7 +30,7 @@ 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 address cannot be determined for any reason, `null` will be returned.
The module returns the address of the internet-facing interface, as determined from the default gateway. When the address cannot be determined for any reason, `undefined` will be returned.

The module relies on operating systems tools. On Linux and Android, the `ip` command must be available, which depending on distribution might not be installed by default. It is usually provided by the `iproute2` package.

Expand Down
10 changes: 5 additions & 5 deletions test.js
Expand Up @@ -2,14 +2,14 @@ import {isIPv4, isIPv6} from 'net';
import test from 'ava';
import internalIp from '.';

// Travis 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
const isCI = Boolean(process.env.CI);

test('IPv6', async t => {
const ip = await internalIp.v6();
if (isCI) {
t.is(ip, null);
t.is(ip, undefined);
} else {
t.true(isIPv6(ip));
}
Expand All @@ -18,7 +18,7 @@ test('IPv6', async t => {
test('IPv4', async t => {
const ip = await internalIp.v4();
if (isCI) {
t.is(ip, null);
t.is(ip, undefined);
} else {
t.true(isIPv4(ip));
}
Expand All @@ -27,7 +27,7 @@ test('IPv4', async t => {
test('synchronous IPv6', t => {
const ip = internalIp.v6.sync();
if (isCI) {
t.is(ip, null);
t.is(ip, undefined);
} else {
t.true(isIPv6(ip));
}
Expand All @@ -36,7 +36,7 @@ test('synchronous IPv6', t => {
test('synchronous IPv4', t => {
const ip = internalIp.v4.sync();
if (isCI) {
t.is(ip, null);
t.is(ip, undefined);
} else {
t.true(isIPv4(ip));
}
Expand Down

0 comments on commit 6467da3

Please sign in to comment.