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

amqp-route

v1.1.0

Published

Data Routing Helpers for AMQP

Downloads

10

Readme

Utility for AMQP Message Routing

Describe AMQP exchange and routing keys in a compact way (and as templates). Purpose is to allow compact (and external, e.g. via configuration strings) declaration of AMQP Exchange and routing keys, and to create routing key based on message data.

Installation

npm install amqp-route

Basic Usage

Create a template with require("amqp-route").template(tplString). Then make a routing key by using fill method with data objects.

var route = require("amqp-route").template("MyExchange/route.key.{str}")
route.exchange;            // "MyExchange"
route.fill({str: "foo"});  // "route.key.foo"
route.fill({});            // "route.key" (strips dots)
route.fill({skip: "here"}, {str: "hereIam"}) // fallback list of data objects
var route2 = require("amqp-route").template("route.key.{str}")
route2.exchange;           // "" (Exchange is optional)

Exchange Options

Generate a subset of available options for node-amqp connection.exchange().

var route = require("amqp-route").template("MyExchange?dc-a/{routingKey}")
route.exchangeOpts; // {durable: true, confirm: true, autoDelete: false}

Options are a single letter (to set to true), prefixed by a - (to set to false).

  • a: autoDelete
  • c: confirm
  • d: durable
  • n: noDeclare
  • p: passive

Publishing Options

Generate a subset of available options for node-amqp exchange.publish().

var route = require("amqp-route").template("{routingKey}?mip")
route.pubOpts; // {mandatory: true, immediate: true, deliveryMode: 2}

Simlar to Exchange Options (single letter, optional -). The exception is p (for "persistent", which sets deliveryMode.

  • m: mandatory
  • i: immediate
  • p: deliveryMode
    • "p" sets "deliveryMode=2" (persistent)
    • "-p" sets "deliveryMode=1" (non-persistent)

Template

Template object returned from require("amqp-route").template():

  • exchange: exchange name
  • exchangeOpts: parsed options suitable for node-amqp connection.exchange()
  • template: routing key template used in (template.fill())[#template-fill]
  • pubOpts: publishing options suitable for node-amqp exchange.publish()

template.fill(object[, object...])

Creates a routing key for some data by replacing all strings in curly braces with value from the first object having that property. Any leading, trailing, or double work separators (dots) are removed.

var route = require("amqp-route").template("{a}.{b}.{c}.{d}")
var rk = route.fill({a: "A"}); // "A"
rk = route.fill({a: "A"}, {a: "X", c: "C"}); // "A.C"
rk = route.fill({a: "A", b: "B", d: "D, c: null}); // "A.B.D"

Publisher

Publisher builds on Template, setting up an Exchange with options and always calling the callback, even when the Exchange is not in confirm mode. It also uses the publishing options from the Template.

var amqpRoute = require("amqp-route");
var amqpConn = require("amqp").createConnection(connectionOpts);
// create a publisher in confirm mode, durable and no autoDelete
amqpRoute.publisher(amqpConn, "My-Exchange?-acd/{routing}.{template}", function(err, pub){
  var routingKeySources = [{routing: "sample"}, {template: "key"}];
  var message = {important: "data"};
  pub.publish(routingKeySources, message, function(err){
    // message published with routing key "sample.key", confirm mode
    // when NOT using confirm mode, this is called via process.nextTick()
  });
});

Additionally, you can include further Exchange options.

var tpl = "My-Exchange?-acd/{routing}.{template}";
amqpRoute.publisher(amqpConn, tpl, {type: "direct"}, function(err, pub){
  // now we are using a Direct exchange instead of topic
});

publisher.publish(routeFillers, message[, pubOpts[, callback]])

Publish a message, additional options are passed to node-amqp exchange.publish() and any passed callback will be called regardless of confirm mode.

routeFillers is an array of sources to create routing key from Template. It corresponds to arguments passed in Template.fill().