glip

Glip provides a common representation for IP addresses on Gleam for Erlang and node/bun on JavaScript. The intent is to have a minimal representation in memory, and avoid unnecessary conversions for common operations. Glip is designed for server-oriented usage, which is why it doesn’t have a browser-compatible implementation, and only wraps functionality in the runtimes. (Feel free to open an issue if you have a valid browser use case).

Types

pub type AddressFamily {
  Ipv4
  Ipv6
}

Constructors

  • Ipv4
  • Ipv6

An IP address to be passed to external functions. NOTE: You can return IpAddress directly from external functions, as long as they follow the correct types, which are inet:ip_address() on Erlang and String on JavaScript. Note that the external function is responsible for the string being a valid IP address on JavaScript!

pub type ExternalIpAddress

An IP address that has been validated and may contain extra information: a string representation on Erlang, address family on JS. This type can be directly returned from external functions, as long as they follow the same type rules as ExternalIpAddress.

pub type IpAddress

Values

pub fn address_family(ip: IpAddress) -> AddressFamily

Gets the address family of an IP address.

pub fn enrich(ip: IpAddress) -> IpAddress

Adds missing information to partial representations for repeated access. On Erlang, fills in the string for addresses coming from external functions or prase_ip. One JavaScript, resolves the address family for addresses returned from external functions. If no information is missing, does nothing.

pub fn ip_to_string(ip: IpAddress) -> String

Converts an IP address to a string.

pub fn parse_ip(address: String) -> Result(IpAddress, Nil)

Tries to parse an IP address from a string. In IPv4, leading zeros or short forms are not allowed. This is in order to keep the behavior constant across targets.

pub fn parse_ip_preserving_string(
  address: String,
) -> Result(IpAddress, Nil)

Tires to parse an IP address from a string, and on Erlang, preserves the original format. This will also be more efficient if the IP needs to be represented as a string often on Erlang. On JavaScript, this is equivalent to parse_ip.

pub fn to_external_ip(ip: IpAddress) -> ExternalIpAddress

Gets the target-specific external representation of an IP address.

Search Document