Ways to implement Proxy Pattern in Node.js

nodejs-proxy-pattern-example-feature-image

Sometimes we may want to access indirectly to an object, via a substitute. That substitute is called Proxy. The Proxy Pattern helps us deal with a proxy object and talk to the actual object. In this tutorial, we’re gonna look at 2 ways to implement Proxy Pattern in Node.js:

  • Using custom Proxy Class
  • Using Proxy API

Proxy Pattern Overview

nodejs-proxy-pattern-example-sample-structure

The image above shows that Proxy and Subject have the same methods. Proxy forwards each operation to Subject, we can improve Subject‘s methods with additional pre-processing or post-processing.

Proxy Pattern in Node.js using custom Class

Create Subject class

We create a Bank class with 3 methods:
deposit() increases cash.
withdraw() decreases cash.
total() returns cash.

Bank.js

Create custom Proxy class

The Proxy class also have 3 methods: deposit(), withdraw(), total(). Inside each method, we call Subject‘s methods with additional process.

Notice that we need initiate Bank object in constructor() method.

BankProxy.js

Create Client

app.js

Run with command: node app.js. Here is the result:

Proxy Pattern in Node.js using Proxy API

Instead of creating our own custom Proxy class, we can use Proxy API that returns a proxy of the object.

Syntax

  • target: the object that Proxy will wrap. It can be a native array, a function or even another proxy.
  • handler: the object whose properties are functions (get, set for examples) which define the behavior of the Proxy.
Use Proxy API to create Proxy objects

We create module call proxy with 2 functions:
encrypt: returns Proxy object that encrypts properties’ values.
decrypt: returns Proxy object that decrypts properties’ values.

We use getter function get and setter function set for handler parameter.

proxy.js

Use Proxy objects

app.js

To run this Nodejs example, we need install cryptr first with command: npm install cryptr.

Proxy that generates Observable object

We can create Proxy with function that executes every time a given object is modified.

Proxy API traps

What is a trap?
=> A trap is a method that provides property access.

So, get & set function in the example above are traps, we have some more traps:
getPrototypeOf()
setPrototypeOf()
isExtensible()
preventExtensions()
getOwnPropertyDescriptor()
defineProperty()
has()
deleteProperty()
ownKeys()
apply()
construct()

You can find details at: Proxy#Methods_of_the_handler_object.

Some notes

– Proxy pattern helps us create wrapper for object that allows external access control to the object.
– Proxy API (ES2015, NodeJs v6+) has Proxy API that enables creation of proxy.
– Proxy implements various useful traps for property access.
– This pattern is good for implementing middleware mechanism for caching, logging, encryption or other kinds of augmented functionality.

By grokonez | April 10, 2019.



Related Posts


Got Something To Say:

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

*