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

node-proxygenerator

v0.0.1

Published

Module to help create tracing proxies for ES6 classes

Downloads

4

Readme

node-proxygenerator

Build Status Coverage Status

Introduction

This module is meant to create a wrapper around ES6 classes so we can trace all actions that are performed on this class, such as constructing the class, function calls, setters, getters etc. It achieves this by wrapping the target class in a Proxy Object.

The Proxy Object is an ES6 object that allows us to catch and/or modify how Javascript works. Other than tracing, what this module does, the Proxy object can also be used to implement things such as case-insensitive functions in Javascript.

Installation

node-proxygenerator can be found on npm.

$ npm install node-proxygenerator

Purpose

This Proxy-generator can be wrapped around common ES6 classes, so you can see exactly which functions and properties are being called/used/modified. This can be useful for analysis of JS classes. The reason why the Proxy object is used is that it does not modify the original code at all. It simply logs what is happening, and forwards the requests as if the Proxy wasn't even there.

Limitations

For now, it can only generate a Proxy around 1 class, excluding extended or inherited classes. So if you want to track multiple classes, they each need to be wrapped in their own Proxy

Usage

const ProxyGenerator = require('node-proxygenerator');

// Create a simple class
class MyClass {
  constructor(msg) {
    this.msg = msg || '';
  }
  
  getMessage() {
    return this.msg;
  }
  
  setMessage(msg) {
    this.msg = msg;
  }
}

// Create a Proxy version of the class
// The first argument for ProxyGenerator is always the class that you want to wrap
const MyProxyClass = new ProxyGenerator(MyClass);

// MyProxy behaves exactly as MyClass, so it can be initialized like this:
const myClass = new MyProxyClass('Foo');
myClass.setMessage('Bar');

// See if myClass.msg has been correctly set
myClass.getMessage(); // "Bar"

This will log the following entries, in order:

> CONSTRUCT new MyClass("Foo")
> CALL MyClass.setMessage("Bar")
> SET MyClass.msg = "Bar"
> CALL MyClass.getMessage()
> GET MyClass.msg
< "Bar"

Custom logging

By default, this module will log everything that happens inside a class, with the exception of calling native functions (such as .toString or .call) because these will return in nearly infinite loops. Javascript calls these functions all the time, for example when accessing or setting properties.

console.log is the default function used for logging, but this can be overwritten by supplying your own logfunction.

const logArr = [];
const logger = msg => logArr.push(msg);

const MyProxyClass = new ProxyGenerator(MyClass, {
  logFunction: logger
});

const myClass = new MyProxyClass('Foo');
myClass.setMessage('Bar');
myClass.getMessage();

Now all log entries are pushed the array instead of logged to console:

[ '> CONSTRUCT new MyClass("Foo")',
  '> CALL MyClass.setMessage("Bar")',
  '> SET MyClass.msg = "Bar"',
  '> CALL MyClass.getMessage()',
  '> GET MyClass.msg',
  '< "Bar"' ]