Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:00,720 --> 00:00:09,120
welcome back let's complete this deep dive
into the internals of node by going all the
2
00:00:09,120 --> 00:00:18,720
way into the libya v source code we're about to
venture somewhere that very few node developers
3
00:00:18,720 --> 00:00:25,760
ever go so we're going to have that
extra advantage when it comes to knowing
4
00:00:25,760 --> 00:00:36,400
exactly what node is doing so let's get into
it in our browser we can go to libuv.org
5
00:00:37,360 --> 00:00:41,280
the official site of our
favorite library over here
6
00:00:42,720 --> 00:00:52,880
while libyuv is part of node it's also meaningful
on its own remember node allows us to use libuv
7
00:00:53,760 --> 00:01:00,880
by connecting to it with the node.js bindings
which allow the functionality in libyuv
8
00:01:01,440 --> 00:01:11,200
to be used by our javascript but other programming
languages have bindings to libyuv as well we can
9
00:01:11,200 --> 00:01:20,800
see this if we go to this documentation tab and
follow this others link here we can see scrolling
10
00:01:20,800 --> 00:01:31,280
down all the way to the bottom here under
bindings that libyuv has bindings to ruby lua
11
00:01:32,160 --> 00:01:42,320
c plus pus and python and all of these other
common and less common programming languages
12
00:01:43,600 --> 00:01:52,480
these are all client languages that can use the
lib uv library just like node this is why it has
13
00:01:52,480 --> 00:02:03,600
its own website which you can see is made with
love by the libya v team notice libyuv with a
14
00:02:03,600 --> 00:02:14,560
very well known client language that's javascript
but as we just saw libyuv has bindings to other
15
00:02:14,560 --> 00:02:24,640
languages as well so the lib uv code lives in a
slightly different place in a different repository
16
00:02:25,520 --> 00:02:34,880
and let's just get into it show me the lib uv
code we're taken to github under the lib uv
17
00:02:34,880 --> 00:02:44,320
organization and again we have a few folders
that we can explore containing our documentation
18
00:02:45,120 --> 00:02:55,040
and tests but the bulk of the c code that
makes up libyuv is in this source folder
19
00:02:57,120 --> 00:03:05,920
now remember we're exploring what happens from
the javascript to our final results when we run
20
00:03:06,640 --> 00:03:13,920
fs dot open to open a file we've
seen the call through the nodejs apis
21
00:03:14,480 --> 00:03:23,200
in our fs.js file and going through the
node.js bindings now let's find it in libuv
22
00:03:25,120 --> 00:03:36,560
now in our source folder we can see that there are
two main folders one for unix which is a family
23
00:03:36,560 --> 00:03:47,120
of operating systems that includes both linux and
mac os where things tend to work in similar ways
24
00:03:48,080 --> 00:03:57,200
this is because both linux and macos are
descendants of this original unix operating system
25
00:03:58,160 --> 00:04:08,400
windows is also a family or a group of operating
systems where things tend to work similarly there
26
00:04:08,400 --> 00:04:16,800
are incremental updates with each version but
many of the fundamentals remain the same so it
27
00:04:16,800 --> 00:04:25,440
makes sense to group these together let's start by
looking at the implementation of fs.open on unix
28
00:04:27,680 --> 00:04:37,920
scrolling down in the folder we can see references
to unix systems like android which is linux based
29
00:04:38,640 --> 00:04:48,320
and darwin which refers to mac os darwin is the
code name for the core components of the apple
30
00:04:48,320 --> 00:04:56,320
operating systems so we're on the right track
but the open functionality is going to be in
31
00:04:57,440 --> 00:05:10,160
this fs dot c file notice that unlike the node api
bindings lib uv is using c rather than c plus plus
32
00:05:10,880 --> 00:05:20,720
with its dot cc file extension going into our file
we see that we have another few thousand lines of
33
00:05:20,720 --> 00:05:28,640
code and we scroll down we can see that this is
c code which might not really mean anything to us
34
00:05:29,520 --> 00:05:38,320
but remember the api bindings we're calling
this function inside of libuv and we can do
35
00:05:38,320 --> 00:05:46,880
a search for that function which if you remember
was called uv underscore because it's in libyuv
36
00:05:47,680 --> 00:05:55,040
and then fs underscore open this
function is being called in a few places
37
00:05:56,240 --> 00:06:06,080
but let's look for the definition so our result
over here is this c function definition but
38
00:06:06,720 --> 00:06:13,120
this doesn't seem to be doing any opening
of files it's just really calling this
39
00:06:13,760 --> 00:06:20,720
init and passing in open this is a way
of calling functions that libyuv uses
40
00:06:21,280 --> 00:06:28,000
but the good news is that we don't have to
search far for the actual implementation
41
00:06:29,200 --> 00:06:36,160
the actual implementation is in this
function called uv underscore underscore
42
00:06:38,000 --> 00:06:47,840
fs underscore open all right here we go this is
where the hard work of actually opening the file
43
00:06:48,640 --> 00:06:55,120
and calling down into the operating system
and doing file system operations is done
44
00:06:56,000 --> 00:07:04,560
when we call this open function here this is
the actual open function for unix operating
45
00:07:04,560 --> 00:07:13,680
systems that makes a system call down into your
operating system to do whatever it needs and then
46
00:07:13,680 --> 00:07:20,800
give you a handle on that file a way of working
with that file and reading and writing to it
47
00:07:22,720 --> 00:07:31,200
that's the r that we get back from this function
call and it's transformed and returned all the way
48
00:07:31,200 --> 00:07:42,160
back up into our javascript wait a second let's go
to our windows version now so we want to go back
49
00:07:42,160 --> 00:07:54,240
out of our unix folder and into our win folder
where we're going to have another fs.c file notice
50
00:07:54,240 --> 00:08:02,400
there's fewer files here for other variants of
windows because windows operating systems are all
51
00:08:02,400 --> 00:08:11,200
relatively similar there's not as much variation
as between android mobile phones and mac os
52
00:08:11,200 --> 00:08:18,960
on apple computers and these open source
distributions of linux that there's
53
00:08:18,960 --> 00:08:28,240
hundreds of so windows is simpler in some way but
let's see what this fs.c file has in store for us
54
00:08:29,600 --> 00:08:37,920
now remember we're still looking for uv underscore
fs underscore open that's what's being called from
55
00:08:37,920 --> 00:08:45,360
our node.js api bindings and again we have
this init pattern where the actual function
56
00:08:46,720 --> 00:08:54,160
that opens the file lives close by and
we can find it by doing a search for
57
00:08:54,160 --> 00:09:04,080
other functions that look like open so we just
saw one and then the second one is this fs
58
00:09:04,080 --> 00:09:10,800
underscore underscore open function this
is where the good stuff happens on windows
59
00:09:11,600 --> 00:09:20,880
and we see these d words which are structures
that windows uses scrolling down we can see that
60
00:09:21,680 --> 00:09:28,880
this function is actually a fair bit
longer than the unix one it's actually
61
00:09:29,920 --> 00:09:38,640
well over a hundred lines of code maybe two
or three hundred and we only get to the actual
62
00:09:39,280 --> 00:09:46,160
call that opens the file on windows
all the way down here this create
63
00:09:46,160 --> 00:09:52,160
file w function gives us that file handle
that we just talked about on windows
64
00:09:54,000 --> 00:10:03,760
and there's more after that this open underscore
osf handle function over here transfers ownership
65
00:10:03,760 --> 00:10:14,240
of the file handle that we got from create file
w to this fd variable here or our file descriptor
66
00:10:14,800 --> 00:10:23,920
which is what we then use in c to work directly
on the file and finally at the bottom of this
67
00:10:24,560 --> 00:10:33,840
super long function we set a result with that file
descriptor and return it back up through our code
68
00:10:35,840 --> 00:10:41,680
so why is this function so much longer
than the unix version well for one
69
00:10:42,640 --> 00:10:49,040
things are just a little bit more involved in
windows land but there's also a bit of code here
70
00:10:49,600 --> 00:11:00,080
near the beginning of our function that allows the
results of opening a file to be compatible between
71
00:11:00,080 --> 00:11:07,680
windows and unix so that we can work with files
in the same way regardless of which operating
72
00:11:07,680 --> 00:11:15,680
system we're on this is one of the benefits that
working with libuv gives us as node developers
73
00:11:16,880 --> 00:11:24,960
okay so maybe you've been following along so
far but now you're wondering will i need to
74
00:11:24,960 --> 00:11:32,960
know c and c plus for this course to be a node
developer the answer to that is a resounding no
75
00:11:33,920 --> 00:11:41,840
the whole reason why we have high level
languages like javascript and runtimes like node
76
00:11:42,400 --> 00:11:50,720
is so we don't have to write code in these
low-level languages and worry about all of
77
00:11:50,720 --> 00:11:58,960
these complications that you've had a taste for
here but as we learn more about what node does
78
00:11:59,840 --> 00:12:08,640
having this knowledge will allow us to be not
quite as intimidated by the internals we've now
79
00:12:08,640 --> 00:12:18,000
explored this code as far as we can any deeper and
we'd have to start looking at the c libraries and
80
00:12:18,640 --> 00:12:27,040
the operating system code for windows and linux
for example now we can point out how things are
81
00:12:27,040 --> 00:12:37,520
implemented in the underlying node source code
this is where our fs.open function call journey
82
00:12:37,520 --> 00:12:46,640
ends we did it going this deep is not something
that many people can do so consider yourself
83
00:12:46,640 --> 00:12:59,760
ahead of the game let's take a well-deserved
break and i'll see you in the next one bye
10532
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.