Security News
Research
Data Theft Repackaged: A Case Study in Malicious Wrapper Packages on npm
The Socket Research Team breaks down a malicious wrapper package that uses obfuscation to harvest credentials and exfiltrate sensitive data.
p-ratelimit
Advanced tools
Promise-based utility to make sure you don’t call rate-limited APIs too quickly.
Makes sure you don’t call rate-limited APIs too quickly.
This is an easy-to-use utility for calling rate-limited APIs. It will prevent you from exceeding rate limits by queueing requests that would go over your rate limit quota.
Rate-limits can be applied across multiple servers if you use Redis.
It works with any API function that returns a Promise.
$ npm i p-ratelimit
const { pRateLimit } = require('p-ratelimit');
// import { pRateLimit } from 'p-ratelimit'; // TypeScript
// create a rate limiter that allows up to 30 API calls per second,
// with max concurrency of 10
const limit = pRateLimit({
interval: 1000, // 1000 ms == 1 second
rate: 30, // 30 API calls per interval
concurrency: 10, // no more than 10 running at once
maxDelay: 2000 // an API call delayed > 2 sec is rejected
});
async function main() {
// original WITHOUT rate limiter:
result = await someFunction(42);
// with rate limiter:
result = await limit(() => someFunction(42));
}
main();
The Quota
configuration object passed to pRateLimit
offers the following configuration settings:
Set both of these:
interval
: the interval over which to apply the rate limit, in millisecondsrate
: how many API calls to allow over the interval periodconcurrency
: how many concurrent API calls to allowIf you want both rate limiting and concurrency, use all three of the above settings (interval
, rate
, concurrency
).
maxDelay
: the maximum amount of time to wait (in milliseconds) before rejecting an API request with RateLimitTimeoutError
(default: 0
, no timeout)fastStart
(Redis only): if true, immediately begin processing requests using the full quota, instead of waiting several seconds to discover other servers (default: false
)If you make an API request that would exceed rate limits, it’s queued and delayed until it can run within the rate limits. Setting maxDelay
will cause the API request to fail if it’s delayed too long.
See the Using Redis section for a discussion of the fastStart
option.
See Using Redis for a detailed discussion.
You can use Redis to coordinate a rate limit among a pool of servers.
const { pRateLimit, RedisQuotaManager } = require('p-ratelimit');
// These must be the same across all servers that share this quota:
const channelName = 'my-api-family';
const quota = { rate: 100, interval: 1000, concurrency: 50 };
// Create a RedisQuotaManager
const qm = new RedisQuotaManager(
quota,
channelName,
redisClient
);
// Create a rate limiter that uses the RedisQuotaManager
const limit = pRateLimit(qm);
// now use limit(…) as usual
Each server that registers with a given channelName
will be allotted 1/(number of servers)
of the available quota. For example, if the pool consists of four servers, each will receive 1/4 the available quota.
When a new server joins the pool, the quota is dynamically adjusted. If a server goes away, its quota is reallocated among the remaining servers within a few minutes.
MIT © Nate Silva
FAQs
Promise-based utility to make sure you don’t call rate-limited APIs too quickly.
The npm package p-ratelimit receives a total of 16,155 weekly downloads. As such, p-ratelimit popularity was classified as popular.
We found that p-ratelimit 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
Research
The Socket Research Team breaks down a malicious wrapper package that uses obfuscation to harvest credentials and exfiltrate sensitive data.
Research
Security News
Attackers used a malicious npm package typosquatting a popular ESLint plugin to steal sensitive data, execute commands, and exploit developer systems.
Security News
The Ultralytics' PyPI Package was compromised four times in one weekend through GitHub Actions cache poisoning and failure to rotate previously compromised API tokens.