https://github.com/thelocalcoder/deno-hosts
The hosts file parsing and resolver module for Deno
https://github.com/thelocalcoder/deno-hosts
deno hostname hostsfile
Last synced: about 1 month ago
JSON representation
The hosts file parsing and resolver module for Deno
- Host: GitHub
- URL: https://github.com/thelocalcoder/deno-hosts
- Owner: thelocalcoder
- License: mit
- Created: 2021-02-28T11:06:19.000Z (over 5 years ago)
- Default Branch: main
- Last Pushed: 2021-03-19T13:00:23.000Z (about 5 years ago)
- Last Synced: 2026-01-12T02:25:47.735Z (5 months ago)
- Topics: deno, hostname, hostsfile
- Language: TypeScript
- Homepage: https://thelocalcoder.github.io/deno-hosts
- Size: 39.1 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Contributing: CONTRIBUTING.md
- License: LICENSE
- Code of conduct: CODE_OF_CONDUCT.md
Awesome Lists containing this project
README
# 🦖 Deno-Hosts
[](https://nest.land/package/deno_hosts)
**The hosts file parsing and resolver module for Deno**
Deno-Hosts is a Deno (Typescript) module for parsing hosts files and performing reverse IP -> hostname or vice versa lookups which are file or text data based (e.g. via `/etc/hosts`).
This can be helpful to determine if there is a prettier (or known) hostname available for an IP address.
These lookups are "extremely inexpensive" compared to normal IP reverse DNS lookups because no network communication is required, as these lookups are all file-based (offline)! Naturally, the (obvious) tradeoff/downside is that this only works in cases where the IP mapping exists in the hostsfile.
## Usages
```javascript
import Hosts from 'https://deno.land/x/deno_hosts@v1.0.1/mod.ts';
const hostsPath = "/etc/hosts"; // Hosts file path
const hosts = new Hosts(Deno.readTextFileSync(hostsPath));
console.log(hosts.toObject());
```
### Output
```json
[
{
"ip": "127.0.0.1",
"hostname": "localhost",
"alias": null
},
{
"ip": "127.0.1.1",
"hostname": "local.test",
"alias": "local"
}
]
```
## Deno Registry Links
- [Nest.land -> deno_hosts](https://nest.land/package/deno_hosts)
- [Deno.land -> deno_hosts](https://deno.land/x/deno_hosts)
**Usages from Nest.land**
```javascript
import Hosts from 'https://x.nest.land/deno_hosts@2.0.0/mod.ts';
```
**Usages from Deno.land**
```javascript
import Hosts from 'https://deno.land/x/deno_hosts/mod.ts'; // Latest version
```
```javascript
import Hosts from 'https://deno.land/x/deno_hosts@v1.0.1/mod.ts' // Specific Version
```
## API
- **hosts.toObject()** - For geeting JSON from hosts file.
- **hosts.toText()** - Re-convert text from parsed hosts file.
- **hosts.resolve(hostname : string)** - Lookup hostname -> IP (Return `undefined` in case of not found).
- **hosts.reverse(ip : string)** - Lookup IP -> hostname (Return `undefined` in case of not found).
- **hosts.match(str: string)** - Lookup and match ip/hostname/alias and return a Host object.
```javascript
type Host = {
ip: string,
hostname: string,
alias: string | null
}
```
```javascript
class Hosts {
constructor(hosts : string) : Object
toObject() : Hosts[]
toText() : string
resolve(hostname : string) : string | undefined
reverse(ip: string) : string | undefined
match(str : string) : Host | undefined
}
```
**Note:** Hosts file location depends on OS. So use according your OS and for checking run time env OS use `Deno.build` stable API.
- Windows - `C:/Windows/System32/drivers/etc/hosts`
- Linux - `/etc/hosts`
- Mac OS X - `/private/etc/hosts`
## ToDo
- [x] Write Test cases.
- [ ] Write JSDoc for class methods.
- [x] Write Inital Readme content.
- [ ] Write HowTo Guide.
- [x] Publish at Deno.land
- [x] Publish at Nest.land
- [ ] Setup auto Testing using Travis CI
## References
1. [What is hosts file (wiki)](https://en.wikipedia.org/wiki/Hosts_%28file%29)
2. [hosts-parser](https://github.com/imyelo/hosts-parser)
3. [go-hostsfile](https://github.com/jaytaylor/go-hostsfile)
## License
Permissive MIT license.
## Contact
You are more than welcome to open issues and send pull requests if you find a bug or want a new feature.
If you appreciate this module please feel free to drop me a line and tell me! It's always nice to hear from people who have benefitted from my work.
Email: [hello (at) ganeshagrawal.com](mailto:hello@ganeshagrawal.com)
Twitter: [@imganeshagrawal](https://twitter.com/imganeshagrawal)