7.8 Node.js compatible framework

The Limax integrates the native Java version's node.js compatible framework, uses the Nashorn engine provided by the JDK8, is implementted according to the description of the node.js manual and makes the appropriate expansion to integrate to the Limax framework and provide the convenience to connect the system outside of Limax.

The Nashorn supports the ES5.1 standard. For some js files matching ES6 standard, it should use the 6to5 tool to convert. The existing manual examples are all ES6 code and should be experimentted after conversion.

The below description conforms to the order of the node.js 6 documents, which is convenient to compare.


  • 7.8.1 Core module

    Except that the Buffer module is quite special and put in the limax.node.js package, the other code modules are all put in the limax.node.js.modules package, and make a pair based on one .java file and one .js file, and the first letter is capital and all letters are lowcase when requiring according to the Java custom.

    • Assert

      The function is consistent with the standard node.js. The AssertionError class makes some information counterfeit. Actually, in js, the Error class can not be effectively inherited, especially on stack issue.


    • Buffer

      Different from the object of the other core modules, the Buffer is not js object, and directly exports the limax.node.js.Buffer object. The reason is that the node.js document describes the [ ] operation, however the ES5.1 standard does not support the access to the indexed properties. So a strange feature of Nashorn is used, through which the object that implements the List interface could be used in [ ] mode. The problem is that the BoundCheck is finished in Nashorn and the exception is directly thrown when the array is out of range, which is inconsistent with js. It is best to avoid using [ ] to access Buffer.


    • Java plugin

      Unlike the node.js is implemnets in C++ and uses C++ to create the plugin, the Node plugin of Limax is created in Java. One plugin is a jar, and a plugin can have only one js file inside, while ensuring that there is a corresponding name of the java class. Please refer the limax.node.js.modules package for the details.

      For example:

      MyTest.java


      package testnodejsmodule;
      
      import limax.node.js.EventLoop;
      import limax.node.js.Module;
      
      public final class MyTest implements Module {
          public MyTest(EventLoop eventLoop) {
          }
      
          public Number add(Number x, Number y) {
              return x.doubleValue() + y.doubleValue();
          }
      }
      

      MyTest.js


      exports.add = function(x, y) {
          return java.add(x, y);
      }
      

      So that the two programs with eclipse export into a jar which can be referenced as a plug-in.

      Name the plugin as test.jar and put it in the main module directory to execute the main module


      console.log(require('./test.jar').add(1,2))
      

      and output the result 3.


      Please refer the module section for the details.


    • Child Processes

      Use java.lang.ProcessBuilder to operate the child processes, and support the process pipe operation.

      • 1. Not support child_process.fork method, the Limax uses the thread model and does not need this method. Please refer the module section for details.

      • 2. The class ChildProcess just supports the exit event (the other events are related to the IPC and do not need) which is triggered after the process ends. Support the child.kill method, and the signal sent to the process by java.lang.Process just supports the SIGTERM and SIGKILL. The non-SIGKILL case is always SIGTERM as default. Support child.stdin, child.stdout, child.stderr and child.stdio.

      • 3. All methods' options parameter support options.cwd, options.env (the Node.js manual declares that spawn method default process.env, exec method default null, and the actual test is that exec default value is proces.env, so default is process.env), options.shell, options.detached, options.input, optios.timeout, options.encoding, options.maxBuffer, options.killSignal (SIGTERM|SIGKILL), options.stdio (only suppot three standard IO objects).

      • 4. The object returned by child_process.spawnSync method, has no pid member because the java.lang.Process does not provide.


    • Cluster

      The Node.js uses process module, and one process corresponds to a Javascript virtual machine. In order to use the cpu's multiple core feature, the cluster concept is posed, to fork more virtual machines, using IPC to coordinate. The Limax, through extending require, which allows require to transfer the parameter, and allows require to create the new virtual machine instance, could totally achieve the same purpose and save more resources.

      Please refer the module section for the details.


    • CLI

      Execute in the shell. In this condition, the java –jar limax.jar node <module> [args] is loaded as the Limax service. Add the below statement in the xml configuration.


      <NodeService module=module_path>
          <parameter value="0" />
          sequencely order according to the parameters
      </NodeService>
      

      One NodeService element launches a javascript virtual thread to execute module.


    • Console

      • 1. The console.dir as the alias for console.log, does not support the options such as the display colors.

      • 2. Internally use java.lang.String.format method to format the output string. The format parameter is a little different with the sprintf format parameter used by node.js, and should be noted. If the format fails, it should be treated according to the common string.

      • 3. Console.timeEnd, uses WeakHashMap to manager, and does not leak without deleting the timer.


    • Crypto

      • 1. The node.js uses to implement. The Limax mainly uses jce to implement. So the set of supported algorithms is a little different. The getCiphers(), getCurves() and getHashes() three methods could be used to obtain the specific supported algorithm to compare.

      • 2. The Cipher class does not provide AEAD, because the use process of AEAD has not been determined.

      • 3. The limax.util.OpenSSLCompat provides the needed openssl compitable support. The jce only supports its own keystore, PKCS11 and PKCS12. Here through using limax.codec.asn1 package, provide the support to PEM format. These two codes could be used in other case.


    • Debugger

      The Nashorn does not generate the js bytecode, but directly generates the java bytecode. So deeply debugging should use the java's debugger.


    • DNS

      The node.js uses the libresolve to implement. The Limax uses the jndi to implement. So it does not provide the error code of the libresolve specifications.

      Please pay attention to the return result of lookup method, which the document description of the node.js is inconsistent with the implementation and the document descripton is error.


    • Domain

      The node.js document declares that this module is obsolete and therefor not available.


    • Error

      The error class is provied by Narshorn and no need to provide.

      It should be noted that in the Limax environment, all the errors reported through callbak, might be the js Error, or might be the java Exception. They are not unified into Error because that the Exception could provide more plentiful stack information. For example:


      function(err) {
          if (err) {
              if (err instanceof Error) {
                  console.log(err.message, err.stack);
              } else {
                  err.printStackTrace();
              }
              return;
          }
      }
      

      This processing could output more complete error message.


Prev Next