MCPcopy Index your code
hub / github.com/apigee/trireme

github.com/apigee/trireme @trireme-0.9.4

Chat with this repo
repository ↗ · DeepWiki ↗ · release trireme-0.9.4 ↗ · + Follow
6,492 symbols 27,829 edges 2,352 files 582 documented · 9%
What it actually does AI analysis from the code graph — generated when you open this
loading…
README

Trireme

This is a set of libraries for running node.js scripts inside Java.

What is Trireme?

Trireme runs Node.js scripts inside the JVM. This is important because there is a lot of software out there (including our own) that is built in Java and isn't going to get rewritten in JavaScript now or in the future.

Trireme is specifically designed to be embeddable within any Java program. There is a lot of support inside Trireme for this specific case:

  • Many Node.js scripts can run inside a single JVM, subject to memory constraints.
  • Each script is totally isolated from the others -- there is no way for one script to affect the heap of the others.
  • A sandbox is provided that lets the container control how, or if, the script gains access to the filesystem and to the network.
  • The HTTP server implementation is pluggable. An "HTTP Adapter" is supported that allows a container to embed a Node.js script inside an existing HTTP container like a web server or other product. (A sample adapter, built using Netty 4.0, is included.)
  • The sandbox supports a Rhino feature that makes it possible to limit the execution time for a script. With this feature enabled, a script that runs an infinite loop can be terminated after some time.

For a more detailed introduction, see our intro presentation:

So, again, why would I use Trireme?

  • To embed Node.js apps inside an existing Java application
  • To run Node.js apps that take advantage of Java libraries you can't live without, like JDBC drivers and XML parsers

If neither of those reasons apply to you, then stick with "regular node!"

How do I get it?

From NPM

sudo npm install -g trireme
trireme -h
trireme <your script name here>

The NPM package for Trireme lets you run it on the command line just like "node".

