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.
The fastq npm package is a fast, in-memory queue with worker functions. It is useful for managing tasks and processing them concurrently with a controlled level of parallelism. It allows you to define tasks, add them to the queue, and process them with a defined concurrency level.
Task Queueing
This feature allows you to create a queue with a worker function that processes tasks. The second argument to `fastq` is the concurrency level, which in this case is set to 2, meaning that up to two tasks can be processed in parallel.
const fastq = require('fastq');
const worker = (task, cb) => {
console.log(`Processing task: ${task}`);
cb();
};
const queue = fastq(worker, 2);
queue.push('task1', (err) => console.log('Finished processing task1'));
queue.push('task2', (err) => console.log('Finished processing task2'));
Pause and Resume
This feature allows you to pause the processing of tasks in the queue and resume it later. This can be useful for throttling or when the system needs to perform other high-priority operations.
const fastq = require('fastq');
const worker = (task, cb) => {
setTimeout(() => {
console.log(`Processed: ${task}`);
cb();
}, 1000);
};
const queue = fastq(worker, 1);
queue.push('task1');
queue.push('task2');
queue.pause();
// ... some time later
queue.resume();
Drain Event
The 'drain' event is emitted when the last item from the queue has been assigned to a worker. It can be used to know when all tasks have been processed and the queue is empty.
const fastq = require('fastq');
const worker = (task, cb) => {
console.log(`Processing task: ${task}`);
cb();
};
const queue = fastq(worker, 2);
queue.drain = () => {
console.log('All tasks have been processed');
};
queue.push('task1');
queue.push('task2');
The 'async' package provides a collection of utilities for working with asynchronous JavaScript. It includes a queue function that can be used to control concurrency similar to fastq. However, async offers a broader set of features for handling asynchronous operations beyond just queueing.
Bee-queue is a simple, fast, robust job/task queue for Node.js, backed by Redis. It is designed to be robust by handling failures and retries. It is more feature-rich for distributed systems compared to fastq, which is more focused on in-memory queueing.
Bull is a Redis-based queue package for handling distributed jobs and messages in Node.js. It offers a rich set of features for job processing such as prioritization, scheduling, and repeatable jobs. Bull is more suitable for complex job processing scenarios compared to fastq's simpler in-memory queueing.
Fast, in memory work queue.
Benchmarks (1 million tasks):
Obtained on node 12.16.1, on a dedicated server.
If you need zero-overhead series function call, check out fastseries. For zero-overhead parallel function call, check out fastparallel.
npm i fastq --save
'use strict'
const queue = require('fastq')(worker, 1)
queue.push(42, function (err, result) {
if (err) { throw err }
console.log('the result is', result)
})
function worker (arg, cb) {
cb(null, arg * 2)
}
const queue = require('fastq').promise(worker, 1)
async function worker (arg) {
return arg * 2
}
async function run () {
const result = await queue.push(42)
console.log('the result is', result)
}
run()
'use strict'
const that = { hello: 'world' }
const queue = require('fastq')(that, worker, 1)
queue.push(42, function (err, result) {
if (err) { throw err }
console.log(this)
console.log('the result is', result)
})
function worker (arg, cb) {
console.log(this)
cb(null, arg * 2)
}
'use strict'
import * as fastq from "fastq";
import type { queue, done } from "fastq";
type Task = {
id: number
}
const q: queue<Task> = fastq(worker, 1)
q.push({ id: 42})
function worker (arg: Task, cb: done) {
console.log(arg.id)
cb(null)
}
'use strict'
import * as fastq from "fastq";
import type { queueAsPromised } from "fastq";
type Task = {
id: number
}
const q: queueAsPromised<Task> = fastq.promise(asyncWorker, 1)
q.push({ id: 42}).catch((err) => console.error(err))
async function asyncWorker (arg: Task): Promise<void> {
// No need for a try-catch block, fastq handles errors automatically
console.log(arg.id)
}
fastqueue()
queue#push()
queue#unshift()
queue#pause()
queue#resume()
queue#idle()
queue#length()
queue#getQueue()
queue#kill()
queue#killAndDrain()
queue#error()
queue#concurrency
queue#drain
queue#empty
queue#saturated
fastqueue.promise()
Creates a new queue.
Arguments:
that
, optional context of the worker
function.worker
, worker function, it would be called with that
as this
,
if that is specified.concurrency
, number of concurrent tasks that could be executed in
parallel.Add a task at the end of the queue. done(err, result)
will be called
when the task was processed.
Add a task at the beginning of the queue. done(err, result)
will be called
when the task was processed.
Pause the processing of tasks. Currently worked tasks are not stopped.
Resume the processing of tasks.
Returns false
if there are tasks being processed or waiting to be processed.
true
otherwise.
Returns the number of tasks waiting to be processed (in the queue).
Returns all the tasks be processed (in the queue). Returns empty array when there are no tasks
Removes all tasks waiting to be processed, and reset drain
to an empty
function.
Same than kill
but the drain
function will be called before reset to empty.
Set a global error handler. handler(err, task)
will be called
each time a task is completed, err
will be not null if the task has thrown an error.
Property that returns the number of concurrent tasks that could be executed in parallel. It can be altered at runtime.
Function that will be called when the last item from the queue has been processed by a worker. It can be altered at runtime.
Function that will be called when the last item from the queue has been assigned to a worker. It can be altered at runtime.
Function that will be called when the queue hits the concurrency limit. It can be altered at runtime.
Creates a new queue with Promise
apis. It also offers all the methods
and properties of the object returned by fastqueue
with the modified
push
and unshift
methods.
Node v10+ is required to use the promisified version.
Arguments:
that
, optional context of the worker
function.worker
, worker function, it would be called with that
as this
,
if that is specified. It MUST return a Promise
.concurrency
, number of concurrent tasks that could be executed in
parallel.Add a task at the end of the queue. The returned Promise
will be fulfilled (rejected)
when the task is completed successfully (unsuccessfully).
This promise could be ignored as it will not lead to a 'unhandledRejection'
.
Add a task at the beginning of the queue. The returned Promise
will be fulfilled (rejected)
when the task is completed successfully (unsuccessfully).
This promise could be ignored as it will not lead to a 'unhandledRejection'
.
Wait for the queue to be drained. The returned Promise
will be resolved when all tasks in the queue have been processed by a worker.
This promise could be ignored as it will not lead to a 'unhandledRejection'
.
ISC
FAQs
Fast, in memory work queue
The npm package fastq receives a total of 20,644,173 weekly downloads. As such, fastq popularity was classified as popular.
We found that fastq 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
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.