URL & Hostname Utilities
parseHostname
Parse and normalize a hostname input for use as a.localhost subdomain.
- Strips protocol prefixes (
http://,https://) - Converts to lowercase
- Appends
.localhostif 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.
.localhost hostname
Throws: Error if the hostname is invalid
Examples:
formatUrl
Format a.localhost URL with the correct protocol and port.
.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
escapeHtml
Escape HTML special characters to prevent XSS attacks.&, <, >, ", and ' to their HTML entity equivalents.
string
required
The string to escape
System Utilities
isErrnoException
Type guard for Node.js system errors with error codes.EADDRINUSE, EACCES, etc.
unknown
required
The error object to check
true if the error is a NodeJS.ErrnoException, false otherwise
Examples:
fixOwnership
Fix file ownership when running under sudo.string[]
required
File or directory paths to fix ownership on
/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.
/etc/hosts mapping each hostname to 127.0.0.1. Requires root access (sudo).
string[]
required
Array of
.localhost hostnames to addtrue on success, false on failure
Examples:
cleanHostsFile
Remove the portless-managed block from/etc/hosts.
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 contentremoveBlock
Remove the portless-managed block from/etc/hosts content.
string
required
The raw
/etc/hosts file contentbuildBlock
Build a portless-managed block for given hostnames.string[]
required
Array of
.localhost hostnamesgetManagedHostnames
Get currently managed hostnames from/etc/hosts.
checkLocalhostResolution
Check whether a.localhost subdomain resolves to 127.0.0.1.
.localhost hostname to 127.0.0.1. Useful for detecting Safari/DNS issues.
string
required
The
.localhost hostname to checktrue if resolution works, false otherwise
Examples:
Import Examples
Related
API Overview
Getting started with the programmatic API
Type Definitions
TypeScript interfaces and types
Safari DNS
Learn about /etc/hosts management