Skip to main content

URL & Hostname Utilities

parseHostname

Parse and normalize a hostname input for use as a .localhost subdomain.
Behavior:
  • Strips protocol prefixes (http://, https://)
  • Converts to lowercase
  • Appends .localhost if not present
  • Validates hostname characters (letters, digits, hyphens, dots only)
  • Enforces DNS label length limits (63 characters per label)
  • Rejects consecutive dots and invalid patterns
string
required
The hostname to parse. Can include protocol prefixes, which will be stripped.
Returns: Normalized .localhost hostname Throws: Error if the hostname is invalid Examples:

formatUrl

Format a .localhost URL with the correct protocol and port.
Constructs a complete URL for a .localhost hostname. Omits the port when it matches the protocol default (80 for HTTP, 443 for HTTPS).
string
required
The .localhost hostname (e.g., “api.myapp.localhost”)
number
required
The proxy port number
boolean
default:false
Whether to use HTTPS protocol
Returns: Complete URL string Examples:

escapeHtml

Escape HTML special characters to prevent XSS attacks.
Converts &, <, >, ", and ' to their HTML entity equivalents.
string
required
The string to escape
Returns: HTML-safe string Examples:

System Utilities

isErrnoException

Type guard for Node.js system errors with error codes.
Check if an error object is a Node.js system error with an error code like EADDRINUSE, EACCES, etc.
unknown
required
The error object to check
Returns: true if the error is a NodeJS.ErrnoException, false otherwise Examples:

fixOwnership

Fix file ownership when running under sudo.
When running as root via sudo, changes file ownership back to the real user. This prevents files created by sudo from being owned by root. No-op when not running as root.
string[]
required
File or directory paths to fix ownership on
Examples:

/etc/hosts Management

These functions manipulate /etc/hosts to add or remove .localhost entries for Safari compatibility.

syncHostsFile

Sync /etc/hosts to include entries for given hostnames.
Adds a portless-managed block to /etc/hosts mapping each hostname to 127.0.0.1. Requires root access (sudo).
string[]
required
Array of .localhost hostnames to add
Returns: true on success, false on failure Examples:

cleanHostsFile

Remove the portless-managed block from /etc/hosts.
Removes all entries added by syncHostsFile. Requires root access (sudo). Returns: true on success, false on failure Examples:

extractManagedBlock

Extract portless-managed entries from /etc/hosts content.
string
required
The raw /etc/hosts file content
Returns: Array of lines between portless markers (excluding the markers themselves) Examples:

removeBlock

Remove the portless-managed block from /etc/hosts content.
string
required
The raw /etc/hosts file content
Returns: Content with portless block removed Examples:

buildBlock

Build a portless-managed block for given hostnames.
string[]
required
Array of .localhost hostnames
Returns: String containing the portless-managed block with markers Examples:

getManagedHostnames

Get currently managed hostnames from /etc/hosts.
Returns: Array of hostnames currently in the portless-managed block Examples:

checkLocalhostResolution

Check whether a .localhost subdomain resolves to 127.0.0.1.
Tests if the system DNS resolver correctly resolves a .localhost hostname to 127.0.0.1. Useful for detecting Safari/DNS issues.
string
required
The .localhost hostname to check
Returns: Promise that resolves to true if resolution works, false otherwise Examples:

Import Examples

API Overview

Getting started with the programmatic API

Type Definitions

TypeScript interfaces and types

Safari DNS

Learn about /etc/hosts management