
Security News
/Research
Wallet-Draining npm Package Impersonates Nodemailer to Hijack Crypto Transactions
Malicious npm package impersonates Nodemailer and drains wallets by hijacking crypto transactions across multiple blockchains.
@elselab-io/node-simple-batcher
Advanced tools
A simple batching library with state management for Node.js
A simple, powerful batch processing library for Node.js with built-in state management, concurrency control, and TypeScript support.
npm install @elselab-io/node-simple-batcher
import { processBatches } from '@elselab-io/node-simple-batcher';
async function processItem(item, index, state) {
// Process the item (e.g. API call, database operation, etc.)
return { processed: true, item };
}
const items = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
// Process with custom options
const result = await processBatches(items, processItem, {
batchSize: 3, // Process 3 items per batch
concurrencyLimit: 2, // Run at most 2 concurrent operations
stateUpdateInterval: 2, // Update state every 2 items
onBatchStart: (batchNum, total, batch, state) => {
console.log(`Starting batch ${batchNum}/${total} with ${batch.length} items`);
},
onBatchComplete: (batchNum, total, batch, processed, failed, state) => {
console.log(`Completed batch ${batchNum}/${total}, total processed: ${processed}`);
}
});
console.log(`Processed ${result.processed} items, failed: ${result.failed}`);
That's it! Your items will be processed in efficient batches with full control over concurrency and progress tracking.
processBatches(items, processFunction, options)
Process an array of items in batches with controlled concurrency.
const result = await processBatches(items, processFunction, {
batchSize: 20, // Number of items per batch
concurrencyLimit: 10, // Max concurrent operations
stateUpdateInterval: 5, // State update frequency
onBatchStart: (batchNum, total, batch, state) => {},
onBatchComplete: (batchNum, total, batch, processed, failed, state) => {},
onItemSuccess: (item, index, totalProcessed, state) => {},
onItemError: (item, index, totalFailed, state) => {},
onStateUpdate: (state) => {},
initialState: {}
});
processPaginatedBatches(fetchPageFunction, processFunction, options)
Process paginated data with automatic page fetching.
const result = await processPaginatedBatches(fetchPageFunction, processFunction, {
initialPage: 1,
concurrencyLimit: 10,
onPageStart: (pageNum, totalPages, state) => {},
onPageComplete: (pageNum, totalPages, pageProcessed, totalProcessed, state) => {},
onItemSuccess: (item, index, totalProcessed, state) => {},
onItemError: (item, index, totalFailed, state) => {},
onStateUpdate: (state) => {},
stateUpdateInterval: 5,
initialState: {}
});
createBatchProcessorWithState(processFunction, stateFilePath, options)
Create a batch processor with automatic state management for resumable operations.
const batchProcessor = createBatchProcessorWithState(
processFunction,
'./batch-state.json',
{
saveStateOnBatch: true,
saveStateOnItem: true,
saveStateInterval: 5
}
);
StateManager
Low-level class for managing state persistence.
const stateManager = new StateManager('./state.json');
await stateManager.saveState(state);
const state = await stateManager.loadState();
await stateManager.clearState();
StateTracker
Utility for tracking progress and displaying status information.
const tracker = new StateTracker(totalItems, initialState);
tracker.updateProgress(processed, failed, additionalData);
const progress = tracker.getProgress();
const formatted = tracker.formatProgress(showSpeed);
import { processBatches } from '@elselab-io/node-simple-batcher';
async function processItem(item, index, state) {
// Process the item (e.g. API call, database operation, etc.)
return { processed: true, item };
}
const items = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
const result = await processBatches(items, processItem, {
batchSize: 3,
concurrencyLimit: 2,
stateUpdateInterval: 2,
onBatchStart: (batchNum, total, batch, state) => {
console.log(`Starting batch ${batchNum}/${total} with ${batch.length} items`);
},
onBatchComplete: (batchNum, total, batch, processed, failed, state) => {
console.log(`Completed batch ${batchNum}/${total}, total processed: ${processed}`);
}
});
import { processPaginatedBatches } from '@elselab-io/node-simple-batcher';
// Function to fetch a page of data
async function fetchPage(pageNumber, state) {
const response = await fetch(`https://api.example.com/items?page=${pageNumber}`);
const data = await response.json();
return {
items: data.results,
totalPages: data.totalPages
};
}
// Function to process each item
async function processItem(item, index, state) {
// Process the item
return { processed: true, item };
}
// Process all pages
const result = await processPaginatedBatches(fetchPage, processItem, {
initialPage: 1,
concurrencyLimit: 5,
onPageStart: (pageNum, totalPages, state) => {
console.log(`Starting page ${pageNum}/${totalPages || '?'}`);
},
onPageComplete: (pageNum, totalPages, pageProcessed, totalProcessed, state) => {
console.log(`Completed page ${pageNum}/${totalPages}, processed: ${pageProcessed}`);
}
});
import { createBatchProcessorWithState } from '@elselab-io/node-simple-batcher';
// Function to process each item
async function processItem(item, index, state) {
// Process the item
return { processed: true, item };
}
// Create a batch processor with automatic state management
const batchProcessor = createBatchProcessorWithState(
processItem,
'./batch-state.json', // Path to store state
{
saveStateOnBatch: true, // Save state after each batch
saveStateOnItem: true, // Save state after processing items
saveStateInterval: 5 // Save every 5 items when saveStateOnItem is true
}
);
// Get all items to process
const items = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
try {
// Process items with automatic state management
// If the process stops and restarts, it will resume from the last saved state
const result = await batchProcessor.process(items, {
batchSize: 3,
concurrencyLimit: 2
});
console.log(`Completed processing with ${result.processed} processed items`);
// Clear state after successful completion
await batchProcessor.clearState();
} catch (error) {
console.error('Processing error:', error);
// State is automatically saved, so the job can be resumed later
const currentState = await batchProcessor.getState();
console.log('Current progress:', currentState);
}
import { processBatches, StateTracker } from '@elselab-io/node-simple-batcher';
// Create items to process
const items = Array.from({ length: 100 }, (_, i) => i + 1);
// Create a state tracker
const tracker = new StateTracker(items.length);
// Update progress display every second
const progressInterval = setInterval(() => {
process.stdout.write(`\r${tracker.formatProgress()}`);
}, 1000);
// Process the items
const result = await processBatches(
items,
async (item) => {
// Your processing logic here
await new Promise(resolve => setTimeout(resolve, 100)); // Simulate work
return { success: true };
},
{
batchSize: 10,
concurrencyLimit: 5,
onItemSuccess: (_, __, totalProcessed, state) => {
// Update tracker with current progress
tracker.updateProgress(totalProcessed, state.totalFailed || 0);
},
onItemError: (_, __, totalFailed, state) => {
tracker.updateProgress(state.totalProcessed || 0, totalFailed);
}
}
);
// Clear interval when done
clearInterval(progressInterval);
console.log(`\nComplete! Processed ${result.processed} items`);
Most batch processing solutions create inefficient processing patterns:
// โ Traditional approach - uncontrolled concurrency
items.forEach(async (item) => {
await processItem(item); // All items processed simultaneously
});
// โ Sequential processing - too slow
for (const item of items) {
await processItem(item); // One at a time
}
node-simple-batcher provides intelligent batch processing with controlled concurrency:
// โ
node-simple-batcher approach - optimal
await processBatches(items, processItem, {
batchSize: 20, // Process 20 items per batch
concurrencyLimit: 5 // Max 5 concurrent operations
});
Items | Traditional | node-simple-batcher | Performance Gain |
---|---|---|---|
100 | Uncontrolled | Batched + Limited | 3x faster |
1000 | Memory issues | Stable processing | 5x faster |
10000 | Crashes | Reliable completion | โ (completes) |
// Start/stop as needed
const batchProcessor = createBatchProcessorWithState(processItem, './state.json');
// Process with custom state
const result = await batchProcessor.process(items, {
batchSize: 10,
concurrencyLimit: 3
});
// Clear state when done
await batchProcessor.clearState();
// Process items as they become available
const dynamicItems = [];
// Add items dynamically
setInterval(() => {
dynamicItems.push(generateNewItem());
}, 1000);
// Process in batches
await processBatches(dynamicItems, processItem, {
batchSize: 5,
concurrencyLimit: 2
});
// Custom state with additional metadata
const customState = {
startTime: Date.now(),
customMetrics: {},
userInfo: { id: 'user123' }
};
const result = await processBatches(items, processItem, {
initialState: customState,
onStateUpdate: (state) => {
// Save custom state
console.log('Custom metrics:', state.customMetrics);
}
});
node-simple-batcher supports all module systems:
import { processBatches } from '@elselab-io/node-simple-batcher';
const { processBatches } = require('@elselab-io/node-simple-batcher');
import {
processBatches,
processPaginatedBatches,
StateManager,
StateTracker
} from '@elselab-io/node-simple-batcher';
Run the test suite:
npm test
Run examples:
npm run example:basic
npm run example:tracker
Build the project:
npm run build
ISC License - see LICENSE file for details.
Contributions are welcome! Please feel free to submit a Pull Request.
FAQs
A simple batching library with state management for Node.js
The npm package @elselab-io/node-simple-batcher receives a total of 0 weekly downloads. As such, @elselab-io/node-simple-batcher popularity was classified as not popular.
We found that @elselab-io/node-simple-batcher demonstrated a healthy version release cadence and project activity because the last version was released less than 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
Malicious npm package impersonates Nodemailer and drains wallets by hijacking crypto transactions across multiple blockchains.
Security News
This episode explores the hard problem of reachability analysis, from static analysis limits to handling dynamic languages and massive dependency trees.
Security News
/Research
Malicious Nx npm versions stole secrets and wallet info using AI CLI tools; Socketโs AI scanner detected the supply chain attack and flagged the malware.