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

zen

v0.1.7

Published

is a simple, safe, basic, fast, general purpose module engine

Downloads

154

Readme

Zen

Z-engine (Zen) is the most simple, safe, minimal, fast, full featured, general purpose javascript module stack engine for Node.js.

Basically it's like Connect or Stack, but simpler and faster.

Zen comes into two flavours: zen-http and zen:

  • zen is a general purpose engine
  • zen-http is designed to be used for triadic modules (like http servers) where extreme performance is a requirement.

Install

As simple as

npm install zen

How to use

var zapp=require('../zen')(
  require('firstHandler')(),
  require('secondHandler')(handleArg1, handleArg2),
);

var result = zapp(<proper>, <application>, <args>); 

Explanation

Zen takes a list of handler functions and will chain them up by the next() method in a z-engine instance.

Each handler needs to be of the form:

function handler(<proper>, <application>, <args>, next) {
  // Either handle the request here using the arguments
  // or call `next()` to pass control to next module

  // next uses node.js callback convention: 
  // any exceptions need to be caught and forwarded to `next(err)`
  // result needs to be forwarded with `next(null,res)`	

  // without a call to `next` Zen "drops" the chain  
}

When using external modules we suggest to use the Setup Pattern, where the module is a callable function that returns the handler function.

module.exports = function setup(<some>, <useful>, <setup>, args) {
  // Do module setup stuff here
  return function handler(<proper>, <application>, <args>, next) {
    // Handle a request here
  };
};

What Zen Does

Zen does a few things under the hood.

  • Creates standalone module engines. Multiple engines could be chained together.
  • Uses continuation passing style, but as long as an application stack uses return next(); value returned from handle functions could be assigned to the caller.
  • Wraps handlers in a try..catch to catch any exception that happens running the engine.
  • Forwards errors and exceptions passed to any next module directly to the error handler. This means module doesn't have to worry about errors from previous modules.
  • Forwards result passed to any next module directly to the result handler. This avoids generic result handling in module's business logic

errorHandler and resultHandler could be overridden by custom functions. As handler above these needs to be of the form:

zapp.errorHandler = function(<proper>, <application>, <args>, err) {
}
zapp.resultHandler= function(<proper>, <application>, <args>, res) {
}

What Zen Does NOT Do

Due its general purpose, Zen does not provide any middleware modules of any kind.

API

  • zapp.errorHandler : this is the default request handler and the called handler on errors. Must be a function. When a Handler throws exception, this is catched by the original errorHandler (that prints on console).
  • zapp.resultHandler : this is the result handler. When it throws exception this is catched by the errorHandler.
  • zapp.pause : pauses the engine and buffers the requests.
  • zapp.stop : stops the engine, requests will be forwarded to the errorHandler.
  • zapp.resume : resumes the engine and flushes the requests buffer on the engine.

Zen-http

zen-http is a special flavour of Zen for triadic handlers (like http server). It includes proper HTTP result and error handlers and default 404 response. Connect and Stack compatible. Use next(err) to push a 500 error message to the client, next(null,result) to send the result with status 200.

Benchmarks

Zen flavours are faster than Stack and Stack2 on real world use cases

Conclusion

Zen is available on github under MIT license. If you found bugs, please fill issues on github. Feel free to fork, modify and have fun with it ;-)

Credits

Zen takes ideas from Connect and Stack