{"name":"birpc","version":"0.2.8","packageManager":"pnpm@7.29.1","description":"Message based Two-way remote procedure call","author":{"name":"Anthony Fu","email":"anthonyfu117@hotmail.com"},"license":"MIT","funding":"https://github.com/sponsors/antfu","homepage":"https://github.com/antfu/birpc#readme","repository":{"type":"git","url":"git+https://github.com/antfu/birpc.git"},"bugs":{"url":"https://github.com/antfu/birpc/issues"},"keywords":["rpc","messages"],"sideEffects":false,"exports":{".":{"types":"./dist/index.d.ts","require":"./dist/index.cjs","import":"./dist/index.mjs"}},"main":"./dist/index.cjs","module":"./dist/index.mjs","types":"./dist/index.d.ts","devDependencies":{"@antfu/eslint-config":"^0.36.0","@antfu/ni":"^0.20.0","@types/node":"^18.15.0","bumpp":"^9.0.0","eslint":"^8.35.0","esno":"^0.16.3","typescript":"^4.9.5","unbuild":"^1.1.2","vite":"^4.1.4","vitest":"^0.29.2"},"scripts":{"build":"unbuild","dev":"unbuild --stub","lint":"eslint .","release":"bumpp && pnpm publish","start":"esno src/index.ts","test":"vitest"},"_id":"birpc@0.2.8","_integrity":"sha512-ag5M7Ikcnru1XPJy46FdsSmtNzAzeVTcQ5b8ANAG7lBUbY5Ua5RTLnFw6V5ZQ+DToLpgp5LVuA0B2vPwGJ8VMw==","_resolved":"/private/var/folders/30/nymxcyb909ggq2j5lwn7b_600000gn/T/59db280d30626b1d25e01942769fb24b/birpc-0.2.8.tgz","_from":"file:birpc-0.2.8.tgz","_nodeVersion":"18.13.0","_npmVersion":"8.19.3","dist":{"integrity":"sha512-ag5M7Ikcnru1XPJy46FdsSmtNzAzeVTcQ5b8ANAG7lBUbY5Ua5RTLnFw6V5ZQ+DToLpgp5LVuA0B2vPwGJ8VMw==","shasum":"bb7eeebddf8414452d0f13922be3cf3e41c2a111","tarball":"http://registry-npm.ywwl.com/birpc/download/birpc-0.2.8.tgz","fileCount":7,"unpackedSize":20791,"signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEYCIQDs0D8gUOysibBRGT6acGS4QY3UyoQGrBMH8eaVVZ2BqgIhALF5vndJqkTDYP7Y6JkhIYYVlhxAYWqBv2UHWCzcBG8J"}],"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v4.10.10\r\nComment: https://openpgpjs.org\r\n\r\nwsFzBAEBCAAGBQJkEZQOACEJED1NWxICdlZqFiEECWMYAoorWMhJKdjhPU1b\r\nEgJ2VmqGxw//TL25+s7Y2bi5veEy6HO61DzeGtvcghmoMjJnA0G6RUWrjoWh\r\neq0ezp58tiZ52QFcCcmIGKzI7eoxNjD4ZMXYxVQ59Qz5LDtZf6gCB7q1nzfS\r\nhwJpm4TjSepr5gKmGwUml/8rQOAan8aiJlp0maFvtVluk6L7Xo6Gyx0O6S+Q\r\n8cs7+lK/gUcQ9l5TYwwVqfctUocQ6w6tR1hA4z2h3c0B5e46nEW7WVKi9ySz\r\nqGyfy7y4NsJwz98CKwRrOOzYA54P7gPfH/kwEsMSn64YsUVqKlup4JNcZ01K\r\naI6/0pg88MZoL9Epl097o9i1tpvXKYY6ovRR73BhofdP9obfUodLTnOa5YvS\r\nybLgWpMZfayZETCxxb6V2rN64en28aTuAckrr15NdtIHaBZ2986HyH8dZl1c\r\n8z2m81ardh4SbvJADG9JUhfktbHUIsOUBwJcD2GPso34zmj8kL3r5GiyYQ0n\r\ntDpcPzhPIKvrAZEkkcRypwjadj4gd0IZ4HjhQjDd2J84yHW4LqdIiwSH7eIY\r\nY7nv+bVBCp+MswGMsqHWLin4GkrEF64G/7xAw3Zn66UTAqk0ljorpx2pgpeZ\r\n6fl3LJ4H5isSwrOr1dOF6gpnyS6wLO3vNW/TI1S32s3QBk7jMFw8+nlsOJVG\r\nA+T61XOLiOiTHcDvvdgrHDufoTQb/h99WGI=\r\n=j7jq\r\n-----END PGP SIGNATURE-----\r\n","size":4919,"noattachment":false,"key":"/birpc/-/birpc-0.2.8.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.8_1678873614106_0.3716421384964663"},"_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":1678873614273,"_cnpm_publish_time":1678873614273,"dist-tags":{"latest":"4.2.0"}}