From 83e3b71bdb879318e1646cd630968d5de6c945a0 Mon Sep 17 00:00:00 2001 From: silverwind Date: Sat, 10 Jun 2023 13:31:41 +0200 Subject: [PATCH] Modernize node module - Update `default-gateway` to latest - Update `is-ip` to latest - Switch from `ipaddr.js` to `cidr-tools` - Expand CI matrix to all node versions and windows, mac --- .github/workflows/main.yml | 15 +++++++------ browser.js | 6 +++--- index.js | 43 ++++++++++++++------------------------ package.json | 8 +++---- test.js | 30 +++++++++++++------------- 5 files changed, 44 insertions(+), 58 deletions(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 441975c..15436bb 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -1,20 +1,19 @@ name: CI -on: - - push - - pull_request +on: [push, pull_request] + jobs: test: - name: Node.js ${{ matrix.node-version }} - runs-on: ubuntu-latest + runs-on: ${{ matrix.os }} strategy: fail-fast: false matrix: - node-version: - - 16 + os: [ubuntu-20.04, ubuntu-22.04, macOS-latest, windows-latest] + node: [16, 18, 20] + steps: - uses: actions/checkout@v2 - uses: actions/setup-node@v2 with: - node-version: ${{ matrix.node-version }} + node-version: ${{ matrix.node }} - run: npm install - run: npm test diff --git a/browser.js b/browser.js index cb96074..142a993 100644 --- a/browser.js +++ b/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 { @@ -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; } } diff --git a/index.js b/index.js index b3f79a6..7ae59d2 100644 --- a/index.js +++ b/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 {} } diff --git a/package.json b/package.json index 6e834c3..58b8fdd 100644 --- a/package.json +++ b/package.json @@ -16,7 +16,7 @@ "default": "./browser.js" }, "engines": { - "node": "^12.20.0 || ^14.13.1 || >=16.0.0" + "node": ">=16.0.0" }, "scripts": { "test": "xo && ava && tsd" @@ -40,9 +40,9 @@ "gateway" ], "dependencies": { - "default-gateway": "^6.0.3", - "ipaddr.js": "^2.0.1", - "is-ip": "^3.1.0", + "cidr-tools": "^6.4.0", + "default-gateway": "^7.2.0", + "is-ip": "^5.0.0", "p-event": "^4.2.0" }, "devDependencies": { diff --git a/test.js b/test.js index d48cd6d..6833dc8 100644 --- a/test.js +++ b/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); - } else { - t.true(isIPv6(ip)); + if (!canTestV6) { + return; } + + t.true(isIPv6(await internalIpV6())); }); 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); - } else { - t.true(isIPv6(ip)); + if (!canTestV6) { + return; } + + t.true(isIPv6(internalIpV6Sync())); }); test('IPv4 - sync', t => { - const ip = internalIpV4Sync(); - t.true(isIPv4(ip)); + t.true(isIPv4(internalIpV4Sync())); });