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.
@elite-libs/promise-pool
Advanced tools
Configurable async task queue, w/ throttling, retries, progress, error handling.
A background task processor focused on performance, reliability, and durability.
TLDR; An upgraded Promise queue that's essentially a stateful Promise.all()
wrapper.
| Table of Contents
Diagram of Promise Pool's 'Minimal Blocking' design
Promise Pool
FeaturesPromise Pool strives to excel at 4 key goals:
* Since this is JavaScript, our Ring Buffer is more like three JS Arrays in a trenchcoat.
Promise Pool
?return
value isn't used (in the current request.)p-retry
for this.).done()
. (Added .drain()
method.)return
hints/stats? (Time in Event Loop? Event Loop wait time? Pending/Complete task counts?)await delay(requestedWaitTime)
) before (or after) each HTTP call.X-RateLimit-WaitTimeMS
.)PromisePool
exposes 3 methods:
.add(...tasks)
- add one (or more) tasks for background processing. (A task is a function that wraps a Promise
value. e.g. () => Promise.resolve(1)
)..drain()
- Returns a promise that resolves when all tasks have been processed, or another thread takes over waiting by calling .drain()
again..done()
- Drains AND 'finalizes' the pool. No more tasks can be added after this. Can be called from multiple threads, only runs once.See either the Usage Section below, or checkout the
/examples
folder for more complete examples.
# with npm
npm install @elite-libs/promise-pool
# or using yarn
yarn add @elite-libs/promise-pool
import PromisePool from '@elite-libs/promise-pool';
// 1/3: Either use the default instance or create a new one.
const pool = new PromisePool();
(async () => {
// 2/3: Add task(s) to the pool as needed.
// PromisePool will execute them in parallel as soon as possible.
pool.add(() => saveToS3(data));
pool.add(() => expensiveBackgroundWork(data));
// 3/3: REQUIRED: in order to ensure your tasks are executed,
// you must await either `pool.drain()` or `pool.done()` at some point in your code (`done` prevents additional tasks from being added).
await pool.drain();
})();
Recommended for virtually all projects. (API, CLI, Lambda, Frontend, etc.)
The singleton pattern creates exactly 1 Promise Pool
- as soon as the script is imported (typically on the first run).
This ensures the maxWorkers
value will act as a global limit on the number of tasks that can run at the same time.
/services/taskPoolSingleton.ts
import PromisePool from '@elite-libs/promise-pool';
export const taskPool = new PromisePool({
maxWorkers: 6, // Optional. Default is `4`.
});
See examples below, or check out the /examples
folder for more complete examples.
Promise Pool
in some middy
middleware:
./services/taskPoolMiddleware.ts
Note: The imported taskPool
is a singleton instance defined in the taskPoolSingleton
file.
import { taskPool } from './services/taskPoolSingleton';
export const taskPoolMiddleware = () => ({
before: (request) => {
Object.assign(request.context, { taskPool });
},
after: async (request) => {
await request.context.taskPool.drain();
}
});
Now you can use taskPool
in your Lambda function via event.context.taskPool
:
./handlers/example.handler.ts
import middy from '@middy/core';
import { taskPoolMiddleware } from './services/taskPoolMiddleware';
const handleEvent = (event) => {
const { taskPool } = event.context;
const data = getDataFromEvent(event);
taskPool.add(() => saveToS3(data));
taskPool.add(() => expensiveBackgroundWork(data));
return {
statusCode: 200,
body: JSON.stringify({
message: 'Success',
}),
}
}
export const handler = middy(handleEvent)
.use(taskPoolMiddleware());
/services/taskPoolSingleton.mjs
import PromisePool from '@elite-libs/promise-pool'
export const taskPool = new PromisePool({
maxWorkers: 6 // Optional. Default is `4`.
})
/middleware/taskPool.middleware.mjs
import { taskPool } from "../services/taskPoolSingleton.mjs";
const taskPoolMiddleware = {
setup: (request, response, next) => {
request.taskPool = taskPool
next()
},
cleanup: (request, response, next) => {
if (request.taskPool && 'drain' in request.taskPool) {
taskPool.drain()
}
next()
}
}
export default taskPoolMiddleware
To use the taskPoolMiddleware
in your Express app, you'd include taskPoolMiddleware.setup()
and taskPoolMiddleware.cleanup()
.
/app.mjs
import taskPoolMiddleware from "../middleware/taskPool.middleware.mjs"
export const app = express()
// Step 1/2: Setup the taskPool
app.use(taskPoolMiddleware.setup)
app.use(express.bodyParser())
app.post('/users/', function post(request, response, next) {
const { taskPool } = request
const data = getDataFromBody(request.body)
// You can .add() tasks wherever needed,
// - they'll run in the background.
taskPool.add(() => logMetrics(data))
taskPool.add(() => saveToS3(request))
taskPool.add(() => expensiveBackgroundWork(data))
// Or, 'schedule' multiple tasks at once.
taskPool.add(
() => logMetrics(data),
() => saveToS3(request),
() => expensiveBackgroundWork(data)
)
next()
})
// Step 2/2: Drain the taskPool
app.use(taskPoolMiddleware.cleanup)
.drain()
behavior..drain()
caller.Server
+ Singleton
use cases will see a major benefit to this design.FAQs
Configurable async task queue, w/ throttling, retries, progress, error handling.
The npm package @elite-libs/promise-pool receives a total of 13 weekly downloads. As such, @elite-libs/promise-pool popularity was classified as not popular.
We found that @elite-libs/promise-pool 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.