is-localhost-ip 3.0.0 → 3.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +30 -10
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,13 +1,16 @@
|
|
|
1
|
-
[](https://codecov.io/gh/tinovyatkin/is-localhost-ip)
|
|
1
|
+
[](https://codecov.io/gh/tinovyatkin/is-localhost-ip)
|
|
2
|
+

|
|
2
3
|
|
|
3
4
|
# is-localhost-ip
|
|
4
5
|
|
|
5
|
-
|
|
6
|
+
Zero-dependency Node.js utility that checks whether a hostname or IPv4/IPv6 address refers to the local machine.
|
|
6
7
|
|
|
7
|
-
|
|
8
|
-
be a host name), then fallback to DNS resolver (so it works with something like `john.dev` remapped locally in `hosts` or with local resolver).
|
|
8
|
+
This package aims to be strict and comprehensive:
|
|
9
9
|
|
|
10
|
-
|
|
10
|
+
- Validates input as an IP address or a syntactically valid hostname (including bracketed IPv6).
|
|
11
|
+
- Treats private/loopback/link-local ranges as local.
|
|
12
|
+
- Optionally verifies the address exists on the current machine by attempting to bind to it.
|
|
13
|
+
- Falls back to DNS resolution, so it works with hostnames mapped in `/etc/hosts` or a local resolver.
|
|
11
14
|
|
|
12
15
|
## Installation
|
|
13
16
|
|
|
@@ -15,9 +18,13 @@ All this in just _~100 lines of code_ without external dependencies.
|
|
|
15
18
|
npm i is-localhost-ip
|
|
16
19
|
# or
|
|
17
20
|
yarn add is-localhost-ip
|
|
21
|
+
# or
|
|
22
|
+
pnpm add is-localhost-ip
|
|
18
23
|
```
|
|
19
24
|
|
|
20
|
-
|
|
25
|
+
Requires Node.js `>=18`.
|
|
26
|
+
|
|
27
|
+
## Usage
|
|
21
28
|
|
|
22
29
|
```js
|
|
23
30
|
const isLocalhost = require("is-localhost-ip");
|
|
@@ -26,19 +33,32 @@ const isLocalhost = require("is-localhost-ip");
|
|
|
26
33
|
await isLocalhost("127.0.0.1"); // true
|
|
27
34
|
await isLocalhost("::ffff:127.0.0.1"); // true
|
|
28
35
|
await isLocalhost("192.168.0.12"); // true
|
|
29
|
-
await isLocalhost("192.168.0.12", true); // true only if
|
|
36
|
+
await isLocalhost("192.168.0.12", true); // true only if an interface has this address
|
|
30
37
|
await isLocalhost("184.55.123.2"); // false
|
|
31
38
|
|
|
32
|
-
await isLocalhost("tino.local"); // true
|
|
39
|
+
await isLocalhost("tino.local"); // true if it resolves to a local address
|
|
33
40
|
await isLocalhost("localhost"); // true
|
|
34
41
|
await isLocalhost("microsoft.com"); // false
|
|
35
42
|
})();
|
|
36
43
|
```
|
|
37
44
|
|
|
45
|
+
## API
|
|
46
|
+
|
|
47
|
+
### `isLocalhost(ipOrHostname, canBind?)`
|
|
48
|
+
|
|
49
|
+
Returns a `Promise<boolean>`.
|
|
50
|
+
|
|
51
|
+
- `ipOrHostname` (`string`): IP address (v4/v6) or a hostname.
|
|
52
|
+
- `canBind` (`boolean`, default `false`): when `true`, additionally checks that the local machine can bind to the
|
|
53
|
+
address (i.e., it is configured on a local interface).
|
|
54
|
+
|
|
55
|
+
The function throws for invalid inputs (non-string values or syntactically invalid hostnames).
|
|
56
|
+
|
|
38
57
|
## Caveats
|
|
39
58
|
|
|
40
|
-
|
|
41
|
-
|
|
59
|
+
Internationalized domain names (IDNs) are not supported. If you need IDNs, use
|
|
60
|
+
[Punycode.js](https://github.com/bestiejs/punycode.js) (or another punycode implementation) to convert the input
|
|
61
|
+
to ASCII before calling this function:
|
|
42
62
|
|
|
43
63
|
```js
|
|
44
64
|
const isLocalhost = require("is-localhost-ip");
|