How to Compile Node.js Code Using Bytenode? – Hacker Noon

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: , 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:

read original article here