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

@jakguru/amqplib-oop-axios

v1.0.5

Published

A client and server allowing HTTP requests to be made from dedicated amqplib consumers.

Downloads

69

Readme

amqplib-oop-axios

Welcome to amqplib-oop-axios, an innovative library designed to revolutionize how axios requests are processed in a distributed system. Built upon the robust foundation of amqplib-oop-ratelimiter, this library introduces a seamless integration between axios and AMQP brokers like RabbitMQ, facilitating a decoupled architecture where requests are sent from the requester to a remote worker for processing.

amqplib-oop-axios consists of a mechanism to generate either single-use or multi-use adapters for use with axios requests, coupled with a dedicated worker module. This powerful combination ensures that axios requests utilize the same API that you are comfortable with while benefiting from the remote processing capabilities. By leveraging an AMQP broker, amqplib-oop-axios not only optimizes request handling but also maintains the integrity and reliability of the communication process.

Doc Coverage Badge

Example Use Cases

  • Rate Limit all requests across your various services to a specific API services
  • Authenticate a request to a service without using axios interceptors without exposing credentials to the requester
  • Horizontally increase processing bandwidth for requests by utilizing a fleet of workers without impacting the core application
  • Distribute a request workload between multiple workers while maintaining the promise chain

Important Notes

  • stream Response Types are not possible since there is no direct connection between the worker(s) and the requesting adapter
  • The beforeRedirect property cannot be set on the requesting adapter, only the worker.
  • The httpAgent property cannot be set on the requesting adapter, only the worker.
  • The httpsAgent property cannot be set on the requesting adapter, only the worker.
  • The lookup property cannot be set on the requesting adapter, only the worker.
  • The requesting adapter and the worker maintain separate transformRequest, transformResponse, and paramsSerializer properties. Be careful not to run the same functionality twice or you may encounter some unexpected results.
  • This library uses v8.serialize and v8.deserialize for serialization of responses and event information. If you are running different versions of NodeJS, this may not behave as expected.
  • Defaults set on the reqesting axios instance override defaults set on the worker
  • The request / response interceptors on the reqesting axios instance and on the worker are both run in the order:
    1. Requesting Axios Instance Request Interceptor
    2. Worker Instance Request Interceptor
    3. Worker Instance Response Interceptor
    4. Requesting Axios Instance Response Interceptor

Installation

npm install @jakguru/amqplib-oop-axios

or

yarn add @jakguru/amqplib-oop-axios

Worker Usage

Import / Require the worker class

import { AmqplibAxiosWorker } from '@jakguru/amqplib-oop-axios'

or

const { AmqplibAxiosWorker } = require('@jakguru/amqplib-oop-axios')

Setting up the worker

const worker = new AmqplibAxiosWorker(
    'example-queue',
    {}, // amqplib connection options
    {
        interval: 1000, // 1 second in ms
        perInterval: 10, // 10 requests per interval
        autostart: true,
    },
    {
        baseURL: 'https://some-domain.com/api',
    }
)
// Optional: Update the axios defaults
worker.defaults.headers.common['Authorization'] = AUTH_TOKEN;

// Optional: Add interceptors
worker.interceptors.request.use(function (config) {
    // Do something before request is sent
    return config;
  }, function (error) {
    // Do something with request error
    return Promise.reject(error);
  });

Adapter Usage

Import / Require Adapter Manager

import { AmqplibAxiosAdapterManager } from '@jakguru/amqplib-oop-axios'

or

const { AmqplibAxiosAdapterManager } = require('@jakguru/amqplib-oop-axios')

Retreiving the Adapter

In order to ensure that the script doesn't hang unexpectedly, if the adapter is initialized with ConnectionConstructorOptions, the connection will be closed once the request is complete and the adapter cannot be re-used. In order to create a reusable adapter instance, you should initialize the adapter with an already initialized Connection instance.

Note: If you initialize the adapter with an already initialized Connection instance, you will need to manually close the connection when you are done with it.

Using a single-use adapter with a single-use request

axios({
  method: 'post',
  url: '/user/12345', // The full domain / URL is not needed because baseUrl is set on the worker
  data: {
    firstName: 'Fred',
    lastName: 'Flintstone'
  },
  adapter: AmqplibAxiosAdapterManager.make('example-queue', {
    // amqplib connection options
  })
}).then((response) => {
    // handle response normally
}).catch((error) => {
    // handle errors as you see fit
});

Creating a multi-use adapter

import { Connection } from '@jakguru/amqplib-oop'
const connection = new Connection()
const adapter = AmqplibAxiosAdapterManager.make('example-queue', connection)

Using a multi-use adapter with a single-use request

axios({
  method: 'post',
  url: '/user/12345', // The full domain / URL is not needed because baseUrl is set on the worker
  data: {
    firstName: 'Fred',
    lastName: 'Flintstone'
  },
  adapter,
}).then((response) => {
    // handle response normally
}).catch((error) => {
    // handle errors as you see fit
});

Using a multi-use adapter with a multi-use axios instance

const instance = axios.create({
    adapter,
})

