Research
Security News
Malicious npm Packages Inject SSH Backdoors via Typosquatted Libraries
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.
node-parallelizer
Advanced tools
A NodeJS package for running code in parallel. Initially created to provide multiprocessing in an AWS Lambda function, but it can be used in any NodeJS environment.
A NodeJS package for running code in parallel. Initially created to provide multiprocessing in an AWS Lambda function, but it can be used in any NodeJS environment.
This parallelizer is specifically designed for processing hundreds or thousands of records in a single invocation when your code performs both CPU-intensive and I/O-intensive operations.
When you call the runBatch(records)
method in this parallelizer, the package will split the list of records you provide into smaller subsets, and your code will be used to execute each subset in parallel.
It uses the NodeJS child process module behind the scenes.
This parallelizer is specifically designed for processing hundreds or thousands of records in a single invocation when your code performs CPU-intensive operations.
When you call the runBatch(records)
method in this parallelizer, the package will split the list of records you provide into smaller subsets, and your code will be used to execute each subset in parallel.
It uses the NodeJS worker threads module behind the scenes.
This package can detect the number of vCPU cores allocated to your Lambda function and maximize their utilization. By default, it generates one child process/thread per vCPU core, but this setting can be customized to meet your specific requirements. Alternatively, you can manually specify the number of child processes/threads the library creates, regardless of the number of vCPU cores available.
It uses the Lambda function environment /tmp
folder to create the required module that runs in the child.
On the Child Process Parallelizer, when you call the parallelizerFunction
method outside of the Lambda handler function, it will reuse the child processes across the different invocations within a Lambda instance, minimazing the impact of creating child process on every invocation. Furthermore, if the package detects a disconnection of any of the child processes, it will recreate it automatically without affecting the execution.
To add this package to your dependency list, run:
npm i node-parallelizer --save
Parallelizer({ type = 'child-process', tmpPath = '/tmp', maxParallelization = false, parallelizationPerCPU = 1, debug = false })
Parameters
type
(String) (Default value: 'child-process') (Options: 'child-process' | 'worker-threads'): The parallelizer type to be used.tmpPath
(String) (Default value: '/tmp'): The path where the module that runs in the thread will be created.maxParallelization
(Number|false) (Default value: false): The maximum number of processes/threads that will be created. If false, it is based on the CPU cores available.parallelizationPerCPU
(Number) (Default value: 1): If the maxParallelization
is set to false
, this parameter defines the amount of processes/threads per CPU.debug
(Boolean) (Default value: false): Enables the internal logs for debuggin purposes.parallelizerFunction({ filePath, processBatchFunctionName })
Parameters
filePath
(String): The absolute path to the file that contains the function that will be executed in parallel.processBatchFunctionName
(String): The name of the function that will be executed in parallel.runBatch(batch)
Parameters
batch
(Array): The records you want to process in parallel.Returns (Array): The thread's responses.
In this example, the repository structure looks like this
src/
handler.js
parallel.js
serverless.yml
package.json
The below snippet represents your Lambda handler
// handler.js
const { Parallelizer, PARALLELIZER_CHILD, PARALLELIZER_THREADS } = require("node-parallelizer");
// Creates a new parallelizer instance.
const parallelizer = new Parallelizer();
// Creates child processes based on your code.
parallelizer.parallelizerFunction({ type: PARALLELIZER_CHILD, filePath: "/var/task/src/parallel.js", processBatchFunctionName: 'batchProcessor' });
module.exports.handler = async(event) => {
// Run batch in parallel
const responses = await parallelizer.runBatch(event.Records);
console.log(responses);
};
Make sure to provide the filePath parameter as an absolute path. In this example, we've included '/var/task/' prefix in the path because Lambda deploys your code within that folder.
The below snippet represents the code you want to run in parallel
// parallel.js
const batchProcessor = ({ batch }) => {
//
// HERE YOUR CODE
//
return { success: true, count: batch.length }
}
module.exports = { batchProcessor }
Verify that the input signature of your function (in this case, batchProcessor) includes batch as a parameter, as it contains the subset of records that a child process will handle.
ChildProcess({ tmpPath = '/tmp', maxParallelization = false, parallelizationPerCPU = 1, debug = false })
Parameters
tmpPath
(String) (Default value: '/tmp'): The path where the module that runs in the child will be created.maxParallelization
(Number|false) (Default value: false): The maximum number of child processes that will be created. If false, it is based on the CPU cores available.parallelizationPerCPU
(Number) (Default value: 1): If the maxParallelization
is set to false
, this parameter defines the amount of processes per CPU.debug
(Boolean) (Default value: false): Enables the internal logs for debuggin purposes.parallelizerFunction({ filePath, processBatchFunctionName })
Parameters
filePath
(String): The absolute path to the file that contains the function that will be executed in parallel.processBatchFunctionName
(String): The name of the function that will be executed in parallel.runBatch(batch)
Parameters
batch
(Array): The records you want to process in parallel.Returns (Array): The child processes' responses.
In this example, the repository structure looks like this
src/
handler.js
parallel.js
serverless.yml
package.json
The below snippet represents your Lambda handler
// handler.js
const { ChildProcess } = require("node-parallelizer");
// Creates a new child process instance.
const childProcess = new ChildProcess();
// Creates child processes based on your code.
childProcess.parallelizerFunction({ filePath: "/var/task/src/parallel.js", processBatchFunctionName: 'batchProcessor' });
module.exports.handler = async(event) => {
// Run batch in parallel
const responses = await childProcess.runBatch(event.Records);
console.log(responses);
};
Make sure to provide the filePath parameter as an absolute path. In this example, we've included '/var/task/' prefix in the path because Lambda deploys your code within that folder.
The below snippet represents the code you want to run in parallel
// parallel.js
const batchProcessor = ({ batch }) => {
//
// HERE YOUR CODE
//
return { success: true, count: batch.length }
}
module.exports = { batchProcessor }
Verify that the input signature of your function (in this case, batchProcessor) includes batch as a parameter, as it contains the subset of records that a child process will handle.
WorkerThreads({ tmpPath = '/tmp', maxParallelization = false, parallelizationPerCPU = 1, debug = false })
Parameters
tmpPath
(String) (Default value: '/tmp'): The path where the module that runs in the thread will be created.maxParallelization
(Number|false) (Default value: false): The maximum number of threads that will be created. If false, it is based on the CPU cores available.parallelizationPerCPU
(Number) (Default value: 1): If the maxParallelization
is set to false
, this parameter defines the amount of threads per CPU.debug
(Boolean) (Default value: false): Enables the internal logs for debuggin purposes.parallelizerFunction({ filePath, processBatchFunctionName })
Parameters
filePath
(String): The absolute path to the file that contains the function that will be executed in parallel.processBatchFunctionName
(String): The name of the function that will be executed in parallel.runBatch(batch)
Parameters
batch
(Array): The records you want to process in parallel.Returns (Array): The thread's responses.
In this example, the repository structure looks like this
src/
handler.js
parallel.js
serverless.yml
package.json
The below snippet represents your Lambda handler
// handler.js
const { WorkerThreads } = require("node-parallelizer");
// Creates a new child process instance.
const threads = new WorkerThreads();
// Creates child processes based on your code.
threads.parallelizerFunction({ filePath: "/var/task/src/parallel.js", processBatchFunctionName: 'batchProcessor' });
module.exports.handler = async(event) => {
// Run batch in parallel
const responses = await threads.runBatch(event.Records);
console.log(responses);
};
Make sure to provide the filePath parameter as an absolute path. In this example, we've included '/var/task/' prefix in the path because Lambda deploys your code within that folder.
The below snippet represents the code you want to run in parallel
// parallel.js
const batchProcessor = ({ batch }) => {
//
// HERE YOUR CODE
//
return { success: true, count: batch.length }
}
module.exports = { batchProcessor }
Verify that the input signature of your function (in this case, batchProcessor) includes batch as a parameter, as it contains the subset of records that a child process will handle.
We welcome contributions to this project. If you are interested in contributing, please feel free to submit a pull request.
FAQs
A NodeJS package for running code in parallel. Initially created to provide multiprocessing in an AWS Lambda function, but it can be used in any NodeJS environment.
The npm package node-parallelizer receives a total of 53 weekly downloads. As such, node-parallelizer popularity was classified as not popular.
We found that node-parallelizer 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.
Research
Security News
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.
Security News
MITRE's 2024 CWE Top 25 highlights critical software vulnerabilities like XSS, SQL Injection, and CSRF, reflecting shifts due to a refined ranking methodology.
Security News
In this segment of the Risky Business podcast, Feross Aboukhadijeh and Patrick Gray discuss the challenges of tracking malware discovered in open source softare.