CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/npm-workerpool

Offload tasks to a pool of workers on node.js and in the browser

Overall
score

95%

Overview
Eval results
Files

task.mdevals/scenario-1/

Image Processing Worker Service

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.

Requirements

Create two files:

  1. Main application file (app.js): Creates a worker pool and provides an interface for processing images
  2. Worker script file (imageWorker.js): Implements the image processing functions

Worker Implementation

The worker script must register the following functions:

  1. grayscale(imageData) - Convert image data to grayscale

    • Takes an object with pixels array (RGBA values) and width/height dimensions
    • Returns the transformed pixel data in the same format
    • Should emit progress events every 25% of completion
  2. adjustBrightness(imageData, factor) - Adjust image brightness

    • Takes image data object and brightness factor (-100 to 100)
    • Returns the adjusted pixel data
    • Should emit progress events every 20% of completion

For each operation, emit events with the following structure:

{ type: 'progress', operation: '<operation-name>', percent: <number> }

Main Application Requirements

The main application should:

  1. Create a worker pool pointing to the worker script file
  2. Provide a function processImage(operation, imageData, ...params) that:
    • Executes the specified operation on the worker
    • Listens for progress events and logs them to console
    • Returns the processed image data
  3. Properly terminate the pool when processing is complete

Test Cases

Test 1: Grayscale Conversion @test

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, 255

Test 2: Brightness Adjustment @test

File: 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, 255

Test 3: Progress Events @test

File: 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> }

Dependencies { .dependencies }

workerpool { .dependency }

Provides worker pool functionality for offloading computational tasks to background workers.

Implementation Notes

  • Use appropriate image processing algorithms for each operation (simple averaging for grayscale, brightness clamping between 0-255, etc.)
  • Ensure all operations maintain the alpha channel unchanged
  • Progress events should be emitted at the specified intervals during processing
  • Handle worker cleanup properly after task completion

Install with Tessl CLI

npx tessl i tessl/npm-workerpool

tile.json