Skip to main content

Script Configuration

Egern allows users to flexibly control network request/response handling and scheduled tasks by writing JavaScript scripts. Through these scripts, users can customize the handling of network requests and responses as needed or set up scheduled tasks to execute certain operations.

Configuration Details

The scriptings configuration type includes three types of scripts: HTTP request scripts (http_request), HTTP response scripts (http_response), and scheduled task scripts (schedule). Each type of script shares some common configuration fields and also has some specific configuration fields.

The following table lists all the configuration fields:

Field NameTypeRequiredDescriptionApplicable Script Types
namestringYesThe name of the script.All
matchstring (RegEx)NoThe URL regex pattern where the script should be applied.http_request, http_response
script_urlstringYesThe URL of the script, can be a local path or a remote link.All
update_intervalintegerNoWhen the script is a remote link, this sets the interval (in seconds) for updating the script. Default is 86400 (one day).All
timeoutintegerNoThe timeout for script execution in seconds. Default is 30.All
max_sizeintegerNoSets the maximum message body size in bytes. If the message body exceeds this value, the script will not be invoked.http_request, http_response
debugbooleanNoWhether to enable debugging. If enabled, Egern will print more debug information.All
body_requiredbooleanNoSpecifies whether the message body is required. If set to true, the script will only be invoked if the message body exists.http_request, http_response
cronstringNoSets the execution time of the scheduled task using a Cron expression.schedule

Configuration Example

Below is an example of a scriptings type configuration:

scriptings:
- http_request:
name: "Example Request Script"
match: "example\\.com"
script_url: "scripts/request.js"
update_interval: 86400
timeout: 30
max_size: 131072
debug: false
body_required: true
- http_response:
name: "Example Response Script"
match: "example\\.com"
script_url: "scripts/response.js"
update_interval: 86400
timeout: 30
max_size: 131072
debug: false
body_required: true
- schedule:
name: "Example Scheduled Task"
cron: "0 0 * * *"
script_url: "scripts/task.js"
update_interval: 86400
timeout: 30
debug: false

In this configuration, we have three scripts: one to handle all HTTP requests matching example.com, one to handle all HTTP responses matching example.com, and one scheduled task that runs daily at midnight. Each script has its own URL, and Egern will update these scripts daily from their URLs.