Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Modernize node module #47

Merged
merged 11 commits into from
Jun 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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()));
});