API Docs

Module for inspecting network interfaces, resolving hostnames, and reading system configuration

Types

This module provides the following types:

Properties

hs.network.reachabilityFlags

{[key: string]: number}

A dictionary of named flag constants for use with HSNetworkReachability.status().

Compare individual bits against these constants to determine which network conditions apply. The numeric values match the deprecated SCNetworkReachabilityFlags for backward compatibility.

Keys: transientConnection, reachable, connectionRequired, connectionOnTraffic, interventionRequired, connectionOnDemand, isLocalAddress, isDirect.

Methods

hs.network.interfaces() -> [[String: Any]]

Returns all network interfaces present on this system.

Each object contains name (string), isLoopback (boolean), isUp (boolean), and isRunning (boolean). A displayName string is included when the system provides a human-readable label for the interface (e.g. "Wi-Fi" or "Ethernet").

hs.network.interfaces() -> [[String: Any]]
[[String: Any]]
An array of objects describing each network interface.
const ifaces = hs.network.interfaces()
ifaces.forEach(i => console.log(i.name + ": " + (i.displayName || "(no display name)")))

hs.network.primaryInterface() -> string

Returns the name of the primary network interface, i.e. the one currently providing the default route.

hs.network.primaryInterface() -> string
string
The BSD interface name (e.g. "en0"), or null if no primary interface can be determined.
const primary = hs.network.primaryInterface()
console.log("Primary interface: " + primary)

hs.network.addresses() -> [[String: Any]]

Returns all IP addresses assigned to this host.

Each object contains interface (the BSD name of the interface), address (the address string), and family ("ipv4" or "ipv6").

hs.network.addresses() -> [[String: Any]]
[[String: Any]]
An array of address objects.
const addrs = hs.network.addresses()
addrs.filter(a => a.family === 'ipv4').forEach(a => console.log(a.interface + ": " + a.address))

hs.network.hostnames() -> string[]

Returns all hostnames known for this Mac.

hs.network.hostnames() -> string[]
string[]
An array of hostname strings (e.g. ["My-Mac.local"]).
const names = hs.network.hostnames()
names.forEach(n => console.log(n))

hs.network.resolve(hostname, family) -> Promise<string[]>

Asynchronously resolves a hostname to its IP addresses using the system DNS resolver.

Uses CFHost, which respects the system's network configuration including VPN routes and proxy settings.

hs.network.resolve(hostname, family) -> Promise<string[]>
Name Type Description
hostname string The hostname to resolve (e.g. "example.com" or "localhost").
family string The address family to query: "ipv4" for A records only, "ipv6" for AAAA records only, or "both" to return all addresses. Defaults to "both" when omitted.
Promise<string[]>
A Promise that resolves to an array of IP address strings, or rejects with an error message if the lookup fails.
hs.network.resolve("example.com").then(addrs => {
    addrs.forEach(a => console.log(a))
})
hs.network.resolve("example.com", "ipv4").then(addrs => console.log("IPv4: " + addrs[0]))

hs.network.reachabilityForAddress(address) -> HSNetworkReachability

Creates a reachability monitor for a specific IP address.

Returns null if address is not a valid IPv4 or IPv6 address literal. Under the hood this monitors general system connectivity (the same as reachabilityInternet()), because NWPathMonitor does not support per-address targeting.

hs.network.reachabilityForAddress(address) -> HSNetworkReachability
Name Type Description
address string An IPv4 or IPv6 address string (e.g. "192.168.1.1" or "::1").
HSNetworkReachability
A new HSNetworkReachability monitor, or null if the address is invalid.
const r = hs.network.reachabilityForAddress("8.8.8.8")
r?.setCallback((obj, flags) => console.log(obj.statusString())).start()

hs.network.reachabilityForAddressPair(localAddress, remoteAddress) -> HSNetworkReachability

Creates a reachability monitor for a source/destination IP address pair.

Returns null if either address is not a valid IPv4 or IPv6 address literal. Under the hood this monitors general system connectivity (the same as reachabilityInternet()), because NWPathMonitor does not support per-address targeting.

hs.network.reachabilityForAddressPair(localAddress, remoteAddress) -> HSNetworkReachability
Name Type Description
localAddress string An IPv4 or IPv6 source address string.
remoteAddress string An IPv4 or IPv6 destination address string.
HSNetworkReachability
A new HSNetworkReachability monitor, or null if either address is invalid.
const r = hs.network.reachabilityForAddressPair("0.0.0.0", "8.8.8.8")
r?.setCallback((obj, flags) => console.log(obj.statusString())).start()

hs.network.reachabilityForHostName(hostName) -> HSNetworkReachability

Creates a reachability monitor for a given hostname.

Returns null if hostName is empty. Under the hood this monitors general system connectivity (the same as reachabilityInternet()), because NWPathMonitor does not support per-hostname targeting.

hs.network.reachabilityForHostName(hostName) -> HSNetworkReachability
Name Type Description
hostName string A hostname string (e.g. "example.com").
HSNetworkReachability
A new HSNetworkReachability monitor, or null if hostName is empty.
const r = hs.network.reachabilityForHostName("example.com")
r?.setCallback((obj, flags) => console.log(obj.statusString())).start()

