Skip to content

Commit

Permalink
Require Node.js 16 (#47)
Browse files Browse the repository at this point in the history
  • Loading branch information
silverwind committed Jun 10, 2023
1 parent b50e281 commit bb1c153
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 52 deletions.
13 changes: 9 additions & 4 deletions .github/workflows/main.yml
Expand Up @@ -4,16 +4,21 @@ on:
- pull_request
jobs:
test:
name: Node.js ${{ matrix.node-version }}
runs-on: ubuntu-latest
runs-on: ${{ matrix.os }}
strategy:
fail-fast: false
matrix:
os:
- ubuntu-20.04
- ubuntu-22.04
- macOS-latest
- windows-latest
node-version:
- 18
- 16
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v2
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
with:
node-version: ${{ matrix.node-version }}
- run: npm install
Expand Down
6 changes: 3 additions & 3 deletions browser.js
@@ -1,6 +1,6 @@
/* eslint-env browser */
import pEvent from 'p-event';
import isIp from 'is-ip';
import {isIPv4, isIPv6} from 'is-ip';

const getIp = async ({isSecondTry = false} = {}) => {
try {
Expand Down Expand Up @@ -51,14 +51,14 @@ const getIp = async ({isSecondTry = false} = {}) => {

export async function internalIpV6() {
const result = await getIp();
if (isIp.v6(result)) {
if (isIPv6(result)) {
return result;
}
}

export async function internalIpV4() {
const result = await getIp();
if (isIp.v4(result)) {
if (isIPv4(result)) {
return result;
}
}
Expand Down
43 changes: 16 additions & 27 deletions index.js
@@ -1,49 +1,38 @@
import {networkInterfaces} from 'node:os';
import defaultGateway from 'default-gateway';
import ip from 'ipaddr.js';
import {gateway4async, gateway4sync, gateway6async, gateway6sync} from 'default-gateway';
import {contains, normalize} from 'cidr-tools';

function findIp(gateway) {
const gatewayIp = ip.parse(gateway);

// Look for the matching interface in all local interfaces.
function findIp({gateway}) {
// Look for the matching interface in all local interfaces
for (const addresses of Object.values(networkInterfaces())) {
for (const {cidr} of addresses) {
const net = ip.parseCIDR(cidr);

// eslint-disable-next-line unicorn/prefer-regexp-test
if (net[0] && net[0].kind() === gatewayIp.kind() && gatewayIp.match(net)) {
return net[0].toString();
if (contains(cidr, gateway)) {
return normalize(cidr).split('/')[0];
}
}
}
}

async function async(family) {
export async function internalIpV6() {
try {
const {gateway} = await defaultGateway[family]();
return findIp(gateway);
return findIp((await gateway6async()));
} catch {}
}

function sync(family) {
export async function internalIpV4() {
try {
const {gateway} = defaultGateway[family].sync();
return findIp(gateway);
return findIp((await gateway4async()));
} catch {}
}

export async function internalIpV6() {
return async('v6');
}

export async function internalIpV4() {
return async('v4');
}

export function internalIpV6Sync() {
return sync('v6');
try {
return findIp(gateway6sync());
} catch {}
}

export function internalIpV4Sync() {
return sync('v4');
try {
return findIp(gateway4sync());
} catch {}
}
9 changes: 5 additions & 4 deletions package.json
Expand Up @@ -11,12 +11,13 @@
"url": "https://sindresorhus.com"
},
"type": "module",
"sideEffects": false,
"exports": {
"node": "./index.js",
"default": "./browser.js"
},
"engines": {
"node": "^12.20.0 || ^14.13.1 || >=16.0.0"
"node": ">=16"
},
"scripts": {
"test": "xo && ava && tsd"
Expand All @@ -40,9 +41,9 @@
"gateway"
],
"dependencies": {
"default-gateway": "^6.0.3",
"ipaddr.js": "^2.0.1",
"is-ip": "^3.1.0",
"cidr-tools": "^6.4.1",
"default-gateway": "^7.2.2",
"is-ip": "^5.0.0",
"p-event": "^4.2.0"
},
"devDependencies": {
Expand Down
26 changes: 12 additions & 14 deletions test.js
@@ -1,34 +1,32 @@
import {isIPv4, isIPv6} from 'node:net';
import process from 'node:process';
import {env} from 'node:process';
import {platform} from 'node:os';
import test from 'ava';
import {internalIpV6, internalIpV4, internalIpV6Sync, internalIpV4Sync} from './index.js';

const isCI = Boolean(process.env.CI);
// Only Darwin has IPv6 on GitHub Actions
const canTestV6 = env.CI ? platform() === 'darwin' : true;

test('IPv6 - async', async t => {
const ip = await internalIpV6();
if (isCI) {
t.is(ip, undefined);
if (canTestV6) {
t.true(isIPv6(await internalIpV6()));
} else {
t.true(isIPv6(ip));
t.is(await internalIpV6(), undefined);
}
});

test('IPv4 - async', async t => {
const ip = await internalIpV4();
t.true(isIPv4(ip));
t.true(isIPv4(await internalIpV4()));
});

test('IPv6 - sync', t => {
const ip = internalIpV6Sync();
if (isCI) {
t.is(ip, undefined);
if (canTestV6) {
t.true(isIPv6(internalIpV6Sync()));
} else {
t.true(isIPv6(ip));
t.is(internalIpV6Sync(), undefined);
}
});

test('IPv4 - sync', t => {
const ip = internalIpV4Sync();
t.true(isIPv4(ip));
t.true(isIPv4(internalIpV4Sync()));
});

0 comments on commit bb1c153

Please sign in to comment.