From 6467da314db91f8f879b0ad355f2e5725507dfb1 Mon Sep 17 00:00:00 2001 From: Sindre Sorhus Date: Wed, 16 Oct 2019 10:54:03 +0700 Subject: [PATCH] Return `undefined` instead of `null` when gateway cannot be determined --- index.d.ts | 76 ++++++++++++++++++++++++++++--------------------- index.js | 12 +++----- index.test-d.ts | 8 +++--- readme.md | 2 +- test.js | 10 +++---- 5 files changed, 58 insertions(+), 50 deletions(-) diff --git a/index.d.ts b/index.d.ts index 2dbaf02..70d1670 100644 --- a/index.d.ts +++ b/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; + @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; /** - * @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; + @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; /** - * @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: { diff --git a/index.js b/index.js index 8d3932f..465d67e 100644 --- a/index.js +++ b/index.js @@ -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 = {}; diff --git a/index.test-d.ts b/index.test-d.ts index e95cb80..8318eb9 100644 --- a/index.test-d.ts +++ b/index.test-d.ts @@ -1,8 +1,8 @@ import {expectType} from 'tsd'; import internalIp = require('.'); -expectType(await internalIp.v4()); -expectType(await internalIp.v6()); +expectType(await internalIp.v4()); +expectType(await internalIp.v6()); -expectType(internalIp.v4.sync()); -expectType(internalIp.v6.sync()); +expectType(internalIp.v4.sync()); +expectType(internalIp.v6.sync()); diff --git a/readme.md b/readme.md index e9b6f04..a168960 100644 --- a/readme.md +++ b/readme.md @@ -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. diff --git a/test.js b/test.js index c1822ed..6d6fc27 100644 --- a/test.js +++ b/test.js @@ -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)); } @@ -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)); } @@ -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)); } @@ -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)); }