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

@leisurelink/wireup

v0.2.2

Published

A bootstrap time, convention-based utility for wiring up modules into an app (nodejs).

Downloads

2

Readme

wireup

A bootstrap time, convention-based utility for wiring up modules into an app (nodejs).

What

wireup introduces a simple, optional wireup step that applications can use to build a predictable startup process.

When a nodejs module is imported via node's require method, node goes through a process of loading the module. A synopsis of what happens when node loads a module is that it reads the module from disk if the requested module doesn't match a module already loaded and cached. When the module is indeed read from disk, node places the module in a new function scope and invokes the module so that it may initialize its dependencies, perform startup logic, and export things other modules rely upon.

OK, that was a simple explanation. For most modules, nothing else is needed.

Nodejs' require can be thought of as a module's self-initialization, or better yet; implicit-wireup.

wireup provides is a means by which modules can specify an optional, explicit-wireup.

How

There are four parts to using wireup effectively:

Understand it

wireup is a simple wrapper for nodejs' require function. For each module that you wire up, wireup looks for an exported function called "wireup" and if found, immediately calls it. Admittedly, this behavior on its own does little to improve on the implicit-wireup that already happens. The improvement comes in when you choose to inject arguments into the module's wireup function.

Basics

You can inject variables (such as a config object) into the modules' #wireup method as the modules are imported into your app:

// this ->                                    vvvvvv
var wireup = require('wireup').dir(__dirname, config);

// Notice we're using wireup to import the modules, not require:

var urmod = wireup('./ur-module');
var other = wireup('./other');

// gets injected here, ->                       vvvvvv
// ur-module::module.exports.wireup = function (config) { /* ... */ }

// and here ->                              vvvvvv
// other::module.exports.wireup = function (config) { /* ... */ }

There are more advanced uses documented later.

Determining Your Strategy

We've mentioned that wireup is convention-based. In actuality, it is half of the convention, the second half is up to you. We definitely don't know as much about your app as you do, so feel free to make up a strategy that makes sense to your community of users.

The basic example illustrates a simple but effective strategy which formalizes a convention for the app; each module's #wireup function will expect a single argument to be passed in -- the config object.

You can make up a different strategy, thus formalizing the convention that makes sense to you.

Wireup Enable Modules

To wireup enable your module, simply export a wireup function:

var util = require('util');

var _localConfig = {
  foo: 'setting one',
  bar: 'setting two'
};

// other module details elided...

module.exports.wireup = function (config) {
  // when/if wireup is called, overlay our default config...
  _localConfig = util._extend(_localConfig, config);
};

Use wireup in Your App

The basic example has already shown how to use wireup in your app. There is however a caveat; in order to enable some advanced usage, we differentiate between the app's root module and the other modules.

In your app's root module:

Your app's root module, call wireup's #dir method to specify your app's root directory [more info].

//...

var wireup = require('wireup').dir(__dirname);

var lib = wireup('./lib');

//...

In your app's other modules:

Other modules should not import wireup. If they need to be participate in the wireup process, they should export a function with the name wireup as illustrated above.

Guidelines & Advice for Easy Wireup

Since wireup is able to inject arguments into a wired module's exported #wireup method, the process is simpler if you use a consistent signature for your module's exported #wireup methods.

If you use an IoC, your IoC container makes a great candidate to pass along the wireup chain. Another great candidate is a top-level config object, like an nconf instance.

If at all possible, bind the wireup arguments in your application's entrypoint module. Since advanced binding appends wireup arguments but never removes previously bound arguments, we recommend using advanced binding only where absolutely necessary since it can make troubleshooting difficult when you've been away from your app for a while.

Advanced Binding

More on this later; study the examples to see how to bind additional wireup arguments at module scope and at each call-site.

License (MIT)