This page covers utility functions available in EdgeScript: encoding and decoding, random number generation, checksums, type conversion, and unique identifier generation.
Base64 encoding and decoding
EdgeScript provides two Base64 variants: standard Base64 (base64_enc / base64_dec) and URL-safe Base64 (base64_enc_safe / base64_dec_safe). Use the standard variant for general encoding; use the URL-safe variant when the encoded string appears in URLs or HTTP headers.
base64_enc
Encodes a string using standard Base64 encoding.
Syntax
base64_enc(s [, no_padding])Parameters
| Parameter | Description |
|---|---|
s | The string to encode. |
no_padding | Specifies whether to omit padding characters. Set to true to remove trailing = characters. Default: false. |
Return value
Returns a Base64-encoded string.
Example
if $http_data {
decdata = base64_dec($http_data)
say(concat('base64_decdata=', decdata))
say(concat('base64_encdata=', base64_enc('hello, dsl')))
}Request header: data: aGVsbG8sIGRzbA==
Output:
base64_decdata=hello, dsl
base64_encdata=aGVsbG8sIGRzbA==base64_dec
Decodes a Base64-encoded string.
Syntax
base64_dec(s)Parameters
| Parameter | Description |
|---|---|
s | The Base64-encoded string to decode. |
Return value
Returns the decoded raw string.
Example
if $http_data {
decdata = base64_dec($http_data)
say(concat('base64_decdata=', decdata))
say(concat('base64_encdata=', base64_enc('hello, dsl')))
}Request header: data: aGVsbG8sIGRzbA==
Output:
base64_decdata=hello, dsl
base64_encdata=aGVsbG8sIGRzbA==base64_enc_safe
Encodes a string using URL-safe Base64 encoding. In the output, + is replaced by -, / is replaced by _, and = padding characters are removed.
Syntax
base64_enc_safe(str)Parameters
| Parameter | Description |
|---|---|
str | The string to encode. |
Return value
Returns a URL-safe Base64-encoded string.
Example
add_rsp_header('X-RESPOND-OUTPUT', concat('base64_enc_safe=', base64_enc_safe('hello, dsl')), true)Response header:
X-RESPOND-OUTPUT: base64_enc_safe=aGVsbG8sIGRzbAbase64_dec_safe
Decodes a URL-safe Base64-encoded string. Before decoding, - is replaced by + and _ is replaced by /. Padding characters (=) are appended as needed to make the string length a multiple of four.
Syntax
base64_dec_safe(str)Parameters
| Parameter | Description |
|---|---|
str | The URL-safe Base64-encoded string to decode. |
Return value
Returns the decoded raw string.
Example
add_rsp_header('X-RESPOND-OUTPUT', concat('base64_dec_safe=', base64_dec_safe(base64_enc_safe('hello, dsl'))), true)Response header:
X-RESPOND-OUTPUT: base64_dec_safe=hello, dslURL encoding and decoding
url_escape
Encodes a string using URL encoding (percent-encoding).
Syntax
url_escape(s)Parameters
| Parameter | Description |
|---|---|
s | The string to encode. |
Return value
Returns a URL-encoded string.
Example
raw = '/abc/123/ dd/file.m3u8'
esdata = url_escape(raw)
dsdata = url_unescape(esdata)
if eq(raw, dsdata) {
say(concat('raw=', raw))
say(concat('dsdata=', dsdata))
}Output:
raw=/abc/123/ dd/file.m3u8
esdata=%2Fabc%2F123%2F%20dd%2Ffile.m3u8
dsdata=/abc/123/ dd/file.m3u8url_unescape
Decodes a URL-encoded string.
Syntax
url_unescape(s)Parameters
| Parameter | Description |
|---|---|
s | The URL-encoded string to decode. |
Return value
Returns the decoded raw string.
Example
raw = '/abc/123/ dd/file.m3u8'
esdata = url_escape(raw)
dsdata = url_unescape(esdata)
if eq(raw, dsdata) {
say(concat('raw=', raw))
say(concat('dsdata=', dsdata))
}Output:
raw=/abc/123/ dd/file.m3u8
esdata=%2Fabc%2F123%2F%20dd%2Ffile.m3u8
dsdata=/abc/123/ dd/file.m3u8Random number generation
randomseed
Initializes the random number generator. Call this before rand to ensure different random sequences across requests.
Syntax
randomseed()Parameters
None.
Return value
None.
Example
randomseed()
r = rand(1, 100)rand
Returns a random number within a specified range (inclusive).
Syntax
rand(n1, n2)Parameters
| Parameter | Description |
|---|---|
n1 | The lower bound. |
n2 | The upper bound. |
The returned value satisfies n1 <= result <= n2.
Return value
Returns a random number.
Example
r = rand(1, 100)rand_hit
Returns true or false based on the specified probability. Use this to implement percentage-based traffic splitting or feature rollouts.
Syntax
rand_hit(ratio)Parameters
| Parameter | Description |
|---|---|
ratio | The probability of returning true. Valid values: 0–100. A value of 100 always returns true; a value of 0 always returns false. |
Return value
Returns true or false.
Example
rand_hit(80)rand_bytes
Generates a random numeric string of the specified length.
Syntax
rand_bytes(len)Parameters
| Parameter | Description |
|---|---|
len | The length of the random numeric string to generate. |
Return value
Returns the generated random numeric string.
Example
rand_bytes(16)Checksum
crc
Calculates a Cyclic Redundancy Check (CRC) value for a string.
Syntax
crc(s)Parameters
| Parameter | Description |
|---|---|
s | The string for which to calculate the CRC value. |
Return value
Returns the CRC value of the input string.
Example
crc('hello edgescript')Type conversion
tonumber
Converts a string to a numeric type.
Syntax
tonumber(s [, base])Parameters
| Parameter | Description |
|---|---|
s | The string to convert. |
base | The base for numeric conversion. Valid values: 10 (decimal) and 16 (hexadecimal). Default: 10. |
Return value
Returns a number.
Example
n = tonumber('100')
say(concat('tonumber()=', n))Output:
tonumber()=100Unique identifier
uuid
Returns a randomly generated UUID string.
Syntax
uuid()Parameters
None.
Return value
Returns a UUID string. Example: 16903a86-4173-4dea-842c-926c5860fe05.
Example
say(uuid())Output:
16903a86-4173-4dea-842c-926c5860fe05