Skip to content

Calling QuickJS addon modules implemented as shared objects #968

@chrisemunt

Description

@chrisemunt

Using the Nginx binding to QuickJS, it doesn't appear to be possible to call addon modules implemented as shared objects. I created a very simple QuickJS extension called 'cmtest.so' which works fine when called through a standalone JavaScript file. For example, extending your simple 'main.js' example (from https://blog.nginx.org/blog/quickjs-engine-support-for-njs) to call this addon:

// main.js
import * as x from '/opt/quickjs/cm/cmtest.so'
function handler(r) {
r.return(200, "This is the response from the QuickJS code module " + x.strtest("test string"));
}
export default { handler };

When Nginx is configured to call this handler (using the configuration in the documentation), Nginx fails to start with the following error message:

nginx: [emerg] js compile SyntaxError: unexpected token in expression: '
at /opt/quickjs/cm/cmtest.so:1:1

Of course, if I remove all references to the addon and its functions it works fine.

The original code (with addon) will work fine if it's called through a standalone JavaScript code module (i.e. outside of Nginx). I used the following JavaScript code harness to verify that it does work.

import foo from '/opt/quickjs/cm/main.js';
class mgserver {
constructor() {}
return(code, text) {
console.log("code: " + code + "; result: " + text);
}
};
let srv = new mgserver();
foo(srv);

Some background to this request. I have a JavaScript addon (written in C) that binds to a number of databases. Currently this module is available for Node.js, Deno and Bun. I would like to make it available for QuickJS and, ideally, be able to call its functionality through Nginx/QuickJS too.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions