Monkey Patching in Node.js? Update code at runtime in Node.js

Introduction

In this blog post, we will look at what Monkey patching is, what are the pros and cons of it, and also an example that illustrates this technique.

What is Monkey patching?

Monkey patching is a technique for dynamic modification of any module or function. By using Monkey patching one can add or modify the default behavior of a piece of code at runtime without changing its original source code.

When to use Monkey patching?

Imagine you as a system administrator, and you want to check every database query which has been made to your database. If you have a web application which does some system calls and you want to intercept what arguments were passed in the system call. Therefore one basic use case of monkey patching is to sniffing the arguments of API call.

With help of monkey patching one can modify the arguments as well and can change the default behaviour of the API.

Lets create a sample program which intercept the child_process module’s API.

const cp = require('child_process');

//logic to update default exec() API
let original_function = cp.exec
cp.exec = function(){
    console.log("Intecepted arguments are:",arguments);
    original_function.apply(this,arguments);   
}

cp.exec('ls -lrt', (error, stdout, stderr) => {
    if (error) {
      console.error(`exec error: ${error}`); 
      return;
    }
  });

Output of the above code is:

% node demo.js
Intecepted arguments are: [Arguments] { '0': 'ls -lrt', '1': [Function] }

As per shown in example code, We have replaced the actual function definition of child_process.exec() to our own version of child_process.exec(). In the modified version of exec() method call, We have added a log to print all the arguments passed to the exec() API call. After that the original function call was done by using apply() function.

Conclusion

In this post, We have covered what Monkey patching is and how it can be used in JavaScript. Hope you got the idea behind the monkey patching. Let’s hack the programming language. Have fun

#HappyCoding # #TopCode

#monkey patching javascript array #monkey patching javascript package #monkey patch javascript module #monkey patch js get #javascript monkey patching library #monkey patching js api #monkey patching node js #monkey patching javascript code #monkey patching javascript example #javascript es6 monkey patching #monkey patch javascript function #monkey patching in javascript #javascript test monkey patching #monkey patch javascript object #monkey patch javascript prototype #What is monkey patching in javascript #runtime instrumentation agent #runtime instrumentation java #runtime and instrumentation version mismatch #javassist runtime instrumentation #runtime code instrumentation #dynamic runtime instrumentation #hibernate runtime bytecode instrumentation #java runtime bytecode instrumentation #runtime instrumentation in java #kernel runtime security instrumentation #function hooking in javascript #function hooking in node.js #node.js agent #dynamic agent node.js #node,js function hook #node.js apply function #javascript apply function monkey patch node module #node js monkey patch #node monkey patch #nodejs monkey patch require #monkey patch node js #node.js monkeypatch require #monkey patch javascript #monkey patch javascript module #javascript monkey patch constructor #javascript monkey patch library #javascript monkey patch variable #javascript monkey patch arguments #javascript monkey patch definition #javascript monkey patch console #monkey patch javascript function #monkey patch javascript array #javascript monkey patch class #javascript define monkey patch #monkey patch javascript example #how to monkey patch javascript #how to do monkey patch in javascript #monkey patching javascript #javascript monkey patch method #monkey patch javascript object #monkey patch javascript prototype

, ,

Related posts

Latest posts

1 comment

  • really helpful blog

Leave a Comment

Leave a Reply

Your email address will not be published. Required fields are marked *

Please disable your adblocker or whitelist this site!

How to whitelist website on AdBlocker?

How to whitelist website on AdBlocker?

  1. 1 Click on the AdBlock Plus icon on the top right corner of your browser
  2. 2 Click on "Enabled on this site" from the AdBlock Plus option
  3. 3 Refresh the page and start browsing the site