Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Deconstructing/Hacking NodeJs

I am trying to understand Node internal workings and am at a point where I am not able to piece together how Node, which is written in js, hooks/binds into the low level system calls written in C/C++.

When I write this piece of code -

var http = require('http');
var server = http.createServer(function(req,res) { res.end('Hacking Node'); });
server.listen(8081);

How does server.listen listen the specific port for which only the OS has access to? Same goes with various other features.

The journey so far has been this.

I checked the net.js and os.js files. But could not make out much.

Also I dug into process file written in C - both unix and windows versions.

Finally,after further investigation hit the Node.js file

// https://github.com/joyent/node/tree/6cbfcdad46d733bb04332063727e304e449dc86b/src/node.js

which had the following comment -

// This file is invoked by node::Load in src/node.cc, and responsible for
// bootstrapping the node.js core. Special caution is given to the performance
// of the startup process, so many dependencies are invoked lazily.

And Node.cc loads this Node.js file in line 2708.

// https://github.com/joyent/node/blob/6cbfcdad46d733bb04332063727e304e449dc86b/src/node.cc - line 2708

That is as far as I got. Can someone point me what happens next and help me fill in the pieces of this puzzle.

like image 867
shrivb Avatar asked Jun 30 '26 08:06

shrivb


1 Answers

I was surprised no one mentioned about libuv. Here is a schematic. (It's not 100% accurate).

              2 V8 compiler
             /             \
            /               \
   1 Your code              3 node.js core system libraries(net,fs,events)
                                       JavaScript
                                      ------------    
                                     V8 bindings(C++)
                                         / | \
                                        /  |  \
                              4 node.js platform code C/C++
                                  events   fs  net
                                        \  |  /
                                         \ | / 
                                          \|/
                                        runs on
                                     5 libuv C/C++
                               High Performance Event Loop
                                           |
                                        syscalls
                                           |
                                        6 Kernel

Remember how you can do .toString() and show JS functions. That does not work for V8 bindings (shows [native code]). node.js platform (C++) functions have V8 bindings. Because node gives evented and async execution, everything you do on event loop runs on libuv. An old blog showing libuv usage.

libuv handles the scheduling of system calls for you and V8 handles your JS code. node extends JS to system.

like image 118
user568109 Avatar answered Jul 02 '26 21:07

user568109



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!