hs.network.reachabilityInternet() -> HSNetworkReachability

Creates a reachability monitor for general internet connectivity.

This is the most common factory method. Use it when you want to know whether the device currently has a working internet connection.

hs.network.reachabilityInternet() -> HSNetworkReachability
HSNetworkReachability
A new HSNetworkReachability monitor.
hs.network.reachabilityInternet()
  .setCallback((r, flags) => console.log(r.statusString()))
  .start()

hs.network.reachabilityLinkLocal() -> HSNetworkReachability

Creates a reachability monitor for link-local connectivity.

Link-local addresses cover the 169.254.x.x (IPv4) and fe80::/10 (IPv6) ranges used for direct device-to-device communication without a router. Under the hood this monitors general system connectivity (the same as reachabilityInternet()), because NWPathMonitor does not distinguish link-local reachability.

hs.network.reachabilityLinkLocal() -> HSNetworkReachability
HSNetworkReachability
A new HSNetworkReachability monitor.
hs.network.reachabilityLinkLocal()
  .setCallback((r, flags) => console.log(r.statusString()))
  .start()

hs.network.configurationStore(pattern) -> {[key: string]: any}

Returns the contents of the macOS System Configuration dynamic store as a dictionary.

The store holds live network configuration for the running system — interface addresses, routing, DNS servers, proxy settings, VPN state, and more. Keys follow a hierarchical path convention (e.g. "State:/Network/Global/IPv4").

hs.network.configurationStore(pattern) -> {[key: string]: any}
Name Type Description
pattern string An optional regular expression that filters which keys are included.
{[key: string]: any}
A dictionary mapping key strings to their current values. Values may be
// Dump everything
console.log(JSON.stringify(hs.network.configurationStore()))

// Filter to network state
const state = hs.network.configurationStore("State:/Network/.*")
console.log(state["State:/Network/Global/IPv4"])

hs.network.configurationLocations() -> {[key: string]: string}

Returns a mapping of all configured network location UUIDs to their display names.

Use this to discover available locations before calling configurationSetLocation().

hs.network.configurationLocations() -> {[key: string]: string}
{[key: string]: string}
A dictionary mapping UUID strings to human-readable location names.
const locs = hs.network.configurationLocations()
Object.entries(locs).forEach(([uuid, name]) => console.log(uuid + ": " + name))

hs.network.configurationSetLocation(location) -> boolean

Switches the active network location to the one with the given name or UUID.

Pass the location's display name (e.g. "Home") or its UUID from configurationLocations(). The change is applied immediately. Returns false if the location was not found or the preferences could not be committed (e.g. insufficient privileges).

hs.network.configurationSetLocation(location) -> boolean
Name Type Description
location string A location display name or UUID string.
boolean
true if the location was changed successfully, false otherwise.
if (!hs.network.configurationSetLocation("Home")) {
  console.log("Failed to switch to Home location")
}

hs.network.configurationWatcher() -> HSNetworkConfigurationWatcher

Creates a watcher that fires a callback when System Configuration dynamic store keys change.

Call setKeys() to specify which keys (or patterns) to watch, setCallback() to register the handler, then start() to begin monitoring. The module automatically stops and destroys all watchers on hs.reload().

hs.network.configurationWatcher() -> HSNetworkConfigurationWatcher
HSNetworkConfigurationWatcher
A new HSNetworkConfigurationWatcher object.
hs.network.configurationWatcher()
  .setKeys(["State:/Network/Global/.*"], true)
  .setCallback((w, keys) => console.log("Changed: " + keys.join(", ")))
  .start()

hs.network.ping(server, options) -> HSNetworkPing

Sends ICMP Echo Requests to server and reports results via a callback.

DNS resolution and the first ping begin immediately. The returned object can be used to pause, resume, or cancel the ping, and to read statistics. The ping runs to completion, with its callback, even if you don't keep a reference to it.

The options argument may be:

  • Omitted (or undefined) — uses all defaults.
  • A callback function — used as the event handler with all other options at their defaults.
  • An object with any of: count (integer, default 5), interval (seconds, default 1.0), timeout (seconds per packet, default 2.0), family ("any" | "ipv4" | "ipv6", default "any"), and callback (function).

The callback receives (ping, event, info):

  • "didStart" — info is the resolved IP address string.
  • "didFail" — info is an error message string (e.g. DNS failure).
  • "sendPacketFailed" — info is [packetObject, errorString].
  • "receivedPacket" — info is the packet object (see HSNetworkPing.packets()).
  • "didFinish" — info is the summary string.
hs.network.ping(server, options) -> HSNetworkPing
Name Type Description
server string A hostname or IP address to ping.
options JSValue A callback function or options object. Optional.
HSNetworkPing
An HSNetworkPing object, or null if the arguments are invalid.
hs.network.ping("8.8.8.8", (ping, event, info) => {
  if (event === "receivedPacket") {
    console.log("seq " + info.sequenceNumber + " rtt=" + (info.rtt * 1000).toFixed(1) + "ms")
  } else if (event === "didFinish") {
    console.log(ping.summary())
  }
})