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

koa-omnibus

v1.0.0-rc3

Published

Base middleware for `koa` servers.

Downloads

59

Readme

koa-omnibus

CircleCI Known Vulnerabilities Maintainability Test Coverage

I wouldn't use this in production until it's more battle-tested. (at least v1)

It does exactly everything you want an API server to do, and nothing more.

Put this at the front of your koa@^2 middleware chain and get hacking.

What do you mean? I want to try before I buy.

Clone, then: npm install && npm install koa && npm run demo or:

Slap this in node server.js | bunyan after npm install koa koa-omnibus:

const omnibus = require('koa-omnibus')
omnibus.createApplication().listen()
// or: application.use(omnibus())

Defaults not good enough? Okay...

All dependencies are included only for defaults; you can override anything you want.

omnibus({

	// simple constants: (all Numbers are in milliseconds)
	headers: { timing:String, tracing:String }, // defaults: 'X-Response-Time' and 'X-Request-ID'
	limits: Object { age:Number, max:Number, next:Number, rpm:Number }, // 60000/1000000/60000/1000
	namespace: String, // default: 'omnibus' (if set false-y, context.state decorated directly)

	// middleware factories (unary):
	limitRate: options:Object => middleware:AsyncFunction, // 429 Too Many Requests
	limitTime: options:Object => middleware:AsyncFunction, // 408 Request Timeout

	// redaction transforms (binary):
	redactedError: (options, context) => error, // do whatever transform(s) you want here
	redactedRequest: (options, context) => _.omit(context.request, ['header']), // " here
	redactedResponse: (options, context) => _.omit(context.response, ['header']), // " here

	// object factories (ternary):
	targetError: (options, context, error) => error, // not redacted (default: Boom)
	targetTracer: (options, context, string) => Object { [string]: "$(uuidgen -t)" },
	targetLogger: (options, context, object) => log, // will include tracking headers
	targetObject: (options, context, object) => object, // context[namespace || 'state']

})

Oh, yeah. One more thing:

This doesn't include any of these modules, but you can .use them, as you wish:

  • https://www.npmjs.com/package/koa-bodyparser
  • https://www.npmjs.com/package/koa-compress
  • https://www.npmjs.com/package/koa-etag
  • https://www.npmjs.com/package/koa-favicon
  • https://www.npmjs.com/package/koa-helmet
  • https://www.npmjs.com/package/koa-logger
  • https://www.npmjs.com/package/koa-mount
  • https://www.npmjs.com/package/koa-ratelimit
  • https://www.npmjs.com/package/koa-rewrite
  • https://www.npmjs.com/package/koa-session
  • https://www.npmjs.com/package/koa-static
  • https://www.npmjs.com/package/@koa/cors

But seriously, what is this, and how does it work?

You want your server to do a few things for you:

  • If the user (tracks $max IPs for $age) has made Too Many Requests ($rpm) throw 429.
  • If the middleware chain has been executing for too long ($next) 408 Request Timeout.
  • It would be nice if my REST API actually produced error bodies for JSON consumers.
  • It would be nice if my logging actually worked how I want it to. (see Bunyan)
  • It would be nice if my logs included what I want, and none of what I don't.
  • It would be nice if the defaults made sense, and nothing got in my way.
  • It would be nice if I could later customize everything how I like it.

Sweet. So, how's it work? See defaults.js. Swish.