Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- 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
- Loading branch information
1 parent
b50e281
commit 83e3b71
Showing
5 changed files
with
44 additions
and
58 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 {} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} 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())); | ||
}); |