Skip to content

olsonpm/hosts-utilities

Repository files navigation

Hosts Utilities - Beta

Table of Contents


What is it?

A few node utilities for working with your hosts file, inspired by Hostile

Note

If you're looking for a CLI frontend, raise an issue and I'll write one.


Why did I make this library?

I wanted to programatically work with my hosts file while maintaining formatting. Hostile doesn't seem to be maintained anymore and lacks features I was looking for.


Why is it in beta?

Because I haven't worked much with hosts files and can't be confident in the code until it's used in the real world.

The hosts file seems simple, but in my experience, real-world implementations of anything are quirky.


Install

npm install hosts-utilities

Use

Assume a hosts file /etc/hosts

1.2.3.4 hostname1

We can add hostname2 using assignHostnames

import * as hosts from 'hosts-utilities'

await hosts.assignHostnames('1.2.3.4', ['hostname2'])

// updates /etc/hosts to
// 1.2.3.4 hostname1 hostname2

API

Note

Both named and sub-path exports are available. Paths are kebab-cased.
Example:

import { assignHostnames } from 'hosts-utilities'
// or
import assignHostnames from 'hosts-utilities/assign-hostnames'

assignHostnames

Add or update host entries

Signature

  • async (ip: string, hostnames: string[], options: AssignOptions = {}) => undefined
    • ip and hostnames must match /^[^#\s]+$/

Examples

Assume a file /etc/hosts with

1.2.3.4 hostname1 hostname2
await assignHostnames('1.2.3.4', ['hostname1', 'hostname3'])
// updates /etc/hosts to
// 1.2.3.4 hostname1 hostname2 hostname3

await assignHostnames('5.6.7.8', ['hostname4'])
// adds the line
// 5.6.7.8 hostname4

hostsPath

A string

  • On windows: C:\Windows\System32\drivers\etc\hosts
  • On not windows: /etc/hosts

parse

Parse the hosts file

Why use it?

  • This let's you perform your own logic on the hosts file. For example, you could parse it, modify the parsed lines, then call write(updatedParsedLines)

Signature

Examples

const parsedLines1 = await parse()
const parsedLines2 = await parse({ filePath: '/path/to/custom/hosts' })

removeHostnames

Signature

  • async (hostnames: string[], options: RemoveOptions = {}) => undefined
    • hostnames must match /^[^#\s]+$/

Examples

Let's assume a file /etc/hosts with

1.2.3.4 hostname1
5.6.7.8 hostname2 hostname3
await removeHostnames(['hostname1', 'hostname2'])
// updates /etc/hosts to
// 5.6.7.8 hostname3

write

Write parsed lines to the hosts file

Why use it?

  • Like parse, this lets you perform your own logic on the hosts file. For example, you could parse it, modify the parsedLines, then write them.

Signature

Examples

const parsedLines = await parse()
const updatedLines = doSomethingTo(parsedLines)
await write(updatedLines)

Schemas

Parse Options

{
  // default: hostsPath
  // validation: minimum of one character
  filePath?: string
}

Remove Hostnames Options

Includes FormatOptions

FormatOptions & {
  // default: hostsPath
  // validation: minimum of one character
  filePath?: string

  // default: undefined (removes hostnames for all ips)
  // validation: matches /^[^#\s]+$/
  withIp?: string
}

Warning

The option withIp is provided because Hostile supports it, meaning I assume someone found it helpful. Keep in mind hosts shouldn't be mapped to more than one IP address, so withIp should be unnecessary.


Assign Hostnames Options

Includes FormatOptions

FormatOptions & {
  // default: hostsPath
  // validation: minimum of one character
  filePath?: string
}

Write Options

Includes FormatOptions

FormatOptions & {
  // default: hostsPath
  // validation: minimum of one character
  filePath?: string
}

Format Options

Note

Due to complexity, these options may not behave as you expect.

At a glance

{
  preserveFormatting?: boolean = true
  separatorParts?: string = '\t'
  separatorHostname?: string = ' '
}

Parsed Line

Note

Due to complexity, this structure may not work how you expect.

At a glance

{
  original: string
  data: {
    ip: string
    hostnamesWithSpace: string[]
    comment: string
    space: {
      beforeIp: string
      afterIp: string
      beforeComment: string
    }
  }
}

About

Utilities for the hosts file

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors