internal-ip
Advanced tools
Comparing version 6.2.0 to 7.0.0
/* eslint-env browser */ | ||
'use strict'; | ||
import pEvent from 'p-event'; | ||
@@ -14,3 +13,3 @@ import isIp from 'is-ip'; | ||
const {candidate} = await pEvent(peerConnection, 'icecandidate', { | ||
timeout: 10000 | ||
timeout: 10_000, | ||
}); | ||
@@ -20,51 +19,56 @@ | ||
if (candidate && candidate.candidate) { | ||
const result = candidate.candidate.split(' ')[4]; | ||
if (result.endsWith('.local')) { | ||
if (isSecondTry) { | ||
return; | ||
} | ||
if (!(candidate && candidate.candidate)) { | ||
return; | ||
} | ||
const inputDevices = await navigator.mediaDevices.enumerateDevices(); | ||
const inputDeviceTypes = new Set(inputDevices.map(({kind}) => kind)); | ||
const result = candidate.candidate.split(' ')[4]; | ||
if (!result.endsWith('.local')) { | ||
return result; | ||
} | ||
const constraints = {}; | ||
if (isSecondTry) { | ||
return; | ||
} | ||
if (inputDeviceTypes.has('audioinput')) { | ||
constraints.audio = true; | ||
} else if (inputDeviceTypes.has('videoinput')) { | ||
constraints.video = true; | ||
} else { | ||
return; | ||
} | ||
const inputDevices = await navigator.mediaDevices.enumerateDevices(); | ||
const inputDeviceTypes = new Set(inputDevices.map(({kind}) => kind)); | ||
const mediaStream = await navigator.mediaDevices.getUserMedia(constraints); | ||
for (const track of mediaStream.getTracks()) { | ||
track.stop(); | ||
} | ||
const constraints = {}; | ||
if (inputDeviceTypes.has('audioinput')) { | ||
constraints.audio = true; | ||
} else if (inputDeviceTypes.has('videoinput')) { | ||
constraints.video = true; | ||
} else { | ||
return; | ||
} | ||
return await getIp({isSecondTry: true}); | ||
} | ||
const mediaStream = await navigator.mediaDevices.getUserMedia(constraints); | ||
for (const track of mediaStream.getTracks()) { | ||
track.stop(); | ||
} | ||
return result; | ||
} | ||
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; | ||
} | ||
}; | ||
} | ||
v6.sync = () => undefined; | ||
export function internalIpV6Sync() { | ||
return undefined; | ||
} | ||
export function internalIpV4Sync() { | ||
return undefined; | ||
} |
@@ -1,62 +0,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. | ||
/** | ||
@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'); | ||
@example | ||
``` | ||
import {internalIpV6} from 'internal-ip'; | ||
console.log(internalIp.v6.sync()); | ||
//=> 'fe80::1' | ||
``` | ||
*/ | ||
sync: () => string | undefined; | ||
console.log(await internalIpV6()); | ||
//=> 'fe80::1' | ||
``` | ||
*/ | ||
export function internalIpV6(): 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. | ||
/** | ||
@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'); | ||
@example | ||
``` | ||
import {internalIpV4} from 'internal-ip'; | ||
console.log(await internalIp.v6()); | ||
//=> 'fe80::1' | ||
``` | ||
*/ | ||
(): Promise<string | undefined>; | ||
} | ||
console.log(await internalIpV4()); | ||
//=> '10.0.0.79' | ||
``` | ||
*/ | ||
export function internalIpV4(): 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. | ||
/** | ||
@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'); | ||
@example | ||
``` | ||
import {internalIpV6Sync} from 'internal-ip'; | ||
console.log(internalIp.v4.sync()) | ||
//=> '10.0.0.79' | ||
``` | ||
*/ | ||
sync: () => string | undefined; | ||
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. | ||
/** | ||
@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'); | ||
@example | ||
``` | ||
import {internalIpV4Sync} from 'internal-ip'; | ||
console.log(await internalIp.v4()) | ||
//=> '10.0.0.79' | ||
``` | ||
*/ | ||
(): Promise<string | undefined>; | ||
} | ||
declare const internalIp: { | ||
v6: v6; | ||
v4: v4; | ||
}; | ||
export = internalIp; | ||
console.log(internalIpV4Sync()); | ||
//=> '10.0.0.79' | ||
``` | ||
*/ | ||
export function internalIpV4Sync(): string | undefined; |
34
index.js
@@ -1,8 +0,7 @@ | ||
'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); | ||
@@ -12,4 +11,5 @@ // Look for the matching interface in all local interfaces. | ||
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)) { | ||
@@ -22,3 +22,3 @@ return net[0].toString(); | ||
async function promise(family) { | ||
async function async(family) { | ||
try { | ||
@@ -37,8 +37,16 @@ const {gateway} = await defaultGateway[family](); | ||
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'); | ||
} | ||
module.exports = internalIp; | ||
export async function internalIpV4() { | ||
return async('v4'); | ||
} | ||
export function internalIpV6Sync() { | ||
return sync('v6'); | ||
} | ||
export function internalIpV4Sync() { | ||
return sync('v4'); | ||
} |
{ | ||
"name": "internal-ip", | ||
"version": "6.2.0", | ||
"version": "7.0.0", | ||
"description": "Get your internal IP address", | ||
@@ -13,4 +13,9 @@ "license": "MIT", | ||
}, | ||
"type": "module", | ||
"exports": { | ||
"node": "./index.js", | ||
"default": "./browser.js" | ||
}, | ||
"engines": { | ||
"node": ">=10" | ||
"node": "^12.20.0 || ^14.13.1 || >=16.0.0" | ||
}, | ||
@@ -23,8 +28,5 @@ "scripts": { | ||
"index.d.ts", | ||
"browser.js" | ||
"browser.js", | ||
"browser.d.ts" | ||
], | ||
"exports": { | ||
"browser": "./browser.js", | ||
"default": "./index.js" | ||
}, | ||
"keywords": [ | ||
@@ -43,4 +45,4 @@ "ip", | ||
"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", | ||
@@ -50,7 +52,6 @@ "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" | ||
} | ||
} |
@@ -1,2 +0,2 @@ | ||
# internal-ip [![Build Status](https://travis-ci.com/sindresorhus/internal-ip.svg?branch=master)](https://travis-ci.com/github/sindresorhus/internal-ip) | ||
# internal-ip | ||
@@ -7,5 +7,5 @@ > Get your internal IP address | ||
```sh | ||
npm install internal-ip | ||
``` | ||
$ npm install internal-ip | ||
``` | ||
@@ -15,26 +15,36 @@ ## Usage | ||
```js | ||
const internalIp = require('internal-ip'); | ||
import {internalIpV6, internalIpV4} from 'internal-ip'; | ||
(async () => { | ||
console.log(await internalIp.v6()); | ||
//=> 'fe80::1' | ||
console.log(await internalIp.v4()); | ||
//=> '10.0.0.79' | ||
})(); | ||
console.log(internalIp.v6.sync()) | ||
console.log(await internalIpV6()); | ||
//=> 'fe80::1' | ||
console.log(internalIp.v4.sync()) | ||
console.log(await internalIpV4()); | ||
//=> '10.0.0.79' | ||
``` | ||
The module returns the 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. | ||
## API | ||
The module relies on operating systems tools. On Linux and Android, the `ip` command must be available, which depending on distribution might not be installed by default. It is usually provided by the `iproute2` package. `.v4.sync()` and `.v6.sync()` are not supported in browsers and just return `undefined`. | ||
The package returns the 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. | ||
The package relies on operating systems tools. On Linux and Android, the `ip` command must be available, which depending on distribution might not be installed by default. It is usually provided by the `iproute2` package. `internalIpV6Sync()` and `internalIpV4Sync()` are not supported in browsers and just return `undefined`. | ||
### internalIpV6() | ||
Returns the internal IPv6 address asynchronously. | ||
### internalIpV4() | ||
Returns the internal IPv4 address asynchronously. | ||
### internalIpV6Sync() | ||
Returns the internal IPv6 address synchronously. | ||
### internalIpV4Sync() | ||
Returns the internal IPv4 address synchronously. | ||
## Related | ||
- [internal-ip-cli](https://github.com/sindresorhus/internal-ip-cli) - CLI for this module | ||
- [internal-ip-cli](https://github.com/sindresorhus/internal-ip-cli) - CLI for this package | ||
- [public-ip](https://github.com/sindresorhus/public-ip) - Get your public IP address | ||
@@ -41,0 +51,0 @@ - [default-gateway](https://github.com/silverwind/default-gateway) - Get your default gateway address |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
8032
7
138
62
Yes
+ Addedipaddr.js@2.2.0(transitive)
- Removedipaddr.js@1.9.1(transitive)
Updateddefault-gateway@^6.0.3
Updatedipaddr.js@^2.0.1