web-worker
Consistent Web Workers in browser and Node.
Last updated 7 months ago by developit .
Apache-2.0 · Repository · Bugs · Original npm · Tarball · package.json
$ cnpm install web-worker 
SYNC missed versions from official npm registry.

web-worker

Native cross-platform Web Workers. Works in published npm modules.

import Worker from 'web-worker';

const worker = new Worker('data:,postMessage("hello")');
worker.onmessage = e => console.log(e.data);  // "hello"

In Node, it's a web-compatible Worker implementation atop Node's worker_threads.

In the browser (and when bundled for the browser), it's simply an alias of Worker.

Features

Here's how this is different from worker_threads:

  • makes Worker code compatible across browser and Node
  • uses DOM-style events (Event.data, Event.type, etc)
  • supports event handler properties (worker.onmessage=..)
  • Worker() constructor accepts a module URL
  • Supports the { type: 'module' } option natively in Node 12.8+
  • emulates browser-style WorkerGlobalScope within the worker

Usage Example

main.js worker.js
import Worker from 'web-worker';

const worker = new Worker(new URL('./worker.js', import.meta.url));

worker.addEventListener('message', e => {
  console.log(e.data)  // "hiya!"
});

worker.postMessage('hello');
addEventListener('message', e => {
  if (e.data === 'hello') {
    postMessage('hiya!');
  }
});

The pattern new URL('./worker.js', import.meta.url) is used above to load the worker relative to the current module instead of the application base URL. Without this, Worker URLs are relative to a document's URL, which in Node.js is interpreted to be process.cwd().

Support for this pattern in build tools and test frameworks is still quite limited, but we are working on growing this support (tracking issue https://github.com/developit/web-worker/issues/4).

Module Workers

Module Workers are supported in Node 12.8+ using this plugin, leveraging Node's native ES Modules support. In the browser, they can be used natively in Chrome 80+, or in all browsers via worker-plugin or rollup-plugin-off-main-thread. As with classic workers, there is no difference in usage between Node and the browser:

main.mjs worker.mjs
import Worker from 'web-worker';

const worker = new Worker(new URL('./worker.mjs', import.meta.url), {
  type: 'module'
});
worker.addEventListener('message', e => {
  console.log(e.data)  // "200 OK"
});
worker.postMessage('https://httpstat.us/200');
import fetch from 'isomorphic-fetch';

addEventListener('message', async e => {
  const url = e.data;
  const res = await fetch(url)
  const text = await res.text();
  postMessage(text);
});

Data URLs

Instantiating Worker using a Data URL is supported in both module and classic workers:

import Worker from 'web-worker';

const worker = new Worker(`data:application/javascript,postMessage(42)`);
worker.addEventListener('message', e => {
  console.log(e.data)  // 42
});

Special Thanks

This module aims to provide a simple and forgettable piece of infrastructure, and as such it needed an obvious and descriptive name. @calvinmetcalf, who you may recognize as the author of Lie and other fine modules, gratiously offered up the name from his web-worker package. Thanks Calvin!

Current Tags

  • 1.0.0                                ...           latest (7 months ago)

8 Versions

  • 1.0.0                                ...           7 months ago
  • 0.1.6                                ...           6 years ago
  • 0.1.5                                ...           6 years ago
  • 0.1.4                                ...           6 years ago
  • 0.1.3                                ...           6 years ago
  • 0.1.2                                ...           6 years ago
  • 0.1.0                                ...           6 years ago
  • 0.0.0                                ...           6 years ago
Downloads
Today 0
This Week 0
This Month 0
Last Day 0
Last Week 4
Last Month 20
Dependencies (0)
None
Dev Dependencies (9)

Copyright 2014 - 2017 © taobao.org |