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

Support for Node.js #5

Open
johnspackman opened this issue Sep 30, 2017 · 5 comments
Open

Support for Node.js #5

johnspackman opened this issue Sep 30, 2017 · 5 comments

Comments

@johnspackman
Copy link

I have a large Java server application that I would love to port to NodeJS but it’s just too big to start that project - assuming that I replaced Tomcat with ExpressJS, are there any restrictions that would prevent NodeJS development?

Although CheerpJ is billed as "on the fly", is it possible to save the generated Javascript and then ditch the Java altogether - making the Javascript code the main repo that is edited and developed going forward?

@reportmill
Copy link

reportmill commented Sep 30, 2017 via email

@bates64
Copy link
Contributor

bates64 commented Jan 26, 2024

Node.js is not yet supported in CheerpJ 3 due to licensing restrictions - you can't self-host. You would be able to work around this using the --experimental-network-imports Node.js flag and import("https://.../cj3.js), but cj3.js expects that it can use fs to read the Wasm file in non-browser contexts, which doesn't work since it's a network module. We might look into changing this in the future so I'll keep this issue open as a feature request.

@bates64 bates64 changed the title Is it possible to use CheerpJ for server (ie NodeJS) applications? Support for Node.js Jan 26, 2024
@bates64
Copy link
Contributor

bates64 commented Jan 26, 2024

I managed to get CheerpJ loaded from the CDN in Node.js using the following module and the --experimental-network-imports flag:

const loaderPath = "https://cjrtnc.leaningtech.com/3.0rc2_305";

const [module, buffer] = await Promise.all([
  import(loaderPath + "/cj3.js"),
  fetch(loaderPath + "/cj3.wasm").then(r => r.arrayBuffer()),
]);
const exports = await module.default({ buffer });

export default async function init(options) {
  await exports.cj3Init(options, loaderPath, exports); // error!
  return {
    runMain: exports.cjRunMain,
    runJar: exports.cjRunJar,
    runLibrary: exports.cjRunLibrary,
    createDisplay: exports.cjCreateDisplay,
    fileBlob: exports.cjFileBlob,
    getRuntimeResources: exports.cjGetRuntimeResources,
    getProguardConfiguration: exports.cjGetProguardConfiguration,
    dumpMethod: exports.dumpMethod,
    dumpClass: exports.dumpClass,
    dumpAllThreads: exports.dumpAllThreads,
  };
}

This currently throws an error because CheerpJ believes it's in a browser context and tries to do stuff with self and document. If you stub these it hangs. With a bit of development work we can probably get this working, at least for features like library mode (obviously, applications that use a display won't work).

@JoseVSeb
Copy link

@bates64 did you get this working?

@bates64
Copy link
Contributor

bates64 commented Mar 4, 2024

Not yet @JoseVSeb.

As a workaround, on native platforms like those that run Node.js, you should be able to run Java code with a standard JVM/JRE (e.g. OpenJDK). Perhaps check out tools like https://github.com/joeferner/node-java (note: I haven't played with this at all, but it seems like what you might want?).

However, approaches like these this won't work if you're in JS/Wasm-only environments like some FaaS platforms. You also have no sandboxing guarantees, so running untrusted user code is out of the question.

We might be able to allocate serious development time to getting CheerpJ 3 working with Node.js/Deno/Bun/etc at some point in the future, but sadly we can't give any timescale guarantees. If you have a commercial use case in mind feel free to get in contact and we can work out a way of sponsoring development on this front.

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

No branches or pull requests

4 participants