easy-proxy

A Proxy handler maker that drastically simplifies making and using Harmony Proxies by combining traps and normalizing their arguments.

npm install easy-proxy
6 downloads in the last week
6 downloads in the last month

Easy Proxy

Shims in Direct-Proxies and then builds a simplified handler on top of that. The traps are abstracted down into a smaller set of traps with normalized parameters and simple to use forwarding.

npm install easy-proxy

Basics

var EasyProxy = require('easy-proxy');

var proxied = EasyProxy(target, {
  list:   function(reflect, target, type, own, hidden)              { return reflect() },
  has:    function(reflect, target, name, own)                      { return reflect() },
  get:    function(reflect, target, name, own, receiver, normalize) { return reflect() },
  set:    function(reflect, target, name, val, receiver, normalize) { return reflect() },
  unset:  function(reflect, target, name)                           { return reflect() },
  fix:    function(reflect, target, type)                           { return reflect() },
  invoke: function(reflect, target, type, args, receiver)           { return reflect() },
});

Usage

Any traps not provided in the handler will default to reflecting normally onto the target.

reflect

The first parameter is always a function that when called with no arguments will execute the default forwarding action. This allows for easy interception and modification. It also accepts a single object passed that specified overrides on the default action.

  • trap: Direct Proxies traps name name to execute instead of the default
  • target: Redirect the action to another target
  • name: Change the name that actual is executed again for traps that are for specific properties
  • value: Change the value for set
  • receiver: Change the receiver for get, set, and invoke
// redirect all set operations to global instead;
var redirected = EasyProxy({}, {
  set: function(reflect){
    return reflect({ target: global });
  }
});

normalize

For get and set, normalize is a function provided as the last parameter. Its pupose is normalize the difference between a descriptor and normal value since these traps are set+defineProperty and get+getOwnPropertyDescriptor.

var typeofWrap = EasyProxy(global, {
  get: function(reflect, target, name, own, receiver, normalize){
    return normalize(typeof target[name]);
  }
});
npm loves you