EdgeScript provides utility functions for encoding, random number generation, checksum calculation, and type conversion.
Encoding functions: base64_enc | base64_dec | base64_enc_safe | base64_dec_safe | url_escape | url_unescape
Random number functions: randomseed | rand | rand_hit
Checksum and conversion functions: crc | tonumber
Encoding functions
base64_enc
Encodes a string in Base64.
Syntax
base64_enc(s [, no_padding])Parameters
| Parameter | Type | Required | Description |
|---|---|---|---|
s | String | Yes | The string to encode. |
no_padding | Boolean | No | Set to true to omit padding characters (=). Default: false. |
Return value
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 | Type | Required | Description |
|---|---|---|---|
s | String | Yes | The Base64-encoded string to decode. |
Return value
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 in URL-safe Base64. Compared to base64_enc, this function replaces + with -, replaces / with _, and removes = padding.
Syntax
base64_enc_safe(str)Parameters
| Parameter | Type | Required | Description |
|---|---|---|---|
str | String | Yes | The string to encode. |
Return value
A URL-safe Base64-encoded string (no padding).
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, this function replaces - with + and _ with /, then appends = characters to pad the string to a multiple of four.
Syntax
base64_dec_safe(str)Parameters
| Parameter | Type | Required | Description |
|---|---|---|---|
str | String | Yes | The URL-safe Base64-encoded string to decode. |
Return value
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_escape
Encodes a string using URL encoding (percent-encoding).
Syntax
url_escape(s)Parameters
| Parameter | Type | Required | Description |
|---|---|---|---|
s | String | Yes | The string to encode. |
Return value
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('esdata=', esdata))
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 | Type | Required | Description |
|---|---|---|---|
s | String | Yes | The URL-encoded string to decode. |
Return value
The decoded raw string.
Example
See the url_escape example, which demonstrates encoding and decoding a string in the same script.
Random number functions
Callrandomseed()before usingrand()orrand_hit(). Without seeding, the random number generator may produce the same sequence across requests.
randomseed
Seeds the random number generator. Call this function before rand() or rand_hit().
Syntax
randomseed()Parameters
None.
Return value
None.
Example
randomseed()
r = rand(1, 100)rand
Generates a random number in the range [n1, n2] (inclusive).
Syntax
rand(n1, n2)Parameters
| Parameter | Type | Required | Description |
|---|---|---|---|
n1 | Number | Yes | The lower bound (inclusive). |
n2 | Number | Yes | The upper bound (inclusive). |
Return value
A random number between n1 and n2, inclusive.
Example
randomseed()
r = rand(1, 100) -- returns a random integer such as 42rand_hit
Returns true or false based on the specified probability. Use this function to implement probabilistic traffic sampling or feature rollouts.
Syntax
rand_hit(ratio)Parameters
| Parameter | Type | Required | Description |
|---|---|---|---|
ratio | Number | Yes | 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
true or false.
Example
randomseed()
if rand_hit(80) {
-- executes with approximately 80% probability
}Checksum and conversion functions
crc
Calculates a Cyclic Redundancy Check (CRC) digest for a string.
Syntax
crc(s)Parameters
| Parameter | Type | Required | Description |
|---|---|---|---|
s | String | Yes | The string for which to calculate a CRC digest. |
Return value
The CRC value of the input string.
Example
crc('hello edgescript')tonumber
Converts a string to a number.
Syntax
tonumber(s [, base])Parameters
| Parameter | Type | Required | Description |
|---|---|---|---|
s | String | Yes | The string to convert. |
base | Number | No | The numeric base for conversion. Valid values: 10 (decimal) and 16 (hexadecimal). Default: 10. |
Return value
The numeric value of the input string.
Example
n = tonumber('100')
say(concat('tonumber()=', n))Output:
tonumber()=100