simple-hosts
Easy way to manipulate the hosts file contents.
Why this?
- simple, lightweight
- synchronous =>
- less error prone over other library since they open the small file for very long time which may cause file corruption
- ease of use, don't need to chain with Promise
- support multiple hostname associated with a single IP address
- well tested before release
- transparent source code
- free of use
What's news?
- 1.1.0
- Improve
set()
function to support new separate entry
- Improve
- 1.2.0
- Fix the
set()
function to support modifying existed hostname
- Fix the
- 1.2.1
- Refactor the source code. Create
SimpleHosts
class supporting input hosts file path - Add test cases
- Update document
- Refactor the source code. Create
- 1.2.2
- Use typescript
- 1.2.3
- Support remove records by IP or hostname with
removeIp()
andremoveHost()
function
- Support remove records by IP or hostname with
This package provides the below synchronous functions
-
Get corresponding IP of a hostname
getIp(hostname: string): string
Returns the corresponding IP that is mapped to the input hostname. If no record is found, it will return an empty string. If there are multiple IPs that are mapped to a same hostname, the first IP will be returned. Multiple IPs for a same hostname is not encouraged because it causes ambiguous
-
Get corresponding hostnames of an IP
getHosts(ip: string): string[]
Returns an array of found hostnames that are mapped to the input IP
-
Create or modify a record in hosts file
set(ip: string, hostname: string): void
If the same record that contains same IP and same hostname has been in the hosts file, no additional writing is performed. If the hostname has been already existed, it will be removed from the map of current IP and then new record will be added.
-
Remove records by ID address
removeIp(ip: string): void
-
Remove records by hostname
removeHost(hostname: string): void