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

@pinpoint-apm/shimmer

v1.2.2

Published

Safe(r) monkeypatching for JavaScript.

Downloads

880

Readme

Changes

https://github.com/othiym23/shimmer.git differs in that it prevents duplicates monkeypatch.

Safer monkeypatching for Node.js

shimmer does a bunch of the work necessary to wrap other methods in a wrapper you provide:

var http = require('http');
var shimmer = require('shimmer');

shimmer.wrap(http, 'request', function (original) {
  return function () {
    console.log("Starting request!");
    var returned = original.apply(this, arguments)
    console.log("Done setting up request -- OH YEAH!");
    return returned;
  };
});

Mandatory disclaimer

There are times when it's necessary to monkeypatch default behavior in JavaScript and Node. However, changing the behavior of the runtime on the fly is rarely a good idea, and you should be using this module because you need to, not because it seems like fun.

API

All monkeypatched functions have an attribute, Symbol('__wrapped'), set to true on them.

shimmer(options)

If you pass in an options object containing a function labeled logger, shimmer will use it instead of the logger, which defaults to console.error. shimmer is built to be as unobtrusive as possible and has no need to run asynchronously, so it defaults to logging when things fail, instead of throwing.

shimmer.wrap(nodule, name, wrapper)

shimmer monkeypatches in place, so it expects to be passed an object. It accepts either instances, prototypes, or the results of calling require. name must be the string key for the field's name on the object.

wrapper is a function that takes a single parameter, which is the original function to be monkeypatched. shimmer assumes that you're adding behavior to the original method, and not replacing it outright. If you are replacing the original function, feel free to ignore the passed-in function.

If you aren't discarding the original, remember these tips:

  • call the original with something like original.apply(this, arguments), unless your reason for monkeypatching is to transform the arguments.
  • always capture and return the return value coming from the original function. Today's null-returning callback is tomorrow's error-code returning callback.
  • Don't make an asynchronous function synchronous and vice versa.

shimmer.massWrap(nodules, names, wrapper)

Just like wrap, with the addition that you can wrap multiple methods on multiple modules. Note that this function expects the list of functions to be monkeypatched on all of the modules to be the same.

shimmer.unwrap(nodule, name)

A convenience function for restoring the function back the way it was before you started. Won't unwrap if somebody else has monkeypatched the function after you (but will log in that case). Won't throw if you try to double-unwrap a function (but will log).

shimmer.massUnwrap(nodules, names)

Just like unwrap, with the addition that you can unwrap multiple methods on multiple modules. Note that this function expects the list of functions to be unwrapped on all of the modules to be the same.