npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

benalu

v1.1.1

Published

A dynamic proxy for javascript

Downloads

41

Readme

#Benalu

Build Status Known Vulnerabilities Test Coverage

NPM

A dynamic proxy for javascript. Provide a light weight proxy class generator with multiple interception.

###About The purpose of Benalu is provide a simple way to do a simple AOP in javascript. Benalu also useful for IOC container library that hasn't support for interception

###Features

  1. Can proxy a Function prototype or an object.

  2. Can proxy method & property

  3. Can add multiple interceptions

###Installation

npm install benalu

###How To Use It Using benalu is very simple. You start building your proxy by using Benalu builder

var Benalu = require('benalu');

//declare the class
function MyObject(){}
MyObject.prototype.getNumber = function(){
    return 700;
}

//create instance
var myObject = new MyObject();
     
//make a proxy   
var proxy = Benalu.fromInstance(myObject)
    .addInterception(function(i) {
        //filter the invocation
        if(i.methodName == "getNumber"){
            //call the real method
            i.proceed();
            //override the return value
            i.returnValue = 300;
        }
    })
    .build();
    
var numResult = proxy.getNumber();
//numResult become 300 vs 700

###Interception & Invocation Interception in Benalu simply a callback function with single parameter of Invocation. Invocation consist of 3 important members:

  1. methodName name of current invoked method. Usefull when you only want to intercept specific method of the class

  2. parameters arguments passed to the invoked method. Usefull when you want to get information of the arguments passed to the method.

  3. proceed() method to proceed current invocation. This method will invoke the method of the real object.

  4. returnValue return value of the current invoked method. this member filled automatically after the proceed() method called. You can override the return value of current invocation by suplying a value to the returnValue member

###Multiple Interception Benalu also support multiple interception.

var Benalu = require('benalu');

function MyObject(){}
MyObject.prototype.getNumber = function(){
    console.log("The real method called");
    return 700;
}

var myObject = new MyObject();
     
var proxy = Benalu.fromInstance(myObject)
    .addInterception(function(i) {
        if(i.methodName == "getNumber"){
            console.log("First interceptor before proceed");
            i.proceed();
            console.log("First interceptor after proceed");
            i.returnValue = 300;
        }
    })
    .addInterception(function(i) {
        if(i.methodName == "getNumber"){
            console.log("Second interceptor before proceed");
            i.proceed();
            console.log("Second interceptor after proceed");
            i.returnValue = i.returnValue + 300;
        }
    })
    .build();
    
var numResult = proxy.getNumber();
//numResult = 600

Above code will write log in the console like below:

Second interceptor before proceed
First interceptor before proceed
The real method called
First interceptor after proceed
Second interceptor after proceed