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

spring-river

v1.0.36

Published

Asynchronous Queue-based Microservices

Downloads

27

Readme

spring-river

Disclaimer

This library is my attempt to make a javascript version of Fred George's (@fredgeorge) RapidsRivers following Zeit's Micro simple bootstrap approach. This is just an experiment at this point.

For more information on the Rapids/Rivers/Ponds paradigm, please refer to the links at the bottom of this file.

Usage

If you learn by looking at code first, check out examples directory.

Now onto the step-by-step:

First, you need to install spring-river as a runtime dependency:

npm install spring-river --save

This library requires an amqp-compatible queue. RabbitMQ works perfectly. In order to configure the queue address, you can either set the environment variable ADDRESS or add the --address (or -a) flag when starting your services and set the address as amqp://….

River

If your microservice is the one generating "solutions", add a {"start": "river"} script, like the following, to your package.json:

{
  "main": "index.js",
  "name": "my-microservice-river",
  "scripts": {
    "start": "river"
  }
}

Your index.js will look like the following. It accepts a packet, which is, for all purposes, a plain javascript object with the packet's contents, and a publish function, which can be used to publish conclusions back to the rapids.

module.exports = (packet, publish) => {
  packet.solution = 10;
  publish(packet);
};

To get you service running, all you need to do is to run:

npm start

Spring

If your microservice is the one generating "needs", add a {"start": "spring"} script, like the following, to your package.json:

{
  "main": "index.js",
  "name": "my-microservice-spring",
  "scripts": {
    "start": "spring"
  }
}

Your index.js will look like the following. It accepts a publish function, which can be used to publish conclusions (a need is a conclusion) to the rapids.

module.exports = (publish) => {
  setInterval(() => {
    publish({need: 'rental_car_offer'});
  }, 5000)
};

To get you service running, all you need to do is to run:

npm start

Predicates

By definition, your exported function will be called whenever a packet arrives (this is so we can keep the bus a dumb pipe). In order to filter out and focus only on interesting packets, you can use decorators around your to-be-exported function. E.g.:

const myFilter = require('./my-filter');

module.exports = myFilter((packet, publish) => {
  packet.solution = 10;
  publish(packet);
});

Where my-filter.js could be something that filters only packets that have {need: 'rental_car_offer'}:

module.exports = (next) => (packet, publish) => {
  if (packet.need === 'rental_car_offer') {
    next(packet, publish);
  }
}

This library ships with a few built-in predicates that you might find useful (perhaps these are candidates to be moved out into their own modules, as these are just very simple functions).

Parameters: (...string): a list of forbidden keys

Parameters: (...string): a list of required keys

Parameters: (object): the object with the required key-value pairs

Usage

The following example is interested in packets that contain the {need: 'rental_car_offer'} key-value pair and does not have a solution key:

const predicates = require('spring-river/predicates');
const {requireValues, forbidKeys} = predicates;

module.exports = predicates(
  requireValues({need: 'rental_car_offer'}),
  forbidKeys('solution')
)((packet, publish) => {
  packet.solution = 10;
  publish(packet);
});

Credits

First and foremost, everything here is built based on Fred George's (@fredgeorge) Rapids/Rivers/Ponds paradigm, so here's a list of resources if you want to get more familiar with this idea:

  • https://vimeo.com/79866979
  • https://youtu.be/2rKEveL55TY
  • https://github.com/fredgeorge/rapids_rivers
  • https://github.com/fredgeorge/microservice_workshop
  • https://www.infoq.com/news/2016/02/not-just-microservices

The structure of this module was inspired by Zeit's Micro, so bin/index.js borrows a lot from it.

License

This software is licensed under the MIT license.