EdgeScript is a scripting language for writing custom logic that runs on Alibaba Cloud CDN edge nodes. This reference covers the syntax rules for annotations, identifiers, data types, variables, operators, clauses, and functions.
Annotations
Start each annotation with a number sign (#).
# this is an annotationIdentifiers
Identifiers are case-sensitive and can contain letters, digits, and underscores (_). An identifier cannot start with a digit.
All names — built-in variables, custom variables, built-in functions, and custom functions — must follow these identifier rules.
Data types
EdgeScript supports four literal constant types:
| Type | Syntax | Example |
|---|---|---|
| String | Single quotation marks | 'hello, EdgeScript' |
| Number | Decimal numbers | 10, -99, 1.1 |
| Boolean | Keywords | true, false |
| Dictionary | Square brackets | See below |
Note: Double quotation marks (") are not supported in EdgeScript. Use single quotation marks (') for all string literals.
Dictionary literals
A dictionary can be either index-based or key-based.
Index-based — values are accessed by numeric index:
['key1', 'key2', 100]This creates:
1 -> 'key1'2 -> 'key2'3 -> '100'
Key-based — values are accessed by string key:
['key1' = 'value1', 'key2' = '1000']This creates:
'key1' -> 'value1''key2' -> 1000
An empty string is represented as [].
Variables
A variable is a symbolic name associated with a value that may change.
Reference built-in variables
Reference a built-in variable by name:
hostTo emphasize that a variable is built-in, prefix it with $:
$hostReference custom variables
Reference a custom variable by name:
seckeyNaming constraints
Custom variables cannot share a name with built-in variables. For a list of built-in variables, see EdgeScript built-in variables.
Operators
EdgeScript provides two operators:
| Operator | Purpose | Example |
|---|---|---|
= | Assignment | seckey = 'ASDLFJ234dxvf34sDF' |
- | Unary minus | inum = -10 |
For all other operations — string manipulation, numeric arithmetic, dictionary access — use built-in functions. Built-in functions support String, Number, and Dictionary data types. See Logical functions for the full list.
Examples:
sval = concat(sval, 'trail')
len(arrvar)
seckeys = ['key1', 'key2']Clauses
Condition clause
if condition {
...
}Variants:
# Nested if
if condition1 {
if condition2 {
...
}
}
# if-else
if condition {
...
} else {
...
}A condition can contain a literal constant, a variable, or a function call. The body can be empty or contain multiple statements — one statement per line. Statement nesting is supported.
Coding style: The opening brace ({) must be on the same line as if condition.
for loop
Use for loops to traverse dictionary or array data:
for k, v in <array_or_dict> {
...
}Example 1: Traverse an index-based dictionary
# Traverse an index-based dictionary and return when value equals 'c'
a = ['a', 'b', 'c', 'd']
def for_func () {
for k, v in a {
if eq(v, 'c') {
return true
}
}
}
for_func()Example 2: Traverse a key-based dictionary
# Traverse a key-based dictionary and return when key equals 'c'
a = ['a' = 1, 'b' = 2, 'c' = 3, 'd' = 4, 'e' = 5, 'f' = 6]
def for_func () {
for k, v in a {
if eq(k, 'c') {
return true
}
}
}
for_func()Example 3: Nested loops
# Nested loops across three index-based dictionaries
num = 0
def for_func () {
a = [0,1,2,3,4,5,6,7,8,9]
for k ,v in a {
b = [0,1,2,3,4,5,6,7,8,9]
for k1 ,v1 in b {
c = [0,1,2,3,4,5,6,7,8,9]
for k2 ,v2 in c {
num = add(num, 1)
if and(eq(v, 3), eq(v1, 5), eq(v2, 7)) {
return true
}
}
}
}
}
for_func()Coding style: The opening brace ({) must be on the same line as the for statement.
Functions
Define a function
def function_name(parameter_list) {
...
}The parameter list can be empty or contain multiple parameters separated by commas (
,).The body can be empty or contain multiple statements — one statement per line.
Use
returnto return a value and exit the function.
Coding style: The opening brace ({) must be on the same line as the function definition.
Call a function
Use the function name followed by parentheses for both built-in and custom functions:
function_name()Limitations
| Limitation | Workaround |
|---|---|
Double quotation marks (") are not supported | Use single quotation marks (') for all string literals |
for loops only traverse Dictionary or Array data | Use condition clauses for other control flow |
break is not supported in for loops | Define a custom function and use return to exit the loop early |