zephyr.util

Encoding and JSON helpers. No permissions required.

7 functions

base64_encode

zephyr.util.base64_encode(data)

Base64-encode raw bytes.

Parameters

datastring

Raw bytes to encode.

Returns

string

The Base64 string.

base64_decode

zephyr.util.base64_decode(data)

Decode a Base64 string to raw bytes.

Parameters

datastring

Base64 string to decode.

Returns

string

The decoded raw bytes.

json_encode

zephyr.util.json_encode(value)

Encode a Lua value as a JSON string.

Parameters

valueany

Lua value to encode.

Returns

string

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

jsonstring

JSON string to decode.

Returns

any

The decoded Lua value.

Example

lua
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

string

A hyphenated lowercase UUID v4.

Example

lua
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

string

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

mininteger

Lower bound (inclusive).

maxinteger

Upper bound (inclusive).

Returns

integer

A random integer in [min, max].