Inline Web Worker Utility

1.1.0 · abandoned · verified Sun Apr 19

inline-worker is a JavaScript utility designed to create a universal Web Worker instance directly from a function. It abstracts away the boilerplate of setting up separate worker files, allowing developers to define worker logic inline within their main script. The package intelligently determines whether to instantiate a native `Worker` in browser environments or a custom `InlineWorker` implementation for Node.js, providing a consistent API across different JavaScript runtimes. The current stable version is 1.1.0, and the package has not seen updates in over eight years (as of April 2026), indicating a low release cadence. Its key differentiator is the inline definition and universal runtime support, making it convenient for small, self-contained worker tasks without the need for separate files or complex build steps. However, its age suggests potential compatibility issues with modern JavaScript features, module systems, and a lack of ongoing maintenance.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to create an `InlineWorker` from a function, pass a shared `self` object for internal worker function testing, send messages to the worker, and receive responses.

const InlineWorker = require('inline-worker');

let self = {};
let worker = new InlineWorker(function(self) {
  // Define worker's message handler
  self.onmessage = function(e) {
    postMessage(self.bark(e.data)); // Calls a worker internal function and posts result back
  };

  // Define a worker internal function (accessible via the shared 'self' object)
  self.bark = function(msg) {
    return msg + '!!';
  };
}, self); // Pass 'self' object for shared access

// Main thread's message handler for worker responses
worker.onmessage = function(e) {
  console.log(e.data + '!!'); // Expected: 'hello!!!!'
};

// Send a message to the worker
worker.postMessage('hello'); // Initiates the worker process

// You can test worker internal functions directly via the shared 'self' object
console.log(self.bark('bye')); // Expected: 'bye!!'

view raw JSON →