Bytenode : Hide node JS source code - austral-electronics/Austral_Support GitHub Wiki

^ Back to Node-Red

TUTORIALS :

Bytenode :

Create and call a NPM module in node-red : CREATE A COMPILED NODE JS MODULE CALLED FROM A NODE-RED FUNCTION

Initialize the module:

 cd ~/git
 mkdir private_module
 cd private_module
 npm init

Then respond like this :

 package name: (private_module) private_module
 version: (1.0.0) 
 description: Quantum private module
 entry point: (index.js) 
 test command: 
 git repository: 
 keywords: 
 author:
 license: (ISC)

Install bytenode in the project :

 npm install bytenode --save

Create private.js : This is your private source file, it will be compiled in V8 and erase for production :

 module.exports = {
   print() {
     console.log('works!');
   },
   sum(a,b) {
     return a+b;
   }
 };

Compilation test of private.js : Create a private.jcs compiled file :

 bytenode --compile private.js

Create index.js, This file re-export all private.jsc functions :

 require('bytenode');
 module.exports = require('./private.jsc');

Optionally, test your module in node JS first : You can also deploy and test directly in Node-Red :

Create test.js :

 var tools = require('./index.js');
 tools.print();
 var value = tools.sum(10,20);
 console.log('sum=' + value);

Launch the test:

 node test.js

You must see : 👍

 works!
 sum=30 

Save your module : If you are in production, you must save the modifications in a private git and then remove private.js before publishing the NPM or installing it in node-red.

Deploy for production : Remove private source code, Stop node-red, Install the compiled module in node-red and relaunch node-red :

 rm private.js && node-red-stop && cd ~/.node-red && npm install ~/git/private_module && node-red-start

Deploy in development : Compile, Stop node-red, Install the compiled module in node-red and relaunch node-red :

 bytenode --compile private.js
 node-red-stop && cd ~/.node-red && npm install ~/git/private_module && node-red-start

Test your module in Node-Red : Create a test node-red function with :

 const myTools = context.global.get('myTools');
 myTools.print();
 msg.payload=myTools.sum(10,20);
 return msg;

Add an inject and a debug nodes to test it in the debug tab, you must see at each inject: 👍

 msg.payload : number
 30

And also in the node-red linux console.

 works!

CREATE A COMPILED NODE-RED NODE :

Tutorial : the lower-case node example :

The compiled version of the lower-case node :

Initialize a new node with 'lower-case-private' name.

Create a V8 directory for the compiled code :

 mkdir V8

Install bytenode in this node :

 npm install bytenode --save

Edit lower-case-private_V8.js (The file to hide in production)

 module.exports = function(RED) {
    function LowerCaseNode(config) {
        RED.nodes.createNode(this,config);
        var node = this;
        node.status({fill:"blue",shape:"dot",text:"Started"});
        //node.log("Started");
        this.on('input', function(msg) {
            //node.log("On input : " + msg.payload);
            var nodeContext = this.context();
            var init=nodeContext.get('init')||false;
            if (init == false) {
                init=true;
                nodeContext.set('init',init);
                node.status({fill:"green",shape:"dot",text:"Operational"});
                //node.log("Init");
            }
            msg.payload = msg.payload.toLowerCase();
            node.send(msg);
        });
    }
    RED.nodes.registerType("lower-case-private",LowerCaseNode);
 }

Edit lower-case-private.js

 require('bytenode');
 module.exports = require('./V8/lower-case-private_V8.jsc');

Edit lower-case-private.html

 <script type="text/javascript">
    RED.nodes.registerType('lower-case-private',{
        category: 'function',
        color: '#a6bbcf',
        defaults: {
            name: {value:""}
        },
        inputs:1,
        outputs:1,
        icon: "sailing_64.png",
        label: function() {
            return this.name||"lower-case-private";
        }
    });
 </script>
 <script type="text/x-red" data-template-name="lower-case-private">
    <div class="form-row">
        <label for="node-input-name"><i class="icon-tag"></i> Name</label>
        <input type="text" id="node-input-name" placeholder="Name">
    </div>
 </script>
 <script type="text/x-red" data-help-name="lower-case-private">
    <p>A private Austral demo node that converts the message payloads into all lower-case characters</p>
 </script>

Compile the *_V8.js files and move them in V8 directory:

 bytenode --compile *_V8.js && mv *.jsc V8

Deploy the node and relaunch node-red

The 'lower-case-private' node is now in node-red.

You can store the node in a private git and erase all *_V8.js source code in production.



              
⚠️ **GitHub.com Fallback** ⚠️