So now it really logs the URL of the request we do. And that would work for any URL because what we injected is actually console.log, rec.method, rec.url. Okay, now I guess the legitimate question is how does that happen? How did that work? And that's what we will spend the next 15 minutes going through together.
So, we've got this running process. It's a simple hello world server, as I said, and it just answers with okay. So, first thing we need to do is to change its state to debug mode, so we can connect with a debugger. So, there's this cool thing in Node.js is that if you send the system signal sigusr1, then it enables the debugger on the process. And then you can connect with that through a debugger on a web circuit. So, the only change I do to the code now is I make it log the PID of the process. That's not necessary. We could use the PS command on the top command to find it, but in that case, I am a bit lazy. I mean, I was already too lazy to write logs, so I am too lazy to find the PID of a running process, so we just log that. Then when we do the kill-usr1 and the PID, it changes the state of the process. So, the kill command on Unix systems is not only to kill processes, but can also be used to send message signals. In our case, we send the sig-usr1 message signal and we pass the PID of the target process as a last argument.
That's what happened when we started to log process running and when we did this command in another shell, we logged DebuggerListName on websocket and then the url. This can also be done programmatically from another node.js process using, you guessed it, process.kill. Process.kill has a syntax where it accepts a signal and a PID and sends a signal to another process. So, ta-da, we actually remotely started the debugger, which is actually very, very useful, but I will explain that later. So, if we go to chrome __inspect, we actually see that the target is available to be debugged. That's the way you can check how many nodejs and chrome tabs are available for debug locally or remotely by going on chrome //inspect on chromium or chrome. Okay.
So, let's not use that. We could be a normal person and use the chrome dev tool and only play with JavaScript stuff. We would, for instance, monkey patch method on the event emitter to identify an event emitter that sends the request event and based on that, we will monkey patch this emitter because we identified an instance of HTTP dot server and that's actually totally doable with the JavaScript shell of the dev tools. But let's not do that. Let's actually learn to use lower level tools and perform that magic of the JavaScript code. So, have you heard of the dev tool protocol? It's actually what is used by any Node.js debugger and also by Chrome debuggers, whether they are remote or local. For instance, when you debug Chrome on an Android phone, that's the protocol you use. When you use a Pupator module to have a headless Chrome control, that's the protocol you use.
Comments