Table of Contents
List of Examples
load
parametermode
parameterjsdt_dofile
usagejsdt_dostring
usagejsdt_run
usagejsdt_runstring
usageTable of Contents
This module allows executing JavaScript scripts from the Kamailio configuration file. It exports all KEMI functions to JavaScript in order to access the currently processed SIP message. These functions are named within the JavaScript object 'KSR'.
The module is based on the Duktape JavaScript engine (http://www.duktape.org), which is a fast and easy to embed JavaScript interpreter. The exported API from Kamailio to JavaScript is documented in the wiki.
The module has two JavaScript contexts:
first is used for functions jsdt_dofile() and jsdt_dostring().
second is used for function jsdt_run() and parameter 'load'. Therefore jsdt_run() cannot execute functions from scripts loaded via jsdt_dofile() in config. This is kind of caching mode, avoiding reading file every time, but you must be sure you do not have something that is executed by default and requires access to SIP message. This environment is also used by KEMI framework for the config SIP routing functions.
Set the path to the JavaScript file to be loaded at startup. Then you can use jsdt_run(function, params) to execute a function from the script at runtime. If you use it for KEMI configuration, then it has to include the required functions.
Default value is “null”.
Example 1.1. Set load
parameter
... modparam("app_jsdt", "load", "/usr/local/etc/kamailio/js/myscript.js") ...
Execute the JavaScript script stored in 'path'. The parameter can be a string with pseudo-variables evaluated at runtime.
Execute the JavaScript script stored in parameter. The parameter can be a string with pseudo-variables.
Example 1.4. jsdt_dostring
usage
... if(!jsdt_dostring('KSR.dbg("test message\n")')) { xdbg("SCRIPT: failed to execute js script!\n"); } ...
Execute the JS function 'func' giving params as parameters. There can be up to 3 string parameters. The function must exist in the script loaded at startup via parameter 'load'. Parameters can be strings with pseudo-variables that are evaluated at runtime.
Example 1.5. jsdt_run
usage
... if(!jsdt_run("js_append_fu_to_reply")) { xdbg("SCRIPT: failed to execute js function!\n"); } ... jsdt_run("js_funcx", "$rU", "2"); ...
Execute the JS script stored in parameter. The parameter can be a string with pseudo-variables. The script is executed in JS context specific to loaded JS files at startup.
Example 1.6. jsdt_runstring
usage
... if(!jsdt_runstring('KSR.dbg("Hello World from $fU\n")')) { xdbg("failed to execute js script!\n"); } ...
Marks the need to reload the js script. The actual reload is done by every working process when the next call to jsdt_run() function or KEMI config is executed.
Name: app_jsdt.reload
Parameters: none
Example:
... kamcmd app_jsdt.reload ...
Create your JS script and store it on the file system, say: '/usr/local/etc/kamailio/js/myscript.js'.
... function sr_append_fu_to_reply() { KSR.hdr.append_to_reply("P-From: " + KSR.pv.get("$fu") + "\r\n"); } ...
Load the script via parameter 'load' and execute function via jsdt_run(...).
... modparam("app_jsdt", "load", "/usr/local/etc/kamailio/js/myscript.js") ... request_route { ... if(!jsdt_run("sr_append_fu_to_reply")) { xdbg("SCRIPT: failed to execute js function!\n"); } ... } ...