{"_id":"@cruise-automation/rpc","_rev":"5-e8b75b02466b8b51f8a7d32977f5f5db","name":"@cruise-automation/rpc","dist-tags":{"latest":"0.0.2"},"versions":{"0.0.1":{"name":"@cruise-automation/rpc","version":"0.0.1","description":"Add RPC to WebWorkers with transferrable object support","main":"lib/index.js","license":"Apache-2.0","scripts":{"build-dev":"BABEL_ENV=$NODE_ENV babel src/index.js --out-dir lib --copy-files --config-file ../../../babel.config.js","build":"NODE_ENV=production npm run build-dev && flow-copy-source -v src lib --ignore '*.test.*'","watch":"NODE_ENV=development npm run build-dev --watch"},"keywords":["WebWorker","rpc"],"publishConfig":{"access":"public"},"_id":"@cruise-automation/rpc@0.0.1","_npmVersion":"6.4.1","_nodeVersion":"10.14.2","_npmUser":{"name":"brianc","email":"brian.m.carlson@gmail.com"},"dist":{"integrity":"sha512-pEm0X0t8jwcAHSeh6Z7utMBPKSM7PVZ2I/P08X2m3QnRWctvy0XO/8sa1SqxDh31D4USXNN13es5inf8qtl6Og==","shasum":"20da48825a7ea0f33457a166a219b6683d893236","tarball":"https://registry.npmjs.org/@cruise-automation/rpc/-/rpc-0.0.1.tgz","fileCount":6,"unpackedSize":24398,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.4\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJcS3ttCRA9TVsSAnZWagAAoyYP+waYxNV/kGV6XyrdQKBk\nPpzUov/fYvp3GRX6mgR562FgxovvQkbvjuA08lAHRlm/mCobnQWshE5DX//E\nlA74ha5cVBVG2wJhcXdOqa18f4BfszHqmgebTUSrTmAi73w59wu/ZKYhBILE\nO8P4tAw4NIp2vgoHJTYIxXqcgkEETBpi5zXOTdLspDIoZI7Bvyr8xCVisfb4\nt3qaj4jXVlD7FBOjwKYV1zOxsd4BMnQf/Jdil6/dML+gOadYrvlXoBl1cElG\nzTdL4s87tiiKYdiKfrOGdRVJFBpZIN470JyOyVX6+aXb+6rrbfAV6V6wKDiK\n7uVBFykU2uV5f8+mKoeKIKa0fg5Cvx+GS7UjVd/9Wtn4JvgZreIOKDLi8zDx\nia3+13LWac8wN702DDPooo7f0R/oBsjSIQEXX8YAe257F+6617Vb2Uuo3RQu\nxBU1kPPhDi8y5NGtDSfp+/giDc4lCblNgDsKIr6+AedI/KpXDJoAfsLPhhYd\n5XPW7oRjx/xBSk/Jb+alTwn/Dtb3qKeWC3ddnJmb+vXSjuFFtlN3WdsYlvOz\nDBW3KL1BmukQAPHMwkH9bYbje153R2xxBEJaBPypRFOpo4NSKkJgVhOINYnu\n3xE2cMNqfGXYq47/FQ5X9uu529tHVX5qqtTmGoljZzyq2oSAs43IfupUJEci\n/38n\r\n=sp21\r\n-----END PGP SIGNATURE-----\r\n","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEUCIQC3Uy2HirrGp+pHj0rgqAonVmCX3HIhpTMhiBQ+ZSwYRwIgFZN8cCxUbG7gFT/BqCurkVDJximlmzO3bjUK/08Gt5E="}]},"maintainers":[{"name":"audreyli","email":"me@audreyli.me"},{"name":"jtbandes","email":"jacob@bandes-stor.ch"},{"name":"brianc","email":"brian.m.carlson@gmail.com"},{"name":"janpaul123","email":"me@janpaulposma.nl"}],"directories":{},"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/rpc_0.0.1_1548450668957_0.28769407278188774"},"_hasShrinkwrap":false},"0.0.2":{"name":"@cruise-automation/rpc","version":"0.0.2","description":"Add RPC to WebWorkers with transferrable object support","main":"lib/index.js","license":"Apache-2.0","scripts":{"build-dev":"BABEL_ENV=$NODE_ENV babel src/index.js --out-dir lib --copy-files --config-file ../../../babel.config.js","build":"NODE_ENV=production npm run build-dev && flow-copy-source -v src lib --ignore '*.test.*'","watch":"NODE_ENV=development npm run build-dev --watch"},"keywords":["WebWorker","rpc"],"publishConfig":{"access":"public"},"_id":"@cruise-automation/rpc@0.0.2","_npmVersion":"6.4.1","_nodeVersion":"10.14.2","_npmUser":{"name":"brianc","email":"brian.m.carlson@gmail.com"},"dist":{"integrity":"sha512-aD/PbMBKe8FSMovh9Q/9pCgll4SuQsm0pWlNBXkoCvfKyaLIpohtnNJUkdrjBQu384pXv/l/Au1Xhyvq2TJJRg==","shasum":"287248f2e7786fdef2bd67c171f066fb5b2ab9a6","tarball":"https://registry.npmjs.org/@cruise-automation/rpc/-/rpc-0.0.2.tgz","fileCount":6,"unpackedSize":24345,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.4\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJcS3+KCRA9TVsSAnZWagAAwOYP/2aN5vSB1YSBSVNqTqJx\neIlwmjt/up1Lfod40kw66p0Zgqqnp8gR2DalpVaGD8Q29eNUqUP5xtMG0Vtg\nPGbFRR6UX+htcttHUbxNdi2mI3NTClpQ2o+AKKIDCoCg4qFA6Mjygoao7xYw\nkrJ1SDEhoybSyzId4dMp+fmrIr+FWGTqMe3qFAElxmelfIp6SJugaEh729fU\nIcqA/XwHlRhJCKkTt1+L3lBMO86n9XElflJKM2SRSozwu384kxDs89rHMIXt\nGsnEgOQRYEYCKYivnkF52SiUG/rziWOd84O2shO8bdVtPSWdrjPNWJMxpTpW\nM3/4cHl+xZhwbdBVAEYt6P1Y3vMR49EJa7nqsA5XEjJ0uQSaObZpFgAm/XG7\nKnEFjgJBHrNPK/e9MwXXbcc5wfCjuS7hJVma3wc7sWRXgGUSWHA5jU5R2c5J\nQ5ACh9vxNW5f4XZovOUNs141a+XNclmlKlu3VoHg0bG58NXHpA9993LvYhzN\nI1j4iWZsPn89PX/4l7Bb2xJk7TB/XDafwDVuevyqnjbiRaLg/CvN60y/557l\nIUU+ywR/lzhQQqEi7K6nFITovQzV94WaloEh3C4LTYn3+pcEHjxU/DVSijBl\n66mKQLBIKvdEmYgfz3Chy9HudunNgvFitbkbRKaKjjtnyxHQ/+5OezHPcX/n\nS7ff\r\n=P9Na\r\n-----END PGP SIGNATURE-----\r\n","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEQCIHzKXa/srkM/rZs4WO4JfQUv5Qgs04gVjp0B62VAVydXAiAuORs9IKn2JzAMrAdx/Itn88Cx3YH3/+XI/SRwlD5AeA=="}]},"maintainers":[{"name":"audreyli","email":"me@audreyli.me"},{"name":"brianc","email":"brian.m.carlson@gmail.com"},{"name":"janpaul123","email":"me@janpaulposma.nl"},{"name":"jtbandes","email":"jacob@bandes-stor.ch"}],"directories":{},"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/rpc_0.0.2_1548451722418_0.2831136454773022"},"_hasShrinkwrap":false}},"time":{"created":"2019-01-25T21:11:08.786Z","0.0.1":"2019-01-25T21:11:09.042Z","modified":"2022-04-05T01:57:47.004Z","0.0.2":"2019-01-25T21:28:42.541Z"},"maintainers":[{"email":"hassoncs@gmail.com","name":"hassoncs"},{"email":"me@janpaulposma.nl","name":"janpaul123"},{"email":"me@audreyli.me","name":"audreyli"}],"description":"Add RPC to WebWorkers with transferrable object support","keywords":["WebWorker","rpc"],"license":"Apache-2.0","readme":"# @cruise-automation/rpc\n\nAn RPC layer to make it easier to communicate between a WebWorker and the main thread.  Has support for sending and responding with [transferable](https://developer.mozilla.org/en-US/docs/Web/API/Transferable) objects to avoid structured cloning of large array buffers.  It also propagates errors thrown in receivers back to the calling thread.\n\n## Example\n\n```js\n// worker.js\nimport Rpc from '@cruise-automation/rpc'\n\nconst rpc = new Rpc(global);\n\nrpc.receive('message', async ({ url }) => {\n  const res = await fetch(url);\n  if (!res.ok) {\n    throw new Error('Bad response ' + res.status);\n  }\n  const json = await res.json()\n  return { body: json }\n});\n\n```\n\n```js\n// ui-thread.js\nconst worker = new WebWorker('./worker.js')\nconst rpc = new Rpc(worker);\nrpc.send('message', { url }).then(({ body }) => {\n  console.log('I got a response', body);\n});\n\n```\n\n## API\n\nThe `Rpc` constructor takes a [`MessagePort`](https://developer.mozilla.org/en-US/docs/Web/API/MessagePort) as its constructor argument.  In a `WebWorker` you generally would use `global` and on the UI thread you would use the instance of the `WebWorker` as the `MessagePort`.\n\n### `rpc.send<TResult>(topic: string, data: any, transferables: Transferable[]): Promise<TResult>`\n\nThe `send` method takes a topic name and any data.  This data is sent over the `MessagePort` and can be received on the other end with a registered `rpc.receive()` receiver on the same topic.  You may also specify an optional array of [transferable](https://developer.mozilla.org/en-US/docs/Web/API/Transferable) objects.  This returns a promise which resolves with whatever the handler registered on `rpc.receive` returns.\n\n```js\nconst rpc = new Rpc(new WebWorker('/worker-script.js'))\n\nrpc.send('fetch-and-parse', { url: '/lots-of-binary-data' }).then(({ result }) => {\n  console.log(result);\n});\n```\n\n### `rpc.receive<T, TOut>(topic: string, handler: (T) => TOut): void`\n\nThe `receive` method registers a function to be called whenever a message is received on the specified topic.  This function's return value can be waited on by a promise from the caller.  To return an object with a list of [transferable](https://developer.mozilla.org/en-US/docs/Web/API/Transferable) objects in the graph you can add the list with a special key to the response from your receiver.\n\n```js\n// worker-script.js\nrpc.receive('fetch-and-parse', async ({ url }) => {\n  const res = await fetch(url);\n  if (!res.ok) {\n    throw new Error('Bad response ' + res.status);\n  }\n  const arrayBuffer = await res.arrayBuffer();\n  const result = doLongRunningParseOperation(arrayBuffer);\n  return {\n    result,\n    [Rpc.transferables]: [result]\n  }\n});\n```\n\nIf the `handler` throws or rejects the error message will be sent through the `MessagePort` and calling thread's promise will reject.\n\n### `Rpc.transferable`\n\nThis is a static property on the `Rpc` class that contains the magic string you must use as a key when responding to a message in a receiver and attaching transferables to the response.\n","readmeFilename":"README.md"}