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.
An async task pooling and throttling utility for javascript.
$ yarn add swimmer
# or
$ npm i swimmer --save
https://unpkg.com/swimmer/umd/swimmer.min.js
Inline Pooling is great for:
import { poolAll } from 'swimmer'
const urls = [...]
const doIntenseTasks = async () => {
try {
const res = await poolAll(
urls.map(task =>
() => fetch(url) // Return an array of functions that return a promise
),
10 // Set the concurrency limit
)
} catch (err, task) {
// If an error is encountered, the entire pool stops and the error is thrown
console.log(`Encountered an error with task: ${task}`)
throw err
}
// If no errors are thrown, you get your results!
console.log(res) // [result, result, result, result]
}
Custom pools are great for:
import { createPool } from 'swimmer'
const urls = [...]
const otherUrls = [...]
// Create a new pool with a throttle speed and some default tasks
const pool = createPool({
concurrency: 5,
tasks: urls.map(url => () => fetch(url))
})
// Subscribe to errors
pool.onError((err, task) => {
console.warn(err)
console.log(`Encountered an error with task ${task}. Resubmitting to pool!`)
pool.add(task)
})
// Subscribe to successes
pool.onSuccess((res, task) => {
console.log(`Task Complete. Result: ${res}`)
})
// Subscribe to settle
pool.onSettled(() => console.log("Pool is empty. All tasks are finished!"))
const doIntenseTasks = async () => {
// Add more tasks to the pool.
tasks.forEach(
url => pool.add(
() => fetch(url)
)
)
// Increase the concurrency to 10! This can also be done while it's running.
pool.throttle(10)
// Pause the pool
pool.stop()
// Start the pool again!
pool.start()
// Add a single task and WAIT for it's completion/failure
try {
const res = await pool.add(() => fetch('http://custom.com/asset.json'))
console.log('A custom asset!', res)
} catch (err) {
console.log('Darn! An error...')
throw err
}
// Then clear the pool. Any running tasks will attempt to finished.
pool.clear()
}
Swimmer exports two functions:
poolAll
- creates an inline async/await/promise compatible pool
Array[Function => Promise]
- An array of functions that return a promise.Int
- The currency limit for this pool.Promise
that resolves when all tasks are complete, or throws an error if one of them fails.createPool
- creates an custom pool
Object{}
- An optional configuration object for this pool
concurrency: Int (default: 5)
- The currency limit for this pool.started: Boolean (default: true)
- Whether the pool should be started by default or not.tasks: Array[Function => Promise]
- An array of functions that return a promise. These tasks will be preloaded into the pool.Object{}
add(() => Promise, config{})
- Adds a task to the pool. Optionally pass a config object
config.priority
- Set this option to true
to queue this task in front of all other pending
tasks.start()
- Starts the pool.stop()
- Stops the pool.throttle(Int)
- Sets a new concurrency rate for the pool.clear()
- Clears all pending tasks from the pool.getActive()
- Returns all active tasks.getPending()
- Returns all pending tasks.getAll()
- Returns all tasks.isRunning()
- Returns true
if the pool is running.isSettled()
- Returns true
if the pool is settled.onSuccess((result, task) => {})
- Registers an onSuccess callback.onError((error, task) => {})
- Registers an onError callback.onSettled(() => {})
- Registers an onSettled callback.Make sure you are passing an array of thunks
. A thunk is a function that returns your task, not your task itself. If you pass an array of tasks that have already been fired off then it's too late for Swimmer to manage them :(
We are always looking for people to help us grow swimmer
's capabilities and examples. If you have an issue, feature request, or pull request, let us know!
Swimmer uses the MIT license. For more information on this license, click here.
FAQs
An async task pooling and throttling utility for javascript.
The npm package swimmer receives a total of 5,952 weekly downloads. As such, swimmer popularity was classified as popular.
We found that swimmer 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.