{"id":15650038,"url":"https://github.com/okikio/sharedworker","last_synced_at":"2025-04-05T08:03:46.967Z","repository":{"id":48162056,"uuid":"416760894","full_name":"okikio/sharedworker","owner":"okikio","description":"A small spec. compliant polyfill for SharedWorkers, it acts as a drop in replacement for normal Workers.","archived":false,"fork":false,"pushed_at":"2024-11-20T08:21:21.000Z","size":1742,"stargazers_count":48,"open_issues_count":2,"forks_count":5,"subscribers_count":3,"default_branch":"main","last_synced_at":"2025-03-29T07:03:09.458Z","etag":null,"topics":["javascript","polyfill","ponyfill","sharedworker","typescript","webworker","worker"],"latest_commit_sha":null,"homepage":"https://sharedworker.okikio.dev","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/okikio.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"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":"2021-10-13T13:44:32.000Z","updated_at":"2025-03-28T12:22:23.000Z","dependencies_parsed_at":"2023-12-13T07:30:53.214Z","dependency_job_id":"30a52211-2625-46bf-aba7-2c411b5cedcb","html_url":"https://github.com/okikio/sharedworker","commit_stats":{"total_commits":84,"total_committers":5,"mean_commits":16.8,"dds":"0.38095238095238093","last_synced_commit":"004a4adbd87f1a98d81f1e9bd386ccc927332b87"},"previous_names":[],"tags_count":8,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/okikio%2Fsharedworker","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/okikio%2Fsharedworker/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/okikio%2Fsharedworker/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/okikio%2Fsharedworker/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/okikio","download_url":"https://codeload.github.com/okikio/sharedworker/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247305932,"owners_count":20917208,"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":["javascript","polyfill","ponyfill","sharedworker","typescript","webworker","worker"],"created_at":"2024-10-03T12:33:05.799Z","updated_at":"2025-04-05T08:03:46.946Z","avatar_url":"https://github.com/okikio.png","language":"JavaScript","readme":"# @okikio/sharedworker\n\n[![Open Bundle](https://bundlejs.com/badge-light.svg)](https://bundlejs.com/?q=@okikio/sharedworker\u0026bundle)\n\n[NPM](https://www.npmjs.com/package/@okikio/sharedworker) \u003cspan style=\"padding-inline: 1rem\"\u003e|\u003c/span\u003e [Github](https://github.com/okikio/sharedworker#readme) \u003cspan style=\"padding-inline: 1rem\"\u003e|\u003c/span\u003e [Docs](https://sharedworker.okikio.dev) \u003cspan style=\"padding-inline: 1rem\"\u003e|\u003c/span\u003e [Licence](./LICENSE)  \n\n\nA small mostly spec. compliant polyfill/ponyfill for `SharedWorkers`, it acts as a drop in replacement for normal `Workers`, and supports a similar API surface that matches normal `Workers`.\n\n\u003e * [Ponyfills](https://github.com/sindresorhus/ponyfill) are seperate modules that are included to replicate the functionality of the original API, but are not required to be used.\n\u003e * [Polyfills](https://developer.mozilla.org/en-US/docs/Glossary/Polyfill) update the original API on the global scope if it isn't supported in that specific environment or it's feature set is lacking compared to modern variations.\n\n\u003e Check out the [blog post](https://blog.okikio.dev/sharedworker), created for it's launch. \n\n## Installation\n```bash\nnpm install @okikio/sharedworker\n```\n\n\u003cdetails\u003e\n    \u003csummary\u003eOthers\u003c/summary\u003e\n\n```bash\nyarn add @okikio/sharedworker\n```\n\nor \n\n```bash\npnpm install @okikio/sharedworker\n```\n\u003c/details\u003e\n\n## Usage\n\n```ts\nimport { SharedWorkerPolyfill as SharedWorker } from \"@okikio/sharedworker\";\n// or \nimport SharedWorker from \"@okikio/sharedworker\";\n```\n\nYou can also use it directly through a script tag:\n```html\n\u003cscript src=\"https://unpkg.com/@okikio/sharedworker\"\u003e\u003c/script\u003e\n\u003cscript type=\"module\"\u003e\n    // You can then use it like this\n    const { SharedWorkerPolyfill: SharedWorker } = window.sharedworker; \n\u003c/script\u003e\n```\n\nYou can also use it via a CDN, e.g.\n```ts\nimport SharedWorker from \"https://cdn.skypack.dev/@okikio/sharedworker\";\n// or \nimport SharedWorker from \"https://cdn.jsdelivr.net/npm/@okikio/sharedworker\";\n// or\nimport SharedWorker from \"https://esm.sh/@okikio/sharedworker\";\n// or any number of other CDN's\n```\n\nFor vite and other bundlers you can also use the new `SharedWorkerPonyfill` like so ([#9](https://github.com/okikio/sharedworker/issues/9)), to address one-off issues with workers\n\n```ts\nimport { SharedWorkerPonyfill, SharedWorkerSupported } from \"@okikio/sharedworker\";\n\nlet worker: SharedWorkerPonyfill;\n\nif (SharedWorkerSupported) {\n    worker = new SharedWorkerPonyfill(new SharedWorker(new URL(\"./../worker.ts\", import.meta.url), { name: \"position-sync\", type: \"module\" }));\n} else {\n    worker = new SharedWorkerPonyfill(new Worker(new URL(\"./../worker.ts\", import.meta.url), { name: \"position-sync\", type: \"module\" }));\n}\n```\n\n`@okikio/sharedworker` supports the same API surfaces as `SharedWorker` and `Worker`, except it adds some none spec. compliant properties and methods to the `SharedWorkerPolyfill` class, that enables devs to use `SharedWorker`'s on browsers that don't support it.\n\nIn order to support browsers that don't natively support `SharedWorker`'s, the actual worker file needs to be tweaked slightly,\n\n```ts\n/* \n * All variables and values outside the `start(...)` function are shared between all pages, this behavior can cause unexpected bugs if you're not careful\n */\nconst start = (port) =\u003e {\n    // All your normal Worker and SharedWorker stuff can be placed here and should just work, with no extra setup required \n    \n    /** \n     * All variables and values inside the `start(...)` function are isolated to each page, and will be allocated seperately per page. \n     */\n    port.onmessage = ({ data }) =\u003e {\n        console.log(\"Cool\")\n    };\n};\n\nself.onconnect = e =\u003e {\n    let [port] = e.ports;\n    start(port);\n};\n\n// This is the fallback for WebWorkers, in case the browser doesn't support SharedWorkers natively\nif (!(\"SharedWorkerGlobalScope\" in self)) \n    start(self);\n```\n\n\u003e _**Note**: make sure to read the comments in the above code carefully to avoid unexpected bugs._\n\n## Showcase\n\nA couple sites that use `@okikio/sharedworker`:\n* [astro.build/play](https://astro.build/play) - [GitHub](https://github.com/snowpackjs/astro-repl)\n* [bundlejs.com](https://bundlejs.com) - [GitHub](https://github.com/okikio/bundle)\n* Your site here...\n\n## API\n\nThe API of `@okikio/sharedworker` closely match the web `SharedWorker` API, except that all the major methods and properties of `SharedWorker.prototype.port` are available directly on `SharedWorker.prototype` including `addEventListener` and `removeEventListener`. \n\n\u003e _**Note:** the normal functionality of the methods and properties that are normally available on `SharedWorker.prototype` will still be kept intact, in `@okikio/sharedworker`._ \n\nIn addition, the `terminate()` method was added to `@okikio/sharedworker`, this allows both the `close()` method (this is from `SharedWorker.prototype.port`) and the `terminate()` method to manually close workers. \n\nCheck out the [API site](https://sharedworker.okikio.dev) for detailed API documentation.\n\n## Browser Support\n\n| Chrome | Edge | Firefox | Safari | IE  |\n| ------ | ---- | ------- | ------ | --- |\n| 4+     | 12+  | 4+      | 4+     | 10+ |\n\nNative support for `SharedWorker` is not supported at all on Safari and IE, as well as all mobile browsers (excluding Firefox For Android).\n\n\u003e _**Note:** some features of `Workers` appeared at later versions of the spec., so, I suggest looking into the feature support table for [Workers](https://developer.mozilla.org/en-US/docs/Web/API/Worker#browser_compatibility) and [SharedWorkers](https://developer.mozilla.org/en-US/docs/Web/API/SharedWorker#browser_compatibility)._ \n\n\n## Contributing\n\nI encourage you to use [pnpm](https://pnpm.io/configuring) to contribute to this repo, but you can also use [yarn](https://classic.yarnpkg.com/lang/en/) or [npm](https://npmjs.com) if you prefer.\n\nInstall all necessary packages\n```bash\nnpm install\n```\n\nThen run tests (WIP)\n```bash\nnpm test\n```\n\nBuild project \n```bash\nnpm run build\n```\n\nPreview API Docs\n```bash\nnpm run typedoc \u0026\u0026 npm run preview\n```\n\n\u003e _**Note**: this project uses [Conventional Commits](https://www.conventionalcommits.org/en/v1.0.0/) standard for commits, so, please format your commits using the rules it sets out._\n\n## Licence\nSee the [LICENSE](./LICENSE) file for license rights and limitations (MIT).\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fokikio%2Fsharedworker","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fokikio%2Fsharedworker","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fokikio%2Fsharedworker/lists"}