Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add duk_module_node to module app_jsdt #3038

Merged

Conversation

xantek-imc
Copy link
Contributor

Pre-Submission Checklist

  • Commit message has the format required by CONTRIBUTING guide
  • Commits are split per component (core, individual modules, libs, utils, ...)
  • Each component has a single commit (if not, squash them into one commit)
  • 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)
  • New feature (non-breaking change which adds new functionality)
  • Breaking change (fix or feature that would change existing functionality)

Checklist:

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.

- reported by GH kamailio#3037
- cb_resolve_module: resolves absolute and relative paths
  resolves absolute if request_id starts with a /
  if parent_id then resolved relative path based parent_id path
  if no parent_id resolves relative path based on app_jsdt load file path
  request_id without /, ./, or ../ are not resolved and return error
- cb_load_module: use jsdt_load_file to load resolved file
- see: https://github.com/svaarala/duktape/tree/master/extras/module-node
@miconda
Copy link
Member

miconda commented Feb 23, 2022

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)?

@miconda
Copy link
Member

miconda commented Feb 25, 2022

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.

@miconda miconda marked this pull request as ready for review February 25, 2022 07:17
@miconda miconda merged commit 4a2415e into kamailio:master Feb 25, 2022
@xantek-imc
Copy link
Contributor Author

  • 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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants