Offload tasks to a pool of workers on node.js and in the browser
Overall
score
95%
Build a worker service that performs computational image processing operations using a pool of background workers. The service should support multiple image transformation operations and provide real-time progress updates during processing.
Create two files:
app.js): Creates a worker pool and provides an interface for processing imagesimageWorker.js): Implements the image processing functionsThe worker script must register the following functions:
grayscale(imageData) - Convert image data to grayscale
pixels array (RGBA values) and width/height dimensionsadjustBrightness(imageData, factor) - Adjust image brightness
For each operation, emit events with the following structure:
{ type: 'progress', operation: '<operation-name>', percent: <number> }The main application should:
processImage(operation, imageData, ...params) that:
File: app.test.js
Test: Verify grayscale conversion produces correct output
const imageData = {
pixels: [255, 0, 0, 255, 0, 255, 0, 255, 0, 0, 255, 255],
width: 3,
height: 1
};
const result = await processImage('grayscale', imageData);
// Result should have grayscale pixels where RGB values are equal
// First pixel (red): ~76, ~76, ~76, 255
// Second pixel (green): ~150, ~150, ~150, 255
// Third pixel (blue): ~29, ~29, ~29, 255File: app.test.js
Test: Verify brightness adjustment works correctly
const imageData = {
pixels: [100, 100, 100, 255, 50, 50, 50, 255],
width: 2,
height: 1
};
const result = await processImage('adjustBrightness', imageData, 50);
// All RGB values should be increased by 50
// First pixel: 150, 150, 150, 255
// Second pixel: 100, 100, 100, 255File: app.test.js
Test: Verify progress events are emitted during processing
const imageData = {
pixels: new Array(1000 * 4).fill(128), // 1000 pixels
width: 100,
height: 10
};
const events = [];
// Configure processImage to capture events
const result = await processImage('grayscale', imageData, {
onProgress: (event) => events.push(event)
});
// Should have received progress events
// events.length > 0
// events should contain { type: 'progress', operation: 'grayscale', percent: <number> }Provides worker pool functionality for offloading computational tasks to background workers.
Install with Tessl CLI
npx tessl i tessl/npm-workerpoolevals
scenario-1
scenario-2
scenario-3
scenario-4
scenario-5
scenario-6
scenario-7
scenario-8
scenario-9
scenario-10