glip

Package Version Hex Docs

gleam add glip@1

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).

import gleam/bool
import gleam/io
import glip.{type ExternalIpAddress}

pub fn main() {
  let assert Ok(ip) = glip.parse_ip("127.0.0.1")
  let result = is_ipv4(glip.to_external_ip(ip))
  io.println(glip.ip_to_string(ip) <> " is IPv4: " <> bool.to_string(result))
}

/// Glip is designed to be FFI friendly and efficient!
@external(erlang, "inet", "is_ipv4_address")
@external(javascript, "node:net", "isIPv4")
fn is_ipv4(ip: ExternalIpAddress) -> Bool

Further documentation can be found at https://hexdocs.pm/glip.

Development

Both of the following work also with the --target javascript option!

gleam test  # Run the tests
gleam dev   # Run the example code
Search Document