instance({
  method: 'post',
  url: '/user/12345', // The full domain / URL is not needed because baseUrl is set on the worker
  data: {
    firstName: 'Fred',
    lastName: 'Flintstone'
  },
}).then((response) => {
    // handle response normally
}).catch((error) => {
    // handle errors as you see fit
});

Client Usage

Import / Require Client

import { AmqpAxiosProxiedClient } from '@jakguru/amqplib-oop-axios'

or

const { AmqpAxiosProxiedClient } = require('@jakguru/amqplib-oop-axios')

Create an Instance of the AmqpAxiosProxiedClient

const instance = new AmqpAxiosProxiedClient('<queue>', {})
instance({
  method: 'post',
  url: '/user/12345', // The full domain / URL is not needed because baseUrl is set on the worker
  data: {
    firstName: 'Fred',
    lastName: 'Flintstone'
  },
}).then((response) => {
    // handle response normally
}).catch((error) => {
    // handle errors as you see fit
});

How it Works

The following is a flowchart of how amqplib-oop-axios handles the flow of information.

                                                                           ┌────────────┐
                                                                           │ Queue Name │
                                                                           └─────┬──────┘
                                                                                 │
                                                                                 │
                                                                                 │
        ┌───────────┐                                                            │
┌───────► Requester │                        ┌────────────────────────┐          │
│       └─────┬─────┘                        │ AMQP Broker Connection │          │
│             │                              └───────────┬────────────┘          │
│             │                                          │                       │
│     ┌───────▼────────┐                ┌────────────────▼──────────────────┐    │
│     │ Axios Instance │                │ amqplib-oop-axios Adapter Manager ◄────┴
│     └───────┬────────┘                └────────────────┬──────────────────┘
│             │                                          │
│             │                                          │
│      ┌──────▼─────────┐                   ┌────────────▼──────────────┐
│      │ Request Config ◄───────────────────┤ amqplib-oop-axios adapter │
│      └──────┬─────────┘                   │    for specific queue     │
│             │                             └───────────────────────────┘
│             │
│             │                                    ┌───────────────┐
│             │                       ┌────────────► Request Queue ├
│             │                       │            └───────────────┘
│         ┌───▼─────┐                 │
│         │         ├─────────────────┤     ┌───────────────────────────────┐
│         │ Request │                 └─────► Request Specific Cancel Queue ├
└─────────┤ Promise ◄──────┐                └───────────────────────────────┘
          │         │      │
          └─────────┘      │               ┌─────────────────────────────────┐
                           │   ┌───────────┤ Request Specific Response Queue │
                           │   │           └─────────────────────────────────┘
                           │   │
                           │   │        ┌────────────────────────────────────────┐
                           └───┼────────┤ Request Specific Upload Progress Queue ◄
                               │        └────────────────────────────────────────┘
                               │
                               │       ┌──────────────────────────────────────────┐
                               └───────┤ Request Specific Download Progress Queue ◄
                                       └──────────────────────────────────────────┘

                                    ┌────────────┐
                                    │ Queue Name │
                                    └─────┬──────┘
                                          │
                                          │
                                          │
                                          │
      ┌────────────────────────┐          │      ┌────────────────────────┐   ┌───────────────────┐
      │ AMQP Broker Connection │          │      │ AMQP Broker Connection │   │ Rate Limit Config │
      └───────────┬────────────┘          │      └───────────┬────────────┘   └─┬─────────────────┘
                  │                       │                  │                  │
 ┌────────────────▼──────────────────┐    │             ┌────▼──────────────────▼──┐   ┌──────────────┐
 │ amqplib-oop-axios Adapter Manager ◄────┴─────────────► amqplib-oop-axios Worker ◄───┤ Axios Config │
 └────────────────┬──────────────────┘                  └────────────┬──┬──────────┘   └──────────────┘
                  │                                                  │  │
                  │                                                  │  │
     ┌────────────▼──────────────┐                                   │  │
     ┤ amqplib-oop-axios adapter │                                   │  │
     │    for specific queue     │                                   │  │
     └───────────────────────────┘                                   │  └────────────────────┐
                                                                     │                       │
            ┌───────────────┐                             ┌──────────▼───────────┐   ┌───────▼────────┐
            ► Request Queue ├───────────────────┬─────────► Worker Job Processor │   │ Axios Instance │
            └───────────────┘                   │         └──────────┬───────────┘   └──▲─────────┬───┘
                                                │                    │                  │         │
     ┌───────────────────────────────┐          │                 ┌──▼──┐               │         │
     ► Request Specific Cancel Queue ├──────────┘                 │ Job ├───────────────┘         │
     └───────────────────────────────┘                            └─────┘                         │
                                                                                                  │
    ┌─────────────────────────────────┐                                                           │
    ┤ Request Specific Response Queue │◄───────────┐                                              │
    └─────────────────────────────────┘            │                                              │
                                                   │                                              │
 ┌────────────────────────────────────────┐        │                                              │
 ┤ Request Specific Upload Progress Queue ◄────────┼──────────────────────────────────────────────┘
 └────────────────────────────────────────┘        │
                                                   │
┌──────────────────────────────────────────┐       │
┤ Request Specific Download Progress Queue ◄───────┘
└──────────────────────────────────────────┘