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 🙏

© 2025 – Pkg Stats / Ryan Hefner

logicbox

v0.6.2

Published

Organize your application logic into manageable components

Readme

logicbox.js

NPM version Build Status Coverage Status Dependency Status devDependency Status

Logicbox is a utility to organize your application logic into managable components seprate from your transport layer. The concept of this module is inspired by Architecture the Lost Years keynote by Robert Martin and the implementation is inspired by substation.

Installation

npm install logicbox --save

Quick Example

var env = {
    logger: require('./logger'),
    utils:  require('./utils'),
    models: require('./models')
};

var createUser = function(env, input, cb) {
  env.models.User.create(input, function(err, user) {
    if(err) { return cb(err); }

    env.logger.log("user created!");
    cb(null, user);
  }
};

var errLogger = function(env, output, err) {
    if(err) { env.logger.log(err); }
}

var config = {
  options: {
    basePath: require('path').join(process.pwd(), 'usecases')
  },
  actions: {
    createUser: {
      handler: createUser,
      observer: errLogger
  }
};

// give logicbox an application environment and config, and you are good to go.
var dispatch = require('logicbox')(env, config);

server.post('/users', function(req, res) {
  var user = request.params.user;

  // dispatch 'createUser' action with user as the input
  dispatch('createUser', user, function(err, user) {
    if(err) { return response.error(err); }
    response.end(user);
  });
});

Actions

An action encapsulates a peiece of business logic, or use case. It must contain at least one handler, and can optionally contain one or more pre / post processors and observers. It is configured in config.actions object.

The simplest example:

// config.js
module.exports = {
  actions: {
    createUser: 'usecases/user/create'
  }
}

This example creates an action named createUser and the handler is specified in usecases/user/create. The value of actions options can contain:

  • String - Logicbox requires supplied string by appending options.basePath.
  • Function - It is used directly. Signature: function(env, input, cb){...}.
  • Object - See below.

Specifying an action with action object:

// config.js
module.exports = {
  actions: {
    createPost: {
      handler: 'usecases/post/create',
      pre:  [
        'preprocessors/authenticate',
        'preprocessors/authorize'
      ],
      post: [
        'postprocessors/convert-to-api-objects',
        'postprocessors/add-hypermedia-links',
        'postprocessors/convert-to-json'
      ],
      observer: [
        'observers/log-event',
        'observers/send-email'
      ]
  }
}

The action object must have a handler, and can optionally specify pre, post, observer keys. handler can accept string or function. pre, post, observer can accept string, function or array.

Global processors and observers

// config.js
module.exports = {
  global: {
    pre:  [
      // ...
    ],
    post: [
      // ...
    ],
    observer: [
      // ...
    ]
  }
}

Similarly, you can define global pre, post, observer.

Execution Order

The order is: global.pre -> pre -> handler -> post -> global.post -> callback. Each processor / handler's output becomes the input of the next. If an error is returned in any part of the chain, the execution is halted, and the callback function will be run with the error. If the entire chain is completed, the callback is invoked with the output of the last handler / processor in the chain.

observers will be invoked after the completion of the chain. observers have the signature of function(env, output, err, done); and the done callback does not take any arguments, so errors has to be handled within the observer. The order for observers is observer -> global.observer.

Signatures

Pre / Post processors and handlers all have the following signature:

function(env, input, callback)

The callback is a standard node.js style callback, and has the following signature:

function(error, output)

Observers have the following signature:

function(env, output, error, done)

The done callback should be called when a observer is completed. Errors inside Observers should be handled internally. As the done callback does not accept errors.

Example Handler

module.exports = function(env, input, callback) {
  // hopefully the input is santized by a pre-processor
  // and is data-massaged into something usable when it reaches here.
  env.models.User.create(input, function(error, user) {
    // something went wrong, let Logicbox know.
    if(err) { return callback(error); }

    // everyting is peachy, continue down the chain.
    // maybe a post process will use `user` as their input,
    // or this could be the end of the chain, in that case
    // the dispatcher's callback will get the result.
    callback(null, user);
  });
}