Security News
pnpm 10.0.0 Blocks Lifecycle Scripts by Default
pnpm 10 blocks lifecycle scripts by default to improve security, addressing supply chain attack risks but sparking debate over compatibility and workflow changes.
chiffchaff-multi
Advanced tools
Multi-task execution for chiffchaff.
Note: Like chiffchaff itself, this example is written in ES2015. Please familiarize yourself with ES2015 before tackling it.
Let's download a couple of files in parallel. Assuming we already have a
DownloadTask
class, let's
create a few of those:
const urls = [
'http://media.w3.org/2010/05/sintel/trailer.mp4',
'http://media.w3.org/2010/05/bunny/trailer.mp4',
'http://media.w3.org/2010/05/bunny/movie.mp4',
'http://media.w3.org/2010/05/video/movie_300.webm'
]
const downloadTasks = urls.map((url) => new DownloadTask(url))
Now, we could start()
those tasks individually, but that's not what we're
after. Using MultiTask
, we can create a task that downloads all four files,
with varying settings.
By default, MultiTask
will execute the provided tasks one by one. Thus, the
following task will download the files sequentially:
const sequentialDownloadTask = new MultiTask(downloadTasks)
The concurrency
option lets us run multiple tasks in parallel. If we pass
Infinity
, all the tasks will run simultaneously. Let's limit the number of
simultaneous downloads to two though:
const twoInParallelDownloadTask = new MultiTask(downloadTasks, {
concurrency: 2
})
One of chiffchaff's core features is cancellation. Because all the tasks we're
providing are of the type DownloadTask
and therefore cancellable, we can also
cancel the MultiTask
, which will automatically cancel all the downloads.
To enable this behavior, we enable the cancel
option:
const cancellableTwoInParallelDownloadTask = new MultiTask(downloadTasks, {
concurrency: 2,
cancel: true
})
There are a few other options, but let's actually download some files first.
Like any other chiffchaff Task
, a MultiTask
is started using the start()
function. The return value is a promise that will be resolved when all the
subtasks have been completed.
cancellableTwoInParallelDownloadTask.start()
.then((result) => console.info('All downloads completed'))
.catch((err) => console.error('Error: %s', err))
Another one of chiffchaff's strengths is its built-in progress reporting.
MultiTask
is no different. Just like with an individual DownloadTask
, we can
subscribe to updates from the compound download task by listening for the
progress
event.
cancellableTwoInParallelDownloadTask.on('progress',
(compl, total) => console.info('Progress: %d/%d', compl, total))
But how is the progress calculated? By default, all tasks will be treated equally. For example, if three out of four files have finished downloading and the fourth one hasn't started, progress will be at 75%. There are two options that affect the calculation.
The first option is weights
. We can optionally supply an array of numbers that
correspond to the weights of the supplied tasks. In our example, if we wanted
to make the first task contribute twice the weight of the other three, we
would pass weights
of [2, 1, 1, 1]
.
For downloads, however, it makes more sense to just add up the byte sizes.
MultiTask
lets us do that as well. If we pass the ignoreWeights
option,
the tasks' weights will be determined by their total sizes as passed when
reporting progress. In the case of DownloadTask
, the total size is the size of
the file that is being downloaded, which is exactly what we need. Hence, we can
get more accurate progress reporting by initializing our MultiTask
as follows
instead:
const accurateDownloadTask = new MultiTask(downloadTasks, {
concurrency: 2,
cancel: true,
ignoreWeights: true
})
This concludes our brief tour of MultiTask
. We've only just scratched the
surface though. Full API documentation is on its way!
MIT
FAQs
Multi-task execution for chiffchaff.
We found that chiffchaff-multi demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 2 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.
Security News
pnpm 10 blocks lifecycle scripts by default to improve security, addressing supply chain attack risks but sparking debate over compatibility and workflow changes.
Product
Socket now supports uv.lock files to ensure consistent, secure dependency resolution for Python projects and enhance supply chain security.
Research
Security News
Socket researchers have discovered multiple malicious npm packages targeting Solana private keys, abusing Gmail to exfiltrate the data and drain Solana wallets.