Afrikaans
Akan
Albanian
Amharic
Arabic
Armenian
Azerbaijani
Basque
Belarusian
Bemba
Bengali
Bihari
Bosnian
Breton
Bulgarian
Cambodian
Catalan
Cebuano
Cherokee
Chichewa
Chinese (Simplified)
Chinese (Traditional)
Corsican
Croatian
Czech
Danish
Dutch
English
Esperanto
Estonian
Ewe
Faroese
Filipino
Finnish
French
Frisian
Ga
Galician
Georgian
German
Greek
Guarani
Gujarati
Haitian Creole
Hausa
Hawaiian
Hebrew
Hindi
Hmong
Hungarian
Icelandic
Igbo
Indonesian
Interlingua
Irish
Italian
Japanese
Javanese
Kannada
Kazakh
Kinyarwanda
Kirundi
Kongo
Korean
Krio (Sierra Leone)
Kurdish
Kurdish (Soranรฎ)
Kyrgyz
Laothian
Latin
Latvian
Lingala
Lithuanian
Lozi
Luganda
Luo
Luxembourgish
Macedonian
Malagasy
Malay
Malayalam
Maltese
Maori
Marathi
Mauritian Creole
Moldavian
Mongolian
Myanmar (Burmese)
Montenegrin
Nepali
Nigerian Pidgin
Northern Sotho
Norwegian
Norwegian (Nynorsk)
Occitan
Oriya
Oromo
Pashto
Polish
Portuguese (Brazil)
Portuguese (Portugal)
Punjabi
Quechua
Romanian
Romansh
Runyakitara
Russian
Samoan
Scots Gaelic
Serbian
Serbo-Croatian
Sesotho
Setswana
Seychellois Creole
Shona
Sindhi
Sinhalese
Slovak
Slovenian
Somali
Spanish
Spanish (Latin American)
Sundanese
Swahili
Swedish
Tajik
Tamil
Tatar
Telugu
Thai
Tigrinya
Tonga
Tshiluba
Tumbuka
Turkish
Turkmen
Twi
Uighur
Ukrainian
Urdu
Uzbek
Vietnamese
Welsh
Wolof
Xhosa
Yiddish
Yoruba
Zulu
welcome back let's complete this deep dive into the internals of node by going all the
way into the libya v source code we're about to venture somewhere that very few node developers
ever go so we're going to have that extra advantage when it comes to knowing
exactly what node is doing so let's get into it in our browser we can go to libuv.org
the official site of our favorite library over here
while libyuv is part of node it's also meaningful on its own remember node allows us to use libuv
by connecting to it with the node.js bindings which allow the functionality in libyuv
to be used by our javascript but other programming languages have bindings to libyuv as well we can
see this if we go to this documentation tab and follow this others link here we can see scrolling
down all the way to the bottom here under bindings that libyuv has bindings to ruby lua
c plus pus and python and all of these other common and less common programming languages
these are all client languages that can use the lib uv library just like node this is why it has
its own website which you can see is made with love by the libya v team notice libyuv with a
very well known client language that's javascript but as we just saw libyuv has bindings to other
languages as well so the lib uv code lives in a slightly different place in a different repository
and let's just get into it show me the lib uv code we're taken to github under the lib uv
organization and again we have a few folders that we can explore containing our documentation
and tests but the bulk of the c code that makes up libyuv is in this source folder
now remember we're exploring what happens from the javascript to our final results when we run
fs dot open to open a file we've seen the call through the nodejs apis
in our fs.js file and going through the node.js bindings now let's find it in libuv
now in our source folder we can see that there are two main folders one for unix which is a family
of operating systems that includes both linux and mac os where things tend to work in similar ways
this is because both linux and macos are descendants of this original unix operating system
windows is also a family or a group of operating systems where things tend to work similarly there
are incremental updates with each version but many of the fundamentals remain the same so it
makes sense to group these together let's start by looking at the implementation of fs.open on unix
scrolling down in the folder we can see references to unix systems like android which is linux based
and darwin which refers to mac os darwin is the code name for the core components of the apple
operating systems so we're on the right track but the open functionality is going to be in
this fs dot c file notice that unlike the node api bindings lib uv is using c rather than c plus plus
with its dot cc file extension going into our file we see that we have another few thousand lines of
code and we scroll down we can see that this is c code which might not really mean anything to us
but remember the api bindings we're calling this function inside of libuv and we can do
a search for that function which if you remember was called uv underscore because it's in libyuv
and then fs underscore open this function is being called in a few places
but let's look for the definition so our result over here is this c function definition but
this doesn't seem to be doing any opening of files it's just really calling this
init and passing in open this is a way of calling functions that libyuv uses
but the good news is that we don't have to search far for the actual implementation
the actual implementation is in this function called uv underscore underscore
fs underscore open all right here we go this is where the hard work of actually opening the file
and calling down into the operating system and doing file system operations is done
when we call this open function here this is the actual open function for unix operating
systems that makes a system call down into your operating system to do whatever it needs and then
give you a handle on that file a way of working with that file and reading and writing to it
that's the r that we get back from this function call and it's transformed and returned all the way
back up into our javascript wait a second let's go to our windows version now so we want to go back
out of our unix folder and into our win folder where we're going to have another fs.c file notice
there's fewer files here for other variants of windows because windows operating systems are all
relatively similar there's not as much variation as between android mobile phones and mac os
on apple computers and these open source distributions of linux that there's
hundreds of so windows is simpler in some way but let's see what this fs.c file has in store for us
now remember we're still looking for uv underscore fs underscore open that's what's being called from
our node.js api bindings and again we have this init pattern where the actual function
that opens the file lives close by and we can find it by doing a search for
other functions that look like open so we just saw one and then the second one is this fs
underscore underscore open function this is where the good stuff happens on windows
and we see these d words which are structures that windows uses scrolling down we can see that
this function is actually a fair bit longer than the unix one it's actually
well over a hundred lines of code maybe two or three hundred and we only get to the actual
call that opens the file on windows all the way down here this create
file w function gives us that file handle that we just talked about on windows
and there's more after that this open underscore osf handle function over here transfers ownership
of the file handle that we got from create file w to this fd variable here or our file descriptor
which is what we then use in c to work directly on the file and finally at the bottom of this
super long function we set a result with that file descriptor and return it back up through our code
so why is this function so much longer than the unix version well for one
things are just a little bit more involved in windows land but there's also a bit of code here
near the beginning of our function that allows the results of opening a file to be compatible between
windows and unix so that we can work with files in the same way regardless of which operating
system we're on this is one of the benefits that working with libuv gives us as node developers
okay so maybe you've been following along so far but now you're wondering will i need to
know c and c plus for this course to be a node developer the answer to that is a resounding no
the whole reason why we have high level languages like javascript and runtimes like node
is so we don't have to write code in these low-level languages and worry about all of
these complications that you've had a taste for here but as we learn more about what node does
having this knowledge will allow us to be not quite as intimidated by the internals we've now
explored this code as far as we can any deeper and we'd have to start looking at the c libraries and
the operating system code for windows and linux for example now we can point out how things are
implemented in the underlying node source code this is where our fs.open function call journey
ends we did it going this deep is not something that many people can do so consider yourself
ahead of the game let's take a well-deserved break and i'll see you in the next one bye
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.