Skip to content
Effect 4.0 is here. One ecosystem. Zero dependencies.

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.

31 exports Since v4.0.0 Source

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>
Since v4.0.0

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): DomainName

Removes the trailing dot of a fully qualified domain name, except from the root name ..

Signature

declare function toRelative(self: DomainName): DomainName

Decoding

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")) // => true

Parses 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"))) // => true
Result.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

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 // => 5432
Host.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): HostsTable

Example 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): string

Formats a host and port, bracketing IPv6 hosts.

See

Signature

declare function formatHostPort(self: HostPort): string

Guards

Returns true when a value is a valid, normalized domain name.

Signature

declare function isDomainName(u: unknown): u is DomainName
Since v4.0.0

Returns true when a value is a host.

Signature

declare function isHost(u: unknown): u is Host

Returns true when a value is an unresolved host and port.

Signature

declare function isHostPort(u: unknown): u is HostPort

Models

Since v4.0.0

A 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

Signature

type DomainName = Brand.Branded<string, "~effect/net/Host/DomainName">
Since v4.0.0

A domain name or a string to parse and normalize.

Signature

type DomainNameInput = DomainName | string

Host

type
Since v4.0.0

A host as written in configuration or a URL authority: a numeric IP address, a scoped IPv6 literal, or a domain name.

See

Signature

type Host = NetAddress.IpAddress | NetAddress.ScopedIpv6Literal | DomainName
Since v4.0.0

A host, or a string or any IpAddressInput to convert to one.

Signature

type HostInput = Host | NetAddress.IpAddressInput

HostPort

interface
Since v4.0.0

An unresolved host and port.

See

Signature

interface HostPort extends Equal, Hash, Inspectable {
readonly _tag: "HostPort";
readonly "~effect/net/Host": "~effect/net/Host";
readonly host: Host;
readonly port: number;
}
Since v4.0.0

An endpoint, or a host:port string or parts to convert to one.

Signature

type HostPortInput = HostPort | string | HostPortParts

HostPortParts

interface
Since v4.0.0

The host and port of an endpoint.

See

Signature

interface HostPortParts {
readonly host: HostInput;
readonly port: number;
}
Since v4.0.0

The addresses of host names, as read from a hosts file.

See

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): boolean

Unsafe

Converts a trusted DomainNameInput to a domain name, throwing on failure.

Signature

declare function domainNameFromInputUnsafe(input: DomainNameInput): DomainName

Parses a trusted domain name, throwing on failure.

Signature

declare function domainNameFromStringUnsafe(input: string): DomainName

Converts a trusted HostInput to a host, throwing on failure.

Signature

declare function hostFromInputUnsafe(input: HostInput): Host

Parses a trusted host, throwing on failure.

Signature

declare function hostFromStringUnsafe(input: string): Host

Converts a trusted HostPortInput to an endpoint, throwing on failure.

Signature

declare function hostPortFromInputUnsafe(input: HostPortInput): HostPort

Parses a trusted host:port string, throwing on failure.

Signature

declare function hostPortFromStringUnsafe(input: string): HostPort

Creates a trusted host and port, throwing on failure.

Signature

declare function hostPortUnsafe(host: Host, port: number): HostPort