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

stackrabbit

v1.0.0

Published

Expressive Middleware for RabbitMQ Listeners

Downloads

26

Readme

StackRabbit [Beta]

Circle CI

Koa inspired(some might say copied) framework for writing RabbitMQ Listeners.

Expressive ~~HTTP~~ RabbitMQ middleware for node.js to make ~~web applications and APIs~~ Rabbit Listeners more enjoyable to write. ~~Koa's~~ StackRabbit's middleware stack flows in a stack-like manner, allowing you to perform actions downstream then filter and manipulate the response upstream. ~~Koa's~~ StackRabbit's use of generators also greatly increases the readability and robustness of your application.

Installation

npm install stackrabbit

Getting Started

const stackrabbit = require('stackrabbit')
const contentParser = require('stackrabbit-content-parser')

const listener = stackrabbit({
  queueName: 'some-queue',
  hosts: ['rabbit.dev'],
  username: 'guest',
  password: 'guest'
})

// You can write middleware just like in Koa
listener.use(function *(next){
  const start = new Date()
  yield next
  const ms = new Date() - start
  console.log('message processed in %s', ms)
})

// Use some more middleware
listener.use(contentParser())

listener.listen(function * () {
  // do something with the message.
  // this.content is available when using the contentParser middleware
  console.log(this.content)

  // make sure to acknowledge you processed the message
  this.app.channel.ack(this.message)
})

listener.on('error', (err) => {
  console.error(err.stack)
})

listener.connect()

Guide

This guide includes suggestions and best practices for using stackrabbit.

Writing Middleware

Middleware in stackrabbit, much like middleware in koa, are simple functions which return a GeneratorFunction and accept another. When the middleware is run by an "upstream" middleware, it must manually yield to the "downstream" middleware.

Here is an example of adding a unique guid to the header of each message and logging before and after the message has been processed.

const uuid = require('node-uuid')

listener.use(function * requestId(next) {
  const start = new Date()
  const headers = this.message.properties.headers

  headers['x-request-id'] = headers['x-request-id'] || uuid.v4()

  console.log('Processing Message: %s', headers['x-request-id'])

  yield next

  const ms = new Date() - start
  console.log('Message %s Processed in %s ms', headers['x-request-id'], ms)
})

Middleware Best Practices

When creating public middleware it's useful to conform to the convention of wrapping the middleware in a function that accepts options, allowing users to extend functionality. Even if your middleware accepts no options, this is still a good idea to keep things uniform.

Here is a requestId middleware that allows changing the header key through configuration.

function requestId(key) {
  key = key || 'x-request-id'

  return function * requestId(next){
    const headers = this.message.properties.headers
    headers[key] = headers[key] || uuid.v4()

    yield next
  }
}

app.use(requestId());
app.use(requestId('some-other-key'));

Named Middleware

Naming your middleware is optional but it helps debugging.

function namedFn() {
  return function * namedFn(next){
    yield next
  }
}

Error Handling

Errors thrown in the middleware stack or message handler with be caught and the application with emit an event of name error. You should listen for this event and log it accordingly.

listener.on('error', (err) => {
  console.error(err.stack)
})

Inside of your middleware you can easily try/catch the downstream middleware to handle the event. Make sure you throw the error when you're done so the errors can be handled by functions earlier in the middleware chain and get caught in the default error handler.

listener.use(function * (next){
  try {
    yield next
  } catch (err) {
    //  do something with the err
    throw err
  }
})

Available Middleware

stackrabbit does not come bundled with any middleware. you'll might want to include some of these packages.

License

MIT