Skip to content

Commit

Permalink
Require Node.js 12.20 and move to ESM
Browse files Browse the repository at this point in the history
  • Loading branch information
sindresorhus committed Oct 16, 2021
1 parent 78ca387 commit 14a9c14
Show file tree
Hide file tree
Showing 9 changed files with 172 additions and 170 deletions.
6 changes: 2 additions & 4 deletions .github/workflows/main.yml
Expand Up @@ -10,12 +10,10 @@ jobs:
fail-fast: false
matrix:
node-version:
- 14
- 12
- 10
- 16
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v1
- uses: actions/setup-node@v2
with:
node-version: ${{ matrix.node-version }}
- run: npm install
Expand Down
1 change: 1 addition & 0 deletions browser.d.ts
@@ -0,0 +1 @@
export * from './index.js';
74 changes: 39 additions & 35 deletions browser.js
@@ -1,5 +1,4 @@
/* eslint-env browser */
'use strict';
import pEvent from 'p-event';
import isIp from 'is-ip';

Expand All @@ -11,58 +10,63 @@ const getIp = async ({isSecondTry = false} = {}) => {
peerConnection.createOffer(peerConnection.setLocalDescription.bind(peerConnection), () => {});

const {candidate} = await pEvent(peerConnection, 'icecandidate', {
timeout: 10000
timeout: 10_000,
});

peerConnection.close();

if (candidate && candidate.candidate) {
const result = candidate.candidate.split(' ')[4];
if (result.endsWith('.local')) {
if (isSecondTry) {
return;
}

const inputDevices = await navigator.mediaDevices.enumerateDevices();
const inputDeviceTypes = new Set(inputDevices.map(({kind}) => kind));
if (!(candidate && candidate.candidate)) {
return;
}

const constraints = {};
const result = candidate.candidate.split(' ')[4];
if (!result.endsWith('.local')) {
return result;
}

if (inputDeviceTypes.has('audioinput')) {
constraints.audio = true;
} else if (inputDeviceTypes.has('videoinput')) {
constraints.video = true;
} else {
return;
}
if (isSecondTry) {
return;
}

const mediaStream = await navigator.mediaDevices.getUserMedia(constraints);
for (const track of mediaStream.getTracks()) {
track.stop();
}
const inputDevices = await navigator.mediaDevices.enumerateDevices();
const inputDeviceTypes = new Set(inputDevices.map(({kind}) => kind));

return await getIp({isSecondTry: true});
}
const constraints = {};
if (inputDeviceTypes.has('audioinput')) {
constraints.audio = true;
} else if (inputDeviceTypes.has('videoinput')) {
constraints.video = true;
} else {
return;
}

return result;
const mediaStream = await navigator.mediaDevices.getUserMedia(constraints);
for (const track of mediaStream.getTracks()) {
track.stop();
}

return await getIp({isSecondTry: true});
} catch {}
};

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

v4.sync = () => undefined;
}

