@eshaz/web-worker

中文文档
1.2.2发布于 2023/11/27Apache-2.0TypeScriptCommonJS零依赖近 30 天下载 366.2万

Consistent Web Workers in browser and Node.

原始使用文档
文档目录5 节
  1. Features
  2. Usage Example
  3. Module Workers
  4. Data URLs
  5. Special Thanks

web-worker npm

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

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
  • supports Module Workers ({type:'module'}) natively in Node 12.8+
  • uses DOM-style events (Event.data, Event.type, etc)
  • supports event handler properties (worker.onmessage=..)
  • Worker() accepts a module URL, Blob URL or Data URL
  • emulates browser-style WorkerGlobalScope within the worker

Usage Example

In its simplest form:

import Worker from 'web-worker';

const worker = new Worker('data:,postMessage("hello")');
worker.onmessage = e => console.log(e.data);  // "hello"
main.jsworker.js
import Worker from 'web-worker';

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

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

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

👉 Notice how 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 limited. We are working on growing this.

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.mjsworker.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!

版本与发布记录

@eshaz/web-worker 共发布 3 个版本,首次发布于 2022/12/12;过去 12 个月发布了 0 个版本。

版本发布日期距今
1.2.2latest2023/11/272 年前
1.2.12023/06/023 年前
1.2.02022/12/123 年前

关于 @eshaz/web-worker 的常见问题

@eshaz/web-worker 是什么?

Consistent Web Workers in browser and Node.

如何安装 @eshaz/web-worker?

在项目目录运行 npm install @eshaz/web-worker,或使用 pnpm add / yarn add / bun add。

@eshaz/web-worker 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@eshaz/web-worker 支持 ESM 还是 CommonJS?

以 CommonJS 形式发布,可以直接 require();在 ES Module 项目中也可以用 import 引入(Node.js 会把 module.exports 作为默认导出)。

@eshaz/web-worker 可以商用吗?

@eshaz/web-worker 使用 Apache-2.0 许可。宽松许可:允许商业使用、修改和再分发,并附带明确的专利授权;分发时需保留许可与 NOTICE 文件,修改过的文件要注明改动。以上为概括,不构成法律意见,请以许可原文为准。

@eshaz/web-worker 的下载量有多少?

根据 npm 官方下载统计,@eshaz/web-worker 在 2026/10/14 – 2026/11/12 这 30 天内被下载 3,662,263 次,日均约 122,075 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@eshaz/web-worker 还在维护吗?

最新版本 1.2.2 发布于 2023/11/27(2 年前);至今共发布 3 个版本,其中过去 12 个月发布了 0 个;首次发布于 2022/12/12。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。