{"id":17236604,"url":"https://github.com/illright/worker-request-response","last_synced_at":"2025-03-26T01:42:07.001Z","repository":{"id":42533800,"uuid":"476306248","full_name":"illright/worker-request-response","owner":"illright","description":"A Promise API for submitting requests to workers and tracking responses.","archived":false,"fork":false,"pushed_at":"2023-09-02T17:56:51.000Z","size":51,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-03-17T19:27:04.919Z","etag":null,"topics":["promise","service-workers","web-workers"],"latest_commit_sha":null,"homepage":"","language":"TypeScript","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/illright.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,"publiccode":null,"codemeta":null}},"created_at":"2022-03-31T12:57:26.000Z","updated_at":"2022-04-06T19:25:07.000Z","dependencies_parsed_at":"2024-10-22T23:07:54.223Z","dependency_job_id":null,"html_url":"https://github.com/illright/worker-request-response","commit_stats":{"total_commits":30,"total_committers":3,"mean_commits":10.0,"dds":"0.23333333333333328","last_synced_commit":"f26e4631ba2425ad73ae7d4b138960f56c85f2a1"},"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/illright%2Fworker-request-response","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/illright%2Fworker-request-response/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/illright%2Fworker-request-response/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/illright%2Fworker-request-response/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/illright","download_url":"https://codeload.github.com/illright/worker-request-response/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245573840,"owners_count":20637670,"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":["promise","service-workers","web-workers"],"created_at":"2024-10-15T05:36:32.958Z","updated_at":"2025-03-26T01:42:06.981Z","avatar_url":"https://github.com/illright.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# `worker-request-response` 👷‍♀️💬🗨️\n\n[![npm version](https://img.shields.io/npm/v/worker-request-response)](https://www.npmjs.com/package/worker-request-response)\n[![minzipped package size](https://img.shields.io/bundlephobia/minzip/worker-request-response.svg)](https://bundlephobia.com/package/worker-request-response)\n\n_Ever wish you could request some data from a service/web worker and have them respond to you asynchronously?_\n\nA Promise API for submitting requests to workers and tracking responses.\n\n## Installation\n\n```bash\npnpm add worker-request-response\n```\n\nType definitions are built in 😎.\n\n\u003cdetails\u003e\n  \u003csummary\u003eI don't use \u003ccode\u003epnpm\u003c/code\u003e\u003c/summary\u003e\n\nWhat do you mean \"I don't use [`pnpm`](https://pnpm.io)\"? It's so much faster! Alright, here's your `npm` command:\n\n```bash\nnpm install --save worker-request-response\n```\n\n\u003c/details\u003e\n\n## Usage\n\nIt has two functions exported: \n* `sendRequest` for your main thread code\n* `handleRequestsWith` for your worker code\n\nHere's an example of a service worker that converts numbers to strings:\n\n```ts\n// In your main thread\nimport { sendRequest } from 'worker-request-response';\n\nexport async function sendRequestToServiceWorker() {\n  const response = await sendRequest\u003cnumber, string\u003e(navigator.serviceWorker.controller, 42);\n  console.log(response, 'is \"42\"');\n}\n```\n\n```ts\n// In your worker\nimport { handleRequestsWith } from 'worker-request-response';\n\nasync function processRequest(event: MessageEvent\u003cnumber\u003e): Promise\u003cstring\u003e {\n  return event.data.toString();\n}\n\nself.addEventListener('message', handleRequestsWith(processRequest));\n```\n\nThere, that simple. In glorious TypeScript, too.\n\n### Usage with web workers\n\nThe `sendRequest` function accepts the worker object as the first argument. For service workers, you would pass `navigator.serviceWorker.controller` (checking for null beforehand). For web workers, you would pass the instance of your worker.\n\n```ts\n// In your main thread\nimport { sendRequest } from 'worker-request-response';\nimport { yourWorker } from './somewhere';\n\nasync function() {\n  const response = await sendRequest\u003cnumber, string\u003e(\n    yourWorker\n    42,\n  );\n}\n```\n\n### Asynchronous request handler in the worker\n\nThe request handler that you pass into `handleRequestsWith` can be synchronous or asynchronous — in the latter case the resulting promise is awaited before being sent back.\n\n## License\n\nThe source code of this project is distributed under the terms of the ISC license. It's like MIT, but better. [Click here](https://choosealicense.com/licenses/isc/) to learn what that means.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fillright%2Fworker-request-response","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fillright%2Fworker-request-response","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fillright%2Fworker-request-response/lists"}