🦖 Deno-Hosts

nest badge

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§

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§

[
    {
        "ip": "127.0.0.1",      
        "hostname": "localhost",
        "alias": null
    },
    {
        "ip": "127.0.1.1",
        "hostname": "local.test",
        "alias": "local"
    }
]

Usages from Nest.land

import Hosts from 'https://x.nest.land/deno_hosts@2.0.0/mod.ts';

Usages from Deno.land

import Hosts from 'https://deno.land/x/deno_hosts/mod.ts'; // Latest version
import Hosts from 'https://deno.land/x/deno_hosts@v1.0.1/mod.ts' // Specific Version

API§

type Host = {
    ip: string,
    hostname: string,
    alias: string | null
}
class Hosts {
    constructor(hosts : string) : Object<Hosts>
    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.

ToDo§

References§

  1. What is hosts file (wiki)
  2. hosts-parser
  3. 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

Twitter: @imganeshagrawal