
Security News
Node.js TSC Votes to Stop Distributing Corepack
Corepack will be phased out from future Node.js releases following a TSC vote.
request-rate-limiter
Advanced tools
Call HTTP APIs that have rate limits and allow request bursts
A simple leaky-bucket based request rate limiter. Mostly used for clients that work against an API that makes use of a leaky bucket for rate limiting incoming requests. Backs off when an API returns the HTTP 429 HTTP «Too Many Requests» status is encountered.
Provides a request module based on the request module by mikael. You may also implement your own request handler module in a very simple way.
ATTENTION: The API of Version 2+ is not backwards compatible with the API of version 1!
ATTENTION: This module makes use of es modules and thus needs to be started using the --experimental-modules flag in node 10 and 12.
The constructor accepts 4 additional options, which can be used to configure the behaviour of the limiter:
import RequestRateLimiter from 'request-rate-limiter';
const limiter = new RequestRateLimiter({
backoffTime: 10,
requestRate: 60,
interval: 60,
timeout: 600,
});
Used to pass a request handler to the limiter
Make use of the request handler provided by this module
import RequestRateLimiter, { RequestsRequestHandler } from 'request-rate-limiter';
const limiter = new RequestRateLimiter();
limiter.setRequestHandler(new RequestsRequestHandler({
backoffHTTPCode: 429,
}));
Implement your own request implementation
import RequestRateLimiter, { BackoffError } from 'request-rate-limiter';
const limiter = new RequestRateLimiter();
class MyRequestHandler {
// this method is th eonly required interface to implement
// it gets passed the request onfig that is passed by the
// user to the request method of the limiter. The mehtod msut
// return an instance of the BackoffError when the limiter
// needs to back off
async request(requestConfig) {
const response = sendRequestUsingSomeLibrary(requestConfig);
if (response.statusCode === 429) throw new BackoffError(`Need to nack off guys!`);
else return response;
}
}
limiter.setRequestHandler(new MyRequestHandler());
The request method is used to send rate limited requests. You need to pass the configuration of the request to it, which later will be passed to the request handler (see above).
import RequestRateLimiter, { RequestsRequestHandler } from 'request-rate-limiter';
const limiter = new RequestRateLimiter();
limiter.setRequestHandler(new RequestsRequestHandler());
// just send one request
const response = await limiter.request('https://joinbox.com/');
// send requests one after another, waiting for each one to finish
// before the next one is sent
for (const requestConfig of requests) {
const response = await limiter.request(requestConfig);
}
// send a buch of requests in parallel
await Promise.all(requests.map(async(requestConfig) => {
const response = await limiter.request(requestConfig);
}));
The idle method returns a promise which is called when the limiter becomes idle. It's like a once event listener which means that once the promise is resolved one must call the idle method again to wait on the next bunch of requests to complete and the limiter to become idle
import RequestRateLimiter, { RequestsRequestHandler } from 'request-rate-limiter';
const limiter = new RequestRateLimiter();
limiter.setRequestHandler(new RequestsRequestHandler());
// send a buch of requests in parallel
Promise.all(requests.map(async(requestConfig) => {
const response = await limiter.request(requestConfig);
})).catch((err) => {
// this happens if the bucket is overflowing
});
// wait until th elimiter becomes idle
await limiter.idle();
FAQs
Call HTTP APIs that have rate limits and allow request bursts
The npm package request-rate-limiter receives a total of 798 weekly downloads. As such, request-rate-limiter popularity was classified as not popular.
We found that request-rate-limiter demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Corepack will be phased out from future Node.js releases following a TSC vote.
Research
Security News
Research uncovers Black Basta's plans to exploit package registries for ransomware delivery alongside evidence of similar attacks already targeting open source ecosystems.
Security News
Oxlint's beta release introduces 500+ built-in linting rules while delivering twice the speed of previous versions, with future support planned for custom plugins and improved IDE integration.