NODE.JS == C++ ?

Chedy
The Startup
Published in
4 min readJan 24, 2021

--

Yes, Node.js has a great portion of it written in C/C++ and a lot of its modules are actually implemented in C/C++.
In this article, we will take a deep dive into Node and explore the C/C++ implemented under the hood

Just like any other javascript project out there, Node.js internally has a collection of dependencies that it uses to actually execute your code.
Two of these most important dependencies are the V8 and libuv projects;

V8 project:

is an open-source Javascript Engine created by google to give you the ability to execute javascript code outside of the browser

libuv project:

is a C open-source project that gives Node access to the operating systems underlying file system. It gives you access to networking and it also handles some aspects of concurrency as well.

One Important thing to note is that V8 and libuv are not javascript code at all. The V8 project is like 70% C++ code and libuv is 100% C.

So the question is: What Is the Purpose of Node Js? Why don’t we use directly V8 or libuv?

Node provides a series of wrappers and a very unified and consistent API for us to use inside of our projects.

For example, Node implements the HTTP, fs, path, and crypto modules… All these modules have very consistent APIs.

And they all ultimately refer to a functionality that is mostly implemented inside of the lib project.

You probably don’t want to get access to direct C/C++ code.

You want to require some javascript function and use it inside of your codebase.

So the purpose of Node is to give you a nice consistent API for getting access to functionality that is ultimately implemented inside V8 and libuv.

Now let’s discover how this actually works behind the scenes:

This schema explains how things really work

To break this down let’s take the pbkdf2 function (if you don’t know what it is don’t worry it’s just an algorithm that is used for hashing some arbitrary data, and usually used to hash a password for storage inside of a database)

We will see how this function is actually written in C++ and how Node gives you the ability to use it as a JS function

Let’s start by visiting the Node git repository

There are two folders that are very relevant to what we’re trying to do right now.

lib directory :

Contains all the javascript definitions of functions and modules that you require into your projects.

So you can think of this lib folder as being like the javascript world or the javascript side of Node

src directory:

Inside is the C++ implementation of all those functions.

So src directory is where Node actually pulls in libuv and the V8 project and actually flushes out the implementation of all the modules that you are used to like FS and HTTP

Back again to the Node git repository, Now navigate to lib/internal/crypto/ and search for the pbkdf2.js file and click on it

Here is the JS implementation of the pbjdf2 function

If you scroll down to the last line you can find where the pbkdf2 is being exported

Now this is the most important part, if you scroll back to the top you are going to see where we require the pbdkf2 function and that’s coming from an unfamiliar looking require statement “internalBinding(‘crypto’)”

This line of code right here is how NodeJs joins up the C++ side to the Javascript side

Now let’s take a look at the real implemented C++ code of this function Navigate back again to the root project directory and go to src/crypto/ Look for the crypto_pbkdf2.cc (make sure it’s .cc not .h) and open it

There you’ll find the real C++ implementation of the pbkdf2 function, make sure to go through it and check it

So I know this has been a very deep dive and you might not know any C++ at all but hopefully,

you now have better sense of how whenever you write javascript code and require in node modules or Node libraries they are depending upon some javascript definition which eventually kind of maps up actual C++ implementation in the other side Node project as well.

Connect with me on Linkedin for more interesting tech stuff!

Thank you very much for your attention

--

--

Chedy
The Startup

Backend Developer in Pursuit of Happiness | Entrepreneur in the making