{"id":18049960,"url":"https://github.com/apacheli/web-workers-polyfill","last_synced_at":"2025-04-10T12:15:02.612Z","repository":{"id":61838842,"uuid":"554956773","full_name":"apacheli/web-workers-polyfill","owner":"apacheli","description":"Web Workers Polyfill for Node.js.","archived":false,"fork":false,"pushed_at":"2024-07-05T22:22:14.000Z","size":16,"stargazers_count":3,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-03-24T11:07:39.221Z","etag":null,"topics":["bun","deno","javascript","js","nodejs","npm","polyfill","web","worker"],"latest_commit_sha":null,"homepage":"https://www.npmjs.com/package/@apacheli/web-workers","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/apacheli.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.txt","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2022-10-20T17:41:46.000Z","updated_at":"2024-07-05T23:42:58.000Z","dependencies_parsed_at":"2024-07-06T00:14:26.446Z","dependency_job_id":null,"html_url":"https://github.com/apacheli/web-workers-polyfill","commit_stats":{"total_commits":5,"total_committers":1,"mean_commits":5.0,"dds":0.0,"last_synced_commit":"60d059ef285a68545bfa082bf0674a143429b409"},"previous_names":[],"tags_count":4,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/apacheli%2Fweb-workers-polyfill","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/apacheli%2Fweb-workers-polyfill/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/apacheli%2Fweb-workers-polyfill/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/apacheli%2Fweb-workers-polyfill/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/apacheli","download_url":"https://codeload.github.com/apacheli/web-workers-polyfill/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248217083,"owners_count":21066633,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["bun","deno","javascript","js","nodejs","npm","polyfill","web","worker"],"created_at":"2024-10-30T21:09:55.603Z","updated_at":"2025-04-10T12:15:02.590Z","avatar_url":"https://github.com/apacheli.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Web Workers Polyfill for Node.js\n\nA polyfill for Web Workers for Node.js.\n\n- Adds `ErrorEvent`\n- Adds `Worker`\n- Adds `globalThis.self`\n\n\u003e [!NOTE]\\\n\u003e Web Workers are currently not supported natively in Node.js v22.4.0. Please 👍\n\u003e [this issue](https://github.com/nodejs/node/issues/43583) to show your\n\u003e support!\n\n## Installing\n\n```sh\n$ npm i @apacheli/web-workers\n```\n\n## Useful Links\n\n- [Node.js ESM Modules](https://nodejs.org/api/esm.html)\n- [Node.js `worker_threads`](https://nodejs.org/api/worker_threads.html)\n- [Web Workers Documentation on MDN](https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API/Using_web_workers)\n\n## Examples\n\n### Using CommonJS\n\nWhile I recommend using ESM instead of CommonJS, the focus of this module is to\nadd Web Workers support to Node.js.\n\nHere is a simple example:\n\n`main.js`\n\n```js\nconst { Worker } = require(\"@apacheli/web-workers\");\n\nconst worker = new Worker(\"./worker.js\");\n\nworker.addEventListener(\"message\", (event) =\u003e {\n  console.log(\"message from worker.js:\", event.data);\n  worker.terminate();\n});\n\nworker.postMessage(\"Hello, World!\");\n```\n\n`worker.js`\n\n```js\nconst { self } = require(\"@apacheli/web-workers\");\n\nself.addEventListener(\"message\", (event) =\u003e {\n  console.log(\"message from main:\", event.data);\n});\n\nself.postMessage(\"hi\");\n```\n\nYields the following:\n\n```\n$ node main.js\nmessage from worker.js: hi\nmessage from main: Hello, World!\n```\n\nRegularly importing the module does not modify the global namespace. Instead,\nyou can import `@apacheli/web-workers/global` script to modify the global\nnamespace:\n\n`main.js`\n\n```js\nrequire(\"@apacheli/web-workers/global\");\n\nconst worker = new Worker(\"./worker.js\");\n\nworker.addEventListener(\"message\", (event) =\u003e {\n  console.log(\"message from worker.js:\", event.data);\n  worker.terminate();\n});\n\nworker.postMessage(\"Hello, World!\");\n```\n\n`worker.js`\n\n```js\nrequire(\"@apacheli/web-workers/global\");\n\nself.addEventListener(\"message\", (event) =\u003e {\n  console.log(\"message from main:\", event.data);\n});\n\nself.postMessage(\"hi\");\n```\n\n## Using ESM\n\n\u003e [!NOTE]\\\n\u003e You may have to append `.js` to the import.\n\n`main.js`\n\n```js\nimport \"@apacheli/web-workers/global.js\";\n\nconst worker = new Worker(new URL(\"./worker.js\", import.meta.url), {\n  type: \"module\",\n});\n\nworker.addEventListener(\"message\", (event) =\u003e {\n  console.log(\"message from worker:\", event.data);\n  worker.terminate();\n});\n\nworker.postMessage(\"Hello, World!\");\n```\n\n`worker.js`\n\n```js\nimport \"@apacheli/web-workers/global.js\";\n\nself.addEventListener(\"message\", (event) =\u003e {\n  console.log(\"message from main:\", event.data);\n});\n\nself.postMessage(\"hi\");\n```\n\nFor maximum cross-platform compatibility, you should use `URL` to specify your\nworker. `new Worker(\"./worker.js\")` will still work in Node.js though.\n\nIf you use platforms such as Deno and Bun, the global namespace will not be\ntampered. It will just reexport the already existing implementations.\n\n```js\nimport { Worker } from \"@apacheli/web-workers\";\n// ^ Reexports `Worker` if it already exists\n\nconsole.log(Worker === globalThis.Worker);\n// =\u003e Node: false - because globalThis.Worker does not exist\n// =\u003e Deno: true\n// =\u003e Bun: true\n```\n\n```js\nimport \"@apacheli/web-workers/global.js\";\n// ^ Does nothing in Deno and Bun\n\nconsole.log(Worker === globalThis.Worker);\n// =\u003e Node: true\n// =\u003e Deno: true\n// =\u003e Bun: true\n```\n\n```js\nconsole.log(Worker === globalThis.Worker);\n// =\u003e Node: Uncaught ReferenceError: Worker is not defined\n// =\u003e Deno: true\n// =\u003e Bun: true\n```\n\n## License\n\n[MIT License](LICENSE.txt)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fapacheli%2Fweb-workers-polyfill","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fapacheli%2Fweb-workers-polyfill","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fapacheli%2Fweb-workers-polyfill/lists"}