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.
Files changed (2) hide show
  1. package/README.md +30 -10
  2. package/package.json +1 -1
package/README.md CHANGED
@@ -1,13 +1,16 @@
1
- [![codecov](https://codecov.io/gh/tinovyatkin/is-localhost-ip/branch/master/graph/badge.svg)](https://codecov.io/gh/tinovyatkin/is-localhost-ip) ![node](https://img.shields.io/node/v/is-localhost-ip)
1
+ [![codecov](https://codecov.io/gh/tinovyatkin/is-localhost-ip/branch/master/graph/badge.svg)](https://codecov.io/gh/tinovyatkin/is-localhost-ip)
2
+ ![node](https://img.shields.io/node/v/is-localhost-ip)
2
3
 
3
4
  # is-localhost-ip
4
5
 
5
- Comprehensive and robust library to checks whether given host name or IPv4/IPv6 address belongs to the local machine
6
+ Zero-dependency Node.js utility that checks whether a hostname or IPv4/IPv6 address refers to the local machine.
6
7
 
7
- Main difference from other libraries here is comprehensiveness: we start from strict RegExp checks (for IP address first, and then for correctness to
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
- All this in just _~100 lines of code_ without external dependencies.
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
- ## Example
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 the local machine has an interface with that address
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
- Doesn't work with internationalized ([RFC 3492](https://tools.ietf.org/html/rfc3492) or [RFC 5891](https://tools.ietf.org/html/rfc5891)) domain names.
41
- If you need that please use wonderful [Punycode.js](https://github.com/bestiejs/punycode.js) to convert the string before passing to this library:
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");
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "is-localhost-ip",
3
- "version": "3.0.0",
3
+ "version": "3.0.1",
4
4
  "description": "Checks whether given DNS name or IPv4/IPv6 address belongs to a local machine",
5
5
  "keywords": [
6
6
  "check-localhost",