未加星标

How to Compile Node.js Code Using Bytenode?

字体大小 | |
[前端(javascript) 所属分类 前端(javascript) | 发布者 店小二04 | 时间 2018 | 作者 红领巾 ] 0人收藏点击收藏
How to Compile Node.js Code Using Bytenode?
How to Compile Node.js Code Using Bytenode?

In this post, I will show you how to “truly” compile Node.js (javascript) code to V8 Bytecode. This allows you to hide or protect your source code in a better way than obfuscation or other not-very-efficient tricks (like encrypting your code using a secret key, which will be embedded in your app binaries, that’s why I said “truly” above).

So, using bytenode tool, you can distribute a binary version .jsc of your JavaScript files. You can also bundle all your .js files using Browserify, then compile that single file into .jsc .

Check bytenode repository on Github.

Long storyshort.. Install bytenode globally:
[sudo] npm install -g bytenode To compile your .js file, run this command:
bytenode --compile my-file.js my-file.jsc Install bytenode in your project too:
npm install --save bytenode In your code, require bytenode ,
const bytenode = require('bytenode'); to register .jsc extension in Node.js module system. That’s why we installed it locally too. You can now require my-file.jsc as a module:
const myFile = require('./my-file.jsc'); You can also remove my-file.js from production build. And if you want to run my-file.jsc using bytenode cli:
bytenode --run my-file.jsc

Now you know how to compile .js files, how to require the compiled version in your code, and how to run .jsc files from the terminal. Let’s move on to the long story.

V8 engine (which Node.js is based on) uses what is called: just in time compilation (JIT), where JavaScript code is compiled just before execution, then it will be optimised subsequently.

Starting from Node.js v5.7.0, the vm module introduced a property called produceCachedData in vm.Script Constructor function, so if you do something like this:

Then, get the bytecode or cachedData buffer:

This helloBuffer can be used to create an identical script that will execute the same instructions when it run, by passing it to the vm.Script Constructor function:

But this will fail, V8 engine will complain about the first argument (that empty string '' ), when it checks whether it is the same code as the one that was used to generate helloBuffer buffer in the first place. However, this checking process is quite easy, it is the length of the code that does matter. So, this will work:

We give it an empty string with the same length (28) as the original code ( console.log("Hello World!"); ). That’s it!

This is interesting, using the cached buffer and the original code length we were able to create an identical script. Both scripts can be run using .runInThisContext(); function. So if you ran them:

you will see ‘Hello World!’ twice.

(Note that if you have used the wrong length, or if you have used another version of Node.js/V8: anotherHelloScript won’t run, and its property cachedDataRejected will be set to true ).

Now to our last step, when we defined anotherHelloScript we used a hard coded value (28) as our code length. How can we change this, so that in the runtime we don’t have to know exactly how long was the original source code?

After some digging in V8 source code, I have found that the header information is defined here (in this file snapshot.h ):

But, Node.js buffer is Uint8Array typed array. This means that each entry from the uint32 array will take four entries in the uint8 buffer. So, the payload length will be:

It will be some thing like this: <Buffer 1c 00 00 00> , which is Little Endian, so it reads: 0x0000001c . That is our code length (28 in decimal).

To convert these four bytes to a numeric value, you may do something like this:

firstByte + (secodeByte * 256) + (thirdByte * 256**2) + (forthByte * 256**3) ,

Or in a more elegant way, you can do this:

As I did here in my library, check it to see the full recipe.

Alternatively, we could use buf.readIntLE() function, which does exactly what we want:

Once you have read the length of the original code (that was used to generate the cachedData buffer), you can now create your script:

Finally, does this technique have an impact on performance? Well, in recent versions of v8 (and Node.js), the performance is almost the same. Using octance benchmark I did not find any difference in performance. I know that Google deprecated octance (because browsers and JS engines were cheating), but the results in our situation are significant, because we are comparing the same code on the same JS engine. So, the final answer is: Bytenode does NOT have a negative impact on performance.

Check my Github repository , where you can find complete working examples. I have added an example for Electron (which has no source code protection at all) and for NW.js (which has a similar tool nwjc , but it works only with browser-side code). I will add more examples (and tests) soon, hopefully.


How to Compile Node.js Code Using Bytenode?

本文前端(javascript)相关术语:javascript是什么意思 javascript下载 javascript权威指南 javascript基础教程 javascript 正则表达式 javascript设计模式 javascript高级程序设计 精通javascript javascript教程

代码区博客精选文章
分页:12
转载请注明
本文标题:How to Compile Node.js Code Using Bytenode?
本站链接:https://www.codesec.net/view/611047.html


1.凡CodeSecTeam转载的文章,均出自其它媒体或其他官网介绍,目的在于传递更多的信息,并不代表本站赞同其观点和其真实性负责;
2.转载的文章仅代表原创作者观点,与本站无关。其原创性以及文中陈述文字和内容未经本站证实,本站对该文以及其中全部或者部分内容、文字的真实性、完整性、及时性,不作出任何保证或承若;
3.如本站转载稿涉及版权等问题,请作者及时联系本站,我们会及时处理。
登录后可拥有收藏文章、关注作者等权限...
技术大类 技术大类 | 前端(javascript) | 评论(0) | 阅读(58)