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 🙏

© 2025 – Pkg Stats / Ryan Hefner

magma

v0.1.2-rc0

Published

A AMQP-powered scalable back end framework.

Downloads

16

Readme

magma

magma is a framework for creating scalable AMQP back ends.

Install Magma

npm install magma

Create a Router

In magma, everything starts with a Router. Routers take in incoming commands and dispatch them to Workers using RabbitMQ. Currently, magma only has one type of Router - the HTTP router. The HTTP router uses express to take incoming HTTP requests and transmit them to a Worker.

var magma  = require('magma');
var router = magma.router();

router.connect(function () {
  router.resource('article');
});

var server = router.listen(3000, function () {
  var host = server.address().address;
  var port = server.address().port;

  console.log('Listening at http://%s:%s', host, port);
});

Resources

The Router above declares one resource: article. In magma, a resource acts as a type of object. Each resource can be created, read, updated, and deleted using HTTP POST, GET, PUT, and DELETE requests respectively. When a resource is declared on the Router, endpoints matching the name of that resource are automatically created. For example:

GET http://.../network

Access Express

The HTTP Router is based around the express framework. You can access the express instance using the Router.app property. This allows you to add helpful features, such as authentication middleware. On a similar note, the Router.listen method is basically a direct proxy to the express listen method.

Create a Worker

The next piece of a magma application is the Worker. Workers are in charge of performing a specific set of tasks related to a resource. While a Worker can handle more than one resource at a time, it's typically best practice to make a new worker for each declared resource. Workers can be spawned and despawned as the workload for the resource increases, and RabbitMQ will automatically handle load balancing between them.

It's important to rememeber that Workers should be—on their own—stateless. Any session or long term storage should be handled using a caching system such as Redis, or a database such as MongoDB. You should write your worker with the assumption that it can be spun up or down at any time.

var magma  = require('magma');
var worker = magma.worker();

worker.connect(function () {
  worker.resource('article', function () {
    this.create = function (req, res) {
      var articleId = req.data.id;                // Access data using `req.data`
      var authHeader = req.headers.authorization; // Access headers using `req.headers`
    };

    this.read = function (req, res) {
      res.success('good job!'); // Send successful responses using `res.success`
    };

    this.update = function (req, res) {
      res.fail('something went wrong!'); // Send failure responses using `res.fail`.
    };

    this.delete = function (req, res) {
      // And so on...
    };
  });
});

When binding a Worker to a Resource, you must implement the create, read, update, and delete methods for that resource. These methods are automatically invoked corresponding to the POST, GET, PUT, and DELETE HTTP requests from the Router.

You can access request data using the req.data object. Query string data as well as POSTed body data will be combined into this key value array.

Remote Procedure Calling

Traditionally it's best to stick with REST-ful API endpoints, which the default router/worker system provides. However, occasionally you need to divert from that path, which is where Magma's RPC feature comes in.

The Router

Start by declaring a router with a custom resource...

var magma  = require('magma');
var router = magma.router();

router.connect(function () {
  router.resource('network', [
    'customAction:get'
  ]);
});

var server = router.listen(3000, funciton () {
  // ...
});

By including a second parameter to the resource method, we have declared a new endpoint on the network resource called customAction. Because we suffixed the endpoint with :get, you will be able to access the custom endpoint like so:

GET http://.../network/customAction

In addition to the :get suffix, you can use :put, :post, and :delete for PUT, POST, and DELETE request respectively. You must include a suffix, there is no default.

The Worker

Now that you have a router defined with your customAction endpoint declared, creating a worker is pretty much business as usual except for one thing: in addition to defining your worker's create, read, update, and delete methods, you will also define a customAction method. The customAction will behave in the same way as the other CRUD methods.

var magma  = require('magma');
var worker = magma.worker();

worker.connect(function () {
  worker.resource('network', function () {
    this.customAction = function (req, res) {
      res.success('customAction');
    };

    this.create = function (req, res) {
      res.success('create');
    };

    this.read = function (req, res) {
      res.success('read');
    };

    this.update = function (req, res) {
      res.success('update');
    };

    this.delete = function (req, res) {
      res.success('delete');
    };
  });
});