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

airbud

v4.0.0

Published

node.js request wrapper adding support for retries, exponential back-off, fixture serving, JSON

Downloads

91

Readme

Build Status

Retrieving stuff from the web is unreliable. Airbud adds retries for production, and fixture support for test.

air_bud_-_golden_receiver

Airbud is a wrapper around request with support for for handling JSON, retries with exponential backoff & injecting fixtures. This will save you some boilerplate and allow you to easier test your applications.

Install

Inside your project, type

npm install --save airbud

Use

To use Airbud, first require it

In JavaScript

var Airbud = require('airbud');

Or CoffeeScript:

Airbud = require "airbud"

Airbud doesn't care.

Example: simple

A common usecase is getting remote JSON. By default Airbud.json will already:

  • Timeout each single operation in 30 seconds
  • Retry 5 times over 10 minutes
  • Return parsed JSON
  • Return err if
    • A non-2xx HTTP code is returned (3xx redirects are followed first)
    • The json could not be parsed

In CoffeeScript:

Airbud.json "https://api.github.com/events", (err, events, meta) ->
  if err
    throw err
  console.log events[0].created_at

Example: local JSON fixtures

Say you're writing an app that among things, retrieves public events from the GitHub API.

Using environment variables, your production environment will have a GITHUB_EVENTS_ENDPOINT of "https://api.github.com/events", but when you source envs/test.sh, it can be "file://./fixtures/github-events.json".

Now just let Airbud.retrieve the process.env.GITHUB_EVENTS_ENDPOINT, and it will either retrieve the fixture, or the real thing, depending which environment you are in.

This makes it easy to test your app's depending functions, without having to worry about GitHub ratelimiting, downtime, or sloth when running your tests. All of this without making your app aware, or changing it's flow. In JavaScript:

var opts   = {
  url: process.env.GITHUB_EVENTS_ENDPOINT,
};

Airbud.json(opts, function (err, events, meta) {
  if (err) {
    throw err;
  }

  console.log('Number of attempts: '+ meta.attempts);
  console.log('Time it took to complete all attempts: ' + meta.totalDuration);
  console.log('Some auto-parsed JSON: ' + events[0].created_at);
});

Example: customize

You don't have to use environment vars or the local fixture feature. You can also use Airbud as a wrapper around request to profit from retries with exponential backoffs. Here's how to customize the retry flow in CoffeeScript:

opts =
  retries         : 3
  randomize       : true
  factor          : 3
  minInterval     : 3  * 1000
  maxInterval     : 30 * 1000
  operationTimeout: 10 * 1000
  expectedStatus  : /^[2345]\d{2}$/
  expectedKey     : "status"
  url             : "https://api.github.com/events"

Airbud.retrieve opts, (err, events, meta) ->
  if err
    throw err

  console.log events

Example: 3 retries in one minute, retry after 3s timeout for each operation

opts =
  url             : "https://api2.transloadit.com/instances"
  retries         : 2
  factor          : 1.73414
  expectedKey     : "instances"
  operationTimeout: 3000

Some other tricks up Airbud's sleeves are expectedKey and expectedStatus, to make it error out when you get invalid data, without you writing all the extra if and maybes.

Options

Here are all of Airbud's options and their default values.

# Timeout of a single operation
operationTimeout: 30000

# Retry 5 times over 10 minutes
# http://www.wolframalpha.com/input/?i=Sum%5Bx%5Ek+*+5%2C+%7Bk%2C+0%2C+4%7D%5D+%3D+10+*+60+%26%26+x+%3E+0
# The maximum amount of times to retry the operation
retries: 4

# The exponential factor to use
factor: 2.99294

# The number of milliseconds before starting the first retry
minInterval: 5 * 1000

# The maximum number of milliseconds between two retries
maxInterval: Infinity

# Randomizes the intervals by multiplying with a factor between 1 to 2
randomize: true

# Automatically parse json
parseJson: null

# A key to find in the rootlevel of the parsed json.
# If not found, Airbud will error out
expectedKey: null

# An array of allowed HTTP Status codes. If specified,
# Airbud will error out if the actual status doesn't match.
# 30x redirect codes are followed automatically.
expectedStatus: "20x"

# Custom headers to submit in the request
headers: []

Meta

Besides, err, data, Airbud returns a third argument meta. It contains some meta data about the operation(s) for your convenience.

# The HTTP status code returned
statusCode
# An array of all errors that occured
errors
# Number of attempts before Airbud was able to retrieve, or gave up
attempts
# Total duration of all attempts
totalDuration
# Average duration of a single attempt
operationDuration

Contribute

I'd be happy to accept pull requests. If you plan on working on something big, please first give a shout!

Compile

This project is written in CoffeeScript, but the JavaScript it generates is commited back into the repository so people can use this module without a CoffeeScript dependency. If you want to work on the source, please do so in ./src and type: make build or make test (also builds first). Please don't edit generated JavaScript in ./lib!

Test

Run tests via make test.

To single out a test use make test GREP=30x

Release

Releasing a new version to npmjs.org can be done via make release-patch (or minor / major, depending on the semantic versioning impact of your changes). This:

  • updates the package.json
  • saves a release commit with the updated version in Git
  • pushes to GitHub
  • publishes to npmjs.org

Authors

Related

  • got has a similar purpose but a much larger community to maintain it. Consider using it. Airbud however does provide meta information (in addition to err and data which are similar to got), that passes you the number of retries involved as well as the time it took for the first successful operation to complete. Airbud also supports file:// URLs meaning you could substitute URLs with fixtures easily for your project's testing purposes.

License

MIT Licensed.