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

wrapper-roo

v0.2.9

Published

JavaScript transparent function wrapper with reflection capabilities

Downloads

37

Readme

wrapper-roo

Wrap any function with custom hooks, executing code before and after the function, controlling arguments, result and so on, while still being as transparent as possible to the rest of the code.

Logo

npm version Node.js CI JavaScript Style Guide

Installation

The package can be locally installed from npm:

$ npm i wrapper-roo

Usage

Pre- and post-hooks

A hook is a function to be executed before or after a given one:

const wrap = require('wrapper-roo')

function hello (name) { console.log('hello ' + name) }

const wrapped = wrap(hello).withPrePostHooks(
  () => console.log('before'), // pre-hook
  () => console.log('after')) // post-hook

wrapped('Crash')
before
hello Crash
after

Arguments and return value are forwarded, thus the wrapper can be used just like the original function. See the wiki for more information.

Function Invocation Information

Hooks receive information about the function call as their first argument:

const wrap = require('wrapper-roo')

function foo () { /* ... */ }

const wrapped = wrap(foo).withPreHook(data => {
  console.log('Calling function ' + data.function.name)
  console.log('Args: ' + data.arguments)
})

wrapped(1, 'hey')
Calling function foo
Args: [1,hey]

Function invocation metadata object received by hooks contains the following properties:

  • function: original function object to be wrapped;
  • arguments: array of arguments that were given to the function;
  • constructor: value of new.target in the original call, namely, the function after the new keyword in constructor calls;
  • this: object bound to this for the call;
  • boundFunction: a version of the wrapped function that is already bound to arguments, this and new.target.

Additional fields are available to post-hooks:

  • result: return value;
  • exception: thrown exception;
  • success: whether the execution of the function completed normally or an exception was thrown.

Note: invocation data is read-only: modifications to properties above are not allowed and will have no effect on the function execution.

Custom Hooks

Greater control can be achieved with custom hooks, if needed:

const wrap = require('wrapper-roo')

function foo () { /* ... */ }

wrap(foo).withCustomHook((data, f) => { /* ... f() ... */ })

Custom hooks conveniently receive as a second argument the wrapped function already bound to this and arguments, and already set up as a constructor call if needed. Just call f().

Note that no automatic handling of binding, arguments, exceptions etc is performed when directly invoking the original function as data.function().

(Fluent) API

The following functions are exposed:

const wrap = require('wrapper-roo')

wrap(func).withPreHook(preHook)
wrap(func).withPostHook(postHook)
wrap(func).withPrePostHooks(preHook, postHook)
wrap(func).withCustomHook(hook)
wrap.the(func)  // just wrap it

Caveats

Of course, identity is not preserved: wrap.the(func) !== func.

The current implementation is based on the Proxy API. Caveats of this approach (corner cases) are listed here.

Contributing

Install Node.js and npm, clone the repo and cd into it.

To install dependencies:

$ npm i

To run linter, tests and get coverage report:

$ npm test

Wiki

See the wiki page for more information.