{"id":13393535,"url":"https://github.com/developit/web-worker","last_synced_at":"2025-05-12T13:17:31.614Z","repository":{"id":40238170,"uuid":"229514441","full_name":"developit/web-worker","owner":"developit","description":"Consistent Web Workers in browser and Node.","archived":false,"fork":false,"pushed_at":"2025-01-31T18:09:24.000Z","size":176,"stargazers_count":1159,"open_issues_count":15,"forks_count":57,"subscribers_count":10,"default_branch":"main","last_synced_at":"2025-05-09T07:55:47.282Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"https://npm.im/web-worker","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/developit.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE","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":"2019-12-22T03:44:28.000Z","updated_at":"2025-05-04T14:02:07.000Z","dependencies_parsed_at":"2024-09-25T00:04:09.287Z","dependency_job_id":"6cc3d204-dc3b-47bb-9d04-f962e1d40f46","html_url":"https://github.com/developit/web-worker","commit_stats":{"total_commits":25,"total_committers":7,"mean_commits":"3.5714285714285716","dds":0.56,"last_synced_commit":"29fef9775702c91887d3d8733e595edf1a188f31"},"previous_names":[],"tags_count":6,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/developit%2Fweb-worker","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/developit%2Fweb-worker/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/developit%2Fweb-worker/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/developit%2Fweb-worker/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/developit","download_url":"https://codeload.github.com/developit/web-worker/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":253547215,"owners_count":21925545,"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":[],"created_at":"2024-07-30T17:00:55.486Z","updated_at":"2025-05-12T13:17:31.566Z","avatar_url":"https://github.com/developit.png","language":"JavaScript","readme":"\u003ch1 align=\"center\"\u003e\n  web-worker\n  \u003ca href=\"https://www.npmjs.org/package/web-worker\"\u003e\u003cimg src=\"https://img.shields.io/npm/v/web-worker.svg?style=flat-square\" alt=\"npm\"\u003e\u003c/a\u003e\n\u003c/h1\u003e\n\u003cp align=\"center\"\u003e\n  Native cross-platform Web Workers. Works in published npm modules.\n\u003c/p\u003e\n\u003cp align=\"center\"\u003e\n  \u003cimg src=\"https://user-images.githubusercontent.com/105127/79602228-1998bf00-80b8-11ea-91e4-26b212aabaa2.png\" width=\"1000\" alt=\"\"\u003e\n\u003c/p\u003e\n\n**In Node**, it's a web-compatible Worker implementation atop Node's [worker_threads](https://nodejs.org/api/worker_threads.html).\n\n**In the browser** (and when bundled for the browser), it's simply an alias of `Worker`.\n\n### Features\n\n_Here's how this is different from worker_threads:_\n\n- makes Worker code compatible across browser and Node\n- supports Module Workers (`{type:'module'}`) natively in Node 12.8+\n- uses DOM-style events (`Event.data`, `Event.type`, etc)\n- supports event handler properties (`worker.onmessage=..`)\n- `Worker()` accepts a module URL, Blob URL or Data URL\n- emulates browser-style [WorkerGlobalScope] within the worker\n\n### Usage Example\n\nIn its simplest form:\n\n```js\nimport Worker from 'web-worker';\n\nconst worker = new Worker('data:,postMessage(\"hello\")');\nworker.onmessage = e =\u003e console.log(e.data);  // \"hello\"\n```\n\n\u003ctable\u003e\n\u003cthead\u003e\u003ctr\u003e\u003cth\u003e\u003cstrong\u003emain.js\u003c/strong\u003e\u003c/th\u003e\u003cth\u003e\u003cstrong\u003eworker.js\u003c/strong\u003e\u003c/th\u003e\u003c/tr\u003e\u003c/thead\u003e\n\u003ctbody\u003e\u003ctr\u003e\u003ctd\u003e\n\n```js\nimport Worker from 'web-worker';\n\nconst url = new URL('./worker.js', import.meta.url);\nconst worker = new Worker(url);\n\nworker.addEventListener('message', e =\u003e {\n  console.log(e.data)  // \"hiya!\"\n});\n\nworker.postMessage('hello');\n```\n\n\u003c/td\u003e\u003ctd valign=\"top\"\u003e\n\n```js\naddEventListener('message', e =\u003e {\n  if (e.data === 'hello') {\n    postMessage('hiya!');\n  }\n});\n```\n\n\u003c/td\u003e\u003c/tr\u003e\u003c/tbody\u003e\n\u003c/table\u003e\n\n👉 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()`.\n\n\u003e _Support for this pattern in build tools and test frameworks is still limited. We are [working on growing this](https://github.com/developit/web-worker/issues/4)._\n\n### Module Workers\n\nModule Workers are supported in Node 12.8+ using this plugin, leveraging Node's native ES Modules support.\nIn 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:\n\n\u003ctable\u003e\n\u003cthead\u003e\u003ctr\u003e\u003cth\u003e\u003cstrong\u003emain.mjs\u003c/strong\u003e\u003c/th\u003e\u003cth\u003e\u003cstrong\u003eworker.mjs\u003c/strong\u003e\u003c/th\u003e\u003c/tr\u003e\u003c/thead\u003e\n\u003ctbody\u003e\u003ctr\u003e\u003ctd\u003e\n\n```js\nimport Worker from 'web-worker';\n\nconst worker = new Worker(\n  new URL('./worker.mjs', import.meta.url),\n  { type: 'module' }\n);\nworker.addEventListener('message', e =\u003e {\n  console.log(e.data)  // \"200 OK\"\n});\nworker.postMessage('https://httpstat.us/200');\n```\n\n\u003c/td\u003e\u003ctd valign=\"top\"\u003e\n\n```js\nimport fetch from 'isomorphic-fetch';\n\naddEventListener('message', async e =\u003e {\n  const url = e.data;\n  const res = await fetch(url)\n  const text = await res.text();\n  postMessage(text);\n});\n```\n\n\u003c/td\u003e\u003c/tr\u003e\u003c/tbody\u003e\n\u003c/table\u003e\n\n\n### Data URLs\n\nInstantiating Worker using a Data URL is supported in both module and classic workers:\n\n```js\nimport Worker from 'web-worker';\n\nconst worker = new Worker(`data:application/javascript,postMessage(42)`);\nworker.addEventListener('message', e =\u003e {\n  console.log(e.data)  // 42\n});\n```\n\n### Special Thanks\n\nThis module aims to provide a simple and forgettable piece of infrastructure,\nand as such it needed an obvious and descriptive name.\n[@calvinmetcalf](https://github.com/calvinmetcalf), who you may recognize as the author of [Lie](https://github.com/calvinmetcalf/lie) and other fine modules, gratiously offered up the name from his `web-worker` package.\nThanks Calvin!\n\n\n[worker-plugin]: https://github.com/googlechromelabs/worker-plugin\n[rollup-plugin-off-main-thread]: https://github.com/surma/rollup-plugin-off-main-thread\n[WorkerGlobalScope]: https://developer.mozilla.org/en-US/docs/Web/API/WorkerGlobalScope\n","funding_links":[],"categories":["JavaScript","others"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdevelopit%2Fweb-worker","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdevelopit%2Fweb-worker","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdevelopit%2Fweb-worker/lists"}