What is the purpose of Node.js ?

What is the purpose of Node.js ?

what is the purpose from Node.js ?


Javascript can't be excuted outside the browser or access the operating system (the file system and the networking ...) , so make it able is a quite hard and require to re write the whole language , so Programmers decided to make use of c++ ,


let's start with very simple diagram

look at PIC1 :?

No alt text provided for this image


Node.js has alot of dependencies , the most two important are :?


-The V8 project is an open source javascript engine created by Google, the purpose of this project is to be able to execute javascript code outside of the browser and that's what we do when we run our javascript code from the terminal.


-The libUV project is a C++ open source project that gives Node.js access to the operating systems, underlying file system, it gives us access to networking and it also handles some aspects of concurrency as well.


So WHAT IS THE PURPOSE OF Node.js and why we can't directly use the V8 and libUV ??


look at PIC2:?

No alt text provided for this image


?some of these other libraries are not javascript code at all,

The V8 project is about 70% C++ code and libUV is 100% C++ .


and we as a JAVASCRIPT DEVELOPERS we don't want to write any c++ code,?so Node.js?gives us a nice interface to use to relate our javascript side of our application to the actual C++ that's running on our computer to actually interpret and execute our Javascript code.


LOOK AT PIC 3:

No alt text provided for this image


Node.js does is provide a series of wrappers and a very unified inconsistent API for us to use inside of our projects,?for example Node.js implements the HTTP module (to work with networking )?FS ( to access the file system), path, and crypto (for hashing ), and all these modules?have very consistent API, And they all refer to a functionality that is mostly implemented inside of the libUV project ( the actual implementation is not a javascript code )


So again The main purpose of Node.js is you?don't want to get access to direct C++ code, you want to require in some javascript function and use a javascript function inside of your codebase, and Node.js relate our javascript side of our application to the actual C++ that's running on our computer

So by making use of Node.js you dont have to work with all the C++ that exists inside of libUV and V8.



-------------

Let's make it clearer by example ( This is really a long example , but it explains what Node.js do behind the scene, so maybe it worth a look)


?we're going to look at some different documentations and some source code and you'll get a much better idea of what Node.js is really doing behind the scene:


let's pick a function from the Node.js standard library and find where that function is implemented in the Node.js source code and?we're going to figure out how Node.js is kind of rabs functionality that is implemented inside libUV and V8 and it gives you a much better idea of how Node.js is internally structured and how it works.


we will pick function called PBKDF2 ( it is used to hash a password for storage inside of a database )


open the github/nodejs/node Repository



look At PIC4

No alt text provided for this image



there's two folders that are very relevant for what we're trying to do right now:?

-: the LIB directory: it contains all the javascript definitions of functions and modules that you and I require into our projects.

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


-: the SRC directory: it is the C++ implementation of all those functions and this directory?is where Node.js actually pulls in libUV and V8 projects and actually flushes out the implementation of all the models that we are using like the FS module, the HTTP module and so on.



go to pbkdf2.js in the lib directory

https://github.com/nodejs/node/blob/main/lib/internal/crypto/pbkdf2.js


This is a javascript file that contains the javascript definition of the PBKDF2 function , and it is a normal javascript function that is included inside of the standard library of Node.js?

?

go to Line 37 ,, see PIC5

No alt text provided for this image



you'll find the Definition for pbkdft2 function (that is exported from this file),


So if we require this function and then run it from inside of Our Code , this is the function that would get executed , But eventually we're going to end up on Another Part of the code base that take us to the C++ side of Node.js


notice that the pbkdf2 function is call another function called?( PBKDF2Job ) (LINE 48)

/*

(we import this function by PROCESS.BINDING (internalBinding)

const {

?PBKDF2Job,

?kCryptoJobAsync,

?kCryptoJobSync,

} = internalBinding('crypto') //LINE 10-14

)

This lines of code is how Node.js joins up the C++ side of its project to the javascript side,

This process.binding(internalBinding) thing is what serves as an actual bridge between the javascript side of Node.js and the C++ side which is where a lot of the internal work of everything that Node.js does is actually implemented.

*/


and PBKDFT2Job takes in all the arguments

that are passed to our current function pbkdf2 and?IN THIS PBKDFT2Job

Is WHERE the hash is Actually calculated. Essentially Node.js takes all the inputs that you provide and it forwards them on to the C++ implementation of this function .


look at PIC6 for more understanding?

No alt text provided for this image




--------------

now we are going to go look up and find on the C++ side of the Node.js where the PBKDF2 function is actually truly implemented.


go to pbkdf2.cc in the src directory

https://github.com/nodejs/node/blob/main/src/crypto/crypto_pbkdf2.cc


(Look at Pic7?)

No alt text provided for this image


and in this file?is where the ACTUAL IMPLEMENTATION of that PBKDF2 function .

and this function is really exported from the c++ side from another file ( is exported from node_crypto.cc )

https://github.com/nodejs/node/blob/main/src/node_crypto.cc

look at PIC8

No alt text provided for this image


-------


and now to get an idea what V8 and libUV actually does

go to https://github.com/nodejs/node/blob/main/src/node_http2.cc


look at PIC9

No alt text provided for this image


you will see at line 20-44


using v8::Array;

using v8::ArrayBuffer;

using v8::ArrayBufferView;

using v8::BackingStore;

using v8::Boolean;

using v8::Context;

using v8::EscapableHandleScope;

using v8::False;

using v8::Function;

using v8::FunctionCallbackInfo;

using v8::FunctionTemplate;

using v8::HandleScope;

using v8::Integer;

using v8::Isolate;

using v8::Local;

using v8::MaybeLocal;

using v8::NewStringType;

using v8::Number;

using v8::Object;

using v8::ObjectTemplate;

using v8::String;

using v8::True;

using v8::Uint8Array;

using v8::Undefined;

using v8::Value;


So the purpose of the V8 project inside of all this Node.js source code is to essentially act as the intermediary and allow values that are defined inside of javascript to be translated into C++ equivalence.


So all these using statements are importing the C++ definition of javascript concepts ( like

C++ understanding of what javascript Boolean, and the C++ understanding of what an Integer is or Object or String or any of these other things )


----?

for libUV

go to https://github.com/nodejs/node/blob/main/src/connection_wrap.cc

look at PIC10

No alt text provided for this image
No alt text provided for this image


at line 33 you will see??(uv_stream_t)?

at line 138 (uv_tcp_t)?

(uv_connect_t)

so here in this file give accss to streaming and networking stuff?



and in another file see PIC12

No alt text provided for this image


go to https://github.com/nodejs/node/blob/main/src/node_worker.cc

ON LINE 647 you will see (uv_thread_t)

it is used for parallel processing (THREADS) ( I will talk more about threads and libUV Thread pool, and how Node.js can create multible threads in another article )


---------------------------------------------------

I hope this helpful to you and you now have a better idea of how whenever writing javascript code and require node modules (Node.js libraries ) , they are depending upon some javascript definition which eventually kind of maps up to actual C++ implementation on the other side of Node.js, and then on the C++ side there's a lot of interoperability between the V8 and libUV?


refs :


the diagrams are taken from Node.js advanced conecpts udemy course https://www.udemy.com/course/advanced-node-for-developers/

要查看或添加评论,请登录

Mohammad Albacha的更多文章

社区洞察

其他会员也浏览了