zephyr.net
Outbound network access. `tcp_connect` requires `net:tcp`; `http` requires `net:http`; `download` requires both `net:http` and `io`.
4 functions
tcp_connect
zephyr.net.tcp_connect(host, port, timeout_ms?)Open a TCP connection.
Returns a connection table whose methods use dot notation (no self): write(data), read(n) (reads exactly n bytes), and close().
Parameters
hoststringHost to connect to.
portnumberTCP port.
timeout_msnumberoptionalConnect/read/write timeout. Defaults to 5000.
Returns
A connection table with write, read, and close.
Raises an error if
- the
net:tcppermission is not granted
Example
local conn = zephyr.net.tcp_connect("example.com", 25565, 3000)
conn.write("hello")
local bytes = conn.read(5)
conn.close()resolve_srv
zephyr.net.resolve_srv(name, timeout_ms?)Look up DNS SRV records.
Records are sorted by priority (lowest first), then by weight (highest first), so the first entry is the one to try. A name with no SRV record returns an empty table rather than raising.
Parameters
namestringFully-qualified SRV name, e.g. _minecraft._tcp.example.com.
timeout_msnumberoptionalLookup timeout. Defaults to 5000.
Returns
Array of { target, port, priority, weight }; empty when nothing resolves.
Raises an error if
- the
net:tcppermission is not granted
Example
local records = zephyr.net.resolve_srv("_minecraft._tcp.example.com")
local host, port = "example.com", 25565
if records[1] then
host, port = records[1].target, records[1].port
endhttp
zephyr.net.http(opts)Send an HTTP request.
Supported methods: GET, POST, PUT, DELETE, PATCH. Unknown methods are sent as GET.
Parameters
optstableRequest: url (required), method (default GET), headers (string map), body (string).
Returns
{ status, headers, body }.
Raises an error if
- the
net:httppermission is not granted
Example
local response = zephyr.net.http({
method = "POST",
url = "https://example.com/api",
headers = { ["content-type"] = "application/json" },
body = zephyr.util.json_encode({ ok = true }),
})download
zephyr.net.download(opts)Stream a GET response into the module's zephyr.io sandbox.
The body is never converted to text, so this is the correct API for binaries. The file is written to a temp file and moved into place only on a successful 2xx download; non-2xx responses return bytes = 0 and create no file.
Parameters
optstableRequest: url and path (required), plus optional headers (string map) and max_bytes.
Returns
{ status, headers, path, bytes }. path is set only when a file was written.
Raises an error if
- the
net:httporiopermission is not granted
Example
local result = zephyr.net.download({
url = asset.browser_download_url,
path = "/bin/" .. asset.name,
max_bytes = 50 * 1024 * 1024,
})