{"id":20513857,"url":"https://github.com/webreflection/proxied-worker","last_synced_at":"2025-04-14T00:01:46.184Z","repository":{"id":48311402,"uuid":"389303533","full_name":"WebReflection/proxied-worker","owner":"WebReflection","description":"A tiny utility to asynchronously drive a namespace exposed through a Worker.","archived":false,"fork":false,"pushed_at":"2023-07-04T16:40:02.000Z","size":70,"stargazers_count":49,"open_issues_count":1,"forks_count":1,"subscribers_count":3,"default_branch":"main","last_synced_at":"2025-03-27T14:11:09.424Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"isc","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/WebReflection.png","metadata":{"files":{"readme":"README.md","changelog":null,"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}},"created_at":"2021-07-25T08:56:23.000Z","updated_at":"2025-02-22T04:56:09.000Z","dependencies_parsed_at":"2024-02-17T06:31:42.160Z","dependency_job_id":"4dd17108-a063-4e34-a625-adf722318dcf","html_url":"https://github.com/WebReflection/proxied-worker","commit_stats":{"total_commits":41,"total_committers":1,"mean_commits":41.0,"dds":0.0,"last_synced_commit":"ae37d672e3f5965145706b3fa47f83955b30f53c"},"previous_names":[],"tags_count":17,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/WebReflection%2Fproxied-worker","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/WebReflection%2Fproxied-worker/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/WebReflection%2Fproxied-worker/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/WebReflection%2Fproxied-worker/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/WebReflection","download_url":"https://codeload.github.com/WebReflection/proxied-worker/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248799931,"owners_count":21163403,"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-11-15T21:13:32.288Z","updated_at":"2025-04-14T00:01:46.127Z","avatar_url":"https://github.com/WebReflection.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# proxied-worker DEPRECATED - See [coincident](https://github.com/WebReflection/coincident#coincidentserver)\n\n\u003csup\u003e**Social Media Photo by [Ricardo Gomez Angel](https://unsplash.com/@ripato) on [Unsplash](https://unsplash.com/)**\u003c/sup\u003e\n\nA tiny utility to asynchronously drive a namespace exposed through a Shared/Service/Worker:\n\n  * property access\n  * functions invokes\n  * instances creation ...\n  * ... and instances methods invokes, or properties access\n\nInstances reflected on the client are automatically cleared up on the worker though a dedicated *FinalizationRegistry*.\n\nIt is also possible, since `v0.5.0`, to use functions as arguments, although these are stored \"*forever*\", so use this feature with caution.\nBear in mind, the context is currently not propagated from the Worker, so if it's strictly needed, bind the listener before passing it as-is.\n\n\n### Related + NodeJS\n\nThis module is a modern simplification of [workway](https://github.com/WebReflection/workway#readme), heavily inspired by [electroff](https://github.com/WebReflection/electroff#readme), but also **[available for NodeJS too](https://github.com/WebReflection/proxied-node#readme)** as a safer, lighter, and easier alternative.\n\n\n## Compatibility / Requirements\n\nThis module works with latest browsers, as long as the following APIs are available:\n\n  * [FinalizationRegistry](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/FinalizationRegistry)\n  * [Proxy](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Proxy)\n  * [Worker](https://developer.mozilla.org/en-US/docs/Web/API/Worker/Worker)\n\n**[Live Demo](https://webreflection.github.io/proxied-worker/test/)**\n\n## API\n\nThe exported namespace provides a mechanism to await any part of it, including a top level `addEventListener` and `removeEventListener`, to allow listening to custom `postMessage` notifications from the Service/Shared/Worker.\n\nSee [worker.js](./test/worker.js) to better understand how this works.\n\n\n## Example\n\n```js\n// client.js\nimport ProxiedWorker from 'https://unpkg.com/proxied-worker/client';\n\n// point at the file that exports a namespace\nconst nmsp = ProxiedWorker('./worker.js');\n\n// custom notifications from the Worker\nnmsp.addEventListener('message', ({data: {action}}) =\u003e {\n  if (action === 'greetings')\n    console.log('Worker said hello 👋');\n});\n\n// v0.5.0+ use listenres like features\nnmsp.on('listener', (action, type) =\u003e {\n  console.log(action, 'called with type', type);\n});\n\n// access its properties\nconsole.log(await nmsp.test);\n\n// or its helpers\nconsole.log(await nmsp.sum(1, 2));\nawait nmsp.delayed();\n\n// or create instances\nconst instance = await new nmsp.Class('🍻');\n// and invoke their methods\nconsole.log(await instance.sum(1, 2));\n\n// - - - - - - - - - - - - - - - - - - - - - - \n\n// worker.js\nimportScripts('https://unpkg.com/proxied-worker/server');\n\nProxiedWorker({\n  test: 'OK',\n  sum(a, b) {\n    return a + b;\n  },\n  on(type, callback) {\n    setTimeout(() =\u003e {\n      callback('Event', type);\n    });\n  },\n  async delayed() {\n    console.log('context', this.test);\n    postMessage({action: 'greetings'});\n    return await new Promise($ =\u003e setTimeout($, 500, Math.random()));\n  },\n  Class: class {\n    constructor(name) {\n      this.name = name;\n    }\n    sum(a, b) {\n      console.log(this.name, a, b);\n      return a + b;\n    }\n  }\n});\n```\n\nAlternatively, if the browser supports workers as module:\n\n```js\n// client.js\nimport ProxiedWorker from 'https://unpkg.com/proxied-worker/client';\nconst nmsp = ProxiedWorker('./worker.js', {type: 'module'});\n\n// worker.js\nimport ProxiedWorker from 'https://unpkg.com/proxied-worker/module';\nProxiedWorker({\n  // ...\n});\n```\n\n\n## As SharedWorker\n\nThe `ProxiedWorker` signature is similar to a `Worker` one, plus an extra third argument that is the constructor to use.\n\nIn order to have a `SharedWorker`, this code might be used:\n\n```js\n// client.js\nimport ProxiedWorker from 'https://unpkg.com/proxied-worker/client';\nconst nmsp = ProxiedWorker('./shared-worker.js', {type: 'module'}, SharedWorker);\n\n// shared-worker.js\nimport ProxiedWorker from 'https://unpkg.com/proxied-worker/module';\nProxiedWorker({\n  // ...\n});\n```\n\n\n## As ServiceWorker\n\nSimilarly to a `SharedWorker`, it is also possible to register and use a `ServiceWorker` to compute heavy tasks.\n\n```js\n// client.js\nimport ProxiedWorker from 'https://unpkg.com/proxied-worker/client';\nconst nmsp = ProxiedWorker('./service-worker.js', {scope: '/'}, ServiceWorker);\n\n// service-worker.js\nimportScripts('https://unpkg.com/proxied-worker/server');\n\nProxiedWorker({\n  // ...\n});\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fwebreflection%2Fproxied-worker","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fwebreflection%2Fproxied-worker","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fwebreflection%2Fproxied-worker/lists"}