Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
name: CI

on:
push:
pull_request:

jobs:
node_tests:
name: Node tests
runs-on: ubuntu-24.04
steps:
- name: Checkout
uses: actions/checkout@v4

- name: Setup Node
uses: actions/setup-node@v4
with:
node-version: "20"

- name: Install dependencies
run: npm ci

- name: Build
run: npm run build

- name: Test
run: npm run test:run
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,3 @@

lib/
node_modules/
package-lock.json
20 changes: 20 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
Copyright (c) 2021 Pau Marfany <p.marfany_96@hotmail.com>
Copyright (c) 2026 Xe Iaso <me@xeiaso.net>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
96 changes: 71 additions & 25 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,46 +1,92 @@
# worker-polyfill

A simple script that emulates web worker threads in non compatible browsers.
The code will still be slow (single threaded) but you can keep you code consistent.
A small polyfill that emulates the Dedicated [Web Worker API](https://developer.mozilla.org/en-US/docs/Web/API/Worker)
in browsers that don't support it (Internet Explorer and other old browsers).
The code is still single-threaded, but it lets you keep one consistent code
path. Messages are structured-cloned and delivered asynchronously, matching
the spec's observable behaviour.

Usage:
```js
// You must check for compatibility before loading the polyfill
if ( !Worker ) { require('worker-loader'); }
## Install

The polyfill installs itself as the global `Worker` **only if the browser has
no native implementation**, so it is safe to include unconditionally:

```html
<script src="worker-polyfill.js"></script>
```

Workers can be loaded and used as usual:
## Usage

Workers are created and used as usual:

```js
var worker = new Worker("your_script.js");
var worker = new Worker("your_script.js");

worker.onmessage = function(event) {
alert("Got: " + event.data);
};
worker.onmessage = function (event) {
alert("Got: " + event.data);
};

worker.onerror = function(error) {
alert("Worker error: " + error);
worker.onerror = function (event) {
alert("Worker error: " + event.message);
};

worker.postMessage("Hello World");
worker.postMessage("Hello World");
```

'addEventListener' methods are also supported:
`addEventListener` is supported with full multi-listener semantics:

```js
var worker = new Worker("your_script.js");

worker.addEventListener("message", function(event) {
alert("Got: " + event.data);
worker.addEventListener("message", function (event) {
alert("Got: " + event.data);
});

worker.addEventListener("error", function(error) {
alert("Worker error: " + error);
worker.addEventListener("error", function (event) {
alert("Worker error: " + event.message);
});
```

worker.postMessage("Hello World");
Inside the worker script, the usual idioms work because the script runs in its
own scope:

```js
// your_script.js
self.onmessage = function (event) {
self.postMessage("echo: " + event.data);
};
// importScripts() is supported and runs synchronously
// close() stops the worker
```

- For more info on worker threads see
https://developer.mozilla.org/En/Using_web_workers
## Supported

`postMessage` (structured-cloned, ordered), `terminate`, `close`,
`addEventListener`/`removeEventListener`/`dispatchEvent`, `message`/`error`
events, `importScripts` (synchronous), `self`/`name`.

## Not supported

Module workers (`type: "module"`), transferable objects, `blob:`/`data:` URLs,
`credentials`, and real OS-thread parallelism. Pass plain, cloneable data.

## Limitations

`importScripts()` is synchronous and executes imported scripts immediately,
but top-level `var`/`function` declarations in an imported script are NOT
visible to the main worker script (or vice versa). Use `self.X = ...`
assignments (which land on the worker scope) to share values across scripts
loaded via `importScripts()`.

The bare identifier `arguments` (and the internal parameter names
`__worker_scope__` and `__worker_src__`) are shadowed inside top-level
worker code due to the Function constructor wrapper—avoid using `arguments`
as an identifier in worker scripts.

## Development

```bash
npm install
npm test # vitest + jsdom conformance suite
npm run build # webpack UMD bundle into lib/
```

- For more on workers see https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API
- Originally exported from Google Code (https://code.google.com/p/ie-web-worker/).
Loading
Loading