{"id":16006103,"url":"https://github.com/pfrazee/pauls-electron-rpc","last_synced_at":"2025-09-13T18:32:33.627Z","repository":{"id":9669240,"uuid":"62951449","full_name":"pfrazee/pauls-electron-rpc","owner":"pfrazee","description":"My RPC solution for exporting APIs from the electron background process to renderers and webviews","archived":false,"fork":false,"pushed_at":"2022-11-10T18:00:34.000Z","size":211,"stargazers_count":37,"open_issues_count":8,"forks_count":7,"subscribers_count":3,"default_branch":"master","last_synced_at":"2024-04-07T10:22:36.162Z","etag":null,"topics":["electron"],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/pfrazee.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2016-07-09T13:46:03.000Z","updated_at":"2023-08-16T07:38:43.000Z","dependencies_parsed_at":"2023-01-11T20:13:01.329Z","dependency_job_id":null,"html_url":"https://github.com/pfrazee/pauls-electron-rpc","commit_stats":null,"previous_names":["pfraze/pauls-electron-rpc"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pfrazee%2Fpauls-electron-rpc","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pfrazee%2Fpauls-electron-rpc/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pfrazee%2Fpauls-electron-rpc/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pfrazee%2Fpauls-electron-rpc/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/pfrazee","download_url":"https://codeload.github.com/pfrazee/pauls-electron-rpc/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":232902854,"owners_count":18594353,"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":["electron"],"created_at":"2024-10-08T11:23:28.125Z","updated_at":"2025-01-07T15:50:46.637Z","avatar_url":"https://github.com/pfrazee.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# pauls-electron-rpc\n\nFeatures:\n\n - Supports RPC calls to/from the renderer or webview or a node child-process to the background process\n - Supports methods which return:\n   - Sync values\n   - Async CBs\n   - Promises\n   - Readable streams\n   - Writable streams\n   - Duplex streams\n - Permissions by examining the sender of the call\n - Monitors renderer/webview lifetime to automatically release streams\n - Optional timeout for async methods\n\n## Example usage\n\nIn a shared `example-api-manifest.js`:\n\n```js\nmodule.exports = {\n  // simple method-types\n  readFile: 'async',\n  readFileSync: 'sync',\n  sayHello: 'promise',\n  createReadStream: 'readable',\n  createWriteStream: 'writable',\n  createDuplexStream: 'duplex'\n}\n```\n\nIn the main electron process:\n\n```js\nvar rpc = require('pauls-electron-rpc')\nvar manifest = require('./example-api-manifest')\nvar fs = require('fs')\n\n// export over the 'example-api' channel\nvar api = rpc.exportAPI('example-api', manifest, {\n  // the exported API behaves like normal calls:\n  readFile: fs.readFile,\n  readFileSync: fs.readFileSync,\n  sayHello: () =\u003e return Promise.resolve('hello!'),\n  createReadStream: fs.createReadStream,\n  createWriteStream: /* ... */,\n  createDuplexStream: /* ... */\n})\n\n// log any errors\napi.on('error', console.log)\n```\n\nIn the renderer or webview process:\n\n```js\nvar rpc = require('pauls-electron-rpc')\nvar manifest = require('./example-api-manifest')\n\n// import over the 'example-api' channel\nvar api = rpc.importAPI('example-api', manifest, { timeout: 30e3 })\n\n// now use, as usual:\napi.readFileSync('/etc/hosts') // =\u003e '...'\n```\n\n## API\n\n### rpc.exportAPI(channelName, manifest, methods, [globalPermissionCheck])\n\nMethods will be called with a `this` set to the `event` object from [electron ipc](http://electron.atom.io/docs/api/ipc-main/#event-object).\nDon't touch `returnValue`.\n\nYou can optionally specify a method for `globalPermissionCheck` with the following signature:\n\n```js\nfunction globalPermissionCheck (event, methodName, args) {\n  if (event.sender.getURL() != 'url-I-trust') return false\n  return true\n}\n```\n\nIf `globalPermissionCheck` is specified, and does not return true, the method call will respond with a 'Denied' error.\n\n### rpc.importAPI(channelName, manifest [,options])\n\n - `options.timeout` Number. Specify how long in ms that async methods wait before erroring. Set to `false` to disable timeout.\n - `options.errors` Object. Provides custom error constructors.\n - `options.wc` WebContents. The web-contents that is exporting the API. Use this when importing an API from a webContents into the main thread.\n - `options.proc` ChildProcess. The child-process that is exporting the API. Use this when importing an API from a node child process into the main thread.\n\n## Readable Streams\n\nReadable streams in the clientside are given a `.close()` method.\nAll serverside streams MUST implement `.close()` or `.destroy()`, either of which will be called.\n\nStream methods can return a promise that resolves to a stream.\n\n## Buffers and ArrayBuffers\n\nArguments and return values are massaged so that they are Buffers on the exporter's side, and ArrayBuffers on the importer side.\n\n## Custom Errors\n\n```js\n// shared code\n// =\n\nvar manifest = {\n  testThrow: 'promise'\n}\n\nclass MyCustomError extends Error {\n  constructor() {\n    super()\n    this.name = 'MyCustomError'\n    this.message = 'Custom error!'\n  }\n}\n\n// server\n// =\n\nrpc.exportAPI('error-api', manifest, {\n  testThrow() {\n    return Promise.reject(new MyCustomError())\n  }\n})\n\n// client\n// =\n\nvar rpcClient = rpc.importAPI('error-api', manifest, {\n  errors: {MyCustomError} // pass in custom error constructors\n})\nrpcClient.testThrow().catch(console.log) // =\u003e MyCustomError\n```","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpfrazee%2Fpauls-electron-rpc","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fpfrazee%2Fpauls-electron-rpc","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpfrazee%2Fpauls-electron-rpc/lists"}