{"name":"birpc","version":"0.2.1","description":"Message based Two-way remote procedure call","keywords":["rpc","messages"],"homepage":"https://github.com/antfu/birpc#readme","bugs":{"url":"https://github.com/antfu/birpc/issues"},"license":"MIT","author":"Anthony Fu <anthonyfu117@hotmail.com>","repository":{"type":"git","url":"git+https://github.com/antfu/birpc.git"},"funding":{"url":"https://github.com/sponsors/antfu"},"main":"./dist/index.cjs","module":"./dist/index.mjs","types":"./dist/index.d.ts","exports":{".":{"require":"./dist/index.cjs","import":"./dist/index.mjs","types":"./dist/index.d.ts"}},"sideEffects":false,"devDependencies":{"@antfu/eslint-config":"^0.18.9","@antfu/ni":"^0.13.2","@types/node":"^17.0.23","bumpp":"^7.1.1","eslint":"^8.12.0","esno":"^0.14.1","typescript":"^4.6.3","unbuild":"^0.7.2","vite":"^2.8.6","vitest":"^0.7.11"},"scripts":{"build":"unbuild","dev":"unbuild --stub","lint":"eslint .","release":"bumpp --commit --push --tag && pnpm publish","start":"esno src/index.ts","test":"vitest"},"_id":"birpc@0.2.1","_integrity":"sha512-aH2AwQcsVtEJX76ensLeZwqiUYwv93zfZ6TG+TlyGRSsn4lCihOLPo1//oumH6sboF6scFX/h/3GuCPEG/UMLw==","_resolved":"/Users/antfu/i/birpc/birpc-0.2.1.tgz","_from":"file:birpc-0.2.1.tgz","_nodeVersion":"16.14.2","_npmVersion":"8.5.0","dist":{"integrity":"sha512-aH2AwQcsVtEJX76ensLeZwqiUYwv93zfZ6TG+TlyGRSsn4lCihOLPo1//oumH6sboF6scFX/h/3GuCPEG/UMLw==","shasum":"17f70fc5495cdfe5a00e4cd4866788e21f776865","tarball":"http://registry-npm.ywwl.com/birpc/download/birpc-0.2.1.tgz","fileCount":6,"unpackedSize":13336,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v4.10.10\r\nComment: https://openpgpjs.org\r\n\r\nwsFzBAEBCAAGBQJiP12bACEJED1NWxICdlZqFiEECWMYAoorWMhJKdjhPU1b\r\nEgJ2VmpvDw/+K63NkKXer0PCDIkxlTjrUBVoQ8stWUhfJ60EvU6IJB8YpDyP\r\n7v/FrtGngq55ha1fU2Q5E3QcKOVM8kv/PqL3DW4I76GzKk/nYlrSspquQk6H\r\ncmLhZpOeohJJDRgHdu/B4A/qnNSMGIusW/gJ+FFk7JCCK+xbX1PMgd9Jql4B\r\nRn+LteTygKJwFZ0gWf3h0PEr2uknGgI4TaRkuJGosg31PZ8dAqIIbUYO2DmF\r\n1R6Qtu/o+7+r1hUJOzpzldNfoaQ37lSbyTz++KMWLWNj2npKZPW/R0W5gFs3\r\nPQEhEQP3/k9J87qDROjCwMmFVWnOIpWv7LLAMNjNkNZK5fno31FSx/0foMum\r\n6DRMu50aP5i1lDG+K3EEMRr0fmjismDKNZltOwVpWL5FPVcoKrT2l3XgSsny\r\n4viDEDra7YDttSGbDWj9igdMX5atL7S3F8XdK4LHsJeNMX+r3pod0836pdiU\r\ngUyS6v9fcAxmgv6SHPEIiBRUtO4taZ2NOkF/Krbtlc/oKYSIZRmANPe9hstr\r\nRQmho4MA++a4BNMCwcmTQ7E5M1pvukxZeLBUHcxaAzaD3OPEt9/Fd8A3o5nn\r\nKClxOjq8fYw8LOMV1CmTPSTddGph4i5rO3zrgU+bnPbZ5lC24dajfqKNz5aF\r\nXw3x28jyvh42Q5DS4FOIUP5I61SLv3A5J64=\r\n=s5KM\r\n-----END PGP SIGNATURE-----\r\n","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEYCIQDdI1s8CFy660N4C9g8v8CVrh5sUWqNWd2Wo92SzItTJQIhAO4RvoE57AO75G1RISQI1R80/lIfZqr8rEssmEkMHRcf"}],"size":3850,"noattachment":false,"key":"/birpc/-/birpc-0.2.1.tgz"},"_npmUser":{"name":"antfu","email":"anthonyfu117@hotmail.com"},"directories":{},"maintainers":[{"name":"antfu","email":"anthonyfu117@hotmail.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/birpc_0.2.1_1648319899072_0.24921899189237928"},"_hasShrinkwrap":false,"readme":"# birpc\n\n[![NPM version](https://img.shields.io/npm/v/birpc?color=a1b858&label=)](https://www.npmjs.com/package/birpc)\n\nMessage-based two-way remote procedure call. Useful for WebSockets and Workers communication.\n\n## Features\n\n- Intuitive - call remote functions just like locals, with Promise to get the response\n- TypeScript - safe function calls for arguments and returns\n- Protocol agonostic - WebSocket, MessageChannel, any protocols with messages communication would work!\n- Zero deps, ~0.5KB\n\n## Examples\n\n### Using WebSocket\n\nWhen using WebSocket, you need to pass your custom serializer and deserializer.\n\n#### Client\n\n```ts\nimport type { ServerFunctions } from './types'\n\nconst ws = new WebSocket('ws://url')\n\nconst clientFunctions: ClientFunctions = {\n  hey(name: string) {\n    return `Hey ${name} from client`\n  }\n}\n\nconst rpc = createBirpc<ServerFunctions>(\n  clientFunctions,\n  {\n    post: data => ws.send(data),\n    on: data => ws.on('message', data),\n    // these are required when using WebSocket\n    serialize: v => JSON.stringify(v),\n    deserialize: v => JSON.parse(v),\n  },\n)\n\nawait rpc.hi('Client') // Hi Client from server\n```\n\n#### Server\n\n```ts\nimport { WebSocketServer } from 'ws'\nimport type { ClientFunctions } from './types'\n\nconst serverFunctions: ServerFunctions = {\n  hi(name: string) {\n    return `Hi ${name} from server`\n  }\n}\n\nconst wss = new WebSocketServer()\n\nwss.on('connection', (ws) => {\n  const rpc = createBirpc<ClientFunctions>(\n    serverFunctions,\n    {\n      post: data => ws.send(data),\n      on: data => ws.on('message', data),\n      serialize: v => JSON.stringify(v),\n      deserialize: v => JSON.parse(v),\n    },\n  )\n\n  await rpc.hey('Server') // Hey Server from client\n})\n```\n\n### Circular References\n\nAs `JSON.stringify` does not supporting circular references, we recommend using [`flatted`](https://github.com/WebReflection/flatted) as the serializer when you expect to have circular references.\n\n```ts\nimport { parse, stringify } from 'flatted'\n\nconst rpc = createBirpc<ServerFunctions>(\n  functions,\n  {\n    post: data => ws.send(data),\n    on: data => ws.on('message', data),\n    // use flatted as serializer\n    serialize: v => stringify(v),\n    deserialize: v => parse(v),\n  },\n)\n```\n\n### Using MessageChannel\n\n[MessageChannel](https://developer.mozilla.org/en-US/docs/Web/API/MessageChannel) will automatically serialize the message and support circular references out-of-box.\n\n```ts\nexport const channel = new MessageChannel()\n```\n\n#### Bob\n\n``` ts\nimport type { AliceFunctions } from './types'\nimport { channel } from './channel'\n\nconst Bob: BobFunctions = {\n  hey(name: string) {\n    return `Hey ${name}, I am Bob`\n  }\n}\n\nconst rpc = createBirpc<AliceFunctions>(\n  Bob,\n  {\n    post: data => channel.port1.postMessage(data),\n    on: data => channel.port1.on('message', data),\n  },\n)\n\nawait rpc.hi('Alice') // Hi Bob, I am Alice\n```\n\n#### Alice\n\n``` ts\nimport type { BobFunctions } from './types'\nimport { channel } from './channel'\n\nconst Alice: AliceFunctions = {\n  hi(name: string) {\n    return `Hi ${name}, I am Alice`\n  }\n}\n\nconst rpc = createBirpc<BobFunctions>(\n  Alice,\n  {\n    post: data => channel.port2.postMessage(data),\n    on: data => channel.port2.on('message', data),\n  },\n)\n\nawait rpc.hey('Alice') // Hey Alice, I am Bob\n```\n\n### One-to-multiple Communication\n\nRefer to [./test/group.test.ts](./test/group.test.ts) as an example.\n\n## Sponsors\n\n<p align=\"center\">\n  <a href=\"https://cdn.jsdelivr.net/gh/antfu/static/sponsors.svg\">\n    <img src='https://cdn.jsdelivr.net/gh/antfu/static/sponsors.svg'/>\n  </a>\n</p>\n\n## License\n\n[MIT](./LICENSE) License © 2021 [Anthony Fu](https://github.com/antfu)\n","publish_time":1648319899208,"_cnpm_publish_time":1648319899208,"dist-tags":{"latest":"4.2.0"}}