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

pluggable

v1.1.4

Published

Write your application in functional style, and replace or transform any part.

Downloads

35

Readme

Pluggable

Circle CI Code Climate

Write your framework or application code in a functional style, and Pluggable will allow consumers of your code to override or transform any part of execution.

Async is fully supported via Promises, and a code analytics tool is made available to compile a dependency graph with metadata for all pluggables you've defined in your code.

Define a pluggable

A pluggable function is defined like so:

const myFunctionName = pluggable(function myFunctionName (myArg) {
  // reference functions this.depA, this.depB, and this.depC
}, { depA, depB, depC });

It's normal function equivalent would look like:

function myFunctionName (myArg) {
  // reference functions depA, depB, and depC
}

Define a plugin

const myPlugin = function (override, transform) {
  // Any data to persist direct pluggable invocation should be defined here.

  // Use `override` if you'd like to completely change the implementation of
  // a pluggable function.
  override("somePluggableDependency", function (username) {
    if (CONDITION) {
      return somethingOtherThanDefaultValue;
    }
    // If you'd like to only override a pluggable's behavior in certain
    // circumstances, you can CONTINUE to the next pluggable override
    // or the default behavior.
    return override.CONTINUE;
  });

  // Use `transform` if you'd like to modify the output of a pluggable function.
  transform("getUserData", function (value, args) {
    // You have access to the pluggable function's output as `value` here.
    // You have access to the originally-passed arguments as `arg` here.
    return Object.assign({}, value, { iveBeenTransformed: true });
  });
}

Get a pluggable context and invocation

import { getBaseContext } from "pluggable";
const cxt = getBaseContext(
  { anything: "you want to go in this goes here" },
  [ AllOfYour, Plugins, GoHere ]
);
myPluggableFunction.call(cxt, allOf, your, normalArgs);

Full Example

How this works is more easily explained using an example. Imagine you had a module that looked something like this:

import github from "github";

function buildSummaryString (userData) {
  const [signupDate, projects, contactInfo] = userData;
  return `User "${username}" signed up on ${signupDate} and has ${projects.length} projects.`;
}

function getUserData (username) {
  return Promise.all(
    github.getSignupDate(username),
    github.getProjects(username),
    github.getContactInfo(username)
  );
}

function getUserSummaries (usernames) {
  return Promise.all(usernames.map(username => {
    return getUserData(username).then(buildSummaryString);
  }));
}

// Which would be used like so.
getUserSummaries(["divmain"]).then(console.log);

This is a simple pattern of inputs and outputs - it is easy to test, and easy to reason about.

However, what if you'd like to allow consumers of this module to modify the behavior of one of the functions. What if they want to add additional information, or what if they want to override the behavior altogether? That's where pluggable comes in.

First, let's rewrite the above example using pluggables - then we'll look at how to modify behavior.

import github from "github";
import { pluggable } from "pluggable";

const buildSummaryString = pluggable(function buildSummaryString (userData) {
  const [signupDate, projects, contactInfo] = userData;
  return `User "${username}" signed up on ${signupDate} and has ${projects.length} projects.`;
});

const getUserData = pluggable(function getUserData (username) {
  return Promise.all(
    github.getSignupDate(username),
    github.getProjects(username),
    github.getContactInfo(username)
  );
});

const getUserSummaries = pluggable(function getUserSummaries (usernames) {
  return Promise.all(usernames.map(username => {
    return this.getUserData(username).then(this.buildSummaryString.bind(this));
  }));
}, { buildSummaryString, getUserData });

You'll want to especially take note of the changes in getUserSummaries. Instead of referring to getUserData and buildSummaryString directly, it refers to this.getUserData and this.buildSummaryString. Under the hood, pluggable creates this contexts that make pluggable dependencies available.

Now lets look at how this might be invoked:

import { getBaseContext } from "pluggable";
const cxt = getBaseContext();
getUserSummaries.call(cxt, ["divmain"]);

There's not much extra here beyond the original implementation, aside from providing a context for the function invocation.

Now let's look at how you might implement a caching plugin.

const cacheUserData = function (override, transform) {
  const cache = {};

  override("getUserData", function (username) {
    if (username in cache) {
      return cache[username];
    }
    return override.CONTINUE;
  });

  // NOTE: we're not actually transforming the output here, although we could.
  transform("getUserData", function (userData, args) {
    const [username] = args;
    cache[username] = userData;
    return userData;
  });
}

You'd invoke getUserSummaries like so:

import { getBaseContext } from "pluggable";
const cxt = getBaseContext({}, [ cacheUserData ]);
getUserSummaries.call(cxt, ["divmain"]);