Unfortunately, Trireme does not support the "repl" yet (and it's hard since Java gives us limited control over the TTY) so just running "trireme" with no arguments produces an error right now.

From Maven Central

The best reason to use Trireme is because it's important to embed Node.js code inside an existing Java application. In that case you will use the modules under "io.apigee.trireme" on Maven Central:

io.apigee.trireme

The "module map" later in this document shows which modules to use in which cases.

From GitHub

See the releases page to download the latest release files.

"trireme-x.y.z.jar" is always a stand-alone jar that you can run just like "node":

java -jar trireme-x.y.z.jar script.js

What version of Node.js does Trireme Support?

Trireme supports two versions of Node.js:

  • 0.10.32. This is the default, fully-supported version.
  • 0.12.7. This version is still a work in progress.

Support for Node.js 4.0 depends on more complete ES6 code in Rhino. The Rhino community is making progress on this but it will be quite some time before we are ready to support 4.0.

Running Trireme

Using NPM

If you installed Trireme using NPM, just run:

trireme <script name>

Trireme will execute your script just like Node.

In addition, the environment variable TRIREME_CLASSPATH may be used to add extra JARs or directories to the classpath used to run Trireme. Anything on this path will be appended to the classpath used to launch Trireme. This allows you to add JDBC drivers, etc.

For help, use:

trireme -h

Using Java

The "jar" module builds a self-contained JAR that may be used to launch Trireme on the command line just like regular Node.js:

mvn install
java -jar jar/target/trireme.X.Y.Z.jar <script name>

(and with no arguments it will launch the "repl" but that implementation is not complete)

For Apigee Edge Customers

Node.js apps on Apigee Edge run inside Trireme using a few specific settings that make them slightly different from the standard Trireme runtime. In particular, an "HTTP adaptor" is used, which is not 100% the same as the standard HTTP module. This does not affect most applications, but it may affect applications that depend on undocumented or internal functions and properties of the default HTTP module. (While this is a poor programming practice, it's quite common.)

In order to test applications that will run on Apigee Edge in the most compatible environment, the module "samples/apigee-edge-like-runner" will build a self-contained JAR that sets up Trireme this way. Use it just like the "Using Java" link above:

mvn install
java -jar samples/apigee-edge-like-runner/target/apigee-edge-like-launcher-X-Y-Z-SNAPSHOT.jar <script name>

Embedding Trireme as a servlet

The war sample is a sample that shows how to assemble a Node.js application into a WAR file. It uses the trireme-servlet module to link the servlet to the Node.js script. Any script that operates as an HTTP server using the "http" module can be embedded in this way.

Embedding Trireme Anywhere Else that Java Runs

There is JavaDoc for the "NodeEnvironment" and "NodeScript" classes, and many other features. Here are the basics:

import io.apigee.trireme.core.NodeEnvironment;
import io.apigee.trireme.core.NodeScript;

// The NodeEnvironment controls the environment for many scripts
NodeEnvironment env = new NodeEnvironment();

// Pass in the script file name, a File pointing to the actual script, and an Object[] containg "argv"
NodeScript script = env.createScript("my-test-script.js",
                                     new File("my-test-script.js"), null);

// Wait for the script to complete
ScriptStatus status = script.execute().get();

// Check the exit code
System.exit(status.getExitCode());

Selecting the Node version

The all-in-one JAR, and the "trireme" NPM package, include code for both Node.js 0.10 and 0.12. To select a version from the command-line, use the "--node-version" option, like this:

trireme --node-version=0.12 foo.js

When embedding Trireme, select the version using the "setNodeVersion" method in the NodeScript class.

With this scheme, both versions of Node can run inside the same JVM.

The version numbers are "semver-style" although they do not support every single feature of semver. The best bet is to use "0.10" and "0.12" to select each.

Trireme Extensions

There are a few NPM modules that only work in Trireme. These allow access to features of the Java platform that are normally accessed via native code in regular Node.js. These modules are as follows:

  • trireme-jdbc: This module provides access to JDBC drivers from inside Node.js code. This makes it possible to use databases that have excellent JDBC drivers (such as Oracle) without compiling any native code.
  • trireme-xslt: This module provides access to the XSLT processor inside the Java platform, which is faster and can support more of the XSLT standard than any of the native Node.js options.
  • trireme-support: Additional Trireme-specific support functions. These include the ability to detect if Trireme is being used, and the ability to dyamically load additional Node.js modules from a JAR file.

Logging

Trireme uses slf4j for logging the stuff that is happening in Java. The pre-built JAR, and the NPM "trireme" wrapper, use "slf4j-simple". To turn on debug logging, set the system property "org.slf4j.simpleLogger.defaultLogLevel" to "debug". (Or "trace" for even more output.)

When embedding trireme, you can use any SLF4J-compatible logging framework you wish, such as logback.

How Complete is Trireme?

Trireme supports most of the Node.js APIs and passes much of the Node.js test suite.

The table below shows each module and its status. "Complete" means that a module is functionally complete, although it may not necessarily pass all the node.js tests.

ModuleStatusSource
assertCompletenode.js
child_processPartialTrireme
clusterNot Implemented Yetnode.js
consoleCompletenode.js
cryptoCompletenode.js + Trireme
debuggerNot Supported
dgramCompletenode.js + Trireme
dnsCompleteTrireme
domainCompletenode.js + Trireme
eventsCompletenode.js
fsCompletenode.js + Trireme
globalsCompletenode.js + Trireme
httpCompletenode.js + Trireme
httpsComplete but See NotesTrireme
moduleCompletenode.js
netCompletenode.js + Trireme
osPartialTrireme
pathCompletenode.js
processCompleteTrireme
punycodeCompletenode.js
querystringCompletenode.js
readlinePartialnode.js
replNot Supportednode.js
streamCompletenode.js
string_decoderCompletenode.js
timersCompletenode.js + Trireme
tlsComplete but See NotesTrireme
ttyCompleteTrireme
urlCompletenode.js
utilCompletenode.js
vmCompleteTrireme
zlibCompleteTrireme

What are the Major Differences with "real" node.js?

A few of the modules are different, some in major ways:

JavaScript Language

Trireme runs in the JVM on Rhino, which is the most complete JavaScript implementation for the JVM and the one that works on the largest variety of Java distributions. The latest version of Rhino has many of the new JavaScript language features that Node users are used to, but still does not have all the features that are supported when the "--harmony" flag is set.

For specifics, see the Rhino compatibility table. Trireme is currently using RHino 1.7.7.

Most of the time the differences between V8 and Rhino do not affect Node.js code, but occasionally there is a problem. We would love some help from the Rhino community to start to address these differences.

TLS/SSL and HTTPS

Trireme uses Java's standard "SSLEngine" for TLS/SSL and HTTPS support, whereas standard Node.js uses OpenSSL. The TLS implementation in Node.js is a fairly thin layer on top of OpenSSL and we chose not to try and replicate this in Java.

For the most part, TLS and HTTPS in Trireme will work just like they do in Node.js. However, they SSLEngine and OpenSSL are not exactly the same. There are a few differences:

1) Most notably, especially with Java 7, SSLEngine supports a different set of cipher suites, particularly the various elliptical curve ciphers. There are ciphers in common (otherwise almost everything will break) but there are many that are not. Many Node.js tests that rely on older cipher suites using DES or RC4 will not run on Trireme because many of these older and weaker cipher suites are disabled by default in Java. However, "OpenSSL style" names work in Trireme just as they do in regular Node and if the JVM supports a particular cipher suite from OpenSSL, you will get the same one in Trireme.

2) Java handles SSL sessions differently, and gives the user less control about it. Right now, Trireme is unable to support the ability of a TLS or HTTPS client to retrieve the session from an existing connection and re-use it for another TCP connection.