export const v6 = async () => {
export async function internalIpV4() {
const result = await getIp();
if (isIp.v6(result)) {
if (isIp.v4(result)) {
return result;
}
};
}

export function internalIpV6Sync() {
return undefined;
}

v6.sync = () => undefined;
export function internalIpV4Sync() {
return undefined;
}
113 changes: 51 additions & 62 deletions index.d.ts
@@ -1,62 +1,51 @@
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, `undefined` will be returned.
@example
```
import internalIp = require('internal-ip');
console.log(internalIp.v6.sync());
//=> 'fe80::1'
```
*/
sync: () => string | undefined;

/**
@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<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, `undefined` will be returned.
@example
```
import internalIp = require('internal-ip');
console.log(internalIp.v4.sync())
//=> '10.0.0.79'
```
*/
sync: () => string | undefined;

/**
@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<string | undefined>;
}

declare const internalIp: {
v6: v6;
v4: v4;
};

export = internalIp;
/**
@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 {internalIpV6} from 'internal-ip';
console.log(await internalIpV6());
//=> 'fe80::1'
```
*/
export function internalIpV6(): Promise<string | undefined>;

/**
@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 {internalIpV4} from 'internal-ip';
console.log(await internalIpV4());
//=> '10.0.0.79'
```
*/
export function internalIpV4(): Promise<string | undefined>;

/**
@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 {internalIpV6Sync} from 'internal-ip';
console.log(internalIpV6Sync());
//=> 'fe80::1'
```
*/
export function internalIpV6Sync(): string | undefined;

/**
@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 {internalIpV4Sync} from 'internal-ip';
console.log(internalIpV4Sync());
//=> '10.0.0.79'
```
*/
export function internalIpV4Sync(): string | undefined;
34 changes: 21 additions & 13 deletions index.js
@@ -1,24 +1,24 @@
'use strict';
const defaultGateway = require('default-gateway');
const {networkInterfaces} = require('os');
const {parse, parseCIDR} = require('ipaddr.js');
import {networkInterfaces} from 'node:os';
import defaultGateway from 'default-gateway';
import ip from 'ipaddr.js';

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

// Look for the matching interface in all local interfaces.
for (const addresses of Object.values(networkInterfaces())) {
for (const {cidr} of addresses) {
const net = parseCIDR(cidr);
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();
}
}
}
}

async function promise(family) {
async function async(family) {
try {
const {gateway} = await defaultGateway[family]();
return findIp(gateway);
Expand All @@ -32,10 +32,18 @@ function sync(family) {
} catch {}
}

const internalIp = {};
internalIp.v6 = () => promise('v6');
internalIp.v4 = () => promise('v4');
internalIp.v6.sync = () => sync('v6');
internalIp.v4.sync = () => sync('v4');
export async function internalIpV6() {
return async('v6');
}

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

module.exports = internalIp;
export function internalIpV6Sync() {
return sync('v6');
}

export function internalIpV4Sync() {
return sync('v4');
}
10 changes: 5 additions & 5 deletions index.test-d.ts
@@ -1,8 +1,8 @@
import {expectType} from 'tsd';
import internalIp = require('.');
import {internalIpV4, internalIpV6, internalIpV4Sync, internalIpV6Sync} from './index.js';

expectType<string | undefined>(await internalIp.v4());
expectType<string | undefined>(await internalIp.v6());
expectType<string | undefined>(await internalIpV4());
expectType<string | undefined>(await internalIpV6());

expectType<string | undefined>(internalIp.v4.sync());
expectType<string | undefined>(internalIp.v6.sync());
expectType<string | undefined>(internalIpV4Sync());
expectType<string | undefined>(internalIpV6Sync());
27 changes: 14 additions & 13 deletions package.json
Expand Up @@ -10,21 +10,23 @@
"email": "sindresorhus@gmail.com",
"url": "https://sindresorhus.com"
},
"type": "module",
"exports": {
"node": "./index.js",
"default": "./browser.js"
},
"engines": {
"node": ">=10"
"node": "^12.20.0 || ^14.13.1 || >=16.0.0"
},
"scripts": {
"test": "xo && ava && tsd"
},
"files": [
"index.js",
"index.d.ts",
"browser.js"
"browser.js",
"browser.d.ts"
],
"exports": {
"browser": "./browser.js",
"default": "./index.js"
},
"keywords": [
"ip",
"ipv6",
Expand All @@ -38,15 +40,14 @@
"gateway"
],
"dependencies": {
"default-gateway": "^6.0.0",
"ipaddr.js": "^1.9.1",
"default-gateway": "^6.0.3",
"ipaddr.js": "^2.0.1",
"is-ip": "^3.1.0",
"p-event": "^4.2.0"
},
"devDependencies": {
"ava": "^2.4.0",
"tsd": "^0.13.1",
"xo": "^0.32.1"
},
"browser": "browser.js"
"ava": "^3.15.0",
"tsd": "^0.18.0",
"xo": "^0.45.0"
}
}

0 comments on commit 14a9c14

Please sign in to comment.