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

patiently

v2.1.4

Published

API rate limit handler for (Node) JS. Make your API client patient. A legal way to wait for API rate limit resets.

Downloads

55

Readme

patiently

API rate limit handler for (Node) JS. Make your API client patient. A legal way to wait for API rate limit resets.

npm version install size Downloads license

Purpose

Sometimes there is no other way to be patient and to wait for the API rate limit to reset. Just think of a personalized API that allows you to perform actions in a specific user context. You want to do like 10.000 reqs as fast as possible. What if there are rate limits like 50 reqs per minute and 750 reqs per hour? And what if those limits are not bound to an IP or a host but to your user? In this case there is no other way but to wait for a limit reset. Of course, you can also politely ask the API owner to increase the limits. But even then you need patiently. :)

Features

  • patiently will handle API rate limits for you and make your API client wait for a limit reset automatically
  • set minutely or hourly rate limits manually

Planned Features

  • [x] Add secondly limit feature (e.g. 2 reqs per second). --> Done: You can now adjust a millisecondly waiting time between two calls.
  • [ ] If required I can add a "maxConcurrent" feature like in bottleneck
  • [ ] I'm also trying to figure out how to deal with rate limit headers like x-ratelimit-remaining or retry-after. At first sight this is not so easy, because the first headers only arrive after the first API call. And until then, any number of API calls could have been fired.

Installing

Using npm:

$ npm install patiently

How to use

Use by setting limits manually

Perhaps you already know about the limits (maybe from the API docs).

     import patiently from "patiently";
     // let patiently = require("patiently");

     let options = {
       startWaitingCallback: function(info){console.log(info)}, // default is function(){}, calls a function if waiting necessary
       endWaitingCallback: function(info){console.log(info)}, // default is function(){}, calls a function after waiting
       waitingTickCallback: function(info){console.log(info)}, // default is function(){}, calls a function every tick
       msBetweenTwoCalls: 1000, // default is 0 milliseconds (no waiting time between two calls)
       minutelyLimit: 50, // default is Infinity (no minutely limit set)
       hourlyLimit: 750, // default is Infinity (no hourly limit set)
       test: false // default is false (if true, max waiting time is 5 secs)
     }

     var waiter = new patiently.LimitWaiter(options);

     let myApiCallFunction = async (url, callback) => {
         waiter.wait(function(){
           // your api call
           axios.get(url)
             .then(res => {
                callback(res.data);
             })
             .error(err => {
                callback(null, err);
             })
         })
     }

     // you can call myApiCallFunction as often you want
     // patiently can handle asynchronous api calls :)
     let url = "https://www.npmjs.com/package/patiently";
     myApiCallFunction(url);
     myApiCallFunction(url);
     myApiCallFunction(url);
     myApiCallFunction(url);
     // ...

How does it work?

  • Each function you give to the waiter as a parameter is queued in an array

    • F -> E D C B A
  • The longest waiting function is first

    • F E D C B -> A
  • Before the function is called, it is checked whether we have to wait first or not

    • F E D C B -> A (wait?)
  • If yes the queue processing is paused and when the waiting time elapsed, function A will be called

    • F E D C B -> A (call)

License

MIT