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

hstacks

v1.0.0

Published

hierarchical stacks

Downloads

6

Readme

hstacks npm status

hierarchical stacks takes the middleware concept and expand it to a hierarchy of stacks. This construct was designed with http framework in mind but the implemetation tries to be as neutral as possible, so it will be usable for other problem domains.

install

With npm do:

npm install --save hstacks

example

A rudimentary http server with middlewares

var HStacks = require('hstacks')
var http = require('http')

var hstacks = new HStacks()

// mount some middlewares
// this is the root
hstacks.mount([''], function (req, res, next) {
    console.log(req.url)
    next()
})

hstacks.mount(['', 'a'], function (req, res, next) {
    console.log('/a middleware')
    res.end()
})

hstacks.mount(['', 'b'], function (req, res, next) {
    console.log('/b middleware')
    res.end()
})

http.createServer(function(req, res) {
    hstacks.dispatch(req.url.split('/'), [req, res])
})

concepts

path

A path inside a tree: e.g a->b->c. A path is always represented by an array ['a', 'b', 'c']. Each path might hold a stack or middlewares and a special error middleware.

stack

A stack is an array of middlewares nested inside a path in the tree. When middlewares are processed in a stack the are processed from index 0 to stack.length -1

middleware

A middleware function is a part of a stack and takes the form of:

function middleware(arg1, arg2, ... , next) {

}

Calling next()indicates that descendant stacks in the tree should also process the arguments. Calling next(err) will break the normal flow of execution and kick in the error middlewares

A middleware is mounted on a path:

hstacks.mount(['a', 'b'], function (msg, next) {
    console.log(msg)
    next()
})

hstacks.mount(['a', 'b'], function (msg, next) {
    console.log('hey its that message...', msg)
})

In this example a->b->c has two middlewares handling messages

error middleware

A special middleware the exists outside of any stack. An error middleware is invoked when a normal middleware calls its next() function with an error.

Mounting an error middleware is done through a different api:

hstacks.mountErrorMiddleware(['a'], function (err, next) {

})

And always have the same signature (as opposed to a normal middleware)

message processing flow

When dispatching a message to the tree, the message is processes by each stack along the path from the root downwards. For example a message dispatched to path ['a', 'b', 'c'] will be processed by the stack that resides in ['a'], the stack in ['a', 'b'] and finally by ['a', 'b', 'c']. Each middleware in each stack most call its next() callback or execution is halted.

error processing flow

When a middleware in one of the stacks calls its next() callback with an error, hstacks will look for an error middleware in the current path level and invoke it. If no error middleware is found, hstack will begin to traverse up the tree along the path looking to error middlewares. If none is found the error will be thrown (as a general rule one should always mount at least one error middleware)

api

new HStacks(trie:Trie (optional), context:Object (optional))

create a new instance of hstack

HStacks.dispatch(path:Array, args:Array)

dispatch a message to hstacks, this message will be handled by all the middlewares that reside along the provided path.

HStacks.mount(path:Array, middleware:Function)

mount a middleware in the tree

HStacks.getStack(path:Array)

gets the stack a particular path

HStacks.getErrorMiddleware(path:Array)

gets the stack a particular path

HStacks.mountErrorMiddleware(path:Array, middleware:Function)

see mount()

license

MIT © yaniv kessler