zephyr.util
Encoding and JSON helpers. No permissions required.
7 functions
base64_encode
zephyr.util.base64_encode(data)Base64-encode raw bytes.
Parameters
datastringRaw bytes to encode.
Returns
The Base64 string.
base64_decode
zephyr.util.base64_decode(data)Decode a Base64 string to raw bytes.
Parameters
datastringBase64 string to decode.
Returns
The decoded raw bytes.
json_encode
zephyr.util.json_encode(value)Encode a Lua value as a JSON string.
Parameters
valueanyLua value to encode.
Returns
The JSON string.
json_decode
zephyr.util.json_decode(json)Decode a JSON string into a Lua value.
Decoded through serde JSON. Note: JSON null maps to mlua's null userdata, not Lua nil.
Parameters
jsonstringJSON string to decode.
Returns
The decoded Lua value.
Example
local data = zephyr.util.json_decode(response.body)uuid
zephyr.util.uuid()Generate a random UUID v4 string.
Uses the host OS CSPRNG. Prefer this over math.random for ids: the sandbox's Lua RNG is not cryptographic and may not be uniquely seeded per process.
Returns
A hyphenated lowercase UUID v4.
Example
local id = zephyr.util.uuid()random_id
zephyr.util.random_id(length?)Generate a short, random, URL-safe id.
Drawn from a 32-character unambiguous alphabet (no l, o, 0, 1). Cryptographically random. length defaults to 16 and is clamped to 1..=64.
Parameters
lengthinteger?Number of characters (default 16).
Returns
The random id.
random_int
zephyr.util.random_int(min, max)Generate a uniform random integer in an inclusive range.
Unbiased, backed by the host OS CSPRNG. Errors if min > max.
Parameters
minintegerLower bound (inclusive).
maxintegerUpper bound (inclusive).
Returns
A random integer in [min, max].