Research
Security News
Malicious npm Package Targets Solana Developers and Hijacks Funds
A malicious npm package targets Solana developers, rerouting funds in 2% of transactions to a hardcoded address.
anti-rate-limit
Advanced tools
anti-rate-limit is a powerful task queue rate limiter designed for Node.js applications. It allows developers to efficiently manage and control the rate at which tasks are executed by enforcing customizable rate limits, concurrency, and automatic retries
anti-rate-limit
is a flexible and efficient task queue rate-limiter for Node.js, designed to manage and throttle task execution with configurable rate limits. It allows you to process tasks with a maximum number of requests per interval, concurrency control, and automatic retries for failed tasks.
This package is useful in scenarios like API rate limiting, background task processing, and controlling the load on resources.
To install anti-rate-limit
in your Node.js project, use npm or yarn:
npm install anti-rate-limit
Or with yarn:
yarn add anti-rate-limit
Here’s a simple example showing how to use anti-rate-limit
for basic rate-limiting.
app.js
const { AntiRateLimit } = require('anti-rate-limit');
// Initialize the rate limiter
const rateLimiter = new AntiRateLimit({
maxRequests: 5, // Max 5 tasks per interval
interval: 1000, // 1-second interval
concurrency: 2, // 2 tasks can run simultaneously
retryLimit: 3 // Retry failed tasks up to 3 times
});
// Add a simple task
rateLimiter.addTask({
id: 'task1',
execute: async () => {
console.log('Executing task1');
// Simulate async work
await new Promise(resolve => setTimeout(resolve, 300));
}
});
You can add multiple tasks in sequence, and they will be executed within the constraints of the rate limiter.
// Add multiple tasks
for (let i = 2; i <= 6; i++) {
rateLimiter.addTask({
id: `task${i}`,
execute: async () => {
console.log(`Executing task${i}`);
await new Promise(resolve => setTimeout(resolve, 300));
}
});
}
If you want to execute simple tasks like logging, you can define them inline:
rateLimiter.addTask({
id: 'task2',
execute: async () => {
console.log('Executing task2');
await new Promise(resolve => setTimeout(resolve, 300));
}
});
Tasks with higher priority are processed first. If you want to control the order of execution, you can specify a priority
value.
rateLimiter.addTask({
id: 'highPriorityTask',
priority: 10, // Higher priority
execute: async () => {
console.log('Executing high priority task');
await new Promise(resolve => setTimeout(resolve, 300));
}
});
rateLimiter.addTask({
id: 'lowPriorityTask',
priority: 1, // Lower priority
execute: async () => {
console.log('Executing low priority task');
await new Promise(resolve => setTimeout(resolve, 300));
}
});
In this case, highPriorityTask
will execute before lowPriorityTask
, regardless of when they were added to the queue.
Tasks that fail can be retried automatically up to a specified limit. If a task exceeds the retry limit, an error is thrown.
rateLimiter.addTask({
id: 'taskWithError',
execute: async () => {
console.log('Task with error is executing');
throw new Error('Task failed');
}
}).catch((err) => {
console.error('Task failed after retries:', err);
});
The task will be retried up to 3 times (as specified by retryLimit
), and if it still fails, it will be rejected with an error message.
AntiRateLimit
The main class used to create and manage the rate-limiting queue.
new AntiRateLimit(options)
maxRequests
: The maximum number of tasks that can be executed within the interval
(default: 10).interval
: The time window for rate-limiting in milliseconds (default: 1000).concurrency
: The number of tasks that can run concurrently (default: 1).retryLimit
: The number of retries allowed for a failed task (default: 3).addTask(task)
: Adds a new task to the queue.
id
: A unique identifier for the task.priority
(optional): A number to define the task's priority (higher numbers execute first).execute
: An async function that will be executed as the task's logic.task.execute()
: The execute
function must return a Promise
. This function contains the logic that the rate limiter will manage.
If you want to add a group of tasks and run them simultaneously (up to the maximum allowed by concurrency
), you can:
const tasks = [1, 2, 3, 4, 5].map((i) => ({
id: `simultaneousTask${i}`,
execute: async () => {
console.log(`Executing task${i}`);
await new Promise(resolve => setTimeout(resolve, 300));
}
}));
tasks.forEach(task => rateLimiter.addTask(task));
In this example, tasks task1
to task5
will execute simultaneously, but only two at a time, as specified by the concurrency
option.
anti-rate-limit
can also be used for more complex tasks like API calls or database queries.
rateLimiter.addTask({
id: 'apiRequestTask',
execute: async () => {
console.log('Making API request...');
const response = await fetch('https://api.example.com/data');
const data = await response.json();
console.log('Received API data:', data);
}
});
In this example, the execute
function performs an async API request and logs the response.
If a task fails, it will be retried up to the number of times specified by the retryLimit
. If the task fails after reaching the retry limit, an error is thrown.
rateLimiter.addTask({
id: 'taskWithError',
execute: async () => {
throw new Error('Task failed');
}
}).catch((err) => {
console.error('Task failed after retries:', err);
});
We welcome contributions! If you'd like to contribute to this project, please fork the repository and submit a pull request.
git clone https://github.com/your-username/anti-rate-limit.git
git checkout -b feature-branch
git commit -m 'Add feature'
git push origin feature-branch
This project is licensed under the MIT License - see the LICENSE file for details.
For any issues or feature requests, please open an issue on GitHub or contact us directly.
FAQs
anti-rate-limit is a powerful task queue rate limiter designed for Node.js applications. It allows developers to efficiently manage and control the rate at which tasks are executed by enforcing customizable rate limits, concurrency, and automatic retries
The npm package anti-rate-limit receives a total of 9 weekly downloads. As such, anti-rate-limit popularity was classified as not popular.
We found that anti-rate-limit demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 0 open source maintainers 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.
Research
Security News
A malicious npm package targets Solana developers, rerouting funds in 2% of transactions to a hardcoded address.
Security News
Research
Socket researchers have discovered malicious npm packages targeting crypto developers, stealing credentials and wallet data using spyware delivered through typosquats of popular cryptographic libraries.
Security News
Socket's package search now displays weekly downloads for npm packages, helping developers quickly assess popularity and make more informed decisions.