- [Instructor] In the last lesson,
we defined the proxy pattern, as well as we introduced
some code where we might be able to use a proxy.
So, currently we're looking at
the files within our exercise files,
under chapter three, lesson four,
within the start folder, and we're
taking a look at the index.js file,
which is allowing us to read both text and Markdown files.
Now, a proxy is an object that
controls access to another object.
There are a number of wonderful things
that you can do with a proxy, and one of those
is restricting access to the actual object.
So that's what we're going to do.
We're going to create a file system proxy
that only allows us to read Markdown files,
it won't let us read any files of any other types.
So, when we create our proxy, what I'm going to do
inside of the index.js file, is use it.
So, we're going to create something called a FS_Proxy,
and now I'm going to go ahead and use the FS_Proxy
from my file system, so the file system
is going to be FS_Proxy, and then
I'm going to go ahead and send the real file system.
So go ahead and require this fs again, and delete this code.
So, now our file system is going to go through
a proxy object that will control access to it.
And what we want to do is code this proxy class
so that we can prevent reading
any files that aren't Markdowns.
So, I'm going to come into the folder here
and create a new file called FS_Proxy,
and we're going to use this file to prevent
access to the readFile method for
anything that's not a Markdown file,
so anything that doesn't have a md extension.
So just like an adapter, a proxy implements
the same interface as the subject.
So I'll go ahead and have class FS_Proxy,
and we'll make sure that we return this class,
and we are going to be overwriting readFile,
so we want to make sure that we have the same interface.
The readFile method takes in a path,
a format, and it has a callback method.
So we want to make sure that the interface for our
readFile method is just like we use it on line 22,
we send in a path, a file format and a callback function.
Now, one of the other things to remember
when creating proxy files, is they should
match the full interface of your object.
For brevity, we're only going to implement readFile,
but we should have writeFile, appendFile, and all of the
methods of the file object when implementing a proxy.
One solution for this is we can simply clone all of the
methods and overwrite the ones that we want to use.
But we want to make sure that we have
the exact same interface as the real file system,
so that we don't have any errors in our client code.
So, for brevity, we're only going to deal with
the readFile method at this time.
So, when we invoke the FS_Proxy,
we're going to have a little constructor
that saves the subject, we'll say FS_subject,
so that's the real file system,
and this proxy controls access to the real file system,
so we'll just save that within this class as FS.
So the next thing that we want to do
is actually write the code that prevents us
from reading files that aren't Markdown files.
So, in here all I need to do within
the read file, is check the path.
If our path doesn't contain, so if it doesn't match
this regular expression that we're about to create,
where the file extension at the end is Markdown,
or it's capital Markdown, do case sensitive,
so if we don't have a .md extension at the end,
then what we're going to do is return our callback,
but we're going to send a new error in it.
So, if the file doesn't have an extension,
we're just going to return the callback immediately
with a new error that we've created here in this proxy.
Now, if it does have a Markdown extension,
we won't hit this return call on line 10,
which means we will execute code after that,
so this is where we want to use
the actual file system to invoke the readFile.
So we're protecting access to the
real readFile function within our proxy,
but we have a Markdown file at this point,
so we will read from that path, format,
and instead of using that callback we're going to
go ahead and use our own callback here,
and this will give us the error in the data
of the file that we just read, where we'll call it contents,
just to keep it all matched up.
So if we have an error reading the
actual file that was sent to us, we'll go ahead
and throw that error to the console,
and then we also need to make sure we return
so we will send that error to the callback.
Now, if we don't have an error reading this file,
then this file has successfully been read.
So we can also return callback null,
so we don't send an error, and then the contents.
So now we've created an FS_Proxy that controls
access to the readFile method,
and it will only let us read Markdown files.
Let's go ahead and give it a shot.
So, we'll go to our code and type node index.js,
and it looks like we forgot to use the new keyword
when invoking the proxy, let me
go ahead and add that there to line four.
So, we'll go ahead and clear this and try it again,
and notice we see a little ding, and it says,
Can only read Markdown files,
so we're throwing an error and exiting the program.
Whenever we get a result on line 11, if there is an error,
we're console logging a little ding.
So that's how the 'boomp' happens,
and then we are actually putting the error out there.
And we can see that the error that
we're seeing is the one that we've created,
Can only read Markdown files.
Additionally, we call process.exit on line 14.
Now let's not try to read the text file.
I'm going to go ahead and comment this line out.
I'll even comment out the file path to the text file.
Let's just try to read the Markdown file
this time and see if it works.
Here we see that we are allowed to read the Markdown file,
that doesn't cause any sort of an error.
So the real key here is that our proxy object
works just like the file system object.
It has the same interface, we have a readFile method.
The difference is, is the proxy object controls
access to the real file system, and will
only let us read files when we have a Markdown file.