3) Java also will produce different certificate validation errors than OpenSSL does. The errors will still come in the same places and for the same reasons, but if your code depends on a specific error message, it will likely get a different one.

4) Java's SSLEngine relies on its own "keystore" files, whereas OpenSSL can operate on a variety of files but typically processes PEM files. Trireme handles this disparity by using the "Bouncy Castle" crypto framework to translate PEM files into keys and certificates that SSLEngine can understand. In addition, you can also use regular Java keystore files, as described below.

In order to support TLS and HTTPS using PEM files, the "trireme-crypto" module and its dependencies (Bouncy Castle) must be in the class path. If they are not present, then TLS is still available, but it will only work with Java keystore files (see below) or without using any keys at all. Trireme checks for this dependency at runtime, so it is simply a matter of including it on the class path, since it will fail at runtime if the dependency is needed, and work otherwise.

(For instance, Trireme can still execute a Node program that acts as an HTTPS client using only default certificates without requiring trireme-crypto. But if it needs to validate a particular CA certificate or if it needs to use a client-side certificate then trireme-crypto is also necessary.)

In addition, the TLS and HTTPS-related methods in Trireme can use a Java keystore instead of PEM files. There are three parameters that are relevant here:

  • keystore: The file name of a Java

Extension points exported contracts — how you extend this code

NodeModule (Interface)
This is a superclass for all modules that may be loaded natively in Java. All modules with this interface listed in META [56 …
core/src/main/java/io/apigee/trireme/core/NodeModule.java
BiCallback (Interface)
A generic one-function interface that can be used to represent various callbacks. [11 implementers]
kernel/src/main/java/io/apigee/trireme/kernel/BiCallback.java
NodeScriptModule (Interface)
This interface is used to write new modules in JavaScript that are plugged in to Noderunner as built-in modules. Impleme [6 …
core/src/main/java/io/apigee/trireme/core/NodeScriptModule.java
TriCallback (Interface)
A generic one-function interface that can be used to represent various callbacks. [11 implementers]
kernel/src/main/java/io/apigee/trireme/kernel/TriCallback.java
InternalNodeModule (Interface)
A subclass of NodeModule that denotes modules that are loaded using the "process.binding" method. This is useful when cr [37 …
core/src/main/java/io/apigee/trireme/core/InternalNodeModule.java
Callback (Interface)
A generic one-function interface that can be used to represent various callbacks. [11 implementers]
kernel/src/main/java/io/apigee/trireme/kernel/Callback.java
ScriptTask (Interface)
Code that runs outside the main Node thread can send tasks to a script by sending these types of objects. [35 implementers]
core/src/main/java/io/apigee/trireme/core/ScriptTask.java
IOCompletionHandler (Interface)
This is a generic interface that gets invoked for various I/O completion events. It always has an integer error code, an [15 …
kernel/src/main/java/io/apigee/trireme/kernel/handles/IOCompletionHandler.java

Core symbols most depended-on inside this repo

Shape

Method 3,444
Function 2,555
Class 452
Interface 35
Enum 6

Languages

Java57%
TypeScript38%
Python4%
C++1%
C1%
Ruby1%

Modules by API surface

nodetests/src/test/python/test.py163 symbols
core/src/main/java/io/apigee/trireme/core/internal/ScriptRunner.java104 symbols
core/src/main/java/io/apigee/trireme/core/modules/HTTPWrap.java75 symbols
kernel/src/main/java/io/apigee/trireme/kernel/dns/Wire.java61 symbols
node10/node10src/src/test/java/io/apigee/trireme/node10/test/BasicTest.java59 symbols
node10/node10src/src/main/java/io/apigee/trireme/node10/modules/ProcessWrap.java59 symbols
node12/node12src/src/test/java/io/apigee/trireme/node12/test/BasicTest.java54 symbols
core/src/main/java/io/apigee/trireme/core/modules/Buffer.java50 symbols
node12/node12src/src/main/java/io/apigee/trireme/node12/modules/Filesystem.java47 symbols
kernel/src/main/java/io/apigee/trireme/kernel/http/HTTPParsingMachine.java45 symbols
node12/node12src/src/main/java/io/apigee/trireme/node12/modules/Process.java44 symbols
node10/node10src/src/main/java/io/apigee/trireme/node10/modules/Filesystem.java44 symbols

For agents

$ claude mcp add trireme \
  -- python -m otcore.mcp_server <graph>

⬇ download graph artifact

Ask about this repo answers extend the page