<!-- Kamailio Pull Request Template -->
#### Pre-Submission Checklist <!-- Go over all points below, and after creating the PR, tick all the checkboxes that apply --> <!-- All points should be verified, otherwise, read the CONTRIBUTING guidelines from above--> <!-- If you're unsure about any of these, don't hesitate to ask on sr-dev mailing list --> - [X] Commit message has the format required by CONTRIBUTING guide - [X] Commits are split per component (core, individual modules, libs, utils, ...) - [X] Each component has a single commit (if not, squash them into one commit) - [X] No commits to README files for modules (changes must be done to docbook files in `doc/` subfolder, the README file is autogenerated)
#### Type Of Change - [ ] Small bug fix (non-breaking change which fixes an issue) - [X] New feature (non-breaking change which adds new functionality) - [ ] Breaking change (fix or feature that would change existing functionality)
#### Checklist: <!-- Go over all points below, and after creating the PR, tick the checkboxes that apply --> - [X] PR should be backported to stable branches - [X] Tested changes locally - [X] Related to issue #3037
#### Description I can not remember the last time I wrote any C which was likely a class assignment last century. Anyways I came across this issue and it seemed in reach to get the ball rolling. I know this PR needs more work but my goal is to share my progress to help move this feature forward.
Based on https://github.com/svaarala/duktape/tree/master/extras/module-node added: - duk_module_node.h - duk_module_node.c
updated: - app_jsdt_api.h - app_jsdt_api.c
Included the extra header and initialize the bindings after 'load JS context' was created.
added `cb_resolve_module` to resolve the full file system path for requested_id and parent_id added `cb_load_module` function which uses `jsdt_load_file` to load resolved file
With absolute and relative modules working I met my current requirements to allow breaking routing into multiple smaller files.
You can view, comment on, or merge this pull request online at:
https://github.com/kamailio/kamailio/pull/3038
-- Commit Summary --
* app_jsdt: add duk_module_node for JS module resolution
-- File Changes --
M src/modules/app_jsdt/app_jsdt_api.c (65) M src/modules/app_jsdt/app_jsdt_api.h (3) A src/modules/app_jsdt/duk_module_node.c (333) A src/modules/app_jsdt/duk_module_node.h (17)
-- Patch Links --
https://github.com/kamailio/kamailio/pull/3038.patch https://github.com/kamailio/kamailio/pull/3038.diff
Thanks! Upon a quick look at the code changes to existing app_jsdt code, all seems ok, so I am going to merge it soon if no other comments.
A few remarks/questions to clarify better what this brings in:
* one can load another JS file in the Kamailio KEMI JS file, but this another JS file has its content following the node.js module specs, right? It cannot be just any JS code * does it allow loading any node.js modules available out there that do not require the node.js core modules (which are embedded (compiled) in node.js binary)?
I am merging it. Afterwards, I will add a modparam to control initializing this extension, just in case has unexpected side effects in some cases.
Merged #3038 into master.
- one can load another JS file in the Kamailio KEMI JS file, but this another JS file has its content following the node.js module specs, right? It cannot be just any JS code
I think the require() must be a node.js, but not sure from my reading of https://github.com/svaarala/duktape/tree/master/extras/module-node. My local testing looks like this:
``` var test_include = require('./test');
function ksr_request_route() { test_include(); ```
./test.js ``` module.exports = function test_include() { KSR.info('LOG FROM INCLUDED test FUNCTION--------'); }; ``` My testing includes do not work as expected without module.exports.
---
- does it allow loading any node.js modules available out there that do not require the node.js core modules (which are embedded (compiled) in node.js binary)?
I tried testing using a simple NPM module with a relative include and kamailio would not start with error ` jsdt_kemi_load_script(): failed running: SyntaxError: expected identifier` I think this indicates as currently implemented not just any node.js module can be used. This PR does not try to resolve package names and only tries to resolve relative and absolute files.