Host
Host names and unresolved host and port endpoints.
A Host is either a numeric IP address, an IPv6 literal with a zone such as
fe80::1%eth0, or a DNS domain name. Numeric addresses are parsed directly
into NetAddress values. The AddressResolver service turns a HostPort
into internet addresses, looking up domain names with the Dns service.
Constructors
Converts a DomainNameInput to a domain name, parsing and normalizing it like
domainNameFromString.
Details
Domain names are strings, so every input is parsed.
Signature
declare function domainNameFromInput(input: DomainNameInput): Result<DomainName, NetAddressError>Converts a HostInput to a host, parsing strings like hostFromString and
converting other inputs like NetAddress.ipFromInput.
Signature
declare function hostFromInput(input: HostInput): Result<Host, NetAddressError>Creates a checked host and port.
Signature
declare function hostPort(host: Host, port: number): Result<HostPort, NetAddressError>Converts a HostPortInput to an endpoint, parsing strings like
hostPortFromString, converting parts like hostPort, and returning
endpoints unchanged.
Signature
declare function hostPortFromInput(input: HostPortInput): Result<HostPort, NetAddressError>Converting
Appends the trailing dot to a domain name, unless it is already fully qualified.
Signature
declare function toFullyQualified(self: DomainName): DomainNameRemoves the trailing dot of a fully qualified domain name, except from the
root name ..
Signature
declare function toRelative(self: DomainName): DomainNameDecoding
Parses and normalizes a domain name.
Details
ASCII names are lowercased. Names containing non-ASCII characters are
converted to their ASCII (xn--) form using the WHATWG URL host parser, then
validated with the same rules as ASCII names. A trailing dot is preserved.
Signature
declare function domainNameFromString(input: string): Result<DomainName, NetAddressError>Example Normalizing domain names
import { Result } from "effect"import { Host } from "effect/net"
Result.getOrThrow(Host.domainNameFromString("Example.COM")) // => "example.com"Result.getOrThrow(Host.domainNameFromString("bรผcher.example")) // => "xn--bcher-kva.example"Result.isFailure(Host.domainNameFromString("1.2.3.4")) // => trueParses a host as a numeric IP address, a scoped IPv6 literal, or a domain name, in that order.
Signature
declare function hostFromString(input: string): Result<Host, NetAddressError>Example Parsing hosts
import { Result } from "effect"import { Host, NetAddress } from "effect/net"
NetAddress.isIpAddress(Result.getOrThrow(Host.hostFromString("10.0.0.5"))) // => trueResult.getOrThrow(Host.hostFromString("fe80::1%eth0")) // => "fe80::1%eth0"Result.getOrThrow(Host.hostFromString("DB.internal")) // => "db.internal"Parses host:port, [IPv6]:port, or [IPv6%zone]:port.
Details
IPv6 hosts must be bracketed and other hosts must not be. The port is
required and must be an unpadded decimal integer from 0 through 65535. A
zone follows the % directly; the percent-encoded %25 separator of RFC
6874 URLs is not decoded, because [fe80::1%253]:80 would be ambiguous.
See
- formatHostPort for the inverse operation
Signature
declare function hostPortFromString(input: string): Result<HostPort, NetAddressError>Example Parsing host and port strings
import { Result } from "effect"import { Host } from "effect/net"
const endpoint = Result.getOrThrow(Host.hostPortFromString("DB.internal:5432"))endpoint.host // => "db.internal"endpoint.port // => 5432Host.formatHostPort(Result.getOrThrow(Host.hostPortFromString("[::1]:80"))) // => "[::1]:80"Parses a hosts file into the addresses of each host name. Invalid entries are skipped.
Signature
declare function parseHostsFile(text: string): HostsTableExample Parsing a hosts file
import { Host, NetAddress } from "effect/net"
const hosts = Host.parseHostsFile(`127.0.0.1 localhost::1 localhost ip6-localhost # loopback`)hosts.get(Host.domainNameFromStringUnsafe("localhost"))?.map(NetAddress.formatIp) // => ["127.0.0.1", "::1"]Encoding
Formats a host without brackets.
Signature
declare function formatHost(self: Host): stringFormats a host and port, bracketing IPv6 hosts.
See
- hostPortFromString for the inverse operation
Signature
declare function formatHostPort(self: HostPort): stringGuards
Returns true when a value is a valid, normalized domain name.
Signature
declare function isDomainName(u: unknown): u is DomainNameReturns true when a value is a host.
Signature
declare function isHost(u: unknown): u is HostReturns true when a value is an unresolved host and port.
Signature
declare function isHostPort(u: unknown): u is HostPortModels
DomainName
typeA syntactically valid DNS domain name in lowercase ASCII form.
Details
Names follow the rules used by Go's resolver: at most 253 characters, labels
of 1 to 63 letters, digits, hyphens, or underscores, and no label starting or
ending with a hyphen. Underscores are allowed so that service names such as
_http._tcp.example.com are valid. The last label must not be numeric, which
keeps IPv4-like strings out. A trailing dot marks a fully qualified name and
is preserved. The root name . is valid.
Internationalized names are stored in their ASCII (xn--) form.
See
- domainNameFromString for parsing and normalizing domain names
Signature
type DomainName = Brand.Branded<string, "~effect/net/Host/DomainName">DomainNameInput
typeA domain name or a string to parse and normalize.
Signature
type DomainNameInput = DomainName | stringA host as written in configuration or a URL authority: a numeric IP address, a scoped IPv6 literal, or a domain name.
See
- hostFromString for parsing hosts
Signature
type Host = NetAddress.IpAddress | NetAddress.ScopedIpv6Literal | DomainNameA host, or a string or any IpAddressInput to convert to one.
Signature
type HostInput = Host | NetAddress.IpAddressInputAn unresolved host and port.
See
- hostPortFromString for parsing
host:portstrings - formatHostPort for formatting
host:portstrings
Signature
interface HostPort extends Equal, Hash, Inspectable { readonly _tag: "HostPort"; readonly "~effect/net/Host": "~effect/net/Host"; readonly host: Host; readonly port: number;}HostPortInput
typeAn endpoint, or a host:port string or parts to convert to one.
Signature
type HostPortInput = HostPort | string | HostPortPartsHostPortParts
interfaceThe host and port of an endpoint.
See
- hostPort for the checked constructor
Signature
interface HostPortParts { readonly host: HostInput; readonly port: number;}HostsTable
typeThe addresses of host names, as read from a hosts file.
See
- parseHostsFile for reading a hosts file
Signature
type HostsTable = ReadonlyMap<DomainName, Arr.NonEmptyReadonlyArray<NetAddress.IpAddress>>Predicates
Returns true when a domain name ends with a dot, which excludes it from
resolver search domains.
Signature
declare function isFullyQualified(self: DomainName): booleanUnsafe
Converts a trusted DomainNameInput to a domain name, throwing on failure.
Signature
declare function domainNameFromInputUnsafe(input: DomainNameInput): DomainNameParses a trusted domain name, throwing on failure.
Signature
declare function domainNameFromStringUnsafe(input: string): DomainNameConverts a trusted HostInput to a host, throwing on failure.
Signature
declare function hostFromInputUnsafe(input: HostInput): HostParses a trusted host, throwing on failure.
Signature
declare function hostFromStringUnsafe(input: string): HostConverts a trusted HostPortInput to an endpoint, throwing on failure.
Signature
declare function hostPortFromInputUnsafe(input: HostPortInput): HostPortParses a trusted host:port string, throwing on failure.
Signature
declare function hostPortFromStringUnsafe(input: string): HostPortCreates a trusted host and port, throwing on failure.
Signature
declare function hostPortUnsafe(host: Host, port: number): HostPort