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

klox

v1.7.0

Published

Taking care of the plumbing

Downloads

582

Readme

KLOX

Klox is a basic API wrapper that abstracts the communication layer.

Our usecase is in a node.js microservice architecture where different services communicates with each other without having to implement the code for basic communication every time.

Klox is taking care of the plumbing.

Basic usage

In order to use klox, it needs to be configured.

First, let use require klox and its transports.

const klox = require('klox');
const Transports = require('klox/transports');

Let us define a basic client that is able to send requests.

let client = klox().sends().through(RabbitmqTransport).replies().through(RedisTransport);

We are basically making a klox instance, that is able to send API requests through RabbitMQ and the expect replies through Redis.

In order to make setup the server-side code to respond to the client, we could use the following snippet:

let server = klox()
    .receives().through(RabbitmqTransport).replies().through(RedisTransport);

It will receive messages through RabbitMQ and replies to them through RedisTransport.

The following example shows how to set up klox to both send and receive messages.

It is possible to mix sending, receiving and reply transports.

let api = klox()
    .receives().through(RabbitmqTransport).replies().through(RedisTransport)
    .sends().through(RabbitmqTransport).replies().through(RedisTransport);

If no transport is supplied to the "through" method, then the primary transport (RabbitMQ in this case), will be used as the reply transport.

let api = klox()
    .receives().through(RabbitmqTransport).replies().through(RedisTransport)
    .sends().through(RabbitmqTransport).replies().through(RedisTransport);

If the "replies" method is not called, klox will ignore listening to and sending replies.

let api = klox()
    .receives().through(RabbitmqTransport)
    .sends().through(RabbitmqTransport);

Timeouts

Example of server setting a timeout of 50 ms

let server = klox({timeout: 50, requeue_on_timeout: false})
    .receives().through(RabbitmqTransport).replies().through(RedisTransport);

By default, timeout is set to "-1", meaning that it is disabled.

requeue_on_timeout is used to tell klox what should happen to a message timing out. By default, it will requeue the payload, meaning that it will end up in a new listen instance somewhere.

Transports

Klox communicates independently across different transport, by internally using a coment set of interface methods.

Out of the box, klox supports the following transports: SocketIO, Redis, RabbitMQ.

They can all be accessed on the common interface below:

const Transports = require('klox/transports');

Redis

let RedisTransport = new Transports.Redis({host: '127.0.0.1', port: 6379});

Env variables

Redis will look for the following environment variables:

| Variable | Default value | | --- | --- | | REDIS_HOST | 127.0.0.1 | | REDIS_PORT | 6379 |

RabbitMQ

let RabbitmqTransport = new Transports.Rabbitmq({host: '127.0.0.1', port: 5672});

Env variables

RabbitMQ will look for the following environment variables:

| Variable | Default value | | --- | --- | | RABBITMQ_HOST | 127.0.0.1 | | RABBITMQ_PORT | 5672 |

SocketIOServer

View "test/socket.js" for an example

const klox = require('klox');
const Transports = require('klox/transports');

let SocketServerTransport = new Transports.SocketIOServer({
    port:4004
});

let server = klox().receives().through(SocketServerTransport).replies();

Env variables

The SocketIO server will look for the following environment variables:

| Variable | Default value | | --- | --- | | PORT | 4001 |

SocketIOClient

View "test/socket.js" for an example

const klox = require('klox');
const Transports = require('klox/transports');

let SocketClientTransport = new Transports.SocketIOClient({
    hosts: ['http://localhost:4004']
});

let client = klox().sends().through(SocketClientTransport).replies();

Releases

1.1.10

  • Removed logging

1.1.9

  • Fixing race condition issue in RabbitMQ creating too many connections

1.1.8

  • Bugfix

1.1.7

  • Bugfix

1.1.6

  • Implemented request validation and casting using Horai

1.1.5

  • Added option to pass credentials to RabbitMQ

1.1.4

  • Fixed potential memory leak in call multiple.

1.1.3

  • Fixed error where payload was sent despite requeue set to true.

1.1.2

  • Fixed memory leak in call reply.

1.1.1

  • Will cleanup internal registry of timeout timeouts.

1.1.0

  • Will not reply from listen, if set to requeue and payload has timed out.

1.0.9

  • Port option can be passed to SocketIOServer transport

1.0.8

  • Option to add timeouts on listen added. Klox accepts "timeout" and "requeue_on_timeout" options.

1.0.7

  • Bugfix, will no longer attempt to reply if no reply_on property exists on payload

1.0.6

  • Call accepts a fourth argument, a callback which is triggered whenever the payload is ready (For backward compatibility)

1.0.5

  • Only expect reply in call, if callback is supplied. Will prevent unneeded listen on reply endpoint

1.0.4

  • Verbose option added
  • RabbitMQ transport refactor

1.0.3

  • Type bugfix

1.0.2

  • RabbitMQ are by default non-durable

1.0.1

  • Call multiple added

1.0.0

  • Initial release